Skip to content

Commit

Permalink
Merge pull request #870 from mollie/PIPRES-349-update-carrier-callers
Browse files Browse the repository at this point in the history
PIPRES-349: Update carrier callers
  • Loading branch information
JevgenijVisockij authored May 14, 2024
2 parents a149a88 + 8e6c0cb commit 4fc0249
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
34 changes: 34 additions & 0 deletions mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Mollie\Service\ExceptionService;
use Mollie\ServiceProvider\LeagueServiceContainerProvider;
use Mollie\Subscription\Handler\CustomerAddressUpdateHandler;
use Mollie\Subscription\Handler\UpdateSubscriptionCarrierHandler;
use Mollie\Subscription\Install\AttributeInstaller;
use Mollie\Subscription\Install\DatabaseTableInstaller;
use Mollie\Subscription\Install\HookInstaller;
Expand Down Expand Up @@ -1271,6 +1272,39 @@ public function hookActionObjectAddressDeleteAfter(array $params): void
$this->addPreventDeleteErrorMessage();
}

public function hookActionCarrierUpdate(array $params): void
{
$oldCarrierId = $params['id_carrier'] ?? 0;
$newCarrier = $params['carrier'] ?? null;

if (empty($oldCarrierId) || empty($newCarrier)) {
return;
}

/** @var UpdateSubscriptionCarrierHandler $subscriptionCarrierUpdateHandler */
$subscriptionCarrierUpdateHandler = $this->getService(UpdateSubscriptionCarrierHandler::class);

/** @var ConfigurationAdapter $configuration */
$configuration = $this->getService(ConfigurationAdapter::class);

/** @var PrestaLoggerInterface $logger */
$logger = $this->getService(PrestaLoggerInterface::class);

if ((int) $oldCarrierId !== (int) $configuration->get(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID)) {
return;
}

$failedSubscriptionOrderIdsToUpdate = $subscriptionCarrierUpdateHandler->run((int) $newCarrier->id);

if (empty($failedSubscriptionOrderIdsToUpdate)) {
return;
}

$logger->error('Failed to update subscription carrier for all orders.', [
'failed_subscription_order_ids' => json_encode($failedSubscriptionOrderIdsToUpdate),
]);
}

public function hookActionFrontControllerAfterInit(): void
{
$this->frontControllerAfterInit();
Expand Down
1 change: 1 addition & 0 deletions src/Install/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public static function getHooks()
'actionObjectOrderPaymentAddAfter',
'displayProductAdditionalInfo',
'displayCustomerAccount',
'actionCarrierUpdate',
];
}

Expand Down
1 change: 0 additions & 1 deletion src/Install/Uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ private function deleteConfig()
Config::METHODS_CONFIG,
Config::MOLLIE_MAIL_WHEN_COMPLETED,
Config::MOLLIE_API_KEY_TEST,
Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID,
Config::MOLLIE_SUBSCRIPTION_ENABLED,
];

Expand Down
34 changes: 34 additions & 0 deletions subscription/Controller/Symfony/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,6 +100,8 @@ public function submitOptionsAction(Request $request): RedirectResponse
return $this->redirectToRoute('admin_subscription_index');
}

$this->updateSubscriptionCarrier($form->getData()['carrier']);

$formHandler->save($form->getData());

$this->addFlash(
Expand All @@ -106,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.
*
Expand Down
4 changes: 2 additions & 2 deletions subscription/Repository/RecurringOrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @codingStandardsIgnoreStart
*#}

{% import '@PrestaShop/Admin/macros.html.twig' as ps %}

{% set subscriptionOptions = subscriptionOptionsForm %}

{% if subscriptionOptionsForm.subscription_options is defined and subscriptionOptionsForm.subscription_options %}
Expand Down

0 comments on commit 4fc0249

Please sign in to comment.