From 71d44edb8cf689cef627d15528f1878ed2aadbe6 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sat, 1 Apr 2023 22:04:30 +0200 Subject: [PATCH] Optimized week recipe handling in mealplan (fixes #2168) --- changelog/70_UNRELEASED_xxxx.xx.xx.md | 1 + controllers/RecipesController.php | 5 +++-- migrations/0217.sql | 27 +++++++++++++++++++++++++++ public/viewjs/mealplan.js | 11 +++++------ views/mealplan.blade.php | 7 ++++--- 5 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 migrations/0217.sql diff --git a/changelog/70_UNRELEASED_xxxx.xx.xx.md b/changelog/70_UNRELEASED_xxxx.xx.xx.md index f1fe2b5c..ccb4d372 100644 --- a/changelog/70_UNRELEASED_xxxx.xx.xx.md +++ b/changelog/70_UNRELEASED_xxxx.xx.xx.md @@ -52,6 +52,7 @@ - Added a new sub feature flag `FEATURE_FLAG_RECIPES_MEALPLAN` (in `config.php`) to only disable the meal plan if not needed (thanks @webysther) - Fixed that consuming a recipe from the meal plan didn't add its "Produces product"-product to stock (if any) +- Fixed that the "Put missing products on shopping list"-button in the header (to put all missing products on the shopping list for a whole week) was missing under certain circumstances (related to locale week numbers and turn of the year) ### Chores diff --git a/controllers/RecipesController.php b/controllers/RecipesController.php index fdf9edfc..da65ddeb 100644 --- a/controllers/RecipesController.php +++ b/controllers/RecipesController.php @@ -23,7 +23,7 @@ class RecipesController extends BaseController $days = $request->getQueryParams()['days']; } - $mealPlanWhereTimespan = "day BETWEEN DATE('$start') AND DATE('$start', '+$days days')"; + $mealPlanWhereTimespan = "day BETWEEN DATE('$start', '-$days days') AND DATE('$start', '+$days days')"; $recipes = $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->fetchAll(); $events = []; @@ -64,7 +64,8 @@ class RecipesController extends BaseController 'quantityUnits' => $this->getDatabase()->quantity_units()->orderBy('name', 'COLLATE NOCASE'), 'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved(), 'mealplanSections' => $this->getDatabase()->meal_plan_sections()->orderBy('sort_number'), - 'usedMealplanSections' => $this->getDatabase()->meal_plan_sections()->where("id IN (SELECT section_id FROM meal_plan WHERE $mealPlanWhereTimespan)")->orderBy('sort_number') + 'usedMealplanSections' => $this->getDatabase()->meal_plan_sections()->where("id IN (SELECT section_id FROM meal_plan WHERE $mealPlanWhereTimespan)")->orderBy('sort_number'), + 'weekRecipe' => $this->getDatabase()->recipes()->where("type = 'mealplan-week' AND name = LTRIM(STRFTIME('%Y-%W', DATE('$start')), '0')")->fetch() ]); } diff --git a/migrations/0217.sql b/migrations/0217.sql new file mode 100644 index 00000000..3845c27a --- /dev/null +++ b/migrations/0217.sql @@ -0,0 +1,27 @@ +DROP VIEW meal_plan_internal_recipe_relation; +CREATE VIEW meal_plan_internal_recipe_relation +AS + +-- Relation between a meal plan (day) and the corresponding internal recipe(s) + +SELECT mp.day, r.id AS recipe_id +FROM meal_plan mp +JOIN recipes r + ON r.name = CAST(mp.day AS TEXT) + AND r.type = 'mealplan-day' + +UNION + +SELECT mp.day, r.id AS recipe_id +FROM meal_plan mp +JOIN recipes r + ON r.name = LTRIM(STRFTIME('%Y-%W', mp.day), '0') + AND r.type = 'mealplan-week' + +UNION + +SELECT mp.day, r.id AS recipe_id +FROM meal_plan mp +JOIN recipes r + ON r.name = CAST(mp.day AS TEXT) || '#' || CAST(mp.id AS TEXT) + AND r.type = 'mealplan-shadow'; diff --git a/public/viewjs/mealplan.js b/public/viewjs/mealplan.js index 38b8a720..e5241a24 100644 --- a/public/viewjs/mealplan.js +++ b/public/viewjs/mealplan.js @@ -73,29 +73,28 @@ $(".calendar").each(function() \ '); - var weekRecipeName = view.start.year().toString() + "-" + ((view.start.week() - 1).toString().padStart(2, "0")).toString(); - var weekRecipe = FindObjectInArrayByPropertyValue(internalRecipes, "name", weekRecipeName); - var weekCosts = 0; var weekRecipeOrderMissingButtonHtml = ""; var weekRecipeConsumeButtonHtml = ""; var weekCostsHtml = ""; if (weekRecipe !== null) { + var weekRecipeResolved = FindObjectInArrayByPropertyValue(recipesResolved, "recipe_id", weekRecipe.id); + if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING) { - weekCosts = FindObjectInArrayByPropertyValue(recipesResolved, "recipe_id", weekRecipe.id).costs; + weekCosts = weekRecipeResolved.costs; weekCostsHtml = __t("Week costs") + ': ' + weekCosts.toString() + " "; } var weekRecipeOrderMissingButtonDisabledClasses = ""; - if (FindObjectInArrayByPropertyValue(recipesResolved, "recipe_id", weekRecipe.id).need_fulfilled_with_shopping_list == 1) + if (weekRecipeResolved.need_fulfilled_with_shopping_list == 1) { weekRecipeOrderMissingButtonDisabledClasses = "disabled"; } var weekRecipeConsumeButtonDisabledClasses = ""; - if (FindObjectInArrayByPropertyValue(recipesResolved, "recipe_id", weekRecipe.id).need_fulfilled == 0 || weekCosts == 0) + if (weekRecipeResolved.need_fulfilled == 0 || weekCosts == 0) { weekRecipeConsumeButtonDisabledClasses = "disabled"; } diff --git a/views/mealplan.blade.php b/views/mealplan.blade.php index d573132f..9b6747ef 100644 --- a/views/mealplan.blade.php +++ b/views/mealplan.blade.php @@ -75,9 +75,10 @@ @section('content')