Tax.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Creditmemo\Total;
  7. /**
  8. * Collects credit memo taxes.
  9. */
  10. class Tax extends AbstractTotal
  11. {
  12. /**
  13. * Collects credit memo taxes.
  14. *
  15. * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  16. * @return $this
  17. *
  18. * @SuppressWarnings(PHPMD.NPathComplexity)
  19. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  20. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  21. */
  22. public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
  23. {
  24. $shippingTaxAmount = 0;
  25. $baseShippingTaxAmount = 0;
  26. $totalTax = 0;
  27. $baseTotalTax = 0;
  28. $totalDiscountTaxCompensation = 0;
  29. $baseTotalDiscountTaxCompensation = 0;
  30. $order = $creditmemo->getOrder();
  31. /** @var $item \Magento\Sales\Model\Order\Creditmemo\Item */
  32. foreach ($creditmemo->getAllItems() as $item) {
  33. $orderItem = $item->getOrderItem();
  34. if ($orderItem->isDummy() || $item->getQty() <= 0) {
  35. continue;
  36. }
  37. $orderItemTax = (double)$orderItem->getTaxInvoiced();
  38. $baseOrderItemTax = (double)$orderItem->getBaseTaxInvoiced();
  39. $orderItemQty = (double)$orderItem->getQtyInvoiced();
  40. if ($orderItemTax && $orderItemQty) {
  41. /**
  42. * Check item tax amount
  43. */
  44. $tax = $orderItemTax - $orderItem->getTaxRefunded();
  45. $baseTax = $baseOrderItemTax - $orderItem->getBaseTaxRefunded();
  46. $discountTaxCompensation = $orderItem->getDiscountTaxCompensationInvoiced() -
  47. $orderItem->getDiscountTaxCompensationRefunded();
  48. $baseDiscountTaxCompensation = $orderItem->getBaseDiscountTaxCompensationInvoiced() -
  49. $orderItem->getBaseDiscountTaxCompensationRefunded();
  50. if (!$item->isLast()) {
  51. $availableQty = $orderItemQty - $orderItem->getQtyRefunded();
  52. $tax = $creditmemo->roundPrice($tax / $availableQty * $item->getQty());
  53. $baseTax = $creditmemo->roundPrice($baseTax / $availableQty * $item->getQty(), 'base');
  54. $discountTaxCompensation =
  55. $creditmemo->roundPrice($discountTaxCompensation / $availableQty * $item->getQty());
  56. $baseDiscountTaxCompensation =
  57. $creditmemo->roundPrice($baseDiscountTaxCompensation / $availableQty * $item->getQty(), 'base');
  58. }
  59. $item->setTaxAmount($tax);
  60. $item->setBaseTaxAmount($baseTax);
  61. $item->setDiscountTaxCompensationAmount($discountTaxCompensation);
  62. $item->setBaseDiscountTaxCompensationAmount($baseDiscountTaxCompensation);
  63. $totalTax += $tax;
  64. $baseTotalTax += $baseTax;
  65. $totalDiscountTaxCompensation += $discountTaxCompensation;
  66. $baseTotalDiscountTaxCompensation += $baseDiscountTaxCompensation;
  67. }
  68. }
  69. $isPartialShippingRefunded = false;
  70. $baseOrderShippingAmount = (float)$order->getBaseShippingAmount();
  71. if ($invoice = $creditmemo->getInvoice()) {
  72. //recalculate tax amounts in case if refund shipping value was changed
  73. if ($baseOrderShippingAmount && $creditmemo->getBaseShippingAmount() !== null) {
  74. $taxFactor = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;
  75. $shippingTaxAmount = $invoice->getShippingTaxAmount() * $taxFactor;
  76. $baseShippingTaxAmount = $invoice->getBaseShippingTaxAmount() * $taxFactor;
  77. $totalDiscountTaxCompensation += $invoice->getShippingDiscountTaxCompensationAmount() * $taxFactor;
  78. $baseTotalDiscountTaxCompensation +=
  79. $invoice->getBaseShippingDiscountTaxCompensationAmnt() * $taxFactor;
  80. $shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
  81. $baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
  82. $totalDiscountTaxCompensation = $creditmemo->roundPrice($totalDiscountTaxCompensation);
  83. $baseTotalDiscountTaxCompensation = $creditmemo->roundPrice($baseTotalDiscountTaxCompensation, 'base');
  84. if ($taxFactor < 1 && $invoice->getShippingTaxAmount() > 0) {
  85. $isPartialShippingRefunded = true;
  86. }
  87. $totalTax += $shippingTaxAmount;
  88. $baseTotalTax += $baseShippingTaxAmount;
  89. }
  90. } else {
  91. $orderShippingAmount = $order->getShippingAmount();
  92. $baseOrderShippingRefundedAmount = $order->getBaseShippingRefunded();
  93. $shippingTaxAmount = 0;
  94. $baseShippingTaxAmount = 0;
  95. $shippingDiscountTaxCompensationAmount = 0;
  96. $baseShippingDiscountTaxCompensationAmount = 0;
  97. $shippingDelta = $baseOrderShippingAmount - $baseOrderShippingRefundedAmount;
  98. if ($shippingDelta > $creditmemo->getBaseShippingAmount()) {
  99. $part = $creditmemo->getShippingAmount() / $orderShippingAmount;
  100. $basePart = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;
  101. $shippingTaxAmount = $order->getShippingTaxAmount() * $part;
  102. $baseShippingTaxAmount = $order->getBaseShippingTaxAmount() * $basePart;
  103. $shippingDiscountTaxCompensationAmount = $order->getShippingDiscountTaxCompensationAmount() * $part;
  104. $baseShippingDiscountTaxCompensationAmount =
  105. $order->getBaseShippingDiscountTaxCompensationAmnt() * $basePart;
  106. $shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
  107. $baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
  108. $shippingDiscountTaxCompensationAmount =
  109. $creditmemo->roundPrice($shippingDiscountTaxCompensationAmount);
  110. $baseShippingDiscountTaxCompensationAmount =
  111. $creditmemo->roundPrice($baseShippingDiscountTaxCompensationAmount, 'base');
  112. if ($part < 1 && $order->getShippingTaxAmount() > 0) {
  113. $isPartialShippingRefunded = true;
  114. }
  115. } elseif ($shippingDelta == $creditmemo->getBaseShippingAmount()) {
  116. $shippingTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
  117. $baseShippingTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
  118. $shippingDiscountTaxCompensationAmount = $order->getShippingDiscountTaxCompensationAmount() -
  119. $order->getShippingDiscountTaxCompensationRefunded();
  120. $baseShippingDiscountTaxCompensationAmount = $order->getBaseShippingDiscountTaxCompensationAmnt() -
  121. $order->getBaseShippingDiscountTaxCompensationRefunded();
  122. }
  123. $totalTax += $shippingTaxAmount;
  124. $baseTotalTax += $baseShippingTaxAmount;
  125. $totalDiscountTaxCompensation += $shippingDiscountTaxCompensationAmount;
  126. $baseTotalDiscountTaxCompensation += $baseShippingDiscountTaxCompensationAmount;
  127. }
  128. $allowedTax = $order->getTaxInvoiced() - $order->getTaxRefunded() - $creditmemo->getTaxAmount();
  129. $allowedBaseTax = $order->getBaseTaxInvoiced() - $order->getBaseTaxRefunded() - $creditmemo->getBaseTaxAmount();
  130. $allowedDiscountTaxCompensation = $order->getDiscountTaxCompensationInvoiced() +
  131. $order->getShippingDiscountTaxCompensationAmount() -
  132. $order->getDiscountTaxCompensationRefunded() -
  133. $order->getShippingDiscountTaxCompensationRefunded() -
  134. $creditmemo->getDiscountTaxCompensationAmount() -
  135. $creditmemo->getShippingDiscountTaxCompensationAmount();
  136. $allowedBaseDiscountTaxCompensation = $order->getBaseDiscountTaxCompensationInvoiced() +
  137. $order->getBaseShippingDiscountTaxCompensationAmnt() -
  138. $order->getBaseDiscountTaxCompensationRefunded() -
  139. $order->getBaseShippingDiscountTaxCompensationRefunded() -
  140. $creditmemo->getBaseShippingDiscountTaxCompensationAmnt() -
  141. $creditmemo->getBaseDiscountTaxCompensationAmount();
  142. if ($creditmemo->isLast() && !$isPartialShippingRefunded) {
  143. $totalTax = $allowedTax;
  144. $baseTotalTax = $allowedBaseTax;
  145. $totalDiscountTaxCompensation = $allowedDiscountTaxCompensation;
  146. $baseTotalDiscountTaxCompensation = $allowedBaseDiscountTaxCompensation;
  147. } else {
  148. $totalTax = min($allowedTax, $totalTax);
  149. $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
  150. $totalDiscountTaxCompensation =
  151. min($allowedDiscountTaxCompensation, $totalDiscountTaxCompensation);
  152. $baseTotalDiscountTaxCompensation =
  153. min($allowedBaseDiscountTaxCompensation, $baseTotalDiscountTaxCompensation);
  154. }
  155. $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $totalTax);
  156. $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTotalTax);
  157. $creditmemo->setDiscountTaxCompensationAmount($totalDiscountTaxCompensation);
  158. $creditmemo->setBaseDiscountTaxCompensationAmount($baseTotalDiscountTaxCompensation);
  159. $creditmemo->setShippingTaxAmount($shippingTaxAmount);
  160. $creditmemo->setBaseShippingTaxAmount($baseShippingTaxAmount);
  161. $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax + $totalDiscountTaxCompensation);
  162. $creditmemo->setBaseGrandTotal(
  163. $creditmemo->getBaseGrandTotal() +
  164. $baseTotalTax + $baseTotalDiscountTaxCompensation
  165. );
  166. return $this;
  167. }
  168. }