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