#!/usr/bin/perl use strict; use warnings; use Config::Simple; use DBI; use DateTime; use YAML; use common; use utf8; binmode STDOUT, 'utf8'; my $goal_energy = 900; tie my %Config, "Config::Simple", '/etc/mat.conf'; my $db = DBI->connect($Config{'database'}, "", "", {RaiseError => 1, AutoCommit => 1, sqlite_unicode => 1}); my $recipe_id = $ARGV[0]; $recipe_id =~ s/(.*\/)*([0-9]*)-.*/$2/; #if ( length(@ingredients) == 1 ) { #if ( 0 ) { if ( $recipe_id eq $ARGV[0] ) { print "original_scale_reading: 0\n"; print "already_eaten: 0\n"; print "scale_reading: 0\n"; print "container_weight: 0\n"; print "\n"; print "ingredients:\n"; my $sql = "SELECT * FROM contents WHERE recipe_id=$recipe_id"; my $sth = $db->prepare($sql); $sth->execute(); # my $first_line = 1; my $ingredient_row; while ( my $content_row = $sth->fetchrow_hashref() ) { # if ( $first_line ) { # print 'my @ingredients = ( '; # $first_line = 0; # } else { # printf ", # %s\n%20s", $last_ingredient->{name}, " "; # } $sql = "SELECT * FROM ingredients WHERE id=$content_row->{ingredient_id}"; ( $ingredient_row ) = $db->selectrow_hashref($sql); # printf '{ ingredient => %4d, quantity => 0 }', $content_row->{ingredient_id}; print " - description: $ingredient_row->{'name'}\n"; print " ingredient: $content_row->{ingredient_id}\n"; print " # $content_row->{quantity} $content_row->{unit}\n"; print " quantity : 0\n\n"; } # printf " ); # %s\n", $last_ingredient->{name}; exit 1; } open my $cooking_file, '<', $ARGV[0]; my $cooking = YAML::LoadFile($cooking_file); my @ingredients = @{ $cooking->{'ingredients'} }; print "Goal energy: ", $goal_energy, " kJ\n\n"; my $energy = 0; my $ingredient_weight = 0; my $proteins = 0; my $carbs = 0; my $fats = 0; my $total_ingredient_weight = 0; for my $ingredient ( @ingredients ) { my $sql = "SELECT * FROM LivsmedelsDatabas WHERE ". "ingredient_id=$ingredient->{ingredient}"; my ( $row ) = $db->selectrow_hashref($sql); if ($row->{'Livsmedelsnamn'}) { # print $row->{'Livsmedelsnamn'}; $ingredient->{quantity} = eval($ingredient->{quantity}); $ingredient_weight += $ingredient->{quantity}; $energy += $ingredient->{quantity} * $row->{'Energi'} / 100; $proteins += $ingredient->{quantity} * $row->{'Protein'}; $carbs += $ingredient->{quantity} * $row->{'Kolhydrater'}; $fats += $ingredient->{quantity} * $row->{'Fett'}; $total_ingredient_weight += $ingredient->{quantity}; } else { $sql = "SELECT * FROM ingredients WHERE id=$ingredient->{ingredient}"; ( $row ) = $db->selectrow_hashref($sql); print "Could not find ingredient $ingredient->{ingredient} ". "($row->{'name'}) in LivsmedelsDatabas.\n"; } printf "%5d%5d g|%-40s%10d kJ (P: %5.2f C: %5.2f F: %5.2f)\n", $ingredient->{ingredient}, $ingredient->{quantity}, $row->{'Livsmedelsnamn'}, $ingredient->{quantity} * $row->{'Energi'} / 100, $row->{Protein}, $row->{Kolhydrater}, $row->{Fett}; # printf "%5d|%5d g|%-50s%10s kJ|%10s kJ\n", $ingredient->{ingredient}, # $ingredient->{quantity}, $row->{'Livsmedelsnamn'}, # $ingredient->{quantity} * $row->{'Energi'} / 100, $energy; } #my $total_food_weight = SCALE_READING - CONTAINER_WEIGHT + ALREADY_EATEN; my $total_food_weight = $cooking->{'scale_reading'} - $cooking->{'container_weight'} + 1.0 * $cooking->{'already_eaten'}; #my $total_food_weight = $cooking->{'scale_reading'} - $cooking->{'container_weight'} # + ($cooking->{'scale_reading'} - $cooking->{'container_weight'}) * # (1.0 * $cooking->{'already_eaten'} / # ($cooking->{'original_scale_reading'} - $cooking->{'container_weight'})); my $specific_energy = $energy / $total_food_weight * 100; # kJ/100 g #my $left = ( $total_food_weight - 198 ) * $specific_energy; #my $reduced = $left / 2.980; $proteins /= $total_ingredient_weight; $carbs /= $total_ingredient_weight; $fats /= $total_ingredient_weight; print "\n"; printf "Total ingredient weight: %d g\n", $ingredient_weight; printf "Total energy (all %d g): %d kJ\n", $total_food_weight, $energy; printf "Specific energi: %d kJ/100 g\n", $specific_energy; print "\n"; my $portions = int($energy/$goal_energy); print $portions, " matlådor på ", int($total_food_weight/$portions), " g ger ", int($total_food_weight/$portions/100 * $specific_energy) ," kJ/matlåda.\n"; $portions++; print $portions, " matlådor på ", int($total_food_weight/$portions), " g ger ", int($total_food_weight/$portions/100 * $specific_energy) ," kJ/matlåda.\n"; print "\n"; my $database_path = (split(":", $Config{'database'}))[2]; add_to_cooking($recipe_id, $database_path, $specific_energy, $proteins, $carbs, $fats, $cooking->{'cooking_comment'}); print "echo \"INSERT INTO cookings VALUES ($recipe_id, '". DateTime->now()->ymd()."', ".int($specific_energy+0.5)."); \" | sqlite3 ". $database_path."\n";