PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Weee/Model/Total/Invoice/Weee.php

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