summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xmat85
1 files changed, 85 insertions, 0 deletions
diff --git a/mat b/mat
index bed1f66..6ec0fe8 100755
--- a/mat
+++ b/mat
@@ -14,6 +14,13 @@ my @recipes;
my @ingredients;
my @contents;
+sub ceiling {
+ my ( $num ) = @_;
+ my $ret = int($num);
+ $ret++ unless (int($num) eq $num);
+ return $ret;
+}
+
sub read_ingredients {
@ingredients = ();
@@ -188,6 +195,7 @@ sub cmd_postpone {
$startdate = DateTime::Format::ISO8601->parse_datetime( $date );
} else {
$startdate = DateTime->now();
+ # FIXME now() is not start of day, set hour, minute and such to 0
}
$gap = 1;
@@ -221,6 +229,7 @@ sub cmd_showplan {
$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 < 7; $i++) {
@@ -368,6 +377,79 @@ sub cmd_addrecipe {
return interactive_edit_recipe_ingredients($recipe_id[0]);
}
+sub cmd_shoppinglist {
+ # Argument is number of days to shop for
+
+ my ( $shopdays ) = @_;
+ my $startdate = DateTime->now();
+ my $enddate = $startdate->clone();
+ $shopdays=7 unless $shopdays;
+
+ $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 $entries = $db->selectall_arrayref("SELECT recipe_id FROM plan WHERE date ".
+ "BETWEEN '".$startdate."' and '".$enddate."'", { Slice => {} });
+ my %recipe_count;
+ for my $entry ( @$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
+ }
+# map {print "| $_ = ${recipe_count{$_}}\n"} sort keys(%recipe_count);
+
+ my @shop_recipes;
+ for my $recipe ( keys(%recipe_count) )
+ {
+ my $servings_col = $db->selectcol_arrayref("SELECT servings FROM recipes WHERE id=".$recipe.";");
+ my $servings;
+
+ if(defined(@$servings_col[0])) {
+ $servings = @$servings_col[0];
+ } else {
+ $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++) {
+ push @shop_recipes, $recipe;
+ }
+ }
+
+ my @shop_ingredients;
+ for my $recipe (@shop_recipes) {
+# my $contents = $db->selectall_arrayref("SELECT * FROM contents WHERE recipe_id=".$recipe.";");
+ my $contents = $db->selectall_hashref("SELECT * FROM contents WHERE recipe_id=".$recipe.";", 'ingredient_id');
+ for my $content ( keys(%$contents)) {
+# print %$contents, "\n";
+ my $ingredientcol = $db->selectcol_arrayref("SELECT name FROM ingredients WHERE id=".$content.";");
+# print $content, " ", @$ingredientcol[0], "\n";
+# print $$contents{$content}{unit}, $content, " ", @$ingredientcol[0], "\n";
+ print $$contents{$content}{quantity}, " ", $$contents{$content}{unit}, " ", @$ingredientcol[0], "\n";
+# print keys(%$contents), "\n";
+ }
+ }
+ return 1;
+}
+
# MAIN PROGRAM ################################################################
read_recipe_db;
@@ -400,6 +482,9 @@ if ($ARGV[0]) {
if ( $ARGV[0] eq "postpone") {
print "Command failed!\n" unless (cmd_postpone($ARGV[1]) >= 0);
}
+ if ( $ARGV[0] eq "shoppinglist") {
+ print "Command failed!\n" unless (cmd_shoppinglist($ARGV[1]) >= 0);
+ }
} else {
cmd_showplan;
}