From f1bc2cc40f1a654af8562e8f7329ec6c55172fbe Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sat, 27 Aug 2022 00:08:23 +0200 Subject: [PATCH] Fixed that consuming partially fulfilled recipes was possible (fixes #1981) --- changelog/69_UNRELEASED_xxxx-xx-xx.md | 1 + services/RecipesService.php | 18 ++++++++++++++---- services/StockService.php | 2 -- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/changelog/69_UNRELEASED_xxxx-xx-xx.md b/changelog/69_UNRELEASED_xxxx-xx-xx.md index c0bbaf91..22d2433a 100644 --- a/changelog/69_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/69_UNRELEASED_xxxx-xx-xx.md @@ -66,3 +66,4 @@ - Endpoint `/stock/volatile` - The field/property `missing_products` now also contains the `product` object +- Endpoint `/recipes/{recipeId}/consume`: Fixed (again) that consuming partially fulfilled recipes was possible, although an error was already returned in that case (and potentially some of the in stock ingredients were consumed in fact) diff --git a/services/RecipesService.php b/services/RecipesService.php index 1a50a897..bb43ecc7 100644 --- a/services/RecipesService.php +++ b/services/RecipesService.php @@ -81,15 +81,25 @@ class RecipesService extends BaseService } $transactionId = uniqid(); - $recipePositions = $this->getDatabase()->recipes_pos_resolved()->where('recipe_id', $recipeId)->fetchAll(); - foreach ($recipePositions as $recipePosition) + + $this->getDatabaseService()->GetDbConnectionRaw()->beginTransaction(); + try { - if ($recipePosition->only_check_single_unit_in_stock == 0) + foreach ($recipePositions as $recipePosition) { - $this->getStockService()->ConsumeProduct($recipePosition->product_id_effective, $recipePosition->recipe_amount, false, StockService::TRANSACTION_TYPE_CONSUME, 'default', $recipeId, null, $transactionId, true, true); + if ($recipePosition->only_check_single_unit_in_stock == 0) + { + $this->getStockService()->ConsumeProduct($recipePosition->product_id_effective, $recipePosition->recipe_amount, false, StockService::TRANSACTION_TYPE_CONSUME, 'default', $recipeId, null, $transactionId, true, true); + } } } + catch (Exception $ex) + { + $this->getDatabaseService()->GetDbConnectionRaw()->rollback(); + throw $ex; + } + $this->getDatabaseService()->GetDbConnectionRaw()->commit(); $recipeRow = $this->getDatabase()->recipes()->where('id = :1', $recipeId)->fetch(); if (!empty($recipeRow->product_id)) diff --git a/services/StockService.php b/services/StockService.php index d99f489a..85f57803 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -493,8 +493,6 @@ class StockService extends BaseService } } - $this->CompactStockEntries($productId); - if (boolval($this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'shopping_list_auto_add_below_min_stock_amount'))) { $this->AddMissingProductsToShoppingList(intval($this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'shopping_list_auto_add_below_min_stock_amount_list_id')));