Weee.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Model\Total\Invoice;
  7. use Magento\Weee\Helper\Data as WeeeHelper;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. use Magento\Framework\App\ObjectManager;
  10. class Weee extends \Magento\Sales\Model\Order\Invoice\Total\AbstractTotal
  11. {
  12. /**
  13. * Weee data
  14. *
  15. * @var WeeeHelper
  16. */
  17. protected $_weeeData = null;
  18. /**
  19. * Instance of serializer.
  20. *
  21. * @var Json
  22. */
  23. private $serializer;
  24. /**
  25. * Constructor
  26. *
  27. * By default is looking for first argument as array and assigns it as object
  28. * attributes This behavior may change in child classes
  29. *
  30. * @param WeeeHelper $weeeData
  31. * @param array $data
  32. * @param Json|null $serializer
  33. */
  34. public function __construct(
  35. WeeeHelper $weeeData,
  36. array $data = [],
  37. Json $serializer = null
  38. ) {
  39. $this->_weeeData = $weeeData;
  40. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  41. parent::__construct($data);
  42. }
  43. /**
  44. * Collect Weee amounts for the invoice
  45. *
  46. * @param \Magento\Sales\Model\Order\Invoice $invoice
  47. * @return $this
  48. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  49. * @SuppressWarnings(PHPMD.NPathComplexity)
  50. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  51. */
  52. public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
  53. {
  54. $store = $invoice->getStore();
  55. $order = $invoice->getOrder();
  56. $totalWeeeAmount = 0;
  57. $baseTotalWeeeAmount = 0;
  58. $totalWeeeAmountInclTax = 0;
  59. $baseTotalWeeeAmountInclTax = 0;
  60. $totalWeeeTaxAmount = 0;
  61. $baseTotalWeeeTaxAmount = 0;
  62. /** @var \Magento\Sales\Model\Order\Invoice\Item $item */
  63. foreach ($invoice->getAllItems() as $item) {
  64. $orderItem = $item->getOrderItem();
  65. $orderItemQty = $orderItem->getQtyOrdered();
  66. if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() < 0) {
  67. continue;
  68. }
  69. $ratio = $item->getQty() / $orderItemQty;
  70. $orderItemWeeeAmount = $orderItem->getWeeeTaxAppliedRowAmount();
  71. $orderItemBaseWeeeAmount = $orderItem->getBaseWeeeTaxAppliedRowAmnt();
  72. $weeeAmount = $invoice->roundPrice($orderItemWeeeAmount * $ratio);
  73. $baseWeeeAmount = $invoice->roundPrice($orderItemBaseWeeeAmount * $ratio, 'base');
  74. $orderItemWeeeInclTax = $this->_weeeData->getRowWeeeTaxInclTax($orderItem);
  75. $orderItemBaseWeeeInclTax = $this->_weeeData->getBaseRowWeeeTaxInclTax($orderItem);
  76. $weeeAmountInclTax = $invoice->roundPrice($orderItemWeeeInclTax * $ratio);
  77. $baseWeeeAmountInclTax = $invoice->roundPrice($orderItemBaseWeeeInclTax * $ratio, 'base');
  78. $orderItemWeeeTax = $orderItemWeeeInclTax - $orderItemWeeeAmount;
  79. $itemWeeeTax = $weeeAmountInclTax - $weeeAmount;
  80. $itemBaseWeeeTax = $baseWeeeAmountInclTax - $baseWeeeAmount;
  81. if ($item->isLast()) {
  82. $weeeAmount = $orderItemWeeeAmount - $this->_weeeData->getWeeeAmountInvoiced($orderItem);
  83. $baseWeeeAmount =
  84. $orderItemBaseWeeeAmount - $this->_weeeData->getBaseWeeeAmountInvoiced($orderItem);
  85. $itemWeeeTax = $orderItemWeeeTax - $this->_weeeData->getWeeeTaxAmountInvoiced($orderItem);
  86. $itemBaseWeeeTax =
  87. $orderItemWeeeTax - $this->_weeeData->getBaseWeeeTaxAmountInvoiced($orderItem);
  88. }
  89. $totalWeeeTaxAmount += $itemWeeeTax;
  90. $baseTotalWeeeTaxAmount += $itemBaseWeeeTax;
  91. //Set the ratio of the tax amount in invoice item compared to tax amount in order item
  92. //This information is needed to calculate tax per tax rate later
  93. if ($orderItemWeeeTax != 0) {
  94. $taxRatio = [];
  95. if ($item->getTaxRatio()) {
  96. $taxRatio = $this->serializer->unserialize($item->getTaxRatio());
  97. }
  98. $taxRatio[\Magento\Weee\Model\Total\Quote\Weee::ITEM_TYPE] = $itemWeeeTax / $orderItemWeeeTax;
  99. $item->setTaxRatio($this->serializer->serialize($taxRatio));
  100. }
  101. $item->setWeeeTaxAppliedRowAmount($weeeAmount);
  102. $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeAmount);
  103. $newApplied = [];
  104. $applied = $this->_weeeData->getApplied($orderItem);
  105. foreach ($applied as $one) {
  106. $title = (string)$one['title'];
  107. $one['base_row_amount'] = $invoice->roundPrice($one['base_row_amount'] * $ratio, $title.'_base');
  108. $one['row_amount'] = $invoice->roundPrice($one['row_amount'] * $ratio, $title);
  109. $one['base_row_amount_incl_tax'] = $invoice->roundPrice(
  110. $one['base_row_amount_incl_tax'] * $ratio,
  111. $title.'_base'
  112. );
  113. $one['row_amount_incl_tax'] = $invoice->roundPrice($one['row_amount_incl_tax'] * $ratio, $title);
  114. $newApplied[] = $one;
  115. }
  116. $this->_weeeData->setApplied($item, $newApplied);
  117. //Update order item
  118. $newApplied = [];
  119. $applied = $this->_weeeData->getApplied($orderItem);
  120. foreach ($applied as $one) {
  121. if (isset($one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED])) {
  122. $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED] =
  123. $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED] + $baseWeeeAmount;
  124. } else {
  125. $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED] = $baseWeeeAmount;
  126. }
  127. if (isset($one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED])) {
  128. $one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED] =
  129. $one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED] + $weeeAmount;
  130. } else {
  131. $one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED] = $weeeAmount;
  132. }
  133. if (isset($one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED])) {
  134. $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED] =
  135. $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED] + $itemWeeeTax;
  136. } else {
  137. $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED] = $itemWeeeTax;
  138. }
  139. if (isset($one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED])) {
  140. $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED] =
  141. $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED] + $itemBaseWeeeTax;
  142. } else {
  143. $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED] = $itemBaseWeeeTax;
  144. }
  145. $newApplied[] = $one;
  146. }
  147. $this->_weeeData->setApplied($orderItem, $newApplied);
  148. $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
  149. $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
  150. $totalWeeeAmount += $weeeAmount;
  151. $baseTotalWeeeAmount += $baseWeeeAmount;
  152. $totalWeeeAmountInclTax += $weeeAmountInclTax;
  153. $baseTotalWeeeAmountInclTax += $baseWeeeAmountInclTax;
  154. }
  155. $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced() - $invoice->getTaxAmount();
  156. $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $invoice->getBaseTaxAmount();
  157. $totalWeeeTaxAmount = min($totalWeeeTaxAmount, $allowedTax);
  158. $baseTotalWeeeTaxAmount = min($baseTotalWeeeTaxAmount, $allowedBaseTax);
  159. $invoice->setTaxAmount($invoice->getTaxAmount() + $totalWeeeTaxAmount);
  160. $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTotalWeeeTaxAmount);
  161. // Add FPT to subtotal and grand total
  162. if ($this->_weeeData->includeInSubtotal($store)) {
  163. $order = $invoice->getOrder();
  164. $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced() - $invoice->getSubtotal();
  165. $allowedBaseSubtotal = $order->getBaseSubtotal() -
  166. $order->getBaseSubtotalInvoiced() -
  167. $invoice->getBaseSubtotal();
  168. $totalWeeeAmount = min($allowedSubtotal, $totalWeeeAmount);
  169. $baseTotalWeeeAmount = min($allowedBaseSubtotal, $baseTotalWeeeAmount);
  170. $invoice->setSubtotal($invoice->getSubtotal() + $totalWeeeAmount);
  171. $invoice->setBaseSubtotal($invoice->getBaseSubtotal() + $baseTotalWeeeAmount);
  172. }
  173. if (!$invoice->isLast()) {
  174. // need to add the Weee amounts including all their taxes
  175. $invoice->setSubtotalInclTax($invoice->getSubtotalInclTax() + $totalWeeeAmountInclTax);
  176. $invoice->setBaseSubtotalInclTax($invoice->getBaseSubtotalInclTax() + $baseTotalWeeeAmountInclTax);
  177. } else {
  178. // since the Subtotal Incl Tax line will already have the taxes on Weee, just add the non-taxable amounts
  179. $invoice->setSubtotalInclTax($invoice->getSubtotalInclTax() + $totalWeeeAmount);
  180. $invoice->setBaseSubtotalInclTax($invoice->getBaseSubtotalInclTax() + $baseTotalWeeeAmount);
  181. }
  182. $invoice->setGrandTotal($invoice->getGrandTotal() + $totalWeeeAmount + $totalWeeeTaxAmount);
  183. $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalWeeeAmount + $baseTotalWeeeTaxAmount);
  184. return $this;
  185. }
  186. }