summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2015-10-03 23:43:14 +0200
committercos <cos>2015-10-03 23:43:14 +0200
commitf28631984cb8ea86193541c0564873182a9c79d6 (patch)
tree7ba0003a8f0c1d529b3c58ab066f512b98d72037
parent7ccd884b6d57e84df601e689336479a74b913ce2 (diff)
downloadmat-f28631984cb8ea86193541c0564873182a9c79d6.zip
Add some unit conversion to shoppinglist.
-rwxr-xr-xmat60
1 files changed, 50 insertions, 10 deletions
diff --git a/mat b/mat
index 12c67d8..7d60ec8 100755
--- a/mat
+++ b/mat
@@ -127,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 [kg], piece [st]
# SELECT DISTINCT unit FROM contents;
# msk, krm, dl, tsk, cl, l, ml, nypa
@@ -147,9 +147,9 @@ sub convert_to_unit {
} elsif ($in_unit eq "krm") {
$convert{'volume'} = $in_amount * 0.001;
} elsif ($in_unit eq "g") {
- $convert{'weight'} = $in_amount;
+ $convert{'weight'} = $in_amount / 1000;
} elsif ($in_unit eq "kg") {
- $convert{'weight'} = $in_amount * 1000;
+ $convert{'weight'} = $in_amount;
} elsif ($in_unit eq "st") {
$convert{'piece'} = $in_amount;
}
@@ -165,10 +165,11 @@ 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; # kg
}
} elsif ( $convert{'weight'} ) {
- $weight = $convert{'weight'} / 1000; # kg
+ $weight = $convert{'weight'}; # kg
+ $out_amount{'weight'} = $weight;
if ($ingredient->{'piece_weight'}) {
$piece_weight = $ingredient->{'piece_weight'}; # g/piece
$out_amount{'piece'} = $convert{'weight'} /
@@ -185,6 +186,12 @@ sub convert_to_unit {
}
if ($out_unit eq "g") {
+ if($out_amount{'weight'}) {
+ return $out_amount{'weight'} * 1000;
+ } else {
+ return
+ }
+ } elsif ($out_unit eq "kg") {
return $out_amount{'weight'};
} elsif ($out_unit eq "st") {
return $out_amount{'piece'};
@@ -926,7 +933,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";
@@ -934,12 +941,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 )
{
@@ -950,10 +979,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($_), ", ";
}