PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/module-catalog-rule/Model/ResourceModel/Rule.php

https://bitbucket.org/sergiu-tot-fb/vendors
PHP | 284 lines | 127 code | 27 blank | 130 comment | 4 complexity | f4ec0ff3c4be1a561eaa95ff8ca606ff MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Catalog rules resource model
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\CatalogRule\Model\ResourceModel;
  12. use Magento\Catalog\Model\Product;
  13. use Magento\Framework\Model\AbstractModel;
  14. use Magento\Framework\Pricing\PriceCurrencyInterface;
  15. /**
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class Rule extends \Magento\Rule\Model\ResourceModel\AbstractResource
  19. {
  20. /**
  21. * Store number of seconds in a day
  22. */
  23. const SECONDS_IN_DAY = 86400;
  24. /**
  25. * @var \Psr\Log\LoggerInterface
  26. */
  27. protected $_logger;
  28. /**
  29. * Catalog rule data
  30. *
  31. * @var \Magento\CatalogRule\Helper\Data
  32. */
  33. protected $_catalogRuleData = null;
  34. /**
  35. * Core event manager proxy
  36. *
  37. * @var \Magento\Framework\Event\ManagerInterface
  38. */
  39. protected $_eventManager = null;
  40. /**
  41. * @var \Magento\Eav\Model\Config
  42. */
  43. protected $_eavConfig;
  44. /**
  45. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  46. */
  47. protected $_coreDate;
  48. /**
  49. * @var \Magento\Catalog\Model\Product\ConditionFactory
  50. */
  51. protected $_conditionFactory;
  52. /**
  53. * @var \Magento\Store\Model\StoreManagerInterface
  54. */
  55. protected $_storeManager;
  56. /**
  57. * @var \Magento\Framework\Stdlib\DateTime
  58. */
  59. protected $dateTime;
  60. /**
  61. * @var PriceCurrencyInterface
  62. */
  63. protected $priceCurrency;
  64. /**
  65. * @var \Magento\Framework\EntityManager\EntityManager
  66. */
  67. protected $entityManager;
  68. /**
  69. * Rule constructor.
  70. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  71. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  72. * @param Product\ConditionFactory $conditionFactory
  73. * @param \Magento\Framework\Stdlib\DateTime\DateTime $coreDate
  74. * @param \Magento\Eav\Model\Config $eavConfig
  75. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  76. * @param \Magento\CatalogRule\Helper\Data $catalogRuleData
  77. * @param \Psr\Log\LoggerInterface $logger
  78. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  79. * @param PriceCurrencyInterface $priceCurrency
  80. * @param null $connectionName
  81. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  82. */
  83. public function __construct(
  84. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  85. \Magento\Store\Model\StoreManagerInterface $storeManager,
  86. \Magento\Catalog\Model\Product\ConditionFactory $conditionFactory,
  87. \Magento\Framework\Stdlib\DateTime\DateTime $coreDate,
  88. \Magento\Eav\Model\Config $eavConfig,
  89. \Magento\Framework\Event\ManagerInterface $eventManager,
  90. \Magento\CatalogRule\Helper\Data $catalogRuleData,
  91. \Psr\Log\LoggerInterface $logger,
  92. \Magento\Framework\Stdlib\DateTime $dateTime,
  93. PriceCurrencyInterface $priceCurrency,
  94. $connectionName = null
  95. ) {
  96. $this->_storeManager = $storeManager;
  97. $this->_conditionFactory = $conditionFactory;
  98. $this->_coreDate = $coreDate;
  99. $this->_eavConfig = $eavConfig;
  100. $this->_eventManager = $eventManager;
  101. $this->_catalogRuleData = $catalogRuleData;
  102. $this->_logger = $logger;
  103. $this->dateTime = $dateTime;
  104. $this->priceCurrency = $priceCurrency;
  105. $this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap();
  106. parent::__construct($context, $connectionName);
  107. }
  108. /**
  109. * Initialize main table and table id field
  110. *
  111. * @return void
  112. * @codeCoverageIgnore
  113. */
  114. protected function _construct()
  115. {
  116. $this->_init('catalogrule', 'rule_id');
  117. }
  118. /**
  119. * @param \Magento\Framework\Model\AbstractModel $rule
  120. * @return $this
  121. */
  122. protected function _afterDelete(\Magento\Framework\Model\AbstractModel $rule)
  123. {
  124. $connection = $this->getConnection();
  125. $connection->delete(
  126. $this->getTable('catalogrule_product'),
  127. ['rule_id=?' => $rule->getId()]
  128. );
  129. $connection->delete(
  130. $this->getTable('catalogrule_group_website'),
  131. ['rule_id=?' => $rule->getId()]
  132. );
  133. return parent::_afterDelete($rule);
  134. }
  135. /**
  136. * Get catalog rules product price for specific date, website and
  137. * customer group
  138. *
  139. * @param \DateTimeInterface $date
  140. * @param int $wId
  141. * @param int $gId
  142. * @param int $pId
  143. * @return float|false
  144. */
  145. public function getRulePrice($date, $wId, $gId, $pId)
  146. {
  147. $data = $this->getRulePrices($date, $wId, $gId, [$pId]);
  148. if (isset($data[$pId])) {
  149. return $data[$pId];
  150. }
  151. return false;
  152. }
  153. /**
  154. * Retrieve product prices by catalog rule for specific date, website and customer group
  155. * Collect data with product Id => price pairs
  156. *
  157. * @param \DateTimeInterface $date
  158. * @param int $websiteId
  159. * @param int $customerGroupId
  160. * @param array $productIds
  161. * @return array
  162. */
  163. public function getRulePrices(\DateTimeInterface $date, $websiteId, $customerGroupId, $productIds)
  164. {
  165. $connection = $this->getConnection();
  166. $select = $connection->select()
  167. ->from($this->getTable('catalogrule_product_price'), ['product_id', 'rule_price'])
  168. ->where('rule_date = ?', $date->format('Y-m-d'))
  169. ->where('website_id = ?', $websiteId)
  170. ->where('customer_group_id = ?', $customerGroupId)
  171. ->where('product_id IN(?)', $productIds);
  172. return $connection->fetchPairs($select);
  173. }
  174. /**
  175. * Get active rule data based on few filters
  176. *
  177. * @param int|string $date
  178. * @param int $websiteId
  179. * @param int $customerGroupId
  180. * @param int $productId
  181. * @return array
  182. */
  183. public function getRulesFromProduct($date, $websiteId, $customerGroupId, $productId)
  184. {
  185. $connection = $this->getConnection();
  186. if (is_string($date)) {
  187. $date = strtotime($date);
  188. }
  189. $select = $connection->select()
  190. ->from($this->getTable('catalogrule_product'))
  191. ->where('website_id = ?', $websiteId)
  192. ->where('customer_group_id = ?', $customerGroupId)
  193. ->where('product_id = ?', $productId)
  194. ->where('from_time = 0 or from_time < ?', $date)
  195. ->where('to_time = 0 or to_time > ?', $date);
  196. return $connection->fetchAll($select);
  197. }
  198. /**
  199. * @param \Magento\Framework\Model\AbstractModel $object
  200. * @param mixed $value
  201. * @param string $field
  202. * @return $this
  203. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  204. */
  205. public function load(\Magento\Framework\Model\AbstractModel $object, $value, $field = null)
  206. {
  207. $this->getEntityManager()->load($object, $value);
  208. return $this;
  209. }
  210. /**
  211. * @param AbstractModel $object
  212. * @return $this
  213. * @throws \Exception
  214. */
  215. public function save(\Magento\Framework\Model\AbstractModel $object)
  216. {
  217. $this->getEntityManager()->save($object);
  218. return $this;
  219. }
  220. /**
  221. * Delete the object
  222. *
  223. * @param \Magento\Framework\Model\AbstractModel $object
  224. * @return $this
  225. * @throws \Exception
  226. */
  227. public function delete(AbstractModel $object)
  228. {
  229. $this->getEntityManager()->delete($object);
  230. return $this;
  231. }
  232. /**
  233. * @return array
  234. * @deprecated 100.1.0
  235. */
  236. private function getAssociatedEntitiesMap()
  237. {
  238. if (!$this->_associatedEntitiesMap) {
  239. $this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance()
  240. ->get(\Magento\CatalogRule\Model\ResourceModel\Rule\AssociatedEntityMap::class)
  241. ->getData();
  242. }
  243. return $this->_associatedEntitiesMap;
  244. }
  245. /**
  246. * @return \Magento\Framework\EntityManager\EntityManager
  247. * @deprecated 100.1.0
  248. */
  249. private function getEntityManager()
  250. {
  251. if (null === $this->entityManager) {
  252. $this->entityManager = \Magento\Framework\App\ObjectManager::getInstance()
  253. ->get(\Magento\Framework\EntityManager\EntityManager::class);
  254. }
  255. return $this->entityManager;
  256. }
  257. }