Don't update the table after adding material, update the material
[misc/kostenrechnung] / masks / materialverbrauch.php
1 <?php
2
3 $jscode[] = <<<EOC
4
5 post_save = materialverbrauch_post_save;
6
7 function materialverbrauch_post_save()
8 {
9     var einsatz = document.getElementById('edit_einsatz');
10
11     var params = 'source=' + grid.tableId.substr(5) + '&id=' + einsatz.value;
12     ajax_request('details', params, details_callback);
13 }
14
15 function einheit_callback(data)
16 {
17   var einheit = document.getElementById('edit_einheit');
18
19   if (!einheit) return false;
20
21   if (data.info[0].einheit.length)
22     einheit.innerHTML = data.info[0].einheit;
23 }
24
25 function select_material(obj)
26 {
27   get_info('einheit', {id: obj.options[obj.selectedIndex].value}, einheit_callback);
28 }
29
30 EOC;
31
32 function fetch_tbody()
33 {
34   $sql = 'SELECT materialien.material,menge,einheiten.einheit FROM materialverbrauch '
35     . 'JOIN materialien ON materialverbrauch.material = materialien.id '
36     . 'JOIN einheiten ON materialien.einheit = einheiten.id '
37     . 'WHERE einsatz = ' . intval($_POST['id']) . ' '
38     . 'ORDER BY materialien.material';
39
40   $data = query_db($sql);
41
42   $ret = '';
43   foreach ($data as $row)
44     $ret .= sprintf('<tr><td>%s</td><td align="right">%.2f %s</td></tr>',
45                     $row['material'], $row['menge'], $row['einheit']);
46
47   return $ret;
48 }
49
50 $mask = array(
51               'table' => 'einsatz',
52               'title' => 'Materialverbrauch zu Arbeitsberichten',
53               'join' => array(
54                               'kostenstellen ON einsatz.kostenstelle = kostenstellen.id',
55                               'gebiet ON kostenstellen.gebiet = gebiet.id',
56                               ),
57               'list' => array(
58                               'id' => array(
59                                             'name' => 'ID',
60                                             'sql' => 'einsatz.id',
61                                             'width' => 60,
62                                             'visible' => false,
63                                             ),
64                               'datum' => array(
65                                             'name' => 'Datum',
66                                             'type' => 'date',
67                                             'width' => 70,
68                                             ),
69                               'gebiet' => array(
70                                             'name' => 'Gebiet',
71                                             'width' => 230,
72                                             'sql' => 'gebiet.gebiet',
73                                             ),
74                               'kostenstelle' => array(
75                                             'name' => 'Kostenstelle',
76                                             'width' => 230,
77                                             'sql' => 'kostenstellen.kostenstelle',
78                                             ),
79                               'material' => array(
80                                             'name' => 'Material',
81                                             'width' => 70,
82                                             'control' => "new Rico.TableColumn.checkbox('t','f','0',true)",
83                                             'sql' => '(SELECT count(*) > 0 FROM materialverbrauch WHERE einsatz = einsatz.id)',
84                                             'specs' => "ClassName: 'aligncenter', canSort: false",
85                                             ),
86                               ),
87               'details' => array(
88                                  'html' => '<table id="materialverbrauch_table" cellspacing="0" style="margin-top: -13px;" border="0" width="100%">'.
89                                  '<thead>'.
90                                  '<tr bgcolor="#ffd76d" style="color: #444;"><th>Material</th><th>Menge</th></tr>'.
91                                  '</thead>'.
92                                  '<tbody id="detail_materialverbrauch_tbody"></tbody>'.
93                                  '</table>',
94                                  'list' => array(
95                                                  'materialverbrauch_tbody' => array(
96                                                                                     'fetch' => fetch_tbody,
97                                                                                     ),
98                                                  ),
99                                  ),
100               'edit_title' => 'Materialverbrauch vermerken',
101               'edit_table' => 'materialverbrauch',
102               'edit' => array(
103                               'einsatz' => array(
104                                             'name' => 'Einsatz',
105                                             'type' => 'hidden',
106                                             'size' => 10,
107                                             'sql' => 'id',
108                                             ),
109                               'material' => array(
110                                             'name' => 'Material',
111                                             'type' => 'select',
112                                             'options' => 'SELECT id,material AS text FROM materialien ORDER BY text',
113                                             'onchange' => 'select_material(this)',
114                                             'sql' => false,
115                                             ),
116                               'menge' => array(
117                                             'name' => 'Menge',
118                                             'type' => 'decimal',
119                                             'size' => 10,
120                                             'comment' => ' <span id="edit_einheit"></span>',
121                                             'sql' => false,
122                                             'required' => true,
123                                             ),
124                               ),
125               'buttons' => array(
126                                  'save' => false,
127                                  'delete' => false,
128                                  ),
129               'info' => array(
130                               'einheit' => array(
131                                             'sql' => 'SELECT einheiten.einheit FROM einheiten ' .
132                                                      'JOIN materialien ON materialien.einheit = einheiten.id ' .
133                                                      'WHERE materialien.id = {id}',
134                                             ),
135                                    ),
136               );
137
138 ?>