summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2015-04-20 21:47:59 +0200
committercos <cos>2015-04-20 21:47:59 +0200
commit52cb72ed01bb5c8cc3b8255f289de081268efdfe (patch)
tree9e1a37a6c206411b4f1c2bca1b2bcfc21138970f
parent2390635012871d02e21d3c6c3b39860159ef862f (diff)
downloadmat-52cb72ed01bb5c8cc3b8255f289de081268efdfe.zip
Initial unit conversion support.
-rw-r--r--README2
-rwxr-xr-xmat90
2 files changed, 88 insertions, 4 deletions
diff --git a/README b/README
index c86f394..35a6b64 100644
--- a/README
+++ b/README
@@ -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);
diff --git a/mat b/mat
index 0e5c33f..e920470 100755
--- a/mat
+++ b/mat
@@ -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 ) = @_;
@@ -675,9 +744,24 @@ 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];
+ 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;