/** * 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; }
/** * Update items stock status and low stock date. * * @param EventObserver $observer * @return void */ public function execute(EventObserver $observer) { $website = $observer->getEvent()->getWebsite(); $this->resourceStock->updateSetOutOfStock($website); $this->resourceStock->updateSetInStock($website); $this->resourceStock->updateLowStockDate($website); }
public function testUpdateItemsStockUponConfigChange() { $websiteId = 1; $this->resourceStock->expects($this->once())->method('updateSetOutOfStock'); $this->resourceStock->expects($this->once())->method('updateSetInStock'); $this->resourceStock->expects($this->once())->method('updateLowStockDate'); $this->event->expects($this->once())->method('getWebsite')->will($this->returnValue($websiteId)); $this->observer->execute($this->eventObserver); }
public function testGetProductsCollection() { /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection', [], [], '', false); $this->product->expects($this->once())->method('getCollection')->will($this->returnValue($productCollection)); $productCollection->expects($this->once())->method('addAttributeToSelect')->will($this->returnSelf()); $productCollection->expects($this->once())->method('addAttributeToFilter')->will($this->returnSelf()); $productCollection->expects($this->once())->method('setOrder')->will($this->returnSelf()); $this->eventManager->expects($this->once())->method('dispatch')->with('rss_catalog_notify_stock_collection_select'); $this->stock->expects($this->once())->method('addLowStockFilter')->with($productCollection); $products = $this->notifyStock->getProductsCollection(); $this->assertEquals($productCollection, $products); }
/** * @expectedException \Magento\Framework\Exception\CouldNotDeleteException * @expectedExceptionMessage Stock with id "1" does not exist. */ public function testDeleteByIdException() { $id = 1; $this->stockFactoryMock->expects($this->once())->method('create')->willReturn($this->stockMock); $this->stockResourceMock->expects($this->once())->method('load')->with($this->stockMock, $id); $this->stockMock->expects($this->once())->method('getId')->willReturn(null); $this->assertTrue($this->model->deleteById($id)); }
/** * @param StockInterface $stock * @return bool|true * @throws CouldNotDeleteException */ public function delete(StockInterface $stock) { try { $this->resource->delete($stock); } catch (\Exception $exception) { throw new CouldNotDeleteException(__($exception->getMessage())); } return true; }
/** * @param StockInterface $stock * @return bool|true * @throws CouldNotDeleteException */ public function delete(StockInterface $stock) { try { $this->resource->delete($stock); $this->getStockRegistryStorage()->removeStock(); } catch (\Exception $exception) { throw new CouldNotDeleteException(__('Unable to remove Stock with id "%1"', $stock->getStockId()), $exception); } return true; }
/** * Filter locked items by stock ID. * * @param \Magento\CatalogInventory\Model\ResourceModel\Stock $subject * @param \Closure $proceed * @param int[] $productIds * @param int $stockId defined in \Praxigento\Warehouse\Plugin\CatalogInventory\Model\StockManagement::aroundRegisterProductsSale * @return array found stock items data as an associative array */ public function aroundLockProductsStock(\Magento\CatalogInventory\Model\ResourceModel\Stock $subject, \Closure $proceed, $productIds, $stockId) { if (empty($productIds)) { return []; } /** @var \Magento\Framework\DB\Adapter\AdapterInterface $conn */ $conn = $subject->getConnection(); $itemTable = $subject->getTable('cataloginventory_stock_item'); $productTable = $subject->getTable('catalog_product_entity'); /** @var \Magento\Framework\DB\Select $select */ $select = $conn->select(); $select->from(['si' => $itemTable]); $select->join(['p' => $productTable], 'p.entity_id=si.product_id', ['type_id']); $select->where('product_id IN(?)', $productIds); $select->forUpdate(true); /* MOBI-375 add filter by $stockId */ $select->where('stock_id=?', $stockId); /* select data */ $result = $conn->fetchAll($select); return $result; }