/**
  * Load inventory data for a list of product ids and a given store.
  *
  * @param integer $storeId    Store id.
  * @param array   $productIds Product ids list.
  *
  * @return array
  */
 public function loadInventoryData($storeId, $productIds)
 {
     $websiteId = $this->getWebsiteId($storeId);
     $stockId = $this->getStockId($websiteId);
     $select = $this->getConnection()->select()->from(['ciss' => $this->getTable('cataloginventory_stock_status')], ['product_id', 'stock_status', 'qty'])->where('ciss.stock_id = ?', $stockId)->where('ciss.website_id = ?', $this->stockConfiguration->getDefaultScopeId())->where('ciss.product_id IN(?)', $productIds);
     return $this->getConnection()->fetchAll($select);
 }
 /**
  * @param string $productSku
  * @param null $scopeId
  * @return int
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  */
 public function getProductStockStatusBySku($productSku, $scopeId = null)
 {
     //if (!$scopeId) {
     $scopeId = $this->stockConfiguration->getDefaultScopeId();
     //}
     $productId = $this->resolveProductId($productSku);
     return $this->getProductStockStatus($productId, $scopeId);
 }
 /**
  * @param int $productId
  * @param float $itemQty
  * @param float $qtyToCheck
  * @param float $origQty
  * @param int $scopeId
  * @return int
  */
 public function checkQuoteItemQty($productId, $itemQty, $qtyToCheck, $origQty, $scopeId = null)
 {
     if ($scopeId === null) {
         $scopeId = $this->stockConfiguration->getDefaultScopeId();
     }
     $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
     return $this->stockStateProvider->checkQuoteItemQty($stockItem, $itemQty, $qtyToCheck, $origQty);
 }
Beispiel #4
0
 /**
  * Retrieve Website Id
  *
  * @return int
  */
 public function getWebsiteId()
 {
     $websiteId = $this->getData(static::WEBSITE_ID);
     if ($websiteId === null) {
         $websiteId = $this->stockConfiguration->getDefaultScopeId();
     }
     return (int) $websiteId;
 }
 /**
  * @magentoDataFixture Magento/Store/_files/website.php
  * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  *
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  */
 public function testReindexEntity()
 {
     /** @var \Magento\Catalog\Model\ProductRepository $productRepository */
     $productRepository = $this->getObject(\Magento\Catalog\Model\ProductRepository::class);
     /** @var \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository */
     $websiteRepository = $this->getObject(\Magento\Store\Api\WebsiteRepositoryInterface::class);
     $product = $productRepository->get('simple');
     $testWebsite = $websiteRepository->get('test');
     $product->setWebsiteIds([1, $testWebsite->getId()])->save();
     /** @var \Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory $criteriaFactory */
     $criteriaFactory = $this->getObject(\Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory::class);
     /** @var \Magento\CatalogInventory\Api\StockStatusRepositoryInterface $stockStatusRepository */
     $stockStatusRepository = $this->getObject(\Magento\CatalogInventory\Api\StockStatusRepositoryInterface::class);
     $criteria = $criteriaFactory->create();
     $criteria->setProductsFilter([$product->getId()]);
     $criteria->addFilter('website', 'website_id', $this->stockConfiguration->getDefaultScopeId());
     $items = $stockStatusRepository->getList($criteria)->getItems();
     $this->assertEquals($product->getId(), $items[$product->getId()]->getProductId());
 }
Beispiel #6
0
 /**
  * Update items low stock date basing on their quantities and config settings
  *
  * @param int|string $website
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  * @return void
  */
 public function updateLowStockDate($website)
 {
     $websiteId = $this->stockConfiguration->getDefaultScopeId();
     $this->_initConfig();
     $connection = $this->getConnection();
     $condition = $connection->quoteInto('(use_config_notify_stock_qty = 1 AND qty < ?)', $this->_configNotifyStockQty) . ' OR (use_config_notify_stock_qty = 0 AND qty < notify_stock_qty)';
     $currentDbTime = $connection->quoteInto('?', $this->dateTime->gmtDate());
     $conditionalDate = $connection->getCheckSql($condition, $currentDbTime, 'NULL');
     $value = ['low_stock_date' => new \Zend_Db_Expr($conditionalDate)];
     $select = $connection->select()->from($this->getTable('catalog_product_entity'), 'entity_id')->where('type_id IN(?)', $this->_configTypeIds);
     $where = sprintf('website_id = %1$d' . ' AND ((use_config_manage_stock = 1 AND 1 = %2$d) OR (use_config_manage_stock = 0 AND manage_stock = 1))' . ' AND product_id IN (%3$s)', $websiteId, $this->_isConfigManageStock, $select->assemble());
     $connection->update($this->getTable('cataloginventory_stock_item'), $value, $where);
 }
 /**
  * Prepare stock item data for save
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return $this
  */
 protected function saveStockItemData($product)
 {
     $stockItemData = $product->getStockData();
     $stockItemData['product_id'] = $product->getId();
     if (!isset($stockItemData['website_id'])) {
         $stockItemData['website_id'] = $this->stockConfiguration->getDefaultScopeId();
     }
     $stockItemData['stock_id'] = $this->stockRegistry->getStock($stockItemData['website_id'])->getStockId();
     foreach ($this->paramListToCheck as $dataKey => $configPath) {
         if (null !== $product->getData($configPath['item']) && null === $product->getData($configPath['config'])) {
             $stockItemData[$dataKey] = false;
         }
     }
     $originalQty = $product->getData('stock_data/original_inventory_qty');
     if (strlen($originalQty) > 0) {
         $stockItemData['qty_correction'] = (isset($stockItemData['qty']) ? $stockItemData['qty'] : 0) - $originalQty;
     }
     // todo resolve issue with builder and identity field name
     $stockItem = $this->stockRegistry->getStockItem($stockItemData['product_id'], $stockItemData['website_id']);
     $stockItem->addData($stockItemData);
     $this->stockItemRepository->save($stockItem);
     return $this;
 }
 /**
  * @param ProductInterface $product
  * @param StockItemInterface $stockItem
  * @throws LocalizedException
  * @return void
  */
 private function validateStockItem(ProductInterface $product, StockItemInterface $stockItem)
 {
     $defaultScopeId = $this->stockConfiguration->getDefaultScopeId();
     $defaultStockId = $this->stockRegistry->getStock($defaultScopeId)->getStockId();
     $stockId = $stockItem->getStockId();
     if ($stockId !== null && $stockId != $defaultStockId) {
         throw new LocalizedException(__('Invalid stock id: %1. Only default stock with id %2 allowed', $stockId, $defaultStockId));
     }
     $stockItemId = $stockItem->getItemId();
     if ($stockItemId !== null && (!is_numeric($stockItemId) || $stockItemId <= 0)) {
         throw new LocalizedException(__('Invalid stock item id: %1. Should be null or numeric value greater than 0', $stockItemId));
     }
     $defaultStockItemId = $this->stockRegistry->getStockItem($product->getId())->getItemId();
     if ($defaultStockItemId && $stockItemId !== null && $defaultStockItemId != $stockItemId) {
         throw new LocalizedException(__('Invalid stock item id: %1. Assigned stock item id is %2', $stockItemId, $defaultStockItemId));
     }
 }
 /**
  * Get back to stock (when order is canceled or whatever else)
  *
  * @param int $productId
  * @param float $qty
  * @param int $scopeId
  * @return bool
  */
 public function backItemQty($productId, $qty, $scopeId = null)
 {
     //if (!$scopeId) {
     $scopeId = $this->stockConfiguration->getDefaultScopeId();
     //}
     $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
     if ($stockItem->getItemId() && $this->stockConfiguration->isQty($this->getProductType($productId))) {
         if ($this->canSubtractQty($stockItem)) {
             $stockItem->setQty($stockItem->getQty() + $qty);
         }
         if ($this->stockConfiguration->getCanBackInStock($stockItem->getStoreId()) && $stockItem->getQty() > $stockItem->getMinQty()) {
             $stockItem->setIsInStock(true);
             $stockItem->setStockStatusChangedAutomaticallyFlag(true);
         }
         $stockItem->save();
     }
     return true;
 }
Beispiel #10
0
 /**
  * Stock item saving.
  *
  * @return $this
  */
 protected function _saveStockItem()
 {
     $indexer = $this->indexerRegistry->get('catalog_product_category');
     /** @var $stockResource \Magento\CatalogInventory\Model\ResourceModel\Stock\Item */
     $stockResource = $this->_stockResItemFac->create();
     $entityTable = $stockResource->getMainTable();
     while ($bunch = $this->_dataSourceModel->getNextBunch()) {
         $stockData = [];
         $productIdsToReindex = [];
         // Format bunch to stock data rows
         foreach ($bunch as $rowNum => $rowData) {
             if (!$this->isRowAllowedToImport($rowData, $rowNum)) {
                 continue;
             }
             $row = [];
             $row['product_id'] = $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['entity_id'];
             $productIdsToReindex[] = $row['product_id'];
             $row['website_id'] = $this->stockConfiguration->getDefaultScopeId();
             $row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId();
             $stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
             $existStockData = $stockItemDo->getData();
             $row = array_merge($this->defaultStockData, array_intersect_key($existStockData, $this->defaultStockData), array_intersect_key($rowData, $this->defaultStockData), $row);
             if ($this->stockConfiguration->isQty($this->skuProcessor->getNewSku($rowData[self::COL_SKU])['type_id'])) {
                 $stockItemDo->setData($row);
                 $row['is_in_stock'] = $this->stockStateProvider->verifyStock($stockItemDo);
                 if ($this->stockStateProvider->verifyNotification($stockItemDo)) {
                     $row['low_stock_date'] = $this->dateTime->gmDate('Y-m-d H:i:s', (new \DateTime())->getTimestamp());
                 }
                 $row['stock_status_changed_auto'] = (int) (!$this->stockStateProvider->verifyStock($stockItemDo));
             } else {
                 $row['qty'] = 0;
             }
             if (!isset($stockData[$rowData[self::COL_SKU]])) {
                 $stockData[$rowData[self::COL_SKU]] = $row;
             }
         }
         // Insert rows
         if (!empty($stockData)) {
             $this->_connection->insertOnDuplicate($entityTable, array_values($stockData));
         }
         if ($productIdsToReindex) {
             $indexer->reindexList($productIdsToReindex);
         }
     }
     return $this;
 }