From bb9caf9cc9f7a78aafb14de61724dfb7342e4ef6 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Tue, 2 Oct 2018 17:06:21 +0200 Subject: [PATCH] Fixed volatil stock logic (fixes #69) --- controllers/StockApiController.php | 2 +- services/StockService.php | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 350a580f..04f9e04f 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -126,7 +126,7 @@ class StockApiController extends BaseApiController $nextXDays = $request->getQueryParams()['expiring_days']; } - $expiringProducts = $this->StockService->GetExpiringProducts($nextXDays); + $expiringProducts = $this->StockService->GetExpiringProducts($nextXDays, true); $expiredProducts = $this->StockService->GetExpiringProducts(-1); $missingProducts = $this->StockService->GetMissingProducts(); return $this->ApiResponse(array( diff --git a/services/StockService.php b/services/StockService.php index c6f81c90..9cf0c95d 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -8,9 +8,14 @@ class StockService extends BaseService const TRANSACTION_TYPE_CONSUME = 'consume'; const TRANSACTION_TYPE_INVENTORY_CORRECTION = 'inventory-correction'; - public function GetCurrentStock() + public function GetCurrentStock($includeNotInStockButMissingProducts = false) { $sql = 'SELECT * from stock_current'; + if ($includeNotInStockButMissingProducts) + { + $sql = 'SELECT * from stock_current WHERE best_before_date IS NOT NULL'; + } + return $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); } @@ -20,10 +25,17 @@ class StockService extends BaseService return $this->DatabaseService->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); } - public function GetExpiringProducts(int $days = 5) + public function GetExpiringProducts(int $days = 5, bool $excludeExpired = false) { - $currentStock = $this->GetCurrentStock(); - return FindAllObjectsInArrayByPropertyValue($currentStock, 'best_before_date', date('Y-m-d', strtotime("+$days days")), '<'); + $currentStock = $this->GetCurrentStock(true); + $currentStock = FindAllObjectsInArrayByPropertyValue($currentStock, 'best_before_date', date('Y-m-d', strtotime("+$days days")), '<'); + + if ($excludeExpired) + { + $currentStock = FindAllObjectsInArrayByPropertyValue($currentStock, 'best_before_date', date('Y-m-d', strtotime('now')), '>'); + } + + return $currentStock; } public function GetProductDetails(int $productId)