From 8c0ff04caa7fa7f441c158547e320713a51b56cc Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Fri, 1 Jan 2021 13:27:57 +0100 Subject: [PATCH] Fixed consume stock amount check for product substitution (fixes #1240) --- services/StockService.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/services/StockService.php b/services/StockService.php index 87610e6d..8aa04bf0 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -277,8 +277,10 @@ class StockService extends BaseService $potentialStockEntries = FindAllObjectsInArrayByPropertyValue($potentialStockEntries, 'stock_id', $specificStockEntryId); } - $productStockAmount = SumArrayValue($potentialStockEntries, 'amount'); - + // TODO: This check doesn't really check against products only at the given location + // (as GetProductDetails returns the stock_amount_aggregated of all locations) + // However, $potentialStockEntries are filtered accordingly, so this currently isn't really a problem at the end + $productStockAmount = ((object)$this->GetProductDetails($productId))->stock_amount_aggregated; if ($amount > $productStockAmount) { throw new \Exception('Amount to be consumed cannot be > current stock amount (if supplied, at the desired location)'); @@ -604,7 +606,6 @@ class StockService extends BaseService $consumeCount = $this->getDatabase()->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->where('undone = 0 AND spoiled = 0')->sum('amount') * -1; $consumeCountSpoiled = $this->getDatabase()->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->where('undone = 0 AND spoiled = 1')->sum('amount') * -1; - if ($consumeCount == 0) { $consumeCount = 1; @@ -727,7 +728,7 @@ class StockService extends BaseService throw new \Exception('Product does not exist or is inactive'); } - $productDetails = (object) $this->GetProductDetails($productId); + $productDetails = (object)$this->GetProductDetails($productId); if ($price === null) {