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