Rule.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\Calculation;
  7. use Magento\Framework\Api\AttributeValueFactory;
  8. use Magento\Framework\Api\ExtensionAttributesFactory;
  9. use Magento\Tax\Api\Data\TaxRuleInterface;
  10. /**
  11. * Tax Rule Model
  12. */
  13. class Rule extends \Magento\Framework\Model\AbstractExtensibleModel implements TaxRuleInterface
  14. {
  15. /**#@+
  16. *
  17. * Tax rule field key.
  18. */
  19. const KEY_ID = 'id';
  20. const KEY_CODE = 'code';
  21. const KEY_PRIORITY = 'priority';
  22. const KEY_POSITION = 'position';
  23. const KEY_CUSTOMER_TAX_CLASS_IDS = 'customer_tax_class_ids';
  24. const KEY_PRODUCT_TAX_CLASS_IDS = 'product_tax_class_ids';
  25. const KEY_TAX_RATE_IDS = 'tax_rate_ids';
  26. const KEY_CALCULATE_SUBTOTAL = 'calculate_subtotal';
  27. /**#@-*/
  28. /**#@-*/
  29. protected $_eventPrefix = 'tax_rule';
  30. /**
  31. * Tax Model Class
  32. *
  33. * @var \Magento\Tax\Model\ClassModel
  34. */
  35. protected $_taxClass;
  36. /**
  37. * @var \Magento\Tax\Model\Calculation
  38. */
  39. protected $_calculation;
  40. /**
  41. * @var \Magento\Tax\Model\Calculation\Rule\Validator
  42. */
  43. protected $validator;
  44. /**
  45. * Name of object id field
  46. *
  47. * @var string
  48. */
  49. protected $_idFieldName = 'tax_calculation_rule_id';
  50. /**
  51. * @param \Magento\Framework\Model\Context $context
  52. * @param \Magento\Framework\Registry $registry
  53. * @param ExtensionAttributesFactory $extensionFactory
  54. * @param AttributeValueFactory $customAttributeFactory
  55. * @param \Magento\Tax\Model\ClassModel $taxClass
  56. * @param \Magento\Tax\Model\Calculation $calculation
  57. * @param Rule\Validator $validator
  58. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  59. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  60. * @param array $data
  61. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  62. */
  63. public function __construct(
  64. \Magento\Framework\Model\Context $context,
  65. \Magento\Framework\Registry $registry,
  66. ExtensionAttributesFactory $extensionFactory,
  67. AttributeValueFactory $customAttributeFactory,
  68. \Magento\Tax\Model\ClassModel $taxClass,
  69. \Magento\Tax\Model\Calculation $calculation,
  70. \Magento\Tax\Model\Calculation\Rule\Validator $validator,
  71. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  72. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  73. array $data = []
  74. ) {
  75. $this->_calculation = $calculation;
  76. $this->validator = $validator;
  77. parent::__construct(
  78. $context,
  79. $registry,
  80. $extensionFactory,
  81. $customAttributeFactory,
  82. $resource,
  83. $resourceCollection,
  84. $data
  85. );
  86. $this->_init(\Magento\Tax\Model\ResourceModel\Calculation\Rule::class);
  87. $this->_taxClass = $taxClass;
  88. }
  89. /**
  90. * After save rule
  91. * Re-declared for populate rate calculations
  92. *
  93. * @return $this
  94. */
  95. public function afterSave()
  96. {
  97. parent::afterSave();
  98. $this->saveCalculationData();
  99. $this->_eventManager->dispatch('tax_settings_change_after');
  100. return $this;
  101. }
  102. /**
  103. * After rule delete
  104. * Re-declared for dispatch tax_settings_change_after event
  105. *
  106. * @return $this
  107. */
  108. public function afterDelete()
  109. {
  110. $this->_eventManager->dispatch('tax_settings_change_after');
  111. return parent::afterDelete();
  112. }
  113. /**
  114. * @return void
  115. */
  116. public function saveCalculationData()
  117. {
  118. $ctc = $this->getData('customer_tax_class_ids');
  119. $ptc = $this->getData('product_tax_class_ids');
  120. $rates = $this->getData('tax_rate_ids');
  121. $this->_calculation->deleteByRuleId($this->getId());
  122. foreach ($ctc as $c) {
  123. foreach ($ptc as $p) {
  124. foreach ($rates as $r) {
  125. $dataArray = [
  126. 'tax_calculation_rule_id' => $this->getId(),
  127. 'tax_calculation_rate_id' => $r,
  128. 'customer_tax_class_id' => $c,
  129. 'product_tax_class_id' => $p,
  130. ];
  131. $this->_calculation->setData($dataArray)->save();
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * @return \Magento\Tax\Model\Calculation
  138. */
  139. public function getCalculationModel()
  140. {
  141. return $this->_calculation;
  142. }
  143. /**
  144. * @return array
  145. */
  146. public function getRates()
  147. {
  148. return $this->getCalculationModel()->getRates($this->getId());
  149. }
  150. /**
  151. * @return array
  152. */
  153. public function getCustomerTaxClasses()
  154. {
  155. return $this->getCalculationModel()->getCustomerTaxClasses($this->getId());
  156. }
  157. /**
  158. * @return array
  159. */
  160. public function getProductTaxClasses()
  161. {
  162. return $this->getCalculationModel()->getProductTaxClasses($this->getId());
  163. }
  164. /**
  165. * Fetches rules by rate, customer tax class and product tax class
  166. * and product tax class combination
  167. *
  168. * @param array $rateId
  169. * @param array $customerTaxClassIds
  170. * @param array $productTaxClassIds
  171. * @return array
  172. */
  173. public function fetchRuleCodes($rateId, $customerTaxClassIds, $productTaxClassIds)
  174. {
  175. return $this->getResource()->fetchRuleCodes($rateId, $customerTaxClassIds, $productTaxClassIds);
  176. }
  177. /**
  178. * @codeCoverageIgnoreStart
  179. * {@inheritdoc}
  180. */
  181. public function getCode()
  182. {
  183. return $this->getData(self::KEY_CODE);
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function getPosition()
  189. {
  190. return (int) $this->getData(self::KEY_POSITION);
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function getCalculateSubtotal()
  196. {
  197. return (bool) $this->getData(self::KEY_CALCULATE_SUBTOTAL);
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function getPriority()
  203. {
  204. return $this->getData(self::KEY_PRIORITY);
  205. }
  206. //@codeCoverageIgnoreEnd
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function getCustomerTaxClassIds()
  211. {
  212. $ids = $this->getData(self::KEY_CUSTOMER_TAX_CLASS_IDS);
  213. if (null === $ids) {
  214. $ids = $this->_getUniqueValues($this->getCustomerTaxClasses());
  215. $this->setData(self::KEY_CUSTOMER_TAX_CLASS_IDS, $ids);
  216. }
  217. return $ids;
  218. }
  219. /**
  220. * {@inheritdoc}
  221. */
  222. public function getProductTaxClassIds()
  223. {
  224. $ids = $this->getData(self::KEY_PRODUCT_TAX_CLASS_IDS);
  225. if (null === $ids) {
  226. $ids = $this->_getUniqueValues($this->getProductTaxClasses());
  227. $this->setData(self::KEY_PRODUCT_TAX_CLASS_IDS, $ids);
  228. }
  229. return $ids;
  230. }
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function getTaxRateIds()
  235. {
  236. $ids = $this->getData(self::KEY_TAX_RATE_IDS);
  237. if (null === $ids) {
  238. $ids = $this->_getUniqueValues($this->getRates());
  239. $this->setData(self::KEY_TAX_RATE_IDS, $ids);
  240. }
  241. return $ids;
  242. }
  243. /**
  244. * Get unique values of indexed array.
  245. *
  246. * @param array|null $values
  247. * @return array|null
  248. */
  249. protected function _getUniqueValues($values)
  250. {
  251. if (!$values) {
  252. return null;
  253. }
  254. return array_values(array_unique($values));
  255. }
  256. /**
  257. * {@inheritdoc}
  258. */
  259. protected function _getValidationRulesBeforeSave()
  260. {
  261. return $this->validator;
  262. }
  263. /**
  264. * Set tax rule code
  265. *
  266. * @param string $code
  267. * @return $this
  268. */
  269. public function setCode($code)
  270. {
  271. return $this->setData(self::KEY_CODE, $code);
  272. }
  273. /**
  274. * Set priority
  275. *
  276. * @param int $priority
  277. * @return $this
  278. */
  279. public function setPriority($priority)
  280. {
  281. return $this->setData(self::KEY_PRIORITY, $priority);
  282. }
  283. /**
  284. * Set sort order.
  285. *
  286. * @param int $position
  287. * @return $this
  288. */
  289. public function setPosition($position)
  290. {
  291. return $this->setData(self::KEY_POSITION, $position);
  292. }
  293. /**
  294. * Set customer tax class id
  295. *
  296. * @param int[] $customerTaxClassIds
  297. * @return $this
  298. */
  299. public function setCustomerTaxClassIds(array $customerTaxClassIds = null)
  300. {
  301. return $this->setData(self::KEY_CUSTOMER_TAX_CLASS_IDS, $customerTaxClassIds);
  302. }
  303. /**
  304. * Set product tax class id
  305. *
  306. * @param int[] $productTaxClassIds
  307. * @return $this
  308. */
  309. public function setProductTaxClassIds(array $productTaxClassIds = null)
  310. {
  311. return $this->setData(self::KEY_PRODUCT_TAX_CLASS_IDS, $productTaxClassIds);
  312. }
  313. /**
  314. * Set tax rate ids
  315. *
  316. * @param int[] $taxRateIds
  317. * @return $this
  318. */
  319. public function setTaxRateIds(array $taxRateIds = null)
  320. {
  321. return $this->setData(self::KEY_TAX_RATE_IDS, $taxRateIds);
  322. }
  323. /**
  324. * Set calculate subtotal.
  325. *
  326. * @param bool $calculateSubtotal
  327. * @return $this
  328. */
  329. public function setCalculateSubtotal($calculateSubtotal)
  330. {
  331. return $this->setData(self::KEY_CALCULATE_SUBTOTAL, $calculateSubtotal);
  332. }
  333. /**
  334. * {@inheritdoc}
  335. *
  336. * @return \Magento\Tax\Api\Data\TaxRuleExtensionInterface|null
  337. */
  338. public function getExtensionAttributes()
  339. {
  340. return $this->_getExtensionAttributes();
  341. }
  342. /**
  343. * {@inheritdoc}
  344. *
  345. * @param \Magento\Tax\Api\Data\TaxRuleExtensionInterface $extensionAttributes
  346. * @return $this
  347. */
  348. public function setExtensionAttributes(\Magento\Tax\Api\Data\TaxRuleExtensionInterface $extensionAttributes)
  349. {
  350. return $this->_setExtensionAttributes($extensionAttributes);
  351. }
  352. }