Propagate summary to headline
[infodrom.org/service.infodrom.org] / class / spritlog.class.php
index 2e501e4..65df3fd 100644 (file)
@@ -9,7 +9,7 @@ class SpritLog extends DatabaseTable {
 
   public function distinctYears()
   {
-    $sql = "SELECT DISTINCT EXTRACT(YEAR from date) AS year FROM sprit_log ORDER BY year DESC";
+    $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";
     return $this->db->fetchObjectList($sql);
   }
 
@@ -22,11 +22,11 @@ class SpritLog extends DatabaseTable {
     $total_liter = 0;
     $total_price = 0;
     foreach ($this->db->fetchObjectList($sql) as $row) {
-      $out .= sprintf('<tr id="%d"><td>%s</td><td>%s</td><td>%s</td>' .
+      $out .= sprintf('<tr id="%d"><td>%s</td><td>%s</td><td><span route="SpritLog/EditTankstelle" item_id="%d">%s</span></td>' .
                      '<td class="right">%.2f</td><td class="right">%.2f</td><td class="right">%.2f</td>' .
                      '<td class="right">%d</td><td class="right">%d</td></tr>',
                      $row->id, assert_german_date($row->date),
-                     $row->city, $row->tankstelle,
+                     $row->city, $row->id, $row->tankstelle,
                      $row->price_liter, $row->liter,$row->price,
                      $row->km, $row->km_total);
       $total_km += $row->km;
@@ -35,12 +35,12 @@ class SpritLog extends DatabaseTable {
     }
 
     if (strlen($out)) {
-      $out = '<table class="smallfont" width="100%">' .
+      $out = '<table class="smallfont spritlog" width="100%">' .
        '<thead><tr>' .
        '<th width="70">Datum</th><th width="130" class="left">Ort</th><th class="left">Tankstelle</th>' .
        '<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>' .
        '</tr></thead>' .
-       $out .
+       '<tbody>' . $out . '</tbody>' .
        '<tfoot><tr>' .
        '<th colspan="4" class="left">Summe</th>' .
        sprintf('<th class="right">%.2f</th><th class="right">%.2f</th><th class="right">%d</th><th>&nbsp;</th>',
@@ -48,8 +48,73 @@ class SpritLog extends DatabaseTable {
        '</tr></tfoot>' .
        '</table>';
     }
-    return $out;
+    return array($out, $total_liter, $total_price, $total_km);
   }
 
-}
+  public function ajaxList(Array $data)
+  {
+    list($table, $liter, $price, $km) = $this->formatYear($data['year']);
+
+    return array('html' => array('list_'.$data['year'] => $table,
+                                'sum_'.$data['year'] => sprintf('&euro; %.2f&nbsp;&nbsp;%d km', $price, $km),
+                                ));
+  }
+
+  public function ajaxAdd(Array $data)
+  {
+    $data = array('machine' => intval($data['machine']),
+                  'date' => assert_iso_date($data['date']),
+                  'city' => $data['city'],
+                  'tankstelle' => $data['tankstelle'],
+                  'price_liter' => str_replace(',','.',$data['price_liter']),
+                  'liter' => str_replace(',','.',$data['liter']),
+                  'price' => str_replace(',','.',$data['price']),
+                  'km' => intval($data['km']),
+                  'km_total' => intval($data['km_total']),
+                  'sys_edit' => 'now()',
+                  'sys_user' => $_SERVER['REMOTE_USER']);
+
+    foreach ($data as $k => $v)
+      if (empty($v))
+        return ajax_error(sprintf('Field %s must not be empty', $k));
+
+    if (empty($data['id'])) {
+      $ok = $this->db->insertInto('sprit_log', $data);
+    } else {
+      $ok = $this->db->update('sprit_log', $data, 'id = ' . intval($data['id']));
+    }
+
+    $d = explode('-', $data['date']);
+    return array('status' => $ok, 'year' => $d[0]);
+  }
 
+  public function ajaxSuggestCity(Array $data)
+  {
+    $data['query'] .= '%';
+    $sql = sprintf("SELECT DISTINCT city FROM sprit_log WHERE lower(city) LIKE %s ORDER BY city",
+                  $this->quote(strtolower($data['query'])));
+    $list = array();
+    foreach ($this->fetchObjectList($sql) as $row)
+      $list[] = array('value' => $row->city, 'data' => $row->city);
+    return array('suggestions' => $list);
+  }
+
+  public function ajaxSuggestTankstelle(Array $data)
+  {
+    $data['query'] .= '%';
+    $sql = sprintf("SELECT DISTINCT tankstelle FROM sprit_log WHERE city = %s AND lower(tankstelle) LIKE %s ORDER BY tankstelle",
+                  $this->quote($data['city']),
+                  $this->quote(strtolower($data['query'])));
+    $list = array();
+    foreach ($this->fetchObjectList($sql) as $row)
+      $list[] = array('value' => $row->tankstelle, 'data' => $row->tankstelle);
+    return array('suggestions' => $list);
+  }
+
+  public function ajaxEditTankstelle(Array $data)
+  {
+    if (!strlen($data['content'])) return false;
+    return $this->modify('tankstelle', $data['content']);
+  }
+
+}