diff options
author | cos <cos> | 2015-02-05 20:55:10 +0100 |
---|---|---|
committer | cos <cos> | 2015-02-05 20:55:10 +0100 |
commit | 606682c3415b6546d688f9aa5a4fed6546b46f12 (patch) | |
tree | 63e7371168d067780c9bc639e5a50eff4a2b06ca | |
parent | c7672c8168bd9dea41e77c1f17572cf099e06ec6 (diff) | |
download | mat-606682c3415b6546d688f9aa5a4fed6546b46f12.zip |
Have cmd_showplan check planstate using actual inventory.
-rwxr-xr-x | mat | 45 |
1 files changed, 30 insertions, 15 deletions
@@ -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 <date> <mealtype> <meal_id>\n"; print "setstate <date> <idea|sourced|frozen|ready>\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"; @@ -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") { |