Add filters
[misc/hallinta-metro] / artikel.php
1 <?php
2
3 $style[] = <<<EOC
4 div#form_price {
5   padding-left: 10px;
6   padding-right: 10px;
7   font-size: 90%;
8   color: #555;
9 }
10 EOC;
11
12 $form = '
13 <div id="form_price">
14 <form>
15 <input id="form_artikel" name="artikel" type="hidden" />
16 <label for="form_datum">Rechnungsdatum</label><br>
17 <input id="form_datum" name="datum" size="8">&nbsp;<img class="calendar" src="images/icons/calendar.gif" onclick="calendar(\\\'form_datum\\\',event)" />
18 <br>
19 <label for="form_steuer">Steuersatz</label><br>
20 <input id="form_steuer" name="steuer" size="10">
21 <br>
22 <label for="form_preis">Preis</label><br>
23 <input id="form_preis" name="preis" size="10">
24 <div class="buttons" style="margin-top: 15px;">
25 <button class="custom" onclick="return add_price(this);">Hinzufügen</button>
26 </div>
27 </form>
28 </div>
29 ';
30
31 $javascript = <<<EOC
32 var custom_this = false;
33 function custom_save_callback(data)
34 {
35     if (data.count > 0) {
36         var text = "Diese Artikelnummer ist bereits " + data.count + " mal vorhanden.\\nSoll der Artikel trotzdem gespeichert werden?";
37         if (confirm(text))
38             form_save(custom_this);
39     } else {
40         form_save(custom_this);
41     }
42 }
43
44 function custom_save(obj)
45 {
46     custom_this = obj;
47     var id = document.getElementById('edit_id');
48
49     if (!id.value.length)
50         return custom_insert(obj);
51
52     var artnr = document.getElementById('edit_artnr');
53
54     var parms = 'source=' + source.innerHTML + '&callback=artnr';
55     parms += '&artnr=' + artnr.value;
56     parms += '&artikel=' + id.value;
57     ajax_request('function', parms, custom_save_callback);
58
59     return false;
60 }
61
62 function custom_insert_callback(data)
63 {
64     if (data.count > 0) {
65         var text = "Diese Artikelnummer ist bereits " + data.count + " mal vorhanden.\\nSoll der Artikel trotzdem hinzugefügt werden?";
66         if (confirm(text))
67             form_insert(custom_this);
68     } else {
69         form_insert(custom_this);
70     }
71 }
72
73 function custom_insert(obj)
74 {
75     custom_this = obj;
76     var artnr = document.getElementById('edit_artnr');
77
78     var parms = 'source=' + source.innerHTML + '&callback=artnr';
79     parms += '&artnr=' + artnr.value;
80     ajax_request('function', parms, custom_insert_callback);
81
82     return false;
83 }
84
85 var price_popup = false;
86 function price_popup_open()
87 {
88     if (!price_popup) {
89         var options = { hideOnClick: false, canDragFunc: true };
90         var content = 'CONTENT_FORM';
91
92         price_popup = new Rico.Popup(options, false, false);
93         price_popup.createWindow('<strong>Verbinden</strong>',content,'auto','130px');
94
95         price_popup.openPopup(100,300);
96     } else
97         price_popup.openPopup();
98
99     price_popup.titleDiv.childNodes[0].innerHTML = 'Preis hinzufügen';
100
101     var form_id = document.getElementById('edit_id');
102     var artikel = document.getElementById('form_artikel');
103     artikel.value = form_id.value;
104
105     var datum = document.getElementById('form_datum');
106     var preis = document.getElementById('form_preis');
107     if (datum.value.length)
108       preis.activate();
109     else
110       datum.activate();
111 }
112
113 function open_popup()
114 {
115     var eid = document.getElementById('edit_id');
116     if (!eid || !eid.value.length) return false;
117
118     price_popup_open();
119     return false;
120 }
121
122 function add_price_callback(data)
123 {
124     info('Preis gespeichert');
125     grid_update(grid);
126 }
127
128 function add_price(obj)
129 {
130     price_popup.closePopup();
131
132     var datum = document.getElementById('form_datum');
133     if (!datum.value.length) {
134         error('Kein Rechnungsdatum angegeben');
135         return false;
136     }
137
138     var steuer = document.getElementById('form_steuer');
139     if (!steuer.value.length) {
140         error('Kein Steuersatz angegeben');
141         return false;
142     }
143
144     var preis = document.getElementById('form_preis');
145     if (!preis.value.length) {
146         error('Kein Preis angegeben');
147         return false;
148     }
149
150     var source = document.getElementById('source');
151     if (!source) return false;
152
153     var parms = 'source=' + source.innerHTML + '&callback=price&';
154     ajax_request('function', parms+Form.serialize(obj.form), add_price_callback);
155
156     preis.value = '';
157
158     return false;
159 }
160
161 Rico.onLoad(function(){
162     var btn = document.getElementById('button_save');
163     btn.onclick = function () {return custom_save(this);};
164     var btn = document.getElementById('button_insert');
165     btn.onclick = function () {return custom_insert(this);};
166 });
167 EOC;
168
169 $jscode[] = str_replace('CONTENT_FORM', str_replace(array("\n"),
170                                                     array("\\\n"), $form), $javascript);
171
172
173 $buttons = <<<EOC
174 <p style="margin-top: 5px; margin-bottom: 4px; text-align: center;">
175 <button class="custom" onclick="return open_popup()">Preis hinzufügen</button>
176 </p>
177 EOC;
178
179 $mask = array(
180               'table' => 'metro_artikel',
181               'title' => 'Artikelstammdaten',
182               'join' => array('metro_abteilung ON abteilung = metro_abteilung.id'),
183               'list' => array(
184                               'id' => array(
185                                             'name' => 'ID',
186                                             'visible' => false,
187                                             'sql' => 'metro_artikel.id',
188                                             ),
189                               'artnr' => array(
190                                             'name' => 'ArtNr',
191                                             'width' => 60,
192                                             'type' => 'number',
193                                             'specs' => "decPlaces: 0, thouSep: '', filterUI: 't='",
194                                             ),
195                               'menge' => array(
196                                             'name' => 'Menge',
197                                             'width' => 60,
198                                             'specs' => "ClassName: 'alignrightpad', filterUI: 't3'",
199                                             ),
200                               'bezeichnung' => array(
201                                             'name' => 'Bezeichnung',
202                                             'width' => 300,
203                                             'specs' => "filterUI: 't'",
204                                             ),
205                               'netto' => array(
206                                             'name' => 'netto',
207                                             'width' => 60,
208                                             'type' => 'number',
209                                             'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
210                                             'sql' => '(SELECT preis FROM metro_artikel_preis ' .
211                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
212                                             ),
213                               'preis' => array(
214                                             'name' => 'Preis',
215                                             'width' => 60,
216                                             'type' => 'number',
217                                             'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
218                                             'sql' => '(SELECT preis * ((100 + steuersatz)/100) FROM metro_artikel_preis ' .
219                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
220                                             ),
221                               'datum' => array(
222                                             'name' => 'Datum',
223                                             'width' => 85,
224                                             'type' => 'text',
225                                             'sql' => '(SELECT datum FROM metro_artikel_preis ' .
226                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
227                                             'visible' => false,
228                                             ),
229                               'hersteller' => array(
230                                             'name' => 'Hersteller',
231                                             'width' => 100,
232                                             'specs' => "filterUI: 't'",
233                                             ),
234                               'abteilung' => array(
235                                             'name' => 'Abteilung',
236                                             'width' => 200,
237                                             'specs' => "filterUI: 's'",
238                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_abteilung','id','name').", 0, '')",
239                                             'distinct' => "SELECT DISTINCT abteilung,metro_abteilung.name FROM metro_artikel JOIN metro_abteilung ON abteilung = metro_abteilung.id ORDER BY name",
240                                             'visible' => false,
241                                             ),
242                               'gang' => array(
243                                             'name' => 'Gang',
244                                             'width' => 50,
245                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
246                                             'visible' => false,
247                                             ),
248                               'vpe' => array(
249                                             'name' => 'Einheit',
250                                             'width' => 70,
251                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
252                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_vpe','id','name').", 0, '')",
253                                             'distinct' => "SELECT DISTINCT vpe,metro_vpe.name FROM metro_artikel JOIN metro_vpe ON vpe = metro_vpe.id ORDER BY name",
254                                             ),
255                               ),
256               'edit' => array(
257                               'artnr' => array(
258                                             'name' => 'Artikelnummer',
259                                             'type' => 'number',
260                                             'size' => 15,
261                                             'required' => true,
262                                             ),
263                               'abteilung' => array(
264                                             'name' => 'Abteilung',
265                                             'type' => 'select',
266                                             'options' => 'SELECT id,name AS text FROM metro_abteilung ORDER BY name',
267                                             'option_empty' => '-- select --',
268                                             'required' => true,
269                                             ),
270                               'vpe' => array(
271                                             'name' => 'Verpackungseinheit',
272                                             'type' => 'select',
273                                             'options' => 'SELECT id,name AS text FROM metro_vpe ORDER BY name',
274                                             'option_empty' => '-- select --',
275                                             'required' => true,
276                                             ),
277                               'menge' => array(
278                                             'name' => 'Menge',
279                                             'type' => 'text',
280                                             'size' => 15,
281                                             'null' => true,
282                                             ),
283                               'bezeichnung' => array(
284                                             'name' => 'Bezeichnung',
285                                             'type' => 'text',
286                                             'size' => 21,
287                                             'required' => true,
288                                             ),
289                               'hinweis' => array(
290                                             'name' => 'Hinweis',
291                                             'type' => 'text',
292                                             'size' => 21,
293                                             'null' => true,
294                                             ),
295                               'hersteller' => array(
296                                             'name' => 'Hersteller',
297                                             'type' => 'text',
298                                             'size' => 21,
299                                             'null' => true,
300                                             ),
301                               'removed' => array(
302                                             'name' => 'Artikel nicht mehr im Programm',
303                                             'type' => 'boolean',
304                                             ),
305                               'buttons' => array(
306                                             'type' => 'html',
307                                             'code' => $buttons,
308                                             'sql' => false,
309                                             ),
310                               ),
311               'second' => array(
312                                 'title' => 'Preisverlauf',
313                                 'rows' => 5,
314                                 'table' => 'metro_artikel_preis',
315                                 'where' => 'artikel = {id} ORDER BY datum DESC',
316                                 'list' => array(
317                                                 'id' => array(
318                                                               'name' => 'ID',
319                                                               'visible' => false,
320                                                               ),
321                                                 'datum' => array(
322                                                               'name' => 'Datum',
323                                                               'width' => 180,
324                                                               ),
325                                                 'steuersatz' => array(
326                                                               'name' => 'Steuer',
327                                                               'width' => 60,
328                                                               'type' => 'number',
329                                                               'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
330                                                               ),
331                                                 'preis' => array(
332                                                               'name' => 'netto',
333                                                               'width' => 60,
334                                                               'type' => 'number',
335                                                               'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
336                                                               ),
337                                                 'brutto' => array(
338                                                               'name' => 'Preis',
339                                                               'width' => 60,
340                                                               'type' => 'number',
341                                                               'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
342                                                               'sql' => 'preis * ((100 + steuersatz)/100)',
343                                                               ),
344                                                 'empty' => array(
345                                                               'name' => '',
346                                                               'width' => 10,
347                                                               'sql' => "''",
348                                                               ),
349                                                 ),
350                                 ),
351               'callbacks' => array(
352                                    'price' => cb_price,
353                                    'artnr' => cb_artnr,
354                                 ),
355               );
356
357 function cb_price()
358 {
359   global $db;
360
361   if (empty($_POST['artikel']) || empty($_POST['datum']) || empty($_POST['preis']))
362     return array('error' => 'Nicht ausreichend Daten übermittelt');
363
364   $date = format_date($_POST['datum']);
365   $_POST['preis'] = str_replace(',','.',$_POST['preis']);
366
367   $sql = sprintf("INSERT INTO metro_artikel_preis (artikel,preis,steuersatz,datum,sys_user,sys_edit) " .
368                  "VALUES (%d,%.3f,%d,'%s',%s,now())",
369                  $_POST['artikel'],
370                  $_POST['preis'],
371                  $_POST['steuer'],
372                  $date,
373                  $db->quote($_SESSION['sys']['login']), $row['id']);
374
375   $sth = $db->query($sql);
376
377   return true;
378 }
379
380 function cb_artnr()
381 {
382   global $db;
383
384   if (empty($_POST['artnr']))
385     return array('error' => 'Nicht ausreichend Daten übermittelt');
386
387   $sql = sprintf("SELECT count(*) AS count FROM metro_artikel WHERE artnr = %d", $_POST['artnr']);
388   if (!empty($_POST['artikel']))
389     $sql .= sprintf(" AND id <> %d", $_POST['artikel']);
390
391   $sth = $db->query($sql);
392   $row = $sth->fetch();
393
394   return array('count' => $row['count']);
395 }
396
397 ?>