summaryrefslogtreecommitdiff
path: root/energyadder.pl
blob: 730a9535057efbf12151f53b09dab3f5d4f11a5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/perl

use strict;
use warnings;

use Config::Simple;
use DBI;
use DateTime;
use YAML;

use common;
use utf8;

binmode STDOUT, 'utf8';

use constant 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;

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'};
    $energy += $ingredient->{quantity} * $row->{'Energi'} / 100;
  } 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\n", $ingredient->{ingredient},
      $ingredient->{quantity}, $row->{'Livsmedelsnamn'},
      $ingredient->{quantity} * $row->{'Energi'} / 100;
#  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;
my $total_food_weight = $cooking->{'scale_reading'} - $cooking->{'container_weight'}
    + $cooking->{'scale_reading'} * (1.0 * $cooking->{'already_eaten'} / $cooking->{'original_scale_reading'});
my $specific_energy = $energy / $total_food_weight * 100; # kJ/100 g
#my $left = ( $total_food_weight - 198 ) * $specific_energy;

#my $reduced = $left / 2.980;

print "\n";
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, $specific_energy, $database_path);
print "echo \"INSERT INTO cookings VALUES ($recipe_id, '".
    DateTime->now()->ymd()."', ".int($specific_energy+0.5)."); \" | sqlite3 ".
    $database_path."\n";