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
8 
13 
14 class Weee extends AbstractTotal
15 {
19  const ITEM_CODE_WEEE_PREFIX = 'weee';
23  const ITEM_TYPE = 'weee';
24 
28  protected $weeeData;
29 
33  protected $_store;
34 
40  protected $counter = 0;
41 
47  protected $weeeCodeToItemMap;
48 
54  protected $weeeTotalExclTax;
55 
62 
66  protected $priceCurrency;
67 
72  public function __construct(
73  \Magento\Weee\Helper\Data $weeeData,
75  ) {
76  $this->priceCurrency = $priceCurrency;
77  $this->weeeData = $weeeData;
78  $this->setCode('weee');
79  $this->weeeCodeToItemMap = [];
80  }
81 
90  public function collect(
91  \Magento\Quote\Model\Quote $quote,
92  \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
93  \Magento\Quote\Model\Quote\Address\Total $total
94  ) {
95  AbstractTotal::collect($quote, $shippingAssignment, $total);
96  $this->_store = $quote->getStore();
97  if (!$this->weeeData->isEnabled($this->_store)) {
98  return $this;
99  }
100 
101  $address = $shippingAssignment->getShipping()->getAddress();
102  $items = $shippingAssignment->getItems();
103  if (!count($items)) {
104  return $this;
105  }
106 
107  $this->weeeTotalExclTax = 0;
108  $this->weeeBaseTotalExclTax = 0;
109  foreach ($items as $item) {
110  if ($item->getParentItem()) {
111  continue;
112  }
113  $this->resetItemData($item);
114  if ($item->getHasChildren() && $item->isChildrenCalculated()) {
115  foreach ($item->getChildren() as $child) {
116  $this->resetItemData($child);
117  $this->process($address, $total, $child);
118  }
119  $this->recalculateParent($item);
120  } else {
121  $this->process($address, $total, $item);
122  }
123  }
124  $total->setWeeeCodeToItemMap($this->weeeCodeToItemMap);
125  $total->setWeeeTotalExclTax($this->weeeTotalExclTax);
126  $total->setWeeeBaseTotalExclTax($this->weeeBaseTotalExclTax);
127  return $this;
128  }
129 
140  protected function process(
141  \Magento\Quote\Model\Quote\Address $address,
142  \Magento\Quote\Model\Quote\Address\Total $total,
143  $item
144  ) {
145  $attributes = $this->weeeData->getProductWeeeAttributes(
146  $item->getProduct(),
147  $address,
148  $address->getQuote()->getBillingAddress(),
149  $this->_store->getWebsiteId()
150  );
151 
152  $productTaxes = [];
153 
154  $totalValueInclTax = 0;
155  $baseTotalValueInclTax = 0;
156  $totalRowValueInclTax = 0;
157  $baseTotalRowValueInclTax = 0;
158 
159  $totalValueExclTax = 0;
160  $baseTotalValueExclTax = 0;
161  $totalRowValueExclTax = 0;
162  $baseTotalRowValueExclTax = 0;
163 
164  $associatedTaxables = $item->getAssociatedTaxables();
165  if (!$associatedTaxables) {
166  $associatedTaxables = [];
167  } else {
168  // remove existing weee associated taxables
169  foreach ($associatedTaxables as $iTaxable => $taxable) {
170  if ($taxable[CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_TYPE] == self::ITEM_TYPE) {
171  unset($associatedTaxables[$iTaxable]);
172  }
173  }
174  }
175 
176  foreach ($attributes as $key => $attribute) {
177  $title = $attribute->getName();
178 
179  $baseValueExclTax = $baseValueInclTax = $attribute->getAmount();
180  $valueExclTax = $valueInclTax = $this->priceCurrency->round(
181  $this->priceCurrency->convert($baseValueExclTax, $this->_store)
182  );
183 
184  $rowValueInclTax = $rowValueExclTax = $this->priceCurrency->round($valueInclTax * $item->getTotalQty());
185  $baseRowValueInclTax = $this->priceCurrency->round($baseValueInclTax * $item->getTotalQty());
186  $baseRowValueExclTax = $baseRowValueInclTax;
187 
188  $totalValueInclTax += $valueInclTax;
189  $baseTotalValueInclTax += $baseValueInclTax;
190  $totalRowValueInclTax += $rowValueInclTax;
191  $baseTotalRowValueInclTax += $baseRowValueInclTax;
192 
193  $totalValueExclTax += $valueExclTax;
194  $baseTotalValueExclTax += $baseValueExclTax;
195  $totalRowValueExclTax += $rowValueExclTax;
196  $baseTotalRowValueExclTax += $baseRowValueExclTax;
197 
198  $productTaxes[] = [
199  'title' => $title,
200  'base_amount' => $baseValueExclTax,
201  'amount' => $valueExclTax,
202  'row_amount' => $rowValueExclTax,
203  'base_row_amount' => $baseRowValueExclTax,
204  'base_amount_incl_tax' => $baseValueInclTax,
205  'amount_incl_tax' => $valueInclTax,
206  'row_amount_incl_tax' => $rowValueInclTax,
207  'base_row_amount_incl_tax' => $baseRowValueInclTax,
208  ];
209 
210  if ($this->weeeData->isTaxable($this->_store)) {
211  $weeeItemCode = self::ITEM_CODE_WEEE_PREFIX . $this->getNextIncrement();
212  $weeeItemCode .= '-' . $title;
213 
214  $associatedTaxables[] = [
220  CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_TAX_CLASS_ID => $item->getProduct()->getTaxClassId(),
221  ];
222  $this->weeeCodeToItemMap[$weeeItemCode] = $item;
223  }
224  }
225  $item->setAssociatedTaxables($associatedTaxables);
226 
227  $item->setWeeeTaxAppliedAmount($totalValueExclTax)
228  ->setBaseWeeeTaxAppliedAmount($baseTotalValueExclTax)
229  ->setWeeeTaxAppliedRowAmount($totalRowValueExclTax)
230  ->setBaseWeeeTaxAppliedRowAmnt($baseTotalRowValueExclTax);
231 
232  $item->setWeeeTaxAppliedAmountInclTax($totalValueInclTax)
233  ->setBaseWeeeTaxAppliedAmountInclTax($baseTotalValueInclTax)
234  ->setWeeeTaxAppliedRowAmountInclTax($totalRowValueInclTax)
235  ->setBaseWeeeTaxAppliedRowAmntInclTax($baseTotalRowValueInclTax);
236 
237  $this->processTotalAmount(
238  $total,
239  $totalRowValueExclTax,
240  $baseTotalRowValueExclTax,
241  $totalRowValueInclTax,
242  $baseTotalRowValueInclTax
243  );
244 
245  $this->weeeData->setApplied($item, array_merge($this->weeeData->getApplied($item), $productTaxes));
246  }
247 
258  protected function processTotalAmount(
259  $total,
260  $rowValueExclTax,
261  $baseRowValueExclTax,
262  $rowValueInclTax,
263  $baseRowValueInclTax
264  ) {
265  if (!$this->weeeData->isTaxable($this->_store)) {
266  //Accumulate the values. Will be used later in the 'weee tax' collector
267  $this->weeeTotalExclTax += $this->priceCurrency->round($rowValueExclTax);
268  $this->weeeBaseTotalExclTax += $this->priceCurrency->round($baseRowValueExclTax);
269  }
270 
271  //This value is used to calculate shipping cost; it will be overridden by tax collector
272  $total->setSubtotalInclTax(
273  $total->getSubtotalInclTax() + $this->priceCurrency->round($rowValueInclTax)
274  );
275  $total->setBaseSubtotalInclTax(
276  $total->getBaseSubtotalInclTax() + $this->priceCurrency->round($baseRowValueInclTax)
277  );
278  return $this;
279  }
280 
287  protected function getNextIncrement()
288  {
289  return ++$this->counter;
290  }
291 
300  protected function recalculateParent(\Magento\Quote\Model\Quote\Item\AbstractItem $item)
301  {
302  $associatedTaxables = [];
303  foreach ($item->getChildren() as $child) {
304  $associatedTaxables = array_merge($associatedTaxables, $child->getAssociatedTaxables());
305  }
306  $item->setAssociatedTaxables($associatedTaxables);
307  }
308 
315  protected function resetItemData($item)
316  {
317  $this->weeeData->setApplied($item, []);
318 
319  $item->setAssociatedTaxables([]);
320 
321  $item->setBaseWeeeTaxDisposition(0);
322  $item->setWeeeTaxDisposition(0);
323 
324  $item->setBaseWeeeTaxRowDisposition(0);
325  $item->setWeeeTaxRowDisposition(0);
326 
327  $item->setBaseWeeeTaxAppliedAmount(0);
328  $item->setBaseWeeeTaxAppliedRowAmnt(0);
329 
330  $item->setWeeeTaxAppliedAmount(0);
331  $item->setWeeeTaxAppliedRowAmount(0);
332  }
333 
340  public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
341  {
342  return null;
343  }
344 
356  {
357  return $config;
358  }
359 
366  public function getLabel()
367  {
368  return '';
369  }
370 }
$title
Definition: default.phtml:14
processConfigArray($config, $store)
Definition: Weee.php:355
collect(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
$config
Definition: fraud_order.php:17
fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
Definition: Weee.php:340
$quote
collect(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
Definition: Weee.php:90
recalculateParent(\Magento\Quote\Model\Quote\Item\AbstractItem $item)
Definition: Weee.php:300
$address
Definition: customer.php:38
processTotalAmount( $total, $rowValueExclTax, $baseRowValueExclTax, $rowValueInclTax, $baseRowValueInclTax)
Definition: Weee.php:258
$attributes
Definition: matrix.phtml:13
process(\Magento\Quote\Model\Quote\Address $address, \Magento\Quote\Model\Quote\Address\Total $total, $item)
Definition: Weee.php:140
__construct(\Magento\Weee\Helper\Data $weeeData, PriceCurrencyInterface $priceCurrency)
Definition: Weee.php:72
$items