diff --git a/CHANGELOG.md b/CHANGELOG.md index 9700573..345a8cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.1.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [3.2.6](https://github.com/unzerdev/magento2/compare/3.2.5..3.2.6) +### Fixed +* Discount amount when taxes are applied + ## [3.2.5](https://github.com/unzerdev/magento2/compare/3.2.4..3.2.5) ### Added * Direct Bank Transfer diff --git a/Helper/Order.php b/Helper/Order.php index 02493f8..955c69b 100644 --- a/Helper/Order.php +++ b/Helper/Order.php @@ -3,6 +3,7 @@ namespace Unzer\PAPI\Helper; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\ProductMetadataInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Module\ModuleListInterface; @@ -12,6 +13,8 @@ use Magento\Sales\Api\Data\OrderAddressInterface; use Magento\Sales\Model\Order as OrderModel; use Magento\Sales\Model\Order\Item; +use Magento\Store\Model\ScopeInterface; +use Magento\Tax\Model\Config as MagentoTaxConfig; use Unzer\PAPI\Block\System\Config\Form\Field\BirthDateFactory; use Unzer\PAPI\Model\Config; use Unzer\PAPI\Model\Source\CreateThreatMetrixId; @@ -54,6 +57,11 @@ class Order */ private ProductMetadataInterface $_productMetadata; + /** + * @var ScopeConfigInterface + */ + private $scopeConfig; + /** * @var BasketFactory */ @@ -80,6 +88,7 @@ class Order * @param Config $moduleConfig * @param ModuleListInterface $moduleList * @param ProductMetadataInterface $productMetadata + * @param ScopeConfigInterface $scopeConfig * @param BasketFactory $basketFactory * @param BasketItemFactory $basketItemFactory * @param BirthDateFactory $birthDateFactory @@ -89,6 +98,7 @@ public function __construct( Config $moduleConfig, ModuleListInterface $moduleList, ProductMetadataInterface $productMetadata, + ScopeConfigInterface $scopeConfig, BasketFactory $basketFactory, BasketItemFactory $basketItemFactory, BirthDateFactory $birthDateFactory, @@ -97,6 +107,7 @@ public function __construct( $this->_moduleConfig = $moduleConfig; $this->_moduleList = $moduleList; $this->_productMetadata = $productMetadata; + $this->scopeConfig = $scopeConfig; $this->basketFactory = $basketFactory; $this->basketItemFactory = $basketItemFactory; $this->birthDateFactory = $birthDateFactory; @@ -113,6 +124,7 @@ public function __construct( public function createBasketForOrder(OrderModel $order): Basket { $basket = $this->createBasket($order); + $vatRate = 0; if ($order->getShippingAmount() > 0) { $basket->addBasketItem( @@ -132,11 +144,15 @@ public function createBasketForOrder(OrderModel $order): Basket $basket->addBasketItem( $this->createBasketItem($orderItem) ); + + if($orderItem->getTaxPercent() !== null){ + $vatRate = (float)$orderItem->getTaxPercent(); + } } if (abs($order->getBaseDiscountAmount()) > 0) { $basket->addBasketItem( - $this->createVoucherItem($order) + $this->createVoucherItem($order, $vatRate) ); } @@ -198,12 +214,36 @@ protected function createBasketItem(Item $orderItem): BasketItem * Create Voucher Item * * @param OrderModel $order + * @param float $vatRate * @return BasketItem */ - protected function createVoucherItem(OrderModel $order): BasketItem + protected function createVoucherItem(OrderModel $order, float $vatRate): BasketItem { + $discount = $order->getBaseDiscountAmount(); + $storeId = $order->getStoreId(); + + $taxAfterDiscount = $this->scopeConfig->getValue( + MagentoTaxConfig::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT, + ScopeInterface::SCOPE_STORE, + $storeId + ); + + $pricesIncludeTax = $this->scopeConfig->getValue( + MagentoTaxConfig::CONFIG_XML_PATH_PRICE_INCLUDES_TAX, + ScopeInterface::SCOPE_STORE, + $storeId + ); + + if ($taxAfterDiscount && !$pricesIncludeTax) { + $discount *= (1 + $vatRate/100); + } + $basketVoucherItemDiscountAmount = $this->basketItemFactory->create(); - $basketVoucherItemDiscountAmount->setAmountDiscountPerUnitGross(abs($order->getBaseDiscountAmount())); + $basketVoucherItemDiscountAmount->setAmountDiscountPerUnitGross( + abs(round($discount, 2, PHP_ROUND_HALF_DOWN) + ) + ); + $basketVoucherItemDiscountAmount->setVat($vatRate); $basketVoucherItemDiscountAmount->setAmountPerUnitGross(0); $basketVoucherItemDiscountAmount->setQuantity(1); $basketVoucherItemDiscountAmount->setTitle('Discount'); diff --git a/composer.json b/composer.json index 70be33e..63c46c5 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "unzerdev/magento2", "description": "This extension for Magento 2 provides a direct integration of the Unzer payment types to your Magento 2 shop via the Unzer Payment API (PAPI).", "type": "magento2-module", - "version": "3.2.5", + "version": "3.2.6", "license": "Apache-2.0", "require": { "php": "~7.4.0|~8.1.0|~8.2.0|~8.3.0", diff --git a/etc/module.xml b/etc/module.xml index f7ad1ca..f717fe6 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,7 +1,7 @@ - +