summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2015-04-04 13:25:46 +0200
committercos <cos>2015-04-04 13:25:46 +0200
commitfa76332e1b202e1477110951e40257bad8282629 (patch)
treed1f875e3b702ffc68287dae345ffd1409435a702
parentf1020f3096ecff934ad93bb78ba597ff4249e068 (diff)
parent9d46b7d8919986c241ee6bb7886994c6c2d0e9b8 (diff)
downloadmat-fa76332e1b202e1477110951e40257bad8282629.zip
Merge branch 'feature/multimeal'
-rwxr-xr-xmat173
1 files changed, 75 insertions, 98 deletions
diff --git a/mat b/mat
index 09aa559..3fa9d8c 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_ids ) = @_;
if ($date =~ /^Mon|Tue|Wed|Thu|Fri|Sat|Sun/) {
my $wday;
@@ -144,15 +143,20 @@ 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]+\$";
- if (get_recipe_name($recipe_id) ne 'NULL') {
- my $sql = "DELETE FROM plan WHERE date='$date' and mealtype='$mealtype';";
- $db->do($sql);
- $sql = "INSERT INTO plan (date, mealtype, recipe_id) VALUES ('$date',
- '$mealtype', $recipe_id);";
- $db->do($sql);
+ return -1 unless $recipe_ids =~ m/^[0-9+]+$|^-$/;
+ my $sql = "DELETE FROM plan WHERE date='$date' and mealtype='$mealtype';";
+ $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;
@@ -202,38 +206,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();
@@ -404,44 +376,74 @@ 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();
- # FIXME now() is not start of day, set hour, minute and such to 0
}
- for (my $i = 0; $i < 14; $i++) {
- my $sql = "SELECT recipe_id, mealtype, comment_id FROM plan WHERE date='".
- $dt->ymd()."';";
- my @ids = $db->selectrow_array($sql);
-
+ for (my $i = 0; $i < $days; $i++) {
+ 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;
- 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();
+ 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 {
+ print " + ", get_recipe_name($meal->[0]), " (", $meal->[0], ")";
+ }
+ $last_mealtype = $mealtype;
}
+ print "\n";
$dt->add(days => 1);
if($dt->day_of_week == 1) {
$weekend_padding = "\n";
@@ -474,10 +476,9 @@ sub cmd_help() {
print "editrecipe <recipe_id>\n";
print "showrecipe <recipe_id>\n";
print "movemeal <source_date> <destination_date>\n";
- print "setmeal <date> <meal_id>\n";
- print "setstate <date> <idea|sourced|frozen|ready>\n";
+ print "setmeal <date> <mealtype> <meal_id>\n";
print "randmeal <date>\n";
- print "showplan [date]\n";
+ print "showplan [days] [date]\n";
print "postpone <date> <gap>\n";
print "storeportion <recipe_id> [amount] [storage]\n";
print "reprintlabel <inventory_id>\n";
@@ -715,40 +716,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) )
@@ -762,8 +743,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++) {
@@ -835,13 +814,11 @@ 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);
- } elsif ( $ARGV[0] eq "setstate") {
- print "Command failed!\n" unless (cmd_setstate($ARGV[1], $ARGV[2]) >= 0);
+ print "Command failed!\n" unless (cmd_setmeal($ARGV[1], $ARGV[2], $ARGV[3]) >= 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") {