/**
  * Update stock item on the stock and distribute qty by lots.
  *
  * @param \Magento\CatalogInventory\Model\StockManagement $subject
  * @param \Closure $proceed
  * @param array $items
  * @param int $websiteId is not used
  * @throws \Magento\Framework\Exception\LocalizedException
  * @return null
  */
 public function aroundRegisterProductsSale(\Magento\CatalogInventory\Model\StockManagement $subject, \Closure $proceed, array $items, $websiteId)
 {
     /* This code is moved from original 'registerProductsSale' method. */
     /* replace websiteId by stockId */
     $stockId = $this->_manStock->getCurrentStockId();
     $def = $this->_manTrans->begin();
     $lockedItems = $this->_resourceStock->lockProductsStock(array_keys($items), $stockId);
     $fullSaveItems = $registeredItems = [];
     foreach ($lockedItems as $lockedItemRecord) {
         $productId = $lockedItemRecord['product_id'];
         $orderedQty = $items[$productId];
         /** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
         $stockItem = $this->_providerStockRegistry->getStockItem($productId, $stockId);
         $stockItemId = $stockItem->getItemId();
         $canSubtractQty = $stockItemId && $this->_canSubtractQty($stockItem);
         if (!$canSubtractQty || !$this->_configStock->isQty($lockedItemRecord['type_id'])) {
             continue;
         }
         if (!$stockItem->hasAdminArea() && !$this->_stockState->checkQty($productId, $orderedQty)) {
             $this->_manTrans->rollback($def);
             throw new \Magento\Framework\Exception\LocalizedException(__('Not all of your products are available in the requested quantity.'));
         }
         if ($this->_canSubtractQty($stockItem)) {
             $stockItem->setQty($stockItem->getQty() - $orderedQty);
         }
         $registeredItems[$productId] = $orderedQty;
         if (!$this->_stockState->verifyStock($productId) || $this->_stockState->verifyNotification($productId)) {
             $fullSaveItems[] = $stockItem;
         }
     }
     $this->_resourceStock->correctItemsQty($registeredItems, $stockId, '-');
     $this->_manTrans->commit($def);
     return $fullSaveItems;
 }
 /**
  * Replace default stock id in the where clause by stock id corresponded with store id.
  *
  * @param \Magento\CatalogInventory\Model\ResourceModel\Stock\Status $subject
  * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $result
  * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
  */
 public function afterAddStockDataToCollection(\Magento\CatalogInventory\Model\ResourceModel\Stock\Status $subject, \Magento\Catalog\Model\ResourceModel\Product\Collection $result)
 {
     /** @var \Magento\Framework\Db\Select $select */
     $select = $result->getSelect();
     $from = $select->getPart('from');
     $join = $from['stock_status_index'];
     $cond = $join['joinCondition'];
     $stockId = $this->_manStock->getCurrentStockId();
     $fixed = str_replace('.stock_id = 1', '.stock_id = ' . $stockId, $cond);
     $join['joinCondition'] = $fixed;
     $from['stock_status_index'] = $join;
     $select->setPart('from', $from);
     return $result;
 }
 /**
  * Detect current stock and get appropriate stock item.
  *
  * @param \Magento\CatalogInventory\Model\StockRegistryProvider $subject
  * @param \Closure $proceed
  * @param int $productId
  * @param int $scopeId
  * @return \Magento\CatalogInventory\Api\Data\StockItemInterface
  */
 public function aroundGetStockItem(\Magento\CatalogInventory\Model\StockRegistryProvider $subject, \Closure $proceed, $productId, $scopeId)
 {
     $result = $this->_storageStockRegistry->getStockItem($productId, $scopeId);
     if (null === $result) {
         $criteria = $this->_factoryStockItemCrit->create();
         $criteria->setProductsFilter($productId);
         $stockId = $this->_toolStockManager->getCurrentStockId();
         $criteria->setStockFilter($stockId);
         $collection = $this->_repoStockItem->getList($criteria);
         $result = current($collection->getItems());
         if ($result && $result->getItemId()) {
             $this->_storageStockRegistry->setStockItem($productId, $scopeId, $result);
         } else {
             $result = $this->_factoryStockItem->create();
         }
     }
     return $result;
 }