diff --git a/mollie.php b/mollie.php index 8fea882c7..de16d96ce 100755 --- a/mollie.php +++ b/mollie.php @@ -1303,8 +1303,6 @@ public function hookActionCarrierUpdate(array $params): void $logger->error('Failed to update subscription carrier for all orders.', [ 'failed_subscription_order_ids' => json_encode($failedSubscriptionOrderIdsToUpdate), ]); - - // TODO maybe notification redirect with failed orders to update } public function hookActionFrontControllerAfterInit(): void diff --git a/subscription/Controller/Symfony/SubscriptionController.php b/subscription/Controller/Symfony/SubscriptionController.php index 9ff6a4037..7dc177088 100644 --- a/subscription/Controller/Symfony/SubscriptionController.php +++ b/subscription/Controller/Symfony/SubscriptionController.php @@ -15,11 +15,15 @@ namespace Mollie\Subscription\Controller\Symfony; use Exception; +use Mollie\Adapter\ConfigurationAdapter; use Mollie\Adapter\Shop; +use Mollie\Config\Config; +use Mollie\Logger\PrestaLoggerInterface; use Mollie\Subscription\Exception\SubscriptionApiException; use Mollie\Subscription\Filters\SubscriptionFilters; use Mollie\Subscription\Grid\SubscriptionGridDefinitionFactory; use Mollie\Subscription\Handler\SubscriptionCancellationHandler; +use Mollie\Subscription\Handler\UpdateSubscriptionCarrierHandler; use Mollie\Utility\PsVersionUtility; use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface; use PrestaShop\PrestaShop\Core\Grid\GridFactoryInterface; @@ -96,9 +100,9 @@ public function submitOptionsAction(Request $request): RedirectResponse return $this->redirectToRoute('admin_subscription_index'); } - $formHandler->save($form->getData()); + $this->updateSubscriptionCarrier($form->getData()['carrier']); - // TODO implement carrier handler update here or somewhere in inner class + $formHandler->save($form->getData()); $this->addFlash( 'success', @@ -108,6 +112,34 @@ public function submitOptionsAction(Request $request): RedirectResponse return $this->redirectToRoute('admin_subscription_index'); } + private function updateSubscriptionCarrier(int $newCarrierId): void + { + /** @var ConfigurationAdapter $configuration */ + $configuration = $this->module->getService(ConfigurationAdapter::class); + $oldCarrierId = $configuration->get(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID); + + if (empty($oldCarrierId) || empty($newCarrierId)) { + $this->addFlash( + 'error', + $this->module->l('Carrier not found', self::FILE_NAME) + ); + } + + /** @var UpdateSubscriptionCarrierHandler $subscriptionCarrierUpdateHandler */ + $subscriptionCarrierUpdateHandler = $this->module->getService(UpdateSubscriptionCarrierHandler::class); + + /** @var PrestaLoggerInterface $logger */ + $logger = $this->module->getService(PrestaLoggerInterface::class); + + $failedSubscriptionOrderIdsToUpdate = $subscriptionCarrierUpdateHandler->run($newCarrierId); + + if (!empty($failedSubscriptionOrderIdsToUpdate)) { + $logger->error('Failed to update subscription carrier for all orders.', [ + 'failed_subscription_order_ids' => json_encode($failedSubscriptionOrderIdsToUpdate), + ]); + } + } + /** * Provides filters functionality. * diff --git a/subscription/Repository/RecurringOrderRepository.php b/subscription/Repository/RecurringOrderRepository.php index 684d37378..3e70cb39e 100644 --- a/subscription/Repository/RecurringOrderRepository.php +++ b/subscription/Repository/RecurringOrderRepository.php @@ -35,8 +35,8 @@ public function getAllOrdersBasedOnStatuses(array $statuses, int $shopId): array ->select( 'mro.id_mol_recurring_order as id, mro.mollie_subscription_id, mro.mollie_customer_id, mro.id_cart, - mro.id_mol_recurring_product as id_recurring_product, - mro.id_invoice_address, mro.id_delivery_address' + mro.id_mol_recurring_orders_product as id_recurring_product, + mro.id_address_invoice, mro.id_delivery_address' ) ->from('mol_recurring_order', 'mro') ->leftJoin(