Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LowQuantityCollection.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
28 use Psr\Log\LoggerInterface;
29 
34 {
38  private $stockConfiguration;
39 
43  private $getAllowedProductTypesForSourceItemManagement;
44 
48  private $attributeRepository;
49 
53  private $metadataPool;
54 
58  private $filterStoreId;
59 
74  public function __construct(
75  EntityFactoryInterface $entityFactory,
77  FetchStrategyInterface $fetchStrategy,
78  ManagerInterface $eventManager,
79  AttributeRepositoryInterface $attributeRepository,
80  StockConfigurationInterface $stockConfiguration,
81  GetAllowedProductTypesForSourceItemManagementInterface $getAllowedProductTypesForSourceItemManagement,
82  MetadataPool $metadataPool,
84  AbstractDb $resource = null
85  ) {
86  parent::__construct(
87  $entityFactory,
88  $logger,
89  $fetchStrategy,
90  $eventManager,
92  $resource
93  );
94 
95  $this->attributeRepository = $attributeRepository;
96  $this->stockConfiguration = $stockConfiguration;
97  $this->getAllowedProductTypesForSourceItemManagement = $getAllowedProductTypesForSourceItemManagement;
98  $this->metadataPool = $metadataPool;
99  }
100 
104  protected function _construct()
105  {
106  $this->_init(SourceItemModel::class, SourceItemResourceModel::class);
107 
108  $this->addFilterToMap('source_code', 'main_table.source_code');
109  $this->addFilterToMap('sku', 'main_table.sku');
110  $this->addFilterToMap('product_name', 'product_entity_varchar.value');
111  }
112 
117  public function addStoreFilter(int $storeId)
118  {
119  $this->filterStoreId = $storeId;
120  }
121 
125  protected function _renderFilters()
126  {
127  if (false === $this->_isFiltersRendered) {
128  $this->joinInventoryConfiguration();
129  $this->joinCatalogProduct();
130 
131  $this->addProductTypeFilter();
132  $this->addNotifyStockQtyFilter();
133  $this->addEnabledSourceFilter();
134  $this->addSourceItemInStockFilter();
135  }
136  return parent::_renderFilters();
137  }
138 
142  protected function _renderOrders()
143  {
144  if (false === $this->_isOrdersRendered) {
145  $this->setOrder(SourceItemInterface::QUANTITY, self::SORT_ORDER_ASC);
146  }
147  return parent::_renderOrders();
148  }
149 
155  private function joinCatalogProduct()
156  {
157  $productEntityTable = $this->getTable('catalog_product_entity');
158  $productEavVarcharTable = $this->getTable('catalog_product_entity_varchar');
159  $nameAttribute = $this->attributeRepository->get('catalog_product', 'name');
160 
161  $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
162  $linkField = $metadata->getLinkField();
163 
164  $this->getSelect()->joinInner(
165  ['product_entity' => $productEntityTable],
166  'main_table.' . SourceItemInterface::SKU . ' = product_entity.' . ProductInterface::SKU,
167  []
168  );
169 
170  $this->getSelect()->joinInner(
171  ['product_entity_varchar' => $productEavVarcharTable],
172  'product_entity_varchar.' . $linkField . ' = product_entity.' . $linkField . ' ' .
173  'AND product_entity_varchar.store_id = ' . Store::DEFAULT_STORE_ID. ' ' .
174  'AND product_entity_varchar.attribute_id = ' . (int)$nameAttribute->getAttributeId(),
175  []
176  );
177 
178  if (null !== $this->filterStoreId) {
179  $this->getSelect()->joinLeft(
180  ['product_entity_varchar_store' => $productEavVarcharTable],
181  'product_entity_varchar_store.' . $linkField . ' = product_entity.' . $linkField . ' ' .
182  'AND product_entity_varchar_store.store_id = ' . (int)$this->filterStoreId . ' ' .
183  'AND product_entity_varchar_store.attribute_id = ' . (int)$nameAttribute->getAttributeId(),
184  [
185  'product_name' => $this->getConnection()->getIfNullSql(
186  'product_entity_varchar_store.value',
187  'product_entity_varchar.value'
188  )
189  ]
190  );
191  } else {
192  $this->getSelect()->columns(['product_name' => 'product_entity_varchar.value']);
193  }
194  }
195 
199  private function joinInventoryConfiguration()
200  {
201  $sourceItemConfigurationTable = $this->getTable('inventory_low_stock_notification_configuration');
202 
203  $this->getSelect()->joinInner(
204  ['notification_configuration' => $sourceItemConfigurationTable],
205  sprintf(
206  'main_table.%s = notification_configuration.%s AND main_table.%s = notification_configuration.%s',
211  ),
212  []
213  );
214  }
215 
219  private function addProductTypeFilter()
220  {
221  $this->addFieldToFilter(
222  'product_entity.type_id',
223  $this->getAllowedProductTypesForSourceItemManagement->execute()
224  );
225  }
226 
230  private function addNotifyStockQtyFilter()
231  {
232  $notifyStockExpression = $this->getConnection()->getIfNullSql(
233  'notification_configuration.' . SourceItemConfigurationInterface::INVENTORY_NOTIFY_QTY,
234  (float)$this->stockConfiguration->getNotifyStockQty()
235  );
236 
237  $this->getSelect()->where(
239  $notifyStockExpression
240  );
241  }
242 
246  private function addEnabledSourceFilter()
247  {
248  $this->getSelect()->joinInner(
249  ['inventory_source' => $this->getTable(Source::TABLE_NAME_SOURCE)],
250  sprintf(
251  'inventory_source.%s = 1 AND inventory_source.%s = main_table.%s',
255  ),
256  []
257  );
258  }
259 
263  private function addSourceItemInStockFilter()
264  {
265  $this->addFieldToFilter('main_table.status', SourceItemInterface::STATUS_IN_STOCK);
266  }
267 }
addFilterToMap($filter, $alias, $group='fields')
Definition: AbstractDb.php:798
__construct(EntityFactoryInterface $entityFactory, LoggerInterface $logger, FetchStrategyInterface $fetchStrategy, ManagerInterface $eventManager, AttributeRepositoryInterface $attributeRepository, StockConfigurationInterface $stockConfiguration, GetAllowedProductTypesForSourceItemManagementInterface $getAllowedProductTypesForSourceItemManagement, MetadataPool $metadataPool, AdapterInterface $connection=null, AbstractDb $resource=null)
$resource
Definition: bulk.php:12
$logger
setOrder($field, $direction=self::SORT_ORDER_DESC)
Definition: AbstractDb.php:274
$connection
Definition: bulk.php:13