diff --git a/controllers/RecipesController.php b/controllers/RecipesController.php index 466fa120..ed0d8005 100644 --- a/controllers/RecipesController.php +++ b/controllers/RecipesController.php @@ -132,20 +132,8 @@ class RecipesController extends BaseController public function MealPlan(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { $recipes = $this->Database->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->fetchAll(); + $events = array(); - - foreach($this->Database->meal_plan_notes() as $mealPlanNote) - { - $events[] = array( - 'id' => intval($mealPlanNote['id']) * -1, - 'title' => '', - 'start' => $mealPlanNote['day'], - 'date_format' => 'date', - 'note' => json_encode($mealPlanNote), - 'type' => 'note' - ); - } - foreach($this->Database->meal_plan() as $mealPlanEntry) { $recipe = FindObjectInArrayByPropertyValue($recipes, 'id', $mealPlanEntry['recipe_id']); @@ -162,7 +150,7 @@ class RecipesController extends BaseController 'date_format' => 'date', 'recipe' => json_encode($recipe), 'mealPlanEntry' => json_encode($mealPlanEntry), - 'type' => 'recipe' + 'type' => $mealPlanEntry['type'] ); } diff --git a/grocy.openapi.json b/grocy.openapi.json index c07a282e..c49ce067 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -3272,8 +3272,7 @@ "userfields", "userentities", "userobjects", - "meal_plan", - "meal_plan_notes" + "meal_plan" ] }, "ExposedEntityButNoListing": { diff --git a/localization/demo_data.pot b/localization/demo_data.pot index 3ffc43a6..51cc69a5 100644 --- a/localization/demo_data.pot +++ b/localization/demo_data.pot @@ -327,3 +327,6 @@ msgstr "" msgid "Portuguese (Brazil)" msgstr "" + +msgid "This is a note" +msgstr "" diff --git a/migrations/0096.sql b/migrations/0096.sql index 35218ef1..a3e66a30 100644 --- a/migrations/0096.sql +++ b/migrations/0096.sql @@ -1,6 +1,120 @@ -CREATE TABLE meal_plan_notes ( +PRAGMA legacy_alter_table = ON; + +ALTER TABLE meal_plan RENAME TO meal_plan_old; + +CREATE TABLE meal_plan ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, day DATE NOT NULL, + type TEXT DEFAULT 'recipe', + recipe_id INTEGER, + recipe_servings INTEGER DEFAULT 1, note TEXT, row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime')) ); + +INSERT INTO meal_plan + (day, recipe_id, recipe_servings, row_created_timestamp, type) +SELECT day, recipe_id, servings, row_created_timestamp, 'recipe' +FROM meal_plan_old; + +DROP TABLE meal_plan_old; + +CREATE TRIGGER create_internal_recipe AFTER INSERT ON meal_plan +BEGIN + /* This contains practically the same logic as the trigger remove_internal_recipe */ + + -- Create a recipe per day + DELETE FROM recipes + WHERE name = NEW.day + AND type = 'mealplan-day'; + + INSERT OR REPLACE INTO recipes + (id, name, type) + VALUES + ((SELECT MIN(id) - 1 FROM recipes), NEW.day, 'mealplan-day'); + + -- Create a recipe per week + DELETE FROM recipes + WHERE name = LTRIM(STRFTIME('%Y-%W', NEW.day), '0') + AND type = 'mealplan-week'; + + INSERT INTO recipes + (id, name, type) + VALUES + ((SELECT MIN(id) - 1 FROM recipes), LTRIM(STRFTIME('%Y-%W', NEW.day), '0'), 'mealplan-week'); + + -- Delete all current nestings entries for the day and week recipe + DELETE FROM recipes_nestings + WHERE recipe_id IN (SELECT id FROM recipes WHERE name = NEW.day AND type = 'mealplan-day') + OR recipe_id IN (SELECT id FROM recipes WHERE name = NEW.day AND type = 'mealplan-week'); + + -- Add all recipes for this day as included recipes in the day-recipe + INSERT INTO recipes_nestings + (recipe_id, includes_recipe_id, servings) + SELECT (SELECT id FROM recipes WHERE name = NEW.day AND type = 'mealplan-day'), recipe_id, SUM(recipe_servings) + FROM meal_plan + WHERE day = NEW.day + AND type = 'recipe' + AND recipe_id IS NOT NULL + GROUP BY recipe_id; + + -- Add all recipes for this week as included recipes in the week-recipe + INSERT INTO recipes_nestings + (recipe_id, includes_recipe_id, servings) + SELECT (SELECT id FROM recipes WHERE name = LTRIM(STRFTIME('%Y-%W', NEW.day), '0') AND type = 'mealplan-week'), recipe_id, SUM(recipe_servings) + FROM meal_plan + WHERE STRFTIME('%Y-%W', day) = STRFTIME('%Y-%W', NEW.day) + AND type = 'recipe' + AND recipe_id IS NOT NULL + GROUP BY recipe_id; +END; + +CREATE TRIGGER remove_internal_recipe AFTER DELETE ON meal_plan +BEGIN + /* This contains practically the same logic as the trigger create_internal_recipe */ + + -- Create a recipe per day + DELETE FROM recipes + WHERE name = OLD.day + AND type = 'mealplan-day'; + + INSERT OR REPLACE INTO recipes + (id, name, type) + VALUES + ((SELECT MIN(id) - 1 FROM recipes), OLD.day, 'mealplan-day'); + + -- Create a recipe per week + DELETE FROM recipes + WHERE name = LTRIM(STRFTIME('%Y-%W', OLD.day), '0') + AND type = 'mealplan-week'; + + INSERT INTO recipes + (id, name, type) + VALUES + ((SELECT MIN(id) - 1 FROM recipes), LTRIM(STRFTIME('%Y-%W', OLD.day), '0'), 'mealplan-week'); + + -- Delete all current nestings entries for the day and week recipe + DELETE FROM recipes_nestings + WHERE recipe_id IN (SELECT id FROM recipes WHERE name = OLD.day AND type = 'mealplan-day') + OR recipe_id IN (SELECT id FROM recipes WHERE name = OLD.day AND type = 'mealplan-week'); + + -- Add all recipes for this day as included recipes in the day-recipe + INSERT INTO recipes_nestings + (recipe_id, includes_recipe_id, servings) + SELECT (SELECT id FROM recipes WHERE name = OLD.day AND type = 'mealplan-day'), recipe_id, SUM(recipe_servings) + FROM meal_plan + WHERE day = OLD.day + AND type = 'recipe' + AND recipe_id IS NOT NULL + GROUP BY recipe_id; + + -- Add all recipes for this week as included recipes in the week-recipe + INSERT INTO recipes_nestings + (recipe_id, includes_recipe_id, servings) + SELECT (SELECT id FROM recipes WHERE name = LTRIM(STRFTIME('%Y-%W', OLD.day), '0') AND type = 'mealplan-week'), recipe_id, SUM(recipe_servings) + FROM meal_plan + WHERE STRFTIME('%Y-%W', day) = STRFTIME('%Y-%W', OLD.day) + AND type = 'recipe' + AND recipe_id IS NOT NULL + GROUP BY recipe_id; +END; diff --git a/public/viewjs/mealplan.js b/public/viewjs/mealplan.js index 0569cfd2..9ddee5be 100644 --- a/public/viewjs/mealplan.js +++ b/public/viewjs/mealplan.js @@ -72,6 +72,9 @@ var calendar = $("#calendar").fullCalendar({ { element.removeClass("fc-event"); element.addClass("text-center"); + element.attr("data-meal-plan-entry", event.mealPlanEntry); + + var mealPlanEntry = JSON.parse(event.mealPlanEntry); if (event.type == "recipe") { @@ -81,11 +84,9 @@ var calendar = $("#calendar").fullCalendar({ return false; } - var mealPlanEntry = JSON.parse(event.mealPlanEntry); var resolvedRecipe = FindObjectInArrayByPropertyValue(recipesResolved, "recipe_id", recipe.id); element.attr("data-recipe", event.recipe); - element.attr("data-meal-plan-entry", event.mealPlanEntry); var recipeOrderMissingButtonDisabledClasses = ""; if (resolvedRecipe.need_fulfilled_with_shopping_list == 1) @@ -119,7 +120,7 @@ var calendar = $("#calendar").fullCalendar({ element.html('\