Skip to content

Commit e1e4dd6

Browse files
author
Mike Weis
committed
MAGETWO-28380: Totals taxes sorting is different on order/invoice/refund create and view pages
- fixed - updated unit test
1 parent 0efd237 commit e1e4dd6

File tree

2 files changed

+99
-20
lines changed

2 files changed

+99
-20
lines changed

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -682,17 +682,23 @@ public function getCalculatedTaxes($source)
682682
* 'tax_amount' => $taxAmount,
683683
* 'base_tax_amount' => $baseTaxAmount,
684684
* 'title' => $title,
685-
* 'percent' => $percent
685+
* 'percent' => $percent,
686+
* 'type' => $type
686687
* )
687688
* )
688689
*
689690
* @param array $taxClassAmount
690691
* @param OrderTaxDetailsItemInterface $itemTaxDetail
691692
* @param float $ratio
693+
* @param string $type
692694
* @return array
693695
*/
694-
private function _aggregateTaxes($taxClassAmount, OrderTaxDetailsItemInterface $itemTaxDetail, $ratio)
695-
{
696+
private function _aggregateTaxes(
697+
$taxClassAmount,
698+
OrderTaxDetailsItemInterface $itemTaxDetail,
699+
$ratio,
700+
$type = 'product'
701+
) {
696702
$itemAppliedTaxes = $itemTaxDetail->getAppliedTaxes();
697703
foreach ($itemAppliedTaxes as $itemAppliedTax) {
698704
$taxAmount = $itemAppliedTax->getAmount() * $ratio;
@@ -705,6 +711,7 @@ private function _aggregateTaxes($taxClassAmount, OrderTaxDetailsItemInterface $
705711
if (!isset($taxClassAmount[$taxCode])) {
706712
$taxClassAmount[$taxCode]['title'] = $itemAppliedTax->getTitle();
707713
$taxClassAmount[$taxCode]['percent'] = $itemAppliedTax->getPercent();
714+
$taxClassAmount[$taxCode]['type'] = $type;
708715
$taxClassAmount[$taxCode]['tax_amount'] = $taxAmount;
709716
$taxClassAmount[$taxCode]['base_tax_amount'] = $baseTaxAmount;
710717
} else {
@@ -796,23 +803,6 @@ protected function calculateTaxForItems(EntityInterface $order, EntityInterface
796803

797804
$orderTaxDetails = $this->orderTaxManagement->getOrderTaxDetails($order->getId());
798805

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-
816806
// Apply any taxes for the items
817807
/** @var $item \Magento\Sales\Model\Order\Invoice\Item|\Magento\Sales\Model\Order\Creditmemo\Item */
818808
foreach ($salesItem->getItems() as $item) {
@@ -844,6 +834,25 @@ protected function calculateTaxForItems(EntityInterface $order, EntityInterface
844834
}
845835
}
846836

837+
// Apply any taxes for shipping
838+
$shippingType = \Magento\Sales\Model\Quote\Address::TYPE_SHIPPING;
839+
$shippingTaxAmount = $salesItem->getShippingTaxAmount();
840+
$originalShippingTaxAmount = $order->getShippingTaxAmount();
841+
if ($shippingTaxAmount && $originalShippingTaxAmount &&
842+
$shippingTaxAmount != 0 && floatval($originalShippingTaxAmount)
843+
) {
844+
//An invoice or credit memo can have a different qty than its order
845+
$shippingRatio = $shippingTaxAmount / $originalShippingTaxAmount;
846+
$itemTaxDetails = $orderTaxDetails->getItems();
847+
foreach ($itemTaxDetails as $itemTaxDetail) {
848+
//Aggregate taxable items associated with shipping
849+
if ($itemTaxDetail->getType() == $shippingType) {
850+
$taxClassAmount =
851+
$this->_aggregateTaxes($taxClassAmount, $itemTaxDetail, $shippingRatio, $shippingType);
852+
}
853+
}
854+
}
855+
847856
return $taxClassAmount;
848857
}
849858
}

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ public function getCalculatedTaxesForOrderItemsDataProvider()
301301
'percent' => 20.0,
302302
'tax_amount' => 2.5,
303303
'base_tax_amount' => 2.5,
304+
'type' => 'product',
304305
],
305306
],
306307
],
@@ -362,6 +363,75 @@ public function getCalculatedTaxesForOrderItemsDataProvider()
362363
'percent' => 20.0,
363364
'tax_amount' => 6.5,
364365
'base_tax_amount' => 6.5,
366+
'type' => 'product',
367+
],
368+
],
369+
],
370+
//Scenario 3: one item, with both shipping and product taxes
371+
'one_item_with_both_shipping_and_product_taxes' => [
372+
'order' => [
373+
'order_id' => 1,
374+
'shipping_tax_amount' => 2,
375+
'order_tax_details' => [
376+
'items' => [
377+
'shippingTax1' => [
378+
'item_id' => null,
379+
'type' => 'shipping',
380+
'applied_taxes' => [
381+
[
382+
'amount' => 2.0,
383+
'base_amount' => 2.0,
384+
'code' => 'US-CA-Ship',
385+
'title' => 'US-CA-Sales-Tax-Ship',
386+
'percent' => 10.0,
387+
],
388+
],
389+
],
390+
'itemTax1' => [
391+
'item_id' => 1,
392+
'applied_taxes' => [
393+
[
394+
'amount' => 5.0,
395+
'base_amount' => 5.0,
396+
'code' => 'US-CA',
397+
'title' => 'US-CA-Sales-Tax',
398+
'percent' => 20.0,
399+
],
400+
],
401+
],
402+
],
403+
],
404+
],
405+
'invoice' => [
406+
'shipping_tax_amount' => 2,
407+
'invoice_items' => [
408+
'item1' => new MagentoObject(
409+
[
410+
'order_item' => new MagentoObject(
411+
[
412+
'id' => 1,
413+
'tax_amount' => 5.00,
414+
]
415+
),
416+
'tax_amount' => 5.00,
417+
]
418+
),
419+
],
420+
],
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+
'type' => 'product',
428+
],
429+
[
430+
'title' => 'US-CA-Sales-Tax-Ship',
431+
'percent' => 10.0,
432+
'tax_amount' => 2.00,
433+
'base_tax_amount' => 2.00,
434+
'type' => 'shipping',
365435
],
366436
],
367437
],

0 commit comments

Comments
 (0)