From 56c0ed6ea514f1a465c4177ed4b54c84dc7ea906 Mon Sep 17 00:00:00 2001 From: Reense Date: Fri, 2 Feb 2024 09:35:30 +0100 Subject: [PATCH] Added support for stock updates via REST api --- Helper/Data.php | 2 ++ Plugin/Stock/StockItemRepository.php | 6 +++++ Plugin/Webapi/Stock/StockItemRepository.php | 27 +++++++++++++++++++++ etc/di.xml | 2 +- etc/webapi_rest/di.xml | 9 +++++++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 Plugin/Webapi/Stock/StockItemRepository.php create mode 100644 etc/webapi_rest/di.xml diff --git a/Helper/Data.php b/Helper/Data.php index c06330d..f849a7d 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -23,6 +23,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper const STOCK_UPDATE = 1; const ORDER_CANCEL = 2; const CREDIT_MEMO = 3; + const WEBAPI_STOCK_UPDATE = 4; + const MOVEMENT_SECTION = "movement_section"; const NEW_PRODUCT = "new_product"; const MOVEMENT_DATA = "movement_data"; diff --git a/Plugin/Stock/StockItemRepository.php b/Plugin/Stock/StockItemRepository.php index b4c9a9c..3c4d3cd 100755 --- a/Plugin/Stock/StockItemRepository.php +++ b/Plugin/Stock/StockItemRepository.php @@ -108,6 +108,12 @@ public function afterSave( $message = __('Product restocked after order cancellation'); } $this->movementRepository->insertStockMovement($stockItem, $message); + } elseif ($movementSection === InventoryLogHelper::WEBAPI_STOCK_UPDATE) { + $message = __('Stock updated via WebAPI'); + $stockAutoFlag = $stockItem->getStockStatusChangedAutomaticallyFlag(); + if (!$stockAutoFlag || $stockItem->getOldQty() != $stockItem->getQty() || $newProduct == 1) { + $this->movementRepository->insertStockMovement($stockItem, $message, $newProduct); + } } $this->helper->unRegisterAllData(); } diff --git a/Plugin/Webapi/Stock/StockItemRepository.php b/Plugin/Webapi/Stock/StockItemRepository.php new file mode 100644 index 0000000..8d85b0d --- /dev/null +++ b/Plugin/Webapi/Stock/StockItemRepository.php @@ -0,0 +1,27 @@ +helper->isModuleEnabled()) { + return [$stockItem]; + } + + $this->registry->register( + \Elgentos\InventoryLog\Helper\Data::MOVEMENT_SECTION, + \Elgentos\InventoryLog\Helper\Data::WEBAPI_STOCK_UPDATE + ); + + return [$stockItem]; + } +} diff --git a/etc/di.xml b/etc/di.xml index cd6d19e..a90f94b 100755 --- a/etc/di.xml +++ b/etc/di.xml @@ -15,7 +15,7 @@ --> - + + + + + +