33
44namespace Unzer \PAPI \Helper ;
55
6+ use Magento \Framework \App \Config \ScopeConfigInterface ;
67use Magento \Framework \App \ProductMetadataInterface ;
78use Magento \Framework \Exception \LocalizedException ;
89use Magento \Framework \Module \ModuleListInterface ;
1213use Magento \Sales \Api \Data \OrderAddressInterface ;
1314use Magento \Sales \Model \Order as OrderModel ;
1415use Magento \Sales \Model \Order \Item ;
16+ use Magento \Store \Model \ScopeInterface ;
17+ use Magento \Tax \Model \Config as MagentoTaxConfig ;
1518use Unzer \PAPI \Block \System \Config \Form \Field \BirthDateFactory ;
1619use Unzer \PAPI \Model \Config ;
1720use Unzer \PAPI \Model \Source \CreateThreatMetrixId ;
@@ -54,6 +57,11 @@ class Order
5457 */
5558 private ProductMetadataInterface $ _productMetadata ;
5659
60+ /**
61+ * @var ScopeConfigInterface
62+ */
63+ private $ scopeConfig ;
64+
5765 /**
5866 * @var BasketFactory
5967 */
@@ -80,6 +88,7 @@ class Order
8088 * @param Config $moduleConfig
8189 * @param ModuleListInterface $moduleList
8290 * @param ProductMetadataInterface $productMetadata
91+ * @param ScopeConfigInterface $scopeConfig
8392 * @param BasketFactory $basketFactory
8493 * @param BasketItemFactory $basketItemFactory
8594 * @param BirthDateFactory $birthDateFactory
@@ -89,6 +98,7 @@ public function __construct(
8998 Config $ moduleConfig ,
9099 ModuleListInterface $ moduleList ,
91100 ProductMetadataInterface $ productMetadata ,
101+ ScopeConfigInterface $ scopeConfig ,
92102 BasketFactory $ basketFactory ,
93103 BasketItemFactory $ basketItemFactory ,
94104 BirthDateFactory $ birthDateFactory ,
@@ -97,6 +107,7 @@ public function __construct(
97107 $ this ->_moduleConfig = $ moduleConfig ;
98108 $ this ->_moduleList = $ moduleList ;
99109 $ this ->_productMetadata = $ productMetadata ;
110+ $ this ->scopeConfig = $ scopeConfig ;
100111 $ this ->basketFactory = $ basketFactory ;
101112 $ this ->basketItemFactory = $ basketItemFactory ;
102113 $ this ->birthDateFactory = $ birthDateFactory ;
@@ -113,6 +124,7 @@ public function __construct(
113124 public function createBasketForOrder (OrderModel $ order ): Basket
114125 {
115126 $ basket = $ this ->createBasket ($ order );
127+ $ vatRate = 0 ;
116128
117129 if ($ order ->getShippingAmount () > 0 ) {
118130 $ basket ->addBasketItem (
@@ -132,11 +144,15 @@ public function createBasketForOrder(OrderModel $order): Basket
132144 $ basket ->addBasketItem (
133145 $ this ->createBasketItem ($ orderItem )
134146 );
147+
148+ if ($ orderItem ->getTaxPercent () !== null ){
149+ $ vatRate = (float )$ orderItem ->getTaxPercent ();
150+ }
135151 }
136152
137153 if (abs ($ order ->getBaseDiscountAmount ()) > 0 ) {
138154 $ basket ->addBasketItem (
139- $ this ->createVoucherItem ($ order )
155+ $ this ->createVoucherItem ($ order, $ vatRate )
140156 );
141157 }
142158
@@ -198,12 +214,36 @@ protected function createBasketItem(Item $orderItem): BasketItem
198214 * Create Voucher Item
199215 *
200216 * @param OrderModel $order
217+ * @param float $vatRate
201218 * @return BasketItem
202219 */
203- protected function createVoucherItem (OrderModel $ order ): BasketItem
220+ protected function createVoucherItem (OrderModel $ order, float $ vatRate ): BasketItem
204221 {
222+ $ discount = $ order ->getBaseDiscountAmount ();
223+ $ storeId = $ order ->getStoreId ();
224+
225+ $ taxAfterDiscount = $ this ->scopeConfig ->getValue (
226+ MagentoTaxConfig::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT ,
227+ ScopeInterface::SCOPE_STORE ,
228+ $ storeId
229+ );
230+
231+ $ pricesIncludeTax = $ this ->scopeConfig ->getValue (
232+ MagentoTaxConfig::CONFIG_XML_PATH_PRICE_INCLUDES_TAX ,
233+ ScopeInterface::SCOPE_STORE ,
234+ $ storeId
235+ );
236+
237+ if ($ taxAfterDiscount && !$ pricesIncludeTax ) {
238+ $ discount *= (1 + $ vatRate /100 );
239+ }
240+
205241 $ basketVoucherItemDiscountAmount = $ this ->basketItemFactory ->create ();
206- $ basketVoucherItemDiscountAmount ->setAmountDiscountPerUnitGross (abs ($ order ->getBaseDiscountAmount ()));
242+ $ basketVoucherItemDiscountAmount ->setAmountDiscountPerUnitGross (
243+ abs (round ($ discount , 2 , PHP_ROUND_HALF_DOWN )
244+ )
245+ );
246+ $ basketVoucherItemDiscountAmount ->setVat ($ vatRate );
207247 $ basketVoucherItemDiscountAmount ->setAmountPerUnitGross (0 );
208248 $ basketVoucherItemDiscountAmount ->setQuantity (1 );
209249 $ basketVoucherItemDiscountAmount ->setTitle ('Discount ' );
0 commit comments