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