Skip to content

Commit c286714

Browse files
author
Mike Weis
committed
Merge branch 'MAGETWO-28380-tax-sorting' into develop
2 parents 2b0988d + 74ab06e commit c286714

File tree

2 files changed

+86
-17
lines changed

2 files changed

+86
-17
lines changed

app/code/Magento/Tax/Helper/Data.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -796,23 +796,6 @@ protected function calculateTaxForItems(EntityInterface $order, EntityInterface
796796

797797
$orderTaxDetails = $this->orderTaxManagement->getOrderTaxDetails($order->getId());
798798

799-
// Apply any taxes for shipping
800-
$shippingTaxAmount = $salesItem->getShippingTaxAmount();
801-
$originalShippingTaxAmount = $order->getShippingTaxAmount();
802-
if ($shippingTaxAmount && $originalShippingTaxAmount &&
803-
$shippingTaxAmount != 0 && floatval($originalShippingTaxAmount)
804-
) {
805-
//An invoice or credit memo can have a different qty than its order
806-
$shippingRatio = $shippingTaxAmount / $originalShippingTaxAmount;
807-
$itemTaxDetails = $orderTaxDetails->getItems();
808-
foreach ($itemTaxDetails as $itemTaxDetail) {
809-
//Aggregate taxable items associated with shipping
810-
if ($itemTaxDetail->getType() == \Magento\Sales\Model\Quote\Address::TYPE_SHIPPING) {
811-
$taxClassAmount = $this->_aggregateTaxes($taxClassAmount, $itemTaxDetail, $shippingRatio);
812-
}
813-
}
814-
}
815-
816799
// Apply any taxes for the items
817800
/** @var $item \Magento\Sales\Model\Order\Invoice\Item|\Magento\Sales\Model\Order\Creditmemo\Item */
818801
foreach ($salesItem->getItems() as $item) {
@@ -844,6 +827,24 @@ protected function calculateTaxForItems(EntityInterface $order, EntityInterface
844827
}
845828
}
846829

830+
// Apply any taxes for shipping
831+
$shippingTaxAmount = $salesItem->getShippingTaxAmount();
832+
$originalShippingTaxAmount = $order->getShippingTaxAmount();
833+
if ($shippingTaxAmount && $originalShippingTaxAmount &&
834+
$shippingTaxAmount != 0 && floatval($originalShippingTaxAmount)
835+
) {
836+
//An invoice or credit memo can have a different qty than its order
837+
$shippingRatio = $shippingTaxAmount / $originalShippingTaxAmount;
838+
$itemTaxDetails = $orderTaxDetails->getItems();
839+
foreach ($itemTaxDetails as $itemTaxDetail) {
840+
//Aggregate taxable items associated with shipping
841+
if ($itemTaxDetail->getType() == \Magento\Sales\Model\Quote\Address::TYPE_SHIPPING) {
842+
$taxClassAmount =
843+
$this->_aggregateTaxes($taxClassAmount, $itemTaxDetail, $shippingRatio);
844+
}
845+
}
846+
}
847+
847848
return $taxClassAmount;
848849
}
849850
}

dev/tests/unit/testsuite/Magento/Tax/Helper/DataTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,74 @@ public function getCalculatedTaxesForOrderItemsDataProvider()
365365
],
366366
],
367367
],
368+
//Scenario 3: one item, with both shipping and product taxes
369+
// note that 'shipping tax' is listed before 'product tax'
370+
'one_item_with_both_shipping_and_product_taxes' => [
371+
'order' => [
372+
'order_id' => 1,
373+
'shipping_tax_amount' => 2,
374+
'order_tax_details' => [
375+
'items' => [
376+
'shippingTax1' => [
377+
'item_id' => null,
378+
'type' => 'shipping',
379+
'applied_taxes' => [
380+
[
381+
'amount' => 2.0,
382+
'base_amount' => 2.0,
383+
'code' => 'US-CA-Ship',
384+
'title' => 'US-CA-Sales-Tax-Ship',
385+
'percent' => 10.0,
386+
],
387+
],
388+
],
389+
'itemTax1' => [
390+
'item_id' => 1,
391+
'applied_taxes' => [
392+
[
393+
'amount' => 5.0,
394+
'base_amount' => 5.0,
395+
'code' => 'US-CA',
396+
'title' => 'US-CA-Sales-Tax',
397+
'percent' => 20.0,
398+
],
399+
],
400+
],
401+
],
402+
],
403+
],
404+
'invoice' => [
405+
'shipping_tax_amount' => 2,
406+
'invoice_items' => [
407+
'item1' => new MagentoObject(
408+
[
409+
'order_item' => new MagentoObject(
410+
[
411+
'id' => 1,
412+
'tax_amount' => 5.00,
413+
]
414+
),
415+
'tax_amount' => 5.00,
416+
]
417+
),
418+
],
419+
],
420+
// note that 'shipping tax' is now listed after 'product tax'
421+
'expected_results' => [
422+
[
423+
'title' => 'US-CA-Sales-Tax',
424+
'percent' => 20.0,
425+
'tax_amount' => 5.00,
426+
'base_tax_amount' => 5.00,
427+
],
428+
[
429+
'title' => 'US-CA-Sales-Tax-Ship',
430+
'percent' => 10.0,
431+
'tax_amount' => 2.00,
432+
'base_tax_amount' => 2.00,
433+
],
434+
],
435+
],
368436
];
369437

370438
return $data;

0 commit comments

Comments
 (0)