Fixed that consuming partially fulfilled recipes was possible (fixes #1981)

This commit is contained in:
Bernd Bestel 2022-08-27 00:08:23 +02:00
parent c0d0b8fc90
commit f1bc2cc40f
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
3 changed files with 15 additions and 6 deletions

View File

@ -66,3 +66,4 @@
- Endpoint `/stock/volatile` - Endpoint `/stock/volatile`
- The field/property `missing_products` now also contains the `product` object - 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)

View File

@ -81,15 +81,25 @@ class RecipesService extends BaseService
} }
$transactionId = uniqid(); $transactionId = uniqid();
$recipePositions = $this->getDatabase()->recipes_pos_resolved()->where('recipe_id', $recipeId)->fetchAll(); $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(); $recipeRow = $this->getDatabase()->recipes()->where('id = :1', $recipeId)->fetch();
if (!empty($recipeRow->product_id)) if (!empty($recipeRow->product_id))

View File

@ -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'))) 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'))); $this->AddMissingProductsToShoppingList(intval($this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'shopping_list_auto_add_below_min_stock_amount_list_id')));