diff --git a/Bootstrapping/Events/UpdateTransactionsSubscriber.php b/Bootstrapping/Events/UpdateTransactionsSubscriber.php index 83e1b405..31368bc8 100644 --- a/Bootstrapping/Events/UpdateTransactionsSubscriber.php +++ b/Bootstrapping/Events/UpdateTransactionsSubscriber.php @@ -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', ]; } @@ -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'; @@ -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; } /** @@ -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'; @@ -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'); + } } diff --git a/Component/Model/ShopwareCustomerWrapper.php b/Component/Model/ShopwareCustomerWrapper.php index 573660f6..df039722 100644 --- a/Component/Model/ShopwareCustomerWrapper.php +++ b/Component/Model/ShopwareCustomerWrapper.php @@ -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(); } @@ -67,6 +72,11 @@ 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(); } @@ -74,7 +84,6 @@ public function getBilling($property = null) $getter = 'get' . ucfirst($property); $billingFresh = $this->getBillingFresh(); - if (!is_null($billingFresh)) { if (Util::existsAndNotEmpty($billingFresh, $getter)) { return $billingFresh->$getter(); diff --git a/README.md b/README.md index 6a41ba52..d6eb661d 100755 --- a/README.md +++ b/README.md @@ -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 | integration@ratepay.com |Full Documentation | https://ratepay.gitbook.io/shopware/ @@ -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 diff --git a/plugin.json b/plugin.json index 0052b3c0..edc2198a 100644 --- a/plugin.json +++ b/plugin.json @@ -10,7 +10,7 @@ "author": "RatePAY GmbH", "supplier": "RatePAY GmbH", "description": "

RatePAY Payment plugin for Shopware Community Edition Version 5

", - "currentVersion": "5.2.5", + "currentVersion": "5.2.6", "payment_confirm": true, "compatibility": { "minimumVersion": "5.0.0",