Meal plan recipe and notes also fit into one table (references #477)

This commit is contained in:
Bernd Bestel 2020-01-23 21:57:47 +01:00
parent 6663551a66
commit fd5a72264d
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
7 changed files with 144 additions and 39 deletions

View File

@ -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']
);
}

View File

@ -3272,8 +3272,7 @@
"userfields",
"userentities",
"userobjects",
"meal_plan",
"meal_plan_notes"
"meal_plan"
]
},
"ExposedEntityButNoListing": {

View File

@ -327,3 +327,6 @@ msgstr ""
msgid "Portuguese (Brazil)"
msgstr ""
msgid "This is a note"
msgstr ""

View File

@ -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;

View File

@ -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('\
<div> \
<h5 class="text-truncate">' + recipe.name + '<h5> \
<h5 class="small text-truncate">' + __n(mealPlanEntry.servings, "%s serving", "%s servings") + '</h5> \
<h5 class="small text-truncate">' + __n(mealPlanEntry.recipe_servings, "%s serving", "%s servings") + '</h5> \
<h5 class="small timeago-contextual text-truncate">' + fulfillmentIconHtml + " " + fulfillmentInfoHtml + '</h5> \
' + costsAndCaloriesPerServing + ' \
<h5> \
@ -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('\
<div> \
<h5 class="text-truncate">' + note.note + '<h5> \
<h5 class="text-truncate">' + mealPlanEntry.note + '<h5> \
<h5> \
<a class="ml-1 btn btn-outline-danger btn-xs remove-note-button" href="#"><i class="fas fa-trash"></i></a> \
</h5> \
@ -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();

View File

@ -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

View File

@ -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 @@
))
<input type="hidden" id="day" name="day" value="">
<input type="hidden" name="type" value="recipe">
</form>
</div>
@ -83,6 +84,8 @@
<textarea class="form-control" rows="2" id="note" name="note"></textarea>
</div>
<input type="hidden" name="type" value="note">
</form>
</div>
<div class="modal-footer">