From c7672c8168bd9dea41e77c1f17572cf099e06ec6 Mon Sep 17 00:00:00 2001 From: cos Date: Thu, 5 Feb 2015 17:58:16 +0100 Subject: Make cmd_setmeal and cmd_showplan handle several mealtypes per day. --- mat | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/mat b/mat index 09aa559..38eae68 100755 --- a/mat +++ b/mat @@ -124,8 +124,7 @@ my @schedule = ( ); sub cmd_setmeal { - my ( $date, $recipe_id ) = @_; - my $mealtype = "lunch"; + my ( $date, $mealtype, $recipe_id ) = @_; if ($date =~ /^Mon|Tue|Wed|Thu|Fri|Sat|Sun/) { my $wday; @@ -144,6 +143,7 @@ sub cmd_setmeal { $date = $dt->ymd(); } + return unless $mealtype =~ "^(frukost)|(elvakaffe)|(lunch)|(fruktstund)|(middag)\$"; return unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$"; return -1 unless $recipe_id =~ "^[0-9]+\$"; @@ -421,27 +421,34 @@ sub cmd_showplan { return -1 unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$"; $dt = DateTime::Format::ISO8601->parse_datetime( $date ); } else { - $dt = DateTime->now(); + $dt = DateTime->now(locale => "sv_SE"); # FIXME Don't hårdkoda svenska, tack! # FIXME now() is not start of day, set hour, minute and such to 0 } - for (my $i = 0; $i < 14; $i++) { + for (my $i = 0; $i < 7; $i++) { my $sql = "SELECT recipe_id, mealtype, comment_id FROM plan WHERE date='". $dt->ymd()."';"; - my @ids = $db->selectrow_array($sql); - + my $ids = $db->selectall_hashref($sql, 'mealtype'); print $weekend_padding; - if ($ids[0]||$ids[2]) { - printf "%9s %-10s %3s|%s %s%s%s%s\n", $dt->ymd(), $ids[1], - (defined($ids[0]) ? $ids[0] : ""), - (defined($ids[0]) ? substr(get_plan_state($dt->ymd()), 0, 1) : " "), - (defined($ids[0]) ? get_recipe_name($ids[0]).", " : ""), - (defined($ids[0]) ? get_recipe_uri($ids[0]) : ""), - (defined($ids[0]) && defined($ids[2]) ? ", " : ""), - (defined($ids[2]) ? get_comment($ids[2]) : ""); - } else { - printf "%-24s | \n", $dt->ymd(); + for my $mealtype ('frukost', 'elvakaffe', 'lunch', 'fruktstund', 'middag') { # FIXME fetch from database + if ($ids->{$mealtype}{'recipe_id'}) { + printf "%10s %-10s %3s|%s\n", ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype + ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), # FIXME remove hard coded mealtype + $mealtype, + $ids->{$mealtype}{'recipe_id'}, + get_recipe_name($ids->{$mealtype}{'recipe_id'}), +# get_recipe_uri($ids->{$mealtype}{'recipe_id'}), +# get_comment($ids->{$mealtype}{'recipe_id'}); +# "apa", "lemur", "katt", "hund"; +## substr(get_plan_state($dt->ymd()), 0, 1) : " "), + } else { + printf "%-10s %-13s | \n", + ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype + ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), $mealtype; # FIXME remove hard coded mealtype +# printf "%-24s | \n", $dt->ymd(); + } } + print "\n"; $dt->add(days => 1); if($dt->day_of_week == 1) { $weekend_padding = "\n"; @@ -474,7 +481,7 @@ sub cmd_help() { print "editrecipe \n"; print "showrecipe \n"; print "movemeal \n"; - print "setmeal \n"; + print "setmeal \n"; print "setstate \n"; print "randmeal \n"; print "showplan [date]\n"; @@ -835,7 +842,7 @@ if ($ARGV[0]) { } elsif ( $ARGV[0] eq "movemeal") { print "Command failed!\n" unless (cmd_movemeal($ARGV[1], $ARGV[2]) >= 0); } elsif ( $ARGV[0] eq "setmeal") { - print "Command failed!\n" unless (cmd_setmeal($ARGV[1], $ARGV[2]) >= 0); + print "Command failed!\n" unless (cmd_setmeal($ARGV[1], $ARGV[2], $ARGV[3]) >= 0); } elsif ( $ARGV[0] eq "setstate") { print "Command failed!\n" unless (cmd_setstate($ARGV[1], $ARGV[2]) >= 0); } elsif ( $ARGV[0] eq "randmeal") { -- cgit v1.2.3 From 606682c3415b6546d688f9aa5a4fed6546b46f12 Mon Sep 17 00:00:00 2001 From: cos Date: Thu, 5 Feb 2015 20:55:10 +0100 Subject: Have cmd_showplan check planstate using actual inventory. --- mat | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/mat b/mat index 38eae68..ca80721 100755 --- a/mat +++ b/mat @@ -404,43 +404,58 @@ sub cmd_reprintlabel { } sub get_plan_state { - my ( $date ) = @_; - - my $sql = "SELECT state FROM plan WHERE date=".$db->quote($date).";"; - my ( $state ) = $db->selectrow_array($sql); - $state = 'u' unless $state; + my ( $recipe_id, $end_date ) = @_; + my $state; + + my $sql = "SELECT COUNT(id) FROM inventory WHERE recipe_id=$recipe_id AND ". + "(storage GLOB '*frys' OR storage GLOB '*kyl');"; + my $prepared = $db->selectcol_arrayref($sql); + + $sql = "SELECT recipe_id FROM plan WHERE recipe_id=$recipe_id AND ". + "date >= '".DateTime->now->ymd()."' AND date < '$end_date';"; + my $already_planned = $db->selectcol_arrayref($sql); + my $portions_left = @{$prepared}[0] - scalar(@{$already_planned}); + if ( $portions_left > 0 ) { + $state = 'prepared'; + } else { + $state = 'unavailable'; + } return $state; } sub cmd_showplan { - my ( $date ) = @_; + my ( $days, $date ) = @_; my $dt; my $weekend_padding = ""; + $days = 7 unless defined($days) && $days =~ /^[0-9]{1,2}$/; + + DateTime->DefaultLocale("sv_SE"); # FIXME Don't hårdkoda svenska, tack! if($date) { - return -1 unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$"; + return -1 unless $date =~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/; $dt = DateTime::Format::ISO8601->parse_datetime( $date ); } else { - $dt = DateTime->now(locale => "sv_SE"); # FIXME Don't hårdkoda svenska, tack! - # FIXME now() is not start of day, set hour, minute and such to 0 + $dt = DateTime->now(); } - for (my $i = 0; $i < 7; $i++) { + for (my $i = 0; $i < $days; $i++) { my $sql = "SELECT recipe_id, mealtype, comment_id FROM plan WHERE date='". $dt->ymd()."';"; my $ids = $db->selectall_hashref($sql, 'mealtype'); print $weekend_padding; for my $mealtype ('frukost', 'elvakaffe', 'lunch', 'fruktstund', 'middag') { # FIXME fetch from database if ($ids->{$mealtype}{'recipe_id'}) { - printf "%10s %-10s %3s|%s\n", ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype + + printf "%-10s %-10s %3s|%s (%s)\n", ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), # FIXME remove hard coded mealtype $mealtype, - $ids->{$mealtype}{'recipe_id'}, + ($ids->{$mealtype}{'recipe_id'} ? substr(get_plan_state($ids->{$mealtype}{'recipe_id'}, $dt->ymd()), 0, 1) : " "), get_recipe_name($ids->{$mealtype}{'recipe_id'}), +# ($portions_left > 0) ? $portions_left : 'u', + $ids->{$mealtype}{'recipe_id'}, # get_recipe_uri($ids->{$mealtype}{'recipe_id'}), # get_comment($ids->{$mealtype}{'recipe_id'}); # "apa", "lemur", "katt", "hund"; -## substr(get_plan_state($dt->ymd()), 0, 1) : " "), } else { printf "%-10s %-13s | \n", ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype @@ -484,7 +499,7 @@ sub cmd_help() { print "setmeal \n"; print "setstate \n"; print "randmeal \n"; - print "showplan [date]\n"; + print "showplan [days] [date]\n"; print "postpone \n"; print "storeportion [amount] [storage]\n"; print "reprintlabel \n"; @@ -848,7 +863,7 @@ if ($ARGV[0]) { } elsif ( $ARGV[0] eq "randmeal") { print "Command failed!\n" unless (cmd_randmeal($ARGV[1]) >= 0); } elsif ( $ARGV[0] eq "showplan") { - print "Command failed!\n" unless (cmd_showplan($ARGV[1]) >= 0); + print "Command failed!\n" unless (cmd_showplan($ARGV[1], $ARGV[2]) >= 0); } elsif ( $ARGV[0] eq "postpone") { print "Command failed!\n" unless (cmd_postpone($ARGV[1]) >= 0); } elsif ( $ARGV[0] eq "inventory") { -- cgit v1.2.3 From 80e18fc5ac7e7dcac500f292f886ef9195dc9341 Mon Sep 17 00:00:00 2001 From: cos Date: Thu, 5 Feb 2015 21:01:33 +0100 Subject: Add unsetting planned entries. --- mat | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mat b/mat index ca80721..2c7c0f5 100755 --- a/mat +++ b/mat @@ -146,10 +146,10 @@ sub cmd_setmeal { return unless $mealtype =~ "^(frukost)|(elvakaffe)|(lunch)|(fruktstund)|(middag)\$"; return unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$"; - return -1 unless $recipe_id =~ "^[0-9]+\$"; - if (get_recipe_name($recipe_id) ne 'NULL') { - my $sql = "DELETE FROM plan WHERE date='$date' and mealtype='$mealtype';"; - $db->do($sql); + return -1 unless $recipe_id =~ m/^[0-9]+$|^-$/; + my $sql = "DELETE FROM plan WHERE date='$date' and mealtype='$mealtype';"; + $db->do($sql); + if ($recipe_id ne '-' && get_recipe_name($recipe_id) ne 'NULL') { $sql = "INSERT INTO plan (date, mealtype, recipe_id) VALUES ('$date', '$mealtype', $recipe_id);"; $db->do($sql); -- cgit v1.2.3 From a478c12859dfcad58da89758a94ba4c59a8ce1fd Mon Sep 17 00:00:00 2001 From: cos Date: Thu, 5 Feb 2015 21:36:02 +0100 Subject: Make cmd_showplan display comments again. --- mat | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mat b/mat index 2c7c0f5..2a1cb16 100755 --- a/mat +++ b/mat @@ -454,8 +454,12 @@ sub cmd_showplan { # ($portions_left > 0) ? $portions_left : 'u', $ids->{$mealtype}{'recipe_id'}, # get_recipe_uri($ids->{$mealtype}{'recipe_id'}), -# get_comment($ids->{$mealtype}{'recipe_id'}); # "apa", "lemur", "katt", "hund"; + } elsif ($ids->{$mealtype}{'comment_id'}) { + printf "%-10s %-13s |%s\n", + ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype + ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), $mealtype, # FIXME remove hard coded mealtype + get_comment($ids->{$mealtype}{'comment_id'}); } else { printf "%-10s %-13s | \n", ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype -- cgit v1.2.3 From 8640c0d3a7f3f6c26577cc26138db14f5f68364f Mon Sep 17 00:00:00 2001 From: cos Date: Thu, 5 Feb 2015 21:44:21 +0100 Subject: Skip prepared meals from shoppinglist. --- mat | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/mat b/mat index 2a1cb16..993151b 100755 --- a/mat +++ b/mat @@ -741,40 +741,20 @@ sub cmd_shoppinglist { $enddate->add(days => $shopdays); -# First: Create a list of all meals from now to now+shopdays -# Second: Divide each meal type with the ration size and apply the roof function -# Third: Generate a list of all ingredients -# Voila! - -# for (my $i = 0; $i < $shopdays; $i++) { -# # FIXME Skip ingredients for ready or frozen recipes -# print $i, "\n"; -# } - - my $plan_entries = $db->selectall_arrayref("SELECT recipe_id FROM plan WHERE recipe_id AND date ". - "BETWEEN '".$startdate."' AND '".$enddate."' AND ". - "IFNULL(state, 'null') != 'frozen' AND ". - "IFNULL(state, 'null') != 'ready' AND ". - "IFNULL(state, 'null') != 'sourced'", { Slice => {} }); + my $plan_entries = $db->selectall_arrayref("SELECT recipe_id, date FROM plan WHERE recipe_id AND date ". + "BETWEEN '".$startdate."' AND '".$enddate."';", { Slice => {} }); my $queue_entries = $db->selectall_arrayref("SELECT recipe_id FROM queue;"); my %recipe_count; for my $entry ( @$plan_entries ) { - $recipe_count{$entry->{recipe_id}}++; -# print $recipe_count{$entry->{recipe_id}}, " ", $entry->{recipe_id}, "\n"; -## print $entry->{recipe_id}.""; -## print " count: ", scalar(grep /$entry->{recipe_id}/, @$entries), " "; -## print " count: ", $count{31}, "\n"; -## print " count: ", $count{$entry->{recipe_id}}, "\n"; -## # FIXME Take number of servings into account -## TODO Loop through recipes to add ingredients to a list -## TODO Print ingredients + if (get_plan_state($entry->{'recipe_id'}, $entry->{'date'}) ne "prepared") { + $recipe_count{$entry->{recipe_id}}++; + } } for my $entry ( @$queue_entries ) { my @s = $db->selectrow_array("SELECT servings FROM queue WHERE ". "recipe_id=@$entry[0]"); $recipe_count{@$entry[0]} += $s[0]; } -# map {print "| $_ = ${recipe_count{$_}}\n"} sort keys(%recipe_count); my @shop_recipes; for my $recipe ( keys(%recipe_count) ) @@ -788,8 +768,6 @@ sub cmd_shoppinglist { $servings = 1; print "WARNING servings is not set for recipe $recipe!\n"; } -# print "> $recipe", " ", $recipe_count{$recipe}, " ", $servings, "\n"; -# print "| $recipe", " ", ceiling($recipe_count{$recipe}/$servings), "\n"; my $cookings = ceiling($recipe_count{$recipe}/$servings); for (my $i=0; $i < $cookings; $i++) { -- cgit v1.2.3 From 9db2116061714eb2f654f7683ded5390533a03a2 Mon Sep 17 00:00:00 2001 From: cos Date: Thu, 5 Feb 2015 21:53:15 +0100 Subject: Remove obsolete cmd_setstate --- mat | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/mat b/mat index 993151b..e69f74b 100755 --- a/mat +++ b/mat @@ -202,38 +202,6 @@ sub cmd_inventory { printf "----\n%3d\n", $total; } -sub cmd_setstate { - my ( $date, $state ) = @_; - my $mealtype = "lunch"; - - if ($date =~ /^Mon|Tue|Wed|Thu|Fri|Sat|Sun/) { - my $wday; - $wday = 1 if ($date =~ /^Mon/); - $wday = 2 if ($date =~ /^Tue/); - $wday = 3 if ($date =~ /^Wed/); - $wday = 4 if ($date =~ /^Thu/); - $wday = 5 if ($date =~ /^Fri/); - $wday = 6 if ($date =~ /^Sat/); - $wday = 7 if ($date =~ /^Sun/); - - my $dt = DateTime->now(); - while ($dt->wday() != $wday) { - $dt->add(days => 1); - } - - $date = $dt->ymd(); - } - return unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$"; - - return -1 unless $state =~ "^frozen|ready|sourced|idea\$"; - if (1) { #FIXME Verify that entry exists ? - my $sql = "UPDATE plan SET state=".$db->quote($state)." WHERE date='$date';"; - $db->do($sql); - } - - return 0; -} - sub cmd_randmeal { my ( $date ) = @_; my $recipe_id = get_random_recipe(); @@ -501,7 +469,6 @@ sub cmd_help() { print "showrecipe \n"; print "movemeal \n"; print "setmeal \n"; - print "setstate \n"; print "randmeal \n"; print "showplan [days] [date]\n"; print "postpone \n"; @@ -840,8 +807,6 @@ if ($ARGV[0]) { print "Command failed!\n" unless (cmd_movemeal($ARGV[1], $ARGV[2]) >= 0); } elsif ( $ARGV[0] eq "setmeal") { print "Command failed!\n" unless (cmd_setmeal($ARGV[1], $ARGV[2], $ARGV[3]) >= 0); - } elsif ( $ARGV[0] eq "setstate") { - print "Command failed!\n" unless (cmd_setstate($ARGV[1], $ARGV[2]) >= 0); } elsif ( $ARGV[0] eq "randmeal") { print "Command failed!\n" unless (cmd_randmeal($ARGV[1]) >= 0); } elsif ( $ARGV[0] eq "showplan") { -- cgit v1.2.3 From 341364c7a7882a5e05a0fb6a13efb4a51df3772a Mon Sep 17 00:00:00 2001 From: cos Date: Thu, 5 Feb 2015 22:53:39 +0100 Subject: Hack cmd_showplan to display multiple dishes per meal. --- mat | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/mat b/mat index e69f74b..dfd3d21 100755 --- a/mat +++ b/mat @@ -407,33 +407,37 @@ sub cmd_showplan { } for (my $i = 0; $i < $days; $i++) { - my $sql = "SELECT recipe_id, mealtype, comment_id FROM plan WHERE date='". - $dt->ymd()."';"; - my $ids = $db->selectall_hashref($sql, 'mealtype'); + my $sql = "SELECT recipe_id, p.mealtype, comment_id FROM plan AS p JOIN ". + "mealtypes AS m ON p.mealtype=m.mealtype WHERE date='".$dt->ymd(). + "' ORDER by m.id;"; + my $plan = $db->selectall_arrayref($sql); print $weekend_padding; - for my $mealtype ('frukost', 'elvakaffe', 'lunch', 'fruktstund', 'middag') { # FIXME fetch from database - if ($ids->{$mealtype}{'recipe_id'}) { - - printf "%-10s %-10s %3s|%s (%s)\n", ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype - ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), # FIXME remove hard coded mealtype - $mealtype, - ($ids->{$mealtype}{'recipe_id'} ? substr(get_plan_state($ids->{$mealtype}{'recipe_id'}, $dt->ymd()), 0, 1) : " "), - get_recipe_name($ids->{$mealtype}{'recipe_id'}), -# ($portions_left > 0) ? $portions_left : 'u', - $ids->{$mealtype}{'recipe_id'}, -# get_recipe_uri($ids->{$mealtype}{'recipe_id'}), -# "apa", "lemur", "katt", "hund"; - } elsif ($ids->{$mealtype}{'comment_id'}) { - printf "%-10s %-13s |%s\n", - ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype - ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), $mealtype, # FIXME remove hard coded mealtype - get_comment($ids->{$mealtype}{'comment_id'}); + my $last_mealtype = ""; + for my $meal ( @$plan ) { + my $mealtype = $meal->[1]; + if ( $mealtype ne $last_mealtype ) { + print "\n"; + if ($meal->[0]) { + printf "%-10s %-10s %3s|%s (%s)", ($last_mealtype eq "" ? $dt->ymd() : + ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), # FIXME remove hard coded mealtype + $mealtype, + ($meal->[0] ? substr(get_plan_state($meal->[0], $dt->ymd()), 0, 1) : " "), + get_recipe_name($meal->[0]), + $meal->[0], + } elsif ($meal->[2]) { + printf "%-10s %-13s |%s", + ($last_mealtype eq "" ? $dt->ymd() : + ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), $mealtype, # FIXME remove hard coded mealtype + get_comment($meal->[2]); + } else { + printf "%-10s %-13s | ", + ($last_mealtype eq "" ? $dt->ymd() : + ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), $mealtype; # FIXME remove hard coded mealtype + } } else { - printf "%-10s %-13s | \n", - ($mealtype eq "frukost" ? $dt->ymd() : # FIXME remove hard coded mealtype - ($mealtype eq "elvakaffe" ? $dt->day_name() : "")), $mealtype; # FIXME remove hard coded mealtype -# printf "%-24s | \n", $dt->ymd(); + print " + ", get_recipe_name($meal->[0]), " (", $meal->[0], ")"; } + $last_mealtype = $mealtype; } print "\n"; $dt->add(days => 1); -- cgit v1.2.3 From 9d46b7d8919986c241ee6bb7886994c6c2d0e9b8 Mon Sep 17 00:00:00 2001 From: cos Date: Sat, 4 Apr 2015 13:25:18 +0200 Subject: Enable setting multiple dishes per meal. --- mat | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/mat b/mat index dfd3d21..3fa9d8c 100755 --- a/mat +++ b/mat @@ -124,7 +124,7 @@ my @schedule = ( ); sub cmd_setmeal { - my ( $date, $mealtype, $recipe_id ) = @_; + my ( $date, $mealtype, $recipe_ids ) = @_; if ($date =~ /^Mon|Tue|Wed|Thu|Fri|Sat|Sun/) { my $wday; @@ -146,13 +146,17 @@ sub cmd_setmeal { return unless $mealtype =~ "^(frukost)|(elvakaffe)|(lunch)|(fruktstund)|(middag)\$"; return unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$"; - return -1 unless $recipe_id =~ m/^[0-9]+$|^-$/; + return -1 unless $recipe_ids =~ m/^[0-9+]+$|^-$/; my $sql = "DELETE FROM plan WHERE date='$date' and mealtype='$mealtype';"; $db->do($sql); - if ($recipe_id ne '-' && get_recipe_name($recipe_id) ne 'NULL') { - $sql = "INSERT INTO plan (date, mealtype, recipe_id) VALUES ('$date', - '$mealtype', $recipe_id);"; - $db->do($sql); + + for my $recipe_id (split('\+', $recipe_ids)) + { + if ($recipe_id ne '-' && get_recipe_name($recipe_id) ne 'NULL') { + $sql = "INSERT INTO plan (date, mealtype, recipe_id) VALUES ('$date', + '$mealtype', $recipe_id);"; + $db->do($sql); + } } return 0; -- cgit v1.2.3