summaryrefslogtreecommitdiff
path: root/mat
diff options
context:
space:
mode:
Diffstat (limited to 'mat')
-rwxr-xr-xmat66
1 files changed, 56 insertions, 10 deletions
diff --git a/mat b/mat
index 707a675..2ec9415 100755
--- a/mat
+++ b/mat
@@ -14,6 +14,7 @@ use Text::Iconv;
use utf8;
binmode(STDOUT, 'utf8:');
+# FIXME die on non-existant config
tie my %Config, "Config::Simple", '/etc/mat.conf';
#use Data::Dumper;
@@ -126,7 +127,7 @@ my @schedule = (
sub convert_to_unit {
my ( $out_unit, $id, $in_amount, $in_unit ) = @_;
my %out_amount;
- my %convert;
+ my %convert; # volume [l], weight [g], piece [st]
# SELECT DISTINCT unit FROM contents;
# msk, krm, dl, tsk, cl, l, ml, nypa
@@ -139,6 +140,10 @@ sub convert_to_unit {
$convert{'volume'} = $in_amount;
} elsif ($in_unit eq "dl") {
$convert{'volume'} = $in_amount / 10;
+ } elsif ($in_unit eq "cl") {
+ $convert{'volume'} = $in_amount / 100;
+ } elsif ($in_unit eq "ml") {
+ $convert{'volume'} = $in_amount / 1000;
} elsif ($in_unit eq "msk") {
$convert{'volume'} = $in_amount * 0.015;
} elsif ($in_unit eq "tsk") {
@@ -164,17 +169,18 @@ sub convert_to_unit {
if ( $convert{'volume'} ) {
$volume = 0.001 * $convert{'volume'}; # m³
if ($ingredient->{density}) {
- $out_amount{'weight'} = 1000 * $density * $volume; # g
+ $out_amount{'weight'} = $density * $volume * 1000; # g
}
} elsif ( $convert{'weight'} ) {
- $weight = $convert{'weight'} / 1000; # kg
+ $weight = $convert{'weight'}; # g
+ $out_amount{'weight'} = $weight;
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
+ $out_amount{'volume'} = $weight / $density; # g
}
} elsif ( $convert{'piece'} ) {
if ($ingredient->{'piece_weight'}) {
@@ -184,6 +190,12 @@ sub convert_to_unit {
}
if ($out_unit eq "g") {
+ if($out_amount{'weight'}) {
+ return $out_amount{'weight'};
+ } else {
+ return
+ }
+ } elsif ($out_unit eq "kg") {
return $out_amount{'weight'};
} elsif ($out_unit eq "st") {
return $out_amount{'piece'};
@@ -297,6 +309,7 @@ sub cmd_inventory {
$sql .= " WHERE storage='$storage'" if ( $storage );
$sql .= " GROUP BY storage, preparation_date, recipe_id";
+ $total{'all'} = 0;
my $all = $db->selectall_arrayref($sql);
foreach my $row (@$all) {
$total{'all'} += @$row[1];
@@ -901,7 +914,7 @@ sub cmd_shoppinglist {
my $servings_col = $db->selectcol_arrayref("SELECT servings FROM recipes WHERE id=".$recipe.";");
my $servings;
- if(defined(@$servings_col[0])) {
+ if(defined(@$servings_col[0]) && (@$servings_col[0]) gt 0) {
$servings = @$servings_col[0];
} else {
$servings = 1;
@@ -924,7 +937,7 @@ sub cmd_shoppinglist {
for my $content ( keys(%$contents)) {
my %shop;
# print %$contents, "\n";
- my $ingredientcol = $db->selectcol_arrayref("SELECT name FROM ingredients WHERE id=".$content.";");
+ my $ingredientrow = $db->selectrow_arrayref("SELECT name, primary_unit FROM ingredients WHERE id=".$content.";");
# print $content, " ", @$ingredientcol[0], "\n";
# print $$contents{$content}{unit}, $content, " ", @$ingredientcol[0], "\n";
# print keys(%$contents), "\n";
@@ -932,12 +945,34 @@ sub cmd_shoppinglist {
$shop{quantity} = $$contents{$content}{quantity};
# $shop{shop_position} =
$shop{unit} = $$contents{$content}{unit};
- $shop{ingredient} = @$ingredientcol[0];
+ $shop{ingredient} = @$ingredientrow[0];
+ $shop{primary_unit} = @$ingredientrow[1];
$shop{recipe}[0] = $recipe;
push @shop_ingredients, \%shop;
}
}
- my @sorted_ingredients = sort { $a->{ingredient} cmp $b->{ingredient} } @shop_ingredients;
+
+
+ my @converted_ingredients;
+ for my $ingredient ( @shop_ingredients )
+ {
+ my $convert_unit;
+ my $convert_result;
+
+ $convert_unit = "g";
+
+ if ($convert_unit) {
+ $convert_result = convert_to_unit($convert_unit, $ingredient->{'id'},
+ $ingredient->{'quantity'}, $ingredient->{'unit'});
+ if ( $convert_result ) {
+ $ingredient->{'unit'} = $convert_unit;
+ $ingredient->{'quantity'} = $convert_result;
+ }
+ }
+ push @converted_ingredients, $ingredient;
+ }
+
+ my @sorted_ingredients = sort { $a->{ingredient} cmp $b->{ingredient} } @converted_ingredients;
my @squeezed_ingredients;
for my $squeeze ( @sorted_ingredients )
{
@@ -948,10 +983,21 @@ sub cmd_shoppinglist {
push @squeezed_ingredients, $squeeze;
}
}
-# for my $shop ( @sorted_ingredients )
for my $shop ( @squeezed_ingredients )
{
- printf "%4s %-8s %-40s", $shop->{quantity}, $shop->{unit}, $shop->{ingredient};
+ my $convert_result;
+ if ($shop->{'primary_unit'}) {
+ $convert_result = convert_to_unit($shop->{'primary_unit'}, $shop->{'id'},
+ $shop->{'quantity'}, $shop->{'unit'});
+ }
+
+ printf "%4s %-8s ", $shop->{quantity}, $shop->{unit};
+ if ($convert_result) {
+ printf "%4.4s %-8s ", $convert_result, $shop->{'primary_unit'};
+ } else {
+ printf "%4.4s %-8s ", "", "";
+ }
+ printf "%-40s", $shop->{ingredient};
for ( @{$shop->{recipe}} ) {
print get_recipe_name($_), ", ";
}