Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Rate.php
Go to the documentation of this file.
1 <?php
8 
13 
20 {
24  const KEY_ID = 'id';
25  const KEY_COUNTRY_ID = 'tax_country_id';
26  const KEY_REGION_ID = 'tax_region_id';
27  const KEY_REGION_NAME = 'region_name';
28  const KEY_POSTCODE = 'tax_postcode';
29  const KEY_ZIP_IS_RANGE = 'zip_is_range';
30  const KEY_ZIP_RANGE_FROM = 'zip_from';
31  const KEY_ZIP_RANGE_TO = 'zip_to';
32  const KEY_PERCENTAGE_RATE = 'rate';
33  const KEY_CODE = 'code';
34  const KEY_TITLES = 'titles';
38  protected $_titles = null;
39 
43  protected $_titleModel = null;
44 
48  protected $_regionFactory;
49 
53  protected $_titleFactory;
57  protected $directoryRegion;
58 
72  public function __construct(
73  \Magento\Framework\Model\Context $context,
74  \Magento\Framework\Registry $registry,
75  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
77  \Magento\Directory\Model\RegionFactory $regionFactory,
78  \Magento\Tax\Model\Calculation\Rate\TitleFactory $taxTitleFactory,
80  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
81  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
82  array $data = []
83  ) {
84  $this->_regionFactory = $regionFactory;
85  $this->_titleFactory = $taxTitleFactory;
86  $this->directoryRegion = $directoryRegion;
87  parent::__construct(
88  $context,
89  $registry,
90  $extensionFactory,
92  $resource,
93  $resourceCollection,
94  $data
95  );
96  }
97 
103  protected function _construct()
104  {
105  $this->_init(\Magento\Tax\Model\ResourceModel\Calculation\Rate::class);
106  }
107 
116  public function beforeSave()
117  {
118  $isWrongRange = $this->getZipIsRange() && ($this->getZipFrom() === '' || $this->getZipTo() === '');
119 
120  $isEmptyValues = $this->getCode() === '' ||
121  $this->getTaxCountryId() === '' ||
122  $this->getRate() === '' ||
123  ($this->getTaxPostcode() === '' && !$this->getZipIsRange());
124 
125  if ($isEmptyValues || $isWrongRange) {
126  throw new \Magento\Framework\Exception\LocalizedException(
127  __('The required information is invalid. Verify the information and try again.')
128  );
129  }
130 
131  if (!is_numeric($this->getRate()) || $this->getRate() < 0) {
132  throw new \Magento\Framework\Exception\LocalizedException(
133  __('The Rate Percent is invalid. Enter a positive number and try again.')
134  );
135  }
136 
137  if ($this->getZipIsRange()) {
138  $zipFrom = $this->getZipFrom();
139  $zipTo = $this->getZipTo();
140 
141  if (strlen($zipFrom) > 9 || strlen($zipTo) > 9) {
142  throw new \Magento\Framework\Exception\LocalizedException(
143  __(
144  'The ZIP Code length is invalid. '
145  . 'Verify that the length is nine characters or fewer and try again.'
146  )
147  );
148  }
149 
150  if (!is_numeric($zipFrom) || !is_numeric($zipTo) || $zipFrom < 0 || $zipTo < 0) {
151  throw new \Magento\Framework\Exception\LocalizedException(
152  __('The ZIP Code is invalid. Use numbers only.')
153  );
154  }
155 
156  if ($zipFrom > $zipTo) {
157  throw new \Magento\Framework\Exception\LocalizedException(
158  __('Range To should be equal or greater than Range From.')
159  );
160  }
161 
162  $this->setTaxPostcode($zipFrom . '-' . $zipTo);
163  } else {
164  $taxPostCode = $this->getTaxPostcode();
165 
166  if (strlen($taxPostCode) > 10) {
167  $taxPostCode = substr($taxPostCode, 0, 10);
168  }
169 
170  $this->setTaxPostcode($taxPostCode)->setZipIsRange(null)->setZipFrom(null)->setZipTo(null);
171  }
172 
173  parent::beforeSave();
174  $country = $this->getTaxCountryId();
175  $region = $this->getTaxRegionId();
177  $regionModel = $this->_regionFactory->create();
178  $regionModel->load($region);
179  if ($regionModel->getCountryId() != $country) {
180  $this->setTaxRegionId('*');
181  }
182  return $this;
183  }
184 
190  public function afterSave()
191  {
192  $this->saveTitles();
193  $this->_eventManager->dispatch('tax_settings_change_after');
194  return parent::afterSave();
195  }
196 
203  public function beforeDelete()
204  {
205  if ($this->_isInRule()) {
206  throw new CouldNotDeleteException(
207  __("The tax rate can't be removed because it exists in a tax rule.")
208  );
209  }
210  return parent::beforeDelete();
211  }
212 
219  public function afterDelete()
220  {
221  $this->_eventManager->dispatch('tax_settings_change_after');
222  return parent::afterDelete();
223  }
224 
231  public function saveTitles($titles = null)
232  {
233  if ($titles === null) {
234  $titles = $this->getTitle();
235  }
236 
237  $this->getTitleModel()->deleteByRateId($this->getId());
238  if (is_array($titles) && $titles) {
239  foreach ($titles as $store => $title) {
240  if ($title !== '') {
241  $this->getTitleModel()->setId(
242  null
243  )->setTaxCalculationRateId(
244  $this->getId()
245  )->setStoreId(
246  (int)$store
247  )->setValue(
248  $title
249  )->save();
250  }
251  }
252  }
253  }
254 
260  public function getTitleModel()
261  {
262  if ($this->_titleModel === null) {
263  $this->_titleModel = $this->_titleFactory->create();
264  }
265  return $this->_titleModel;
266  }
267 
271  public function getTitles()
272  {
273  if ($this->getData(self::KEY_TITLES)) {
274  return $this->getData(self::KEY_TITLES);
275  }
276  if ($this->_titles === null) {
277  $this->_titles = $this->getTitleModel()->getCollection()->loadByRateId($this->getId())->getItems();
278  }
279  return $this->_titles;
280  }
281 
287  public function deleteAllRates()
288  {
289  $this->_getResource()->deleteAllRates();
290  $this->_eventManager->dispatch('tax_settings_change_after');
291  return $this;
292  }
293 
300  public function loadByCode($code)
301  {
302  $this->load($code, 'code');
303  return $this;
304  }
305 
311  protected function _isInRule()
312  {
313  return $this->getResource()->isInRule($this->getId());
314  }
315 
319  public function getRegionName()
320  {
321  if (!$this->getData(self::KEY_REGION_NAME)) {
322  $regionName = $this->directoryRegion->load($this->getTaxRegionId())->getCode();
323  $this->setData(self::KEY_REGION_NAME, $regionName);
324  }
325  return $this->getData(self::KEY_REGION_NAME);
326  }
327 
332  public function getTaxCalculationRateId()
333  {
334  return $this->getData(self::KEY_ID);
335  }
336 
340  public function getTaxCountryId()
341  {
342  return $this->getData(self::KEY_COUNTRY_ID);
343  }
344 
348  public function getTaxRegionId()
349  {
350  return $this->getData(self::KEY_REGION_ID);
351  }
352 
356  public function getTaxPostcode()
357  {
358  return $this->getData(self::KEY_POSTCODE);
359  }
360 
364  public function getZipFrom()
365  {
366  return $this->getData(self::KEY_ZIP_RANGE_FROM);
367  }
368 
372  public function getZipTo()
373  {
374  return $this->getData(self::KEY_ZIP_RANGE_TO);
375  }
376 
380  public function getRate()
381  {
382  return $this->getData(self::KEY_PERCENTAGE_RATE);
383  }
384 
388  public function getCode()
389  {
390  return $this->getData(self::KEY_CODE);
391  }
392 
396  public function getZipIsRange()
397  {
398  return $this->getData(self::KEY_ZIP_IS_RANGE);
399  }
400 
407  public function setTaxCountryId($taxCountryId)
408  {
409  return $this->setData(self::KEY_COUNTRY_ID, $taxCountryId);
410  }
411 
418  public function setTaxRegionId($taxRegionId)
419  {
420  return $this->setData(self::KEY_REGION_ID, $taxRegionId);
421  }
422 
429  public function setRegionName($regionName)
430  {
431  return $this->setData(self::KEY_REGION_NAME, $regionName);
432  }
433 
440  public function setTaxPostcode($taxPostCode)
441  {
442  return $this->setData(self::KEY_POSTCODE, $taxPostCode);
443  }
444 
451  public function setZipIsRange($zipIsRange)
452  {
453  return $this->setData(self::KEY_ZIP_IS_RANGE, $zipIsRange);
454  }
455 
462  public function setZipFrom($zipFrom)
463  {
464  return $this->setData(self::KEY_ZIP_RANGE_FROM, $zipFrom);
465  }
466 
473  public function setZipTo($zipTo)
474  {
475  return $this->setData(self::KEY_ZIP_RANGE_TO, $zipTo);
476  }
477 
484  public function setRate($rate)
485  {
486  return $this->setData(self::KEY_PERCENTAGE_RATE, $rate);
487  }
488 
495  public function setCode($code)
496  {
497  return $this->setData(self::KEY_CODE, $code);
498  }
499 
506  public function setTitles(array $titles = null)
507  {
508  return $this->setData(self::KEY_TITLES, $titles);
509  }
510 
511  // @codeCoverageIgnoreEnd
512 
518  public function getExtensionAttributes()
519  {
520  return $this->_getExtensionAttributes();
521  }
522 
529  public function setExtensionAttributes(\Magento\Tax\Api\Data\TaxRateExtensionInterface $extensionAttributes)
530  {
531  return $this->_setExtensionAttributes($extensionAttributes);
532  }
533 }
$title
Definition: default.phtml:14
setTitles(array $titles=null)
Definition: Rate.php:506
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
setTaxCountryId($taxCountryId)
Definition: Rate.php:407
setExtensionAttributes(\Magento\Tax\Api\Data\TaxRateExtensionInterface $extensionAttributes)
Definition: Rate.php:529
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Tax\Model\Calculation\Rate\TitleFactory $taxTitleFactory, Region $directoryRegion, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Rate.php:72
$code
Definition: info.phtml:12