From ef9413c5c1ea81f10acdfec517ab414c3f2bc095 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sun, 19 Jan 2025 21:31:36 +0100 Subject: [PATCH] Optimized recipe missing ingredients shopping list item handling (closes #1890, closes #2134) --- changelog/77_UNRELEASED_xxxx-xx-xx.md | 5 ++++- localization/strings.pot | 3 --- services/RecipesService.php | 28 ++++++++++++++++----------- services/StockService.php | 1 - 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/changelog/77_UNRELEASED_xxxx-xx-xx.md b/changelog/77_UNRELEASED_xxxx-xx-xx.md index 4676a5b1..4a59652d 100644 --- a/changelog/77_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/77_UNRELEASED_xxxx-xx-xx.md @@ -52,7 +52,10 @@ - Added a new recipe ingredient option "Round up quantity amounts to the nearest whole number" - When enabled, resulting quantity amounts (after scaling according the desired serving amount) are always rounded up to the nearest whole number - Defaults to disabled, so no changed behavior when not configured -- Optimized that when adding missing recipe ingredients with the option "Only check if any amount is in stock" enabled to the shopping list and when no corresponding unit conversion exists, the amount/unit is now taken "as is" (as defined in the recipe ingredient) into the created shopping list item +- Optimizations/changes when adding missing recipe ingredients to the shopping list: + - When the ingredient option "Only check if any amount is in stock" is enabled and when no corresponding unit conversion exists, the amount/unit is now taken "as is" (as defined in the recipe ingredient) into the created shopping list item + - The shopping list item no longer gets a note "Added for recipe" set and the ingredient note is no longer appended + - When the corresponding product is already on the shopping list, the amount of the existing item is incremented instead of creating a new shopping list item - When no price information is available for at least one ingredient, a red exclamation mark is now displayed next to the recipe total cost information - Fixed that calories/costs of recipe ingredients were wrong when the ingredient option "Only check if any amount is in stock" was set and the on the ingredient used quantity unit was different from the product's QU stock - Fixed that multi-nested recipes (at least 3 levels of "included recipes") resulted in wrong amounts/costs/calories calculated for the ingredients orginating in those nested recipes (also affected the meal plan) diff --git a/localization/strings.pot b/localization/strings.pot index 3f3aa1ff..7f190871 100644 --- a/localization/strings.pot +++ b/localization/strings.pot @@ -476,9 +476,6 @@ msgstr "" msgid "Are you sure you want to put all missing ingredients for recipe \"%s\" on the shopping list?" msgstr "" -msgid "Added for recipe %s" -msgstr "" - msgid "Manage users" msgstr "" diff --git a/services/RecipesService.php b/services/RecipesService.php index 2af4f9dd..bdcfbf02 100644 --- a/services/RecipesService.php +++ b/services/RecipesService.php @@ -46,6 +46,7 @@ class RecipesService extends BaseService } else { + // No conversion exists => take the amount/unit as is $quId = $recipePosition->qu_id; $toOrderAmount = $recipePosition->missing_amount; } @@ -53,19 +54,24 @@ class RecipesService extends BaseService if ($toOrderAmount > 0) { - $note = $this->getLocalizationService()->__t('Added for recipe %s', $recipe->name); - if (!empty($recipePosition->note)) + $alreadyExistingEntry = $this->getDatabase()->shopping_list()->where('product_id', $recipePosition->product_id)->fetch(); + if ($alreadyExistingEntry) { - $note .= "\n" . $recipePosition->note; + // Update + $alreadyExistingEntry->update([ + 'amount' => $alreadyExistingEntry->amount + $toOrderAmount + ]); + } + else + { + // Insert + $shoppinglistRow = $this->getDataBase()->shopping_list()->createRow([ + 'product_id' => $recipePosition->product_id, + 'amount' => $toOrderAmount, + 'qu_id' => $quId + ]); + $shoppinglistRow->save(); } - - $shoppinglistRow = $this->getDataBase()->shopping_list()->createRow([ - 'product_id' => $recipePosition->product_id, - 'amount' => $toOrderAmount, - 'qu_id' => $quId, - 'note' => $note - ]); - $shoppinglistRow->save(); } } } diff --git a/services/StockService.php b/services/StockService.php index 7adad17a..2261db4e 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -32,7 +32,6 @@ class StockService extends BaseService $amountToAdd = round($missingProduct->amount_missing, 2); $alreadyExistingEntry = $this->getDatabase()->shopping_list()->where('product_id', $missingProduct->id)->fetch(); - if ($alreadyExistingEntry) { // Update