3
3
4
4
namespace Unzer \PAPI \Helper ;
5
5
6
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
6
7
use Magento \Framework \App \ProductMetadataInterface ;
7
8
use Magento \Framework \Exception \LocalizedException ;
8
9
use Magento \Framework \Module \ModuleListInterface ;
12
13
use Magento \Sales \Api \Data \OrderAddressInterface ;
13
14
use Magento \Sales \Model \Order as OrderModel ;
14
15
use Magento \Sales \Model \Order \Item ;
16
+ use Magento \Store \Model \ScopeInterface ;
17
+ use Magento \Tax \Model \Config as MagentoTaxConfig ;
15
18
use Unzer \PAPI \Block \System \Config \Form \Field \BirthDateFactory ;
16
19
use Unzer \PAPI \Model \Config ;
17
20
use Unzer \PAPI \Model \Source \CreateThreatMetrixId ;
@@ -54,6 +57,11 @@ class Order
54
57
*/
55
58
private ProductMetadataInterface $ _productMetadata ;
56
59
60
+ /**
61
+ * @var ScopeConfigInterface
62
+ */
63
+ private $ scopeConfig ;
64
+
57
65
/**
58
66
* @var BasketFactory
59
67
*/
@@ -80,6 +88,7 @@ class Order
80
88
* @param Config $moduleConfig
81
89
* @param ModuleListInterface $moduleList
82
90
* @param ProductMetadataInterface $productMetadata
91
+ * @param ScopeConfigInterface $scopeConfig
83
92
* @param BasketFactory $basketFactory
84
93
* @param BasketItemFactory $basketItemFactory
85
94
* @param BirthDateFactory $birthDateFactory
@@ -89,6 +98,7 @@ public function __construct(
89
98
Config $ moduleConfig ,
90
99
ModuleListInterface $ moduleList ,
91
100
ProductMetadataInterface $ productMetadata ,
101
+ ScopeConfigInterface $ scopeConfig ,
92
102
BasketFactory $ basketFactory ,
93
103
BasketItemFactory $ basketItemFactory ,
94
104
BirthDateFactory $ birthDateFactory ,
@@ -97,6 +107,7 @@ public function __construct(
97
107
$ this ->_moduleConfig = $ moduleConfig ;
98
108
$ this ->_moduleList = $ moduleList ;
99
109
$ this ->_productMetadata = $ productMetadata ;
110
+ $ this ->scopeConfig = $ scopeConfig ;
100
111
$ this ->basketFactory = $ basketFactory ;
101
112
$ this ->basketItemFactory = $ basketItemFactory ;
102
113
$ this ->birthDateFactory = $ birthDateFactory ;
@@ -113,6 +124,7 @@ public function __construct(
113
124
public function createBasketForOrder (OrderModel $ order ): Basket
114
125
{
115
126
$ basket = $ this ->createBasket ($ order );
127
+ $ vatRate = 0 ;
116
128
117
129
if ($ order ->getShippingAmount () > 0 ) {
118
130
$ basket ->addBasketItem (
@@ -132,11 +144,15 @@ public function createBasketForOrder(OrderModel $order): Basket
132
144
$ basket ->addBasketItem (
133
145
$ this ->createBasketItem ($ orderItem )
134
146
);
147
+
148
+ if ($ orderItem ->getTaxPercent () !== null ){
149
+ $ vatRate = (float )$ orderItem ->getTaxPercent ();
150
+ }
135
151
}
136
152
137
153
if (abs ($ order ->getBaseDiscountAmount ()) > 0 ) {
138
154
$ basket ->addBasketItem (
139
- $ this ->createVoucherItem ($ order )
155
+ $ this ->createVoucherItem ($ order, $ vatRate )
140
156
);
141
157
}
142
158
@@ -198,12 +214,36 @@ protected function createBasketItem(Item $orderItem): BasketItem
198
214
* Create Voucher Item
199
215
*
200
216
* @param OrderModel $order
217
+ * @param float $vatRate
201
218
* @return BasketItem
202
219
*/
203
- protected function createVoucherItem (OrderModel $ order ): BasketItem
220
+ protected function createVoucherItem (OrderModel $ order, float $ vatRate ): BasketItem
204
221
{
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
+
205
241
$ 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 );
207
247
$ basketVoucherItemDiscountAmount ->setAmountPerUnitGross (0 );
208
248
$ basketVoucherItemDiscountAmount ->setQuantity (1 );
209
249
$ basketVoucherItemDiscountAmount ->setTitle ('Discount ' );
0 commit comments