Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Rate Class Reference
Inheritance diagram for Rate:
AbstractExtensibleModel TaxRateInterface AbstractModel CustomAttributesDataInterface ExtensibleDataInterface DataObject ExtensibleDataInterface

Public Member Functions

 __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=[])
 
 afterSave ()
 
 beforeDelete ()
 
 afterDelete ()
 
 saveTitles ($titles=null)
 
 getTitleModel ()
 
 getTitles ()
 
 deleteAllRates ()
 
 loadByCode ($code)
 
 getRegionName ()
 
 getTaxCalculationRateId ()
 
 getTaxCountryId ()
 
 getTaxRegionId ()
 
 getTaxPostcode ()
 
 getZipFrom ()
 
 getZipTo ()
 
 getRate ()
 
 getCode ()
 
 getZipIsRange ()
 
 setTaxCountryId ($taxCountryId)
 
 setTaxRegionId ($taxRegionId)
 
 setRegionName ($regionName)
 
 setTaxPostcode ($taxPostCode)
 
 setZipIsRange ($zipIsRange)
 
 setZipFrom ($zipFrom)
 
 setZipTo ($zipTo)
 
 setRate ($rate)
 
 setCode ($code)
 
 setTitles (array $titles=null)
 
 getExtensionAttributes ()
 
 setExtensionAttributes (\Magento\Tax\Api\Data\TaxRateExtensionInterface $extensionAttributes)
 
- Public Member Functions inherited from AbstractExtensibleModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 getCustomAttributes ()
 
 getCustomAttribute ($attributeCode)
 
 setCustomAttributes (array $attributes)
 
 setCustomAttribute ($attributeCode, $attributeValue)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 setId ($value)
 
 __sleep ()
 
 __wakeup ()
 
- Public Member Functions inherited from AbstractModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 __sleep ()
 
 __wakeup ()
 
 setIdFieldName ($name)
 
 getIdFieldName ()
 
 getId ()
 
 setId ($value)
 
 isDeleted ($isDeleted=null)
 
 hasDataChanges ()
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 setDataChanges ($value)
 
 getOrigData ($key=null)
 
 setOrigData ($key=null, $data=null)
 
 dataHasChangedFor ($field)
 
 getResourceName ()
 
 getResourceCollection ()
 
 getCollection ()
 
 load ($modelId, $field=null)
 
 beforeLoad ($identifier, $field=null)
 
 afterLoad ()
 
 isSaveAllowed ()
 
 setHasDataChanges ($flag)
 
 save ()
 
 afterCommitCallback ()
 
 isObjectNew ($flag=null)
 
 beforeSave ()
 
 validateBeforeSave ()
 
 getCacheTags ()
 
 cleanModelCache ()
 
 afterSave ()
 
 delete ()
 
 beforeDelete ()
 
 afterDelete ()
 
 afterDeleteCommit ()
 
 getResource ()
 
 getEntityId ()
 
 setEntityId ($entityId)
 
 clearInstance ()
 
 getStoredData ()
 
 getEventPrefix ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from TaxRateInterface
 getId ()
 
 setId ($id)
 

Data Fields

const KEY_ID = 'id'
 
const KEY_COUNTRY_ID = 'tax_country_id'
 
const KEY_REGION_ID = 'tax_region_id'
 
const KEY_REGION_NAME = 'region_name'
 
const KEY_POSTCODE = 'tax_postcode'
 
const KEY_ZIP_IS_RANGE = 'zip_is_range'
 
const KEY_ZIP_RANGE_FROM = 'zip_from'
 
const KEY_ZIP_RANGE_TO = 'zip_to'
 
const KEY_PERCENTAGE_RATE = 'rate'
 
const KEY_CODE = 'code'
 
const KEY_TITLES = 'titles'
 
- Data Fields inherited from CustomAttributesDataInterface
const CUSTOM_ATTRIBUTES = 'custom_attributes'
 
- Data Fields inherited from ExtensibleDataInterface
const EXTENSION_ATTRIBUTES_KEY = 'extension_attributes'
 

Protected Member Functions

 _construct ()
 
 _isInRule ()
 
- Protected Member Functions inherited from AbstractExtensibleModel
 filterCustomAttributes ($data)
 
 initializeCustomAttributes ()
 
 convertCustomAttributeValues (array &$customAttributes)
 
 getCustomAttributesCodes ()
 
 _setExtensionAttributes (\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
 
 _getExtensionAttributes ()
 
- Protected Member Functions inherited from AbstractModel
 _construct ()
 
 _init ($resourceModel)
 
 _setResourceModel ($resourceName, $collectionName=null)
 
 _getResource ()
 
 _getEventData ()
 
 _beforeLoad ($modelId, $field=null)
 
 _afterLoad ()
 
 _hasModelChanged ()
 
 _getValidatorBeforeSave ()
 
 _createValidatorBeforeSave ()
 
 _getValidationRulesBeforeSave ()
 
 _clearReferences ()
 
 _clearData ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_titles = null
 
 $_titleModel = null
 
 $_regionFactory
 
 $_titleFactory
 
 $directoryRegion
 
- Protected Attributes inherited from AbstractExtensibleModel
 $extensionAttributesFactory
 
 $extensionAttributes
 
 $customAttributeFactory
 
 $customAttributesCodes = null
 
 $customAttributesChanged = false
 
- Protected Attributes inherited from AbstractModel
 $_eventPrefix = 'core_abstract'
 
 $_eventObject = 'object'
 
 $_idFieldName = 'id'
 
 $_hasDataChanges = false
 
 $_origData
 
 $_isDeleted = false
 
 $_resource
 
 $_resourceCollection
 
 $_resourceName
 
 $_collectionName
 
 $_cacheTag = false
 
 $_dataSaveAllowed = true
 
 $_isObjectNew = null
 
 $_validatorBeforeSave = null
 
 $_eventManager
 
 $_cacheManager
 
 $_registry
 
 $_logger
 
 $_appState
 
 $_actionValidator
 
 $storedData = []
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Tax Rate Model

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 19 of file Rate.php.

Constructor & Destructor Documentation

◆ __construct()

__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 = [] 
)
Parameters
\Magento\Framework\Model\Context$context
\Magento\Framework\Registry$registry
\Magento\Framework\Api\ExtensionAttributesFactory$extensionFactory
AttributeValueFactory$customAttributeFactory
\Magento\Directory\Model\RegionFactory$regionFactory
Rate\TitleFactory$taxTitleFactory
Region$directoryRegion
\Magento\Framework\Model\ResourceModel\AbstractResource$resource
\Magento\Framework\Data\Collection\AbstractDb$resourceCollection
array$data@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 72 of file Rate.php.

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  }
$resource
Definition: bulk.php:12

Member Function Documentation

◆ _construct()

_construct ( )
protected

Magento model constructor

Returns
void

Definition at line 103 of file Rate.php.

104  {
105  $this->_init(\Magento\Tax\Model\ResourceModel\Calculation\Rate::class);
106  }

◆ _isInRule()

_isInRule ( )
protected

Check if rate exists in tax rule

Returns
array

Definition at line 311 of file Rate.php.

312  {
313  return $this->getResource()->isInRule($this->getId());
314  }

◆ afterDelete()

afterDelete ( )

After rate delete redeclared for dispatch tax_settings_change_after event

Returns
\Magento\Tax\Model\Calculation\Rate

Definition at line 219 of file Rate.php.

220  {
221  $this->_eventManager->dispatch('tax_settings_change_after');
222  return parent::afterDelete();
223  }

◆ afterSave()

afterSave ( )

Save rate titles

Returns
\Magento\Tax\Model\Calculation\Rate

Definition at line 190 of file Rate.php.

191  {
192  $this->saveTitles();
193  $this->_eventManager->dispatch('tax_settings_change_after');
194  return parent::afterSave();
195  }

◆ beforeDelete()

beforeDelete ( )

Processing object before delete data

Returns
\Magento\Tax\Model\Calculation\Rate
Exceptions

Definition at line 203 of file Rate.php.

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  }
__()
Definition: __.php:13

◆ deleteAllRates()

deleteAllRates ( )

Deletes all tax rates

Returns
\Magento\Tax\Model\Calculation\Rate

Definition at line 287 of file Rate.php.

288  {
289  $this->_getResource()->deleteAllRates();
290  $this->_eventManager->dispatch('tax_settings_change_after');
291  return $this;
292  }

◆ getCode()

getCode ( )

{Get tax rate code

Returns
string
}

Implements TaxRateInterface.

Definition at line 388 of file Rate.php.

389  {
390  return $this->getData(self::KEY_CODE);
391  }

◆ getExtensionAttributes()

getExtensionAttributes ( )

{Retrieve existing extension attributes object or create a new one.

Returns
\Magento\Tax\Api\Data\TaxRateExtensionInterface|null
}

Returns
\Magento\Tax\Api\Data\TaxRateExtensionInterface|null

Implements TaxRateInterface.

Definition at line 518 of file Rate.php.

◆ getRate()

getRate ( )

{Get tax rate in percentage

Returns
float
}

Implements TaxRateInterface.

Definition at line 380 of file Rate.php.

381  {
382  return $this->getData(self::KEY_PERCENTAGE_RATE);
383  }

◆ getRegionName()

getRegionName ( )

{Get region name

Returns
string|null
}

Implements TaxRateInterface.

Definition at line 319 of file Rate.php.

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  }

◆ getTaxCalculationRateId()

getTaxCalculationRateId ( )

@codeCoverageIgnoreStart {}

Definition at line 332 of file Rate.php.

333  {
334  return $this->getData(self::KEY_ID);
335  }

◆ getTaxCountryId()

getTaxCountryId ( )

{Get country id

Returns
string
}

Implements TaxRateInterface.

Definition at line 340 of file Rate.php.

341  {
342  return $this->getData(self::KEY_COUNTRY_ID);
343  }

◆ getTaxPostcode()

getTaxPostcode ( )

{Get postcode

Returns
string|null
}

Implements TaxRateInterface.

Definition at line 356 of file Rate.php.

357  {
358  return $this->getData(self::KEY_POSTCODE);
359  }

◆ getTaxRegionId()

getTaxRegionId ( )

{Get region id

Returns
int|null
}

Implements TaxRateInterface.

Definition at line 348 of file Rate.php.

349  {
350  return $this->getData(self::KEY_REGION_ID);
351  }

◆ getTitleModel()

getTitleModel ( )

Returns a tax title

Returns
\Magento\Tax\Model\Calculation\Rate\Title

Definition at line 260 of file Rate.php.

261  {
262  if ($this->_titleModel === null) {
263  $this->_titleModel = $this->_titleFactory->create();
264  }
265  return $this->_titleModel;
266  }

◆ getTitles()

getTitles ( )

{Get tax rate titles

Returns
\Magento\Tax\Api\Data\TaxRateTitleInterface[]|null
}

Implements TaxRateInterface.

Definition at line 271 of file Rate.php.

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  }

◆ getZipFrom()

getZipFrom ( )

{Get zip range from

Returns
int|null
}

Implements TaxRateInterface.

Definition at line 364 of file Rate.php.

365  {
366  return $this->getData(self::KEY_ZIP_RANGE_FROM);
367  }

◆ getZipIsRange()

getZipIsRange ( )

{Get zip is range

Returns
int|null
}

Implements TaxRateInterface.

Definition at line 396 of file Rate.php.

397  {
398  return $this->getData(self::KEY_ZIP_IS_RANGE);
399  }

◆ getZipTo()

getZipTo ( )

{Get zip range to

Returns
int|null
}

Implements TaxRateInterface.

Definition at line 372 of file Rate.php.

373  {
374  return $this->getData(self::KEY_ZIP_RANGE_TO);
375  }

◆ loadByCode()

loadByCode (   $code)

Load rate model by code

Parameters
string$code
Returns
\Magento\Tax\Model\Calculation\Rate

Definition at line 300 of file Rate.php.

301  {
302  $this->load($code, 'code');
303  return $this;
304  }
$code
Definition: info.phtml:12

◆ saveTitles()

saveTitles (   $titles = null)

Saves the tax titles

Parameters
array | null$titles
Returns
void

Definition at line 231 of file Rate.php.

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  }
$title
Definition: default.phtml:14

◆ setCode()

setCode (   $code)

Set tax rate code

Parameters
string$code
Returns
$this

Implements TaxRateInterface.

Definition at line 495 of file Rate.php.

496  {
497  return $this->setData(self::KEY_CODE, $code);
498  }
$code
Definition: info.phtml:12

◆ setExtensionAttributes()

setExtensionAttributes ( \Magento\Tax\Api\Data\TaxRateExtensionInterface  $extensionAttributes)

{Set an extension attributes object.

Parameters
\Magento\Tax\Api\Data\TaxRateExtensionInterface$extensionAttributes
Returns
$this
}

Parameters
\Magento\Tax\Api\Data\TaxRateExtensionInterface$extensionAttributes
Returns
$this

Implements TaxRateInterface.

Definition at line 529 of file Rate.php.

530  {
532  }
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)

◆ setRate()

setRate (   $rate)

Set tax rate in percentage

Parameters
float$rate
Returns
$this

Implements TaxRateInterface.

Definition at line 484 of file Rate.php.

485  {
486  return $this->setData(self::KEY_PERCENTAGE_RATE, $rate);
487  }

◆ setRegionName()

setRegionName (   $regionName)

Set region name

Parameters
string$regionName
Returns
$this

Implements TaxRateInterface.

Definition at line 429 of file Rate.php.

430  {
431  return $this->setData(self::KEY_REGION_NAME, $regionName);
432  }

◆ setTaxCountryId()

setTaxCountryId (   $taxCountryId)

Set country id

Parameters
string$taxCountryId
Returns
$this

Implements TaxRateInterface.

Definition at line 407 of file Rate.php.

408  {
409  return $this->setData(self::KEY_COUNTRY_ID, $taxCountryId);
410  }

◆ setTaxPostcode()

setTaxPostcode (   $taxPostCode)

Set postcode

Parameters
string$taxPostCode
Returns
$this

Implements TaxRateInterface.

Definition at line 440 of file Rate.php.

441  {
442  return $this->setData(self::KEY_POSTCODE, $taxPostCode);
443  }

◆ setTaxRegionId()

setTaxRegionId (   $taxRegionId)

Set region id

Parameters
int$taxRegionId
Returns
$this

Implements TaxRateInterface.

Definition at line 418 of file Rate.php.

419  {
420  return $this->setData(self::KEY_REGION_ID, $taxRegionId);
421  }

◆ setTitles()

setTitles ( array  $titles = null)

Set tax rate titles

Parameters

Implements TaxRateInterface.

Definition at line 506 of file Rate.php.

507  {
508  return $this->setData(self::KEY_TITLES, $titles);
509  }

◆ setZipFrom()

setZipFrom (   $zipFrom)

Set zip range from

Parameters
int$zipFrom
Returns
$this

Implements TaxRateInterface.

Definition at line 462 of file Rate.php.

463  {
464  return $this->setData(self::KEY_ZIP_RANGE_FROM, $zipFrom);
465  }

◆ setZipIsRange()

setZipIsRange (   $zipIsRange)

Set zip is range

Parameters
int$zipIsRange
Returns
$this

Implements TaxRateInterface.

Definition at line 451 of file Rate.php.

452  {
453  return $this->setData(self::KEY_ZIP_IS_RANGE, $zipIsRange);
454  }

◆ setZipTo()

setZipTo (   $zipTo)

Set zip range to

Parameters
int$zipTo
Returns
$this

Implements TaxRateInterface.

Definition at line 473 of file Rate.php.

474  {
475  return $this->setData(self::KEY_ZIP_RANGE_TO, $zipTo);
476  }

Field Documentation

◆ $_regionFactory

$_regionFactory
protected

Definition at line 48 of file Rate.php.

◆ $_titleFactory

$_titleFactory
protected

Definition at line 53 of file Rate.php.

◆ $_titleModel

$_titleModel = null
protected

Definition at line 43 of file Rate.php.

◆ $_titles

$_titles = null
protected

#- #-

Definition at line 38 of file Rate.php.

◆ $directoryRegion

$directoryRegion
protected

Definition at line 57 of file Rate.php.

◆ KEY_CODE

const KEY_CODE = 'code'

Definition at line 33 of file Rate.php.

◆ KEY_COUNTRY_ID

const KEY_COUNTRY_ID = 'tax_country_id'

Definition at line 25 of file Rate.php.

◆ KEY_ID

const KEY_ID = 'id'

#+ Constants defined for keys of array, makes typos less likely

Definition at line 24 of file Rate.php.

◆ KEY_PERCENTAGE_RATE

const KEY_PERCENTAGE_RATE = 'rate'

Definition at line 32 of file Rate.php.

◆ KEY_POSTCODE

const KEY_POSTCODE = 'tax_postcode'

Definition at line 28 of file Rate.php.

◆ KEY_REGION_ID

const KEY_REGION_ID = 'tax_region_id'

Definition at line 26 of file Rate.php.

◆ KEY_REGION_NAME

const KEY_REGION_NAME = 'region_name'

Definition at line 27 of file Rate.php.

◆ KEY_TITLES

const KEY_TITLES = 'titles'

Definition at line 34 of file Rate.php.

◆ KEY_ZIP_IS_RANGE

const KEY_ZIP_IS_RANGE = 'zip_is_range'

Definition at line 29 of file Rate.php.

◆ KEY_ZIP_RANGE_FROM

const KEY_ZIP_RANGE_FROM = 'zip_from'

Definition at line 30 of file Rate.php.

◆ KEY_ZIP_RANGE_TO

const KEY_ZIP_RANGE_TO = 'zip_to'

Definition at line 31 of file Rate.php.


The documentation for this class was generated from the following file: