e0b8f7066c31b90dd1e2d8adcba8727e5e36b5b9
[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 EXTRACT(YEAR from date) AS year,sum(price) AS sum,sum(km) AS km FROM sprit_log GROUP BY year 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 spritlog" 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         '<tbody>' . $out . '</tbody>' .
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   public function ajaxList(Array $data)
55   {
56     return array('html' => array('list_'.$data['year'] => $this->formatYear($data['year'])));
57   }
58
59   public function ajaxAdd(Array $data)
60   {
61     $data = array('machine' => intval($data['machine']),
62                   'date' => assert_iso_date($data['date']),
63                   'city' => $data['city'],
64                   'tankstelle' => $data['tankstelle'],
65                   'price_liter' => str_replace(',','.',$data['price_liter']),
66                   'liter' => str_replace(',','.',$data['liter']),
67                   'price' => str_replace(',','.',$data['price']),
68                   'km' => intval($data['km']),
69                   'km_total' => intval($data['km_total']),
70                   'sys_edit' => 'now()',
71                   'sys_user' => $_SERVER['REMOTE_USER']);
72
73     foreach ($data as $k => $v)
74       if (empty($v))
75         return ajax_error(sprintf('Field %s must not be empty', $k));
76
77     if (empty($data['id'])) {
78       $ok = $this->db->insertInto('sprit_log', $data);
79     } else {
80       $ok = $this->db->update('sprit_log', $data, 'id = ' . intval($data['id']));
81     }
82
83     $d = explode('-', $data['date']);
84     return array('status' => $ok, 'year' => $d[0]);
85
86   }
87 }