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