Skip to content

Commit

Permalink
PIPRES-338: Mixed cart subscription listing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mandan2 committed Sep 18, 2023
1 parent 85d6f0d commit 8a7f971
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 35 deletions.
2 changes: 1 addition & 1 deletion controllers/front/recurringOrderDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Mollie\Controller\AbstractMollieController;
use Mollie\Subscription\Handler\FreeOrderCreationHandler;
use Mollie\Subscription\Handler\SubscriptionCancellationHandler;
use Mollie\Subscription\Logger\RecurringOrderPresenter;
use Mollie\Subscription\Presenter\RecurringOrderPresenter;
use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface;

class MollieRecurringOrderDetailModuleFrontController extends AbstractMollieController
Expand Down
2 changes: 1 addition & 1 deletion controllers/front/subscriptions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Mollie\Repository\MolCustomerRepository;
use Mollie\Subscription\Logger\RecurringOrdersPresenter;
use Mollie\Subscription\Presenter\RecurringOrdersPresenter;

/**
* 2007-2020 PrestaShop and Contributors
Expand Down
3 changes: 1 addition & 2 deletions src/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Mollie\Adapter\ToolsAdapter;
use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface;
use Mollie\Subscription\Repository\RecurringOrdersProductRepositoryInterface;
use Mollie\Utility\NumberUtility;
use MolRecurringOrder;
use MolRecurringOrdersProduct;
use Order;
Expand Down Expand Up @@ -186,7 +185,7 @@ private function getSubscriptionCancellationWarningMailData(
Customer $customer
): array {
$product = new Product($recurringOrderProduct->id_product, false, $customer->id_lang);
$totalPrice = NumberUtility::times((float) $recurringOrderProduct->unit_price, (float) $recurringOrderProduct->quantity);
$totalPrice = $recurringOrder->total_tax_incl;
$unitPrice = new Number((string) $recurringOrderProduct->unit_price);

return [
Expand Down
4 changes: 4 additions & 0 deletions subscription/Entity/MolRecurringOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class MolRecurringOrder extends ObjectModel
/** @var string */
public $status;

/** @var float */
public $total_tax_incl;

/** @var string */
public $payment_method;

Expand Down Expand Up @@ -71,6 +74,7 @@ class MolRecurringOrder extends ObjectModel
'mollie_customer_id' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'description' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'status' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'total_tax_incl' => ['type' => self::TYPE_FLOAT, 'validate' => 'isFloat'],
'payment_method' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'next_payment' => ['type' => self::TYPE_DATE],
'reminder_at' => ['type' => self::TYPE_DATE],
Expand Down
6 changes: 5 additions & 1 deletion subscription/Factory/CreateSubscriptionDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ public function build(Order $order, array $subscriptionProduct): SubscriptionDat
$currency = $this->currencyAdapter->getById((int) $order->id_currency);
$description = $this->subscriptionDescription->getSubscriptionDescription($order);

$orderTotal = (float) $subscriptionProduct['total_price_tax_incl']
+ (float) $order->total_wrapping_tax_incl
+ (float) $order->total_shipping_tax_incl;

/**
* NOTE: we will only send product price as total for subscriptions
*/
$orderAmount = new Amount((float) $subscriptionProduct['total_price_tax_incl'], $currency->iso_code);
$orderAmount = new Amount($orderTotal, $currency->iso_code);
$subscriptionData = new SubscriptionDataDTO(
$molCustomer->customer_id,
$orderAmount,
Expand Down
12 changes: 6 additions & 6 deletions subscription/Grid/SubscriptionGridDefinitionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ protected function getColumns()
'sortable' => true,
])
)
->add((new DataColumn('unit_price'))
->setName($this->module->l('Unit price', self::FILE_NAME))
->add((new DataColumn('total_price'))
->setName($this->module->l('Total price', self::FILE_NAME))
->setOptions([
'field' => 'unit_price',
'field' => 'total_price',
'sortable' => true,
])
)
Expand Down Expand Up @@ -220,14 +220,14 @@ protected function getFilters()
])
->setAssociatedColumn('status')
)
->add((new Filter('unit_price', MoneyType::class))
->add((new Filter('total_price', MoneyType::class))
->setTypeOptions([
'required' => false,
'attr' => [
'placeholder' => $this->module->l('Unit price', self::FILE_NAME),
'placeholder' => $this->module->l('Total price', self::FILE_NAME),
],
])
->setAssociatedColumn('unit_price')
->setAssociatedColumn('total_price')
)
->add((new Filter('iso_code', TextType::class))
->setTypeOptions([
Expand Down
4 changes: 2 additions & 2 deletions subscription/Grid/SubscriptionGridQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getSearchQueryBuilder(SearchCriteriaInterface $searchCriteria):
$qb = $this->getQueryBuilder($searchCriteria->getFilters())
->select('recurring_order.*')
->addSelect($this->getNameField() . ' as fullname')
->addSelect('ROUND(orders.total_paid, 2) as unit_price')
->addSelect('recurring_order.total_tax_incl as total_price')
->addSelect('currency.iso_code')
;

Expand Down Expand Up @@ -106,7 +106,7 @@ private function applyFilters(array $filters, QueryBuilder $qb): void
'fullname' => $this->getNameField(),
'description' => 'recurring_order.description',
'status' => 'recurring_order.status',
'unit_price' => 'orders.total_paid',
'total_price' => 'recurring_order.total_tax_incl',
'iso_code' => 'currency.iso_code',
];

Expand Down
1 change: 1 addition & 0 deletions subscription/Handler/SubscriptionCreationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private function createRecurringOrder(MolRecurringOrdersProduct $recurringOrders
$recurringOrder->id_address_invoice = $order->id_address_invoice;
$recurringOrder->description = $subscription->description;
$recurringOrder->status = $subscription->status;
$recurringOrder->total_tax_incl = (float) $subscription->amount->value;
$recurringOrder->payment_method = $method;
$recurringOrder->next_payment = $subscription->nextPaymentDate;
$recurringOrder->reminder_at = $subscription->nextPaymentDate; //todo: add logic to get reminder date when reminder is done
Expand Down
55 changes: 54 additions & 1 deletion subscription/Install/DatabaseTableInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function install(): bool
}
}

return true;
return $this->alterTableCommands();
}

/**
Expand All @@ -45,6 +45,7 @@ private function getCommands(): array
`mollie_customer_id` VARCHAR(64) NOT NULL,
`payment_method` VARCHAR(64) NOT NULL,
`id_mol_recurring_orders_product` INT(64) NOT NULL,
`total_tax_incl` decimal(20, 6) NOT NULL,
`date_add` datetime NOT NULL,
`date_update` datetime NOT NULL
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';
Expand All @@ -61,4 +62,56 @@ private function getCommands(): array

return $sql;
}

private function alterTableCommands(): bool
{
$queries = [
[
'verification' => '
SELECT COUNT(*) > 0 AS count
FROM information_schema.columns
WHERE TABLE_SCHEMA = "' . _DB_NAME_ . '" AND table_name = "' . _DB_PREFIX_ . 'mollie_payments" AND column_name = "mandate_id";
',
'alter' => [
'
ALTER TABLE ' . _DB_PREFIX_ . 'mollie_payments
ADD COLUMN mandate_id VARCHAR(64);
',
],
],
[
'verification' => '
SELECT COUNT(*) > 0 AS count
FROM information_schema.columns
WHERE TABLE_SCHEMA = "' . _DB_NAME_ . '" AND table_name = "' . _DB_PREFIX_ . 'mol_recurring_order" AND column_name = "total_tax_incl";
',
'alter' => [
'
ALTER TABLE ' . _DB_PREFIX_ . 'mol_recurring_order
ADD COLUMN total_tax_incl decimal(20, 6) NOT NULL;
',
'
UPDATE ' . _DB_PREFIX_ . 'mol_recurring_order ro
JOIN ' . _DB_PREFIX_ . 'orders o ON ro.id_order = o.id_order
SET ro.total_tax_incl = o.total_paid_tax_incl;
',
],
],
];

foreach ($queries as $query) {
/* only run if it doesn't exist */
if (Db::getInstance()->getValue($query['verification'])) {
continue;
}

foreach ($query['alter'] as $alterQuery) {
if (!Db::getInstance()->execute($alterQuery)) {
return false;
}
}
}

return true;
}
}
35 changes: 35 additions & 0 deletions subscription/Presenter/OrderPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Mollie\Subscription\Presenter;

class OrderPresenter
{
public function present(
\Order $order,
int $recurringOrderProductAttributeId,
float $recurringOrderTotalTaxIncl
): RecurringOrderLazyArray {
$orderProducts = $order->getCartProducts();

foreach ($orderProducts as $orderProduct) {
if ((int) $orderProduct['id_product_attribute'] !== $recurringOrderProductAttributeId) {
$order->total_paid_tax_excl -= (float) $orderProduct['total_price_tax_excl'];

continue;
}

$order->total_products = (float) $orderProduct['total_price_tax_excl'];
$order->total_products_wt = (float) $orderProduct['total_price_tax_incl'];
$order->total_paid_tax_incl = $recurringOrderTotalTaxIncl;
$order->total_paid = $recurringOrderTotalTaxIncl;

break;
}

$orderLazyArray = new RecurringOrderLazyArray($order);

$orderLazyArray->setRecurringOrderProductAttributeId($recurringOrderProductAttributeId);

return $orderLazyArray;
}
}
40 changes: 40 additions & 0 deletions subscription/Presenter/RecurringOrderLazyArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Mollie\Subscription\Presenter;

use PrestaShop\PrestaShop\Adapter\Presenter\Order\OrderLazyArray;

class RecurringOrderLazyArray extends OrderLazyArray
{
/** @var int */
private $recurringOrderProductAttributeId;

public function setRecurringOrderProductAttributeId(int $recurringOrderProductAttributeId): void
{
$this->recurringOrderProductAttributeId = $recurringOrderProductAttributeId;
}

/**
* @arrayAccess
*
* @return array
*/
public function getProducts(): array
{
$subscriptionProduct = [];

$orderProducts = parent::getProducts();

foreach ($orderProducts as $orderProduct) {
if ((int) $orderProduct['id_product_attribute'] !== $this->recurringOrderProductAttributeId) {
continue;
}

$subscriptionProduct[] = $orderProduct;

break;
}

return $subscriptionProduct;
}
}
15 changes: 11 additions & 4 deletions subscription/Presenter/RecurringOrderPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

declare(strict_types=1);

namespace Mollie\Subscription\Logger;
namespace Mollie\Subscription\Presenter;

use Currency;
use Mollie\Adapter\Language;
use Mollie\Subscription\Api\MethodApi;
use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface;
use Mollie\Subscription\Repository\RecurringOrdersProductRepositoryInterface;
use Order;
use PrestaShop\PrestaShop\Adapter\Presenter\Order\OrderPresenter;
use Product;

class RecurringOrderPresenter
Expand All @@ -23,17 +22,21 @@ class RecurringOrderPresenter
private $language;
/** @var MethodApi */
private $methodApi;
/** @var OrderPresenter */
private $orderPresenter;

public function __construct(
RecurringOrderRepositoryInterface $recurringOrderRepository,
RecurringOrdersProductRepositoryInterface $recurringOrdersProductRepository,
Language $language,
MethodApi $methodApi
MethodApi $methodApi,
OrderPresenter $orderPresenter
) {
$this->recurringOrderRepository = $recurringOrderRepository;
$this->recurringOrdersProductRepository = $recurringOrdersProductRepository;
$this->language = $language;
$this->methodApi = $methodApi;
$this->orderPresenter = $orderPresenter;
}

public function present(int $recurringOrderId): array
Expand All @@ -56,7 +59,11 @@ public function present(int $recurringOrderId): array
$recurringOrderData['recurring_order'] = $recurringOrder;
$recurringOrderData['recurring_product'] = $recurringProduct;
$recurringOrderData['product'] = $product;
$recurringOrderData['order'] = (new OrderPresenter())->present($order);
$recurringOrderData['order'] = $this->orderPresenter->present(
$order,
(int) $recurringProduct->id_product_attribute,
(float) $recurringOrder->total_tax_incl
);
$recurringOrderData['payment_methods'] = $this->methodApi->getMethodsForFirstPayment($this->language->getContextLanguage()->locale, $currency->iso_code);

return $recurringOrderData;
Expand Down
15 changes: 2 additions & 13 deletions subscription/Presenter/RecurringOrdersPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

declare(strict_types=1);

namespace Mollie\Subscription\Logger;
namespace Mollie\Subscription\Presenter;

use Currency;
use Mollie\Adapter\Context;
use Mollie\Adapter\Language;
use Mollie\Adapter\Link;
use Mollie\Adapter\ToolsAdapter;
use Mollie\Repository\OrderRepositoryInterface;
use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface;
use Mollie\Subscription\Repository\RecurringOrdersProductRepositoryInterface;
use Mollie\Utility\NumberUtility;
use MolRecurringOrder;
use Order;
use Product;

class RecurringOrdersPresenter
Expand All @@ -29,8 +27,6 @@ class RecurringOrdersPresenter
private $language;
/** @var ToolsAdapter */
private $tools;
/** @var OrderRepositoryInterface */
private $orderRepository;
/** @var Context */
private $context;

Expand All @@ -40,15 +36,13 @@ public function __construct(
Link $link,
Language $language,
ToolsAdapter $tools,
OrderRepositoryInterface $orderRepository,
Context $context
) {
$this->recurringOrderRepository = $recurringOrderRepository;
$this->link = $link;
$this->recurringOrdersProductRepository = $recurringOrdersProductRepository;
$this->language = $language;
$this->tools = $tools;
$this->orderRepository = $orderRepository;
$this->context = $context;
}

Expand All @@ -64,11 +58,6 @@ public function present(string $molCustomerId): array
$recurringOrdersPresentData = [];
/** @var MolRecurringOrder $recurringOrder */
foreach ($recurringOrders as $recurringOrder) {
/** @var Order $order */
$order = $this->orderRepository->findOneBy([
'id_order' => $recurringOrder->id_order,
]);

$recurringProduct = $this->recurringOrdersProductRepository->findOneBy([
'id_mol_recurring_orders_product' => $recurringOrder->id,
]);
Expand All @@ -79,7 +68,7 @@ public function present(string $molCustomerId): array
$recurringOrderData['recurring_order'] = $recurringOrder;
$recurringOrderData['details_url'] = $this->link->getModuleLink('mollie', 'recurringOrderDetail', ['id_mol_recurring_order' => $recurringOrder->id]);
$recurringOrderData['product_name'] = is_array($product->name) ? $product->name[$this->context->getLanguageId()] : $product->name;
$recurringOrderData['total_price'] = $this->tools->displayPrice(NumberUtility::toPrecision((float) $order->total_paid, 2), new Currency($recurringOrder->id_currency));
$recurringOrderData['total_price'] = $this->tools->displayPrice(NumberUtility::toPrecision((float) $recurringOrder->total_tax_incl, 2), new Currency($recurringOrder->id_currency));
$recurringOrderData['currency'] = new \Currency($recurringOrder->id_currency);
$recurringOrdersPresentData[] = $recurringOrderData;
}
Expand Down
Loading

0 comments on commit 8a7f971

Please sign in to comment.