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