diff options
-rw-r--r-- | README | 1 | ||||
-rwxr-xr-x | mat | 100 |
2 files changed, 95 insertions, 6 deletions
@@ -11,6 +11,7 @@ CREATE TABLE contents (recipe_id INTEGER, ingredient_id INTEGER, quantity FLOAT, CREATE TABLE plan (date DATETIME, mealtype, recipe_id INTEGER, state, comment_id); CREATE TABLE comments (id INTEGER PRIMARY KEY AUTOINCREMENT, comment); CREATE TABLE inventory (id INTEGER PRIMARY KEY AUTOINCREMENT, recipe_id INTEGER, preparation_date DATETIME, amount INTEGER, storage); +CREATE TABLE queue (id INTEGER PRIMARY KEY, recipe_id INTEGER, servings INTEGER); All basic operations are performed using the perl script mat. It is possible to get a crude list of commands available by running: "mat help". @@ -11,13 +11,15 @@ use GD; use GD::Barcode; use GD::Barcode::QRcode; use Text::Iconv; +use utf8; +binmode(STDOUT, 'utf8:'); tie my %Config, "Config::Simple", '/etc/mat.conf'; #use Data::Dumper; -my $db = DBI->connect("dbi:SQLite:recipe.db", "", "", {RaiseError => 1, - AutoCommit => 1}); +my $db = DBI->connect($Config{'database'}, "", "", {RaiseError => 1, + AutoCommit => 1, sqlite_unicode => 1}); my @recipes; my @ingredients; @@ -467,6 +469,10 @@ sub cmd_help() { print "relocate <portion_id> <new_storage>\n"; print "inventory [storage]\n"; print "shoppinglist <days_to_shop_for>\n"; + print "queueshow\n"; + print "queueadd <recipe_id> <servings>\n"; + print "queuerm <queue_id>\n"; + print "queuemv (Yet to be implemented)\n"; } sub interactive_edit_recipe_ingredients { @@ -631,6 +637,58 @@ sub cmd_searchrecipes { return 1; } +sub cmd_queueadd { + my ( $recipe_id, $servings ) = @_; + + my $sql = "SELECT COUNT(id) FROM queue;"; + my $id = $db->selectrow_arrayref($sql); + + return -1 unless $recipe_id =~ "^[0-9]+\$"; + return -1 unless $servings =~ "^[0-9]+\$"; + if (get_recipe_name($recipe_id) ne 'NULL') { + $sql = "INSERT INTO queue (id, recipe_id, servings) VALUES (@$id[0], ". + "$recipe_id, $servings)"; + $db->do($sql); + } + + return 0; +} + +sub cmd_queueshow { + my $sql = "SELECT id, recipe_id, servings FROM queue"; + + my $sth = $db->prepare($sql); + my $rv = $sth->execute; + $db->{RaiseError} = 0; + while (my @row_ary = $sth->fetchrow_array) { + printf "%3s|%2d x %s (%d)\n", $row_ary[0], $row_ary[2], + get_recipe_name($row_ary[1]), $row_ary[1]; + + } + $db->{RaiseError} = 1; +} + +sub cmd_queuerm { + my ( $queue_id ) = @_; + + my $sql = "DELETE FROM queue WHERE id=$queue_id;"; + my $sth = $db->prepare($sql); + my $rv = $sth->execute; + + my $id = $queue_id; + do { + my $next = $id + 1; + $sql = "UPDATE queue SET id=$id WHERE id=$next;"; + + $id++; + } while($db->do($sql) == 1); +} + +sub cmd_queuemv { + print "Yet to be implemented!\n"; +} + + sub cmd_shoppinglist { # Argument is number of days to shop for @@ -651,13 +709,14 @@ sub cmd_shoppinglist { # print $i, "\n"; # } - my $entries = $db->selectall_arrayref("SELECT recipe_id FROM plan WHERE recipe_id AND date ". + 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 $queue_entries = $db->selectall_arrayref("SELECT recipe_id FROM queue;"); my %recipe_count; - for my $entry ( @$entries ) { + for my $entry ( @$plan_entries ) { $recipe_count{$entry->{recipe_id}}++; # print $recipe_count{$entry->{recipe_id}}, " ", $entry->{recipe_id}, "\n"; ## print $entry->{recipe_id}.""; @@ -668,6 +727,11 @@ sub cmd_shoppinglist { ## TODO Loop through recipes to add ingredients to a list ## TODO Print ingredients } + 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; @@ -710,13 +774,29 @@ sub cmd_shoppinglist { # $shop{shop_position} = $shop{unit} = $$contents{$content}{unit}; $shop{ingredient} = @$ingredientcol[0]; + $shop{recipe}[0] = $recipe; push @shop_ingredients, \%shop; } } my @sorted_ingredients = sort { $a->{ingredient} cmp $b->{ingredient} } @shop_ingredients; - for my $shop ( @sorted_ingredients ) + my @squeezed_ingredients; + for my $squeeze ( @sorted_ingredients ) { - printf "%4s %-8s %s\n", $shop->{quantity}, $shop->{unit}, $shop->{ingredient}; + if (exists($squeezed_ingredients[-1]) and $squeezed_ingredients[-1]{ingredient} eq $squeeze->{ingredient} and $squeezed_ingredients[-1]{unit} eq $squeeze->{unit}) { + $squeezed_ingredients[-1]{quantity} += $squeeze->{quantity}; + push @{$squeezed_ingredients[-1]{recipe}}, $squeeze->{recipe}[0] unless grep(/$squeeze->{recipe}[0]/, @{$squeezed_ingredients[-1]{recipe}}); + } else { + push @squeezed_ingredients, $squeeze; + } + } +# for my $shop ( @sorted_ingredients ) + for my $shop ( @squeezed_ingredients ) + { + printf "%4s %-8s %-40s", $shop->{quantity}, $shop->{unit}, $shop->{ingredient}; + for ( @{$shop->{recipe}} ) { + print get_recipe_name($_), ", "; + } + print "\n"; } return 1; } @@ -754,6 +834,14 @@ if ($ARGV[0]) { print "Command failed!\n" unless (cmd_storeportion($ARGV[1], $ARGV[2], $ARGV[3]) >= 0); } elsif ( $ARGV[0] eq "relocate") { print "Command failed!\n" unless (cmd_relocate($ARGV[1], $ARGV[2]) >= 0); + } elsif ( $ARGV[0] eq "queueadd") { + print "Command failed!\n" unless (cmd_queueadd($ARGV[1], $ARGV[2]) >= 0); + } elsif ( $ARGV[0] eq "queuerm") { + print "Command failed!\n" unless (cmd_queuerm($ARGV[1], $ARGV[2]) >= 0); + } elsif ( $ARGV[0] eq "queuemv") { + print "Command failed!\n" unless (cmd_queuemv($ARGV[1], $ARGV[2]) >= 0); + } elsif ( $ARGV[0] eq "queueshow") { + print "Command failed!\n" unless (cmd_queueshow()); } elsif ( $ARGV[0] eq "shoppinglist") { print "Command failed!\n" unless (cmd_shoppinglist($ARGV[1]) >= 0); } else { |