From 64ff8c57db76e1f0df7ed6d27ae5dce67cd35d5b Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Thu, 26 Sep 2024 15:45:25 +0200 Subject: [PATCH] NTR: PISHPS-360: fix order expiration (#845) Co-authored-by: Vitalij Mik # Conflicts: # src/Setting/MollieSettingStruct.php --- .../OrderExpiration/ExpireAction.php | 105 ++++++++++++++++++ .../OrderExpiration/ExpireActionResult.php | 8 ++ src/Resources/config/config.xml | 10 ++ .../config/services/scheduled_tasks.xml | 3 +- src/Resources/config/services/services.xml | 7 ++ .../OrderStatus/ExpireOrderTaskHandler.php | 54 ++------- src/Setting/MollieSettingStruct.php | 16 +++ 7 files changed, 158 insertions(+), 45 deletions(-) create mode 100644 src/Components/OrderExpiration/ExpireAction.php create mode 100644 src/Components/OrderExpiration/ExpireActionResult.php diff --git a/src/Components/OrderExpiration/ExpireAction.php b/src/Components/OrderExpiration/ExpireAction.php new file mode 100644 index 000000000..edd47656d --- /dev/null +++ b/src/Components/OrderExpiration/ExpireAction.php @@ -0,0 +1,105 @@ +orderRepository = $orderRepository; + $this->salesChannelRepository = $salesChannelRepository; + $this->orderExpireService = $orderExpireService; + $this->settingsService = $settingsService; + $this->logger = $logger; + } + + public function expireOrders(Context $context): void + { + $this->logger->debug('Start resetting in_progress orders'); + + $salesChannelsCriteria = new Criteria(); + $salesChannelsCriteria->addFilter(new EqualsFilter('active', true)); + + $salesChannels = $this->salesChannelRepository->search($salesChannelsCriteria, $context); + + if ($salesChannels->count() === 0) { + $this->logger->debug('No sales channels found'); + return; + } + + /** + * @var SalesChannelEntity $salesChannel + */ + foreach ($salesChannels->getIterator() as $salesChannel) { + $this->expireOrdersInSalesChannel($salesChannel, $context); + } + } + + private function expireOrdersInSalesChannel(SalesChannelEntity $salesChannelEntity, Context $context): void + { + $settings = $this->settingsService->getSettings($salesChannelEntity->getId()); + + if ($settings->isAutomaticOrderExpire() === false) { + $this->logger->debug('Automatic order expire is disabled for this saleschannel', ['salesChannel' => $salesChannelEntity->getName()]); + return; + } + + $this->logger->info('Start expire orders for saleschannel', ['salesChannel' => $salesChannelEntity->getName()]); + + $date = new \DateTime(); + $date->modify('-2 months'); + + $criteria = new Criteria(); + $criteria->addAssociation('transactions.stateMachineState'); + $criteria->addFilter(new EqualsFilter('transactions.stateMachineState.technicalName', OrderTransactionStates::STATE_IN_PROGRESS)); + $criteria->addFilter(new EqualsFilter('salesChannelId', $salesChannelEntity->getId())); + $criteria->addFilter(new RangeFilter('orderDateTime', [RangeFilter::GTE => $date->format(Defaults::STORAGE_DATE_TIME_FORMAT)])); + $criteria->addSorting(new FieldSorting('orderDateTime', FieldSorting::DESCENDING)); + $criteria->setLimit(10); + + $this->logger->debug('Search for orders with payment status in progress'); + + $searchResult = $this->orderRepository->search($criteria, $context); + if ($searchResult->count() === 0) { + $this->logger->debug('No orders found with payment status in progress'); + return; + } + + $this->logger->info('Found orders which are in progress', ['foundOrders' => $searchResult->count()]); + + /** + * @var OrderCollection $orders + */ + $orders = $searchResult->getEntities(); + $expiredOrders = $this->orderExpireService->cancelExpiredOrders($orders, $context); + + $this->logger->info('Expired orders with status in progress', ['expiredOrders' => $expiredOrders]); + } +} diff --git a/src/Components/OrderExpiration/ExpireActionResult.php b/src/Components/OrderExpiration/ExpireActionResult.php new file mode 100644 index 000000000..d49f877da --- /dev/null +++ b/src/Components/OrderExpiration/ExpireActionResult.php @@ -0,0 +1,8 @@ +Wenn aktiviert, storniert das Plugin Mollie-Bestellungen automatisch, wenn sie in Shopware storniert werden. Nur Bestellungen, die erstellt, autorisiert oder teilweise versandt wurden, werden storniert. Bei teilweisem Versand werden nur Artikel storniert, die nicht versendet wurden. Indien ingeschakeld, zal de plug-in automatisch Mollie-bestellingen annuleren wanneer ze worden geannuleerd in Shopware. Alleen bestellingen die zijn aangemaakt, geautoriseerd of gedeeltelijk zijn verzonden, worden geannuleerd. Indien gedeeltelijk verzonden, worden alleen artikelen geannuleerd die niet zijn verzonden. + + automaticOrderExpire + false + + + + If enabled, the plugin will automatically expire orders with the payment status "In Progress". + Wenn aktiviert, lässt das Plugin Bestellungen mit dem Status „In Bearbeitung“ und dem Zahlungsstatus „Offen“ oder „Autorisiert“ automatisch ablaufen. + Als deze optie is ingeschakeld, verlopen bestellingen met de status 'In uitvoering' en de betalingsstatus 'Open' of 'Geautoriseerd' automatisch. + Refund Manager diff --git a/src/Resources/config/services/scheduled_tasks.xml b/src/Resources/config/services/scheduled_tasks.xml index 062ac68c2..aa0a190d7 100755 --- a/src/Resources/config/services/scheduled_tasks.xml +++ b/src/Resources/config/services/scheduled_tasks.xml @@ -35,8 +35,7 @@ - - + diff --git a/src/Resources/config/services/services.xml b/src/Resources/config/services/services.xml index ddfc61aea..f9ea177d3 100644 --- a/src/Resources/config/services/services.xml +++ b/src/Resources/config/services/services.xml @@ -208,6 +208,13 @@ + + + + + + + diff --git a/src/ScheduledTask/OrderStatus/ExpireOrderTaskHandler.php b/src/ScheduledTask/OrderStatus/ExpireOrderTaskHandler.php index 7f4969368..f8af248c2 100644 --- a/src/ScheduledTask/OrderStatus/ExpireOrderTaskHandler.php +++ b/src/ScheduledTask/OrderStatus/ExpireOrderTaskHandler.php @@ -3,64 +3,32 @@ namespace Kiener\MolliePayments\ScheduledTask\OrderStatus; -use Kiener\MolliePayments\Repository\Order\OrderRepositoryInterface; +use Kiener\MolliePayments\Components\OrderExpiration\ExpireAction; use Kiener\MolliePayments\Repository\ScheduledTask\ScheduledTaskRepositoryInterface; -use Kiener\MolliePayments\Service\Order\OrderExpireService; use Psr\Log\LoggerInterface; -use Shopware\Core\Checkout\Order\OrderCollection; -use Shopware\Core\Checkout\Order\OrderStates; use Shopware\Core\Framework\Api\Context\SystemSource; use Shopware\Core\Framework\Context; -use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; -use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; -use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting; use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler; #[\Symfony\Component\Messenger\Attribute\AsMessageHandler(handles: ExpireOrderTask::class)] class ExpireOrderTaskHandler extends ScheduledTaskHandler { - private LoggerInterface $logger; - private OrderRepositoryInterface $orderRepository; - private OrderExpireService $orderExpireService; - - public function __construct(ScheduledTaskRepositoryInterface $scheduledTaskRepository, OrderRepositoryInterface $orderRepository, OrderExpireService $orderExpireService, LoggerInterface $logger) - { - /** @phpstan-ignore-next-line */ + private ExpireAction $expireAction; + + public function __construct( + ScheduledTaskRepositoryInterface $scheduledTaskRepository, + ExpireAction $expireAction, + LoggerInterface $logger + ) { + /** @phpstan-ignore-next-line */ parent::__construct($scheduledTaskRepository->getRepository(), $logger); - $this->logger = $logger; - $this->orderRepository = $orderRepository; - $this->orderExpireService = $orderExpireService; + $this->expireAction = $expireAction; } public function run(): void { - $this->logger->debug('Start resetting in_progress orders'); - $context = new Context(new SystemSource()); - - $criteria = new Criteria(); - $criteria->addAssociation('transactions.stateMachineState'); - $criteria->addFilter(new EqualsFilter('transactions.stateMachineState.technicalName', OrderStates::STATE_IN_PROGRESS)); - $criteria->addSorting(new FieldSorting('orderDateTime', FieldSorting::DESCENDING)); - $criteria->setLimit(10); - - $this->logger->debug('Search for orders which are in progress state'); - - $searchResult = $this->orderRepository->search($criteria, $context); - if ($searchResult->count() === 0) { - $this->logger->debug('No in progress orders found'); - return; - } - - $this->logger->info('Found orders which are in progress', ['foundOrders' => $searchResult->count()]); - - /** - * @var OrderCollection $orders - */ - $orders = $searchResult->getEntities(); - $resetted = $this->orderExpireService->cancelExpiredOrders($orders, $context); - - $this->logger->info('Rested expired orders', ['restedOrders' => $resetted]); + $this->expireAction->expireOrders($context); } /** diff --git a/src/Setting/MollieSettingStruct.php b/src/Setting/MollieSettingStruct.php index cfa7eff52..c6fc9aec2 100644 --- a/src/Setting/MollieSettingStruct.php +++ b/src/Setting/MollieSettingStruct.php @@ -265,6 +265,11 @@ class MollieSettingStruct extends Struct */ protected $paymentFinalizeTransactionTime; + + /** + * @var bool + */ + protected $automaticOrderExpire = false; /** * @return string */ @@ -1013,4 +1018,15 @@ public function setPaymentFinalizeTransactionTime(int $paymentFinalizeTransactio { $this->paymentFinalizeTransactionTime = $paymentFinalizeTransactionTime; } + + + public function isAutomaticOrderExpire(): bool + { + return $this->automaticOrderExpire; + } + + public function setAutomaticOrderExpire(bool $automaticOrderExpire): void + { + $this->automaticOrderExpire = $automaticOrderExpire; + } }