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