Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClassModel.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Tax\Model;
8 
11 
17 {
21  const KEY_ID = 'class_id';
22  const KEY_NAME = 'class_name';
23  const KEY_TYPE = 'class_type';
29  const TAX_CLASS_TYPE_CUSTOMER = 'CUSTOMER';
30 
34  const TAX_CLASS_TYPE_PRODUCT = 'PRODUCT';
35 
39  protected $_classFactory;
40 
51  public function __construct(
52  \Magento\Framework\Model\Context $context,
53  \Magento\Framework\Registry $registry,
54  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
56  \Magento\Tax\Model\TaxClass\Factory $classFactory,
57  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
58  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
59  array $data = []
60  ) {
61  parent::__construct(
62  $context,
63  $registry,
64  $extensionFactory,
66  $resource,
67  $resourceCollection,
68  $data
69  );
70  $this->_classFactory = $classFactory;
71  }
72 
76  public function _construct()
77  {
78  $this->_init(\Magento\Tax\Model\ResourceModel\TaxClass::class);
79  }
80 
87  protected function checkClassCanBeDeleted()
88  {
89  if (!$this->getId()) {
90  throw new CouldNotDeleteException(__('This class no longer exists.'));
91  }
92 
93  $typeModel = $this->_classFactory->create($this);
94 
95  if ($typeModel->getAssignedToRules()->getSize() > 0) {
96  throw new CouldNotDeleteException(
97  __(
98  'You cannot delete this tax class because it is used in Tax Rules.'
99  . ' You have to delete the rules it is used in first.'
100  )
101  );
102  }
103 
104  if ($typeModel->isAssignedToObjects()) {
105  throw new CouldNotDeleteException(
106  __(
107  'You cannot delete this tax class because it is used in existing %1(s).',
108  $typeModel->getObjectTypeName()
109  )
110  );
111  }
112 
113  return true;
114  }
115 
122  public function beforeDelete()
123  {
124  $this->checkClassCanBeDeleted();
125  return parent::beforeDelete();
126  }
127 
132  public function getClassId()
133  {
134  return $this->getData(self::KEY_ID);
135  }
136 
140  public function getClassName()
141  {
142  return $this->getData(self::KEY_NAME);
143  }
144 
148  public function getClassType()
149  {
150  return $this->getData(self::KEY_TYPE);
151  }
152 
159  public function setClassId($classId)
160  {
161  return $this->setData(self::KEY_ID, $classId);
162  }
163 
170  public function setClassName($className)
171  {
172  return $this->setData(self::KEY_NAME, $className);
173  }
174 
181  public function setClassType($classType)
182  {
183  return $this->setData(self::KEY_TYPE, $classType);
184  }
185 
186  //@codeCoverageIgnoreEnd
187 
193  public function getExtensionAttributes()
194  {
195  return $this->_getExtensionAttributes();
196  }
197 
204  public function setExtensionAttributes(\Magento\Tax\Api\Data\TaxClassExtensionInterface $extensionAttributes)
205  {
206  return $this->_setExtensionAttributes($extensionAttributes);
207  }
208 }
setExtensionAttributes(\Magento\Tax\Api\Data\TaxClassExtensionInterface $extensionAttributes)
Definition: ClassModel.php:204
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Tax\Model\TaxClass\Factory $classFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: ClassModel.php:51
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31