PageRenderTime 185ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-reports/Model/ResourceModel/Product/Lowstock/Collection.php

https://gitlab.com/daigiangaitu91/magento
PHP | 303 lines | 161 code | 23 blank | 119 comment | 9 complexity | 11724555665281a25d01c4c956d0116c MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Product Low Stock Report Collection
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Reports\Model\ResourceModel\Product\Lowstock;
  12. /**
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class Collection extends \Magento\Reports\Model\ResourceModel\Product\Collection
  16. {
  17. /**
  18. * Flag about is joined CatalogInventory Stock Item
  19. *
  20. * @var bool
  21. */
  22. protected $_inventoryItemJoined = false;
  23. /**
  24. * Alias for CatalogInventory Stock Item Table
  25. *
  26. * @var string
  27. */
  28. protected $_inventoryItemTableAlias = 'lowstock_inventory_item';
  29. /**
  30. * @var \Magento\CatalogInventory\Api\StockRegistryInterface
  31. */
  32. protected $stockRegistry;
  33. /**
  34. * @var \Magento\CatalogInventory\Api\StockConfigurationInterface
  35. */
  36. protected $stockConfiguration;
  37. /**
  38. * @var \Magento\CatalogInventory\Model\ResourceModel\Stock\Item
  39. */
  40. protected $_itemResource;
  41. /**
  42. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  43. * @param \Psr\Log\LoggerInterface $logger
  44. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  45. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  46. * @param \Magento\Eav\Model\Config $eavConfig
  47. * @param \Magento\Framework\App\ResourceConnection $resource
  48. * @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
  49. * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
  50. * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
  51. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  52. * @param \Magento\Framework\Module\Manager $moduleManager
  53. * @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState
  54. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  55. * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
  56. * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrl
  57. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  58. * @param \Magento\Customer\Model\Session $customerSession
  59. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  60. * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
  61. * @param \Magento\Catalog\Model\ResourceModel\Product $product
  62. * @param \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory
  63. * @param \Magento\Catalog\Model\Product\Type $productType
  64. * @param \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteResource
  65. * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
  66. * @param \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
  67. * @param \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $itemResource
  68. * @param mixed $connection
  69. *
  70. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  71. */
  72. public function __construct(
  73. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  74. \Psr\Log\LoggerInterface $logger,
  75. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  76. \Magento\Framework\Event\ManagerInterface $eventManager,
  77. \Magento\Eav\Model\Config $eavConfig,
  78. \Magento\Framework\App\ResourceConnection $resource,
  79. \Magento\Eav\Model\EntityFactory $eavEntityFactory,
  80. \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
  81. \Magento\Framework\Validator\UniversalFactory $universalFactory,
  82. \Magento\Store\Model\StoreManagerInterface $storeManager,
  83. \Magento\Framework\Module\Manager $moduleManager,
  84. \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState,
  85. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  86. \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
  87. \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
  88. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  89. \Magento\Customer\Model\Session $customerSession,
  90. \Magento\Framework\Stdlib\DateTime $dateTime,
  91. \Magento\Customer\Api\GroupManagementInterface $groupManagement,
  92. \Magento\Catalog\Model\ResourceModel\Product $product,
  93. \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory,
  94. \Magento\Catalog\Model\Product\Type $productType,
  95. \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteResource,
  96. \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
  97. \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
  98. \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $itemResource,
  99. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
  100. ) {
  101. parent::__construct(
  102. $entityFactory,
  103. $logger,
  104. $fetchStrategy,
  105. $eventManager,
  106. $eavConfig,
  107. $resource,
  108. $eavEntityFactory,
  109. $resourceHelper,
  110. $universalFactory,
  111. $storeManager,
  112. $moduleManager,
  113. $catalogProductFlatState,
  114. $scopeConfig,
  115. $productOptionFactory,
  116. $catalogUrl,
  117. $localeDate,
  118. $customerSession,
  119. $dateTime,
  120. $groupManagement,
  121. $product,
  122. $eventTypeFactory,
  123. $productType,
  124. $quoteResource,
  125. $connection
  126. );
  127. $this->stockRegistry = $stockRegistry;
  128. $this->stockConfiguration = $stockConfiguration;
  129. $this->_itemResource = $itemResource;
  130. }
  131. /**
  132. * Retrieve CatalogInventory Stock Item Table
  133. *
  134. * @return string
  135. */
  136. protected function _getInventoryItemTable()
  137. {
  138. return $this->_itemResource->getMainTable();
  139. }
  140. /**
  141. * Retrieve CatalogInventory Stock Item Table Id field name
  142. *
  143. * @return string
  144. */
  145. protected function _getInventoryItemIdField()
  146. {
  147. return $this->_itemResource->getIdFieldName();
  148. }
  149. /**
  150. * Retrieve alias for CatalogInventory Stock Item Table
  151. *
  152. * @return string
  153. */
  154. protected function _getInventoryItemTableAlias()
  155. {
  156. return $this->_inventoryItemTableAlias;
  157. }
  158. /**
  159. * Add catalog inventory stock item field to select
  160. *
  161. * @param string $field
  162. * @param string $alias
  163. * @return $this
  164. */
  165. protected function _addInventoryItemFieldToSelect($field, $alias = null)
  166. {
  167. if (empty($alias)) {
  168. $alias = $field;
  169. }
  170. if (isset($this->_joinFields[$alias])) {
  171. return $this;
  172. }
  173. $this->_joinFields[$alias] = ['table' => $this->_getInventoryItemTableAlias(), 'field' => $field];
  174. $this->getSelect()->columns([$alias => $field], $this->_getInventoryItemTableAlias());
  175. return $this;
  176. }
  177. /**
  178. * Retrieve catalog inventory stock item field correlation name
  179. *
  180. * @param string $field
  181. * @return string
  182. */
  183. protected function _getInventoryItemField($field)
  184. {
  185. return sprintf('%s.%s', $this->_getInventoryItemTableAlias(), $field);
  186. }
  187. /**
  188. * Join catalog inventory stock item table for further stock_item values filters
  189. *
  190. * @param array $fields
  191. * @return $this
  192. */
  193. public function joinInventoryItem($fields = [])
  194. {
  195. if (!$this->_inventoryItemJoined) {
  196. $this->getSelect()->join(
  197. [$this->_getInventoryItemTableAlias() => $this->_getInventoryItemTable()],
  198. sprintf(
  199. 'e.%s = %s.product_id',
  200. $this->getEntity()->getEntityIdField(),
  201. $this->_getInventoryItemTableAlias()
  202. ),
  203. []
  204. );
  205. $this->_inventoryItemJoined = true;
  206. }
  207. if (!is_array($fields)) {
  208. if (empty($fields)) {
  209. $fields = [];
  210. } else {
  211. $fields = [$fields];
  212. }
  213. }
  214. foreach ($fields as $alias => $field) {
  215. if (!is_string($alias)) {
  216. $alias = null;
  217. }
  218. $this->_addInventoryItemFieldToSelect($field, $alias);
  219. }
  220. return $this;
  221. }
  222. /**
  223. * Add filter by product type(s)
  224. *
  225. * @param array|string $typeFilter
  226. * @return $this
  227. */
  228. public function filterByProductType($typeFilter)
  229. {
  230. if (!is_string($typeFilter) && !is_array($typeFilter)) {
  231. new \Magento\Framework\Exception\LocalizedException(__('The product type filter specified is incorrect.'));
  232. }
  233. $this->addAttributeToFilter('type_id', $typeFilter);
  234. return $this;
  235. }
  236. /**
  237. * Add filter by product types from config - only types which have QTY parameter
  238. *
  239. * @return $this
  240. */
  241. public function filterByIsQtyProductTypes()
  242. {
  243. $this->filterByProductType(array_keys(array_filter($this->stockConfiguration->getIsQtyTypeIds())));
  244. return $this;
  245. }
  246. /**
  247. * Add Use Manage Stock Condition to collection
  248. *
  249. * @param null|int $storeId
  250. * @return $this
  251. */
  252. public function useManageStockFilter($storeId = null)
  253. {
  254. $this->joinInventoryItem();
  255. $manageStockExpr = $this->getConnection()->getCheckSql(
  256. $this->_getInventoryItemField('use_config_manage_stock') . ' = 1',
  257. (int)$this->stockConfiguration->getManageStock($storeId),
  258. $this->_getInventoryItemField('manage_stock')
  259. );
  260. $this->getSelect()->where($manageStockExpr . ' = ?', 1);
  261. return $this;
  262. }
  263. /**
  264. * Add Notify Stock Qty Condition to collection
  265. *
  266. * @param null|int $storeId
  267. * @return $this
  268. */
  269. public function useNotifyStockQtyFilter($storeId = null)
  270. {
  271. $this->joinInventoryItem(['qty']);
  272. $notifyStockExpr = $this->getConnection()->getCheckSql(
  273. $this->_getInventoryItemField('use_config_notify_stock_qty') . ' = 1',
  274. (int)$this->stockConfiguration->getNotifyStockQty($storeId),
  275. $this->_getInventoryItemField('notify_stock_qty')
  276. );
  277. $this->getSelect()->where($this->_getInventoryItemField('qty') . ' < ?', $notifyStockExpr);
  278. return $this;
  279. }
  280. }