Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WeeeTax.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class WeeeTax extends Weee
13 {
24  public function collect(
25  \Magento\Quote\Model\Quote $quote,
26  \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
27  \Magento\Quote\Model\Quote\Address\Total $total
28  ) {
29  \Magento\Quote\Model\Quote\Address\Total\AbstractTotal::collect($quote, $shippingAssignment, $total);
30  $this->_store = $quote->getStore();
31  if (!$this->weeeData->isEnabled($this->_store)) {
32  return $this;
33  }
34 
35  $items = $shippingAssignment->getItems();
36  if (!count($items)) {
37  return $this;
38  }
39 
40  //If Weee is not taxable, then the 'weee' collector has accumulated the non-taxable total values
41  if (!$this->weeeData->isTaxable($this->_store)) {
42  //Because Weee is not taxable: Weee excluding tax == Weee including tax
43  $weeeTotal = $total->getWeeeTotalExclTax();
44  $weeeBaseTotal = $total->getWeeeBaseTotalExclTax();
45 
46  //Add to appropriate 'subtotal' or 'weee' accumulators
47  $this->processTotalAmount($total, $weeeTotal, $weeeBaseTotal, $weeeTotal, $weeeBaseTotal);
48  return $this;
49  }
50 
51  $extraTaxableDetails = $total->getExtraTaxableDetails();
52 
53  if (isset($extraTaxableDetails[self::ITEM_TYPE])) {
54  //Get mapping from weeeCode to item
55  $weeeCodeToItemMap = $total->getWeeeCodeToItemMap();
56 
57  //Create mapping from item to weeeCode
58  $itemToWeeeCodeMap = $this->createItemToWeeeCodeMapping($weeeCodeToItemMap);
59 
60  //Create mapping from weeeCode to weeeTaxDetails
61  $weeeCodeToWeeeTaxDetailsMap = [];
62  foreach ($extraTaxableDetails[self::ITEM_TYPE] as $weeeAttributesTaxDetails) {
63  foreach ($weeeAttributesTaxDetails as $weeeTaxDetails) {
64  $weeeCode = $weeeTaxDetails['code'];
65  $weeeCodeToWeeeTaxDetailsMap[$weeeCode] = $weeeTaxDetails;
66  }
67  }
68 
69  //Process each item that has taxable weee
70  foreach ($itemToWeeeCodeMap as $mapping) {
71  $item = $mapping['item'];
72 
73  $this->weeeData->setApplied($item, []);
74  $productTaxes = [];
75 
76  $totalValueInclTax = 0;
77  $baseTotalValueInclTax = 0;
78  $totalRowValueInclTax = 0;
79  $baseTotalRowValueInclTax = 0;
80 
81  $totalValueExclTax = 0;
82  $baseTotalValueExclTax = 0;
83  $totalRowValueExclTax = 0;
84  $baseTotalRowValueExclTax = 0;
85 
86  //Process each taxed weee attribute of an item
87  foreach ($mapping['weeeCodes'] as $weeeCode) {
88  if (!array_key_exists($weeeCode, $weeeCodeToWeeeTaxDetailsMap)) {
89  //Need to ensure that everyone is in sync for which weee code to process
90  continue;
91  }
92  $weeeTaxDetails = $weeeCodeToWeeeTaxDetailsMap[$weeeCode];
93  $attributeCode = explode('-', $weeeCode)[1];
94 
95  $valueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_PRICE_EXCL_TAX];
96  $baseValueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_PRICE_EXCL_TAX];
97  $valueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_PRICE_INCL_TAX];
98  $baseValueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_PRICE_INCL_TAX];
99 
100  $rowValueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_ROW_TOTAL];
101  $baseRowValueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_ROW_TOTAL];
102  $rowValueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_ROW_TOTAL_INCL_TAX];
103  $baseRowValueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_ROW_TOTAL_INCL_TAX];
104 
105  $totalValueInclTax += $valueInclTax;
106  $baseTotalValueInclTax += $baseValueInclTax;
107  $totalRowValueInclTax += $rowValueInclTax;
108  $baseTotalRowValueInclTax += $baseRowValueInclTax;
109 
110  $totalValueExclTax += $valueExclTax;
111  $baseTotalValueExclTax += $baseValueExclTax;
112  $totalRowValueExclTax += $rowValueExclTax;
113  $baseTotalRowValueExclTax += $baseRowValueExclTax;
114 
115  $productTaxes[] = [
116  'title' => $attributeCode, //TODO: fix this
117  'base_amount' => $baseValueExclTax,
118  'amount' => $valueExclTax,
119  'row_amount' => $rowValueExclTax,
120  'base_row_amount' => $baseRowValueExclTax,
121  'base_amount_incl_tax' => $baseValueInclTax,
122  'amount_incl_tax' => $valueInclTax,
123  'row_amount_incl_tax' => $rowValueInclTax,
124  'base_row_amount_incl_tax' => $baseRowValueInclTax,
125  ];
126  }
127 
128  $item->setWeeeTaxAppliedAmount($totalValueExclTax)
129  ->setBaseWeeeTaxAppliedAmount($baseTotalValueExclTax)
130  ->setWeeeTaxAppliedRowAmount($totalRowValueExclTax)
131  ->setBaseWeeeTaxAppliedRowAmnt($baseTotalRowValueExclTax);
132 
133  $item->setWeeeTaxAppliedAmountInclTax($totalValueInclTax)
134  ->setBaseWeeeTaxAppliedAmountInclTax($baseTotalValueInclTax)
135  ->setWeeeTaxAppliedRowAmountInclTax($totalRowValueInclTax)
136  ->setBaseWeeeTaxAppliedRowAmntInclTax($baseTotalRowValueInclTax);
137 
138  $this->processTotalAmount(
139  $total,
140  $totalRowValueExclTax,
141  $baseTotalRowValueExclTax,
142  $totalRowValueInclTax,
143  $baseTotalRowValueInclTax
144  );
145 
146  $this->weeeData->setApplied($item, array_merge($this->weeeData->getApplied($item), $productTaxes));
147  }
148  }
149  return $this;
150  }
151 
181  {
182  $itemToCodeMap = [];
183  foreach ($weeeCodeToItemMap as $weeeCode => $item) {
184  $key = spl_object_hash($item); // note: $item->getItemId() can be null
185  if (!array_key_exists($key, $itemToCodeMap)) {
186  //Create the initial structure for this item
187  $itemToCodeMap[$key] = ['item' => $item, 'weeeCodes' => [$weeeCode]];
188  } else {
189  //Append the weeeCode to the existing structure
190  $itemToCodeMap[$key]['weeeCodes'][] = $weeeCode;
191  }
192  }
193  return $itemToCodeMap;
194  }
195 
206  protected function processTotalAmount(
207  $total,
208  $rowValueExclTax,
209  $baseRowValueExclTax,
210  $rowValueInclTax,
211  $baseRowValueInclTax
212  ) {
213  if ($this->weeeData->includeInSubtotal($this->_store)) {
214  $total->addTotalAmount('subtotal', $rowValueExclTax);
215  $total->addBaseTotalAmount('subtotal', $baseRowValueExclTax);
216  } else {
217  $total->addTotalAmount('weee', $rowValueExclTax);
218  $total->addBaseTotalAmount('weee', $baseRowValueExclTax);
219  }
220 
221  $total->setSubtotalInclTax($total->getSubtotalInclTax() + $rowValueInclTax);
222  $total->setBaseSubtotalInclTax($total->getBaseSubtotalInclTax() + $baseRowValueInclTax);
223  return $this;
224  }
225 
233  public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
234  {
236  $items = isset($total['address_quote_items']) ? $total['address_quote_items'] : [];
237 
238  $weeeTotal = $this->weeeData->getTotalAmounts($items, $quote->getStore());
239  if ($weeeTotal) {
240  return [
241  'code' => $this->getCode(),
242  'title' => __('FPT'),
243  'value' => $weeeTotal,
244  'area' => null,
245  ];
246  }
247  return null;
248  }
249 }
fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
Definition: Weee.php:340
$quote
__()
Definition: __.php:13
createItemToWeeeCodeMapping($weeeCodeToItemMap)
Definition: WeeeTax.php:180
$attributeCode
Definition: extend.phtml:12
processTotalAmount( $total, $rowValueExclTax, $baseRowValueExclTax, $rowValueInclTax, $baseRowValueInclTax)
Definition: WeeeTax.php:206
collect(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
Definition: WeeeTax.php:24
$items