Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Stock.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
21 
27  protected $_isConfig;
28 
35 
42 
48  protected $_configMinQty;
49 
55  protected $_configTypeIds;
56 
63 
69  protected $_scopeConfig;
70 
74  protected $dateTime;
75 
80  protected $storeManager;
81 
90  public function __construct(
91  \Magento\Framework\Model\ResourceModel\Db\Context $context,
92  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
93  \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
96  $connectionName = null
97  ) {
98  parent::__construct($context, $connectionName);
99  $this->_scopeConfig = $scopeConfig;
100  $this->dateTime = $dateTime;
101  $this->stockConfiguration = $stockConfiguration;
102  $this->storeManager = $storeManager;
103  }
104 
110  protected function _construct()
111  {
112  $this->_init('cataloginventory_stock', 'stock_id');
113  }
114 
122  public function lockProductsStock(array $productIds, $websiteId)
123  {
124  if (empty($productIds)) {
125  return [];
126  }
127  $itemTable = $this->getTable('cataloginventory_stock_item');
128  $select = $this->getConnection()->select()->from(['si' => $itemTable])
129  ->where('website_id = ?', $websiteId)
130  ->where('product_id IN(?)', $productIds)
131  ->forUpdate(true);
132 
133  $productTable = $this->getTable('catalog_product_entity');
134  $selectProducts = $this->getConnection()->select()->from(['p' => $productTable], [])
135  ->where('entity_id IN (?)', $productIds)
136  ->columns(
137  [
138  'product_id' => 'entity_id',
139  'type_id' => 'type_id',
140  ]
141  );
142  $items = [];
143 
144  foreach ($this->getConnection()->query($select)->fetchAll() as $si) {
145  $items[$si['product_id']] = $si;
146  }
147  foreach ($this->getConnection()->fetchAll($selectProducts) as $p) {
148  $items[$p['product_id']]['type_id'] = $p['type_id'];
149  }
150 
151  return $items;
152  }
153 
157  public function correctItemsQty(array $items, $websiteId, $operator)
158  {
159  if (empty($items)) {
160  return;
161  }
162 
163  $connection = $this->getConnection();
164  $conditions = [];
165  foreach ($items as $productId => $qty) {
166  $case = $connection->quoteInto('?', $productId);
167  $result = $connection->quoteInto("qty{$operator}?", $qty);
168  $conditions[$case] = $result;
169  }
170 
171  $value = $connection->getCaseSql('product_id', $conditions, 'qty');
172  $where = ['product_id IN (?)' => array_keys($items), 'website_id = ?' => $websiteId];
173 
174  $connection->beginTransaction();
175  $connection->update($this->getTable('cataloginventory_stock_item'), ['qty' => $value], $where);
176  $connection->commit();
177  }
178 
184  protected function _initConfig()
185  {
186  if (!$this->_isConfig) {
187  $configMap = [
192  ];
193 
194  foreach ($configMap as $field => $const) {
195  $this->{$field} = (int) $this->_scopeConfig->getValue(
196  $const,
197  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
198  );
199  }
200 
201  $this->_isConfig = true;
202  $this->_configTypeIds = array_keys($this->stockConfiguration->getIsQtyTypeIds(true));
203  }
204  }
205 
215  public function updateSetOutOfStock($website = null)
216  {
217  $websiteId = $this->stockConfiguration->getDefaultScopeId();
218  $this->_initConfig();
219  $connection = $this->getConnection();
220  $values = ['is_in_stock' => 0, 'stock_status_changed_auto' => 1];
221 
222  $select = $connection->select()->from($this->getTable('catalog_product_entity'), 'entity_id')
223  ->where('type_id IN(?)', $this->_configTypeIds);
224 
225  $where = sprintf(
226  'website_id = %1$d' .
227  ' AND is_in_stock = 1' .
228  ' AND ((use_config_manage_stock = 1 AND 1 = %2$d) OR (use_config_manage_stock = 0 AND manage_stock = 1))' .
229  ' AND ((use_config_backorders = 1 AND %3$d = %4$d) OR (use_config_backorders = 0 AND backorders = %3$d))' .
230  ' AND ((use_config_min_qty = 1 AND qty <= %5$d) OR (use_config_min_qty = 0 AND qty <= min_qty))' .
231  ' AND product_id IN (%6$s)',
232  $websiteId,
233  $this->_isConfigManageStock,
234  \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO,
235  $this->_isConfigBackorders,
236  $this->_configMinQty,
237  $select->assemble()
238  );
239 
240  $connection->update($this->getTable('cataloginventory_stock_item'), $values, $where);
241  }
242 
252  public function updateSetInStock($website)
253  {
254  $websiteId = $this->stockConfiguration->getDefaultScopeId();
255  $this->_initConfig();
256  $connection = $this->getConnection();
257  $values = ['is_in_stock' => 1];
258 
259  $select = $connection->select()->from($this->getTable('catalog_product_entity'), 'entity_id')
260  ->where('type_id IN(?)', $this->_configTypeIds);
261 
262  $where = sprintf(
263  'website_id = %1$d' .
264  ' AND is_in_stock = 0' .
265  ' AND stock_status_changed_auto = 1' .
266  ' AND ((use_config_manage_stock = 1 AND 1 = %2$d) OR (use_config_manage_stock = 0 AND manage_stock = 1))' .
267  ' AND ((use_config_min_qty = 1 AND qty > %3$d) OR (use_config_min_qty = 0 AND qty > min_qty))' .
268  ' AND product_id IN (%4$s)',
269  $websiteId,
270  $this->_isConfigManageStock,
271  $this->_configMinQty,
272  $select->assemble()
273  );
274 
275  $connection->update($this->getTable('cataloginventory_stock_item'), $values, $where);
276  }
277 
287  public function updateLowStockDate($website)
288  {
289  $websiteId = $this->stockConfiguration->getDefaultScopeId();
290  $this->_initConfig();
291 
292  $connection = $this->getConnection();
293  $condition = $connection->quoteInto(
294  '(use_config_notify_stock_qty = 1 AND qty < ?)',
295  $this->_configNotifyStockQty
296  ) . ' OR (use_config_notify_stock_qty = 0 AND qty < notify_stock_qty)';
297  $currentDbTime = $connection->quoteInto('?', $this->dateTime->gmtDate());
298  $conditionalDate = $connection->getCheckSql($condition, $currentDbTime, 'NULL');
299 
300  $value = ['low_stock_date' => new \Zend_Db_Expr($conditionalDate)];
301 
302  $select = $connection->select()->from($this->getTable('catalog_product_entity'), 'entity_id')
303  ->where('type_id IN(?)', $this->_configTypeIds);
304 
305  $where = sprintf(
306  'website_id = %1$d' .
307  ' AND ((use_config_manage_stock = 1 AND 1 = %2$d) OR (use_config_manage_stock = 0 AND manage_stock = 1))' .
308  ' AND product_id IN (%3$s)',
309  $websiteId,
310  $this->_isConfigManageStock,
311  $select->assemble()
312  );
313 
314  $connection->update($this->getTable('cataloginventory_stock_item'), $value, $where);
315  }
316 
324  public function addLowStockFilter(\Magento\Catalog\Model\ResourceModel\Product\Collection $collection, $fields)
325  {
326  $this->_initConfig();
327  $connection = $collection->getSelect()->getConnection();
328  $qtyIf = $connection->getCheckSql(
329  'invtr.use_config_notify_stock_qty > 0',
330  $this->_configNotifyStockQty,
331  'invtr.notify_stock_qty'
332  );
333  $conditions = [
334  [
335  $connection->prepareSqlCondition('invtr.use_config_manage_stock', 1),
336  $connection->prepareSqlCondition($this->_isConfigManageStock, 1),
337  $connection->prepareSqlCondition('invtr.qty', ['lt' => $qtyIf]),
338  ],
339  [
340  $connection->prepareSqlCondition('invtr.use_config_manage_stock', 0),
341  $connection->prepareSqlCondition('invtr.manage_stock', 1)
342  ],
343  ];
344 
345  $where = [];
346  foreach ($conditions as $k => $part) {
347  $where[$k] = join(' ' . \Magento\Framework\DB\Select::SQL_AND . ' ', $part);
348  }
349 
350  $where = $connection->prepareSqlCondition(
351  'invtr.low_stock_date',
352  ['notnull' => true]
353  ) . ' ' . \Magento\Framework\DB\Select::SQL_AND . ' ((' . join(
354  ') ' . \Magento\Framework\DB\Select::SQL_OR . ' (',
355  $where
356  ) . '))';
357 
358  $collection->joinTable(
359  ['invtr' => 'cataloginventory_stock_item'],
360  'product_id = entity_id',
361  $fields,
362  $where
363  );
364  return $this;
365  }
366 }
lockProductsStock(array $productIds, $websiteId)
Definition: Stock.php:122
$case
$fields
Definition: details.phtml:14
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16
const SQL_AND
Definition: Select.php:77
correctItemsQty(array $items, $websiteId, $operator)
Definition: Stock.php:157
$connection
Definition: bulk.php:13
addLowStockFilter(\Magento\Catalog\Model\ResourceModel\Product\Collection $collection, $fields)
Definition: Stock.php:324
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Stdlib\DateTime\DateTime $dateTime, StockConfigurationInterface $stockConfiguration, StoreManagerInterface $storeManager, $connectionName=null)
Definition: Stock.php:90
$items