From 2804d0172da765e8998709646bc62b1a4de53bcc Mon Sep 17 00:00:00 2001 From: Gytautas Date: Tue, 15 Oct 2024 12:39:48 +0300 Subject: [PATCH 01/28] initial cart service refactoring --- src/Service/CartLinesService.php | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 0312ed108..15486dd85 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -54,6 +54,69 @@ public function __construct(LanguageService $languageService, VoucherService $vo $this->context = $context; } + public function buildCartLines( + $amount, $paymentFeeData, $currencyIsoCode, $cartSummary, $shippingCost, $cartItems, $psGiftWrapping, $selectedVoucherCategory + ) { + + $apiRoundingPrecision = Config::API_ROUNDING_PRECISION; + $vatRatePrecision = Config::VAT_RATE_ROUNDING_PRECISION; + + $totalPrice = round($amount, $apiRoundingPrecision); + $roundedShippingCost = round($shippingCost, $apiRoundingPrecision); + foreach ($cartSummary['discounts'] as $discount) { + if ($discount['free_shipping']) { + $roundedShippingCost = 0; + } + } + + $wrappingPrice = $psGiftWrapping ? round($cartSummary['total_wrapping'], $apiRoundingPrecision) : 0; + $totalDiscounts = isset($cartSummary['total_discounts']) ? $cartSummary['total_discounts'] : 0; + $remaining = round( + CalculationUtility::getCartRemainingPrice((float) $totalPrice, (float) $roundedShippingCost, (float) $wrappingPrice), + $apiRoundingPrecision + ); + + $orderLines = []; + + // Create product lines from cart items + $productLinesResult = $this->cartItemsService->createProductLines($cartItems, $cartSummary, $selectedVoucherCategory); + $orderLines = $productLinesResult['orderLines']; + $remaining += $productLinesResult['remaining']; + + // Add discounts to the order lines + //todo move this inside discount service + $totalDiscounts = isset($cartSummary['total_discounts']) ? $cartSummary['total_discounts'] : 0; + $discountsResult = $this->discountService->addDiscounts($orderLines, $cartSummary['total_discounts']); + $orderLines = $discountsResult['orderLines']; + $remaining += $discountsResult['remaining']; + + //todo move these both methods inside some kind of utility class + // Compensate for order total rounding inaccuracies + $orderLines = $this->compositeRoundingInaccuracies($remaining, $apiRoundingPrecision, $orderLines); + + // Fill the order lines with the rest of the data (tax, total amount, etc.) + $orderLines = $this->fillProductLinesWithRemainingData($orderLines, $apiRoundingPrecision, $vatRatePrecision); + + // Add shipping costs to the order lines + $shippingResult = $this->shippingService->addShippingLine($orderLines, $shippingCost, $cartSummary); + $orderLines = $shippingResult['orderLines']; + $remaining += $shippingResult['remaining']; + + // Add wrapping costs to the order lines + $wrappingResult = $this->wrappingService->addWrappingLine($orderLines, $cartSummary, $psGiftWrapping); + $orderLines = $shippingResult['orderLines']; + $remaining += $shippingResult['remaining']; + + // Add payment fees to the order lines + $paymentFeeResult = $this->paymentFeeService->addPaymentFee($orderLines, $paymentFeeData); + $orderLines = $shippingResult['orderLines']; + $remaining += $shippingResult['remaining']; + + return $orderLines; + // Finalize and return order lines +// return $this->finalizeOrderLines($orderLines, $currencyIsoCode); + } + /** * @param float $amount * @param PaymentFeeData $paymentFeeData From f18909cc2dc940f3f599fea94515e2e4672f6e51 Mon Sep 17 00:00:00 2001 From: Gytautas Date: Tue, 3 Dec 2024 09:39:15 +0200 Subject: [PATCH 02/28] splitting into more services for the code readability --- .../CartLine/CartItemDiscountService.php | 51 ++++ .../CartLine/CartItemShippingLineService.php | 59 +++++ .../CartLine/CartItemWrappingService.php | 60 +++++ src/Service/CartLine/CartItemsService.php | 112 +++++++++ src/Service/CartLinesService.php | 236 +++--------------- 5 files changed, 320 insertions(+), 198 deletions(-) create mode 100644 src/Service/CartLine/CartItemDiscountService.php create mode 100644 src/Service/CartLine/CartItemShippingLineService.php create mode 100644 src/Service/CartLine/CartItemWrappingService.php create mode 100644 src/Service/CartLine/CartItemsService.php diff --git a/src/Service/CartLine/CartItemDiscountService.php b/src/Service/CartLine/CartItemDiscountService.php new file mode 100644 index 000000000..4f6576313 --- /dev/null +++ b/src/Service/CartLine/CartItemDiscountService.php @@ -0,0 +1,51 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Service\CartLine; + +use Mollie\Config\Config; +use Mollie\Utility\NumberUtility; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class CartItemDiscountService +{ + /** + * @param float $totalDiscounts + * @param int $apiRoundingPrecision + * @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]; + } +} \ No newline at end of file diff --git a/src/Service/CartLine/CartItemShippingLineService.php b/src/Service/CartLine/CartItemShippingLineService.php new file mode 100644 index 000000000..ac9df826a --- /dev/null +++ b/src/Service/CartLine/CartItemShippingLineService.php @@ -0,0 +1,59 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +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 int $apiRoundingPrecision + * + * @return array + */ + public function addShippingLine($roundedShippingCost, $cartSummary, array $orderLines) + { + if (round($roundedShippingCost, 2) > 0) { + $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; + } +} \ No newline at end of file diff --git a/src/Service/CartLine/CartItemWrappingService.php b/src/Service/CartLine/CartItemWrappingService.php new file mode 100644 index 000000000..8746d49ab --- /dev/null +++ b/src/Service/CartLine/CartItemWrappingService.php @@ -0,0 +1,60 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Service\CartLine; + +use Mollie\Config\Config; +use Mollie\Service\LanguageService; +use Mollie\Utility\CalculationUtility; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class CartItemWrappingService +{ + /** + * @var LanguageService + */ + private $languageService; + + public function __construct(LanguageService $languageService) + { + $this->languageService = $languageService; + } + + public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $vatRatePrecision, array $orderLines) + { + if (round($wrappingPrice, 2) > 0) { + $wrappingVatRate = round( + CalculationUtility::getActualVatRate( + $cartSummary['total_wrapping'], + $cartSummary['total_wrapping_tax_exc'] + ), + $vatRatePrecision + ); + + $orderLines['wrapping'] = [ + [ + 'name' => $this->languageService->lang('Gift wrapping'), + 'quantity' => 1, + 'unitPrice' => round($wrappingPrice, Config::API_ROUNDING_PRECISION), + 'totalAmount' => round($wrappingPrice, Config::API_ROUNDING_PRECISION), + 'vatAmount' => round($wrappingPrice * $wrappingVatRate / ($wrappingVatRate + 100), Config::API_ROUNDING_PRECISION), + 'vatRate' => $wrappingVatRate, + ], + ]; + } + + return $orderLines; + } +} \ No newline at end of file diff --git a/src/Service/CartLine/CartItemsService.php b/src/Service/CartLine/CartItemsService.php new file mode 100644 index 000000000..1baa8f7c4 --- /dev/null +++ b/src/Service/CartLine/CartItemsService.php @@ -0,0 +1,112 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Service\CartLine; + +use Mollie\Adapter\Context; +use Mollie\Config\Config; +use Mollie\Service\VoucherService; +use Mollie\Utility\NumberUtility; +use Mollie\Utility\TextFormatUtility; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class CartItemsService +{ + /** + * @var Context + */ + private $context; + /** + * @var VoucherService + */ + private $voucherService; + + public function __construct(Context $context, VoucherService $voucherService) + { + $this->context = $context; + $this->voucherService = $voucherService; + } + + /** + * @param int $apiRoundingPrecision + * @param array $giftProducts + * @param string $selectedVoucherCategory + * @param float $remaining + * + * @return array + */ + public function createProductLines(array $cartItems, array $giftProducts, array $orderLines, string $selectedVoucherCategory, float $remaining): array + { + $apiRoundingPrecision = Config::API_ROUNDING_PRECISION; + + foreach ($cartItems as $cartItem) { + // Get the rounded total w/ tax + $roundedTotalWithTax = round($cartItem['total_wt'], $apiRoundingPrecision); + + // Skip if no qty + $quantity = (int) $cartItem['cart_quantity']; + if ($quantity <= 0 || $cartItem['price_wt'] <= 0) { + continue; + } + + // Generate the product hash + $idProduct = TextFormatUtility::formatNumber($cartItem['id_product'], 0); + $idProductAttribute = TextFormatUtility::formatNumber($cartItem['id_product_attribute'], 0); + $idCustomization = TextFormatUtility::formatNumber($cartItem['id_customization'], 0); + + $productHash = "{$idProduct}¤{$idProductAttribute}¤{$idCustomization}"; + + foreach ($giftProducts as $gift_product) { + if ($gift_product['id_product'] === $cartItem['id_product']) { + $quantity = NumberUtility::minus($quantity, $gift_product['cart_quantity']); + + $productHashGift = "{$idProduct}¤{$idProductAttribute}¤{$idCustomization}gift"; + $orderLines[$productHashGift][] = [ + 'name' => $cartItem['name'], + 'sku' => $productHashGift, + 'targetVat' => (float) $cartItem['rate'], + 'quantity' => $gift_product['cart_quantity'], + 'unitPrice' => 0, + 'totalAmount' => 0, + 'category' => '', + 'product_url' => $this->context->getProductLink($cartItem['id_product']), + 'image_url' => $this->context->getImageLink($cartItem['link_rewrite'], $cartItem['id_image']), + ]; + continue; + } + } + + if ((int) $quantity <= 0) { + continue; + } + + // Try to spread this product evenly and account for rounding differences on the order line + $orderLines[$productHash][] = [ + 'name' => $cartItem['name'], + 'sku' => $productHash, + 'targetVat' => (float) $cartItem['rate'], + 'quantity' => $quantity, + 'unitPrice' => round($cartItem['price_wt'], $apiRoundingPrecision), + 'totalAmount' => (float) $roundedTotalWithTax, + 'category' => $this->voucherService->getVoucherCategory($cartItem, $selectedVoucherCategory), + 'product_url' => $this->context->getProductLink($cartItem['id_product']), + 'image_url' => $this->context->getImageLink($cartItem['link_rewrite'], $cartItem['id_image']), + ]; + $remaining -= $roundedTotalWithTax; + } + + return [$orderLines, $remaining]; + } +} \ No newline at end of file diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 15486dd85..6bec18935 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -12,13 +12,14 @@ namespace Mollie\Service; -use Cart; -use Mollie\Adapter\Context; -use Mollie\Adapter\ToolsAdapter; use Mollie\Config\Config; use Mollie\DTO\Line; use Mollie\DTO\Object\Amount; use Mollie\DTO\PaymentFeeData; +use Mollie\Service\CartLine\CartItemDiscountService; +use Mollie\Service\CartLine\CartItemShippingLineService; +use Mollie\Service\CartLine\CartItemsService; +use Mollie\Service\CartLine\CartItemWrappingService; use Mollie\Utility\CalculationUtility; use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; @@ -30,28 +31,34 @@ class CartLinesService { - /** - * @var VoucherService - */ - private $voucherService; - /** * @var LanguageService */ private $languageService; - /** - * @var ToolsAdapter + * @var CartItemsService */ - private $tools; - private $context; - - public function __construct(LanguageService $languageService, VoucherService $voucherService, ToolsAdapter $tools, Context $context) + private $cartItemsService; + /** + * @var CartItemDiscountService + */ + private $cartItemDiscountService; + private $cartItemShippingLineService; + private $cartItemWrappingService; + + public function __construct( + LanguageService $languageService, + CartItemsService $cartItemsService, + CartItemDiscountService $cartItemDiscountService, + CartItemShippingLineService $cartItemShippingLineService, + CartItemWrappingService $cartItemWrappingService + ) { - $this->voucherService = $voucherService; $this->languageService = $languageService; - $this->tools = $tools; - $this->context = $context; + $this->cartItemsService = $cartItemsService; + $this->cartItemDiscountService = $cartItemDiscountService; + $this->cartItemShippingLineService = $cartItemShippingLineService; + $this->cartItemWrappingService = $cartItemWrappingService; } public function buildCartLines( @@ -63,6 +70,7 @@ public function buildCartLines( $totalPrice = round($amount, $apiRoundingPrecision); $roundedShippingCost = round($shippingCost, $apiRoundingPrecision); + foreach ($cartSummary['discounts'] as $discount) { if ($discount['free_shipping']) { $roundedShippingCost = 0; @@ -70,7 +78,6 @@ public function buildCartLines( } $wrappingPrice = $psGiftWrapping ? round($cartSummary['total_wrapping'], $apiRoundingPrecision) : 0; - $totalDiscounts = isset($cartSummary['total_discounts']) ? $cartSummary['total_discounts'] : 0; $remaining = round( CalculationUtility::getCartRemainingPrice((float) $totalPrice, (float) $roundedShippingCost, (float) $wrappingPrice), $apiRoundingPrecision @@ -78,17 +85,13 @@ public function buildCartLines( $orderLines = []; - // Create product lines from cart items - $productLinesResult = $this->cartItemsService->createProductLines($cartItems, $cartSummary, $selectedVoucherCategory); - $orderLines = $productLinesResult['orderLines']; - $remaining += $productLinesResult['remaining']; + /* Item */ + list($orderLines, $remaining) = $this->cartItemsService->createProductLines($cartItems, $cartSummary['gift_products'], $orderLines, $selectedVoucherCategory, $remaining); // Add discounts to the order lines - //todo move this inside discount service $totalDiscounts = isset($cartSummary['total_discounts']) ? $cartSummary['total_discounts'] : 0; - $discountsResult = $this->discountService->addDiscounts($orderLines, $cartSummary['total_discounts']); - $orderLines = $discountsResult['orderLines']; - $remaining += $discountsResult['remaining']; + list($orderLines, $remaining) = $this->cartItemDiscountService->addDiscountsToProductLines($totalDiscounts, $orderLines, $remaining); + //todo move these both methods inside some kind of utility class // Compensate for order total rounding inaccuracies @@ -98,23 +101,17 @@ public function buildCartLines( $orderLines = $this->fillProductLinesWithRemainingData($orderLines, $apiRoundingPrecision, $vatRatePrecision); // Add shipping costs to the order lines - $shippingResult = $this->shippingService->addShippingLine($orderLines, $shippingCost, $cartSummary); - $orderLines = $shippingResult['orderLines']; - $remaining += $shippingResult['remaining']; + $orderLines = $this->cartItemShippingLineService->addShippingLine($roundedShippingCost, $cartSummary, $orderLines); // Add wrapping costs to the order lines - $wrappingResult = $this->wrappingService->addWrappingLine($orderLines, $cartSummary, $psGiftWrapping); - $orderLines = $shippingResult['orderLines']; - $remaining += $shippingResult['remaining']; + $orderLines = $this->cartItemWrappingService->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $orderLines); // Add payment fees to the order lines - $paymentFeeResult = $this->paymentFeeService->addPaymentFee($orderLines, $paymentFeeData); - $orderLines = $shippingResult['orderLines']; - $remaining += $shippingResult['remaining']; + $orderLines = $this->paymentFeeService->addPaymentFee($orderLines, $paymentFeeData); - return $orderLines; - // Finalize and return order lines -// return $this->finalizeOrderLines($orderLines, $currencyIsoCode); + $newItems = $this->ungroupLines($orderLines); + + return $this->convertToLineArray($newItems, $currencyIsoCode, $apiRoundingPrecision); } /** @@ -163,10 +160,10 @@ public function getCartLines( $orderLines = []; /* Item */ - list($orderLines, $remaining) = $this->createProductLines($cartItems, $apiRoundingPrecision, $cartSummary['gift_products'], $orderLines, $selectedVoucherCategory, $remaining); +// [$orderLines, $remaining] = $this->createProductLines($cartItems, $apiRoundingPrecision, $cartSummary['gift_products'], $orderLines, $selectedVoucherCategory, $remaining); // Add discount if applicable - list($orderLines, $remaining) = $this->addDiscountsToProductLines($totalDiscounts, $apiRoundingPrecision, $orderLines, $remaining); +// [$orderLines, $remaining] = $this->addDiscountsToProductLines($totalDiscounts, $apiRoundingPrecision, $orderLines, $remaining); // Compensate for order total rounding inaccuracies $orderLines = $this->compositeRoundingInaccuracies($remaining, $apiRoundingPrecision, $orderLines); @@ -175,7 +172,7 @@ public function getCartLines( $orderLines = $this->fillProductLinesWithRemainingData($orderLines, $apiRoundingPrecision, $vatRatePrecision); // Add shipping - $orderLines = $this->addShippingLine($roundedShippingCost, $cartSummary, $apiRoundingPrecision, $orderLines); +// $orderLines = $this->addShippingLine($roundedShippingCost, $cartSummary, $apiRoundingPrecision, $orderLines); // Add wrapping $orderLines = $this->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $apiRoundingPrecision, $orderLines); @@ -225,103 +222,6 @@ public static function spreadCartLineGroup($cartLineGroup, $newTotal) return $newCartLineGroup; } - /** - * @param int $apiRoundingPrecision - * @param array $giftProducts - * @param string $selectedVoucherCategory - * @param float $remaining - * - * @return array - */ - private function createProductLines(array $cartItems, $apiRoundingPrecision, $giftProducts, array $orderLines, $selectedVoucherCategory, $remaining) - { - foreach ($cartItems as $cartItem) { - // Get the rounded total w/ tax - $roundedTotalWithTax = round($cartItem['total_wt'], $apiRoundingPrecision); - - // Skip if no qty - $quantity = (int) $cartItem['cart_quantity']; - if ($quantity <= 0 || $cartItem['price_wt'] <= 0) { - continue; - } - - // Generate the product hash - $idProduct = TextFormatUtility::formatNumber($cartItem['id_product'], 0); - $idProductAttribute = TextFormatUtility::formatNumber($cartItem['id_product_attribute'], 0); - $idCustomization = TextFormatUtility::formatNumber($cartItem['id_customization'], 0); - - $productHash = "{$idProduct}¤{$idProductAttribute}¤{$idCustomization}"; - - foreach ($giftProducts as $gift_product) { - if ($gift_product['id_product'] === $cartItem['id_product']) { - $quantity = NumberUtility::minus($quantity, $gift_product['cart_quantity']); - - $productHashGift = "{$idProduct}¤{$idProductAttribute}¤{$idCustomization}gift"; - $orderLines[$productHashGift][] = [ - 'name' => $cartItem['name'], - 'sku' => $productHashGift, - 'targetVat' => (float) $cartItem['rate'], - 'quantity' => $gift_product['cart_quantity'], - 'unitPrice' => 0, - 'totalAmount' => 0, - 'category' => '', - 'product_url' => $this->context->getProductLink($cartItem['id_product']), - 'image_url' => $this->context->getImageLink($cartItem['link_rewrite'], $cartItem['id_image']), - ]; - continue; - } - } - - if ((int) $quantity <= 0) { - continue; - } - - // Try to spread this product evenly and account for rounding differences on the order line - $orderLines[$productHash][] = [ - 'name' => $cartItem['name'], - 'sku' => $productHash, - 'targetVat' => (float) $cartItem['rate'], - 'quantity' => $quantity, - 'unitPrice' => round($cartItem['price_wt'], $apiRoundingPrecision), - 'totalAmount' => (float) $roundedTotalWithTax, - 'category' => $this->voucherService->getVoucherCategory($cartItem, $selectedVoucherCategory), - 'product_url' => $this->context->getProductLink($cartItem['id_product']), - 'image_url' => $this->context->getImageLink($cartItem['link_rewrite'], $cartItem['id_image']), - ]; - $remaining -= $roundedTotalWithTax; - } - - return [$orderLines, $remaining]; - } - - /** - * @param float $totalDiscounts - * @param int $apiRoundingPrecision - * @param array $orderLines - * @param float $remaining - * - * @return array - */ - private function addDiscountsToProductLines($totalDiscounts, $apiRoundingPrecision, $orderLines, $remaining) - { - if ($totalDiscounts >= 0.01) { - $orderLines['discount'] = [ - [ - 'name' => 'Discount', - 'type' => 'discount', - 'quantity' => 1, - 'unitPrice' => -round($totalDiscounts, $apiRoundingPrecision), - 'totalAmount' => -round($totalDiscounts, $apiRoundingPrecision), - 'targetVat' => 0, - 'category' => '', - ], - ]; - $remaining = NumberUtility::plus($remaining, $totalDiscounts); - } - - return [$orderLines, $remaining]; - } - /** * @param float $remaining * @param int $apiRoundingPrecision @@ -422,66 +322,6 @@ private function fillProductLinesWithRemainingData(array $orderLines, $apiRoundi return $orderLines; } - /** - * @param float $roundedShippingCost - * @param array $cartSummary - * @param int $apiRoundingPrecision - * - * @return array - */ - private function addShippingLine($roundedShippingCost, $cartSummary, $apiRoundingPrecision, array $orderLines) - { - if (round($roundedShippingCost, 2) > 0) { - $shippingVatRate = round(($cartSummary['total_shipping'] - $cartSummary['total_shipping_tax_exc']) / $cartSummary['total_shipping_tax_exc'] * 100, $apiRoundingPrecision); - - $orderLines['shipping'] = [ - [ - 'name' => $this->languageService->lang('Shipping'), - 'quantity' => 1, - 'unitPrice' => round($roundedShippingCost, $apiRoundingPrecision), - 'totalAmount' => round($roundedShippingCost, $apiRoundingPrecision), - 'vatAmount' => round($roundedShippingCost * $shippingVatRate / ($shippingVatRate + 100), $apiRoundingPrecision), - 'vatRate' => $shippingVatRate, - ], - ]; - } - - return $orderLines; - } - - /** - * @param float $wrappingPrice - * @param int $vatRatePrecision - * @param int $apiRoundingPrecision - * - * @return array - */ - private function addWrappingLine($wrappingPrice, array $cartSummary, $vatRatePrecision, $apiRoundingPrecision, array $orderLines) - { - if (round($wrappingPrice, 2) > 0) { - $wrappingVatRate = round( - CalculationUtility::getActualVatRate( - $cartSummary['total_wrapping'], - $cartSummary['total_wrapping_tax_exc'] - ), - $vatRatePrecision - ); - - $orderLines['wrapping'] = [ - [ - 'name' => $this->languageService->lang('Gift wrapping'), - 'quantity' => 1, - 'unitPrice' => round($wrappingPrice, $apiRoundingPrecision), - 'totalAmount' => round($wrappingPrice, $apiRoundingPrecision), - 'vatAmount' => round($wrappingPrice * $wrappingVatRate / ($wrappingVatRate + 100), $apiRoundingPrecision), - 'vatRate' => $wrappingVatRate, - ], - ]; - } - - return $orderLines; - } - /** * @param PaymentFeeData $paymentFeeData * @param int $apiRoundingPrecision From 6abac4e2ac37989935c99dcaf03e1b7ae77d28fe Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 11:22:06 +0200 Subject: [PATCH 03/28] [PIPRES-460] created new service --- .../CartLine/CartItemPaymentFeeService.php | 63 +++++++++++++ .../CartLine/CartItemProductLinesService.php | 88 +++++++++++++++++++ src/Service/CartLinesService.php | 33 +------ 3 files changed, 154 insertions(+), 30 deletions(-) create mode 100644 src/Service/CartLine/CartItemPaymentFeeService.php create mode 100644 src/Service/CartLine/CartItemProductLinesService.php diff --git a/src/Service/CartLine/CartItemPaymentFeeService.php b/src/Service/CartLine/CartItemPaymentFeeService.php new file mode 100644 index 000000000..c423d5511 --- /dev/null +++ b/src/Service/CartLine/CartItemPaymentFeeService.php @@ -0,0 +1,63 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace mollie\src\Service\CartLine; + +use Mollie\Config\Config; +use Mollie\DTO\PaymentFeeData; +use Mollie\Service\LanguageService; +use Mollie\Utility\CalculationUtility; +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 + */ + private function addPaymentFeeLine($paymentFeeData, array $orderLines) + { + 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; + } +} diff --git a/src/Service/CartLine/CartItemProductLinesService.php b/src/Service/CartLine/CartItemProductLinesService.php new file mode 100644 index 000000000..3aee2e655 --- /dev/null +++ b/src/Service/CartLine/CartItemProductLinesService.php @@ -0,0 +1,88 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Service\CartLine; + +use Mollie\Config\Config; +use Mollie\Utility\CalculationUtility; +use Mollie\Utility\NumberUtility; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class CartItemProductLinesService +{ + public function __construct() + { + } + + /** + * @param int $vatRatePrecision + * + * @return array + * + * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException + */ + private function fillProductLinesWithRemainingData(array $orderLines, $vatRatePrecision) + { + $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( + $vatAmount = 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; + } +} diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 6bec18935..4ef0d4d47 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -169,16 +169,16 @@ public function getCartLines( $orderLines = $this->compositeRoundingInaccuracies($remaining, $apiRoundingPrecision, $orderLines); // Fill the order lines with the rest of the data (tax, total amount, etc.) - $orderLines = $this->fillProductLinesWithRemainingData($orderLines, $apiRoundingPrecision, $vatRatePrecision); +// $orderLines = $this->fillProductLinesWithRemainingData($orderLines, $apiRoundingPrecision, $vatRatePrecision); // Add shipping // $orderLines = $this->addShippingLine($roundedShippingCost, $cartSummary, $apiRoundingPrecision, $orderLines); // Add wrapping - $orderLines = $this->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $apiRoundingPrecision, $orderLines); +// $orderLines = $this->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $apiRoundingPrecision, $orderLines); // Add fee - $orderLines = $this->addPaymentFeeLine($paymentFeeData, $apiRoundingPrecision, $orderLines); +// $orderLines = $this->addPaymentFeeLine($paymentFeeData, $apiRoundingPrecision, $orderLines); // Ungroup all the cart lines, just one level $newItems = $this->ungroupLines($orderLines); @@ -322,33 +322,6 @@ private function fillProductLinesWithRemainingData(array $orderLines, $apiRoundi return $orderLines; } - /** - * @param PaymentFeeData $paymentFeeData - * @param int $apiRoundingPrecision - * - * @return array - */ - private function addPaymentFeeLine($paymentFeeData, $apiRoundingPrecision, array $orderLines) - { - if (!$paymentFeeData->isActive()) { - return $orderLines; - } - - $orderLines['surcharge'] = [ - [ - 'name' => $this->languageService->lang('Payment fee'), - 'sku' => Config::PAYMENT_FEE_SKU, - 'quantity' => 1, - 'unitPrice' => round($paymentFeeData->getPaymentFeeTaxIncl(), $apiRoundingPrecision), - 'totalAmount' => round($paymentFeeData->getPaymentFeeTaxIncl(), $apiRoundingPrecision), - 'vatAmount' => NumberUtility::minus($paymentFeeData->getPaymentFeeTaxIncl(), $paymentFeeData->getPaymentFeeTaxExcl()), - 'vatRate' => $paymentFeeData->getTaxRate(), - ], - ]; - - return $orderLines; - } - /** * @return array */ From 10adc01a26aa9561c191828866e1e32fad73d646 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 11:24:10 +0200 Subject: [PATCH 04/28] [PIPRES-460] updated phpdocs --- src/Service/CartLine/CartItemDiscountService.php | 3 +-- src/Service/CartLine/CartItemShippingLineService.php | 3 +-- src/Service/CartLine/CartItemWrappingService.php | 10 +++++++++- src/Service/CartLine/CartItemsService.php | 3 +-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Service/CartLine/CartItemDiscountService.php b/src/Service/CartLine/CartItemDiscountService.php index 4f6576313..6df8b31bf 100644 --- a/src/Service/CartLine/CartItemDiscountService.php +++ b/src/Service/CartLine/CartItemDiscountService.php @@ -23,7 +23,6 @@ class CartItemDiscountService { /** * @param float $totalDiscounts - * @param int $apiRoundingPrecision * @param array $orderLines * @param float $remaining * @@ -48,4 +47,4 @@ public function addDiscountsToProductLines(float $totalDiscounts, array $orderLi return [$orderLines, $remaining]; } -} \ No newline at end of file +} diff --git a/src/Service/CartLine/CartItemShippingLineService.php b/src/Service/CartLine/CartItemShippingLineService.php index ac9df826a..f2bbe96c1 100644 --- a/src/Service/CartLine/CartItemShippingLineService.php +++ b/src/Service/CartLine/CartItemShippingLineService.php @@ -33,7 +33,6 @@ public function __construct(LanguageService $languageService) /** * @param float $roundedShippingCost * @param array $cartSummary - * @param int $apiRoundingPrecision * * @return array */ @@ -56,4 +55,4 @@ public function addShippingLine($roundedShippingCost, $cartSummary, array $order return $orderLines; } -} \ No newline at end of file +} diff --git a/src/Service/CartLine/CartItemWrappingService.php b/src/Service/CartLine/CartItemWrappingService.php index 8746d49ab..7d921b2d1 100644 --- a/src/Service/CartLine/CartItemWrappingService.php +++ b/src/Service/CartLine/CartItemWrappingService.php @@ -32,6 +32,14 @@ public function __construct(LanguageService $languageService) $this->languageService = $languageService; } + /** + * @param float $wrappingPrice + * @param array $cartSummary + * @param int $vatRatePrecision + * @param array $orderLines + * + * @return array + */ public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $vatRatePrecision, array $orderLines) { if (round($wrappingPrice, 2) > 0) { @@ -57,4 +65,4 @@ public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $v return $orderLines; } -} \ No newline at end of file +} diff --git a/src/Service/CartLine/CartItemsService.php b/src/Service/CartLine/CartItemsService.php index 1baa8f7c4..a4899afe0 100644 --- a/src/Service/CartLine/CartItemsService.php +++ b/src/Service/CartLine/CartItemsService.php @@ -40,7 +40,6 @@ public function __construct(Context $context, VoucherService $voucherService) } /** - * @param int $apiRoundingPrecision * @param array $giftProducts * @param string $selectedVoucherCategory * @param float $remaining @@ -109,4 +108,4 @@ public function createProductLines(array $cartItems, array $giftProducts, array return [$orderLines, $remaining]; } -} \ No newline at end of file +} From 8b9a50a3ba158e88a55e109ffe4c87dd9effcd78 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 11:41:49 +0200 Subject: [PATCH 05/28] [PIPRES-460] changed methods visibility, added via dependency injection --- src/Service/CartLine/CartItemPaymentFeeService.php | 2 +- src/Service/CartLine/CartItemProductLinesService.php | 2 +- src/Service/CartLinesService.php | 12 ++++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Service/CartLine/CartItemPaymentFeeService.php b/src/Service/CartLine/CartItemPaymentFeeService.php index c423d5511..cfba1acac 100644 --- a/src/Service/CartLine/CartItemPaymentFeeService.php +++ b/src/Service/CartLine/CartItemPaymentFeeService.php @@ -40,7 +40,7 @@ public function __construct(LanguageService $languageService) * * @return array */ - private function addPaymentFeeLine($paymentFeeData, array $orderLines) + public function addPaymentFeeLine($paymentFeeData, array $orderLines) { if (!$paymentFeeData->isActive()) { return $orderLines; diff --git a/src/Service/CartLine/CartItemProductLinesService.php b/src/Service/CartLine/CartItemProductLinesService.php index 3aee2e655..a12e49b52 100644 --- a/src/Service/CartLine/CartItemProductLinesService.php +++ b/src/Service/CartLine/CartItemProductLinesService.php @@ -33,7 +33,7 @@ public function __construct() * * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException */ - private function fillProductLinesWithRemainingData(array $orderLines, $vatRatePrecision) + public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePrecision) { $roundingPrecision = CONFIG::API_ROUNDING_PRECISION; diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 4ef0d4d47..e46fdd97e 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -17,9 +17,11 @@ use Mollie\DTO\Object\Amount; use Mollie\DTO\PaymentFeeData; use Mollie\Service\CartLine\CartItemDiscountService; +use Mollie\Service\CartLine\CartItemProductLinesService; use Mollie\Service\CartLine\CartItemShippingLineService; use Mollie\Service\CartLine\CartItemsService; use Mollie\Service\CartLine\CartItemWrappingService; +use mollie\src\Service\CartLine\CartItemPaymentFeeService; use Mollie\Utility\CalculationUtility; use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; @@ -45,13 +47,17 @@ class CartLinesService private $cartItemDiscountService; private $cartItemShippingLineService; private $cartItemWrappingService; + private $cartItemProductLinesService; + private $cartItemPaymentFeeService; public function __construct( LanguageService $languageService, CartItemsService $cartItemsService, CartItemDiscountService $cartItemDiscountService, CartItemShippingLineService $cartItemShippingLineService, - CartItemWrappingService $cartItemWrappingService + CartItemWrappingService $cartItemWrappingService, + CartItemProductLinesService $cartItemProductLinesService, + CartItemPaymentFeeService $cartItemPaymentFeeService ) { $this->languageService = $languageService; @@ -59,6 +65,8 @@ public function __construct( $this->cartItemDiscountService = $cartItemDiscountService; $this->cartItemShippingLineService = $cartItemShippingLineService; $this->cartItemWrappingService = $cartItemWrappingService; + $this->cartItemProductLinesService = $cartItemProductLinesService; + $this->cartItemPaymentFeeService = $cartItemPaymentFeeService; } public function buildCartLines( @@ -178,7 +186,7 @@ public function getCartLines( // $orderLines = $this->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $apiRoundingPrecision, $orderLines); // Add fee -// $orderLines = $this->addPaymentFeeLine($paymentFeeData, $apiRoundingPrecision, $orderLines); +// $orderLines = $this->cartItemPaymentFeeService->addPaymentFeeLine($paymentFeeData, $orderLines); // Ungroup all the cart lines, just one level $newItems = $this->ungroupLines($orderLines); From 15720727f4a7015975f9cb61e36515942c27014f Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 11:46:18 +0200 Subject: [PATCH 06/28] [PIPRES-460] added service methods on main service --- src/Service/CartLinesService.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index e46fdd97e..9ce3e6412 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -168,25 +168,25 @@ public function getCartLines( $orderLines = []; /* Item */ -// [$orderLines, $remaining] = $this->createProductLines($cartItems, $apiRoundingPrecision, $cartSummary['gift_products'], $orderLines, $selectedVoucherCategory, $remaining); + [$orderLines, $remaining] = $this->cartItemsService->createProductLines($cartItems, $cartSummary['gift_products'], $orderLines, $selectedVoucherCategory, $remaining); // Add discount if applicable -// [$orderLines, $remaining] = $this->addDiscountsToProductLines($totalDiscounts, $apiRoundingPrecision, $orderLines, $remaining); + [$orderLines, $remaining] = $this->cartItemDiscountService->addDiscountsToProductLines($totalDiscounts, $orderLines, $remaining); // Compensate for order total rounding inaccuracies $orderLines = $this->compositeRoundingInaccuracies($remaining, $apiRoundingPrecision, $orderLines); // Fill the order lines with the rest of the data (tax, total amount, etc.) -// $orderLines = $this->fillProductLinesWithRemainingData($orderLines, $apiRoundingPrecision, $vatRatePrecision); + $orderLines = $this->cartItemProductLinesService->fillProductLinesWithRemainingData($orderLines, $vatRatePrecision); // Add shipping -// $orderLines = $this->addShippingLine($roundedShippingCost, $cartSummary, $apiRoundingPrecision, $orderLines); + $orderLines = $this->cartItemShippingLineService->addShippingLine($roundedShippingCost, $cartSummary, $orderLines); // Add wrapping -// $orderLines = $this->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $apiRoundingPrecision, $orderLines); + $orderLines = $this->cartItemWrappingService->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $orderLines); // Add fee -// $orderLines = $this->cartItemPaymentFeeService->addPaymentFeeLine($paymentFeeData, $orderLines); + $orderLines = $this->cartItemPaymentFeeService->addPaymentFeeLine($paymentFeeData, $orderLines); // Ungroup all the cart lines, just one level $newItems = $this->ungroupLines($orderLines); From d8843dcce7178356d14077db579eb3412b77144b Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 13:26:22 +0200 Subject: [PATCH 07/28] [PIPRES-460] added LineUtility --- src/Service/CartLinesService.php | 65 ++++---------------------- src/Utility/LineUtility.php | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 56 deletions(-) create mode 100644 src/Utility/LineUtility.php diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 9ce3e6412..137fa84a2 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -22,6 +22,7 @@ use Mollie\Service\CartLine\CartItemsService; use Mollie\Service\CartLine\CartItemWrappingService; use mollie\src\Service\CartLine\CartItemPaymentFeeService; +use mollie\src\Utility\LineUtility; use Mollie\Utility\CalculationUtility; use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; @@ -49,6 +50,7 @@ class CartLinesService private $cartItemWrappingService; private $cartItemProductLinesService; private $cartItemPaymentFeeService; + private $lineUtility; public function __construct( LanguageService $languageService, @@ -57,7 +59,8 @@ public function __construct( CartItemShippingLineService $cartItemShippingLineService, CartItemWrappingService $cartItemWrappingService, CartItemProductLinesService $cartItemProductLinesService, - CartItemPaymentFeeService $cartItemPaymentFeeService + CartItemPaymentFeeService $cartItemPaymentFeeService, + LineUtility $lineUtility ) { $this->languageService = $languageService; @@ -67,8 +70,10 @@ public function __construct( $this->cartItemWrappingService = $cartItemWrappingService; $this->cartItemProductLinesService = $cartItemProductLinesService; $this->cartItemPaymentFeeService = $cartItemPaymentFeeService; + $this->lineUtility = $lineUtility; } + // new public function buildCartLines( $amount, $paymentFeeData, $currencyIsoCode, $cartSummary, $shippingCost, $cartItems, $psGiftWrapping, $selectedVoucherCategory ) { @@ -115,11 +120,11 @@ public function buildCartLines( $orderLines = $this->cartItemWrappingService->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $orderLines); // Add payment fees to the order lines - $orderLines = $this->paymentFeeService->addPaymentFee($orderLines, $paymentFeeData); + $orderLines = $this->cartItemPaymentFeeService->addPaymentFeeLine($paymentFeeData, $orderLines); $newItems = $this->ungroupLines($orderLines); - return $this->convertToLineArray($newItems, $currencyIsoCode, $apiRoundingPrecision); + return $this->lineUtility->convertToLineArray($newItems, $currencyIsoCode, $apiRoundingPrecision); } /** @@ -136,6 +141,7 @@ public function buildCartLines( * * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException */ + // old public function getCartLines( $amount, $paymentFeeData, @@ -344,57 +350,4 @@ private function ungroupLines(array $orderLines) return $newItems; } - - /** - * @param string $currencyIsoCode - * @param int $apiRoundingPrecision - * - * @return array - */ - private function convertToLineArray(array $newItems, $currencyIsoCode, $apiRoundingPrecision) - { - foreach ($newItems as $index => $item) { - $line = new Line(); - $line->setName($item['name'] ?: $item['sku']); - $line->setQuantity((int) $item['quantity']); - $line->setSku(isset($item['sku']) ? $item['sku'] : ''); - - $currency = strtoupper(strtolower($currencyIsoCode)); - - if (isset($item['discount'])) { - $line->setDiscountAmount(new Amount( - $currency, - TextFormatUtility::formatNumber($item['discount'], $apiRoundingPrecision, '.', '') - ) - ); - } - - $line->setUnitPrice(new Amount( - $currency, - TextFormatUtility::formatNumber($item['unitPrice'], $apiRoundingPrecision, '.', '') - )); - - $line->setTotalPrice(new Amount( - $currency, - TextFormatUtility::formatNumber($item['totalAmount'], $apiRoundingPrecision, '.', '') - )); - - $line->setVatAmount(new Amount( - $currency, - TextFormatUtility::formatNumber($item['vatAmount'], $apiRoundingPrecision, '.', '') - )); - - if (isset($item['category'])) { - $line->setCategory($item['category']); - } - - $line->setVatRate(TextFormatUtility::formatNumber($item['vatRate'], $apiRoundingPrecision, '.', '')); - $line->setProductUrl($item['product_url'] ?? null); - $line->setImageUrl($item['image_url'] ?? null); - - $newItems[$index] = $line; - } - - return $newItems; - } } diff --git a/src/Utility/LineUtility.php b/src/Utility/LineUtility.php new file mode 100644 index 000000000..ba88023e6 --- /dev/null +++ b/src/Utility/LineUtility.php @@ -0,0 +1,79 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace mollie\src\Utility; + +use Mollie\Config\Config; +use Mollie\DTO\Line; +use Mollie\DTO\Object\Amount; +use Mollie\Utility\TextFormatUtility; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class LineUtility +{ + /** + * @param string $currencyIsoCode + * @param int $apiRoundingPrecision + * + * @return array + */ + public function convertToLineArray(array $newItems, $currencyIsoCode) + { + $roundingPrecision = CONFIG::API_ROUNDING_PRECISION; + foreach ($newItems as $index => $item) { + $line = new Line(); + $line->setName($item['name'] ?: $item['sku']); + $line->setQuantity((int) $item['quantity']); + $line->setSku(isset($item['sku']) ? $item['sku'] : ''); + + $currency = strtoupper(strtolower($currencyIsoCode)); + + if (isset($item['discount'])) { + $line->setDiscountAmount(new Amount( + $currency, + TextFormatUtility::formatNumber($item['discount'], $roundingPrecision, '.', '') + ) + ); + } + + $line->setUnitPrice(new Amount( + $currency, + TextFormatUtility::formatNumber($item['unitPrice'], $roundingPrecision, '.', '') + )); + + $line->setTotalPrice(new Amount( + $currency, + TextFormatUtility::formatNumber($item['totalAmount'], $roundingPrecision, '.', '') + )); + + $line->setVatAmount(new Amount( + $currency, + TextFormatUtility::formatNumber($item['vatAmount'], $roundingPrecision, '.', '') + )); + + if (isset($item['category'])) { + $line->setCategory($item['category']); + } + + $line->setVatRate(TextFormatUtility::formatNumber($item['vatRate'], $roundingPrecision, '.', '')); + $line->setProductUrl($item['product_url'] ?? null); + $line->setImageUrl($item['image_url'] ?? null); + + $newItems[$index] = $line; + } + + return $newItems; + } +} From 8566734edbe7f97592737a0f80b729e4064ba99d Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 13:44:58 +0200 Subject: [PATCH 08/28] [PIPRES-460] updated LineUtility --- src/Service/CartLinesService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 137fa84a2..f68e9373d 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -111,7 +111,7 @@ public function buildCartLines( $orderLines = $this->compositeRoundingInaccuracies($remaining, $apiRoundingPrecision, $orderLines); // Fill the order lines with the rest of the data (tax, total amount, etc.) - $orderLines = $this->fillProductLinesWithRemainingData($orderLines, $apiRoundingPrecision, $vatRatePrecision); + $orderLines = $this->cartItemProductLinesService->fillProductLinesWithRemainingData($orderLines, $vatRatePrecision); // Add shipping costs to the order lines $orderLines = $this->cartItemShippingLineService->addShippingLine($roundedShippingCost, $cartSummary, $orderLines); @@ -124,7 +124,7 @@ public function buildCartLines( $newItems = $this->ungroupLines($orderLines); - return $this->lineUtility->convertToLineArray($newItems, $currencyIsoCode, $apiRoundingPrecision); + return $this->lineUtility->convertToLineArray($newItems, $currencyIsoCode); } /** From f038742b7b853bab3188ce3cb3eca0cadc5ba4d0 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 14:28:09 +0200 Subject: [PATCH 09/28] [PIPRES-460] refactor code --- src/Service/CartLine/CartItemsService.php | 42 ++++++++++++++++++++--- src/Service/CartLinesService.php | 39 ++------------------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/Service/CartLine/CartItemsService.php b/src/Service/CartLine/CartItemsService.php index a4899afe0..d4e82a49d 100644 --- a/src/Service/CartLine/CartItemsService.php +++ b/src/Service/CartLine/CartItemsService.php @@ -15,6 +15,7 @@ use Mollie\Adapter\Context; use Mollie\Config\Config; use Mollie\Service\VoucherService; +use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; use Mollie\Utility\TextFormatUtility; @@ -48,11 +49,9 @@ public function __construct(Context $context, VoucherService $voucherService) */ public function createProductLines(array $cartItems, array $giftProducts, array $orderLines, string $selectedVoucherCategory, float $remaining): array { - $apiRoundingPrecision = Config::API_ROUNDING_PRECISION; - foreach ($cartItems as $cartItem) { // Get the rounded total w/ tax - $roundedTotalWithTax = round($cartItem['total_wt'], $apiRoundingPrecision); + $roundedTotalWithTax = round($cartItem['total_wt'], Config::API_ROUNDING_PRECISION); // Skip if no qty $quantity = (int) $cartItem['cart_quantity']; @@ -97,7 +96,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array 'sku' => $productHash, 'targetVat' => (float) $cartItem['rate'], 'quantity' => $quantity, - 'unitPrice' => round($cartItem['price_wt'], $apiRoundingPrecision), + 'unitPrice' => round($cartItem['price_wt'], Config::API_ROUNDING_PRECISION), 'totalAmount' => (float) $roundedTotalWithTax, 'category' => $this->voucherService->getVoucherCategory($cartItem, $selectedVoucherCategory), 'product_url' => $this->context->getProductLink($cartItem['id_product']), @@ -108,4 +107,39 @@ public function createProductLines(array $cartItems, array $giftProducts, array return [$orderLines, $remaining]; } + + /** + * Spread the cart line amount evenly. + * + * Optionally split into multiple lines in case of rounding inaccuracies + * + * @param array[] $cartLineGroup Cart Line Group WITHOUT VAT details (except target VAT rate) + * @param float $newTotal + * + * @return array[] + * + * @since 3.2.2 + * @since 3.3.3 Omits VAT details + */ + public static function spreadCartLineGroup($cartLineGroup, $newTotal) + { + $newTotal = round($newTotal, Config::API_ROUNDING_PRECISION); + $quantity = array_sum(array_column($cartLineGroup, 'quantity')); + $newCartLineGroup = []; + $spread = CartPriceUtility::spreadAmountEvenly($newTotal, $quantity); + + foreach ($spread as $unitPrice => $qty) { + $newCartLineGroup[] = [ + 'name' => $cartLineGroup[0]['name'], + 'quantity' => $qty, + 'unitPrice' => (float) $unitPrice, + 'totalAmount' => (float) $unitPrice * $qty, + 'sku' => isset($cartLineGroup[0]['sku']) ? $cartLineGroup[0]['sku'] : '', + 'targetVat' => $cartLineGroup[0]['targetVat'], + 'category' => $cartLineGroup[0]['category'], + ]; + } + + return $newCartLineGroup; + } } diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index f68e9373d..c9d21cea5 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -198,42 +198,7 @@ public function getCartLines( $newItems = $this->ungroupLines($orderLines); // Convert floats to strings for the Mollie API and add additional info - return $this->convertToLineArray($newItems, $currencyIsoCode, $apiRoundingPrecision); - } - - /** - * Spread the cart line amount evenly. - * - * Optionally split into multiple lines in case of rounding inaccuracies - * - * @param array[] $cartLineGroup Cart Line Group WITHOUT VAT details (except target VAT rate) - * @param float $newTotal - * - * @return array[] - * - * @since 3.2.2 - * @since 3.3.3 Omits VAT details - */ - public static function spreadCartLineGroup($cartLineGroup, $newTotal) - { - $apiRoundingPrecision = Config::API_ROUNDING_PRECISION; - $newTotal = round($newTotal, $apiRoundingPrecision); - $quantity = array_sum(array_column($cartLineGroup, 'quantity')); - $newCartLineGroup = []; - $spread = CartPriceUtility::spreadAmountEvenly($newTotal, $quantity); - foreach ($spread as $unitPrice => $qty) { - $newCartLineGroup[] = [ - 'name' => $cartLineGroup[0]['name'], - 'quantity' => $qty, - 'unitPrice' => (float) $unitPrice, - 'totalAmount' => (float) $unitPrice * $qty, - 'sku' => isset($cartLineGroup[0]['sku']) ? $cartLineGroup[0]['sku'] : '', - 'targetVat' => $cartLineGroup[0]['targetVat'], - 'category' => $cartLineGroup[0]['category'], - ]; - } - - return $newCartLineGroup; + return $this->lineUtility->convertToLineArray($newItems, $currencyIsoCode, $apiRoundingPrecision); } /** @@ -269,7 +234,7 @@ private function compositeRoundingInaccuracies($remaining, $apiRoundingPrecision // Grab the line group's total amount $totalAmount = array_sum(array_column($items, 'totalAmount')); // Otherwise spread the cart line again with the updated total - $orderLines[$hash] = static::spreadCartLineGroup($items, $totalAmount + $remaining); + $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); break; } } From b5384e3cf837f51a98afbeae60bf6d20b10d999d Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 14:41:11 +0200 Subject: [PATCH 10/28] [PIPRES-460] added RoundingUtility --- src/Service/CartLinesService.php | 95 ++++++++++++-------------------- src/Utility/RoundingUtility.php | 63 +++++++++++++++++++++ 2 files changed, 97 insertions(+), 61 deletions(-) create mode 100644 src/Utility/RoundingUtility.php diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index c9d21cea5..8aa1dbd46 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -23,6 +23,7 @@ use Mollie\Service\CartLine\CartItemWrappingService; use mollie\src\Service\CartLine\CartItemPaymentFeeService; use mollie\src\Utility\LineUtility; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CalculationUtility; use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; @@ -34,10 +35,6 @@ class CartLinesService { - /** - * @var LanguageService - */ - private $languageService; /** * @var CartItemsService */ @@ -51,19 +48,19 @@ class CartLinesService private $cartItemProductLinesService; private $cartItemPaymentFeeService; private $lineUtility; + private $roundingUtility; public function __construct( - LanguageService $languageService, CartItemsService $cartItemsService, CartItemDiscountService $cartItemDiscountService, CartItemShippingLineService $cartItemShippingLineService, CartItemWrappingService $cartItemWrappingService, CartItemProductLinesService $cartItemProductLinesService, CartItemPaymentFeeService $cartItemPaymentFeeService, - LineUtility $lineUtility + LineUtility $lineUtility, + RoundingUtility $roundingUtility ) { - $this->languageService = $languageService; $this->cartItemsService = $cartItemsService; $this->cartItemDiscountService = $cartItemDiscountService; $this->cartItemShippingLineService = $cartItemShippingLineService; @@ -71,18 +68,35 @@ public function __construct( $this->cartItemProductLinesService = $cartItemProductLinesService; $this->cartItemPaymentFeeService = $cartItemPaymentFeeService; $this->lineUtility = $lineUtility; + $this->roundingUtility = $roundingUtility; } // new + /** + * @param float $amount + * @param PaymentFeeData $paymentFeeData + * @param string $currencyIsoCode + * @param array $cartSummary + * @param float $shippingCost + * @param array $cartItems + * @param bool $psGiftWrapping + * @param string $selectedVoucherCategory + * + * @return array + * + * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException + */ public function buildCartLines( - $amount, $paymentFeeData, $currencyIsoCode, $cartSummary, $shippingCost, $cartItems, $psGiftWrapping, $selectedVoucherCategory + $amount, + $paymentFeeData, + $currencyIsoCode, $cartSummary, + $shippingCost, + $cartItems, + $psGiftWrapping, + $selectedVoucherCategory ) { - - $apiRoundingPrecision = Config::API_ROUNDING_PRECISION; - $vatRatePrecision = Config::VAT_RATE_ROUNDING_PRECISION; - - $totalPrice = round($amount, $apiRoundingPrecision); - $roundedShippingCost = round($shippingCost, $apiRoundingPrecision); + $totalPrice = round($amount, Config::API_ROUNDING_PRECISION); + $roundedShippingCost = round($shippingCost, Config::API_ROUNDING_PRECISION); foreach ($cartSummary['discounts'] as $discount) { if ($discount['free_shipping']) { @@ -90,10 +104,10 @@ public function buildCartLines( } } - $wrappingPrice = $psGiftWrapping ? round($cartSummary['total_wrapping'], $apiRoundingPrecision) : 0; + $wrappingPrice = $psGiftWrapping ? round($cartSummary['total_wrapping'], Config::API_ROUNDING_PRECISION) : 0; $remaining = round( CalculationUtility::getCartRemainingPrice((float) $totalPrice, (float) $roundedShippingCost, (float) $wrappingPrice), - $apiRoundingPrecision + Config::API_ROUNDING_PRECISION ); $orderLines = []; @@ -108,16 +122,16 @@ public function buildCartLines( //todo move these both methods inside some kind of utility class // Compensate for order total rounding inaccuracies - $orderLines = $this->compositeRoundingInaccuracies($remaining, $apiRoundingPrecision, $orderLines); + $orderLines = $this->roundingUtility->compositeRoundingInaccuracies($remaining, $orderLines); // Fill the order lines with the rest of the data (tax, total amount, etc.) - $orderLines = $this->cartItemProductLinesService->fillProductLinesWithRemainingData($orderLines, $vatRatePrecision); + $orderLines = $this->cartItemProductLinesService->fillProductLinesWithRemainingData($orderLines, Config::VAT_RATE_ROUNDING_PRECISION); // Add shipping costs to the order lines $orderLines = $this->cartItemShippingLineService->addShippingLine($roundedShippingCost, $cartSummary, $orderLines); // Add wrapping costs to the order lines - $orderLines = $this->cartItemWrappingService->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $orderLines); + $orderLines = $this->cartItemWrappingService->addWrappingLine($wrappingPrice, $cartSummary, Config::VAT_RATE_ROUNDING_PRECISION, $orderLines); // Add payment fees to the order lines $orderLines = $this->cartItemPaymentFeeService->addPaymentFeeLine($paymentFeeData, $orderLines); @@ -180,7 +194,7 @@ public function getCartLines( [$orderLines, $remaining] = $this->cartItemDiscountService->addDiscountsToProductLines($totalDiscounts, $orderLines, $remaining); // Compensate for order total rounding inaccuracies - $orderLines = $this->compositeRoundingInaccuracies($remaining, $apiRoundingPrecision, $orderLines); + $orderLines = $this->roundingUtility->compositeRoundingInaccuracies($remaining, $orderLines); // Fill the order lines with the rest of the data (tax, total amount, etc.) $orderLines = $this->cartItemProductLinesService->fillProductLinesWithRemainingData($orderLines, $vatRatePrecision); @@ -201,47 +215,6 @@ public function getCartLines( return $this->lineUtility->convertToLineArray($newItems, $currencyIsoCode, $apiRoundingPrecision); } - /** - * @param float $remaining - * @param int $apiRoundingPrecision - * @param array $orderLines - * - * @return array - */ - private function compositeRoundingInaccuracies($remaining, $apiRoundingPrecision, $orderLines) - { - $remaining = round($remaining, $apiRoundingPrecision); - if ($remaining < 0) { - foreach (array_reverse($orderLines) as $hash => $items) { - // Grab the line group's total amount - $totalAmount = array_sum(array_column($items, 'totalAmount')); - - // Remove when total is lower than remaining - if ($totalAmount <= $remaining) { - // The line total is less than remaining, we should remove this line group and continue - $remaining = $remaining - $totalAmount; - unset($items); - continue; - } - - // Otherwise spread the cart line again with the updated total - //TODO: check why remaining comes -100 when testing and new total becomes different - $orderLines[$hash] = static::spreadCartLineGroup($items, $totalAmount + $remaining); - break; - } - } elseif ($remaining > 0) { - foreach (array_reverse($orderLines) as $hash => $items) { - // Grab the line group's total amount - $totalAmount = array_sum(array_column($items, 'totalAmount')); - // Otherwise spread the cart line again with the updated total - $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); - break; - } - } - - return $orderLines; - } - /** * @param int $apiRoundingPrecision * @param int $vatRatePrecision diff --git a/src/Utility/RoundingUtility.php b/src/Utility/RoundingUtility.php new file mode 100644 index 000000000..738d64fdf --- /dev/null +++ b/src/Utility/RoundingUtility.php @@ -0,0 +1,63 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace mollie\src\Utility; + +use Mollie\Config\Config; +use Mollie\Service\CartLine\CartItemsService; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class RoundingUtility +{ + /** + * @param float $remaining + * @param array $orderLines + * + * @return array + */ + public function compositeRoundingInaccuracies($remaining, $orderLines) + { + $remaining = round($remaining, CONFIG::API_ROUNDING_PRECISION); + if ($remaining < 0) { + foreach (array_reverse($orderLines) as $hash => $items) { + // Grab the line group's total amount + $totalAmount = array_sum(array_column($items, 'totalAmount')); + + // Remove when total is lower than remaining + if ($totalAmount <= $remaining) { + // The line total is less than remaining, we should remove this line group and continue + $remaining = $remaining - $totalAmount; + unset($items); + continue; + } + + // Otherwise spread the cart line again with the updated total + //TODO: check why remaining comes -100 when testing and new total becomes different + $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); + break; + } + } elseif ($remaining > 0) { + foreach (array_reverse($orderLines) as $hash => $items) { + // Grab the line group's total amount + $totalAmount = array_sum(array_column($items, 'totalAmount')); + // Otherwise spread the cart line again with the updated total + $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); + break; + } + } + + return $orderLines; + } +} From 573dcfdc85d1e8b53d2538554b9c25d86326be99 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 14:51:20 +0200 Subject: [PATCH 11/28] [PIPRES-460] added rounding method --- src/Service/CartLinesService.php | 28 ++++++++++++++-------------- src/Utility/RoundingUtility.php | 13 ++++++++++++- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 8aa1dbd46..0fcb0ce31 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -95,8 +95,8 @@ public function buildCartLines( $psGiftWrapping, $selectedVoucherCategory ) { - $totalPrice = round($amount, Config::API_ROUNDING_PRECISION); - $roundedShippingCost = round($shippingCost, Config::API_ROUNDING_PRECISION); + $totalPrice = $this->roundingUtility->round($amount, Config::API_ROUNDING_PRECISION); + $roundedShippingCost = $this->roundingUtility->round($shippingCost, Config::API_ROUNDING_PRECISION); foreach ($cartSummary['discounts'] as $discount) { if ($discount['free_shipping']) { @@ -104,8 +104,8 @@ public function buildCartLines( } } - $wrappingPrice = $psGiftWrapping ? round($cartSummary['total_wrapping'], Config::API_ROUNDING_PRECISION) : 0; - $remaining = round( + $wrappingPrice = $psGiftWrapping ? $this->roundingUtility->round($cartSummary['total_wrapping'], Config::API_ROUNDING_PRECISION) : 0; + $remaining = $this->roundingUtility->round( CalculationUtility::getCartRemainingPrice((float) $totalPrice, (float) $roundedShippingCost, (float) $wrappingPrice), Config::API_ROUNDING_PRECISION ); @@ -171,17 +171,17 @@ public function getCartLines( $apiRoundingPrecision = Config::API_ROUNDING_PRECISION; $vatRatePrecision = Config::VAT_RATE_ROUNDING_PRECISION; - $totalPrice = round($amount, $apiRoundingPrecision); - $roundedShippingCost = round($shippingCost, $apiRoundingPrecision); + $totalPrice = $this->roundingUtility->round($amount, $apiRoundingPrecision); + $roundedShippingCost = $this->roundingUtility->round($shippingCost, $apiRoundingPrecision); foreach ($cartSummary['discounts'] as $discount) { if ($discount['free_shipping']) { $roundedShippingCost = 0; } } - $wrappingPrice = $psGiftWrapping ? round($cartSummary['total_wrapping'], $apiRoundingPrecision) : 0; + $wrappingPrice = $psGiftWrapping ? $this->roundingUtility->round($cartSummary['total_wrapping'], $apiRoundingPrecision) : 0; $totalDiscounts = isset($cartSummary['total_discounts']) ? $cartSummary['total_discounts'] : 0; - $remaining = round( + $remaining = $this->roundingUtility->round( CalculationUtility::getCartRemainingPrice((float) $totalPrice, (float) $roundedShippingCost, (float) $wrappingPrice), $apiRoundingPrecision ); @@ -230,7 +230,7 @@ private function fillProductLinesWithRemainingData(array $orderLines, $apiRoundi $quantity = (int) $line['quantity']; $targetVat = $line['targetVat']; $unitPrice = $line['unitPrice']; - $unitPriceNoTax = round(CalculationUtility::getUnitPriceNoTax( + $unitPriceNoTax = $this->roundingUtility->round(CalculationUtility::getUnitPriceNoTax( $line['unitPrice'], $targetVat ), @@ -241,7 +241,7 @@ private function fillProductLinesWithRemainingData(array $orderLines, $apiRoundi $totalAmount = $line['totalAmount']; $actualVatRate = 0; if ($unitPriceNoTax > 0) { - $actualVatRate = round( + $actualVatRate = $this->roundingUtility->round( $vatAmount = CalculationUtility::getActualVatRate($unitPrice, $unitPriceNoTax, $quantity), $vatRatePrecision ); @@ -256,10 +256,10 @@ private function fillProductLinesWithRemainingData(array $orderLines, $apiRoundi 'name' => $line['name'], 'category' => $line['category'], 'quantity' => (int) $quantity, - 'unitPrice' => round($unitPrice, $apiRoundingPrecision), - 'totalAmount' => round($totalAmount, $apiRoundingPrecision), - 'vatRate' => round($actualVatRate, $apiRoundingPrecision), - 'vatAmount' => round($vatAmount, $apiRoundingPrecision), + 'unitPrice' => $this->roundingUtility->round($unitPrice, $apiRoundingPrecision), + 'totalAmount' => $this->roundingUtility->round($totalAmount, $apiRoundingPrecision), + 'vatRate' => $this->roundingUtility->round($actualVatRate, $apiRoundingPrecision), + 'vatAmount' => $this->roundingUtility->round($vatAmount, $apiRoundingPrecision), 'product_url' => $line['product_url'] ?? null, 'image_url' => $line['image_url'] ?? null, ]; diff --git a/src/Utility/RoundingUtility.php b/src/Utility/RoundingUtility.php index 738d64fdf..f6a63074a 100644 --- a/src/Utility/RoundingUtility.php +++ b/src/Utility/RoundingUtility.php @@ -21,6 +21,17 @@ class RoundingUtility { + /** + * @param float $amount + * @param int $precision + * + * @return float + */ + public function round(float $amount, int $precision): float + { + return round($amount, $precision); + } + /** * @param float $remaining * @param array $orderLines @@ -29,7 +40,7 @@ class RoundingUtility */ public function compositeRoundingInaccuracies($remaining, $orderLines) { - $remaining = round($remaining, CONFIG::API_ROUNDING_PRECISION); + $remaining = $this->round($remaining, CONFIG::API_ROUNDING_PRECISION); if ($remaining < 0) { foreach (array_reverse($orderLines) as $hash => $items) { // Grab the line group's total amount From 6952ef72bfa0a2aa748c04aa60a5ccfd833f8579 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 14:55:32 +0200 Subject: [PATCH 12/28] [PIPRES-460] alignment, comment styling --- src/Service/CartLinesService.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 0fcb0ce31..240a7fc25 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -89,7 +89,8 @@ public function __construct( public function buildCartLines( $amount, $paymentFeeData, - $currencyIsoCode, $cartSummary, + $currencyIsoCode, + $cartSummary, $shippingCost, $cartItems, $psGiftWrapping, @@ -105,24 +106,24 @@ public function buildCartLines( } $wrappingPrice = $psGiftWrapping ? $this->roundingUtility->round($cartSummary['total_wrapping'], Config::API_ROUNDING_PRECISION) : 0; - $remaining = $this->roundingUtility->round( + + $remainingAmount = $this->roundingUtility->round( CalculationUtility::getCartRemainingPrice((float) $totalPrice, (float) $roundedShippingCost, (float) $wrappingPrice), Config::API_ROUNDING_PRECISION ); $orderLines = []; - /* Item */ - list($orderLines, $remaining) = $this->cartItemsService->createProductLines($cartItems, $cartSummary['gift_products'], $orderLines, $selectedVoucherCategory, $remaining); + // Item + list($orderLines, $remainingAmount) = $this->cartItemsService->createProductLines($cartItems, $cartSummary['gift_products'], $orderLines, $selectedVoucherCategory, $remainingAmount); // Add discounts to the order lines - $totalDiscounts = isset($cartSummary['total_discounts']) ? $cartSummary['total_discounts'] : 0; - list($orderLines, $remaining) = $this->cartItemDiscountService->addDiscountsToProductLines($totalDiscounts, $orderLines, $remaining); + $totalDiscounts = $cartSummary['total_discounts'] ?? 0; + list($orderLines, $remainingAmount) = $this->cartItemDiscountService->addDiscountsToProductLines($totalDiscounts, $orderLines, $remainingAmount); - //todo move these both methods inside some kind of utility class // Compensate for order total rounding inaccuracies - $orderLines = $this->roundingUtility->compositeRoundingInaccuracies($remaining, $orderLines); + $orderLines = $this->roundingUtility->compositeRoundingInaccuracies($remainingAmount, $orderLines); // Fill the order lines with the rest of the data (tax, total amount, etc.) $orderLines = $this->cartItemProductLinesService->fillProductLinesWithRemainingData($orderLines, Config::VAT_RATE_ROUNDING_PRECISION); @@ -131,7 +132,7 @@ public function buildCartLines( $orderLines = $this->cartItemShippingLineService->addShippingLine($roundedShippingCost, $cartSummary, $orderLines); // Add wrapping costs to the order lines - $orderLines = $this->cartItemWrappingService->addWrappingLine($wrappingPrice, $cartSummary, Config::VAT_RATE_ROUNDING_PRECISION, $orderLines); + $orderLines = $this->cartItemWrappingService->addWrappingLine($wrappingPrice, $cartSummary, Config::VAT_RATE_ROUNDING_PRECISION, $orderLines); // Add payment fees to the order lines $orderLines = $this->cartItemPaymentFeeService->addPaymentFeeLine($paymentFeeData, $orderLines); From f53e73cbed1674115ee85563cba97e89261a7cb9 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 15:35:57 +0200 Subject: [PATCH 13/28] [PIPRES-460] moved method to ArrayUtility --- src/Service/CartLinesService.php | 23 ++++++----------------- src/Utility/ArrayUtility.php | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 240a7fc25..af8ad1157 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -24,6 +24,7 @@ use mollie\src\Service\CartLine\CartItemPaymentFeeService; use mollie\src\Utility\LineUtility; use mollie\src\Utility\RoundingUtility; +use Mollie\Utility\ArrayUtility; use Mollie\Utility\CalculationUtility; use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; @@ -49,6 +50,7 @@ class CartLinesService private $cartItemPaymentFeeService; private $lineUtility; private $roundingUtility; + private $arrayUtility; public function __construct( CartItemsService $cartItemsService, @@ -58,7 +60,8 @@ public function __construct( CartItemProductLinesService $cartItemProductLinesService, CartItemPaymentFeeService $cartItemPaymentFeeService, LineUtility $lineUtility, - RoundingUtility $roundingUtility + RoundingUtility $roundingUtility, + ArrayUtility $arrayUtility ) { $this->cartItemsService = $cartItemsService; @@ -69,6 +72,7 @@ public function __construct( $this->cartItemPaymentFeeService = $cartItemPaymentFeeService; $this->lineUtility = $lineUtility; $this->roundingUtility = $roundingUtility; + $this->arrayUtility = $arrayUtility; } // new @@ -137,7 +141,7 @@ public function buildCartLines( // Add payment fees to the order lines $orderLines = $this->cartItemPaymentFeeService->addPaymentFeeLine($paymentFeeData, $orderLines); - $newItems = $this->ungroupLines($orderLines); + $newItems = $this->arrayUtility->ungroupLines($orderLines); return $this->lineUtility->convertToLineArray($newItems, $currencyIsoCode); } @@ -274,19 +278,4 @@ private function fillProductLinesWithRemainingData(array $orderLines, $apiRoundi return $orderLines; } - - /** - * @return array - */ - private function ungroupLines(array $orderLines) - { - $newItems = []; - foreach ($orderLines as &$items) { - foreach ($items as &$item) { - $newItems[] = $item; - } - } - - return $newItems; - } } diff --git a/src/Utility/ArrayUtility.php b/src/Utility/ArrayUtility.php index e769dd2ba..4b752e37e 100644 --- a/src/Utility/ArrayUtility.php +++ b/src/Utility/ArrayUtility.php @@ -22,4 +22,21 @@ public static function getLastElement($array) { return end($array); } + + /** + * @param array $orderLines + * + * @return array + */ + public function ungroupLines(array $orderLines) + { + $newItems = []; + foreach ($orderLines as &$items) { + foreach ($items as &$item) { + $newItems[] = $item; + } + } + + return $newItems; + } } From ed95407aa7334ca2d1517e5e6269c647e8b31209 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 15:36:31 +0200 Subject: [PATCH 14/28] [PIPRES-460] renamed argument --- src/Utility/ArrayUtility.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Utility/ArrayUtility.php b/src/Utility/ArrayUtility.php index 4b752e37e..a8a91458d 100644 --- a/src/Utility/ArrayUtility.php +++ b/src/Utility/ArrayUtility.php @@ -28,10 +28,10 @@ public static function getLastElement($array) * * @return array */ - public function ungroupLines(array $orderLines) + public function ungroupLines(array $lines) { $newItems = []; - foreach ($orderLines as &$items) { + foreach ($lines as &$items) { foreach ($items as &$item) { $newItems[] = $item; } From 4f2686bb68917efff49ef23392620373af3d2a1d Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 15:42:02 +0200 Subject: [PATCH 15/28] [PIPRES-460] cslint --- .../CartLine/CartItemPaymentFeeService.php | 1 - .../CartLine/CartItemShippingLineService.php | 5 ++- src/Service/CartLinesService.php | 35 ++++++++++--------- src/Utility/ArrayUtility.php | 2 +- src/Utility/LineUtility.php | 2 +- 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/Service/CartLine/CartItemPaymentFeeService.php b/src/Service/CartLine/CartItemPaymentFeeService.php index cfba1acac..29730292f 100644 --- a/src/Service/CartLine/CartItemPaymentFeeService.php +++ b/src/Service/CartLine/CartItemPaymentFeeService.php @@ -15,7 +15,6 @@ use Mollie\Config\Config; use Mollie\DTO\PaymentFeeData; use Mollie\Service\LanguageService; -use Mollie\Utility\CalculationUtility; use Mollie\Utility\NumberUtility; if (!defined('_PS_VERSION_')) { diff --git a/src/Service/CartLine/CartItemShippingLineService.php b/src/Service/CartLine/CartItemShippingLineService.php index f2bbe96c1..6ff8527eb 100644 --- a/src/Service/CartLine/CartItemShippingLineService.php +++ b/src/Service/CartLine/CartItemShippingLineService.php @@ -21,15 +21,14 @@ class CartItemShippingLineService { - /** - * @var LanguageService - */ + /* @var LanguageService */ private $languageService; public function __construct(LanguageService $languageService) { $this->languageService = $languageService; } + /** * @param float $roundedShippingCost * @param array $cartSummary diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index af8ad1157..3dc7b32e7 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -13,8 +13,6 @@ namespace Mollie\Service; use Mollie\Config\Config; -use Mollie\DTO\Line; -use Mollie\DTO\Object\Amount; use Mollie\DTO\PaymentFeeData; use Mollie\Service\CartLine\CartItemDiscountService; use Mollie\Service\CartLine\CartItemProductLinesService; @@ -26,9 +24,7 @@ use mollie\src\Utility\RoundingUtility; use Mollie\Utility\ArrayUtility; use Mollie\Utility\CalculationUtility; -use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; -use Mollie\Utility\TextFormatUtility; if (!defined('_PS_VERSION_')) { exit; @@ -36,20 +32,31 @@ class CartLinesService { - /** - * @var CartItemsService - */ + /* @var CartItemsService */ private $cartItemsService; - /** - * @var CartItemDiscountService - */ + + /* @var CartItemDiscountService */ private $cartItemDiscountService; + + /* @var CartItemShippingLineService */ private $cartItemShippingLineService; + + /* @var CartItemWrappingService */ private $cartItemWrappingService; + + /* @var CartItemProductLinesService */ private $cartItemProductLinesService; + + /* @var CartItemPaymentFeeService */ private $cartItemPaymentFeeService; + + /* @var LineUtility */ private $lineUtility; + + /* @var RoundingUtility */ private $roundingUtility; + + /* @var ArrayUtility */ private $arrayUtility; public function __construct( @@ -62,8 +69,7 @@ public function __construct( LineUtility $lineUtility, RoundingUtility $roundingUtility, ArrayUtility $arrayUtility - ) - { + ) { $this->cartItemsService = $cartItemsService; $this->cartItemDiscountService = $cartItemDiscountService; $this->cartItemShippingLineService = $cartItemShippingLineService; @@ -75,7 +81,6 @@ public function __construct( $this->arrayUtility = $arrayUtility; } - // new /** * @param float $amount * @param PaymentFeeData $paymentFeeData @@ -125,7 +130,6 @@ public function buildCartLines( $totalDiscounts = $cartSummary['total_discounts'] ?? 0; list($orderLines, $remainingAmount) = $this->cartItemDiscountService->addDiscountsToProductLines($totalDiscounts, $orderLines, $remainingAmount); - // Compensate for order total rounding inaccuracies $orderLines = $this->roundingUtility->compositeRoundingInaccuracies($remainingAmount, $orderLines); @@ -160,7 +164,6 @@ public function buildCartLines( * * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException */ - // old public function getCartLines( $amount, $paymentFeeData, @@ -214,7 +217,7 @@ public function getCartLines( $orderLines = $this->cartItemPaymentFeeService->addPaymentFeeLine($paymentFeeData, $orderLines); // Ungroup all the cart lines, just one level - $newItems = $this->ungroupLines($orderLines); + $newItems = $this->arrayUtility->ungroupLines($orderLines); // Convert floats to strings for the Mollie API and add additional info return $this->lineUtility->convertToLineArray($newItems, $currencyIsoCode, $apiRoundingPrecision); diff --git a/src/Utility/ArrayUtility.php b/src/Utility/ArrayUtility.php index a8a91458d..386f3d6d6 100644 --- a/src/Utility/ArrayUtility.php +++ b/src/Utility/ArrayUtility.php @@ -24,7 +24,7 @@ public static function getLastElement($array) } /** - * @param array $orderLines + * @param array $lines * * @return array */ diff --git a/src/Utility/LineUtility.php b/src/Utility/LineUtility.php index ba88023e6..f375853fd 100644 --- a/src/Utility/LineUtility.php +++ b/src/Utility/LineUtility.php @@ -25,7 +25,7 @@ class LineUtility { /** * @param string $currencyIsoCode - * @param int $apiRoundingPrecision + * @param string $currencyIsoCode * * @return array */ From a678dea0e70a7c2239f9531c75ae851d63b751c4 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 15:54:56 +0200 Subject: [PATCH 16/28] [PIPRES-460] added return types --- .../CartLine/CartItemPaymentFeeService.php | 2 +- .../CartLine/CartItemProductLinesService.php | 2 +- .../CartLine/CartItemShippingLineService.php | 2 +- .../CartLine/CartItemWrappingService.php | 2 +- src/Service/CartLinesService.php | 137 +----------------- src/Utility/ArrayUtility.php | 2 +- src/Utility/LineUtility.php | 2 +- src/Utility/RoundingUtility.php | 2 +- 8 files changed, 10 insertions(+), 141 deletions(-) diff --git a/src/Service/CartLine/CartItemPaymentFeeService.php b/src/Service/CartLine/CartItemPaymentFeeService.php index 29730292f..cbb654213 100644 --- a/src/Service/CartLine/CartItemPaymentFeeService.php +++ b/src/Service/CartLine/CartItemPaymentFeeService.php @@ -39,7 +39,7 @@ public function __construct(LanguageService $languageService) * * @return array */ - public function addPaymentFeeLine($paymentFeeData, array $orderLines) + public function addPaymentFeeLine($paymentFeeData, array $orderLines): array { if (!$paymentFeeData->isActive()) { return $orderLines; diff --git a/src/Service/CartLine/CartItemProductLinesService.php b/src/Service/CartLine/CartItemProductLinesService.php index a12e49b52..ef85e9330 100644 --- a/src/Service/CartLine/CartItemProductLinesService.php +++ b/src/Service/CartLine/CartItemProductLinesService.php @@ -33,7 +33,7 @@ public function __construct() * * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException */ - public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePrecision) + public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePrecision): array { $roundingPrecision = CONFIG::API_ROUNDING_PRECISION; diff --git a/src/Service/CartLine/CartItemShippingLineService.php b/src/Service/CartLine/CartItemShippingLineService.php index 6ff8527eb..b50efeb62 100644 --- a/src/Service/CartLine/CartItemShippingLineService.php +++ b/src/Service/CartLine/CartItemShippingLineService.php @@ -35,7 +35,7 @@ public function __construct(LanguageService $languageService) * * @return array */ - public function addShippingLine($roundedShippingCost, $cartSummary, array $orderLines) + public function addShippingLine($roundedShippingCost, $cartSummary, array $orderLines): array { if (round($roundedShippingCost, 2) > 0) { $shippingVatRate = round(($cartSummary['total_shipping'] - $cartSummary['total_shipping_tax_exc']) / $cartSummary['total_shipping_tax_exc'] * 100, Config::API_ROUNDING_PRECISION); diff --git a/src/Service/CartLine/CartItemWrappingService.php b/src/Service/CartLine/CartItemWrappingService.php index 7d921b2d1..b20fa4265 100644 --- a/src/Service/CartLine/CartItemWrappingService.php +++ b/src/Service/CartLine/CartItemWrappingService.php @@ -40,7 +40,7 @@ public function __construct(LanguageService $languageService) * * @return array */ - public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $vatRatePrecision, array $orderLines) + public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $vatRatePrecision, array $orderLines): array { if (round($wrappingPrice, 2) > 0) { $wrappingVatRate = round( diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 3dc7b32e7..c9b35eedc 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -95,7 +95,7 @@ public function __construct( * * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException */ - public function buildCartLines( + public function getCartLines( $amount, $paymentFeeData, $currencyIsoCode, @@ -104,7 +104,8 @@ public function buildCartLines( $cartItems, $psGiftWrapping, $selectedVoucherCategory - ) { + ): array + { $totalPrice = $this->roundingUtility->round($amount, Config::API_ROUNDING_PRECISION); $roundedShippingCost = $this->roundingUtility->round($shippingCost, Config::API_ROUNDING_PRECISION); @@ -149,136 +150,4 @@ public function buildCartLines( return $this->lineUtility->convertToLineArray($newItems, $currencyIsoCode); } - - /** - * @param float $amount - * @param PaymentFeeData $paymentFeeData - * @param string $currencyIsoCode - * @param array $cartSummary - * @param float $shippingCost - * @param array $cartItems - * @param bool $psGiftWrapping - * @param string $selectedVoucherCategory - * - * @return array - * - * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException - */ - public function getCartLines( - $amount, - $paymentFeeData, - $currencyIsoCode, - $cartSummary, - $shippingCost, - $cartItems, - $psGiftWrapping, - $selectedVoucherCategory - ) { - // TODO refactor whole service, split order line append into separate services and test them individually at least!!! - - $apiRoundingPrecision = Config::API_ROUNDING_PRECISION; - $vatRatePrecision = Config::VAT_RATE_ROUNDING_PRECISION; - - $totalPrice = $this->roundingUtility->round($amount, $apiRoundingPrecision); - $roundedShippingCost = $this->roundingUtility->round($shippingCost, $apiRoundingPrecision); - foreach ($cartSummary['discounts'] as $discount) { - if ($discount['free_shipping']) { - $roundedShippingCost = 0; - } - } - - $wrappingPrice = $psGiftWrapping ? $this->roundingUtility->round($cartSummary['total_wrapping'], $apiRoundingPrecision) : 0; - $totalDiscounts = isset($cartSummary['total_discounts']) ? $cartSummary['total_discounts'] : 0; - $remaining = $this->roundingUtility->round( - CalculationUtility::getCartRemainingPrice((float) $totalPrice, (float) $roundedShippingCost, (float) $wrappingPrice), - $apiRoundingPrecision - ); - - $orderLines = []; - /* Item */ - [$orderLines, $remaining] = $this->cartItemsService->createProductLines($cartItems, $cartSummary['gift_products'], $orderLines, $selectedVoucherCategory, $remaining); - - // Add discount if applicable - [$orderLines, $remaining] = $this->cartItemDiscountService->addDiscountsToProductLines($totalDiscounts, $orderLines, $remaining); - - // Compensate for order total rounding inaccuracies - $orderLines = $this->roundingUtility->compositeRoundingInaccuracies($remaining, $orderLines); - - // Fill the order lines with the rest of the data (tax, total amount, etc.) - $orderLines = $this->cartItemProductLinesService->fillProductLinesWithRemainingData($orderLines, $vatRatePrecision); - - // Add shipping - $orderLines = $this->cartItemShippingLineService->addShippingLine($roundedShippingCost, $cartSummary, $orderLines); - - // Add wrapping - $orderLines = $this->cartItemWrappingService->addWrappingLine($wrappingPrice, $cartSummary, $vatRatePrecision, $orderLines); - - // Add fee - $orderLines = $this->cartItemPaymentFeeService->addPaymentFeeLine($paymentFeeData, $orderLines); - - // Ungroup all the cart lines, just one level - $newItems = $this->arrayUtility->ungroupLines($orderLines); - - // Convert floats to strings for the Mollie API and add additional info - return $this->lineUtility->convertToLineArray($newItems, $currencyIsoCode, $apiRoundingPrecision); - } - - /** - * @param int $apiRoundingPrecision - * @param int $vatRatePrecision - * - * @return array - * - * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException - */ - private function fillProductLinesWithRemainingData(array $orderLines, $apiRoundingPrecision, $vatRatePrecision) - { - foreach ($orderLines as $productHash => $aItem) { - $orderLines[$productHash] = array_map(function ($line) use ($apiRoundingPrecision, $vatRatePrecision) { - $quantity = (int) $line['quantity']; - $targetVat = $line['targetVat']; - $unitPrice = $line['unitPrice']; - $unitPriceNoTax = $this->roundingUtility->round(CalculationUtility::getUnitPriceNoTax( - $line['unitPrice'], - $targetVat - ), - $apiRoundingPrecision - ); - - // Calculate VAT - $totalAmount = $line['totalAmount']; - $actualVatRate = 0; - if ($unitPriceNoTax > 0) { - $actualVatRate = $this->roundingUtility->round( - $vatAmount = 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' => $this->roundingUtility->round($unitPrice, $apiRoundingPrecision), - 'totalAmount' => $this->roundingUtility->round($totalAmount, $apiRoundingPrecision), - 'vatRate' => $this->roundingUtility->round($actualVatRate, $apiRoundingPrecision), - 'vatAmount' => $this->roundingUtility->round($vatAmount, $apiRoundingPrecision), - '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; - } } diff --git a/src/Utility/ArrayUtility.php b/src/Utility/ArrayUtility.php index 386f3d6d6..f96b48308 100644 --- a/src/Utility/ArrayUtility.php +++ b/src/Utility/ArrayUtility.php @@ -28,7 +28,7 @@ public static function getLastElement($array) * * @return array */ - public function ungroupLines(array $lines) + public function ungroupLines(array $lines): array { $newItems = []; foreach ($lines as &$items) { diff --git a/src/Utility/LineUtility.php b/src/Utility/LineUtility.php index f375853fd..6ec3b732d 100644 --- a/src/Utility/LineUtility.php +++ b/src/Utility/LineUtility.php @@ -29,7 +29,7 @@ class LineUtility * * @return array */ - public function convertToLineArray(array $newItems, $currencyIsoCode) + public function convertToLineArray(array $newItems, $currencyIsoCode): array { $roundingPrecision = CONFIG::API_ROUNDING_PRECISION; foreach ($newItems as $index => $item) { diff --git a/src/Utility/RoundingUtility.php b/src/Utility/RoundingUtility.php index f6a63074a..718eeb18e 100644 --- a/src/Utility/RoundingUtility.php +++ b/src/Utility/RoundingUtility.php @@ -38,7 +38,7 @@ public function round(float $amount, int $precision): float * * @return array */ - public function compositeRoundingInaccuracies($remaining, $orderLines) + public function compositeRoundingInaccuracies($remaining, $orderLines): array { $remaining = $this->round($remaining, CONFIG::API_ROUNDING_PRECISION); if ($remaining < 0) { From 43f63ba05f89020d5f17a68df714739564e06a69 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:14:17 +0200 Subject: [PATCH 17/28] [PIPRES-460] added RoundingUtility widely --- .../CartLine/CartItemDiscountService.php | 13 ++++++++++-- .../CartLine/CartItemPaymentFeeService.php | 12 +++++------ .../CartLine/CartItemProductLinesService.php | 19 ++++++++++------- .../CartLine/CartItemShippingLineService.php | 17 +++++++++------ .../CartLine/CartItemWrappingService.php | 21 +++++++++++-------- src/Service/CartLine/CartItemsService.php | 15 ++++++++----- src/Utility/RoundingUtility.php | 12 +++++++++-- 7 files changed, 72 insertions(+), 37 deletions(-) diff --git a/src/Service/CartLine/CartItemDiscountService.php b/src/Service/CartLine/CartItemDiscountService.php index 6df8b31bf..90c2dcd5c 100644 --- a/src/Service/CartLine/CartItemDiscountService.php +++ b/src/Service/CartLine/CartItemDiscountService.php @@ -13,6 +13,7 @@ namespace Mollie\Service\CartLine; use Mollie\Config\Config; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\NumberUtility; if (!defined('_PS_VERSION_')) { @@ -21,6 +22,14 @@ class CartItemDiscountService { + /* @var RoundingUtility */ + private $roundingUtility; + + public function __construct(RoundingUtility $roundingUtility) + { + $this->roundingUtility = $roundingUtility; + } + /** * @param float $totalDiscounts * @param array $orderLines @@ -36,8 +45,8 @@ public function addDiscountsToProductLines(float $totalDiscounts, array $orderLi 'name' => 'Discount', 'type' => 'discount', 'quantity' => 1, - 'unitPrice' => -round($totalDiscounts, Config::API_ROUNDING_PRECISION), - 'totalAmount' => -round($totalDiscounts, Config::API_ROUNDING_PRECISION), + 'unitPrice' => -$this->roundingUtility->round($totalDiscounts, Config::API_ROUNDING_PRECISION), + 'totalAmount' => -$this->roundingUtility->round($totalDiscounts, Config::API_ROUNDING_PRECISION), 'targetVat' => 0, 'category' => '', ], diff --git a/src/Service/CartLine/CartItemPaymentFeeService.php b/src/Service/CartLine/CartItemPaymentFeeService.php index cbb654213..c3a6e1fd0 100644 --- a/src/Service/CartLine/CartItemPaymentFeeService.php +++ b/src/Service/CartLine/CartItemPaymentFeeService.php @@ -15,6 +15,7 @@ use Mollie\Config\Config; use Mollie\DTO\PaymentFeeData; use Mollie\Service\LanguageService; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\NumberUtility; if (!defined('_PS_VERSION_')) { @@ -23,14 +24,13 @@ class CartItemPaymentFeeService { - /** - * @var LanguageService - */ + /* @var LanguageService */ private $languageService; - public function __construct(LanguageService $languageService) + public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) { $this->languageService = $languageService; + $this->roundingUtility = $roundingUtility; } /** @@ -50,8 +50,8 @@ public function addPaymentFeeLine($paymentFeeData, array $orderLines): array '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), + 'unitPrice' => $this->roundingUtility->round($paymentFeeData->getPaymentFeeTaxIncl(), CONFIG::API_ROUNDING_PRECISION), + 'totalAmount' => $this->roundingUtility->round($paymentFeeData->getPaymentFeeTaxIncl(), CONFIG::API_ROUNDING_PRECISION), 'vatAmount' => NumberUtility::minus($paymentFeeData->getPaymentFeeTaxIncl(), $paymentFeeData->getPaymentFeeTaxExcl()), 'vatRate' => $paymentFeeData->getTaxRate(), ], diff --git a/src/Service/CartLine/CartItemProductLinesService.php b/src/Service/CartLine/CartItemProductLinesService.php index ef85e9330..851b51f77 100644 --- a/src/Service/CartLine/CartItemProductLinesService.php +++ b/src/Service/CartLine/CartItemProductLinesService.php @@ -13,6 +13,7 @@ namespace Mollie\Service\CartLine; use Mollie\Config\Config; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CalculationUtility; use Mollie\Utility\NumberUtility; @@ -22,8 +23,12 @@ class CartItemProductLinesService { - public function __construct() + /* @var RoundingUtility */ + private $roundingUtility; + + public function __construct(RoundingUtility $roundingUtility) { + $this->roundingUtility = $roundingUtility; } /** @@ -42,7 +47,7 @@ public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePre $quantity = (int) $line['quantity']; $targetVat = $line['targetVat']; $unitPrice = $line['unitPrice']; - $unitPriceNoTax = round(CalculationUtility::getUnitPriceNoTax( + $unitPriceNoTax = $this->roundingUtility->round(CalculationUtility::getUnitPriceNoTax( $line['unitPrice'], $targetVat ), @@ -53,7 +58,7 @@ public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePre $totalAmount = $line['totalAmount']; $actualVatRate = 0; if ($unitPriceNoTax > 0) { - $actualVatRate = round( + $actualVatRate = $this->roundingUtility->round( $vatAmount = CalculationUtility::getActualVatRate($unitPrice, $unitPriceNoTax, $quantity), $vatRatePrecision ); @@ -68,10 +73,10 @@ public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePre '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), + 'unitPrice' => $this->roundingUtility->round($unitPrice, $roundingPrecision), + 'totalAmount' => $this->roundingUtility->round($totalAmount, $roundingPrecision), + 'vatRate' => $this->roundingUtility->round($actualVatRate, $roundingPrecision), + 'vatAmount' => $this->roundingUtility->round($vatAmount, $roundingPrecision), 'product_url' => $line['product_url'] ?? null, 'image_url' => $line['image_url'] ?? null, ]; diff --git a/src/Service/CartLine/CartItemShippingLineService.php b/src/Service/CartLine/CartItemShippingLineService.php index b50efeb62..4585235ea 100644 --- a/src/Service/CartLine/CartItemShippingLineService.php +++ b/src/Service/CartLine/CartItemShippingLineService.php @@ -14,6 +14,7 @@ use Mollie\Config\Config; use Mollie\Service\LanguageService; +use mollie\src\Utility\RoundingUtility; if (!defined('_PS_VERSION_')) { exit; @@ -24,9 +25,13 @@ class CartItemShippingLineService /* @var LanguageService */ private $languageService; - public function __construct(LanguageService $languageService) + /* @var RoundingUtility */ + private $roundingUtility; + + public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) { $this->languageService = $languageService; + $this->roundingUtility = $roundingUtility; } /** @@ -37,16 +42,16 @@ public function __construct(LanguageService $languageService) */ public function addShippingLine($roundedShippingCost, $cartSummary, array $orderLines): array { - if (round($roundedShippingCost, 2) > 0) { - $shippingVatRate = round(($cartSummary['total_shipping'] - $cartSummary['total_shipping_tax_exc']) / $cartSummary['total_shipping_tax_exc'] * 100, Config::API_ROUNDING_PRECISION); + if ($this->roundingUtility->round($roundedShippingCost, 2) > 0) { + $shippingVatRate = $this->roundingUtility->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), + 'unitPrice' => $this->roundingUtility->round($roundedShippingCost, Config::API_ROUNDING_PRECISION), + 'totalAmount' => $this->roundingUtility->round($roundedShippingCost, Config::API_ROUNDING_PRECISION), + 'vatAmount' => $this->roundingUtility->round($roundedShippingCost * $shippingVatRate / ($shippingVatRate + 100), Config::API_ROUNDING_PRECISION), 'vatRate' => $shippingVatRate, ], ]; diff --git a/src/Service/CartLine/CartItemWrappingService.php b/src/Service/CartLine/CartItemWrappingService.php index b20fa4265..e1d51ba1e 100644 --- a/src/Service/CartLine/CartItemWrappingService.php +++ b/src/Service/CartLine/CartItemWrappingService.php @@ -14,6 +14,7 @@ use Mollie\Config\Config; use Mollie\Service\LanguageService; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CalculationUtility; if (!defined('_PS_VERSION_')) { @@ -22,14 +23,16 @@ class CartItemWrappingService { - /** - * @var LanguageService - */ + /* @var LanguageService */ private $languageService; - public function __construct(LanguageService $languageService) + /* @var RoundingUtility */ + private $roundingUtility; + + public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) { $this->languageService = $languageService; + $this->roundingUtility = $roundingUtility; } /** @@ -42,8 +45,8 @@ public function __construct(LanguageService $languageService) */ public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $vatRatePrecision, array $orderLines): array { - if (round($wrappingPrice, 2) > 0) { - $wrappingVatRate = round( + if ($this->roundingUtility->round($wrappingPrice, 2) > 0) { + $wrappingVatRate = $this->roundingUtility->round( CalculationUtility::getActualVatRate( $cartSummary['total_wrapping'], $cartSummary['total_wrapping_tax_exc'] @@ -55,9 +58,9 @@ public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $v [ 'name' => $this->languageService->lang('Gift wrapping'), 'quantity' => 1, - 'unitPrice' => round($wrappingPrice, Config::API_ROUNDING_PRECISION), - 'totalAmount' => round($wrappingPrice, Config::API_ROUNDING_PRECISION), - 'vatAmount' => round($wrappingPrice * $wrappingVatRate / ($wrappingVatRate + 100), Config::API_ROUNDING_PRECISION), + 'unitPrice' => $this->roundingUtility->round($wrappingPrice, Config::API_ROUNDING_PRECISION), + 'totalAmount' => $this->roundingUtility->round($wrappingPrice, Config::API_ROUNDING_PRECISION), + 'vatAmount' => $this->roundingUtility->round($wrappingPrice * $wrappingVatRate / ($wrappingVatRate + 100), Config::API_ROUNDING_PRECISION), 'vatRate' => $wrappingVatRate, ], ]; diff --git a/src/Service/CartLine/CartItemsService.php b/src/Service/CartLine/CartItemsService.php index d4e82a49d..988308dee 100644 --- a/src/Service/CartLine/CartItemsService.php +++ b/src/Service/CartLine/CartItemsService.php @@ -15,6 +15,7 @@ use Mollie\Adapter\Context; use Mollie\Config\Config; use Mollie\Service\VoucherService; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; use Mollie\Utility\TextFormatUtility; @@ -34,10 +35,14 @@ class CartItemsService */ private $voucherService; - public function __construct(Context $context, VoucherService $voucherService) + /* @var RoundingUtility */ + private $roundingUtility; + + public function __construct(Context $context, VoucherService $voucherService, RoundingUtility $roundingUtility) { $this->context = $context; $this->voucherService = $voucherService; + $this->roundingUtility = $roundingUtility; } /** @@ -51,7 +56,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array { foreach ($cartItems as $cartItem) { // Get the rounded total w/ tax - $roundedTotalWithTax = round($cartItem['total_wt'], Config::API_ROUNDING_PRECISION); + $roundedTotalWithTax = $this->roundingUtility->round($cartItem['total_wt'], Config::API_ROUNDING_PRECISION); // Skip if no qty $quantity = (int) $cartItem['cart_quantity']; @@ -96,7 +101,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array 'sku' => $productHash, 'targetVat' => (float) $cartItem['rate'], 'quantity' => $quantity, - 'unitPrice' => round($cartItem['price_wt'], Config::API_ROUNDING_PRECISION), + 'unitPrice' => $this->roundingUtility->round($cartItem['price_wt'], Config::API_ROUNDING_PRECISION), 'totalAmount' => (float) $roundedTotalWithTax, 'category' => $this->voucherService->getVoucherCategory($cartItem, $selectedVoucherCategory), 'product_url' => $this->context->getProductLink($cartItem['id_product']), @@ -121,9 +126,9 @@ public function createProductLines(array $cartItems, array $giftProducts, array * @since 3.2.2 * @since 3.3.3 Omits VAT details */ - public static function spreadCartLineGroup($cartLineGroup, $newTotal) + public function spreadCartLineGroup($cartLineGroup, $newTotal) { - $newTotal = round($newTotal, Config::API_ROUNDING_PRECISION); + $newTotal = $this->roundingUtility->round($newTotal, Config::API_ROUNDING_PRECISION); $quantity = array_sum(array_column($cartLineGroup, 'quantity')); $newCartLineGroup = []; $spread = CartPriceUtility::spreadAmountEvenly($newTotal, $quantity); diff --git a/src/Utility/RoundingUtility.php b/src/Utility/RoundingUtility.php index 718eeb18e..3c49a17bc 100644 --- a/src/Utility/RoundingUtility.php +++ b/src/Utility/RoundingUtility.php @@ -21,6 +21,14 @@ class RoundingUtility { + /* @var CartItemsService */ + private $cartItemsService; + + public function __construct(CartItemsService $cartItemsService) + { + $this->cartItemsService = $cartItemsService; + } + /** * @param float $amount * @param int $precision @@ -56,7 +64,7 @@ public function compositeRoundingInaccuracies($remaining, $orderLines): array // Otherwise spread the cart line again with the updated total //TODO: check why remaining comes -100 when testing and new total becomes different - $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); + $orderLines[$hash] = $this->cartItemsService->spreadCartLineGroup($items, $totalAmount + $remaining); break; } } elseif ($remaining > 0) { @@ -64,7 +72,7 @@ public function compositeRoundingInaccuracies($remaining, $orderLines): array // Grab the line group's total amount $totalAmount = array_sum(array_column($items, 'totalAmount')); // Otherwise spread the cart line again with the updated total - $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); + $orderLines[$hash] = $this->cartItemsService->spreadCartLineGroup($items, $totalAmount + $remaining); break; } } From 19865dc397f51494974a32d9da2d9dcd61220906 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:27:14 +0200 Subject: [PATCH 18/28] Revert "[PIPRES-460] added RoundingUtility widely" This reverts commit 43f63ba05f89020d5f17a68df714739564e06a69. --- .../CartLine/CartItemDiscountService.php | 13 ++---------- .../CartLine/CartItemPaymentFeeService.php | 12 +++++------ .../CartLine/CartItemProductLinesService.php | 19 +++++++---------- .../CartLine/CartItemShippingLineService.php | 17 ++++++--------- .../CartLine/CartItemWrappingService.php | 21 ++++++++----------- src/Service/CartLine/CartItemsService.php | 15 +++++-------- src/Utility/RoundingUtility.php | 12 ++--------- 7 files changed, 37 insertions(+), 72 deletions(-) diff --git a/src/Service/CartLine/CartItemDiscountService.php b/src/Service/CartLine/CartItemDiscountService.php index 90c2dcd5c..6df8b31bf 100644 --- a/src/Service/CartLine/CartItemDiscountService.php +++ b/src/Service/CartLine/CartItemDiscountService.php @@ -13,7 +13,6 @@ namespace Mollie\Service\CartLine; use Mollie\Config\Config; -use mollie\src\Utility\RoundingUtility; use Mollie\Utility\NumberUtility; if (!defined('_PS_VERSION_')) { @@ -22,14 +21,6 @@ class CartItemDiscountService { - /* @var RoundingUtility */ - private $roundingUtility; - - public function __construct(RoundingUtility $roundingUtility) - { - $this->roundingUtility = $roundingUtility; - } - /** * @param float $totalDiscounts * @param array $orderLines @@ -45,8 +36,8 @@ public function addDiscountsToProductLines(float $totalDiscounts, array $orderLi 'name' => 'Discount', 'type' => 'discount', 'quantity' => 1, - 'unitPrice' => -$this->roundingUtility->round($totalDiscounts, Config::API_ROUNDING_PRECISION), - 'totalAmount' => -$this->roundingUtility->round($totalDiscounts, Config::API_ROUNDING_PRECISION), + 'unitPrice' => -round($totalDiscounts, Config::API_ROUNDING_PRECISION), + 'totalAmount' => -round($totalDiscounts, Config::API_ROUNDING_PRECISION), 'targetVat' => 0, 'category' => '', ], diff --git a/src/Service/CartLine/CartItemPaymentFeeService.php b/src/Service/CartLine/CartItemPaymentFeeService.php index c3a6e1fd0..cbb654213 100644 --- a/src/Service/CartLine/CartItemPaymentFeeService.php +++ b/src/Service/CartLine/CartItemPaymentFeeService.php @@ -15,7 +15,6 @@ use Mollie\Config\Config; use Mollie\DTO\PaymentFeeData; use Mollie\Service\LanguageService; -use mollie\src\Utility\RoundingUtility; use Mollie\Utility\NumberUtility; if (!defined('_PS_VERSION_')) { @@ -24,13 +23,14 @@ class CartItemPaymentFeeService { - /* @var LanguageService */ + /** + * @var LanguageService + */ private $languageService; - public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) + public function __construct(LanguageService $languageService) { $this->languageService = $languageService; - $this->roundingUtility = $roundingUtility; } /** @@ -50,8 +50,8 @@ public function addPaymentFeeLine($paymentFeeData, array $orderLines): array 'name' => $this->languageService->lang('Payment fee'), 'sku' => Config::PAYMENT_FEE_SKU, 'quantity' => 1, - 'unitPrice' => $this->roundingUtility->round($paymentFeeData->getPaymentFeeTaxIncl(), CONFIG::API_ROUNDING_PRECISION), - 'totalAmount' => $this->roundingUtility->round($paymentFeeData->getPaymentFeeTaxIncl(), CONFIG::API_ROUNDING_PRECISION), + '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(), ], diff --git a/src/Service/CartLine/CartItemProductLinesService.php b/src/Service/CartLine/CartItemProductLinesService.php index 851b51f77..ef85e9330 100644 --- a/src/Service/CartLine/CartItemProductLinesService.php +++ b/src/Service/CartLine/CartItemProductLinesService.php @@ -13,7 +13,6 @@ namespace Mollie\Service\CartLine; use Mollie\Config\Config; -use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CalculationUtility; use Mollie\Utility\NumberUtility; @@ -23,12 +22,8 @@ class CartItemProductLinesService { - /* @var RoundingUtility */ - private $roundingUtility; - - public function __construct(RoundingUtility $roundingUtility) + public function __construct() { - $this->roundingUtility = $roundingUtility; } /** @@ -47,7 +42,7 @@ public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePre $quantity = (int) $line['quantity']; $targetVat = $line['targetVat']; $unitPrice = $line['unitPrice']; - $unitPriceNoTax = $this->roundingUtility->round(CalculationUtility::getUnitPriceNoTax( + $unitPriceNoTax = round(CalculationUtility::getUnitPriceNoTax( $line['unitPrice'], $targetVat ), @@ -58,7 +53,7 @@ public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePre $totalAmount = $line['totalAmount']; $actualVatRate = 0; if ($unitPriceNoTax > 0) { - $actualVatRate = $this->roundingUtility->round( + $actualVatRate = round( $vatAmount = CalculationUtility::getActualVatRate($unitPrice, $unitPriceNoTax, $quantity), $vatRatePrecision ); @@ -73,10 +68,10 @@ public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePre 'name' => $line['name'], 'category' => $line['category'], 'quantity' => (int) $quantity, - 'unitPrice' => $this->roundingUtility->round($unitPrice, $roundingPrecision), - 'totalAmount' => $this->roundingUtility->round($totalAmount, $roundingPrecision), - 'vatRate' => $this->roundingUtility->round($actualVatRate, $roundingPrecision), - 'vatAmount' => $this->roundingUtility->round($vatAmount, $roundingPrecision), + '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, ]; diff --git a/src/Service/CartLine/CartItemShippingLineService.php b/src/Service/CartLine/CartItemShippingLineService.php index 4585235ea..b50efeb62 100644 --- a/src/Service/CartLine/CartItemShippingLineService.php +++ b/src/Service/CartLine/CartItemShippingLineService.php @@ -14,7 +14,6 @@ use Mollie\Config\Config; use Mollie\Service\LanguageService; -use mollie\src\Utility\RoundingUtility; if (!defined('_PS_VERSION_')) { exit; @@ -25,13 +24,9 @@ class CartItemShippingLineService /* @var LanguageService */ private $languageService; - /* @var RoundingUtility */ - private $roundingUtility; - - public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) + public function __construct(LanguageService $languageService) { $this->languageService = $languageService; - $this->roundingUtility = $roundingUtility; } /** @@ -42,16 +37,16 @@ public function __construct(LanguageService $languageService, RoundingUtility $r */ public function addShippingLine($roundedShippingCost, $cartSummary, array $orderLines): array { - if ($this->roundingUtility->round($roundedShippingCost, 2) > 0) { - $shippingVatRate = $this->roundingUtility->round(($cartSummary['total_shipping'] - $cartSummary['total_shipping_tax_exc']) / $cartSummary['total_shipping_tax_exc'] * 100, Config::API_ROUNDING_PRECISION); + if (round($roundedShippingCost, 2) > 0) { + $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' => $this->roundingUtility->round($roundedShippingCost, Config::API_ROUNDING_PRECISION), - 'totalAmount' => $this->roundingUtility->round($roundedShippingCost, Config::API_ROUNDING_PRECISION), - 'vatAmount' => $this->roundingUtility->round($roundedShippingCost * $shippingVatRate / ($shippingVatRate + 100), Config::API_ROUNDING_PRECISION), + '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, ], ]; diff --git a/src/Service/CartLine/CartItemWrappingService.php b/src/Service/CartLine/CartItemWrappingService.php index e1d51ba1e..b20fa4265 100644 --- a/src/Service/CartLine/CartItemWrappingService.php +++ b/src/Service/CartLine/CartItemWrappingService.php @@ -14,7 +14,6 @@ use Mollie\Config\Config; use Mollie\Service\LanguageService; -use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CalculationUtility; if (!defined('_PS_VERSION_')) { @@ -23,16 +22,14 @@ class CartItemWrappingService { - /* @var LanguageService */ + /** + * @var LanguageService + */ private $languageService; - /* @var RoundingUtility */ - private $roundingUtility; - - public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) + public function __construct(LanguageService $languageService) { $this->languageService = $languageService; - $this->roundingUtility = $roundingUtility; } /** @@ -45,8 +42,8 @@ public function __construct(LanguageService $languageService, RoundingUtility $r */ public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $vatRatePrecision, array $orderLines): array { - if ($this->roundingUtility->round($wrappingPrice, 2) > 0) { - $wrappingVatRate = $this->roundingUtility->round( + if (round($wrappingPrice, 2) > 0) { + $wrappingVatRate = round( CalculationUtility::getActualVatRate( $cartSummary['total_wrapping'], $cartSummary['total_wrapping_tax_exc'] @@ -58,9 +55,9 @@ public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $v [ 'name' => $this->languageService->lang('Gift wrapping'), 'quantity' => 1, - 'unitPrice' => $this->roundingUtility->round($wrappingPrice, Config::API_ROUNDING_PRECISION), - 'totalAmount' => $this->roundingUtility->round($wrappingPrice, Config::API_ROUNDING_PRECISION), - 'vatAmount' => $this->roundingUtility->round($wrappingPrice * $wrappingVatRate / ($wrappingVatRate + 100), Config::API_ROUNDING_PRECISION), + 'unitPrice' => round($wrappingPrice, Config::API_ROUNDING_PRECISION), + 'totalAmount' => round($wrappingPrice, Config::API_ROUNDING_PRECISION), + 'vatAmount' => round($wrappingPrice * $wrappingVatRate / ($wrappingVatRate + 100), Config::API_ROUNDING_PRECISION), 'vatRate' => $wrappingVatRate, ], ]; diff --git a/src/Service/CartLine/CartItemsService.php b/src/Service/CartLine/CartItemsService.php index 988308dee..d4e82a49d 100644 --- a/src/Service/CartLine/CartItemsService.php +++ b/src/Service/CartLine/CartItemsService.php @@ -15,7 +15,6 @@ use Mollie\Adapter\Context; use Mollie\Config\Config; use Mollie\Service\VoucherService; -use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; use Mollie\Utility\TextFormatUtility; @@ -35,14 +34,10 @@ class CartItemsService */ private $voucherService; - /* @var RoundingUtility */ - private $roundingUtility; - - public function __construct(Context $context, VoucherService $voucherService, RoundingUtility $roundingUtility) + public function __construct(Context $context, VoucherService $voucherService) { $this->context = $context; $this->voucherService = $voucherService; - $this->roundingUtility = $roundingUtility; } /** @@ -56,7 +51,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array { foreach ($cartItems as $cartItem) { // Get the rounded total w/ tax - $roundedTotalWithTax = $this->roundingUtility->round($cartItem['total_wt'], Config::API_ROUNDING_PRECISION); + $roundedTotalWithTax = round($cartItem['total_wt'], Config::API_ROUNDING_PRECISION); // Skip if no qty $quantity = (int) $cartItem['cart_quantity']; @@ -101,7 +96,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array 'sku' => $productHash, 'targetVat' => (float) $cartItem['rate'], 'quantity' => $quantity, - 'unitPrice' => $this->roundingUtility->round($cartItem['price_wt'], Config::API_ROUNDING_PRECISION), + 'unitPrice' => round($cartItem['price_wt'], Config::API_ROUNDING_PRECISION), 'totalAmount' => (float) $roundedTotalWithTax, 'category' => $this->voucherService->getVoucherCategory($cartItem, $selectedVoucherCategory), 'product_url' => $this->context->getProductLink($cartItem['id_product']), @@ -126,9 +121,9 @@ public function createProductLines(array $cartItems, array $giftProducts, array * @since 3.2.2 * @since 3.3.3 Omits VAT details */ - public function spreadCartLineGroup($cartLineGroup, $newTotal) + public static function spreadCartLineGroup($cartLineGroup, $newTotal) { - $newTotal = $this->roundingUtility->round($newTotal, Config::API_ROUNDING_PRECISION); + $newTotal = round($newTotal, Config::API_ROUNDING_PRECISION); $quantity = array_sum(array_column($cartLineGroup, 'quantity')); $newCartLineGroup = []; $spread = CartPriceUtility::spreadAmountEvenly($newTotal, $quantity); diff --git a/src/Utility/RoundingUtility.php b/src/Utility/RoundingUtility.php index 3c49a17bc..718eeb18e 100644 --- a/src/Utility/RoundingUtility.php +++ b/src/Utility/RoundingUtility.php @@ -21,14 +21,6 @@ class RoundingUtility { - /* @var CartItemsService */ - private $cartItemsService; - - public function __construct(CartItemsService $cartItemsService) - { - $this->cartItemsService = $cartItemsService; - } - /** * @param float $amount * @param int $precision @@ -64,7 +56,7 @@ public function compositeRoundingInaccuracies($remaining, $orderLines): array // Otherwise spread the cart line again with the updated total //TODO: check why remaining comes -100 when testing and new total becomes different - $orderLines[$hash] = $this->cartItemsService->spreadCartLineGroup($items, $totalAmount + $remaining); + $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); break; } } elseif ($remaining > 0) { @@ -72,7 +64,7 @@ public function compositeRoundingInaccuracies($remaining, $orderLines): array // Grab the line group's total amount $totalAmount = array_sum(array_column($items, 'totalAmount')); // Otherwise spread the cart line again with the updated total - $orderLines[$hash] = $this->cartItemsService->spreadCartLineGroup($items, $totalAmount + $remaining); + $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); break; } } From 62c57a7e4851b09c1a1b5adb059728178a10d1fb Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:29:59 +0200 Subject: [PATCH 19/28] [PIPRES-460] added RoundingUtility 1 --- src/Service/CartLine/CartItemDiscountService.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Service/CartLine/CartItemDiscountService.php b/src/Service/CartLine/CartItemDiscountService.php index 6df8b31bf..fd77d8b62 100644 --- a/src/Service/CartLine/CartItemDiscountService.php +++ b/src/Service/CartLine/CartItemDiscountService.php @@ -13,6 +13,7 @@ namespace Mollie\Service\CartLine; use Mollie\Config\Config; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\NumberUtility; if (!defined('_PS_VERSION_')) { @@ -21,6 +22,14 @@ class CartItemDiscountService { + /* @var RoundingUtility $roundingUtility */ + private $roundingUtility; + + public function __construct(RoundingUtility $roundingUtility) + { + $this->roundingUtility = $roundingUtility; + } + /** * @param float $totalDiscounts * @param array $orderLines @@ -36,8 +45,8 @@ public function addDiscountsToProductLines(float $totalDiscounts, array $orderLi 'name' => 'Discount', 'type' => 'discount', 'quantity' => 1, - 'unitPrice' => -round($totalDiscounts, Config::API_ROUNDING_PRECISION), - 'totalAmount' => -round($totalDiscounts, Config::API_ROUNDING_PRECISION), + 'unitPrice' => -$this->roundingUtility->round($totalDiscounts, Config::API_ROUNDING_PRECISION), + 'totalAmount' => -$this->roundingUtility->round($totalDiscounts, Config::API_ROUNDING_PRECISION), 'targetVat' => 0, 'category' => '', ], From 6ceba0fbd65bc06e5b410422231faf091a9ab0a7 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:38:07 +0200 Subject: [PATCH 20/28] [PIPRES-460] added RoundingUtility 2 --- .../CartLine/CartItemPaymentFeeService.php | 12 +++++++++--- .../CartLine/CartItemProductLinesService.php | 19 ++++++++++++------- .../CartLine/CartItemWrappingService.php | 15 ++++++++++----- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/Service/CartLine/CartItemPaymentFeeService.php b/src/Service/CartLine/CartItemPaymentFeeService.php index cbb654213..0bbe58030 100644 --- a/src/Service/CartLine/CartItemPaymentFeeService.php +++ b/src/Service/CartLine/CartItemPaymentFeeService.php @@ -15,6 +15,7 @@ use Mollie\Config\Config; use Mollie\DTO\PaymentFeeData; use Mollie\Service\LanguageService; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\NumberUtility; if (!defined('_PS_VERSION_')) { @@ -28,9 +29,14 @@ class CartItemPaymentFeeService */ private $languageService; - public function __construct(LanguageService $languageService) + /* @var RoundingUtility */ + private $roundingUtility; + + + public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) { $this->languageService = $languageService; + $this->roundingUtility = $roundingUtility; } /** @@ -50,8 +56,8 @@ public function addPaymentFeeLine($paymentFeeData, array $orderLines): array '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), + 'unitPrice' => $this->roundingUtility->round($paymentFeeData->getPaymentFeeTaxIncl(), CONFIG::API_ROUNDING_PRECISION), + 'totalAmount' => $this->roundingUtility->round($paymentFeeData->getPaymentFeeTaxIncl(), CONFIG::API_ROUNDING_PRECISION), 'vatAmount' => NumberUtility::minus($paymentFeeData->getPaymentFeeTaxIncl(), $paymentFeeData->getPaymentFeeTaxExcl()), 'vatRate' => $paymentFeeData->getTaxRate(), ], diff --git a/src/Service/CartLine/CartItemProductLinesService.php b/src/Service/CartLine/CartItemProductLinesService.php index ef85e9330..f0bdd6416 100644 --- a/src/Service/CartLine/CartItemProductLinesService.php +++ b/src/Service/CartLine/CartItemProductLinesService.php @@ -13,6 +13,7 @@ namespace Mollie\Service\CartLine; use Mollie\Config\Config; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CalculationUtility; use Mollie\Utility\NumberUtility; @@ -22,8 +23,12 @@ class CartItemProductLinesService { - public function __construct() + /* @var RoundingUtility $roundingUtility */ + private $roundingUtility; + + public function __construct(RoundingUtility $roundingUtility) { + $this->roundingUtility = $roundingUtility; } /** @@ -42,7 +47,7 @@ public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePre $quantity = (int) $line['quantity']; $targetVat = $line['targetVat']; $unitPrice = $line['unitPrice']; - $unitPriceNoTax = round(CalculationUtility::getUnitPriceNoTax( + $unitPriceNoTax = $this->roundingUtility->round(CalculationUtility::getUnitPriceNoTax( $line['unitPrice'], $targetVat ), @@ -53,7 +58,7 @@ public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePre $totalAmount = $line['totalAmount']; $actualVatRate = 0; if ($unitPriceNoTax > 0) { - $actualVatRate = round( + $actualVatRate = $this->roundingUtility->round( $vatAmount = CalculationUtility::getActualVatRate($unitPrice, $unitPriceNoTax, $quantity), $vatRatePrecision ); @@ -68,10 +73,10 @@ public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePre '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), + 'unitPrice' => $this->roundingUtility->round($unitPrice, $roundingPrecision), + 'totalAmount' => $this->roundingUtility->round($totalAmount, $roundingPrecision), + 'vatRate' => $this->roundingUtility->round($actualVatRate, $roundingPrecision), + 'vatAmount' => $this->roundingUtility->round($vatAmount, $roundingPrecision), 'product_url' => $line['product_url'] ?? null, 'image_url' => $line['image_url'] ?? null, ]; diff --git a/src/Service/CartLine/CartItemWrappingService.php b/src/Service/CartLine/CartItemWrappingService.php index b20fa4265..604157001 100644 --- a/src/Service/CartLine/CartItemWrappingService.php +++ b/src/Service/CartLine/CartItemWrappingService.php @@ -14,6 +14,7 @@ use Mollie\Config\Config; use Mollie\Service\LanguageService; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CalculationUtility; if (!defined('_PS_VERSION_')) { @@ -27,9 +28,13 @@ class CartItemWrappingService */ private $languageService; - public function __construct(LanguageService $languageService) + /* @var RoundingUtility */ + private $roundingUtility; + + public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) { $this->languageService = $languageService; + $this->roundingUtility = $roundingUtility; } /** @@ -43,7 +48,7 @@ public function __construct(LanguageService $languageService) public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $vatRatePrecision, array $orderLines): array { if (round($wrappingPrice, 2) > 0) { - $wrappingVatRate = round( + $wrappingVatRate = $this->roundingUtility->round( CalculationUtility::getActualVatRate( $cartSummary['total_wrapping'], $cartSummary['total_wrapping_tax_exc'] @@ -55,9 +60,9 @@ public function addWrappingLine(float $wrappingPrice, array $cartSummary, int $v [ 'name' => $this->languageService->lang('Gift wrapping'), 'quantity' => 1, - 'unitPrice' => round($wrappingPrice, Config::API_ROUNDING_PRECISION), - 'totalAmount' => round($wrappingPrice, Config::API_ROUNDING_PRECISION), - 'vatAmount' => round($wrappingPrice * $wrappingVatRate / ($wrappingVatRate + 100), Config::API_ROUNDING_PRECISION), + 'unitPrice' => $this->roundingUtility->round($wrappingPrice, Config::API_ROUNDING_PRECISION), + 'totalAmount' => $this->roundingUtility->round($wrappingPrice, Config::API_ROUNDING_PRECISION), + 'vatAmount' => $this->roundingUtility->round($wrappingPrice * $wrappingVatRate / ($wrappingVatRate + 100), Config::API_ROUNDING_PRECISION), 'vatRate' => $wrappingVatRate, ], ]; From 7a738c405ef2427de44a4fee5ea8274e756fe04f Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:39:31 +0200 Subject: [PATCH 21/28] [PIPRES-460] added RoundingUtility 3 --- .../CartLine/CartItemShippingLineService.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Service/CartLine/CartItemShippingLineService.php b/src/Service/CartLine/CartItemShippingLineService.php index b50efeb62..7301990c6 100644 --- a/src/Service/CartLine/CartItemShippingLineService.php +++ b/src/Service/CartLine/CartItemShippingLineService.php @@ -14,6 +14,7 @@ use Mollie\Config\Config; use Mollie\Service\LanguageService; +use mollie\src\Utility\RoundingUtility; if (!defined('_PS_VERSION_')) { exit; @@ -24,9 +25,13 @@ class CartItemShippingLineService /* @var LanguageService */ private $languageService; - public function __construct(LanguageService $languageService) + /* @var RoundingUtility */ + private $roundingUtility; + + public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) { $this->languageService = $languageService; + $this->roundingUtility = $roundingUtility; } /** @@ -37,16 +42,16 @@ public function __construct(LanguageService $languageService) */ public function addShippingLine($roundedShippingCost, $cartSummary, array $orderLines): array { - if (round($roundedShippingCost, 2) > 0) { + if ($this->roundingUtility->round($roundedShippingCost, 2) > 0) { $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), + 'unitPrice' => $this->roundingUtility->round($roundedShippingCost, Config::API_ROUNDING_PRECISION), + 'totalAmount' => $this->roundingUtility->round($roundedShippingCost, Config::API_ROUNDING_PRECISION), + 'vatAmount' => $this->roundingUtility->round($roundedShippingCost * $shippingVatRate / ($shippingVatRate + 100), Config::API_ROUNDING_PRECISION), 'vatRate' => $shippingVatRate, ], ]; From 91be923111b063a852d0f092acbd193cb2df23d7 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:44:47 +0200 Subject: [PATCH 22/28] [PIPRES-460] workaround and added RoundingUtility --- src/Service/CartLine/CartItemsService.php | 17 +++++++++++++---- src/Utility/RoundingUtility.php | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Service/CartLine/CartItemsService.php b/src/Service/CartLine/CartItemsService.php index d4e82a49d..07962d9e6 100644 --- a/src/Service/CartLine/CartItemsService.php +++ b/src/Service/CartLine/CartItemsService.php @@ -15,6 +15,7 @@ use Mollie\Adapter\Context; use Mollie\Config\Config; use Mollie\Service\VoucherService; +use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; use Mollie\Utility\TextFormatUtility; @@ -25,6 +26,10 @@ class CartItemsService { + /** + * @var RoundingUtility + */ + private static $roundingUtility; /** * @var Context */ @@ -34,10 +39,14 @@ class CartItemsService */ private $voucherService; - public function __construct(Context $context, VoucherService $voucherService) + /* @var RoundingUtility */ + private $roundingUtility; + + public function __construct(Context $context, VoucherService $voucherService, RoundingUtility $roundingUtility) { $this->context = $context; $this->voucherService = $voucherService; + $this->roundingUtility = $roundingUtility; } /** @@ -51,7 +60,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array { foreach ($cartItems as $cartItem) { // Get the rounded total w/ tax - $roundedTotalWithTax = round($cartItem['total_wt'], Config::API_ROUNDING_PRECISION); + $roundedTotalWithTax = $this->roundingUtility->round($cartItem['total_wt'], Config::API_ROUNDING_PRECISION); // Skip if no qty $quantity = (int) $cartItem['cart_quantity']; @@ -96,7 +105,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array 'sku' => $productHash, 'targetVat' => (float) $cartItem['rate'], 'quantity' => $quantity, - 'unitPrice' => round($cartItem['price_wt'], Config::API_ROUNDING_PRECISION), + 'unitPrice' => $this->roundingUtility->round($cartItem['price_wt'], Config::API_ROUNDING_PRECISION), 'totalAmount' => (float) $roundedTotalWithTax, 'category' => $this->voucherService->getVoucherCategory($cartItem, $selectedVoucherCategory), 'product_url' => $this->context->getProductLink($cartItem['id_product']), @@ -123,7 +132,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array */ public static function spreadCartLineGroup($cartLineGroup, $newTotal) { - $newTotal = round($newTotal, Config::API_ROUNDING_PRECISION); + $newTotal = self::$roundingUtility->round($newTotal, Config::API_ROUNDING_PRECISION); $quantity = array_sum(array_column($cartLineGroup, 'quantity')); $newCartLineGroup = []; $spread = CartPriceUtility::spreadAmountEvenly($newTotal, $quantity); diff --git a/src/Utility/RoundingUtility.php b/src/Utility/RoundingUtility.php index 718eeb18e..14f187717 100644 --- a/src/Utility/RoundingUtility.php +++ b/src/Utility/RoundingUtility.php @@ -21,6 +21,16 @@ class RoundingUtility { + /** + * @var CartItemsService + */ + private $cartItemsService; + + public function __construct(CartItemsService $cartItemsService) + { + $this->cartItemsService = $cartItemsService; + } + /** * @param float $amount * @param int $precision @@ -56,7 +66,7 @@ public function compositeRoundingInaccuracies($remaining, $orderLines): array // Otherwise spread the cart line again with the updated total //TODO: check why remaining comes -100 when testing and new total becomes different - $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); + $orderLines[$hash] = $this->cartItemsService->spreadCartLineGroup($items, $totalAmount + $remaining); break; } } elseif ($remaining > 0) { @@ -64,7 +74,7 @@ public function compositeRoundingInaccuracies($remaining, $orderLines): array // Grab the line group's total amount $totalAmount = array_sum(array_column($items, 'totalAmount')); // Otherwise spread the cart line again with the updated total - $orderLines[$hash] = CartItemsService::spreadCartLineGroup($items, $totalAmount + $remaining); + $orderLines[$hash] = $this->cartItemsService->spreadCartLineGroup($items, $totalAmount + $remaining); break; } } From 2588879388748dd45b13ab1328546405c0900f90 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:48:06 +0200 Subject: [PATCH 23/28] [PIPRES-460] fix phpstan --- src/Service/CartLine/CartItemPaymentFeeService.php | 5 +---- src/Service/CartLinesService.php | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Service/CartLine/CartItemPaymentFeeService.php b/src/Service/CartLine/CartItemPaymentFeeService.php index 0bbe58030..b55a59ad8 100644 --- a/src/Service/CartLine/CartItemPaymentFeeService.php +++ b/src/Service/CartLine/CartItemPaymentFeeService.php @@ -24,15 +24,12 @@ class CartItemPaymentFeeService { - /** - * @var LanguageService - */ + /* @var LanguageService */ private $languageService; /* @var RoundingUtility */ private $roundingUtility; - public function __construct(LanguageService $languageService, RoundingUtility $roundingUtility) { $this->languageService = $languageService; diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index c9b35eedc..11868af28 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -24,7 +24,6 @@ use mollie\src\Utility\RoundingUtility; use Mollie\Utility\ArrayUtility; use Mollie\Utility\CalculationUtility; -use Mollie\Utility\NumberUtility; if (!defined('_PS_VERSION_')) { exit; @@ -104,8 +103,7 @@ public function getCartLines( $cartItems, $psGiftWrapping, $selectedVoucherCategory - ): array - { + ): array { $totalPrice = $this->roundingUtility->round($amount, Config::API_ROUNDING_PRECISION); $roundedShippingCost = $this->roundingUtility->round($shippingCost, Config::API_ROUNDING_PRECISION); From a19c2bc5780a2487ec46a00ca7ac047e00e10019 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:57:28 +0200 Subject: [PATCH 24/28] [PIPRES-460] fixed infinite loop --- src/Service/CartLine/CartItemsService.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/Service/CartLine/CartItemsService.php b/src/Service/CartLine/CartItemsService.php index 07962d9e6..de61106ee 100644 --- a/src/Service/CartLine/CartItemsService.php +++ b/src/Service/CartLine/CartItemsService.php @@ -15,7 +15,6 @@ use Mollie\Adapter\Context; use Mollie\Config\Config; use Mollie\Service\VoucherService; -use mollie\src\Utility\RoundingUtility; use Mollie\Utility\CartPriceUtility; use Mollie\Utility\NumberUtility; use Mollie\Utility\TextFormatUtility; @@ -26,10 +25,6 @@ class CartItemsService { - /** - * @var RoundingUtility - */ - private static $roundingUtility; /** * @var Context */ @@ -39,14 +34,10 @@ class CartItemsService */ private $voucherService; - /* @var RoundingUtility */ - private $roundingUtility; - - public function __construct(Context $context, VoucherService $voucherService, RoundingUtility $roundingUtility) + public function __construct(Context $context, VoucherService $voucherService) { $this->context = $context; $this->voucherService = $voucherService; - $this->roundingUtility = $roundingUtility; } /** @@ -132,7 +123,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array */ public static function spreadCartLineGroup($cartLineGroup, $newTotal) { - $newTotal = self::$roundingUtility->round($newTotal, Config::API_ROUNDING_PRECISION); + $newTotal = round($newTotal, Config::API_ROUNDING_PRECISION); $quantity = array_sum(array_column($cartLineGroup, 'quantity')); $newCartLineGroup = []; $spread = CartPriceUtility::spreadAmountEvenly($newTotal, $quantity); From 7746ccecbd931ef12e6049ee0e559f55247d1bbe Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:58:31 +0200 Subject: [PATCH 25/28] [PIPRES-460] removed round method from CartItemsService because of infinite loop --- src/Service/CartLine/CartItemsService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Service/CartLine/CartItemsService.php b/src/Service/CartLine/CartItemsService.php index de61106ee..d4e82a49d 100644 --- a/src/Service/CartLine/CartItemsService.php +++ b/src/Service/CartLine/CartItemsService.php @@ -51,7 +51,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array { foreach ($cartItems as $cartItem) { // Get the rounded total w/ tax - $roundedTotalWithTax = $this->roundingUtility->round($cartItem['total_wt'], Config::API_ROUNDING_PRECISION); + $roundedTotalWithTax = round($cartItem['total_wt'], Config::API_ROUNDING_PRECISION); // Skip if no qty $quantity = (int) $cartItem['cart_quantity']; @@ -96,7 +96,7 @@ public function createProductLines(array $cartItems, array $giftProducts, array 'sku' => $productHash, 'targetVat' => (float) $cartItem['rate'], 'quantity' => $quantity, - 'unitPrice' => $this->roundingUtility->round($cartItem['price_wt'], Config::API_ROUNDING_PRECISION), + 'unitPrice' => round($cartItem['price_wt'], Config::API_ROUNDING_PRECISION), 'totalAmount' => (float) $roundedTotalWithTax, 'category' => $this->voucherService->getVoucherCategory($cartItem, $selectedVoucherCategory), 'product_url' => $this->context->getProductLink($cartItem['id_product']), From 6438eb18f11035115266016baf36a5dfccc0fe63 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 16:59:41 +0200 Subject: [PATCH 26/28] [PIPRES-460] optimized ternary if --- src/Service/CartLine/CartItemsService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/CartLine/CartItemsService.php b/src/Service/CartLine/CartItemsService.php index d4e82a49d..f65a623f7 100644 --- a/src/Service/CartLine/CartItemsService.php +++ b/src/Service/CartLine/CartItemsService.php @@ -134,7 +134,7 @@ public static function spreadCartLineGroup($cartLineGroup, $newTotal) 'quantity' => $qty, 'unitPrice' => (float) $unitPrice, 'totalAmount' => (float) $unitPrice * $qty, - 'sku' => isset($cartLineGroup[0]['sku']) ? $cartLineGroup[0]['sku'] : '', + 'sku' => $cartLineGroup[0]['sku'] ?? '', 'targetVat' => $cartLineGroup[0]['targetVat'], 'category' => $cartLineGroup[0]['category'], ]; From c91af88f7e8711ee40e1ab438fb9727c1752e64b Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 17:03:29 +0200 Subject: [PATCH 27/28] [PIPRES-460] added types arguments --- .../CartLine/CartItemPaymentFeeService.php | 2 +- .../CartLine/CartItemProductLinesService.php | 2 +- .../CartLine/CartItemShippingLineService.php | 5 +++-- src/Service/CartLinesService.php | 16 ++++++++-------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Service/CartLine/CartItemPaymentFeeService.php b/src/Service/CartLine/CartItemPaymentFeeService.php index b55a59ad8..3b34216e2 100644 --- a/src/Service/CartLine/CartItemPaymentFeeService.php +++ b/src/Service/CartLine/CartItemPaymentFeeService.php @@ -42,7 +42,7 @@ public function __construct(LanguageService $languageService, RoundingUtility $r * * @return array */ - public function addPaymentFeeLine($paymentFeeData, array $orderLines): array + public function addPaymentFeeLine(PaymentFeeData $paymentFeeData, array $orderLines): array { if (!$paymentFeeData->isActive()) { return $orderLines; diff --git a/src/Service/CartLine/CartItemProductLinesService.php b/src/Service/CartLine/CartItemProductLinesService.php index f0bdd6416..f52e311ba 100644 --- a/src/Service/CartLine/CartItemProductLinesService.php +++ b/src/Service/CartLine/CartItemProductLinesService.php @@ -38,7 +38,7 @@ public function __construct(RoundingUtility $roundingUtility) * * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException */ - public function fillProductLinesWithRemainingData(array $orderLines, $vatRatePrecision): array + public function fillProductLinesWithRemainingData(array $orderLines, int $vatRatePrecision): array { $roundingPrecision = CONFIG::API_ROUNDING_PRECISION; diff --git a/src/Service/CartLine/CartItemShippingLineService.php b/src/Service/CartLine/CartItemShippingLineService.php index 7301990c6..ed92d78ac 100644 --- a/src/Service/CartLine/CartItemShippingLineService.php +++ b/src/Service/CartLine/CartItemShippingLineService.php @@ -37,13 +37,14 @@ public function __construct(LanguageService $languageService, RoundingUtility $r /** * @param float $roundedShippingCost * @param array $cartSummary + * @param array $orderLines * * @return array */ - public function addShippingLine($roundedShippingCost, $cartSummary, array $orderLines): array + public function addShippingLine(float $roundedShippingCost, array $cartSummary, array $orderLines): array { if ($this->roundingUtility->round($roundedShippingCost, 2) > 0) { - $shippingVatRate = round(($cartSummary['total_shipping'] - $cartSummary['total_shipping_tax_exc']) / $cartSummary['total_shipping_tax_exc'] * 100, Config::API_ROUNDING_PRECISION); + $shippingVatRate = $this->roundingUtility->round(($cartSummary['total_shipping'] - $cartSummary['total_shipping_tax_exc']) / $cartSummary['total_shipping_tax_exc'] * 100, Config::API_ROUNDING_PRECISION); $orderLines['shipping'] = [ [ diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 11868af28..8345ea46c 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -95,14 +95,14 @@ public function __construct( * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException */ public function getCartLines( - $amount, - $paymentFeeData, - $currencyIsoCode, - $cartSummary, - $shippingCost, - $cartItems, - $psGiftWrapping, - $selectedVoucherCategory + float $amount, + PaymentFeeData $paymentFeeData, + string $currencyIsoCode, + array $cartSummary, + float $shippingCost, + array $cartItems, + bool $psGiftWrapping, + string $selectedVoucherCategory ): array { $totalPrice = $this->roundingUtility->round($amount, Config::API_ROUNDING_PRECISION); $roundedShippingCost = $this->roundingUtility->round($shippingCost, Config::API_ROUNDING_PRECISION); From ae4dec71b056309d4f22cbda4ca1a6e0d9755bb9 Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Fri, 20 Dec 2024 17:08:59 +0200 Subject: [PATCH 28/28] [PIPRES-460] cslint --- src/Service/CartLinesService.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Service/CartLinesService.php b/src/Service/CartLinesService.php index 8345ea46c..7054fc578 100644 --- a/src/Service/CartLinesService.php +++ b/src/Service/CartLinesService.php @@ -95,13 +95,13 @@ public function __construct( * @throws \PrestaShop\Decimal\Exception\DivisionByZeroException */ public function getCartLines( - float $amount, + float $amount, PaymentFeeData $paymentFeeData, - string $currencyIsoCode, - array $cartSummary, - float $shippingCost, - array $cartItems, - bool $psGiftWrapping, + string $currencyIsoCode, + array $cartSummary, + float $shippingCost, + array $cartItems, + bool $psGiftWrapping, string $selectedVoucherCategory ): array { $totalPrice = $this->roundingUtility->round($amount, Config::API_ROUNDING_PRECISION);