@@ -682,17 +682,23 @@ public function getCalculatedTaxes($source)
682
682
* 'tax_amount' => $taxAmount,
683
683
* 'base_tax_amount' => $baseTaxAmount,
684
684
* 'title' => $title,
685
- * 'percent' => $percent
685
+ * 'percent' => $percent,
686
+ * 'type' => $type
686
687
* )
687
688
* )
688
689
*
689
690
* @param array $taxClassAmount
690
691
* @param OrderTaxDetailsItemInterface $itemTaxDetail
691
692
* @param float $ratio
693
+ * @param string $type
692
694
* @return array
693
695
*/
694
- private function _aggregateTaxes ($ taxClassAmount , OrderTaxDetailsItemInterface $ itemTaxDetail , $ ratio )
695
- {
696
+ private function _aggregateTaxes (
697
+ $ taxClassAmount ,
698
+ OrderTaxDetailsItemInterface $ itemTaxDetail ,
699
+ $ ratio ,
700
+ $ type = 'product '
701
+ ) {
696
702
$ itemAppliedTaxes = $ itemTaxDetail ->getAppliedTaxes ();
697
703
foreach ($ itemAppliedTaxes as $ itemAppliedTax ) {
698
704
$ taxAmount = $ itemAppliedTax ->getAmount () * $ ratio ;
@@ -705,6 +711,7 @@ private function _aggregateTaxes($taxClassAmount, OrderTaxDetailsItemInterface $
705
711
if (!isset ($ taxClassAmount [$ taxCode ])) {
706
712
$ taxClassAmount [$ taxCode ]['title ' ] = $ itemAppliedTax ->getTitle ();
707
713
$ taxClassAmount [$ taxCode ]['percent ' ] = $ itemAppliedTax ->getPercent ();
714
+ $ taxClassAmount [$ taxCode ]['type ' ] = $ type ;
708
715
$ taxClassAmount [$ taxCode ]['tax_amount ' ] = $ taxAmount ;
709
716
$ taxClassAmount [$ taxCode ]['base_tax_amount ' ] = $ baseTaxAmount ;
710
717
} else {
@@ -796,23 +803,6 @@ protected function calculateTaxForItems(EntityInterface $order, EntityInterface
796
803
797
804
$ orderTaxDetails = $ this ->orderTaxManagement ->getOrderTaxDetails ($ order ->getId ());
798
805
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
-
816
806
// Apply any taxes for the items
817
807
/** @var $item \Magento\Sales\Model\Order\Invoice\Item|\Magento\Sales\Model\Order\Creditmemo\Item */
818
808
foreach ($ salesItem ->getItems () as $ item ) {
@@ -844,6 +834,25 @@ protected function calculateTaxForItems(EntityInterface $order, EntityInterface
844
834
}
845
835
}
846
836
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
+
847
856
return $ taxClassAmount ;
848
857
}
849
858
}
0 commit comments