Simple sprit accounting module
[infodrom.org/service.infodrom.org] / class / spritlog.class.php
1 <?php
2
3 class SpritLog extends DatabaseTable {
4
5   public function __construct($id=false)
6   {
7     parent::__construct('sprit_log', $id);
8   }
9
10   public function distinctYears()
11   {
12     $sql = "SELECT DISTINCT EXTRACT(YEAR from date) AS year FROM sprit_log ORDER BY year DESC";
13     return $this->db->fetchObjectList($sql);
14   }
15
16   public function formatYear($year)
17   {
18     $out = '';
19     $sql = sprintf("SELECT * FROM sprit_log WHERE EXTRACT(YEAR from date) = %d ORDER BY date ASC", $year);;
20
21     $total_km = 0;
22     $total_liter = 0;
23     $total_price = 0;
24     foreach ($this->db->fetchObjectList($sql) as $row) {
25       $out .= sprintf('<tr id="%d"><td>%s</td><td>%s</td><td>%s</td>' .
26                       '<td class="right">%.2f</td><td class="right">%.2f</td><td class="right">%.2f</td>' .
27                       '<td class="right">%d</td><td class="right">%d</td></tr>',
28                       $row->id, assert_german_date($row->date),
29                       $row->city, $row->tankstelle,
30                       $row->price_liter, $row->liter,$row->price,
31                       $row->km, $row->km_total);
32       $total_km += $row->km;
33       $total_liter += $row->liter;
34       $total_price += $row->price;
35     }
36
37     if (strlen($out)) {
38       $out = '<table class="smallfont" width="100%">' .
39         '<thead><tr>' .
40         '<th width="70">Datum</th><th width="130" class="left">Ort</th><th class="left">Tankstelle</th>' .
41         '<th width="40">EUR/l</th><th width="40">l</th><th width="40">EUR</th><th width="40">km</th><th width="40">gesamt</th>' .
42         '</tr></thead>' .
43         $out .
44         '<tfoot><tr>' .
45         '<th colspan="4" class="left">Summe</th>' .
46         sprintf('<th class="right">%.2f</th><th class="right">%.2f</th><th class="right">%d</th><th>&nbsp;</th>',
47                 $total_liter, $total_price, $total_km).
48         '</tr></tfoot>' .
49         '</table>';
50     }
51     return $out;
52   }
53
54 }
55