Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Weee.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Weee\Helper\Data as WeeeHelper;
11 
12 class Weee extends \Magento\Sales\Model\Order\Invoice\Total\AbstractTotal
13 {
19  protected $_weeeData = null;
20 
26  private $serializer;
27 
38  public function __construct(
39  WeeeHelper $weeeData,
40  array $data = [],
41  Json $serializer = null
42  ) {
43  $this->_weeeData = $weeeData;
44  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
45  parent::__construct($data);
46  }
47 
57  public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
58  {
59  $store = $invoice->getStore();
60  $order = $invoice->getOrder();
61 
62  $totalWeeeAmount = 0;
63  $baseTotalWeeeAmount = 0;
64  $totalWeeeAmountInclTax = 0;
65  $baseTotalWeeeAmountInclTax = 0;
66  $totalWeeeTaxAmount = 0;
67  $baseTotalWeeeTaxAmount = 0;
68 
70  foreach ($invoice->getAllItems() as $item) {
71  $orderItem = $item->getOrderItem();
72  $orderItemQty = $orderItem->getQtyOrdered();
73 
74  if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() < 0) {
75  continue;
76  }
77 
78  $ratio = $item->getQty() / $orderItemQty;
79 
80  $orderItemWeeeAmount = $orderItem->getWeeeTaxAppliedRowAmount();
81  $orderItemBaseWeeeAmount = $orderItem->getBaseWeeeTaxAppliedRowAmnt();
82  $weeeAmount = $invoice->roundPrice($orderItemWeeeAmount * $ratio);
83  $baseWeeeAmount = $invoice->roundPrice($orderItemBaseWeeeAmount * $ratio, 'base');
84 
85  $orderItemWeeeInclTax = $this->_weeeData->getRowWeeeTaxInclTax($orderItem);
86  $orderItemBaseWeeeInclTax = $this->_weeeData->getBaseRowWeeeTaxInclTax($orderItem);
87  $weeeAmountInclTax = $invoice->roundPrice($orderItemWeeeInclTax * $ratio);
88  $baseWeeeAmountInclTax = $invoice->roundPrice($orderItemBaseWeeeInclTax * $ratio, 'base');
89 
90  $orderItemWeeeTax = $orderItemWeeeInclTax - $orderItemWeeeAmount;
91  $itemWeeeTax = $weeeAmountInclTax - $weeeAmount;
92  $itemBaseWeeeTax = $baseWeeeAmountInclTax - $baseWeeeAmount;
93 
94  if ($item->isLast()) {
95  $weeeAmount = $orderItemWeeeAmount - $this->_weeeData->getWeeeAmountInvoiced($orderItem);
96  $baseWeeeAmount =
97  $orderItemBaseWeeeAmount - $this->_weeeData->getBaseWeeeAmountInvoiced($orderItem);
98  $itemWeeeTax = $orderItemWeeeTax - $this->_weeeData->getWeeeTaxAmountInvoiced($orderItem);
99  $itemBaseWeeeTax =
100  $orderItemWeeeTax - $this->_weeeData->getBaseWeeeTaxAmountInvoiced($orderItem);
101  }
102 
103  $totalWeeeTaxAmount += $itemWeeeTax;
104  $baseTotalWeeeTaxAmount += $itemBaseWeeeTax;
105 
106  //Set the ratio of the tax amount in invoice item compared to tax amount in order item
107  //This information is needed to calculate tax per tax rate later
108  if ($orderItemWeeeTax != 0) {
109  $taxRatio = [];
110  if ($item->getTaxRatio()) {
111  $taxRatio = $this->serializer->unserialize($item->getTaxRatio());
112  }
113  $taxRatio[\Magento\Weee\Model\Total\Quote\Weee::ITEM_TYPE] = $itemWeeeTax / $orderItemWeeeTax;
114  $item->setTaxRatio($this->serializer->serialize($taxRatio));
115  }
116 
117  $item->setWeeeTaxAppliedRowAmount($weeeAmount);
118  $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeAmount);
119  $newApplied = [];
120  $applied = $this->_weeeData->getApplied($orderItem);
121  foreach ($applied as $one) {
122  $title = (string)$one['title'];
123  $one['base_row_amount'] = $invoice->roundPrice($one['base_row_amount'] * $ratio, $title.'_base');
124  $one['row_amount'] = $invoice->roundPrice($one['row_amount'] * $ratio, $title);
125  $one['base_row_amount_incl_tax'] = $invoice->roundPrice(
126  $one['base_row_amount_incl_tax'] * $ratio,
127  $title.'_base'
128  );
129  $one['row_amount_incl_tax'] = $invoice->roundPrice($one['row_amount_incl_tax'] * $ratio, $title);
130 
131  $newApplied[] = $one;
132  }
133  $this->_weeeData->setApplied($item, $newApplied);
134 
135  //Update order item
136  $newApplied = [];
137  $applied = $this->_weeeData->getApplied($orderItem);
138  foreach ($applied as $one) {
139  if (isset($one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED])) {
140  $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED] =
141  $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED] + $baseWeeeAmount;
142  } else {
143  $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED] = $baseWeeeAmount;
144  }
145  if (isset($one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED])) {
146  $one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED] =
147  $one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED] + $weeeAmount;
148  } else {
149  $one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED] = $weeeAmount;
150  }
151  if (isset($one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED])) {
152  $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED] =
153  $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED] + $itemWeeeTax;
154  } else {
155  $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED] = $itemWeeeTax;
156  }
157  if (isset($one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED])) {
158  $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED] =
159  $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED] + $itemBaseWeeeTax;
160  } else {
161  $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED] = $itemBaseWeeeTax;
162  }
163  $newApplied[] = $one;
164  }
165  $this->_weeeData->setApplied($orderItem, $newApplied);
166 
167  $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
168  $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
169 
170  $totalWeeeAmount += $weeeAmount;
171  $baseTotalWeeeAmount += $baseWeeeAmount;
172 
173  $totalWeeeAmountInclTax += $weeeAmountInclTax;
174  $baseTotalWeeeAmountInclTax += $baseWeeeAmountInclTax;
175  }
176 
177  $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced() - $invoice->getTaxAmount();
178  $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $invoice->getBaseTaxAmount();
179  $totalWeeeTaxAmount = min($totalWeeeTaxAmount, $allowedTax);
180  $baseTotalWeeeTaxAmount = min($baseTotalWeeeTaxAmount, $allowedBaseTax);
181 
182  $invoice->setTaxAmount($invoice->getTaxAmount() + $totalWeeeTaxAmount);
183  $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTotalWeeeTaxAmount);
184 
185  // Add FPT to subtotal and grand total
186  if ($this->_weeeData->includeInSubtotal($store)) {
187  $order = $invoice->getOrder();
188  $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced() - $invoice->getSubtotal();
189  $allowedBaseSubtotal = $order->getBaseSubtotal() -
190  $order->getBaseSubtotalInvoiced() -
191  $invoice->getBaseSubtotal();
192  $totalWeeeAmount = min($allowedSubtotal, $totalWeeeAmount);
193  $baseTotalWeeeAmount = min($allowedBaseSubtotal, $baseTotalWeeeAmount);
194 
195  $invoice->setSubtotal($invoice->getSubtotal() + $totalWeeeAmount);
196  $invoice->setBaseSubtotal($invoice->getBaseSubtotal() + $baseTotalWeeeAmount);
197  }
198 
199  if (!$invoice->isLast()) {
200  // need to add the Weee amounts including all their taxes
201  $invoice->setSubtotalInclTax($invoice->getSubtotalInclTax() + $totalWeeeAmountInclTax);
202  $invoice->setBaseSubtotalInclTax($invoice->getBaseSubtotalInclTax() + $baseTotalWeeeAmountInclTax);
203  } else {
204  // since the Subtotal Incl Tax line will already have the taxes on Weee, just add the non-taxable amounts
205  $invoice->setSubtotalInclTax($invoice->getSubtotalInclTax() + $totalWeeeAmount);
206  $invoice->setBaseSubtotalInclTax($invoice->getBaseSubtotalInclTax() + $baseTotalWeeeAmount);
207  }
208 
209  $invoice->setGrandTotal($invoice->getGrandTotal() + $totalWeeeAmount + $totalWeeeTaxAmount);
210  $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalWeeeAmount + $baseTotalWeeeTaxAmount);
211 
212  return $this;
213  }
214 }
$title
Definition: default.phtml:14
collect(\Magento\Sales\Model\Order\Invoice $invoice)
$orderItem
Definition: order.php:30
$order
Definition: order.php:55
__construct(WeeeHelper $weeeData, array $data=[], Json $serializer=null)
Definition: Weee.php:38
$invoice