Rule.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogRule\Model;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Catalog\Model\ProductFactory;
  9. use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
  10. use Magento\CatalogRule\Api\Data\RuleExtensionInterface;
  11. use Magento\CatalogRule\Api\Data\RuleInterface;
  12. use Magento\CatalogRule\Helper\Data;
  13. use Magento\CatalogRule\Model\Data\Condition\Converter;
  14. use Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor;
  15. use Magento\CatalogRule\Model\ResourceModel\Rule as RuleResourceModel;
  16. use Magento\CatalogRule\Model\Rule\Action\CollectionFactory as RuleCollectionFactory;
  17. use Magento\CatalogRule\Model\Rule\Condition\CombineFactory;
  18. use Magento\Customer\Model\Session;
  19. use Magento\Framework\Api\AttributeValueFactory;
  20. use Magento\Framework\Api\ExtensionAttributesFactory;
  21. use Magento\Framework\App\Cache\TypeListInterface;
  22. use Magento\Framework\App\ObjectManager;
  23. use Magento\Framework\Data\Collection\AbstractDb;
  24. use Magento\Framework\Data\FormFactory;
  25. use Magento\Framework\DataObject;
  26. use Magento\Framework\DataObject\IdentityInterface;
  27. use Magento\Framework\Model\Context;
  28. use Magento\Framework\Model\ResourceModel\AbstractResource;
  29. use Magento\Framework\Model\ResourceModel\Iterator;
  30. use Magento\Framework\Registry;
  31. use Magento\Framework\Serialize\Serializer\Json;
  32. use Magento\Framework\Stdlib\DateTime;
  33. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  34. use Magento\Store\Model\StoreManagerInterface;
  35. use Magento\CatalogRule\Model\ResourceModel\Product\ConditionsToCollectionApplier;
  36. /**
  37. * Catalog Rule data model
  38. *
  39. * @method \Magento\CatalogRule\Model\Rule setFromDate(string $value)
  40. * @method \Magento\CatalogRule\Model\Rule setToDate(string $value)
  41. * @method \Magento\CatalogRule\Model\Rule setCustomerGroupIds(string $value)
  42. * @method string getWebsiteIds()
  43. * @method \Magento\CatalogRule\Model\Rule setWebsiteIds(string $value)
  44. * @SuppressWarnings(PHPMD.TooManyFields)
  45. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  46. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  47. */
  48. class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, IdentityInterface
  49. {
  50. /**
  51. * Prefix of model events names
  52. *
  53. * @var string
  54. */
  55. protected $_eventPrefix = 'catalogrule_rule';
  56. /**
  57. * Parameter name in event
  58. *
  59. * In observe method you can use $observer->getEvent()->getRule() in this case
  60. *
  61. * @var string
  62. */
  63. protected $_eventObject = 'rule';
  64. /**
  65. * Store matched product Ids
  66. *
  67. * @var array
  68. */
  69. protected $_productIds;
  70. /**
  71. * Limitation for products collection
  72. *
  73. * @var int|array|null
  74. */
  75. protected $_productsFilter = null;
  76. /**
  77. * Store current date at "Y-m-d H:i:s" format
  78. *
  79. * @var string
  80. */
  81. protected $_now;
  82. /**
  83. * Cached data of prices calculated by price rules
  84. *
  85. * @var array
  86. */
  87. protected static $_priceRulesData = [];
  88. /**
  89. * Catalog rule data
  90. *
  91. * @var \Magento\CatalogRule\Helper\Data
  92. */
  93. protected $_catalogRuleData;
  94. /**
  95. * @var \Magento\Framework\App\Cache\TypeListInterface
  96. */
  97. protected $_cacheTypesList;
  98. /**
  99. * @var array
  100. */
  101. protected $_relatedCacheTypes;
  102. /**
  103. * @var \Magento\Framework\Stdlib\DateTime
  104. */
  105. protected $dateTime;
  106. /**
  107. * @var \Magento\Framework\Model\ResourceModel\Iterator
  108. */
  109. protected $_resourceIterator;
  110. /**
  111. * @var \Magento\Customer\Model\Session
  112. */
  113. protected $_customerSession;
  114. /**
  115. * @var \Magento\CatalogRule\Model\Rule\Condition\CombineFactory
  116. */
  117. protected $_combineFactory;
  118. /**
  119. * @var \Magento\CatalogRule\Model\Rule\Action\CollectionFactory
  120. */
  121. protected $_actionCollectionFactory;
  122. /**
  123. * @var \Magento\Catalog\Model\ProductFactory
  124. */
  125. protected $_productFactory;
  126. /**
  127. * @var \Magento\Store\Model\StoreManagerInterface
  128. */
  129. protected $_storeManager;
  130. /**
  131. * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
  132. */
  133. protected $_productCollectionFactory;
  134. /**
  135. * @var \Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor;
  136. */
  137. protected $_ruleProductProcessor;
  138. /**
  139. * @var Data\Condition\Converter
  140. */
  141. protected $ruleConditionConverter;
  142. /**
  143. * @var ConditionsToCollectionApplier
  144. */
  145. private $conditionsToCollectionApplier;
  146. /**
  147. * @var array
  148. */
  149. private $websitesMap;
  150. /**
  151. * @var RuleResourceModel
  152. */
  153. private $ruleResourceModel;
  154. /**
  155. * Rule constructor
  156. *
  157. * @param \Magento\Framework\Model\Context $context
  158. * @param \Magento\Framework\Registry $registry
  159. * @param \Magento\Framework\Data\FormFactory $formFactory
  160. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  161. * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
  162. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  163. * @param Rule\Condition\CombineFactory $combineFactory
  164. * @param Rule\Action\CollectionFactory $actionCollectionFactory
  165. * @param \Magento\Catalog\Model\ProductFactory $productFactory
  166. * @param \Magento\Framework\Model\ResourceModel\Iterator $resourceIterator
  167. * @param \Magento\Customer\Model\Session $customerSession
  168. * @param \Magento\CatalogRule\Helper\Data $catalogRuleData
  169. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypesList
  170. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  171. * @param Indexer\Rule\RuleProductProcessor $ruleProductProcessor
  172. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  173. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  174. * @param array $relatedCacheTypes
  175. * @param array $data
  176. * @param ExtensionAttributesFactory|null $extensionFactory
  177. * @param AttributeValueFactory|null $customAttributeFactory
  178. * @param \Magento\Framework\Serialize\Serializer\Json $serializer
  179. * @param \Magento\CatalogRule\Model\ResourceModel\RuleResourceModel|null $ruleResourceModel
  180. * @param ConditionsToCollectionApplier $conditionsToCollectionApplier
  181. *
  182. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  183. */
  184. public function __construct(
  185. Context $context,
  186. Registry $registry,
  187. FormFactory $formFactory,
  188. TimezoneInterface $localeDate,
  189. CollectionFactory $productCollectionFactory,
  190. StoreManagerInterface $storeManager,
  191. CombineFactory $combineFactory,
  192. RuleCollectionFactory $actionCollectionFactory,
  193. ProductFactory $productFactory,
  194. Iterator $resourceIterator,
  195. Session $customerSession,
  196. Data $catalogRuleData,
  197. TypeListInterface $cacheTypesList,
  198. DateTime $dateTime,
  199. RuleProductProcessor $ruleProductProcessor,
  200. AbstractResource $resource = null,
  201. AbstractDb $resourceCollection = null,
  202. array $relatedCacheTypes = [],
  203. array $data = [],
  204. ExtensionAttributesFactory $extensionFactory = null,
  205. AttributeValueFactory $customAttributeFactory = null,
  206. Json $serializer = null,
  207. RuleResourceModel $ruleResourceModel = null,
  208. ConditionsToCollectionApplier $conditionsToCollectionApplier = null
  209. ) {
  210. $this->_productCollectionFactory = $productCollectionFactory;
  211. $this->_storeManager = $storeManager;
  212. $this->_combineFactory = $combineFactory;
  213. $this->_actionCollectionFactory = $actionCollectionFactory;
  214. $this->_productFactory = $productFactory;
  215. $this->_resourceIterator = $resourceIterator;
  216. $this->_customerSession = $customerSession;
  217. $this->_catalogRuleData = $catalogRuleData;
  218. $this->_cacheTypesList = $cacheTypesList;
  219. $this->_relatedCacheTypes = $relatedCacheTypes;
  220. $this->dateTime = $dateTime;
  221. $this->_ruleProductProcessor = $ruleProductProcessor;
  222. $this->ruleResourceModel = $ruleResourceModel ?: ObjectManager::getInstance()->get(RuleResourceModel::class);
  223. $this->conditionsToCollectionApplier = $conditionsToCollectionApplier
  224. ?? ObjectManager::getInstance()->get(ConditionsToCollectionApplier::class);
  225. parent::__construct(
  226. $context,
  227. $registry,
  228. $formFactory,
  229. $localeDate,
  230. $resource,
  231. $resourceCollection,
  232. $data,
  233. $extensionFactory,
  234. $customAttributeFactory,
  235. $serializer
  236. );
  237. }
  238. /**
  239. * Init resource model and id field
  240. *
  241. * @return void
  242. */
  243. protected function _construct()
  244. {
  245. parent::_construct();
  246. $this->_init(RuleResourceModel::class);
  247. $this->setIdFieldName('rule_id');
  248. }
  249. /**
  250. * Getter for rule conditions collection
  251. *
  252. * @return \Magento\Rule\Model\Condition\Combine
  253. */
  254. public function getConditionsInstance()
  255. {
  256. return $this->_combineFactory->create();
  257. }
  258. /**
  259. * Getter for rule actions collection
  260. *
  261. * @return \Magento\CatalogRule\Model\Rule\Action\Collection
  262. */
  263. public function getActionsInstance()
  264. {
  265. return $this->_actionCollectionFactory->create();
  266. }
  267. /**
  268. * Get catalog rule customer group Ids
  269. *
  270. * @return array|null
  271. */
  272. public function getCustomerGroupIds()
  273. {
  274. if (!$this->hasCustomerGroupIds()) {
  275. $customerGroupIds = $this->ruleResourceModel->getCustomerGroupIds($this->getId());
  276. $this->setData('customer_group_ids', (array)$customerGroupIds);
  277. }
  278. return $this->_getData('customer_group_ids');
  279. }
  280. /**
  281. * Retrieve current date for current rule
  282. *
  283. * @return string
  284. */
  285. public function getNow()
  286. {
  287. if (!$this->_now) {
  288. return (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT);
  289. }
  290. return $this->_now;
  291. }
  292. /**
  293. * Set current date for current rule
  294. *
  295. * @param string $now
  296. * @return void
  297. * @codeCoverageIgnore
  298. */
  299. public function setNow($now)
  300. {
  301. $this->_now = $now;
  302. }
  303. /**
  304. * Get array of product ids which are matched by rule
  305. *
  306. * @return array
  307. */
  308. public function getMatchingProductIds()
  309. {
  310. if ($this->_productIds === null) {
  311. $this->_productIds = [];
  312. $this->setCollectedAttributes([]);
  313. if ($this->getWebsiteIds()) {
  314. /** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */
  315. $productCollection = $this->_productCollectionFactory->create();
  316. $productCollection->addWebsiteFilter($this->getWebsiteIds());
  317. if ($this->_productsFilter) {
  318. $productCollection->addIdFilter($this->_productsFilter);
  319. }
  320. $this->getConditions()->collectValidatedAttributes($productCollection);
  321. if ($this->canPreMapProducts()) {
  322. $productCollection = $this->conditionsToCollectionApplier
  323. ->applyConditionsToCollection($this->getConditions(), $productCollection);
  324. }
  325. $this->_resourceIterator->walk(
  326. $productCollection->getSelect(),
  327. [[$this, 'callbackValidateProduct']],
  328. [
  329. 'attributes' => $this->getCollectedAttributes(),
  330. 'product' => $this->_productFactory->create()
  331. ]
  332. );
  333. }
  334. }
  335. return $this->_productIds;
  336. }
  337. /**
  338. * Check if we can use mapping for rule conditions
  339. *
  340. * @return bool
  341. */
  342. private function canPreMapProducts()
  343. {
  344. $conditions = $this->getConditions();
  345. // No need to map products if there is no conditions in rule
  346. if (!$conditions || !$conditions->getConditions()) {
  347. return false;
  348. }
  349. return true;
  350. }
  351. /**
  352. * Callback function for product matching
  353. *
  354. * @param array $args
  355. * @return void
  356. */
  357. public function callbackValidateProduct($args)
  358. {
  359. $product = clone $args['product'];
  360. $product->setData($args['row']);
  361. $websites = $this->_getWebsitesMap();
  362. $results = [];
  363. foreach ($websites as $websiteId => $defaultStoreId) {
  364. $product->setStoreId($defaultStoreId);
  365. $results[$websiteId] = $this->getConditions()->validate($product);
  366. }
  367. $this->_productIds[$product->getId()] = $results;
  368. }
  369. /**
  370. * Prepare website map
  371. *
  372. * @return array
  373. */
  374. protected function _getWebsitesMap()
  375. {
  376. if ($this->websitesMap === null) {
  377. $this->websitesMap = [];
  378. $websites = $this->_storeManager->getWebsites();
  379. foreach ($websites as $website) {
  380. // Continue if website has no store to be able to create catalog rule for website without store
  381. if ($website->getDefaultStore() === null) {
  382. continue;
  383. }
  384. $this->websitesMap[$website->getId()] = $website->getDefaultStore()->getId();
  385. }
  386. }
  387. return $this->websitesMap;
  388. }
  389. /**
  390. * {@inheritdoc}
  391. */
  392. public function validateData(DataObject $dataObject)
  393. {
  394. $result = parent::validateData($dataObject);
  395. if ($result === true) {
  396. $result = [];
  397. }
  398. $action = $dataObject->getData('simple_action');
  399. $discount = $dataObject->getData('discount_amount');
  400. $result = array_merge($result, $this->validateDiscount($action, $discount));
  401. return !empty($result) ? $result : true;
  402. }
  403. /**
  404. * Validate discount based on action
  405. *
  406. * @param string $action
  407. * @param string|int|float $discount
  408. *
  409. * @return array Validation errors
  410. */
  411. protected function validateDiscount($action, $discount)
  412. {
  413. $result = [];
  414. switch ($action) {
  415. case 'by_percent':
  416. case 'to_percent':
  417. if ($discount < 0 || $discount > 100) {
  418. $result[] = __('Percentage discount should be between 0 and 100.');
  419. }
  420. break;
  421. case 'by_fixed':
  422. case 'to_fixed':
  423. if ($discount < 0) {
  424. $result[] = __('Discount value should be 0 or greater.');
  425. }
  426. break;
  427. default:
  428. $result[] = __('Unknown action.');
  429. }
  430. return $result;
  431. }
  432. /**
  433. * Calculate price using catalog price rule of product
  434. *
  435. * @param Product $product
  436. * @param float $price
  437. * @return float|null
  438. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  439. */
  440. public function calcProductPriceRule(Product $product, $price)
  441. {
  442. $priceRules = null;
  443. $productId = $product->getId();
  444. $storeId = $product->getStoreId();
  445. $websiteId = $this->_storeManager->getStore($storeId)->getWebsiteId();
  446. if ($product->hasCustomerGroupId()) {
  447. $customerGroupId = $product->getCustomerGroupId();
  448. } else {
  449. $customerGroupId = $this->_customerSession->getCustomerGroupId();
  450. }
  451. $dateTs = $this->_localeDate->scopeTimeStamp($storeId);
  452. $cacheKey = date('Y-m-d', $dateTs) . "|{$websiteId}|{$customerGroupId}|{$productId}|{$price}";
  453. if (!array_key_exists($cacheKey, self::$_priceRulesData)) {
  454. $rulesData = $this->_getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
  455. if ($rulesData) {
  456. foreach ($rulesData as $ruleData) {
  457. if ($product->getParentId()) {
  458. $priceRules = $priceRules ? $priceRules : $price;
  459. if ($ruleData['action_stop']) {
  460. break;
  461. }
  462. } else {
  463. $priceRules = $this->_catalogRuleData->calcPriceRule(
  464. $ruleData['action_operator'],
  465. $ruleData['action_amount'],
  466. $priceRules ? $priceRules : $price
  467. );
  468. if ($ruleData['action_stop']) {
  469. break;
  470. }
  471. }
  472. }
  473. return self::$_priceRulesData[$cacheKey] = $priceRules;
  474. } else {
  475. self::$_priceRulesData[$cacheKey] = null;
  476. }
  477. } else {
  478. return self::$_priceRulesData[$cacheKey];
  479. }
  480. return null;
  481. }
  482. /**
  483. * Get rules from product
  484. *
  485. * @param string $dateTs
  486. * @param int $websiteId
  487. * @param array $customerGroupId
  488. * @param int $productId
  489. * @return array
  490. */
  491. protected function _getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId)
  492. {
  493. return $this->ruleResourceModel->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
  494. }
  495. /**
  496. * Filtering products that must be checked for matching with rule
  497. *
  498. * @param int|array $productIds
  499. * @return void
  500. * @codeCoverageIgnore
  501. */
  502. public function setProductsFilter($productIds)
  503. {
  504. $this->_productsFilter = $productIds;
  505. }
  506. /**
  507. * Returns products filter
  508. *
  509. * @return array|int|null
  510. * @codeCoverageIgnore
  511. */
  512. public function getProductsFilter()
  513. {
  514. return $this->_productsFilter;
  515. }
  516. /**
  517. * Invalidate related cache types
  518. *
  519. * @return $this
  520. */
  521. protected function _invalidateCache()
  522. {
  523. if (count($this->_relatedCacheTypes)) {
  524. $this->_cacheTypesList->invalidate($this->_relatedCacheTypes);
  525. }
  526. return $this;
  527. }
  528. /**
  529. * {@inheritdoc}
  530. *
  531. * @return $this
  532. */
  533. public function afterSave()
  534. {
  535. if ($this->isObjectNew() && !$this->_ruleProductProcessor->isIndexerScheduled()) {
  536. $productIds = $this->getMatchingProductIds();
  537. if (!empty($productIds) && is_array($productIds)) {
  538. $this->ruleResourceModel->addCommitCallback([$this, 'reindex']);
  539. }
  540. } else {
  541. $this->_ruleProductProcessor->getIndexer()->invalidate();
  542. }
  543. return parent::afterSave();
  544. }
  545. /**
  546. * Init indexing process after rule save
  547. *
  548. * @return void
  549. */
  550. public function reindex()
  551. {
  552. $productIds = $this->_productIds ? array_keys(array_filter($this->_productIds, function (array $data) {
  553. return array_filter($data);
  554. })) : [];
  555. $this->_ruleProductProcessor->reindexList($productIds);
  556. }
  557. /**
  558. * {@inheritdoc}
  559. *
  560. * @return $this
  561. */
  562. public function afterDelete()
  563. {
  564. $this->_ruleProductProcessor->getIndexer()->invalidate();
  565. return parent::afterDelete();
  566. }
  567. /**
  568. * Check if rule behavior changed
  569. *
  570. * @return bool
  571. */
  572. public function isRuleBehaviorChanged()
  573. {
  574. if (!$this->isObjectNew()) {
  575. $arrayDiff = $this->dataDiff($this->getOrigData(), $this->getStoredData());
  576. unset($arrayDiff['name']);
  577. unset($arrayDiff['description']);
  578. if (empty($arrayDiff)) {
  579. return false;
  580. }
  581. }
  582. return true;
  583. }
  584. /**
  585. * Get array with data differences
  586. * @param array $array1
  587. * @param array $array2
  588. *
  589. * @return array
  590. */
  591. protected function dataDiff($array1, $array2)
  592. {
  593. $result = [];
  594. foreach ($array1 as $key => $value) {
  595. if (array_key_exists($key, $array2)) {
  596. if ($value != $array2[$key]) {
  597. $result[$key] = true;
  598. }
  599. } else {
  600. $result[$key] = true;
  601. }
  602. }
  603. return $result;
  604. }
  605. /**
  606. * @param string $formName
  607. * @return string
  608. */
  609. public function getConditionsFieldSetId($formName = '')
  610. {
  611. return $formName . 'rule_conditions_fieldset_' . $this->getId();
  612. }
  613. //@codeCoverageIgnoreStart
  614. /**
  615. * {@inheritdoc}
  616. */
  617. public function getRuleId()
  618. {
  619. return $this->getData(self::RULE_ID);
  620. }
  621. /**
  622. * {@inheritdoc}
  623. */
  624. public function setRuleId($ruleId)
  625. {
  626. return $this->setData(self::RULE_ID, $ruleId);
  627. }
  628. /**
  629. * {@inheritdoc}
  630. */
  631. public function getName()
  632. {
  633. return $this->getData(self::NAME);
  634. }
  635. /**
  636. * {@inheritdoc}
  637. */
  638. public function setName($name)
  639. {
  640. return $this->setData(self::NAME, $name);
  641. }
  642. /**
  643. * {@inheritdoc}
  644. */
  645. public function getDescription()
  646. {
  647. return $this->getData(self::DESCRIPTION);
  648. }
  649. /**
  650. * {@inheritdoc}
  651. */
  652. public function setDescription($description)
  653. {
  654. return $this->setData(self::DESCRIPTION, $description);
  655. }
  656. /**
  657. * {@inheritdoc}
  658. */
  659. public function getIsActive()
  660. {
  661. return $this->getData(self::IS_ACTIVE);
  662. }
  663. /**
  664. * {@inheritdoc}
  665. */
  666. public function setIsActive($isActive)
  667. {
  668. return $this->setData(self::IS_ACTIVE, $isActive);
  669. }
  670. /**
  671. * {@inheritdoc}
  672. */
  673. public function getRuleCondition()
  674. {
  675. return $this->getRuleConditionConverter()->arrayToDataModel($this->getConditions()->asArray());
  676. }
  677. /**
  678. * {@inheritdoc}
  679. */
  680. public function setRuleCondition($condition)
  681. {
  682. $this->getConditions()
  683. ->setConditions([])
  684. ->loadArray($this->getRuleConditionConverter()->dataModelToArray($condition));
  685. return $this;
  686. }
  687. /**
  688. * {@inheritdoc}
  689. */
  690. public function getStopRulesProcessing()
  691. {
  692. return $this->getData(self::STOP_RULES_PROCESSING);
  693. }
  694. /**
  695. * {@inheritdoc}
  696. */
  697. public function setStopRulesProcessing($isStopProcessing)
  698. {
  699. return $this->setData(self::STOP_RULES_PROCESSING, $isStopProcessing);
  700. }
  701. /**
  702. * {@inheritdoc}
  703. */
  704. public function getSortOrder()
  705. {
  706. return $this->getData(self::SORT_ORDER);
  707. }
  708. /**
  709. * {@inheritdoc}
  710. */
  711. public function setSortOrder($sortOrder)
  712. {
  713. return $this->setData(self::SORT_ORDER, $sortOrder);
  714. }
  715. /**
  716. * {@inheritdoc}
  717. */
  718. public function getSimpleAction()
  719. {
  720. return $this->getData(self::SIMPLE_ACTION);
  721. }
  722. /**
  723. * {@inheritdoc}
  724. */
  725. public function setSimpleAction($action)
  726. {
  727. return $this->setData(self::SIMPLE_ACTION, $action);
  728. }
  729. /**
  730. * {@inheritdoc}
  731. */
  732. public function getDiscountAmount()
  733. {
  734. return $this->getData(self::DISCOUNT_AMOUNT);
  735. }
  736. /**
  737. * {@inheritdoc}
  738. */
  739. public function setDiscountAmount($amount)
  740. {
  741. return $this->setData(self::DISCOUNT_AMOUNT, $amount);
  742. }
  743. /**
  744. * @return string
  745. */
  746. public function getFromDate()
  747. {
  748. return $this->getData('from_date');
  749. }
  750. /**
  751. * @return string
  752. */
  753. public function getToDate()
  754. {
  755. return $this->getData('to_date');
  756. }
  757. /**
  758. * {@inheritdoc}
  759. *
  760. * @return \Magento\CatalogRule\Api\Data\RuleExtensionInterface|null
  761. */
  762. public function getExtensionAttributes()
  763. {
  764. return $this->_getExtensionAttributes();
  765. }
  766. /**
  767. * {@inheritdoc}
  768. *
  769. * @param \Magento\CatalogRule\Api\Data\RuleExtensionInterface $extensionAttributes
  770. * @return $this
  771. */
  772. public function setExtensionAttributes(RuleExtensionInterface $extensionAttributes)
  773. {
  774. return $this->_setExtensionAttributes($extensionAttributes);
  775. }
  776. /**
  777. * @return Data\Condition\Converter
  778. * @deprecated 100.1.0
  779. */
  780. private function getRuleConditionConverter()
  781. {
  782. if (null === $this->ruleConditionConverter) {
  783. $this->ruleConditionConverter = ObjectManager::getInstance()
  784. ->get(Converter::class);
  785. }
  786. return $this->ruleConditionConverter;
  787. }
  788. //@codeCoverageIgnoreEnd
  789. /**
  790. * @inheritDoc
  791. */
  792. public function getIdentities()
  793. {
  794. return ['price'];
  795. }
  796. }