ClassModel.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model;
  7. use Magento\Framework\Api\AttributeValueFactory;
  8. use Magento\Framework\Exception\CouldNotDeleteException;
  9. /**
  10. * Tax class model
  11. */
  12. class ClassModel extends \Magento\Framework\Model\AbstractExtensibleModel implements
  13. \Magento\Tax\Api\Data\TaxClassInterface
  14. {
  15. /**#@+
  16. * Constants defined for keys of array, makes typos less likely
  17. */
  18. const KEY_ID = 'class_id';
  19. const KEY_NAME = 'class_name';
  20. const KEY_TYPE = 'class_type';
  21. /**#@-*/
  22. /**
  23. * Defines Customer Tax Class string
  24. */
  25. const TAX_CLASS_TYPE_CUSTOMER = 'CUSTOMER';
  26. /**
  27. * Defines Product Tax Class string
  28. */
  29. const TAX_CLASS_TYPE_PRODUCT = 'PRODUCT';
  30. /**
  31. * @var \Magento\Tax\Model\TaxClass\Factory
  32. */
  33. protected $_classFactory;
  34. /**
  35. * @param \Magento\Framework\Model\Context $context
  36. * @param \Magento\Framework\Registry $registry
  37. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  38. * @param AttributeValueFactory $customAttributeFactory
  39. * @param TaxClass\Factory $classFactory
  40. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  41. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  42. * @param array $data
  43. */
  44. public function __construct(
  45. \Magento\Framework\Model\Context $context,
  46. \Magento\Framework\Registry $registry,
  47. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  48. AttributeValueFactory $customAttributeFactory,
  49. \Magento\Tax\Model\TaxClass\Factory $classFactory,
  50. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  51. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  52. array $data = []
  53. ) {
  54. parent::__construct(
  55. $context,
  56. $registry,
  57. $extensionFactory,
  58. $customAttributeFactory,
  59. $resource,
  60. $resourceCollection,
  61. $data
  62. );
  63. $this->_classFactory = $classFactory;
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function _construct()
  69. {
  70. $this->_init(\Magento\Tax\Model\ResourceModel\TaxClass::class);
  71. }
  72. /**
  73. * Check whether this class can be deleted
  74. *
  75. * @return bool
  76. * @throws CouldNotDeleteException
  77. */
  78. protected function checkClassCanBeDeleted()
  79. {
  80. if (!$this->getId()) {
  81. throw new CouldNotDeleteException(__('This class no longer exists.'));
  82. }
  83. $typeModel = $this->_classFactory->create($this);
  84. if ($typeModel->getAssignedToRules()->getSize() > 0) {
  85. throw new CouldNotDeleteException(
  86. __(
  87. 'You cannot delete this tax class because it is used in Tax Rules.'
  88. . ' You have to delete the rules it is used in first.'
  89. )
  90. );
  91. }
  92. if ($typeModel->isAssignedToObjects()) {
  93. throw new CouldNotDeleteException(
  94. __(
  95. 'You cannot delete this tax class because it is used in existing %1(s).',
  96. $typeModel->getObjectTypeName()
  97. )
  98. );
  99. }
  100. return true;
  101. }
  102. /**
  103. * Validate tax class can be deleted
  104. *
  105. * @return $this
  106. * @throws \Magento\Framework\Exception\LocalizedException
  107. */
  108. public function beforeDelete()
  109. {
  110. $this->checkClassCanBeDeleted();
  111. return parent::beforeDelete();
  112. }
  113. /**
  114. * @codeCoverageIgnoreStart
  115. * {@inheritdoc}
  116. */
  117. public function getClassId()
  118. {
  119. return $this->getData(self::KEY_ID);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function getClassName()
  125. {
  126. return $this->getData(self::KEY_NAME);
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getClassType()
  132. {
  133. return $this->getData(self::KEY_TYPE);
  134. }
  135. /**
  136. * Set tax class ID.
  137. *
  138. * @param int $classId
  139. * @return $this
  140. */
  141. public function setClassId($classId)
  142. {
  143. return $this->setData(self::KEY_ID, $classId);
  144. }
  145. /**
  146. * Set tax class name.
  147. *
  148. * @param string $className
  149. * @return $this
  150. */
  151. public function setClassName($className)
  152. {
  153. return $this->setData(self::KEY_NAME, $className);
  154. }
  155. /**
  156. * Set tax class type.
  157. *
  158. * @param string $classType
  159. * @return $this
  160. */
  161. public function setClassType($classType)
  162. {
  163. return $this->setData(self::KEY_TYPE, $classType);
  164. }
  165. //@codeCoverageIgnoreEnd
  166. /**
  167. * {@inheritdoc}
  168. *
  169. * @return \Magento\Tax\Api\Data\TaxClassExtensionInterface|null
  170. */
  171. public function getExtensionAttributes()
  172. {
  173. return $this->_getExtensionAttributes();
  174. }
  175. /**
  176. * {@inheritdoc}
  177. *
  178. * @param \Magento\Tax\Api\Data\TaxClassExtensionInterface $extensionAttributes
  179. * @return $this
  180. */
  181. public function setExtensionAttributes(\Magento\Tax\Api\Data\TaxClassExtensionInterface $extensionAttributes)
  182. {
  183. return $this->_setExtensionAttributes($extensionAttributes);
  184. }
  185. }