Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Rule.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
21  const KEY_ID = 'id';
22  const KEY_CODE = 'code';
23  const KEY_PRIORITY = 'priority';
24  const KEY_POSITION = 'position';
25  const KEY_CUSTOMER_TAX_CLASS_IDS = 'customer_tax_class_ids';
26  const KEY_PRODUCT_TAX_CLASS_IDS = 'product_tax_class_ids';
27  const KEY_TAX_RATE_IDS = 'tax_rate_ids';
28  const KEY_CALCULATE_SUBTOTAL = 'calculate_subtotal';
32  protected $_eventPrefix = 'tax_rule';
33 
39  protected $_taxClass;
40 
44  protected $_calculation;
45 
49  protected $validator;
50 
56  protected $_idFieldName = 'tax_calculation_rule_id';
57 
71  public function __construct(
72  \Magento\Framework\Model\Context $context,
73  \Magento\Framework\Registry $registry,
74  ExtensionAttributesFactory $extensionFactory,
76  \Magento\Tax\Model\ClassModel $taxClass,
77  \Magento\Tax\Model\Calculation $calculation,
79  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
80  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
81  array $data = []
82  ) {
83  $this->_calculation = $calculation;
84  $this->validator = $validator;
85  parent::__construct(
86  $context,
87  $registry,
88  $extensionFactory,
90  $resource,
91  $resourceCollection,
92  $data
93  );
94  $this->_init(\Magento\Tax\Model\ResourceModel\Calculation\Rule::class);
95  $this->_taxClass = $taxClass;
96  }
97 
104  public function afterSave()
105  {
106  parent::afterSave();
107  $this->saveCalculationData();
108  $this->_eventManager->dispatch('tax_settings_change_after');
109  return $this;
110  }
111 
118  public function afterDelete()
119  {
120  $this->_eventManager->dispatch('tax_settings_change_after');
121  return parent::afterDelete();
122  }
123 
127  public function saveCalculationData()
128  {
129  $ctc = $this->getData('customer_tax_class_ids');
130  $ptc = $this->getData('product_tax_class_ids');
131  $rates = $this->getData('tax_rate_ids');
132 
133  $this->_calculation->deleteByRuleId($this->getId());
134  foreach ($ctc as $c) {
135  foreach ($ptc as $p) {
136  foreach ($rates as $r) {
137  $dataArray = [
138  'tax_calculation_rule_id' => $this->getId(),
139  'tax_calculation_rate_id' => $r,
140  'customer_tax_class_id' => $c,
141  'product_tax_class_id' => $p,
142  ];
143  $this->_calculation->setData($dataArray)->save();
144  }
145  }
146  }
147  }
148 
152  public function getCalculationModel()
153  {
154  return $this->_calculation;
155  }
156 
160  public function getRates()
161  {
162  return $this->getCalculationModel()->getRates($this->getId());
163  }
164 
168  public function getCustomerTaxClasses()
169  {
170  return $this->getCalculationModel()->getCustomerTaxClasses($this->getId());
171  }
172 
176  public function getProductTaxClasses()
177  {
178  return $this->getCalculationModel()->getProductTaxClasses($this->getId());
179  }
180 
190  public function fetchRuleCodes($rateId, $customerTaxClassIds, $productTaxClassIds)
191  {
192  return $this->getResource()->fetchRuleCodes($rateId, $customerTaxClassIds, $productTaxClassIds);
193  }
194 
199  public function getCode()
200  {
201  return $this->getData(self::KEY_CODE);
202  }
203 
207  public function getPosition()
208  {
209  return (int) $this->getData(self::KEY_POSITION);
210  }
211 
215  public function getCalculateSubtotal()
216  {
217  return (bool) $this->getData(self::KEY_CALCULATE_SUBTOTAL);
218  }
219 
223  public function getPriority()
224  {
225  return $this->getData(self::KEY_PRIORITY);
226  }
227 
228  //@codeCoverageIgnoreEnd
229 
233  public function getCustomerTaxClassIds()
234  {
235  $ids = $this->getData(self::KEY_CUSTOMER_TAX_CLASS_IDS);
236  if (null === $ids) {
237  $ids = $this->_getUniqueValues($this->getCustomerTaxClasses());
238  $this->setData(self::KEY_CUSTOMER_TAX_CLASS_IDS, $ids);
239  }
240  return $ids;
241  }
242 
246  public function getProductTaxClassIds()
247  {
248  $ids = $this->getData(self::KEY_PRODUCT_TAX_CLASS_IDS);
249  if (null === $ids) {
250  $ids = $this->_getUniqueValues($this->getProductTaxClasses());
251  $this->setData(self::KEY_PRODUCT_TAX_CLASS_IDS, $ids);
252  }
253  return $ids;
254  }
255 
259  public function getTaxRateIds()
260  {
261  $ids = $this->getData(self::KEY_TAX_RATE_IDS);
262  if (null === $ids) {
263  $ids = $this->_getUniqueValues($this->getRates());
264  $this->setData(self::KEY_TAX_RATE_IDS, $ids);
265  }
266  return $ids;
267  }
268 
275  protected function _getUniqueValues($values)
276  {
277  if (!$values) {
278  return null;
279  }
280  return array_values(array_unique($values));
281  }
282 
286  protected function _getValidationRulesBeforeSave()
287  {
288  return $this->validator;
289  }
290 
297  public function setCode($code)
298  {
299  return $this->setData(self::KEY_CODE, $code);
300  }
301 
308  public function setPriority($priority)
309  {
310  return $this->setData(self::KEY_PRIORITY, $priority);
311  }
312 
319  public function setPosition($position)
320  {
321  return $this->setData(self::KEY_POSITION, $position);
322  }
323 
330  public function setCustomerTaxClassIds(array $customerTaxClassIds = null)
331  {
332  return $this->setData(self::KEY_CUSTOMER_TAX_CLASS_IDS, $customerTaxClassIds);
333  }
334 
341  public function setProductTaxClassIds(array $productTaxClassIds = null)
342  {
343  return $this->setData(self::KEY_PRODUCT_TAX_CLASS_IDS, $productTaxClassIds);
344  }
345 
352  public function setTaxRateIds(array $taxRateIds = null)
353  {
354  return $this->setData(self::KEY_TAX_RATE_IDS, $taxRateIds);
355  }
356 
363  public function setCalculateSubtotal($calculateSubtotal)
364  {
365  return $this->setData(self::KEY_CALCULATE_SUBTOTAL, $calculateSubtotal);
366  }
367 
373  public function getExtensionAttributes()
374  {
375  return $this->_getExtensionAttributes();
376  }
377 
384  public function setExtensionAttributes(\Magento\Tax\Api\Data\TaxRuleExtensionInterface $extensionAttributes)
385  {
386  return $this->_setExtensionAttributes($extensionAttributes);
387  }
388 }
setProductTaxClassIds(array $productTaxClassIds=null)
Definition: Rule.php:341
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
$values
Definition: options.phtml:88
setExtensionAttributes(\Magento\Tax\Api\Data\TaxRuleExtensionInterface $extensionAttributes)
Definition: Rule.php:384
$resource
Definition: bulk.php:12
fetchRuleCodes($rateId, $customerTaxClassIds, $productTaxClassIds)
Definition: Rule.php:190
$rates
Definition: tax.phtml:35
setCalculateSubtotal($calculateSubtotal)
Definition: Rule.php:363
setTaxRateIds(array $taxRateIds=null)
Definition: Rule.php:352
setCustomerTaxClassIds(array $customerTaxClassIds=null)
Definition: Rule.php:330
$code
Definition: info.phtml:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Tax\Model\ClassModel $taxClass, \Magento\Tax\Model\Calculation $calculation, \Magento\Tax\Model\Calculation\Rule\Validator $validator, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Rule.php:71