db->fetchObjectList($sql);
}
public function formatYear($year)
{
$out = '';
$sql = sprintf("SELECT * FROM sprit_log WHERE EXTRACT(YEAR from date) = %d ORDER BY date ASC", $year);;
$total_km = 0;
$total_liter = 0;
$total_price = 0;
foreach ($this->db->fetchObjectList($sql) as $row) {
$out .= sprintf('
%s | %s | %s | ' .
'%.2f | %.2f | %.2f | ' .
'%d | %d |
',
$row->id, assert_german_date($row->date),
$row->city, $row->id, $row->tankstelle,
$row->price_liter, $row->liter,$row->price,
$row->km, $row->km_total);
$total_km += $row->km;
$total_liter += $row->liter;
$total_price += $row->price;
}
if (strlen($out)) {
$out = '' .
'' .
'Datum | Ort | Tankstelle | ' .
'EUR/l | l | EUR | km | gesamt | ' .
'
' .
'' . $out . '' .
'' .
'Summe | ' .
sprintf('%.2f | %.2f | %d | | ',
$total_liter, $total_price, $total_km).
'
' .
'
';
}
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('€ %.2f %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']);
}
}