diff options
author | cos <cos> | 2015-08-04 20:03:32 +0200 |
---|---|---|
committer | cos <cos> | 2015-08-04 20:03:32 +0200 |
commit | 11bb49856b9883c3acdfd246cf68aa2fb551cbd1 (patch) | |
tree | 307cbace900bfc7961930af2c57617b8154c1b55 | |
parent | 9f8f7ba22658ced4a728706bbd01b79e66261f1f (diff) | |
parent | 8da4fc1b825748208d5de50a9250ddd4314c139d (diff) | |
download | mat-11bb49856b9883c3acdfd246cf68aa2fb551cbd1.zip |
Merge branch 'master' into debian
-rw-r--r-- | README | 2 | ||||
-rwxr-xr-x | mat | 195 |
2 files changed, 143 insertions, 54 deletions
@@ -6,7 +6,7 @@ If for some obscure reason, you would like to use this hackish beast, the first thing you need to do is create a sqlite database with the following schema: CREATE TABLE recipes (id INTEGER PRIMARY KEY AUTOINCREMENT, name, uri, servings INTEGER); -CREATE TABLE ingredients (id INTEGER PRIMARY KEY AUTOINCREMENT, name, shop_position INTEGER); +CREATE TABLE ingredients (id INTEGER PRIMARY KEY AUTOINCREMENT, name, shop_position INTEGER, primary_unit, density, piece_weight); CREATE TABLE contents (recipe_id INTEGER, ingredient_id INTEGER, quantity FLOAT, unit); CREATE TABLE plan (date DATETIME, mealtype, recipe_id INTEGER, state, comment_id); CREATE TABLE comments (id INTEGER PRIMARY KEY AUTOINCREMENT, comment); @@ -123,6 +123,75 @@ my @schedule = ( } ); +sub convert_to_unit { + my ( $out_unit, $id, $in_amount, $in_unit ) = @_; + my %out_amount; + my %convert; + + # SELECT DISTINCT unit FROM contents; + # msk, krm, dl, tsk, cl, l, ml, nypa + # st, knippen, burk, paket, påse, blad, ark, skiv + # dm, cm, mm + # n/a + # g, kg + + if ($in_unit eq "l") { + $convert{'volume'} = $in_amount; + } elsif ($in_unit eq "dl") { + $convert{'volume'} = $in_amount / 10; + } elsif ($in_unit eq "msk") { + $convert{'volume'} = $in_amount * 0.015; + } elsif ($in_unit eq "tsk") { + $convert{'volume'} = $in_amount * 0.005; + } elsif ($in_unit eq "g") { + $convert{'weight'} = $in_amount; + } elsif ($in_unit eq "kg") { + $convert{'weight'} = $in_amount * 1000; + } elsif ($in_unit eq "st") { + $convert{'piece'} = $in_amount; + } + + my $sql = "SELECT density, piece_weight FROM ingredients WHERE id=".$id.";"; + my $ingredient = $db->selectrow_hashref($sql); + my $volume; + my $density; + my $weight; + $density = $ingredient->{density}; # kg/m³ + my $piece_weight; + + if ( $convert{'volume'} ) { + $volume = 0.001 * $convert{'volume'}; # m³ + if ($ingredient->{density}) { + $out_amount{'weight'} = 1000 * $density * $volume; # g + } + } elsif ( $convert{'weight'} ) { + $weight = $convert{'weight'} / 1000; # kg + if ($ingredient->{'piece_weight'}) { + $piece_weight = $ingredient->{'piece_weight'}; # g/piece + $out_amount{'piece'} = $convert{'weight'} / + $ingredient->{'piece_weight'}; # piece + } + if ($ingredient->{density}) { + $out_amount{'volume'} = 1000 * $weight / $density; # g + } + } elsif ( $convert{'piece'} ) { + if ($ingredient->{'piece_weight'}) { + $piece_weight = $ingredient->{'piece_weight'}; # g/piece + $out_amount{'weight'} = $convert{'piece'} * $piece_weight; # g + } + } + + if ($out_unit eq "g") { + return $out_amount{'weight'}; + } elsif ($out_unit eq "st") { + return $out_amount{'piece'}; + } elsif ($out_unit eq "l") { + return $out_amount{'volume'}; + } + + return undef; +} + sub cmd_setmeal { my ( $date, $mealtype, $recipe_ids ) = @_; @@ -300,7 +369,7 @@ sub print_label { } my $row3; if ($energy) { - $row3 = $energy.' kJ'; + $row3 = $energy.' kJ ('.int($energy * 100 / $amount).' kJ/hg)'; } else { $row3 = '<energy content unknown>'; } @@ -627,7 +696,7 @@ sub cmd_addrecipe { # FIXME Make it possible to provide name and uri as command line arguments # instead of solely interactive mode. - my ( $recipe_name, $uri, $answer, $sql ); + my ( $recipe_name, $uri, $servings, $answer, $sql ); print "Recipe name: "; $recipe_name = <STDIN>; chomp $recipe_name; @@ -635,9 +704,13 @@ sub cmd_addrecipe { print "Recipe uri: "; $uri = <STDIN>; chomp $uri; + print "Servings: "; + $servings = <STDIN>; + chomp $servings; - $sql = "INSERT INTO recipes (name, uri) VALUES (".$db->quote($recipe_name). - ", ".$db->quote($uri).");"; + $sql = "INSERT INTO recipes (name, uri, servings) VALUES (". + $db->quote($recipe_name).", ".$db->quote($uri).", ". + $db->quote($servings).");"; print "$sql\n\n"; print "Add to database? (y/n): "; $answer = <STDIN>; @@ -654,10 +727,11 @@ sub cmd_addrecipe { sub cmd_showrecipe { # Argument is recipe_id - my ( $recipe_id ) = @_; + my ( $recipe_id, $desired_servings ) = @_; my $recipe_row = $db->selectrow_arrayref("SELECT name, uri, servings FROM recipes WHERE id=".$recipe_id.";"); my $servings; + my $ingredient_multipler = 1; print @$recipe_row[0]; if(defined(@$recipe_row[1])) { @@ -667,7 +741,12 @@ sub cmd_showrecipe { } if(defined(@$recipe_row[2])) { $servings = @$recipe_row[2]; - print "Serves: $servings\n\n" + if(defined($desired_servings)) { + print "Serves: $desired_servings\n\n"; + $ingredient_multipler = $desired_servings/$servings; + } else { + print "Serves: $servings\n\n"; + } } else { $servings = 1; print "WARNING servings is not set for recipe $recipe_id!\n\n"; @@ -675,9 +754,25 @@ sub cmd_showrecipe { my $contents = $db->selectall_hashref("SELECT * FROM contents WHERE recipe_id=".$recipe_id.";", 'ingredient_id'); for my $content ( keys(%$contents)) { - my $ingredientcol = $db->selectcol_arrayref("SELECT name FROM ingredients WHERE id=".$content.";"); - printf "%4s %-8s %s\n", $$contents{$content}{quantity}, - $$contents{$content}{unit}, @$ingredientcol[0]; + $$contents{$content}{quantity} = $$contents{$content}{quantity} * $ingredient_multipler; + my $ingredientcol = $db->selectrow_arrayref("SELECT name, primary_unit FROM ingredients WHERE id=".$content.";"); + my $convert_unit; + my $convert_result; + if ($$contents{$content}{unit} eq "g" ) { + $convert_unit = @$ingredientcol[1]; + } else { + $convert_unit = "g"; + } + if ($convert_unit) { + $convert_result = convert_to_unit($convert_unit, $content, $$contents{$content}{quantity}, $$contents{$content}{unit}); + } + printf "%4s %-8s ", $$contents{$content}{quantity}, $$contents{$content}{unit}; + if ( $convert_result ) { + printf "(%4.4s %-8s)", $convert_result, $convert_unit; + } else { + printf "%15s", " "; + } + printf " %s\n", @$ingredientcol[0]; } return 1; @@ -841,8 +936,9 @@ sub cmd_shoppinglist { print "\n"; } open(my $f, "extrashopping.txt"); - my $extra = <$f>; - print "$extra"; + while(my $extra = <$f>) { + print "$extra"; + } return 1; } @@ -850,49 +946,42 @@ sub cmd_shoppinglist { read_recipe_db; +my $commands = { + "help" => { func => \&cmd_help, min => 0, max => 0 }, + "addrecipe" => { func => \&cmd_addrecipe, min => 0, max => 0 }, + "searchrecipes" => { func => \&cmd_searchrecipes, min => 1, max => 1 }, + "editrecipe" => { func => \&cmd_editrecipe, min => 1, max => 1 }, + "showrecipe" => { func => \&cmd_showrecipe, min => 1, max => 2 }, + "movemeal" => { func => \&cmd_movemeal, min => 2, max => 2 }, + "setmeal" => { func => \&cmd_setmeal, min => 3, max => 3 }, + "setcomment" => { func => \&cmd_setcomment, min => 3, max => 3 }, + "randmeal" => { func => \&cmd_randmeal, min => 1, max => 1 }, + "showplan" => { func => \&cmd_showplan, min => 0, max => 2 }, + "postpone" => { func => \&cmd_postpone, min => 2, max => 2 }, + "inventory" => { func => \&cmd_inventory, min => 1, max => 1 }, + "storeportion" => { func => \&cmd_storeportion, min => 1, max => 3 }, + "reprintlabel" => { func => \&cmd_reprintlabel, min => 1, max => 1 }, + "relocate" => { func => \&cmd_relocate, min => 2, max => 2 }, + "queueadd" => { func => \&cmd_queueadd, min => 2, max => 2 }, + "queuerm" => { func => \&cmd_queuerm, min => 1, max => 1 }, + "queuemv" => { func => \&cmd_queuemv, min => 2, max => 2 }, + "queueshow" => { func => \&cmd_queueshow, min => 0, max => 0 }, + "shoppinglist" => { func => \&cmd_shoppinglist, min => 0, max => 1 } +}; + if ($ARGV[0]) { - if ( $ARGV[0] eq "help") { - print "Command failed!\n" unless (cmd_help >= 0); - } elsif ( $ARGV[0] eq "addrecipe") { - print "Command failed!\n" unless (cmd_addrecipe() >= 0); - } elsif ( $ARGV[0] eq "searchrecipes") { - print "Command failed!\n" unless (cmd_searchrecipes($ARGV[1]) >= 0); - } elsif ( $ARGV[0] eq "editrecipe") { - print "Command failed!\n" unless (cmd_editrecipe($ARGV[1]) >= 0); - } elsif ( $ARGV[0] eq "showrecipe") { - print "Command failed!\n" unless (cmd_showrecipe($ARGV[1]) >= 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], $ARGV[3]) >= 0); - } elsif ( $ARGV[0] eq "setcomment") { - print "Command failed!\n" unless (cmd_setcomment($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], $ARGV[2]) >= 0); - } elsif ( $ARGV[0] eq "postpone") { - print "Command failed!\n" unless (cmd_postpone($ARGV[1]) >= 0); - } elsif ( $ARGV[0] eq "inventory") { - print "Command failed!\n" unless (cmd_inventory($ARGV[1]) >= 0); - } elsif ( $ARGV[0] eq "storeportion") { - print "Command failed!\n" unless (cmd_storeportion($ARGV[1], $ARGV[2], $ARGV[3]) >= 0); - } elsif ( $ARGV[0] eq "labb") { - print "Command failed!\n" unless (cmd_labb($ARGV[1], $ARGV[2], $ARGV[3]) >= 0); - } elsif ( $ARGV[0] eq "reprintlabel") { - print "Command failed!\n" unless (cmd_reprintlabel($ARGV[1]) >= 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); + my $command = $commands->{$ARGV[0]}; + my $argcount = $#ARGV; + if (defined($command)) { + if ($argcount < $command->{'min'}) { + print "Too few arguments for command $ARGV[0].\n"; + exit 1; + } + if ($argcount > $command->{'max'}) { + print "Too many arguments for command $ARGV[0].\n"; + exit 1; + } + print "Command failed!\n" unless($command->{'func'}(splice @ARGV, 1) >= 0); } else { print "Unknown command $ARGV[0]\n"; exit 1; |