Tax.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Model\Attribute\Backend\Weee;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;
  9. class Tax extends \Magento\Catalog\Model\Product\Attribute\Backend\Price
  10. {
  11. /**
  12. * @var \Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax
  13. */
  14. protected $_attributeTax;
  15. /**
  16. * @var \Magento\Store\Model\StoreManagerInterface
  17. */
  18. protected $_storeManager;
  19. /**
  20. * @var \Magento\Directory\Helper\Data
  21. */
  22. protected $_directoryHelper;
  23. /**
  24. * Initialize dependencies.
  25. *
  26. * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
  27. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  28. * @param \Magento\Catalog\Helper\Data $catalogData
  29. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  30. * @param \Magento\Framework\Locale\FormatInterface $localeFormat
  31. * @param \Magento\Directory\Helper\Data $directoryHelper
  32. * @param \Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax $attributeTax
  33. * @param ScopeOverriddenValue|null $scopeOverriddenValue
  34. */
  35. public function __construct(
  36. \Magento\Directory\Model\CurrencyFactory $currencyFactory,
  37. \Magento\Store\Model\StoreManagerInterface $storeManager,
  38. \Magento\Catalog\Helper\Data $catalogData,
  39. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  40. \Magento\Framework\Locale\FormatInterface $localeFormat,
  41. \Magento\Directory\Helper\Data $directoryHelper,
  42. \Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax $attributeTax,
  43. ScopeOverriddenValue $scopeOverriddenValue = null
  44. ) {
  45. $this->_directoryHelper = $directoryHelper;
  46. $this->_storeManager = $storeManager;
  47. $this->_attributeTax = $attributeTax;
  48. parent::__construct(
  49. $currencyFactory,
  50. $storeManager,
  51. $catalogData,
  52. $config,
  53. $localeFormat,
  54. $scopeOverriddenValue
  55. );
  56. }
  57. /**
  58. * @return string
  59. */
  60. public static function getBackendModelName()
  61. {
  62. return \Magento\Weee\Model\Attribute\Backend\Weee\Tax::class;
  63. }
  64. /**
  65. * Validate data
  66. *
  67. * @param \Magento\Catalog\Model\Product $object
  68. * @return $this
  69. * @throws \Magento\Framework\Exception\LocalizedException
  70. */
  71. public function validate($object)
  72. {
  73. $taxes = $object->getData($this->getAttribute()->getName());
  74. if (empty($taxes)) {
  75. return $this;
  76. }
  77. $dup = [];
  78. foreach ($taxes as $tax) {
  79. if (!empty($tax['delete'])) {
  80. continue;
  81. }
  82. $state = isset($tax['state']) ? ($tax['state'] > 0 ? $tax['state'] : 0) : '0';
  83. $key1 = implode('-', [$tax['website_id'], $tax['country'], $state]);
  84. if (!empty($dup[$key1])) {
  85. throw new LocalizedException(
  86. __('Set unique country-state combinations within the same fixed product tax. '
  87. . 'Verify the combinations and try again.')
  88. );
  89. }
  90. $dup[$key1] = 1;
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Assign WEEE taxes to product data
  96. *
  97. * @param \Magento\Catalog\Model\Product $object
  98. * @return $this
  99. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  100. */
  101. public function afterLoad($object)
  102. {
  103. $data = $this->_attributeTax->loadProductData($object, $this->getAttribute());
  104. foreach ($data as $i => $row) {
  105. if ($data[$i]['website_id'] == 0) {
  106. $rate = $this->_storeManager->getStore()->getBaseCurrency()->getRate(
  107. $this->_directoryHelper->getBaseCurrencyCode()
  108. );
  109. if ($rate) {
  110. $data[$i]['website_value'] = $data[$i]['value'] / $rate;
  111. } else {
  112. unset($data[$i]);
  113. }
  114. } else {
  115. $data[$i]['website_value'] = $data[$i]['value'];
  116. }
  117. }
  118. $object->setData($this->getAttribute()->getName(), $data);
  119. return $this;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  124. * @SuppressWarnings(PHPMD.NPathComplexity)
  125. */
  126. public function afterSave($object)
  127. {
  128. $orig = $object->getOrigData($this->getAttribute()->getName());
  129. $current = $object->getData($this->getAttribute()->getName());
  130. if ($orig == $current) {
  131. return $this;
  132. }
  133. $this->_attributeTax->deleteProductData($object, $this->getAttribute());
  134. $taxes = $object->getData($this->getAttribute()->getName());
  135. if (!is_array($taxes)) {
  136. return $this;
  137. }
  138. foreach ($taxes as $tax) {
  139. if ((empty($tax['price']) && empty($tax['value'])) || empty($tax['country']) || !empty($tax['delete'])) {
  140. continue;
  141. }
  142. $state = isset($tax['state']) ? $tax['state'] : '0';
  143. $data = [];
  144. $data['website_id'] = $tax['website_id'];
  145. $data['country'] = $tax['country'];
  146. $data['state'] = $state;
  147. $data['value'] = !empty($tax['price']) ? $tax['price'] : $tax['value'];
  148. $data['attribute_id'] = $this->getAttribute()->getId();
  149. $this->_attributeTax->insertProductData($object, $data);
  150. }
  151. return $this;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function afterDelete($object)
  157. {
  158. $this->_attributeTax->deleteProductData($object, $this->getAttribute());
  159. return $this;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function getTable()
  165. {
  166. return $this->_attributeTax->getTable('weee_tax');
  167. }
  168. /**
  169. * @inheritdoc
  170. */
  171. public function getEntityIdField()
  172. {
  173. return $this->_attributeTax->getIdFieldName();
  174. }
  175. }