From edd0a731fa1664401c111adb079b405fc723b176 Mon Sep 17 00:00:00 2001 From: RomanKis Date: Mon, 7 May 2018 15:08:34 +0300 Subject: [PATCH 01/13] MSI-793: Incorrect process of Out-of-Stock Threshold. --- ...yCatalogInventoryAtSourceItemsSavePlugin.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php index 94122951f74f..4e94fb689df9 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php @@ -81,16 +81,17 @@ public function afterExecute(SourceItemsSaveInterface $subject, $result, array $ if ($sourceItem->getSourceCode() !== $this->defaultSourceProvider->getCode()) { continue; } - $this->setDataToLegacyStockItem->execute( + $this->setDataToLegacyStockStatus->execute( $sourceItem->getSku(), (float)$sourceItem->getQuantity(), (int)$sourceItem->getStatus() ); - $this->setDataToLegacyStockStatus->execute( + + $isSalable = (int)$this->isProductSalable->execute( $sourceItem->getSku(), - (float)$sourceItem->getQuantity(), - (int)$sourceItem->getStatus() + $this->defaultStockProvider->getId() ); + /** * We need to call setDataToLegacyStockStatus second time because we don't have On Save re-indexation * as cataloginventory_stock_item table updated with plane SQL queries @@ -99,7 +100,13 @@ public function afterExecute(SourceItemsSaveInterface $subject, $result, array $ $this->setDataToLegacyStockStatus->execute( $sourceItem->getSku(), (float)$sourceItem->getQuantity(), - (int)$this->isProductSalable->execute($sourceItem->getSku(), $this->defaultStockProvider->getId()) + $isSalable + ); + + $this->setDataToLegacyStockItem->execute( + $sourceItem->getSku(), + (float)$sourceItem->getQuantity(), + $isSalable ); } } From c1fbb51dfefc2f6293b2040ff0ac790019b13783 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 10 May 2018 14:15:07 +0300 Subject: [PATCH 02/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- ...atalogInventoryAtSourceItemsSavePlugin.php | 118 ++++++++++++------ .../Model/GetStockItemConfiguration.php | 18 +-- 2 files changed, 82 insertions(+), 54 deletions(-) diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php index 4e94fb689df9..92a552a72858 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php @@ -7,13 +7,18 @@ namespace Magento\InventoryCatalog\Plugin\InventoryApi; +use Magento\CatalogInventory\Api\Data\StockItemInterface; +use Magento\CatalogInventory\Api\StockItemRepositoryInterface; +use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; +use Magento\CatalogInventory\Model\Indexer\Stock\Processor; +use Magento\CatalogInventory\Model\Spi\StockStateProviderInterface; +use Magento\CatalogInventory\Model\Stock; +use Magento\Framework\Exception\InputException; use Magento\InventoryApi\Api\Data\SourceItemInterface; use Magento\InventoryApi\Api\SourceItemsSaveInterface; use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface; +use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface; use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem; -use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus; -use Magento\InventorySalesApi\Api\IsProductSalableInterface; -use Magento\InventoryCatalog\Api\DefaultStockProviderInterface; /** * Set Qty and status for legacy CatalogInventory Stock Status and Stock Item DB tables, @@ -32,39 +37,55 @@ class SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin private $setDataToLegacyStockItem; /** - * @var SetDataToLegacyStockStatus + * @var StockItemCriteriaInterfaceFactory */ - private $setDataToLegacyStockStatus; + private $legacyStockItemCriteriaFactory; /** - * @var IsProductSalableInterface + * @var StockItemRepositoryInterface */ - private $isProductSalable; + private $legacyStockItemRepository; /** - * @var DefaultStockProviderInterface + * @var GetProductIdsBySkusInterface */ - private $defaultStockProvider; + private $getProductIdsBySkus; + + /** + * @var StockStateProviderInterface + */ + private $stockStateProvider; + + /** + * @var Processor + */ + private $indexerProcessor; /** * @param DefaultSourceProviderInterface $defaultSourceProvider * @param SetDataToLegacyStockItem $setDataToLegacyStockItem - * @param SetDataToLegacyStockStatus $setDataToLegacyStockStatus - * @param IsProductSalableInterface $isProductSalable - * @param DefaultStockProviderInterface $defaultStockProvider + * @param StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory + * @param StockItemRepositoryInterface $legacyStockItemRepository + * @param GetProductIdsBySkusInterface $getProductIdsBySkus + * @param StockStateProviderInterface $stockStateProvider + * @param Processor $indexerProcessor */ public function __construct( DefaultSourceProviderInterface $defaultSourceProvider, SetDataToLegacyStockItem $setDataToLegacyStockItem, - SetDataToLegacyStockStatus $setDataToLegacyStockStatus, - IsProductSalableInterface $isProductSalable, - DefaultStockProviderInterface $defaultStockProvider + StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory, + StockItemRepositoryInterface $legacyStockItemRepository, + GetProductIdsBySkusInterface $getProductIdsBySkus, + StockStateProviderInterface $stockStateProvider, + Processor $indexerProcessor ) { $this->defaultSourceProvider = $defaultSourceProvider; $this->setDataToLegacyStockItem = $setDataToLegacyStockItem; - $this->setDataToLegacyStockStatus = $setDataToLegacyStockStatus; - $this->isProductSalable = $isProductSalable; - $this->defaultStockProvider = $defaultStockProvider; + $this->legacyStockItemCriteriaFactory = $legacyStockItemCriteriaFactory; + $this->legacyStockItemRepository = $legacyStockItemRepository; + $this->getProductIdsBySkus = $getProductIdsBySkus; + $this->stockStateProvider = $stockStateProvider; + $this->indexerProcessor = $indexerProcessor; } /** @@ -72,42 +93,59 @@ public function __construct( * @param void $result * @param SourceItemInterface[] $sourceItems * @return void - * @see SourceItemsSaveInterface::execute * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterExecute(SourceItemsSaveInterface $subject, $result, array $sourceItems) + public function afterExecute(SourceItemsSaveInterface $subject, $result, array $sourceItems): void { + $productIds = []; foreach ($sourceItems as $sourceItem) { if ($sourceItem->getSourceCode() !== $this->defaultSourceProvider->getCode()) { continue; } - $this->setDataToLegacyStockStatus->execute( - $sourceItem->getSku(), - (float)$sourceItem->getQuantity(), - (int)$sourceItem->getStatus() - ); + $sku = $sourceItem->getSku(); - $isSalable = (int)$this->isProductSalable->execute( - $sourceItem->getSku(), - $this->defaultStockProvider->getId() - ); + try { + $productId = $this->getProductIdsBySkus->execute([$sku])[$sku]; + } catch (InputException $e) { + // Saving source item data for not existed product + continue; + } - /** - * We need to call setDataToLegacyStockStatus second time because we don't have On Save re-indexation - * as cataloginventory_stock_item table updated with plane SQL queries - * Thus, initially we put the raw data there, and after that persist the calculated value - */ - $this->setDataToLegacyStockStatus->execute( - $sourceItem->getSku(), - (float)$sourceItem->getQuantity(), - $isSalable - ); + $isInStock = (int)$sourceItem->getStatus(); + + $legacyStockItem = $this->getLegacyStockItem($productId); + if ($legacyStockItem->getManageStock()) { + $legacyStockItem->setIsInStock($isInStock); + $legacyStockItem->setQty((float)$sourceItem->getQuantity()); + + $isInStock = (int)$this->stockStateProvider->verifyStock($legacyStockItem); + } $this->setDataToLegacyStockItem->execute( $sourceItem->getSku(), (float)$sourceItem->getQuantity(), - $isSalable + $isInStock ); + $productIds[] = $productId; } + + $this->indexerProcessor->reindexList($productIds); + } + + /** + * @param int $productId + * @return StockItemInterface + */ + private function getLegacyStockItem(int $productId): StockItemInterface + { + $searchCriteria = $this->legacyStockItemCriteriaFactory->create(); + + $searchCriteria->addFilter(StockItemInterface::PRODUCT_ID, StockItemInterface::PRODUCT_ID, $productId); + $searchCriteria->addFilter(StockItemInterface::STOCK_ID, StockItemInterface::STOCK_ID, Stock::DEFAULT_STOCK_ID); + + $stockItemCollection = $this->legacyStockItemRepository->getList($searchCriteria); + $stockItems = $stockItemCollection->getItems(); + $stockItem = reset($stockItems); + return $stockItem; } } diff --git a/app/code/Magento/InventoryConfiguration/Model/GetStockItemConfiguration.php b/app/code/Magento/InventoryConfiguration/Model/GetStockItemConfiguration.php index b9c68d179810..19752141e074 100644 --- a/app/code/Magento/InventoryConfiguration/Model/GetStockItemConfiguration.php +++ b/app/code/Magento/InventoryConfiguration/Model/GetStockItemConfiguration.php @@ -10,7 +10,7 @@ use Magento\CatalogInventory\Api\StockItemRepositoryInterface; use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; -use Magento\InventoryCatalog\Api\DefaultStockProviderInterface; +use Magento\CatalogInventory\Model\Stock; use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface; use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface; use Magento\InventorySales\Model\GetStockItemDataInterface; @@ -46,33 +46,25 @@ class GetStockItemConfiguration implements GetStockItemConfigurationInterface */ private $stockItemConfigurationFactory; - /** - * @var DefaultStockProviderInterface - */ - private $defaultStockProvider; - /** * @param GetStockItemDataInterface $getStockItemData * @param StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory * @param StockItemRepositoryInterface $legacyStockItemRepository * @param GetProductIdsBySkusInterface $getProductIdsBySkus * @param StockItemConfigurationFactory $stockItemConfigurationFactory - * @param DefaultStockProviderInterface $defaultStockProvider */ public function __construct( GetStockItemDataInterface $getStockItemData, StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory, StockItemRepositoryInterface $legacyStockItemRepository, GetProductIdsBySkusInterface $getProductIdsBySkus, - StockItemConfigurationFactory $stockItemConfigurationFactory, - DefaultStockProviderInterface $defaultStockProvider + StockItemConfigurationFactory $stockItemConfigurationFactory ) { $this->getStockItemData = $getStockItemData; $this->legacyStockItemCriteriaFactory = $legacyStockItemCriteriaFactory; $this->legacyStockItemRepository = $legacyStockItemRepository; $this->getProductIdsBySkus = $getProductIdsBySkus; $this->stockItemConfigurationFactory = $stockItemConfigurationFactory; - $this->defaultStockProvider = $defaultStockProvider; } /** @@ -105,10 +97,8 @@ private function getLegacyStockItem(string $sku): StockItemInterface $productId = $this->getProductIdsBySkus->execute([$sku])[$sku]; $searchCriteria->addFilter(StockItemInterface::PRODUCT_ID, StockItemInterface::PRODUCT_ID, $productId); - // TODO We use $legacyStockId until we have proper multi-stock item configuration - $legacyStockId = $this->defaultStockProvider->getId(); - - $searchCriteria->addFilter(StockItemInterface::STOCK_ID, StockItemInterface::STOCK_ID, $legacyStockId); + // TODO We use Stock::DEFAULT_STOCK_ID until we have proper multi-stock item configuration + $searchCriteria->addFilter(StockItemInterface::STOCK_ID, StockItemInterface::STOCK_ID, Stock::DEFAULT_STOCK_ID); $stockItemCollection = $this->legacyStockItemRepository->getList($searchCriteria); if ($stockItemCollection->getTotalCount() === 0) { From a4d02a19cc687c0f75b44a410146983b561884d7 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 10 May 2018 14:20:03 +0300 Subject: [PATCH 03/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- .../SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php index 92a552a72858..e8b352e49148 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php @@ -105,7 +105,7 @@ public function afterExecute(SourceItemsSaveInterface $subject, $result, array $ $sku = $sourceItem->getSku(); try { - $productId = $this->getProductIdsBySkus->execute([$sku])[$sku]; + $productId = (int)$this->getProductIdsBySkus->execute([$sku])[$sku]; } catch (InputException $e) { // Saving source item data for not existed product continue; From 321de2e06dc50de99ed1a25be8875e1ba22d81c8 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 10 May 2018 14:26:58 +0300 Subject: [PATCH 04/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- ...SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php index e8b352e49148..b9c401e1baee 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php @@ -129,7 +129,9 @@ public function afterExecute(SourceItemsSaveInterface $subject, $result, array $ $productIds[] = $productId; } - $this->indexerProcessor->reindexList($productIds); + if ($productIds) { + $this->indexerProcessor->reindexList($productIds); + } } /** From 78bf10588b3a0d3c11d644815fe4dd908500a68d Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 10 May 2018 14:47:42 +0300 Subject: [PATCH 05/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- ...SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php index b9c401e1baee..22b10f815a5c 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php @@ -118,7 +118,9 @@ public function afterExecute(SourceItemsSaveInterface $subject, $result, array $ $legacyStockItem->setIsInStock($isInStock); $legacyStockItem->setQty((float)$sourceItem->getQuantity()); - $isInStock = (int)$this->stockStateProvider->verifyStock($legacyStockItem); + if (false === $this->stockStateProvider->verifyStock($legacyStockItem)) { + $isInStock = 0; + } } $this->setDataToLegacyStockItem->execute( From a2b3e924fc65d7f75a4de0da06f8a8697b889142 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 10 May 2018 18:00:18 +0300 Subject: [PATCH 06/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- ...SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php index 22b10f815a5c..7e0885addff4 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php @@ -148,6 +148,10 @@ private function getLegacyStockItem(int $productId): StockItemInterface $searchCriteria->addFilter(StockItemInterface::STOCK_ID, StockItemInterface::STOCK_ID, Stock::DEFAULT_STOCK_ID); $stockItemCollection = $this->legacyStockItemRepository->getList($searchCriteria); + if ($stockItemCollection->getTotalCount() === 0) { + return \Magento\Framework\App\ObjectManager::getInstance()->create(StockItemInterface::class); + } + $stockItems = $stockItemCollection->getItems(); $stockItem = reset($stockItems); return $stockItem; From 23a707697ae7a78ff446dfee0020e6c7beaee54c Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 10 May 2018 19:13:36 +0300 Subject: [PATCH 07/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- ...atalogInventoryAtSourceItemsSavePlugin.php | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php index 7e0885addff4..9b1ed5dbf620 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php @@ -18,7 +18,9 @@ use Magento\InventoryApi\Api\SourceItemsSaveInterface; use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface; use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface; +use Magento\InventoryCatalog\Model\GetProductTypesBySkusInterface; use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem; +use Magento\InventoryConfiguration\Model\IsSourceItemsAllowedForProductTypeInterface; /** * Set Qty and status for legacy CatalogInventory Stock Status and Stock Item DB tables, @@ -61,6 +63,16 @@ class SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin */ private $indexerProcessor; + /** + * @var IsSourceItemsAllowedForProductTypeInterface + */ + private $isSourceItemsAllowedForProductType; + + /** + * @var GetProductTypesBySkusInterface + */ + private $getProductTypeBySku; + /** * @param DefaultSourceProviderInterface $defaultSourceProvider * @param SetDataToLegacyStockItem $setDataToLegacyStockItem @@ -69,6 +81,8 @@ class SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin * @param GetProductIdsBySkusInterface $getProductIdsBySkus * @param StockStateProviderInterface $stockStateProvider * @param Processor $indexerProcessor + * @param IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType + * @param GetProductTypesBySkusInterface $getProductTypeBySku */ public function __construct( DefaultSourceProviderInterface $defaultSourceProvider, @@ -77,7 +91,9 @@ public function __construct( StockItemRepositoryInterface $legacyStockItemRepository, GetProductIdsBySkusInterface $getProductIdsBySkus, StockStateProviderInterface $stockStateProvider, - Processor $indexerProcessor + Processor $indexerProcessor, + IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType, + GetProductTypesBySkusInterface $getProductTypeBySku ) { $this->defaultSourceProvider = $defaultSourceProvider; $this->setDataToLegacyStockItem = $setDataToLegacyStockItem; @@ -86,6 +102,8 @@ public function __construct( $this->getProductIdsBySkus = $getProductIdsBySkus; $this->stockStateProvider = $stockStateProvider; $this->indexerProcessor = $indexerProcessor; + $this->isSourceItemsAllowedForProductType = $isSourceItemsAllowedForProductType; + $this->getProductTypeBySku = $getProductTypeBySku; } /** @@ -102,6 +120,7 @@ public function afterExecute(SourceItemsSaveInterface $subject, $result, array $ if ($sourceItem->getSourceCode() !== $this->defaultSourceProvider->getCode()) { continue; } + $sku = $sourceItem->getSku(); try { @@ -111,9 +130,18 @@ public function afterExecute(SourceItemsSaveInterface $subject, $result, array $ continue; } - $isInStock = (int)$sourceItem->getStatus(); + $typeId = $this->getProductTypeBySku->execute([$sku])[$sku]; + if (false === $this->isSourceItemsAllowedForProductType->execute($typeId)) { + continue; + } $legacyStockItem = $this->getLegacyStockItem($productId); + if (null === $legacyStockItem) { + continue; + } + + $isInStock = (int)$sourceItem->getStatus(); + if ($legacyStockItem->getManageStock()) { $legacyStockItem->setIsInStock($isInStock); $legacyStockItem->setQty((float)$sourceItem->getQuantity()); @@ -138,9 +166,9 @@ public function afterExecute(SourceItemsSaveInterface $subject, $result, array $ /** * @param int $productId - * @return StockItemInterface + * @return null|StockItemInterface */ - private function getLegacyStockItem(int $productId): StockItemInterface + private function getLegacyStockItem(int $productId): ?StockItemInterface { $searchCriteria = $this->legacyStockItemCriteriaFactory->create(); @@ -149,7 +177,7 @@ private function getLegacyStockItem(int $productId): StockItemInterface $stockItemCollection = $this->legacyStockItemRepository->getList($searchCriteria); if ($stockItemCollection->getTotalCount() === 0) { - return \Magento\Framework\App\ObjectManager::getInstance()->create(StockItemInterface::class); + return null; } $stockItems = $stockItemCollection->getItems(); From c0603f5acb4b21f54d495005342750b8e73837ed Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 10 May 2018 20:31:25 +0300 Subject: [PATCH 08/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- .../SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php index 9b1ed5dbf620..16cbbde3ba28 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php @@ -25,6 +25,9 @@ /** * Set Qty and status for legacy CatalogInventory Stock Status and Stock Item DB tables, * if corresponding MSI SourceItem assigned to Default Source has been saved + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * TODO: https://github.com/magento-engcom/msi/pull/1082/ */ class SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin { From 0d675bd3ad2f61599a62fe3e1f756e32ba6cef76 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Mon, 14 May 2018 13:33:18 +0300 Subject: [PATCH 09/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- .../Model/SourceItem/SourceItemsSave.php | 74 +++++++++++++++++++ app/code/Magento/InventoryCatalog/etc/di.xml | 8 ++ 2 files changed, 82 insertions(+) create mode 100644 app/code/Magento/InventoryCatalog/Model/SourceItem/SourceItemsSave.php diff --git a/app/code/Magento/InventoryCatalog/Model/SourceItem/SourceItemsSave.php b/app/code/Magento/InventoryCatalog/Model/SourceItem/SourceItemsSave.php new file mode 100644 index 000000000000..6918445ce1e1 --- /dev/null +++ b/app/code/Magento/InventoryCatalog/Model/SourceItem/SourceItemsSave.php @@ -0,0 +1,74 @@ +sourceItemsValidator = $sourceItemsValidator; + $this->saveMultiple = $saveMultiple; + $this->logger = $logger; + } + + /** + * @inheritdoc + */ + public function execute(array $sourceItems) + { + if (empty($sourceItems)) { + throw new InputException(__('Input data is empty')); + } + + $validationResult = $this->sourceItemsValidator->validate($sourceItems); + if (!$validationResult->isValid()) { + throw new ValidationException(__('Validation Failed'), null, 0, $validationResult); + } + + try { + $this->saveMultiple->execute($sourceItems); + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + throw new CouldNotSaveException(__('Could not save Source Item'), $e); + } + } +} diff --git a/app/code/Magento/InventoryCatalog/etc/di.xml b/app/code/Magento/InventoryCatalog/etc/di.xml index a75ecd3853fb..9f2ce6d99177 100644 --- a/app/code/Magento/InventoryCatalog/etc/di.xml +++ b/app/code/Magento/InventoryCatalog/etc/di.xml @@ -60,4 +60,12 @@ + + + Magento\InventoryCatalog\Model\SourceItem\SourceItemsSave + + + + + From 5d055d39183ef136adf54d6c61a629a655a22c13 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Mon, 14 May 2018 13:42:08 +0300 Subject: [PATCH 10/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- .../SetDataToLegacyStockStatus.php | 68 ------------------- ...alogInventoryAtSourceItemsDeletePlugin.php | 65 +++++++++++++++--- 2 files changed, 55 insertions(+), 78 deletions(-) delete mode 100644 app/code/Magento/InventoryCatalog/Model/ResourceModel/SetDataToLegacyStockStatus.php diff --git a/app/code/Magento/InventoryCatalog/Model/ResourceModel/SetDataToLegacyStockStatus.php b/app/code/Magento/InventoryCatalog/Model/ResourceModel/SetDataToLegacyStockStatus.php deleted file mode 100644 index 523a3d772eb8..000000000000 --- a/app/code/Magento/InventoryCatalog/Model/ResourceModel/SetDataToLegacyStockStatus.php +++ /dev/null @@ -1,68 +0,0 @@ -resourceConnection = $resourceConnection; - $this->getProductIdsBySkus = $getProductIdsBySkus; - } - - /** - * @param string $sku - * @param float $quantity - * @param int $status - * @return void - */ - public function execute(string $sku, float $quantity, int $status) - { - $productIds = $this->getProductIdsBySkus->execute([$sku]); - - if (isset($productIds[$sku])) { - $productId = $productIds[$sku]; - - $connection = $this->resourceConnection->getConnection(); - $connection->update( - $this->resourceConnection->getTableName('cataloginventory_stock_status'), - [ - StockStatusInterface::QTY => $quantity, - StockStatusInterface::STOCK_STATUS => $status, - ], - [ - StockStatusInterface::PRODUCT_ID . ' = ?' => $productId, - 'website_id = ?' => 0, - ] - ); - } - } -} diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin.php index 2871ace146d1..18d17367b4c8 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin.php @@ -7,13 +7,15 @@ namespace Magento\InventoryCatalog\Plugin\InventoryApi; -use Magento\CatalogInventory\Model\Stock\Status; +use Magento\CatalogInventory\Model\Indexer\Stock\Processor; +use Magento\Framework\Exception\InputException; use Magento\InventoryApi\Api\Data\SourceItemInterface; use Magento\InventoryApi\Api\SourceItemsDeleteInterface; use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface; +use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface; +use Magento\InventoryCatalog\Model\GetProductTypesBySkusInterface; use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem; -use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus; -use Psr\Log\LoggerInterface; +use Magento\InventoryConfiguration\Model\IsSourceItemsAllowedForProductTypeInterface; /** * Set to zero Qty and status to ‘Out of Stock’ for legacy CatalogInventory Stock Status and Stock Item DB tables, @@ -32,23 +34,47 @@ class SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin private $setDataToLegacyStockItem; /** - * @var SetDataToLegacyStockStatus + * @var GetProductIdsBySkusInterface */ - private $setDataToLegacyStockStatus; + private $getProductIdsBySkus; + + /** + * @var Processor + */ + private $indexerProcessor; + + /** + * @var IsSourceItemsAllowedForProductTypeInterface + */ + private $isSourceItemsAllowedForProductType; + + /** + * @var GetProductTypesBySkusInterface + */ + private $getProductTypeBySku; /** * @param DefaultSourceProviderInterface $defaultSourceProvider * @param SetDataToLegacyStockItem $setDataToLegacyStockItem - * @param SetDataToLegacyStockStatus $setDataToLegacyStockStatus + * @param GetProductIdsBySkusInterface $getProductIdsBySkus + * @param Processor $indexerProcessor + * @param IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType + * @param GetProductTypesBySkusInterface $getProductTypeBySku */ public function __construct( DefaultSourceProviderInterface $defaultSourceProvider, SetDataToLegacyStockItem $setDataToLegacyStockItem, - SetDataToLegacyStockStatus $setDataToLegacyStockStatus + GetProductIdsBySkusInterface $getProductIdsBySkus, + Processor $indexerProcessor, + IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType, + GetProductTypesBySkusInterface $getProductTypeBySku ) { $this->defaultSourceProvider = $defaultSourceProvider; $this->setDataToLegacyStockItem = $setDataToLegacyStockItem; - $this->setDataToLegacyStockStatus = $setDataToLegacyStockStatus; + $this->getProductIdsBySkus = $getProductIdsBySkus; + $this->indexerProcessor = $indexerProcessor; + $this->isSourceItemsAllowedForProductType = $isSourceItemsAllowedForProductType; + $this->getProductTypeBySku = $getProductTypeBySku; } /** @@ -56,17 +82,36 @@ public function __construct( * @param void $result * @param SourceItemInterface[] $sourceItems * @return void - * @see SourceItemsDeleteInterface::execute * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterExecute(SourceItemsDeleteInterface $subject, $result, array $sourceItems) { + $productIds = []; foreach ($sourceItems as $sourceItem) { if ($sourceItem->getSourceCode() !== $this->defaultSourceProvider->getCode()) { continue; } + + $sku = $sourceItem->getSku(); + + try { + $productId = (int)$this->getProductIdsBySkus->execute([$sku])[$sku]; + } catch (InputException $e) { + // Delete source item data for not existed product + continue; + } + + $typeId = $this->getProductTypeBySku->execute([$sku])[$sku]; + if (false === $this->isSourceItemsAllowedForProductType->execute($typeId)) { + continue; + } + $this->setDataToLegacyStockItem->execute($sourceItem->getSku(), 0, 0); - $this->setDataToLegacyStockStatus->execute($sourceItem->getSku(), 0, Status::STATUS_OUT_OF_STOCK); + $productIds[] = $productId; + } + + if ($productIds) { + $this->indexerProcessor->reindexList($productIds); } } } From a1425bbe5bd1ecdd812cbb4e80b1b399e037dd0b Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Mon, 14 May 2018 18:05:28 +0300 Subject: [PATCH 11/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- .../InventoryConfiguration/Model/GetStockItemConfiguration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/InventoryConfiguration/Model/GetStockItemConfiguration.php b/app/code/Magento/InventoryConfiguration/Model/GetStockItemConfiguration.php index a249ce997584..daa1bf94e169 100644 --- a/app/code/Magento/InventoryConfiguration/Model/GetStockItemConfiguration.php +++ b/app/code/Magento/InventoryConfiguration/Model/GetStockItemConfiguration.php @@ -10,6 +10,7 @@ use Magento\CatalogInventory\Api\StockItemRepositoryInterface; use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; +use Magento\CatalogInventory\Model\Stock; use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface; use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface; use Magento\InventorySales\Model\GetStockItemDataInterface; From 4818199d50cb22917a6314e97ed5b1a7b9cff802 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Tue, 15 May 2018 01:16:56 +0300 Subject: [PATCH 12/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- app/code/Magento/InventoryCatalog/composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/InventoryCatalog/composer.json b/app/code/Magento/InventoryCatalog/composer.json index afe01ef089dc..b71ba2ba9eb0 100644 --- a/app/code/Magento/InventoryCatalog/composer.json +++ b/app/code/Magento/InventoryCatalog/composer.json @@ -6,6 +6,7 @@ "magento/framework": "*", "magento/module-catalog": "*", "magento/module-catalog-inventory": "*", + "magento/module-inventory": "*", "magento/module-inventory-api": "*", "magento/module-inventory-catalog-api": "*", "magento/module-store": "*", From 049e2cb7a2e403875a0a77a1f3e4610a5f1c8412 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Tue, 15 May 2018 11:23:42 +0300 Subject: [PATCH 13/13] MSI-793: Incorrect process of Out-of-Stock Threshold --- ...ataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php | 8 ++++---- ...eroLegacyCatalogInventoryAtSourceItemsDeletePlugin.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php index 6c0e02d5e1cf..e7eca12d4fe1 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin.php @@ -20,7 +20,7 @@ use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface; use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface; use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem; -use Magento\InventoryConfiguration\Model\IsSourceItemsAllowedForProductTypeInterface; +use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface; /** * Set Qty and status for legacy CatalogInventory Stock Status and Stock Item DB tables, @@ -67,7 +67,7 @@ class SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin private $indexerProcessor; /** - * @var IsSourceItemsAllowedForProductTypeInterface + * @var IsSourceItemManagementAllowedForProductTypeInterface */ private $isSourceItemsAllowedForProductType; @@ -84,7 +84,7 @@ class SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin * @param GetProductIdsBySkusInterface $getProductIdsBySkus * @param StockStateProviderInterface $stockStateProvider * @param Processor $indexerProcessor - * @param IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType + * @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemsAllowedForProductType * @param GetProductTypesBySkusInterface $getProductTypeBySku */ public function __construct( @@ -95,7 +95,7 @@ public function __construct( GetProductIdsBySkusInterface $getProductIdsBySkus, StockStateProviderInterface $stockStateProvider, Processor $indexerProcessor, - IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType, + IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemsAllowedForProductType, GetProductTypesBySkusInterface $getProductTypeBySku ) { $this->defaultSourceProvider = $defaultSourceProvider; diff --git a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin.php b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin.php index ec6197807c94..79812e026052 100644 --- a/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin.php +++ b/app/code/Magento/InventoryCatalog/Plugin/InventoryApi/SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin.php @@ -15,7 +15,7 @@ use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface; use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface; use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockItem; -use Magento\InventoryConfiguration\Model\IsSourceItemsAllowedForProductTypeInterface; +use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface; /** * Set to zero Qty and status to ‘Out of Stock’ for legacy CatalogInventory Stock Status and Stock Item DB tables, @@ -44,7 +44,7 @@ class SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin private $indexerProcessor; /** - * @var IsSourceItemsAllowedForProductTypeInterface + * @var IsSourceItemManagementAllowedForProductTypeInterface */ private $isSourceItemsAllowedForProductType; @@ -58,7 +58,7 @@ class SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin * @param SetDataToLegacyStockItem $setDataToLegacyStockItem * @param GetProductIdsBySkusInterface $getProductIdsBySkus * @param Processor $indexerProcessor - * @param IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType + * @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemsAllowedForProductType * @param GetProductTypesBySkusInterface $getProductTypeBySku */ public function __construct( @@ -66,7 +66,7 @@ public function __construct( SetDataToLegacyStockItem $setDataToLegacyStockItem, GetProductIdsBySkusInterface $getProductIdsBySkus, Processor $indexerProcessor, - IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType, + IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemsAllowedForProductType, GetProductTypesBySkusInterface $getProductTypeBySku ) { $this->defaultSourceProvider = $defaultSourceProvider;