15
15
use Magento \Quote \Model \Quote \Item \AbstractItem ;
16
16
use Magento \Store \Model \Store ;
17
17
use Magento \Tax \Api \Data \QuoteDetailsInterfaceFactory ;
18
+ use Magento \Tax \Api \Data \QuoteDetailsItemInterface ;
18
19
use Magento \Tax \Api \Data \TaxClassKeyInterfaceFactory ;
19
20
use Magento \Tax \Api \Data \TaxClassKeyInterface ;
20
21
use Magento \Tax \Api \Data \TaxDetailsInterface ;
21
22
use Magento \Tax \Api \Data \TaxDetailsItemInterface ;
22
23
use Magento \Tax \Api \Data \QuoteDetailsInterface ;
23
24
use Magento \Quote \Api \Data \ShippingAssignmentInterface ;
25
+ use Magento \Tax \Helper \Data as TaxHelper ;
26
+ use Magento \Framework \App \ObjectManager ;
27
+ use Magento \Tax \Api \Data \QuoteDetailsItemExtensionInterface ;
28
+ use Magento \Tax \Api \Data \QuoteDetailsItemExtensionInterfaceFactory ;
24
29
25
30
/**
26
31
* Tax totals calculation model
@@ -129,6 +134,16 @@ class CommonTaxCollector extends AbstractTotal
129
134
*/
130
135
protected $ quoteDetailsItemDataObjectFactory ;
131
136
137
+ /**
138
+ * @var TaxHelper
139
+ */
140
+ private $ taxHelper ;
141
+
142
+ /**
143
+ * @var QuoteDetailsItemExtensionInterfaceFactory
144
+ */
145
+ private $ quoteDetailsItemExtensionFactory ;
146
+
132
147
/**
133
148
* Class constructor
134
149
*
@@ -139,6 +154,8 @@ class CommonTaxCollector extends AbstractTotal
139
154
* @param \Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyDataObjectFactory
140
155
* @param CustomerAddressFactory $customerAddressFactory
141
156
* @param CustomerAddressRegionFactory $customerAddressRegionFactory
157
+ * @param TaxHelper|null $taxHelper
158
+ * @param QuoteDetailsItemExtensionInterfaceFactory|null $quoteDetailsItemExtensionInterfaceFactory
142
159
*/
143
160
public function __construct (
144
161
\Magento \Tax \Model \Config $ taxConfig ,
@@ -147,7 +164,9 @@ public function __construct(
147
164
\Magento \Tax \Api \Data \QuoteDetailsItemInterfaceFactory $ quoteDetailsItemDataObjectFactory ,
148
165
\Magento \Tax \Api \Data \TaxClassKeyInterfaceFactory $ taxClassKeyDataObjectFactory ,
149
166
CustomerAddressFactory $ customerAddressFactory ,
150
- CustomerAddressRegionFactory $ customerAddressRegionFactory
167
+ CustomerAddressRegionFactory $ customerAddressRegionFactory ,
168
+ TaxHelper $ taxHelper = null ,
169
+ QuoteDetailsItemExtensionInterfaceFactory $ quoteDetailsItemExtensionInterfaceFactory = null
151
170
) {
152
171
$ this ->taxCalculationService = $ taxCalculationService ;
153
172
$ this ->quoteDetailsDataObjectFactory = $ quoteDetailsDataObjectFactory ;
@@ -156,6 +175,9 @@ public function __construct(
156
175
$ this ->quoteDetailsItemDataObjectFactory = $ quoteDetailsItemDataObjectFactory ;
157
176
$ this ->customerAddressFactory = $ customerAddressFactory ;
158
177
$ this ->customerAddressRegionFactory = $ customerAddressRegionFactory ;
178
+ $ this ->taxHelper = $ taxHelper ?: ObjectManager::getInstance ()->get (TaxHelper::class);
179
+ $ this ->quoteDetailsItemExtensionFactory = $ quoteDetailsItemExtensionInterfaceFactory ?:
180
+ ObjectManager::getInstance ()->get (QuoteDetailsItemExtensionInterfaceFactory::class);
159
181
}
160
182
161
183
/**
@@ -186,7 +208,7 @@ public function mapAddress(QuoteAddress $address)
186
208
* @param bool $priceIncludesTax
187
209
* @param bool $useBaseCurrency
188
210
* @param string $parentCode
189
- * @return \Magento\Tax\Api\Data\ QuoteDetailsItemInterface
211
+ * @return QuoteDetailsItemInterface
190
212
*/
191
213
public function mapItem (
192
214
\Magento \Tax \Api \Data \QuoteDetailsItemInterfaceFactory $ itemDataObjectFactory ,
@@ -199,7 +221,7 @@ public function mapItem(
199
221
$ sequence = 'sequence- ' . $ this ->getNextIncrement ();
200
222
$ item ->setTaxCalculationItemId ($ sequence );
201
223
}
202
- /** @var \Magento\Tax\Api\Data\ QuoteDetailsItemInterface $itemDataObject */
224
+ /** @var QuoteDetailsItemInterface $itemDataObject */
203
225
$ itemDataObject = $ itemDataObjectFactory ->create ();
204
226
$ itemDataObject ->setCode ($ item ->getTaxCalculationItemId ())
205
227
->setQuantity ($ item ->getQty ())
@@ -215,12 +237,28 @@ public function mapItem(
215
237
if (!$ item ->getBaseTaxCalculationPrice ()) {
216
238
$ item ->setBaseTaxCalculationPrice ($ item ->getBaseCalculationPriceOriginal ());
217
239
}
240
+
241
+ if ($ this ->taxHelper ->applyTaxOnOriginalPrice ()) {
242
+ $ baseTaxCalculationPrice = $ item ->getBaseOriginalPrice ();
243
+ } else {
244
+ $ baseTaxCalculationPrice = $ item ->getBaseCalculationPriceOriginal ();
245
+ }
246
+ $ this ->setPriceForTaxCalculation ($ itemDataObject , (float )$ baseTaxCalculationPrice );
247
+
218
248
$ itemDataObject ->setUnitPrice ($ item ->getBaseTaxCalculationPrice ())
219
249
->setDiscountAmount ($ item ->getBaseDiscountAmount ());
220
250
} else {
221
251
if (!$ item ->getTaxCalculationPrice ()) {
222
252
$ item ->setTaxCalculationPrice ($ item ->getCalculationPriceOriginal ());
223
253
}
254
+
255
+ if ($ this ->taxHelper ->applyTaxOnOriginalPrice ()) {
256
+ $ taxCalculationPrice = $ item ->getOriginalPrice ();
257
+ } else {
258
+ $ taxCalculationPrice = $ item ->getCalculationPriceOriginal ();
259
+ }
260
+ $ this ->setPriceForTaxCalculation ($ itemDataObject , (float )$ taxCalculationPrice );
261
+
224
262
$ itemDataObject ->setUnitPrice ($ item ->getTaxCalculationPrice ())
225
263
->setDiscountAmount ($ item ->getDiscountAmount ());
226
264
}
@@ -230,14 +268,31 @@ public function mapItem(
230
268
return $ itemDataObject ;
231
269
}
232
270
271
+ /**
272
+ * Set price for tax calculation.
273
+ *
274
+ * @param QuoteDetailsItemInterface $quoteDetailsItem
275
+ * @param float $taxCalculationPrice
276
+ * @return void
277
+ */
278
+ private function setPriceForTaxCalculation (QuoteDetailsItemInterface $ quoteDetailsItem , float $ taxCalculationPrice )
279
+ {
280
+ $ extensionAttributes = $ quoteDetailsItem ->getExtensionAttributes ();
281
+ if (!$ extensionAttributes ) {
282
+ $ extensionAttributes = $ this ->quoteDetailsItemExtensionFactory ->create ();
283
+ }
284
+ $ extensionAttributes ->setPriceForTaxCalculation ($ taxCalculationPrice );
285
+ $ quoteDetailsItem ->setExtensionAttributes ($ extensionAttributes );
286
+ }
287
+
233
288
/**
234
289
* Map item extra taxables
235
290
*
236
291
* @param \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $itemDataObjectFactory
237
292
* @param AbstractItem $item
238
293
* @param bool $priceIncludesTax
239
294
* @param bool $useBaseCurrency
240
- * @return \Magento\Tax\Api\Data\ QuoteDetailsItemInterface[]
295
+ * @return QuoteDetailsItemInterface[]
241
296
*/
242
297
public function mapItemExtraTaxables (
243
298
\Magento \Tax \Api \Data \QuoteDetailsItemInterfaceFactory $ itemDataObjectFactory ,
@@ -260,7 +315,7 @@ public function mapItemExtraTaxables(
260
315
} else {
261
316
$ unitPrice = $ extraTaxable [self ::KEY_ASSOCIATED_TAXABLE_UNIT_PRICE ];
262
317
}
263
- /** @var \Magento\Tax\Api\Data\ QuoteDetailsItemInterface $itemDataObject */
318
+ /** @var QuoteDetailsItemInterface $itemDataObject */
264
319
$ itemDataObject = $ itemDataObjectFactory ->create ();
265
320
$ itemDataObject ->setCode ($ extraTaxable [self ::KEY_ASSOCIATED_TAXABLE_CODE ])
266
321
->setType ($ extraTaxable [self ::KEY_ASSOCIATED_TAXABLE_TYPE ])
@@ -283,9 +338,9 @@ public function mapItemExtraTaxables(
283
338
* Add quote items
284
339
*
285
340
* @param ShippingAssignmentInterface $shippingAssignment
286
- * @param bool $useBaseCurrency
287
341
* @param bool $priceIncludesTax
288
- * @return \Magento\Tax\Api\Data\QuoteDetailsItemInterface[]
342
+ * @param bool $useBaseCurrency
343
+ * @return QuoteDetailsItemInterface[]
289
344
*/
290
345
public function mapItems (
291
346
ShippingAssignmentInterface $ shippingAssignment ,
@@ -361,10 +416,12 @@ public function populateAddressData(QuoteDetailsInterface $quoteDetails, QuoteAd
361
416
}
362
417
363
418
/**
419
+ * Get shipping data object.
420
+ *
364
421
* @param ShippingAssignmentInterface $shippingAssignment
365
422
* @param QuoteAddress\Total $total
366
423
* @param bool $useBaseCurrency
367
- * @return \Magento\Tax\Api\Data\ QuoteDetailsItemInterface
424
+ * @return QuoteDetailsItemInterface
368
425
*/
369
426
public function getShippingDataObject (
370
427
ShippingAssignmentInterface $ shippingAssignment ,
@@ -379,7 +436,7 @@ public function getShippingDataObject(
379
436
$ total ->setBaseShippingTaxCalculationAmount ($ total ->getBaseShippingAmount ());
380
437
}
381
438
if ($ total ->getShippingTaxCalculationAmount () !== null ) {
382
- /** @var \Magento\Tax\Api\Data\ QuoteDetailsItemInterface $itemDataObject */
439
+ /** @var QuoteDetailsItemInterface $itemDataObject */
383
440
$ itemDataObject = $ this ->quoteDetailsItemDataObjectFactory ->create ()
384
441
->setType (self ::ITEM_TYPE_SHIPPING )
385
442
->setCode (self ::ITEM_CODE_SHIPPING )
@@ -414,7 +471,7 @@ public function getShippingDataObject(
414
471
* Populate QuoteDetails object from quote address object
415
472
*
416
473
* @param ShippingAssignmentInterface $shippingAssignment
417
- * @param \Magento\Tax\Api\Data\ QuoteDetailsItemInterface[] $itemDataObjects
474
+ * @param QuoteDetailsItemInterface[] $itemDataObjects
418
475
* @return \Magento\Tax\Api\Data\QuoteDetailsInterface
419
476
*/
420
477
protected function prepareQuoteDetails (ShippingAssignmentInterface $ shippingAssignment , $ itemDataObjects )
@@ -543,6 +600,7 @@ protected function processProductItems(
543
600
* Process applied taxes for items and quote
544
601
*
545
602
* @param QuoteAddress\Total $total
603
+ * @param ShippingAssignmentInterface $shippingAssignment
546
604
* @param array $itemsByType
547
605
* @return $this
548
606
*/
@@ -846,8 +904,9 @@ protected function saveAppliedTaxes()
846
904
}
847
905
848
906
/**
849
- * Increment and return counter. This function is intended to be used to generate temporary
850
- * id for an item.
907
+ * Increment and return counter.
908
+ *
909
+ * This function is intended to be used to generate temporary id for an item.
851
910
*
852
911
* @return int
853
912
*/
0 commit comments