Skip to content

PIPRES-460 Cart Line service refactoring initiation #1000

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

Open
wants to merge 73 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
2804d01
initial cart service refactoring
GytisZum Oct 15, 2024
06ecefe
Merge branch 'master' of github.com:mollie/PrestaShop into PIPRES-460…
GytisZum Nov 25, 2024
f18909c
splitting into more services for the code readability
GytisZum Dec 3, 2024
6abac4e
[PIPRES-460] created new service
MarijusDilys Dec 20, 2024
10adc01
[PIPRES-460] updated phpdocs
MarijusDilys Dec 20, 2024
8b9a50a
[PIPRES-460] changed methods visibility, added via dependency injection
MarijusDilys Dec 20, 2024
1572072
[PIPRES-460] added service methods on main service
MarijusDilys Dec 20, 2024
d8843dc
[PIPRES-460] added LineUtility
MarijusDilys Dec 20, 2024
8566734
[PIPRES-460] updated LineUtility
MarijusDilys Dec 20, 2024
f038742
[PIPRES-460] refactor code
MarijusDilys Dec 20, 2024
b5384e3
[PIPRES-460] added RoundingUtility
MarijusDilys Dec 20, 2024
573dcfd
[PIPRES-460] added rounding method
MarijusDilys Dec 20, 2024
6952ef7
[PIPRES-460] alignment, comment styling
MarijusDilys Dec 20, 2024
f53e73c
[PIPRES-460] moved method to ArrayUtility
MarijusDilys Dec 20, 2024
ed95407
[PIPRES-460] renamed argument
MarijusDilys Dec 20, 2024
4f2686b
[PIPRES-460] cslint
MarijusDilys Dec 20, 2024
a678dea
[PIPRES-460] added return types
MarijusDilys Dec 20, 2024
43f63ba
[PIPRES-460] added RoundingUtility widely
MarijusDilys Dec 20, 2024
19865dc
Revert "[PIPRES-460] added RoundingUtility widely"
MarijusDilys Dec 20, 2024
62c57a7
[PIPRES-460] added RoundingUtility 1
MarijusDilys Dec 20, 2024
6ceba0f
[PIPRES-460] added RoundingUtility 2
MarijusDilys Dec 20, 2024
7a738c4
[PIPRES-460] added RoundingUtility 3
MarijusDilys Dec 20, 2024
91be923
[PIPRES-460] workaround and added RoundingUtility
MarijusDilys Dec 20, 2024
2588879
[PIPRES-460] fix phpstan
MarijusDilys Dec 20, 2024
a19c2bc
[PIPRES-460] fixed infinite loop
MarijusDilys Dec 20, 2024
7746cce
[PIPRES-460] removed round method from CartItemsService because of in…
MarijusDilys Dec 20, 2024
6438eb1
[PIPRES-460] optimized ternary if
MarijusDilys Dec 20, 2024
c91af88
[PIPRES-460] added types arguments
MarijusDilys Dec 20, 2024
ae4dec7
[PIPRES-460] cslint
MarijusDilys Dec 20, 2024
82791d8
Merge branch 'develop' of github.com:mollie/PrestaShop into PIPRES-46…
MarijusDilys Feb 5, 2025
174d8fc
Merge branch 'develop' of github.com:mollie/PrestaShop into PIPRES-46…
MarijusDilys Feb 6, 2025
43de8a8
[PIPRES-460] add exceptions
MarijusDilys Feb 6, 2025
f9f27c7
[PIPRES-460] exceptions
MarijusDilys Feb 6, 2025
cd44660
[PIPRES-460] fix ex
MarijusDilys Feb 6, 2025
b230d10
Merge branch 'develop' of github.com:mollie/PrestaShop into PIPRES-46…
MarijusDilys Feb 11, 2025
3153a5b
Merge branch 'develop' of github.com:mollie/PrestaShop into PIPRES-46…
MarijusDilys Feb 17, 2025
3db0750
Merge branch 'develop' of github.com:mollie/PrestaShop into PIPRES-46…
MarijusDilys Feb 18, 2025
82b36f3
Merge branch 'develop' of github.com:mollie/PrestaShop into PIPRES-46…
MarijusDilys Feb 20, 2025
496a55c
[PIPRES-460] delete method
MarijusDilys Feb 25, 2025
3817495
added test
MarijusDilys Feb 25, 2025
d74464f
[PIPRES-460] remove useless class
MarijusDilys Feb 25, 2025
eed7a41
[PIPRES-460] fix
MarijusDilys Feb 25, 2025
0b35b73
[PIPRES-460] add test case
MarijusDilys Mar 4, 2025
be6c927
[PIPRES-460] tests
MarijusDilys Mar 4, 2025
35703eb
[PIPRES-460] tests
MarijusDilys Mar 4, 2025
5a6d618
[PIPRES-460] tests
MarijusDilys Mar 4, 2025
fde234e
[PIPRES-460] fix
MarijusDilys Mar 4, 2025
6f81ac9
[PIPRES-460] tests
MarijusDilys Mar 4, 2025
1b0725f
Merge branch 'develop' into PIPRES-460/cart-line-service-refactoring
MarijusDilys Mar 7, 2025
800f675
Merge branch 'develop' of github.com:mollie/PrestaShop into PIPRES-46…
MarijusDilys Mar 10, 2025
638c254
Merge branch 'release-6.2.7' of github.com:mollie/PrestaShop into PIP…
MarijusDilys Mar 10, 2025
e42d68c
[PIPRES-460] fix cslint
MarijusDilys Mar 17, 2025
33e6ab6
[PIPRES-460] fix
MarijusDilys Mar 17, 2025
4bbe5aa
[PIPRES-460] changelog
MarijusDilys Mar 18, 2025
be27a0d
Merge branch 'develop' into PIPRES-460/cart-line-service-refactoring
MarijusDilys Mar 19, 2025
36b034d
Merge branch 'develop' into PIPRES-460/cart-line-service-refactoring
MarijusDilys Apr 1, 2025
7b6c574
[PIPRES-460] fix
MarijusDilys Apr 1, 2025
275fd7b
[PIPRES-460] fix
MarijusDilys Apr 1, 2025
93a97aa
[PIPRES-460] fix
MarijusDilys Apr 1, 2025
02f36e7
[PIPRES-460] debug coding standarts
MarijusDilys Apr 1, 2025
1dfddbb
fix linter
MarijusDilys Apr 1, 2025
05ec1c5
[PIPRES-460] fix
MarijusDilys Apr 8, 2025
6abdd90
[PIPRES-460] fix
MarijusDilys Apr 8, 2025
5ef6686
[PIPRES-460] fix
MarijusDilys Apr 8, 2025
ad856a8
[PIPRES-460] fix
MarijusDilys Apr 8, 2025
4047712
[PIPRES-460] fix
MarijusDilys Apr 8, 2025
7178bc1
[PIPRES-460] fix
MarijusDilys Apr 8, 2025
303049e
[PIPRES-460][PIPRES-576] added product image replace unsupported chars
MarijusDilys Apr 14, 2025
6d2be2d
[PIPRES-460][PIPRES-576] added product image replace unsupported chars
MarijusDilys Apr 14, 2025
57ed7de
[PIPRES-460] fix
MarijusDilys Apr 28, 2025
3482101
Merge branch 'develop' of github.com:mollie/PrestaShop into PIPRES-46…
GytisZum May 12, 2025
cef6534
cart line conflict fix
GytisZum May 12, 2025
4a59b69
product url and payment surcharge fix
GytisZum May 14, 2025
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
12 changes: 11 additions & 1 deletion src/Exception/Code/ExceptionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
* @codingStandardsIgnoreStart
*/

namespace Mollie\Exception\Code;
Expand Down Expand Up @@ -35,4 +34,15 @@ class ExceptionCode
public const ORDER_FAILED_TO_RETRIEVE_PAYMENT_FEE = 3004;
public const ORDER_FAILED_TO_CREATE_ORDER_PAYMENT_FEE = 3005;
public const ORDER_FAILED_TO_UPDATE_ORDER_TOTAL_WITH_PAYMENT_FEE = 3006;

// Service error codes starts from 4000
public const SERVICE_FAILED_TO_ROUND_AMOUNT = 4001;
const SERVICE_FAILED_TO_FILL_PRODUCT_LINES_WITH_REMAINING_DATA = 4002;
const SERVICE_FAILED_TO_ADD_SHIPPING_LINE = 4003;
const SERVICE_FAILED_TO_ADD_WRAPPING_LINE = 4004;
const SERVICE_FAILED_TO_ADD_PAYMENT_FEE = 4005;
const SERVICE_FAILED_TO_UNGROUP_LINES = 4006;
const SERVICE_FAILED_TO_CONVERT_TO_LINE_ARRAY = 4007;
const SERVICE_FAILED_TO_CREATE_PRODUCT_LINES = 4008;
const SERVICE_FAILED_TO_ADD_DISCOUNTS_TO_PRODUCT_LINES = 4009;
}
103 changes: 103 additions & 0 deletions src/Exception/CouldNotProcessCartLinesException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
*/

namespace Mollie\Exception;

use Exception;
use Mollie\Exception\Code\ExceptionCode;

if (!defined('_PS_VERSION_')) {
exit;
}

class CouldNotProcessCartLinesException extends \Exception
{
public static function failedToRoundAmount(Exception $exception): self
{
return new self(
'Failed to round amount.',
ExceptionCode::SERVICE_FAILED_TO_ROUND_AMOUNT,
$exception
);
}

public static function failedToFillProductLinesWithRemainingData(Exception $e)
{
return new self(
'Failed to fill product lines with remaining data.',
ExceptionCode::SERVICE_FAILED_TO_FILL_PRODUCT_LINES_WITH_REMAINING_DATA,
$e
);
}

public static function failedToAddShippingLine(Exception $e)
{
return new self(
'Failed to add shipping line.',
ExceptionCode::SERVICE_FAILED_TO_ADD_SHIPPING_LINE,
$e
);
}

public static function failedToAddWrappingLine(Exception $e)
{
return new self(
'Failed to add wrapping line.',
ExceptionCode::SERVICE_FAILED_TO_ADD_WRAPPING_LINE,
$e
);
}

public static function failedToAddPaymentFee(Exception $e)
{
return new self(
'Failed to add payment fee.',
ExceptionCode::SERVICE_FAILED_TO_ADD_PAYMENT_FEE,
$e
);
}

public static function failedToUngroupLines(Exception $e)
{
return new self(
'Failed to ungroup lines.',
ExceptionCode::SERVICE_FAILED_TO_UNGROUP_LINES,
$e
);
}

public static function failedConvertToLineArray(Exception $e)
{
return new self(
'Failed to convert to line array.',
ExceptionCode::SERVICE_FAILED_TO_CONVERT_TO_LINE_ARRAY,
$e
);
}

public static function failedToCreateProductLines(Exception $e)
{
return new self(
'Failed to create product lines.',
ExceptionCode::SERVICE_FAILED_TO_CREATE_PRODUCT_LINES,
$e
);
}

public static function failedToAddDiscountsToProductLines(Exception $e)
{
return new self(
'Failed to add discounts to product lines.',
ExceptionCode::SERVICE_FAILED_TO_ADD_DISCOUNTS_TO_PRODUCT_LINES,
$e
);
}
}
50 changes: 50 additions & 0 deletions src/Service/CartLine/CartItemDiscountService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
*/

namespace Mollie\Service\CartLine;

use Mollie\Config\Config;
use Mollie\Utility\NumberUtility;

if (!defined('_PS_VERSION_')) {
exit;
}

class CartItemDiscountService
{
/**
* @param float $totalDiscounts
* @param array $orderLines
* @param float $remaining
*
* @return array
*/
public function addDiscountsToProductLines(float $totalDiscounts, array $orderLines, float $remaining): array
{
if ($totalDiscounts >= 0.01) {
$orderLines['discount'] = [
[
'name' => 'Discount',
'type' => 'discount',
'quantity' => 1,
'unitPrice' => -round($totalDiscounts, Config::API_ROUNDING_PRECISION),
'totalAmount' => -round($totalDiscounts, Config::API_ROUNDING_PRECISION),
'targetVat' => 0,
'category' => '',
],
];

$remaining = NumberUtility::plus($remaining, $totalDiscounts);
}

return [$orderLines, $remaining];
}
}
59 changes: 59 additions & 0 deletions src/Service/CartLine/CartItemPaymentFeeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
*/

namespace mollie\src\Service\CartLine;

use Mollie\Config\Config;
use Mollie\DTO\PaymentFeeData;
use Mollie\Service\LanguageService;
use Mollie\Utility\NumberUtility;

if (!defined('_PS_VERSION_')) {
exit;
}

class CartItemPaymentFeeService
{
/* @var LanguageService */
private $languageService;

public function __construct(LanguageService $languageService)
{
$this->languageService = $languageService;
}

/**
* @param PaymentFeeData $paymentFeeData
* @param array $orderLines
*
* @return array
*/
public function addPaymentFeeLine(PaymentFeeData $paymentFeeData, array $orderLines): array
{
if (!$paymentFeeData->isActive()) {
return $orderLines;
}

$orderLines['surcharge'] = [
[
'name' => $this->languageService->lang('Payment fee'),
'sku' => Config::PAYMENT_FEE_SKU,
'quantity' => 1,
'unitPrice' => round($paymentFeeData->getPaymentFeeTaxIncl(), CONFIG::API_ROUNDING_PRECISION),
'totalAmount' => round($paymentFeeData->getPaymentFeeTaxIncl(), CONFIG::API_ROUNDING_PRECISION),
'vatAmount' => NumberUtility::minus($paymentFeeData->getPaymentFeeTaxIncl(), $paymentFeeData->getPaymentFeeTaxExcl()),
'vatRate' => $paymentFeeData->getTaxRate(),
],
];

return $orderLines;
}
}
87 changes: 87 additions & 0 deletions src/Service/CartLine/CartItemProductLinesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
*/

namespace Mollie\Service\CartLine;

use Mollie\Config\Config;
use Mollie\Utility\CalculationUtility;
use Mollie\Utility\NumberUtility;

if (!defined('_PS_VERSION_')) {
exit;
}

class CartItemProductLinesService
{
/**
* @param array $orderLines
* @param int $vatRatePrecision
*
* @return array
*
* @throws \PrestaShop\Decimal\Exception\DivisionByZeroException
*/
public function fillProductLinesWithRemainingData(array $orderLines, int $vatRatePrecision): array
{
$roundingPrecision = CONFIG::API_ROUNDING_PRECISION;

foreach ($orderLines as $productHash => $aItem) {
$orderLines[$productHash] = array_map(function ($line) use ($roundingPrecision, $vatRatePrecision) {
$quantity = (int) $line['quantity'];
$targetVat = $line['targetVat'];
$unitPrice = $line['unitPrice'];

$unitPriceNoTax = round(
CalculationUtility::getUnitPriceNoTax($line['unitPrice'], $targetVat),
$roundingPrecision
);

// Calculate VAT
$totalAmount = $line['totalAmount'];
$actualVatRate = 0;

if ($unitPriceNoTax > 0) {
$actualVatRate = round(
CalculationUtility::getActualVatRate($unitPrice, $unitPriceNoTax, $quantity),
$vatRatePrecision
);
}

$vatRateWithPercentages = NumberUtility::plus($actualVatRate, 100);

$vatAmount = NumberUtility::times(
$totalAmount,
NumberUtility::divide($actualVatRate, $vatRateWithPercentages)
);

$newItem = [
'name' => $line['name'],
'category' => $line['category'],
'quantity' => (int) $quantity,
'unitPrice' => round($unitPrice, $roundingPrecision),
'totalAmount' => round($totalAmount, $roundingPrecision),
'vatRate' => round($actualVatRate, $roundingPrecision),
'vatAmount' => round($vatAmount, $roundingPrecision),
'product_url' => $line['product_url'] ?? null,
'image_url' => $line['image_url'] ?? null,
];

if (isset($line['sku'])) {
$newItem['sku'] = $line['sku'];
}

return $newItem;
}, $aItem);
}

return $orderLines;
}
}
59 changes: 59 additions & 0 deletions src/Service/CartLine/CartItemShippingLineService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
*/

namespace Mollie\Service\CartLine;

use Mollie\Config\Config;
use Mollie\Service\LanguageService;

if (!defined('_PS_VERSION_')) {
exit;
}

class CartItemShippingLineService
{
/* @var LanguageService */
private $languageService;

public function __construct(LanguageService $languageService)
{
$this->languageService = $languageService;
}

/**
* @param float $roundedShippingCost
* @param array $cartSummary
* @param array $orderLines
*
* @return array
*/
public function addShippingLine(float $roundedShippingCost, array $cartSummary, array $orderLines): array
{
if (round($roundedShippingCost, 2) <= 0) {
return $orderLines;
}

$shippingVatRate = round(($cartSummary['total_shipping'] - $cartSummary['total_shipping_tax_exc']) / $cartSummary['total_shipping_tax_exc'] * 100, Config::API_ROUNDING_PRECISION);

$orderLines['shipping'] = [
[
'name' => $this->languageService->lang('Shipping'),
'quantity' => 1,
'unitPrice' => round($roundedShippingCost, Config::API_ROUNDING_PRECISION),
'totalAmount' => round($roundedShippingCost, Config::API_ROUNDING_PRECISION),
'vatAmount' => round($roundedShippingCost * $shippingVatRate / ($shippingVatRate + 100), Config::API_ROUNDING_PRECISION),
'vatRate' => $shippingVatRate,
],
];

return $orderLines;
}
}
Loading
Loading