Skip to content

Commit

Permalink
Merge branch 'release/5.2.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriarte-mendez committed Oct 1, 2018
2 parents ac6b005 + a7a5e38 commit cd7157b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 27 deletions.
105 changes: 82 additions & 23 deletions Bootstrapping/Events/UpdateTransactionsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ class UpdateTransactionsSubscriber implements \Enlight\Event\SubscriberInterface
{
const JOB_NAME = 'Shopware_Cronjob_UpdateRatepayTransactions';

/**
* @var string
*/
protected $__cronjobLastExecutionDate;

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
self::JOB_NAME => 'updateRatepayTransactions',
'UpdateRatepayTransactions' => 'updateRatepayTransactions',
];
}

Expand All @@ -27,22 +36,32 @@ public function updateRatepayTransactions(\Shopware_Components_Cron_CronJob $job
{
$config = Shopware()->Plugins()->Frontend()->RpayRatePay()->Config();

if (!$config->get('RatePayBidirectional')) {
return 'Bidrectionality is turned off.';
if (!$this->hasBiDirectionalityActivated($config)) {
Logger::singleton()->info('RatePAY bidirectionality is turned off.');
return 'RatePAY bidirectionality is turned off.';
}

try {
$orderIds = $this->findCandidateOrdersForUpdate($config);
$totalOrders = count($orderIds);
$orderProcessor = new \Shopware_Plugins_Frontend_RpayRatePay_Component_Service_OrderStatusChangeHandler();
foreach ($orderIds as $orderId) {
foreach ($orderIds as $key => $orderId) {
/* @var \Shopware\Models\Order\Order $order */
$order = Shopware()->Models()->find('Shopware\Models\Order\Order', $orderId);
Logger::singleton()->info(
sprintf(
'[%d/%d] Processing order %d ...notify needed updated to RatePAY',
($key + 1),
$totalOrders,
$orderId
)
);
$orderProcessor->informRatepayOfOrderStatusChange($order);
}
} catch (\Exception $e) {
Logger::singleton()->error('Fehler UpdateTransactionsSubscriber: ' .
$e->getMessage() . ' ' .
$e->getTraceAsString());
Logger::singleton()->error(
sprintf('Fehler UpdateTransactionsSubscriber: %s %s', $e->getMessage(), $e->getTraceAsString())
);
return $e->getMessage();
}
return 'Success';
Expand All @@ -53,8 +72,17 @@ public function updateRatepayTransactions(\Shopware_Components_Cron_CronJob $job
*/
private function getLastUpdateDate()
{
$query = 'SELECT `end` FROM s_crontab WHERE `action` = ?';
return Shopware()->Db()->fetchOne($query, [self::JOB_NAME]);
if (empty($this->_cronjobLastExecutionDate)) {
$query = 'SELECT `next`, `interval` FROM s_crontab WHERE `action` = ?';
$row = Shopware()->Db()->fetchRow($query, [self::JOB_NAME]);

$date = new \DateTime($row['next']);
$date->sub(new \DateInterval('PT' . $row['interval'] . 'S'));

$this->_cronjobLastExecutionDate = $date;
}

return $this->_cronjobLastExecutionDate;
}

/**
Expand All @@ -64,30 +92,18 @@ private function getLastUpdateDate()
*/
private function findCandidateOrdersForUpdate($config)
{
$paymentMethods = \Shopware_Plugins_Frontend_RpayRatePay_Bootstrap::getPaymentMethods();
$orderStatus = [
$config['RatePayFullDelivery'],
$config['RatePayFullCancellation'],
$config['RatePayFullReturn'],
];

$paymentMethodsWrapped = [];
foreach ($paymentMethods as $paymentMethod) {
$paymentMethodsWrapped[] = "'{$paymentMethod}'";
}

$changeDate = $this->getLastUpdateDate();

if (empty($changeDate)) {
$date = new \DateTime();
$date->sub(new \DateInterval('PT1H'));
$changeDate = $date->format('Y-m-d H:i:s');
}
$paymentMethods = $this->getAllowedPaymentMethods();
$changeDate = $this->getChangeDateLimit();

$query = 'SELECT o.id FROM s_order o
INNER JOIN s_order_history oh ON oh.orderID = o.id
LEFT JOIN s_core_paymentmeans cp ON cp.id = o.paymentID
WHERE cp.name in (' . join(',', $paymentMethodsWrapped) . ')
WHERE cp.name in (' . join(',', $paymentMethods) . ')
AND o.status in (' . join(',', $orderStatus) . ')
AND oh.change_date >= :changeDate
GROUP BY o.id';
Expand All @@ -96,4 +112,47 @@ private function findCandidateOrdersForUpdate($config)

return array_column($rows, 'id');
}

/**
* Gets the bottom limits to fetch order updates.
* This is important to keep a well performing process, due to
* an unknown amount of orders could take a long of time.
*
* @return string
* @throws \Exception
*/
private function getChangeDateLimit()
{
$date = $this->getLastUpdateDate();
if (empty($date)) {
$date = new \DateTime();
}

$date->sub(new \DateInterval('PT1H'));
$changeDate = $date->format('Y-m-d H:i:s');

return $changeDate;
}

/**
* @return mixed
*/
private function getAllowedPaymentMethods()
{
$paymentMethods = \Shopware_Plugins_Frontend_RpayRatePay_Bootstrap::getPaymentMethods();
$quotedPaymentMethods = array_map(function ($method) {
return "'" . $method . "'";
}, $paymentMethods);

return $quotedPaymentMethods;
}

/**
* @param $config
* @return bool
*/
private function hasBiDirectionalityActivated($config)
{
return (bool)$config->get('RatePayBidirectional');
}
}
11 changes: 10 additions & 1 deletion Component/Model/ShopwareCustomerWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function __construct(Customer $customer, $em)
*/
public function getShipping($property = null)
{
$shippingId = Shopware()->Session()->offsetGet('checkoutShippingAddressId');
if (!empty($shippingId)) {
return Shopware()->Models()->find('Shopware\Models\Customer\Address', $shippingId);
}

if (is_null($property)) {
return $this->getShippingChaotic();
}
Expand Down Expand Up @@ -67,14 +72,18 @@ public function getShipping($property = null)
*/
public function getBilling($property = null)
{
$billingId = Shopware()->Session()->offsetGet('checkoutBillingAddressId');
if (!empty($billingId)) {
return Shopware()->Models()->find('Shopware\Models\Customer\Address', $billingId);
}

if (is_null($property)) {
return $this->getBillingChaotic();
}

$getter = 'get' . ucfirst($property);

$billingFresh = $this->getBillingFresh();

if (!is_null($billingFresh)) {
if (Util::existsAndNotEmpty($billingFresh, $getter)) {
return $billingFresh->$getter();
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
|------|----------
|Author | Annegret Seufert
|Shop Version | `5.0.x` `5.1.x` `5.2.x` `5.3.x`
|Version | `5.2.5`
|Version | `5.2.6`
|Link | http://www.ratepay.com
|Mail | [email protected]
|Full Documentation | https://ratepay.gitbook.io/shopware/
Expand All @@ -24,7 +24,11 @@

## Changelog

### Version 5.2.5 - Released 2018-19-20
### Version 5.2.6 - Released 2018-10-01
* fix ignored updates in bidirectional process
* fix usage of addresses for shipping/billing during checkout

### Version 5.2.5 - Released 2018-09-20
* fix cronjob duplications during update
* fix global namespacing to php and shopware classes
* fix uninstall routine to remove menues
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "RatePAY GmbH",
"supplier": "RatePAY GmbH",
"description": "<h2>RatePAY Payment plugin for Shopware Community Edition Version 5</h2><ul><li style='list-style: inherit;'>RatePAY Payment Module</li><li style='list-style: inherit;'>Payment means: Invoice, Direct Debit (ELV), Rate</li><li style='list-style: inherit;'>Cancellations, Returns, etc. can be created from an additional tab in the order detail view</li><li style='list-style: inherit;'>Integrated support for multishops</li><li style='list-style: inherit;'>Improved payment form with visual feedback for your customers</li><li style='list-style: inherit;'>Supported Languages: German, English</li><li style='list-style: inherit;'>Backend Log with custom View accessible from your shop backend</li></ul>",
"currentVersion": "5.2.5",
"currentVersion": "5.2.6",
"payment_confirm": true,
"compatibility": {
"minimumVersion": "5.0.0",
Expand Down

0 comments on commit cd7157b

Please sign in to comment.