WeeeTax.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Model\Total\Quote;
  7. use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
  8. use Magento\Store\Model\Store;
  9. use Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector;
  10. class WeeeTax extends Weee
  11. {
  12. /**
  13. * Collect Weee taxes amount and prepare items prices for taxation and discount
  14. *
  15. * @param \Magento\Quote\Model\Quote $quote
  16. * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface|\Magento\Quote\Model\Quote\Address $shippingAssignment
  17. * @param \Magento\Quote\Model\Quote\Address\Total $total
  18. * @return $this
  19. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  20. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  21. */
  22. public function collect(
  23. \Magento\Quote\Model\Quote $quote,
  24. \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
  25. \Magento\Quote\Model\Quote\Address\Total $total
  26. ) {
  27. \Magento\Quote\Model\Quote\Address\Total\AbstractTotal::collect($quote, $shippingAssignment, $total);
  28. $this->_store = $quote->getStore();
  29. if (!$this->weeeData->isEnabled($this->_store)) {
  30. return $this;
  31. }
  32. $items = $shippingAssignment->getItems();
  33. if (!count($items)) {
  34. return $this;
  35. }
  36. //If Weee is not taxable, then the 'weee' collector has accumulated the non-taxable total values
  37. if (!$this->weeeData->isTaxable($this->_store)) {
  38. //Because Weee is not taxable: Weee excluding tax == Weee including tax
  39. $weeeTotal = $total->getWeeeTotalExclTax();
  40. $weeeBaseTotal = $total->getWeeeBaseTotalExclTax();
  41. //Add to appropriate 'subtotal' or 'weee' accumulators
  42. $this->processTotalAmount($total, $weeeTotal, $weeeBaseTotal, $weeeTotal, $weeeBaseTotal);
  43. return $this;
  44. }
  45. $extraTaxableDetails = $total->getExtraTaxableDetails();
  46. if (isset($extraTaxableDetails[self::ITEM_TYPE])) {
  47. //Get mapping from weeeCode to item
  48. $weeeCodeToItemMap = $total->getWeeeCodeToItemMap();
  49. //Create mapping from item to weeeCode
  50. $itemToWeeeCodeMap = $this->createItemToWeeeCodeMapping($weeeCodeToItemMap);
  51. //Create mapping from weeeCode to weeeTaxDetails
  52. $weeeCodeToWeeeTaxDetailsMap = [];
  53. foreach ($extraTaxableDetails[self::ITEM_TYPE] as $weeeAttributesTaxDetails) {
  54. foreach ($weeeAttributesTaxDetails as $weeeTaxDetails) {
  55. $weeeCode = $weeeTaxDetails['code'];
  56. $weeeCodeToWeeeTaxDetailsMap[$weeeCode] = $weeeTaxDetails;
  57. }
  58. }
  59. //Process each item that has taxable weee
  60. foreach ($itemToWeeeCodeMap as $mapping) {
  61. $item = $mapping['item'];
  62. $this->weeeData->setApplied($item, []);
  63. $productTaxes = [];
  64. $totalValueInclTax = 0;
  65. $baseTotalValueInclTax = 0;
  66. $totalRowValueInclTax = 0;
  67. $baseTotalRowValueInclTax = 0;
  68. $totalValueExclTax = 0;
  69. $baseTotalValueExclTax = 0;
  70. $totalRowValueExclTax = 0;
  71. $baseTotalRowValueExclTax = 0;
  72. //Process each taxed weee attribute of an item
  73. foreach ($mapping['weeeCodes'] as $weeeCode) {
  74. if (!array_key_exists($weeeCode, $weeeCodeToWeeeTaxDetailsMap)) {
  75. //Need to ensure that everyone is in sync for which weee code to process
  76. continue;
  77. }
  78. $weeeTaxDetails = $weeeCodeToWeeeTaxDetailsMap[$weeeCode];
  79. $attributeCode = explode('-', $weeeCode)[1];
  80. $valueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_PRICE_EXCL_TAX];
  81. $baseValueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_PRICE_EXCL_TAX];
  82. $valueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_PRICE_INCL_TAX];
  83. $baseValueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_PRICE_INCL_TAX];
  84. $rowValueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_ROW_TOTAL];
  85. $baseRowValueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_ROW_TOTAL];
  86. $rowValueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_ROW_TOTAL_INCL_TAX];
  87. $baseRowValueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_ROW_TOTAL_INCL_TAX];
  88. $totalValueInclTax += $valueInclTax;
  89. $baseTotalValueInclTax += $baseValueInclTax;
  90. $totalRowValueInclTax += $rowValueInclTax;
  91. $baseTotalRowValueInclTax += $baseRowValueInclTax;
  92. $totalValueExclTax += $valueExclTax;
  93. $baseTotalValueExclTax += $baseValueExclTax;
  94. $totalRowValueExclTax += $rowValueExclTax;
  95. $baseTotalRowValueExclTax += $baseRowValueExclTax;
  96. $productTaxes[] = [
  97. 'title' => $attributeCode, //TODO: fix this
  98. 'base_amount' => $baseValueExclTax,
  99. 'amount' => $valueExclTax,
  100. 'row_amount' => $rowValueExclTax,
  101. 'base_row_amount' => $baseRowValueExclTax,
  102. 'base_amount_incl_tax' => $baseValueInclTax,
  103. 'amount_incl_tax' => $valueInclTax,
  104. 'row_amount_incl_tax' => $rowValueInclTax,
  105. 'base_row_amount_incl_tax' => $baseRowValueInclTax,
  106. ];
  107. }
  108. $item->setWeeeTaxAppliedAmount($totalValueExclTax)
  109. ->setBaseWeeeTaxAppliedAmount($baseTotalValueExclTax)
  110. ->setWeeeTaxAppliedRowAmount($totalRowValueExclTax)
  111. ->setBaseWeeeTaxAppliedRowAmnt($baseTotalRowValueExclTax);
  112. $item->setWeeeTaxAppliedAmountInclTax($totalValueInclTax)
  113. ->setBaseWeeeTaxAppliedAmountInclTax($baseTotalValueInclTax)
  114. ->setWeeeTaxAppliedRowAmountInclTax($totalRowValueInclTax)
  115. ->setBaseWeeeTaxAppliedRowAmntInclTax($baseTotalRowValueInclTax);
  116. $this->processTotalAmount(
  117. $total,
  118. $totalRowValueExclTax,
  119. $baseTotalRowValueExclTax,
  120. $totalRowValueInclTax,
  121. $baseTotalRowValueInclTax
  122. );
  123. $this->weeeData->setApplied($item, array_merge($this->weeeData->getApplied($item), $productTaxes));
  124. }
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Given a mapping from a weeeCode to an item, create a mapping from the item to the list of weeeCodes.
  130. *
  131. * Example of input:
  132. * [
  133. * "weeeCode1" -> item1,
  134. * "weeeCode2" -> item1,
  135. * ...
  136. * "weeeCodeX" -> item22,
  137. * "weeeCodeY" -> item22,
  138. * ...
  139. * ]
  140. *
  141. * Example of output:
  142. * [
  143. * item1Id -> [ "item" -> item1,
  144. * "weeeCodes" -> [weeeCode1, weeeCode2, ...]
  145. * ],
  146. * ...
  147. * item22Id -> [ "item" -> item22,
  148. * "weeeCodes" -> [weeeCodeX, weeeCodeY, ...]
  149. * ],
  150. * ...
  151. * ]
  152. *
  153. * @param array $weeeCodeToItemMap
  154. * @return array
  155. */
  156. protected function createItemToWeeeCodeMapping($weeeCodeToItemMap)
  157. {
  158. $itemToCodeMap = [];
  159. foreach ($weeeCodeToItemMap as $weeeCode => $item) {
  160. $key = spl_object_hash($item); // note: $item->getItemId() can be null
  161. if (!array_key_exists($key, $itemToCodeMap)) {
  162. //Create the initial structure for this item
  163. $itemToCodeMap[$key] = ['item' => $item, 'weeeCodes' => [$weeeCode]];
  164. } else {
  165. //Append the weeeCode to the existing structure
  166. $itemToCodeMap[$key]['weeeCodes'][] = $weeeCode;
  167. }
  168. }
  169. return $itemToCodeMap;
  170. }
  171. /**
  172. * Process row amount based on FPT total amount configuration setting
  173. *
  174. * @param \Magento\Quote\Model\Quote\Address\Total $total
  175. * @param float $rowValueExclTax
  176. * @param float $baseRowValueExclTax
  177. * @param float $rowValueInclTax
  178. * @param float $baseRowValueInclTax
  179. * @return $this
  180. */
  181. protected function processTotalAmount(
  182. $total,
  183. $rowValueExclTax,
  184. $baseRowValueExclTax,
  185. $rowValueInclTax,
  186. $baseRowValueInclTax
  187. ) {
  188. if ($this->weeeData->includeInSubtotal($this->_store)) {
  189. $total->addTotalAmount('subtotal', $rowValueExclTax);
  190. $total->addBaseTotalAmount('subtotal', $baseRowValueExclTax);
  191. } else {
  192. $total->addTotalAmount('weee', $rowValueExclTax);
  193. $total->addBaseTotalAmount('weee', $baseRowValueExclTax);
  194. }
  195. $total->setSubtotalInclTax($total->getSubtotalInclTax() + $rowValueInclTax);
  196. $total->setBaseSubtotalInclTax($total->getBaseSubtotalInclTax() + $baseRowValueInclTax);
  197. return $this;
  198. }
  199. /**
  200. * Fetch the Weee total amount for display in totals block when building the initial quote
  201. *
  202. * @param \Magento\Quote\Model\Quote $quote
  203. * @param \Magento\Quote\Model\Quote\Address\Total $total
  204. * @return array
  205. */
  206. public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
  207. {
  208. /** @var $items \Magento\Sales\Model\Order\Item[] */
  209. $items = isset($total['address_quote_items']) ? $total['address_quote_items'] : [];
  210. $weeeTotal = $this->weeeData->getTotalAmounts($items, $quote->getStore());
  211. if ($weeeTotal) {
  212. return [
  213. 'code' => $this->getCode(),
  214. 'title' => __('FPT'),
  215. 'value' => $weeeTotal,
  216. 'area' => null,
  217. ];
  218. }
  219. return null;
  220. }
  221. }