Data.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Helper;
  7. use Magento\Sales\Model\Order\Item as OrderItem;
  8. use Magento\Quote\Model\Quote\Item\AbstractItem as QuoteAbstractItem;
  9. use Magento\Store\Model\Store;
  10. use Magento\Store\Model\Website;
  11. use Magento\Weee\Model\Tax as WeeeDisplayConfig;
  12. /**
  13. * WEEE data helper
  14. *
  15. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. * @api
  18. * @since 100.0.2
  19. */
  20. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  21. {
  22. /**#@+
  23. * Constants defined for keys of array, makes typos less likely
  24. */
  25. const KEY_WEEE_AMOUNT_INVOICED = 'weee_amount_invoiced';
  26. const KEY_BASE_WEEE_AMOUNT_INVOICED = 'base_weee_amount_invoiced';
  27. const KEY_WEEE_TAX_AMOUNT_INVOICED = 'weee_tax_amount_invoiced';
  28. const KEY_BASE_WEEE_TAX_AMOUNT_INVOICED = 'base_weee_tax_amount_invoiced';
  29. const KEY_WEEE_AMOUNT_REFUNDED = 'weee_amount_refunded';
  30. const KEY_BASE_WEEE_AMOUNT_REFUNDED = 'base_weee_amount_refunded';
  31. const KEY_WEEE_TAX_AMOUNT_REFUNDED = 'weee_tax_amount_refunded';
  32. const KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED = 'base_weee_tax_amount_refunded';
  33. /**#@-*/
  34. /**#@-*/
  35. protected $_storeDisplayConfig = [];
  36. /**
  37. * Core registry
  38. *
  39. * @var \Magento\Framework\Registry
  40. */
  41. protected $_coreRegistry;
  42. /**
  43. * Tax data
  44. *
  45. * @var \Magento\Tax\Helper\Data
  46. */
  47. protected $_taxData;
  48. /**
  49. * @var \Magento\Weee\Model\Tax
  50. */
  51. protected $_weeeTax;
  52. /**
  53. * @var \Magento\Weee\Model\Config
  54. */
  55. protected $_weeeConfig;
  56. /**
  57. * @var \Magento\Store\Model\StoreManagerInterface
  58. */
  59. protected $_storeManager;
  60. /**
  61. * @var string
  62. */
  63. protected $cacheProductWeeeAmount = '_cache_product_weee_amount';
  64. /**
  65. * @var \Magento\Framework\Serialize\Serializer\Json
  66. */
  67. private $serializer;
  68. /**
  69. * Data constructor.
  70. *
  71. * @param \Magento\Framework\App\Helper\Context $context
  72. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  73. * @param \Magento\Weee\Model\Tax $weeeTax
  74. * @param \Magento\Weee\Model\Config $weeeConfig
  75. * @param \Magento\Tax\Helper\Data $taxData
  76. * @param \Magento\Framework\Registry $coreRegistry
  77. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  78. * @throws \RuntimeException
  79. */
  80. public function __construct(
  81. \Magento\Framework\App\Helper\Context $context,
  82. \Magento\Store\Model\StoreManagerInterface $storeManager,
  83. \Magento\Weee\Model\Tax $weeeTax,
  84. \Magento\Weee\Model\Config $weeeConfig,
  85. \Magento\Tax\Helper\Data $taxData,
  86. \Magento\Framework\Registry $coreRegistry,
  87. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  88. ) {
  89. $this->_storeManager = $storeManager;
  90. $this->_weeeTax = $weeeTax;
  91. $this->_coreRegistry = $coreRegistry;
  92. $this->_taxData = $taxData;
  93. $this->_weeeConfig = $weeeConfig;
  94. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  95. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  96. parent::__construct($context);
  97. }
  98. /**
  99. * Get weee amount display type on product view page
  100. *
  101. * @param null|string|bool|int|Store $store
  102. * @return int
  103. */
  104. public function getPriceDisplayType($store = null)
  105. {
  106. return $this->_weeeConfig->getPriceDisplayType($store);
  107. }
  108. /**
  109. * Get weee amount display type on product list page
  110. *
  111. * @param null|string|bool|int|Store $store
  112. * @return int
  113. */
  114. public function getListPriceDisplayType($store = null)
  115. {
  116. return $this->_weeeConfig->getListPriceDisplayType($store);
  117. }
  118. /**
  119. * Get weee amount display type in sales modules
  120. *
  121. * @param null|string|bool|int|Store $store
  122. * @return int
  123. */
  124. public function getSalesPriceDisplayType($store = null)
  125. {
  126. return $this->_weeeConfig->getSalesPriceDisplayType($store);
  127. }
  128. /**
  129. * Get weee amount display type in email templates
  130. *
  131. * @param null|string|bool|int|Store $store
  132. * @return int
  133. */
  134. public function getEmailPriceDisplayType($store = null)
  135. {
  136. return $this->_weeeConfig->getEmailPriceDisplayType($store);
  137. }
  138. /**
  139. * Check if weee tax amount should be taxable
  140. *
  141. * @param null|string|bool|int|Store $store
  142. * @return bool
  143. */
  144. public function isTaxable($store = null)
  145. {
  146. return $this->_weeeConfig->isTaxable($store);
  147. }
  148. /**
  149. * Check if weee tax amount should be included to subtotal
  150. *
  151. * @param null|string|bool|int|Store $store
  152. * @return bool
  153. */
  154. public function includeInSubtotal($store = null)
  155. {
  156. return $this->_weeeConfig->includeInSubtotal($store);
  157. }
  158. /**
  159. * Check if fixed taxes are used in system
  160. *
  161. * @param null|string|bool|int|Store $store
  162. * @return bool
  163. */
  164. public function isEnabled($store = null)
  165. {
  166. return $this->_weeeConfig->isEnabled($store);
  167. }
  168. /**
  169. * Check if the FPT totals line(s) should be displayed with tax included
  170. *
  171. * @param null|string|bool|int|Store $store
  172. * @return bool
  173. */
  174. public function displayTotalsInclTax($store = null)
  175. {
  176. // If catalog prices include tax, then display FPT totals with tax included
  177. return $this->_taxData->priceIncludesTax($store);
  178. }
  179. /**
  180. * Get weee tax amount for product based on website
  181. *
  182. * @param \Magento\Catalog\Model\Product $product
  183. * @param mixed $website
  184. * @return float
  185. */
  186. public function getAmountExclTax($product, $website = null)
  187. {
  188. if (!$product->hasData($this->cacheProductWeeeAmount)) {
  189. /** @var \Magento\Store\Model\Store $store */
  190. if ($website) {
  191. $store = $this->_storeManager->getWebsite($website)->getDefaultGroup()->getDefaultStore();
  192. } else {
  193. $store = $product->getStore();
  194. }
  195. $amount = 0;
  196. if ($this->isEnabled($store)) {
  197. $amount = $this->_weeeTax->getWeeeAmountExclTax($product, null, null, $website);
  198. }
  199. $product->setData($this->cacheProductWeeeAmount, $amount);
  200. }
  201. return $product->getData($this->cacheProductWeeeAmount);
  202. }
  203. /**
  204. * Returns display type for price accordingly to current zone
  205. *
  206. * @param int|int[]|null $compareTo
  207. * @param string $zone
  208. * @param Store|int|string $store
  209. * @return bool|int
  210. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  211. */
  212. public function typeOfDisplay(
  213. $compareTo = null,
  214. $zone = \Magento\Framework\Pricing\Render::ZONE_DEFAULT,
  215. $store = null
  216. ) {
  217. if (!$this->isEnabled($store)) {
  218. return false;
  219. }
  220. switch ($zone) {
  221. case \Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW:
  222. $type = $this->getPriceDisplayType($store);
  223. break;
  224. case \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST:
  225. $type = $this->getListPriceDisplayType($store);
  226. break;
  227. case \Magento\Framework\Pricing\Render::ZONE_SALES:
  228. case \Magento\Framework\Pricing\Render::ZONE_CART:
  229. $type = $this->getSalesPriceDisplayType($store);
  230. break;
  231. case \Magento\Framework\Pricing\Render::ZONE_EMAIL:
  232. $type = $this->getEmailPriceDisplayType($store);
  233. break;
  234. default:
  235. if ($this->_coreRegistry->registry('current_product')) {
  236. $type = $this->getPriceDisplayType($store);
  237. } else {
  238. $type = $this->getListPriceDisplayType($store);
  239. }
  240. break;
  241. }
  242. if ($compareTo === null) {
  243. return $type;
  244. } else {
  245. if (is_array($compareTo)) {
  246. return in_array($type, $compareTo);
  247. } else {
  248. return $type == $compareTo;
  249. }
  250. }
  251. }
  252. /**
  253. * Proxy for \Magento\Weee\Model\Tax::getProductWeeeAttributes()
  254. *
  255. * @param \Magento\Catalog\Model\Product $product
  256. * @param null|false|\Magento\Framework\DataObject $shipping
  257. * @param null|false|\Magento\Framework\DataObject $billing
  258. * @param Website $website
  259. * @param bool $calculateTaxes
  260. * @param bool $round
  261. * @return \Magento\Framework\DataObject[]
  262. */
  263. public function getProductWeeeAttributes(
  264. $product,
  265. $shipping = null,
  266. $billing = null,
  267. $website = null,
  268. $calculateTaxes = false,
  269. $round = true
  270. ) {
  271. return $this->_weeeTax->getProductWeeeAttributes(
  272. $product,
  273. $shipping,
  274. $billing,
  275. $website,
  276. $calculateTaxes,
  277. $round
  278. );
  279. }
  280. /**
  281. * Returns applied weee tax amount
  282. *
  283. * @param QuoteAbstractItem $item
  284. * @return float
  285. */
  286. public function getWeeeTaxAppliedAmount($item)
  287. {
  288. return $this->getRecursiveNumericAmount($item, 'getWeeeTaxAppliedAmount');
  289. }
  290. /**
  291. * Returns applied weee tax amount for the row
  292. *
  293. * @param QuoteAbstractItem $item
  294. * @return float
  295. */
  296. public function getWeeeTaxAppliedRowAmount($item)
  297. {
  298. return $this->getRecursiveNumericAmount($item, 'getWeeeTaxAppliedRowAmount');
  299. }
  300. /**
  301. * Returns applied base weee tax amount for the row
  302. *
  303. * @param QuoteAbstractItem|OrderItem $item
  304. * @return float
  305. * @since 100.2.0
  306. */
  307. public function getBaseWeeeTaxAppliedRowAmount($item)
  308. {
  309. return $this->getRecursiveNumericAmount($item, 'getBaseWeeeTaxAppliedRowAmnt');
  310. }
  311. /**
  312. * Returns accumulated amounts for the item
  313. *
  314. * @param QuoteAbstractItem|OrderItem $item
  315. * @param string $functionName
  316. * @return float
  317. */
  318. protected function getRecursiveNumericAmount($item, $functionName)
  319. {
  320. if ($item instanceof QuoteAbstractItem || $item instanceof OrderItem) {
  321. if ($item->getHasChildren() && $item->isChildrenCalculated()) {
  322. $result = 0;
  323. $children = $item instanceof OrderItem ? $item->getChildrenItems() : $item->getChildren();
  324. foreach ($children as $child) {
  325. $childData = $this->getRecursiveNumericAmount($child, $functionName);
  326. if (!empty($childData)) {
  327. $result += $childData;
  328. }
  329. }
  330. return $result;
  331. }
  332. }
  333. $data = $item->$functionName();
  334. if (empty($data)) {
  335. return 0;
  336. }
  337. return $data;
  338. }
  339. /**
  340. * Returns applied weee taxes
  341. *
  342. * @param QuoteAbstractItem $item
  343. * @return array
  344. */
  345. public function getApplied($item)
  346. {
  347. if ($item instanceof QuoteAbstractItem) {
  348. if ($item->getHasChildren() && $item->isChildrenCalculated()) {
  349. $result = [];
  350. foreach ($item->getChildren() as $child) {
  351. $childData = $this->getApplied($child);
  352. if (is_array($childData)) {
  353. $result = array_merge($result, $childData);
  354. }
  355. }
  356. return $result;
  357. }
  358. }
  359. // if order item data is old enough then weee_tax_applied might not be valid
  360. $data = $item->getWeeeTaxApplied();
  361. if (empty($data)) {
  362. return [];
  363. }
  364. return $this->serializer->unserialize($item->getWeeeTaxApplied());
  365. }
  366. /**
  367. * Sets applied weee taxes
  368. *
  369. * @param QuoteAbstractItem $item
  370. * @param array $value
  371. * @return $this
  372. */
  373. public function setApplied($item, $value)
  374. {
  375. $item->setWeeeTaxApplied($this->serializer->serialize($value));
  376. return $this;
  377. }
  378. /**
  379. * Returns array of weee attributes allowed for display
  380. *
  381. * @param \Magento\Catalog\Model\Product $product
  382. * @return \Magento\Framework\DataObject[]
  383. */
  384. public function getProductWeeeAttributesForDisplay($product)
  385. {
  386. /** @var \Magento\Store\Model\Store $store */
  387. $store = $product->getStore();
  388. if ($this->isEnabled($store)) {
  389. $calculateTax = ($this->typeOfDisplay(1) || $this->typeOfDisplay(2)) ? 1 : 0;
  390. return $this->getProductWeeeAttributes($product, null, null, null, $calculateTax, false);
  391. }
  392. return [];
  393. }
  394. /**
  395. * Get Product Weee attributes for price renderer
  396. *
  397. * @param \Magento\Catalog\Model\Product $product
  398. * @param null|false|\Magento\Framework\DataObject $shipping Shipping Address
  399. * @param null|false|\Magento\Framework\DataObject $billing Billing Address
  400. * @param null|Website $website
  401. * @param bool $calculateTaxes
  402. * @return \Magento\Framework\DataObject[]
  403. */
  404. public function getProductWeeeAttributesForRenderer(
  405. $product,
  406. $shipping = null,
  407. $billing = null,
  408. $website = null,
  409. $calculateTaxes = false
  410. ) {
  411. /** @var \Magento\Store\Model\Store $store */
  412. if ($website) {
  413. $store = $this->_storeManager->getWebsite($website)->getDefaultGroup()->getDefaultStore();
  414. } else {
  415. $store = $product->getStore();
  416. }
  417. if ($this->isEnabled($store)) {
  418. return $this->getProductWeeeAttributes(
  419. $product,
  420. $shipping,
  421. $billing,
  422. $website,
  423. $calculateTaxes ? $calculateTaxes : $this->typeOfDisplay(1)
  424. );
  425. }
  426. return [];
  427. }
  428. /**
  429. * Get the weee tax including tax
  430. *
  431. * @param QuoteAbstractItem $item
  432. * @return float
  433. */
  434. public function getWeeeTaxInclTax($item)
  435. {
  436. $weeeTaxAppliedAmounts = $this->getApplied($item);
  437. $totalWeeeTaxIncTaxApplied = 0;
  438. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  439. $totalWeeeTaxIncTaxApplied += max($weeeTaxAppliedAmount['amount_incl_tax'], 0);
  440. }
  441. return $totalWeeeTaxIncTaxApplied;
  442. }
  443. /**
  444. * Get the total base weee tax
  445. *
  446. * @param QuoteAbstractItem $item
  447. * @return float
  448. */
  449. public function getBaseWeeeTaxInclTax($item)
  450. {
  451. $weeeTaxAppliedAmounts = $this->getApplied($item);
  452. $totalBaseWeeeTaxIncTaxApplied = 0;
  453. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  454. $totalBaseWeeeTaxIncTaxApplied += max($weeeTaxAppliedAmount['base_amount_incl_tax'], 0);
  455. }
  456. return $totalBaseWeeeTaxIncTaxApplied;
  457. }
  458. /**
  459. * Get the total weee including tax by row
  460. *
  461. * @param QuoteAbstractItem $item
  462. * @return float
  463. */
  464. public function getRowWeeeTaxInclTax($item)
  465. {
  466. $weeeTaxAppliedAmounts = $this->getApplied($item);
  467. $totalWeeeTaxIncTaxApplied = 0;
  468. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  469. $totalWeeeTaxIncTaxApplied += max($weeeTaxAppliedAmount['row_amount_incl_tax'], 0);
  470. }
  471. return $totalWeeeTaxIncTaxApplied;
  472. }
  473. /**
  474. * Get the total base weee including tax by row
  475. *
  476. * @param QuoteAbstractItem $item
  477. * @return float
  478. */
  479. public function getBaseRowWeeeTaxInclTax($item)
  480. {
  481. $weeeTaxAppliedAmounts = $this->getApplied($item);
  482. $totalWeeeTaxIncTaxApplied = 0;
  483. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  484. $totalWeeeTaxIncTaxApplied += max($weeeTaxAppliedAmount['base_row_amount_incl_tax'], 0);
  485. }
  486. return $totalWeeeTaxIncTaxApplied;
  487. }
  488. /**
  489. * Get the total tax applied on weee by unit
  490. *
  491. * @param QuoteAbstractItem $item
  492. * @return float
  493. */
  494. public function getTotalTaxAppliedForWeeeTax($item)
  495. {
  496. $weeeTaxAppliedAmounts = $this->getApplied($item);
  497. $totalTaxForWeeeTax = 0;
  498. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  499. $totalTaxForWeeeTax += max(
  500. $weeeTaxAppliedAmount['amount_incl_tax']
  501. - $weeeTaxAppliedAmount['amount'],
  502. 0
  503. );
  504. }
  505. return $totalTaxForWeeeTax;
  506. }
  507. /**
  508. * Get the total tax applied on weee by unit
  509. *
  510. * @param QuoteAbstractItem $item
  511. * @return float
  512. */
  513. public function getBaseTotalTaxAppliedForWeeeTax($item)
  514. {
  515. $weeeTaxAppliedAmounts = $this->getApplied($item);
  516. $totalTaxForWeeeTax = 0;
  517. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  518. $totalTaxForWeeeTax += max(
  519. $weeeTaxAppliedAmount['base_amount_incl_tax']
  520. - $weeeTaxAppliedAmount['base_amount'],
  521. 0
  522. );
  523. }
  524. return $totalTaxForWeeeTax;
  525. }
  526. /**
  527. * @param OrderItem $orderItem
  528. * @return float
  529. */
  530. public function getWeeeAmountInvoiced($orderItem)
  531. {
  532. $weeeTaxAppliedAmounts = $this->getApplied($orderItem);
  533. $totalAmountInvoiced = 0;
  534. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  535. if (isset($weeeTaxAppliedAmount[self::KEY_WEEE_AMOUNT_INVOICED])) {
  536. $totalAmountInvoiced = $weeeTaxAppliedAmount[self::KEY_WEEE_AMOUNT_INVOICED];
  537. break;
  538. }
  539. }
  540. return $totalAmountInvoiced;
  541. }
  542. /**
  543. * @param OrderItem $orderItem
  544. * @return float
  545. */
  546. public function getBaseWeeeAmountInvoiced($orderItem)
  547. {
  548. $weeeTaxAppliedAmounts = $this->getApplied($orderItem);
  549. $baseTotalAmountInvoiced = 0;
  550. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  551. if (isset($weeeTaxAppliedAmount[self::KEY_BASE_WEEE_AMOUNT_INVOICED])) {
  552. $baseTotalAmountInvoiced = $weeeTaxAppliedAmount[self::KEY_BASE_WEEE_AMOUNT_INVOICED];
  553. break;
  554. }
  555. }
  556. return $baseTotalAmountInvoiced;
  557. }
  558. /**
  559. * @param OrderItem $orderItem
  560. * @return float
  561. */
  562. public function getWeeeTaxAmountInvoiced($orderItem)
  563. {
  564. $weeeTaxAppliedAmounts = $this->getApplied($orderItem);
  565. $totalTaxInvoiced = 0;
  566. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  567. if (isset($weeeTaxAppliedAmount[self::KEY_WEEE_TAX_AMOUNT_INVOICED])) {
  568. $totalTaxInvoiced = $weeeTaxAppliedAmount[self::KEY_WEEE_TAX_AMOUNT_INVOICED];
  569. break;
  570. }
  571. }
  572. return $totalTaxInvoiced;
  573. }
  574. /**
  575. * @param OrderItem $orderItem
  576. * @return float
  577. */
  578. public function getBaseWeeeTaxAmountInvoiced($orderItem)
  579. {
  580. $weeeTaxAppliedAmounts = $this->getApplied($orderItem);
  581. $baseTotalTaxInvoiced = 0;
  582. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  583. if (isset($weeeTaxAppliedAmount[self::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED])) {
  584. $baseTotalTaxInvoiced = $weeeTaxAppliedAmount[self::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED];
  585. break;
  586. }
  587. }
  588. return $baseTotalTaxInvoiced;
  589. }
  590. /**
  591. * @param OrderItem $orderItem
  592. * @return float
  593. */
  594. public function getWeeeAmountRefunded($orderItem)
  595. {
  596. $weeeTaxAppliedAmounts = $this->getApplied($orderItem);
  597. $totalAmountRefunded = 0;
  598. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  599. if (isset($weeeTaxAppliedAmount[self::KEY_WEEE_AMOUNT_REFUNDED])) {
  600. $totalAmountRefunded = $weeeTaxAppliedAmount[self::KEY_WEEE_AMOUNT_REFUNDED];
  601. break;
  602. }
  603. }
  604. return $totalAmountRefunded;
  605. }
  606. /**
  607. * @param OrderItem $orderItem
  608. * @return float
  609. */
  610. public function getBaseWeeeAmountRefunded($orderItem)
  611. {
  612. $weeeTaxAppliedAmounts = $this->getApplied($orderItem);
  613. $baseTotalAmountRefunded = 0;
  614. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  615. if (isset($weeeTaxAppliedAmount[self::KEY_BASE_WEEE_AMOUNT_REFUNDED])) {
  616. $baseTotalAmountRefunded = $weeeTaxAppliedAmount[self::KEY_BASE_WEEE_AMOUNT_REFUNDED];
  617. break;
  618. }
  619. }
  620. return $baseTotalAmountRefunded;
  621. }
  622. /**
  623. * @param OrderItem $orderItem
  624. * @return float
  625. */
  626. public function getWeeeTaxAmountRefunded($orderItem)
  627. {
  628. $weeeTaxAppliedAmounts = $this->getApplied($orderItem);
  629. $totalTaxRefunded = 0;
  630. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  631. if (isset($weeeTaxAppliedAmount[self::KEY_WEEE_TAX_AMOUNT_REFUNDED])) {
  632. $totalTaxRefunded = $weeeTaxAppliedAmount[self::KEY_WEEE_TAX_AMOUNT_REFUNDED];
  633. break;
  634. }
  635. }
  636. return $totalTaxRefunded;
  637. }
  638. /**
  639. * @param OrderItem $orderItem
  640. * @return float
  641. */
  642. public function getBaseWeeeTaxAmountRefunded($orderItem)
  643. {
  644. $weeeTaxAppliedAmounts = $this->getApplied($orderItem);
  645. $baseTotalTaxRefunded = 0;
  646. foreach ($weeeTaxAppliedAmounts as $weeeTaxAppliedAmount) {
  647. if (isset($weeeTaxAppliedAmount[self::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED])) {
  648. $baseTotalTaxRefunded = $weeeTaxAppliedAmount[self::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED];
  649. break;
  650. }
  651. }
  652. return $baseTotalTaxRefunded;
  653. }
  654. /**
  655. * Returns the total amount of FPT across all items. Used for displaying the FPT totals line item.
  656. *
  657. * @param QuoteAbstractItem[] $items
  658. * @param null|string|bool|int|Store $store
  659. * @return float
  660. */
  661. public function getTotalAmounts($items, $store = null)
  662. {
  663. $weeeTotal = 0;
  664. $displayTotalsInclTax = $this->displayTotalsInclTax($store);
  665. foreach ($items as $item) {
  666. if ($displayTotalsInclTax) {
  667. $weeeTotal += $this->getRowWeeeTaxInclTax($item);
  668. } else {
  669. $weeeTotal += $item->getWeeeTaxAppliedRowAmount();
  670. }
  671. }
  672. return $weeeTotal;
  673. }
  674. /**
  675. * Returns the base total amount of FPT across all items. Used for displaying the FPT totals line item.
  676. *
  677. * @param QuoteAbstractItem[] $items
  678. * @param null|string|bool|int|Store $store
  679. * @return float
  680. * @since 100.1.0
  681. */
  682. public function getBaseTotalAmounts($items, $store = null)
  683. {
  684. $baseWeeeTotal = 0;
  685. $displayTotalsInclTax = $this->displayTotalsInclTax($store);
  686. foreach ($items as $item) {
  687. if ($displayTotalsInclTax) {
  688. $baseWeeeTotal += $this->getBaseRowWeeeTaxInclTax($item);
  689. } else {
  690. $baseWeeeTotal += $item->getBaseWeeeTaxAppliedRowAmnt();
  691. }
  692. }
  693. return $baseWeeeTotal;
  694. }
  695. /**
  696. * Get FPT DISPLAY_INCL setting
  697. *
  698. * @param int|null $storeId
  699. * @return bool
  700. */
  701. public function isDisplayIncl($storeId = null)
  702. {
  703. return $this->typeOfDisplay(
  704. WeeeDisplayConfig::DISPLAY_INCL,
  705. \Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW,
  706. $storeId
  707. );
  708. }
  709. /**
  710. * Get FPT DISPLAY_INCL_DESCR setting
  711. *
  712. * @param int|null $storeId
  713. * @return bool
  714. */
  715. public function isDisplayInclDesc($storeId = null)
  716. {
  717. return $this->typeOfDisplay(
  718. WeeeDisplayConfig::DISPLAY_INCL_DESCR,
  719. \Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW,
  720. $storeId
  721. );
  722. }
  723. /**
  724. * Get FPT DISPLAY_EXCL_DESCR_INCL setting
  725. *
  726. * @param int|null $storeId
  727. * @return bool
  728. */
  729. public function isDisplayExclDescIncl($storeId = null)
  730. {
  731. return $this->typeOfDisplay(
  732. WeeeDisplayConfig::DISPLAY_EXCL_DESCR_INCL,
  733. \Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW,
  734. $storeId
  735. );
  736. }
  737. /**
  738. * Get FPT DISPLAY_EXCL setting
  739. *
  740. * @param int|null $storeId
  741. * @return bool
  742. */
  743. public function isDisplayExcl($storeId = null)
  744. {
  745. return $this->typeOfDisplay(
  746. WeeeDisplayConfig::DISPLAY_EXCL,
  747. \Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW,
  748. $storeId
  749. );
  750. }
  751. /**
  752. * Get tax price display settings
  753. *
  754. * @param null|string|bool|int|Store $store
  755. * @return int
  756. */
  757. public function getTaxDisplayConfig($store = null)
  758. {
  759. return $this->_taxData->getPriceDisplayType($store);
  760. }
  761. /**
  762. * Return an array of FPT attributes for a bundle product
  763. *
  764. * @param \Magento\Catalog\Model\Product $product
  765. * @return array
  766. */
  767. public function getWeeeAttributesForBundle($product)
  768. {
  769. if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
  770. $typeInstance = $product->getTypeInstance();
  771. $typeInstance->setStoreFilter($product->getStoreId(), $product);
  772. $selectionCollection = $typeInstance->getSelectionsCollection(
  773. $typeInstance->getOptionsIds($product),
  774. $product
  775. );
  776. $insertedWeeeCodesArray = [];
  777. foreach ($selectionCollection as $selectionItem) {
  778. $weeeAttributes = $this->getProductWeeeAttributes(
  779. $selectionItem,
  780. null,
  781. null,
  782. $product->getStore()->getWebsiteId(),
  783. true,
  784. false
  785. );
  786. $priceTaxDisplay = $this->getTaxDisplayConfig();
  787. $priceIncludesTax = $this->displayTotalsInclTax();
  788. foreach ($weeeAttributes as $weeeAttribute) {
  789. if ($priceTaxDisplay == \Magento\Tax\Model\Config::DISPLAY_TYPE_INCLUDING_TAX ||
  790. $priceTaxDisplay == \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH) {
  791. if ($priceIncludesTax == false) {
  792. $weeeAttribute['amount'] = $weeeAttribute['amount_excl_tax'] + $weeeAttribute['tax_amount'];
  793. }
  794. } elseif ($priceTaxDisplay == \Magento\Tax\Model\Config::DISPLAY_TYPE_EXCLUDING_TAX) {
  795. if ($priceIncludesTax == true) {
  796. $weeeAttribute['amount'] = $weeeAttribute['amount_excl_tax'];
  797. }
  798. }
  799. $insertedWeeeCodesArray[$selectionItem->getId()][$weeeAttribute->getCode()] = $weeeAttribute;
  800. }
  801. }
  802. return $insertedWeeeCodesArray;
  803. }
  804. return [];
  805. }
  806. }