Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tax.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class Tax extends \Magento\Catalog\Model\Product\Attribute\Backend\Price
13 {
17  protected $_attributeTax;
18 
22  protected $_storeManager;
23 
27  protected $_directoryHelper;
28 
41  public function __construct(
42  \Magento\Directory\Model\CurrencyFactory $currencyFactory,
44  \Magento\Catalog\Helper\Data $catalogData,
45  \Magento\Framework\App\Config\ScopeConfigInterface $config,
46  \Magento\Framework\Locale\FormatInterface $localeFormat,
47  \Magento\Directory\Helper\Data $directoryHelper,
48  \Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax $attributeTax,
49  ScopeOverriddenValue $scopeOverriddenValue = null
50  ) {
51  $this->_directoryHelper = $directoryHelper;
52  $this->_storeManager = $storeManager;
53  $this->_attributeTax = $attributeTax;
54  parent::__construct(
55  $currencyFactory,
57  $catalogData,
58  $config,
60  $scopeOverriddenValue
61  );
62  }
63 
67  public static function getBackendModelName()
68  {
69  return \Magento\Weee\Model\Attribute\Backend\Weee\Tax::class;
70  }
71 
79  public function validate($object)
80  {
81  $taxes = $object->getData($this->getAttribute()->getName());
82  if (empty($taxes)) {
83  return $this;
84  }
85  $dup = [];
86  foreach ($taxes as $tax) {
87  if (!empty($tax['delete'])) {
88  continue;
89  }
90  $state = isset($tax['state']) ? ($tax['state'] > 0 ? $tax['state'] : 0) : '0';
91  $key1 = implode('-', [$tax['website_id'], $tax['country'], $state]);
92  if (!empty($dup[$key1])) {
93  throw new LocalizedException(
94  __('Set unique country-state combinations within the same fixed product tax. '
95  . 'Verify the combinations and try again.')
96  );
97  }
98  $dup[$key1] = 1;
99  }
100  return $this;
101  }
102 
110  public function afterLoad($object)
111  {
112  $data = $this->_attributeTax->loadProductData($object, $this->getAttribute());
113 
114  foreach ($data as $i => $row) {
115  if ($data[$i]['website_id'] == 0) {
116  $rate = $this->_storeManager->getStore()->getBaseCurrency()->getRate(
117  $this->_directoryHelper->getBaseCurrencyCode()
118  );
119  if ($rate) {
120  $data[$i]['website_value'] = $data[$i]['value'] / $rate;
121  } else {
122  unset($data[$i]);
123  }
124  } else {
125  $data[$i]['website_value'] = $data[$i]['value'];
126  }
127  }
128  $object->setData($this->getAttribute()->getName(), $data);
129  return $this;
130  }
131 
137  public function afterSave($object)
138  {
139  $orig = $object->getOrigData($this->getAttribute()->getName());
140  $current = $object->getData($this->getAttribute()->getName());
141  if ($orig == $current) {
142  return $this;
143  }
144 
145  $this->_attributeTax->deleteProductData($object, $this->getAttribute());
146  $taxes = $object->getData($this->getAttribute()->getName());
147 
148  if (!is_array($taxes)) {
149  return $this;
150  }
151 
152  foreach ($taxes as $tax) {
153  if ((empty($tax['price']) && empty($tax['value'])) || empty($tax['country']) || !empty($tax['delete'])) {
154  continue;
155  }
156 
157  $state = isset($tax['state']) ? $tax['state'] : '0';
158 
159  $data = [];
160  $data['website_id'] = $tax['website_id'];
161  $data['country'] = $tax['country'];
162  $data['state'] = $state;
163  $data['value'] = !empty($tax['price']) ? $tax['price'] : $tax['value'];
164  $data['attribute_id'] = $this->getAttribute()->getId();
165 
166  $this->_attributeTax->insertProductData($object, $data);
167  }
168 
169  return $this;
170  }
171 
175  public function afterDelete($object)
176  {
177  $this->_attributeTax->deleteProductData($object, $this->getAttribute());
178  return $this;
179  }
180 
184  public function getTable()
185  {
186  return $this->_attributeTax->getTable('weee_tax');
187  }
188 
192  public function getEntityIdField()
193  {
194  return $this->_attributeTax->getIdFieldName();
195  }
196 }
$config
Definition: fraud_order.php:17
$storeManager
__()
Definition: __.php:13
__construct(\Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Helper\Data $catalogData, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\Locale\FormatInterface $localeFormat, \Magento\Directory\Helper\Data $directoryHelper, \Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax $attributeTax, ScopeOverriddenValue $scopeOverriddenValue=null)
Definition: Tax.php:41
$i
Definition: gallery.phtml:31