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('\
\
' + recipe.name + '
\ -
' + __n(mealPlanEntry.servings, "%s serving", "%s servings") + '
\ +
' + __n(mealPlanEntry.recipe_servings, "%s serving", "%s servings") + '
\
' + fulfillmentIconHtml + " " + fulfillmentInfoHtml + '
\ ' + costsAndCaloriesPerServing + ' \
\ @@ -158,13 +159,9 @@ var calendar = $("#calendar").fullCalendar({ } else if (event.type == "note") { - var note = JSON.parse(event.note); - - element.attr("data-note", event.note); - element.html('\
\ -
' + note.note + '
\ +
' + mealPlanEntry.note + '
\
\ \
\ @@ -234,9 +231,9 @@ $(document).on("click", ".remove-recipe-button", function(e) $(document).on("click", ".remove-note-button", function(e) { - var note = JSON.parse($(this).parents(".fc-h-event:first").attr("data-note")); + var mealPlanEntry = JSON.parse($(this).parents(".fc-h-event:first").attr("data-meal-plan-entry")); - Grocy.Api.Delete('objects/meal_plan_notes/' + note.id.toString(), { }, + Grocy.Api.Delete('objects/meal_plan/' + mealPlanEntry.id.toString(), { }, function(result) { window.location.reload(); @@ -280,7 +277,7 @@ $('#save-add-note-button').on('click', function(e) var jsonData = $('#add-note-form').serializeJSON(); jsonData.day = $("#day").val(); - Grocy.Api.Post('objects/meal_plan_notes', jsonData, + Grocy.Api.Post('objects/meal_plan', jsonData, function(result) { window.location.reload(); diff --git a/services/DemoDataGeneratorService.php b/services/DemoDataGeneratorService.php index 76d217ac..f1728c4f 100644 --- a/services/DemoDataGeneratorService.php +++ b/services/DemoDataGeneratorService.php @@ -128,16 +128,17 @@ class DemoDataGeneratorService extends BaseService INSERT INTO recipes_pos (recipe_id, product_id, amount) VALUES (5, 2, 1); INSERT INTO recipes_pos (recipe_id, product_id, amount, qu_id, only_check_single_unit_in_stock) VALUES (5, 23, 200, 11, 1); - INSERt INTO recipes_nestings(recipe_id, includes_recipe_id) VALUES (6, 4); - INSERt INTO recipes_nestings(recipe_id, includes_recipe_id) VALUES (6, 5); + INSERT INTO recipes_nestings(recipe_id, includes_recipe_id) VALUES (6, 4); + INSERT INTO recipes_nestings(recipe_id, includes_recipe_id) VALUES (6, 5); - INSERt INTO meal_plan(day, recipe_id) VALUES ('{$mondayThisWeek}', 1); - INSERt INTO meal_plan(day, recipe_id) VALUES ('{$tuesdayThisWeek}', 2); - INSERt INTO meal_plan(day, recipe_id) VALUES ('{$wednesdayThisWeek}', 3); - INSERt INTO meal_plan(day, recipe_id) VALUES ('{$thursdayThisWeek}', 4); - INSERt INTO meal_plan(day, recipe_id) VALUES ('{$fridayThisWeek}', 1); - INSERt INTO meal_plan(day, recipe_id) VALUES ('{$saturdayThisWeek}', 2); - INSERt INTO meal_plan(day, recipe_id) VALUES ('{$sundayThisWeek}', 4); + INSERT INTO meal_plan(day, recipe_id) VALUES ('{$mondayThisWeek}', 1); + INSERT INTO meal_plan(day, recipe_id) VALUES ('{$tuesdayThisWeek}', 2); + INSERT INTO meal_plan(day, recipe_id) VALUES ('{$wednesdayThisWeek}', 3); + INSERT INTO meal_plan(day, recipe_id) VALUES ('{$thursdayThisWeek}', 4); + INSERT INTO meal_plan(day, recipe_id) VALUES ('{$fridayThisWeek}', 1); + INSERT INTO meal_plan(day, recipe_id) VALUES ('{$saturdayThisWeek}', 2); + INSERT INTO meal_plan(day, recipe_id) VALUES ('{$sundayThisWeek}', 4); + INSERT INTO meal_plan(day, type, note) VALUES ('{$tuesdayThisWeek}', 'note', '{$this->__t_sql('This is a note')}'); INSERT INTO chores (name, period_type, period_days) VALUES ('{$this->__t_sql('Changed towels in the bathroom')}', 'manually', 5); --1 INSERT INTO chores (name, period_type, period_days, assignment_type, assignment_config, next_execution_assigned_to_user_id) VALUES ('{$this->__t_sql('Cleaned the kitchen floor')}', 'dynamic-regular', 7, 'random', '1,2,3,4', 1); --2 diff --git a/views/mealplan.blade.php b/views/mealplan.blade.php index 22b1e9ce..e4759cad 100644 --- a/views/mealplan.blade.php +++ b/views/mealplan.blade.php @@ -46,11 +46,11 @@ @include('components.recipepicker', array( 'recipes' => $recipes, 'isRequired' => true, - 'nextInputSelector' => '#servings' + 'nextInputSelector' => '#recipe_servings' )) @include('components.numberpicker', array( - 'id' => 'servings', + 'id' => 'recipe_servings', 'label' => 'Servings', 'min' => 1, 'value' => '1', @@ -58,6 +58,7 @@ )) +
@@ -83,6 +84,8 @@
+ +