Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIPRES-338: Mixed cart subscription listing improvements #812

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 36 additions & 2 deletions src/Install/DatabaseTableInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public function install()
$commands = $this->getCommands();

foreach ($commands as $query) {
if (false == Db::getInstance()->execute($query)) {
if (!Db::getInstance()->execute($query)) {
return false;
}
}

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

/**
Expand Down Expand Up @@ -142,4 +142,38 @@ private function getCommands()

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);
',
],
],
];

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;
}
}
1 change: 0 additions & 1 deletion src/Install/DatabaseTableUninstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ private function getCommands(): array
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'mol_country`;';
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'mol_payment_method`;';
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'mol_payment_method_issuer`;';
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'mol_order_payment_fee`;';
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'mol_carrier_information`;';
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'mol_pending_order_cart`;';
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'mol_excluded_country`;';
Expand Down
7 changes: 3 additions & 4 deletions src/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use Order;
use OrderState;
use PDF;
use PrestaShop\Decimal\Number;
use Product;
use State;
use Tools;
Expand Down Expand Up @@ -186,13 +185,13 @@ 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);
$unitPrice = new Number((string) $recurringOrderProduct->unit_price);
$totalPrice = NumberUtility::toPrecision((float) $recurringOrder->total_tax_incl, 2);
$unitPrice = NumberUtility::toPrecision((float) $recurringOrderProduct->unit_price, 2);

return [
'subscription_reference' => $recurringOrder->mollie_subscription_id,
'product_name' => $product->name,
'unit_price' => $this->toolsAdapter->displayPrice($unitPrice->toPrecision(2), new Currency($recurringOrder->id_currency)),
'unit_price' => $this->toolsAdapter->displayPrice($unitPrice, new Currency($recurringOrder->id_currency)),
'quantity' => $recurringOrderProduct->quantity,
'total_price' => $this->toolsAdapter->displayPrice($totalPrice, new Currency($recurringOrder->id_currency)),
'firstName' => $customer->firstname,
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('ROUND(recurring_order.total_tax_incl, 2) 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
44 changes: 42 additions & 2 deletions subscription/Install/DatabaseTableInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public function install(): bool
$commands = $this->getCommands();

foreach ($commands as $query) {
if (false == Db::getInstance()->execute($query)) {
if (!Db::getInstance()->execute($query)) {
return false;
}
}

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,43 @@ 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_ . '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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it really extend?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using theme's existing template customer/_partials/order-detail-no-return.tpl and OrderLazyArray for this template is needed. Extension makes sens as we don't need to change other methods, only the ones we need to manipulate.

image

{
/** @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;
}
}
16 changes: 12 additions & 4 deletions subscription/Presenter/RecurringOrderPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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 Mollie\Utility\NumberUtility;
use Order;
use PrestaShop\PrestaShop\Adapter\Presenter\Order\OrderPresenter;
use Product;

class RecurringOrderPresenter
Expand All @@ -23,17 +23,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 +60,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,
NumberUtility::toPrecision((float) $recurringOrder->total_tax_incl, 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constant intead of number

);
$recurringOrderData['payment_methods'] = $this->methodApi->getMethodsForFirstPayment($this->language->getContextLanguage()->locale, $currency->iso_code);

return $recurringOrderData;
Expand Down
Loading