Simple sprit accounting module
[infodrom.org/service.infodrom.org] / src / InfoCon / sprit / moduleajaxbackend.class.php
1 <?php
2
3 class ModuleAJAXBackend extends AJAXBackendBase {
4
5   public function machines()
6   {
7     $out = '';
8     $sql = "SELECT id,name FROM sprit_machine WHERE active = 1 ORDER BY name";
9     foreach ($this->db->fetchObjectList($sql) as $row) {
10       $out .= sprintf('<li>%s ', $row->name);
11       $out .= '<form action="list.php" method="POST" style="display:inline;">';
12       $out .= sprintf('<input type="hidden" name="machine" value="%d">', $row->id);
13       $out .= sprintf('<img src="%spix/arrowrightmonth.gif" onclick="machine_list(this);" style="margin-bottom:-4px;" title="Liste zeigen">',
14                       $this->relativeRootPath());;
15       $out .= '</form>';
16       $out .= '</li>';
17     }
18     return array('status' => true, 'list' => $out);;
19   }
20
21   public function savemachine()
22   {
23     if (empty($_POST['id'])) {
24       $sql = sprintf("INSERT INTO sprit_machine (name,active,sys_user,sys_edit) " .
25                      "VALUES (%s,1,%s,now())",
26                      $this->db->quote(utf8_decode($_POST['name'])),
27                      $this->db->quote($_SERVER['REMOTE_USER']));
28     } else {
29       $sql = sprintf("UPDATE sprit_machine SET name=%s,sys_edit=now(),sys_user=%s WHERE id = %d",
30                      $this->db->quote(utf8_decode($_POST['name'])),
31                      $this->db->quote($_SERVER['REMOTE_USER']),
32                      $_POST['id']);
33     }
34     return array('status' => $this->db->execute($sql));
35   }
36
37   public function savelog()
38   {
39     $data = array('machine' => intval($_POST['machine']),
40                   'date' => assert_iso_date($_POST['date']),
41                   'city' => $_POST['city'],
42                   'tankstelle' => $_POST['tankstelle'],
43                   'price_liter' => str_replace(',','.',$_POST['price_liter']),
44                   'liter' => str_replace(',','.',$_POST['liter']),
45                   'price' => str_replace(',','.',$_POST['price']),
46                   'km' => intval($_POST['km']),
47                   'km_total' => intval($_POST['km_total']),
48                   'sys_edit' => 'now()',
49                   'sys_user' => $_SERVER['REMOTE_USER']);
50
51     foreach ($data as $k => $v)
52       if (empty($v))
53         json_error(sprintf('Field %s must not be empty', $k));
54
55     if (empty($_POST['id'])) {
56       $ok = $this->db->insertInto('sprit_log', $data);
57     } else {
58       $ok = $this->db->update('sprit_log', $data, 'id = ' . intval($_POST['id']));
59     }
60
61     $d = explode('-', $data['date']);
62     return array('status' => $ok, 'year' => $d[0]);
63   }
64
65   public function loadyear()
66   {
67     $log = new SpritLog();
68     return array('status' => true,
69                  'list' => $log->formatYear($_POST['year']),
70                  'year' => $_POST['year']);
71   }
72
73 }