Skip to content

Commit

Permalink
PIPRES-261: Subscription order detail view refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mandan2 committed Oct 2, 2023
1 parent a3ab164 commit d23bb6d
Show file tree
Hide file tree
Showing 18 changed files with 604 additions and 178 deletions.
21 changes: 17 additions & 4 deletions controllers/front/recurringOrderDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

use Mollie\Controller\AbstractMollieController;
use Mollie\Logger\PrestaLoggerInterface;
use Mollie\Subscription\Handler\FreeOrderCreationHandler;
use Mollie\Subscription\Handler\SubscriptionCancellationHandler;
use Mollie\Subscription\Presenter\RecurringOrderPresenter;
Expand Down Expand Up @@ -74,13 +75,25 @@ public function initContent()
Tools::redirect(Context::getContext()->link->getModuleLink($this->module->name, 'subscriptions', [], true));
}

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

/** @var RecurringOrderPresenter $recurringOrderPresenter */
$recurringOrderPresenter = $this->module->getService(RecurringOrderPresenter::class);

$this->context->smarty->assign([
'recurringOrderData' => $recurringOrderPresenter->present($recurringOrderId),
'token' => Tools::getToken(),
]);
try {
$this->context->smarty->assign([
'recurringOrderData' => $recurringOrderPresenter->present($recurringOrderId),
'token' => Tools::getToken(),
]);
} catch (Throwable $exception) {
$logger->error('Failed to present subscription order', [
'Exception message' => $exception->getMessage(),
'Exception code' => $exception->getCode(),
]);

Tools::redirect(Context::getContext()->link->getModuleLink($this->module->name, 'subscriptions', [], true));
}

parent::initContent();
$this->context->controller->addCSS($this->module->getPathUri() . 'views/css/front/subscription/customer_order_detail.css');
Expand Down
14 changes: 14 additions & 0 deletions src/Adapter/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,18 @@ public function getShopGroupId(): int
{
return (int) PrestashopContext::getContext()->shop->id_shop_group;
}

public function formatPrice(float $price, string $isoCode): string
{
$locale = PrestashopContext::getContext()->getCurrentLocale();

if (!$locale) {
return (string) $price;
}

return $locale->formatPrice(
$price,
$isoCode
);
}
}
23 changes: 23 additions & 0 deletions src/Repository/ProductRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Mollie\Repository;

class ProductRepository extends AbstractRepository implements ProductRepositoryInterface
{
public function __construct()
{
parent::__construct(\Product::class);
}

public function getCombinationImageById(int $productAttributeId, int $langId): ?array
{
$result = \Product::getCombinationImageById($productAttributeId, $langId);

return empty($result) ? null : $result;
}

public function getCover(int $productId, \Context $context = null): array
{
return \Product::getCover($productId, $context);
}
}
10 changes: 10 additions & 0 deletions src/Repository/ProductRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Mollie\Repository;

interface ProductRepositoryInterface extends ReadOnlyRepositoryInterface
{
public function getCombinationImageById(int $productAttributeId, int $langId): ?array;

public function getCover(int $productId, \Context $context = null): array;
}
6 changes: 6 additions & 0 deletions src/ServiceProvider/BaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
use Mollie\Repository\PaymentMethodRepositoryInterface;
use Mollie\Repository\PendingOrderCartRuleRepository;
use Mollie\Repository\PendingOrderCartRuleRepositoryInterface;
use Mollie\Repository\ProductRepository;
use Mollie\Repository\ProductRepositoryInterface;
use Mollie\Repository\TaxRepository;
use Mollie\Repository\TaxRepositoryInterface;
use Mollie\Repository\TaxRuleRepository;
Expand Down Expand Up @@ -98,6 +100,8 @@
use Mollie\Subscription\Install\InstallerInterface;
use Mollie\Subscription\Logger\Logger;
use Mollie\Subscription\Logger\LoggerInterface;
use Mollie\Subscription\Repository\OrderDetailRepository;
use Mollie\Subscription\Repository\OrderDetailRepositoryInterface;
use Mollie\Subscription\Repository\RecurringOrderRepository;
use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface;
use Mollie\Subscription\Repository\RecurringOrdersProductRepository;
Expand Down Expand Up @@ -135,6 +139,8 @@ public function register(Container $container)

$this->addService($container, RetryHandlerInterface::class, $container->get(RetryHandler::class));

$this->addService($container, ProductRepositoryInterface::class, $container->get(ProductRepository::class));
$this->addService($container, OrderDetailRepositoryInterface::class, $container->get(OrderDetailRepository::class));
$this->addService($container, CountryRepositoryInterface::class, $container->get(CountryRepository::class));
$this->addService($container, PaymentMethodRepositoryInterface::class, $container->get(PaymentMethodRepository::class));
$this->addService($container, GenderRepositoryInterface::class, $container->get(GenderRepository::class));
Expand Down
38 changes: 38 additions & 0 deletions subscription/Exception/CouldNotPresentOrderDetail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Mollie\Subscription\Exception;

class CouldNotPresentOrderDetail extends MollieSubscriptionException
{
public static function failedToFindOrder(): CouldNotPresentOrderDetail
{
return new self(
'Failed to find order',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER
);
}

public static function failedToFindOrderDetail(): CouldNotPresentOrderDetail
{
return new self(
'Failed to find order detail',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER_DETAIL
);
}

public static function failedToFindProduct(): CouldNotPresentOrderDetail
{
return new self(
'Failed to find product',
ExceptionCode::ORDER_FAILED_TO_FIND_PRODUCT
);
}

public static function failedToFindCurrency(): CouldNotPresentOrderDetail
{
return new self(
'Failed to find currency',
ExceptionCode::ORDER_FAILED_TO_FIND_CURRENCY
);
}
}
20 changes: 11 additions & 9 deletions subscription/Exception/ExceptionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ class ExceptionCode
{
//Order error codes starts from 1000

public const ORDER_FAILED_TO_CREATE_ORDER_PAYMENT_FEE = 1001;
public const ORDER_FAILED_TO_UPDATE_ORDER_TOTAL_WITH_PAYMENT_FEE = 1002;
public const ORDER_FAILED_TO_FIND_SELECTED_CARRIER = 1003;
public const ORDER_FAILED_TO_FIND_ORDER_CART = 1004;
public const ORDER_FAILED_TO_FIND_ORDER_CUSTOMER = 1005;
public const ORDER_FAILED_TO_APPLY_SELECTED_CARRIER = 1006;
public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS = 1007;
public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY = 1008;
public const ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE = 1009;
public const ORDER_FAILED_TO_FIND_SELECTED_CARRIER = 1001;
public const ORDER_FAILED_TO_FIND_ORDER_CART = 1002;
public const ORDER_FAILED_TO_FIND_ORDER_CUSTOMER = 1003;
public const ORDER_FAILED_TO_APPLY_SELECTED_CARRIER = 1004;
public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS = 1005;
public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY = 1006;
public const ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE = 1007;
public const ORDER_FAILED_TO_FIND_ORDER = 1008;
public const ORDER_FAILED_TO_FIND_ORDER_DETAIL = 1009;
public const ORDER_FAILED_TO_FIND_PRODUCT = 1010;
public const ORDER_FAILED_TO_FIND_CURRENCY = 1011;

//Cart error codes starts from 2000

Expand Down
130 changes: 130 additions & 0 deletions subscription/Presenter/OrderDetailPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Mollie\Subscription\Presenter;

use Mollie\Adapter\Context;
use Mollie\Api\Types\SubscriptionStatus;
use Mollie\Repository\CurrencyRepositoryInterface;
use Mollie\Repository\OrderRepositoryInterface;
use Mollie\Repository\ProductRepositoryInterface;
use Mollie\Subscription\Exception\CouldNotPresentOrderDetail;
use Mollie\Subscription\Repository\OrderDetailRepositoryInterface;
use Mollie\Utility\NumberUtility;

class OrderDetailPresenter
{
/** @var OrderDetailRepositoryInterface */
private $orderDetailRepository;
/** @var Context */
private $context;
/** @var OrderRepositoryInterface */
private $orderRepository;
/** @var ProductRepositoryInterface */
private $productRepository;
/** @var CurrencyRepositoryInterface */
private $currencyRepository;

public function __construct(
OrderDetailRepositoryInterface $orderDetailRepository,
Context $context,
OrderRepositoryInterface $orderRepository,
ProductRepositoryInterface $productRepository,
CurrencyRepositoryInterface $currencyRepository
) {
$this->orderDetailRepository = $orderDetailRepository;
$this->context = $context;
$this->orderRepository = $orderRepository;
$this->productRepository = $productRepository;
$this->currencyRepository = $currencyRepository;
}

/**
* @throws CouldNotPresentOrderDetail
*/
public function present(
\MolRecurringOrder $recurringOrder,
\MolRecurringOrdersProduct $recurringProduct
): array {
$result = [];

/** @var \Order|null $order */
$order = $this->orderRepository->findOneBy([
'id_order' => (int) $recurringOrder->id_order,
]);

if (!$order) {
throw CouldNotPresentOrderDetail::failedToFindOrder();
}

/** @var \OrderDetail|null $orderDetail */
$orderDetail = $this->orderDetailRepository->findOneBy([
'id_order' => (int) $recurringOrder->id_order,
'product_id' => (int) $recurringProduct->id_product,
'product_attribute_id' => (int) $recurringProduct->id_product_attribute,
]);

if (!$orderDetail) {
throw CouldNotPresentOrderDetail::failedToFindOrderDetail();
}

/** @var \Product|null $product */
$product = $this->productRepository->findOneBy([
'id_product' => (int) $recurringProduct->id_product,
]);

if (!$product) {
throw CouldNotPresentOrderDetail::failedToFindProduct();
}

/** @var \Currency|null $currency */
$currency = $this->currencyRepository->findOneBy([
'id_currency' => (int) $order->id_currency,
]);

if (!$currency) {
throw CouldNotPresentOrderDetail::failedToFindCurrency();
}

$linkRewrite = is_array($product->link_rewrite) && isset($product->link_rewrite[$order->id_lang])
? $product->link_rewrite[$order->id_lang]
: $product->link_rewrite;

$image = $this->productRepository->getCombinationImageById((int) $recurringProduct->id_product_attribute, (int) $order->id_lang);

if (!$image) {
$image = $this->productRepository->getCover((int) $recurringProduct->id_product);
}

$result['name'] = $orderDetail->product_name;
$result['link'] = $this->context->getProductLink($product);
$result['img'] = $this->context->getImageLink($linkRewrite, (string) $image['id_image']);
$result['quantity'] = $orderDetail->product_quantity;
$result['unit_price'] = $this->context->formatPrice(
NumberUtility::toPrecision(
(float) $orderDetail->unit_price_tax_incl,
NumberUtility::DECIMAL_PRECISION
),
$currency->iso_code
);
$result['total'] = $this->context->formatPrice(
NumberUtility::toPrecision(
(float) $recurringOrder->total_tax_incl,
NumberUtility::DECIMAL_PRECISION
),
$currency->iso_code
);

$result['status'] = $recurringOrder->status;
$result['start_date'] = $recurringOrder->date_add;

if ($recurringOrder->status === SubscriptionStatus::STATUS_ACTIVE) {
$result['next_payment_date'] = $recurringOrder->next_payment;
}

if ($recurringOrder->status === SubscriptionStatus::STATUS_CANCELED) {
$result['cancelled_date'] = $recurringOrder->cancelled_at;
}

return $result;
}
}
35 changes: 0 additions & 35 deletions subscription/Presenter/OrderPresenter.php

This file was deleted.

40 changes: 0 additions & 40 deletions subscription/Presenter/RecurringOrderLazyArray.php

This file was deleted.

Loading

0 comments on commit d23bb6d

Please sign in to comment.