summaryrefslogtreecommitdiff
path: root/energyadder.pl
blob: 501a04bc92db4363aec0ed35d0921101b87f44fd (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/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);
print "echo \"INSERT INTO cookings VALUES ($recipe_id, '".
    DateTime->now()->ymd()."', ".int($specific_energy+0.5)."); \" | sqlite3 ".
    $database_path."\n";