Support disabling products
[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 price_popup = false;
33 function price_popup_open()
34 {
35     if (!price_popup) {
36         var options = { hideOnClick: false, canDragFunc: true };
37         var content = 'CONTENT_FORM';
38
39         price_popup = new Rico.Popup(options, false, false);
40         price_popup.createWindow('<strong>Verbinden</strong>',content,'auto','130px');
41
42         price_popup.openPopup(100,300);
43     } else
44         price_popup.openPopup();
45
46     price_popup.titleDiv.childNodes[0].innerHTML = 'Preis hinzufügen';
47
48     var form_id = document.getElementById('edit_id');
49     var artikel = document.getElementById('form_artikel');
50     artikel.value = form_id.value;
51
52     var datum = document.getElementById('form_datum');
53     var preis = document.getElementById('form_preis');
54     if (datum.value.length)
55       preis.activate();
56     else
57       datum.activate();
58 }
59
60 function open_popup()
61 {
62     var eid = document.getElementById('edit_id');
63     if (!eid || !eid.value.length) return false;
64
65     price_popup_open();
66     return false;
67 }
68
69 function add_price_callback(data)
70 {
71     info('Preis gespeichert');
72     grid_update(grid);
73 }
74
75 function add_price(obj)
76 {
77     price_popup.closePopup();
78
79     var datum = document.getElementById('form_datum');
80     if (!datum.value.length) {
81         error('Kein Rechnungsdatum angegeben');
82         return false;
83     }
84
85     var steuer = document.getElementById('form_steuer');
86     if (!steuer.value.length) {
87         error('Kein Steuersatz angegeben');
88         return false;
89     }
90
91     var preis = document.getElementById('form_preis');
92     if (!preis.value.length) {
93         error('Kein Preis angegeben');
94         return false;
95     }
96
97     var source = document.getElementById('source');
98     if (!source) return false;
99
100     var parms = 'source=' + source.innerHTML + '&callback=price&';
101     ajax_request('function', parms+Form.serialize(obj.form), add_price_callback);
102
103     preis.value = '';
104
105     return false;
106 }
107
108 EOC;
109
110 $jscode[] = str_replace('CONTENT_FORM', str_replace(array("\n"),
111                                                     array("\\\n"), $form), $javascript);
112
113
114 $buttons = <<<EOC
115 <p style="margin-top: 5px; margin-bottom: 4px; text-align: center;">
116 <button class="custom" onclick="return open_popup()">Preis hinzufügen</button>
117 </p>
118 EOC;
119
120 $mask = array(
121               'table' => 'metro_artikel',
122               'title' => 'Artikelstammdaten',
123               'join' => array('metro_abteilung ON abteilung = metro_abteilung.id'),
124               'list' => array(
125                               'id' => array(
126                                             'name' => 'ID',
127                                             'visible' => false,
128                                             'sql' => 'metro_artikel.id',
129                                             ),
130                               'artnr' => array(
131                                             'name' => 'ArtNr',
132                                             'width' => 60,
133                                             'type' => 'number',
134                                             'specs' => "decPlaces: 0, thouSep: '', filterUI: 't='",
135                                             ),
136                               'menge' => array(
137                                             'name' => 'Menge',
138                                             'width' => 60,
139                                             'specs' => "ClassName: 'alignrightpad', filterUI: 't3'",
140                                             ),
141                               'bezeichnung' => array(
142                                             'name' => 'Bezeichnung',
143                                             'width' => 300,
144                                             'specs' => "filterUI: 't'",
145                                             ),
146                               'netto' => array(
147                                             'name' => 'netto',
148                                             'width' => 60,
149                                             'type' => 'number',
150                                             'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
151                                             'sql' => '(SELECT preis FROM metro_artikel_preis ' .
152                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
153                                             ),
154                               'preis' => array(
155                                             'name' => 'Preis',
156                                             'width' => 60,
157                                             'type' => 'number',
158                                             'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
159                                             'sql' => '(SELECT preis * ((100 + steuersatz)/100) FROM metro_artikel_preis ' .
160                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
161                                             ),
162                               'datum' => array(
163                                             'name' => 'Datum',
164                                             'width' => 85,
165                                             'type' => 'text',
166                                             'sql' => '(SELECT datum FROM metro_artikel_preis ' .
167                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
168                                             'visible' => false,
169                                             ),
170                               'hersteller' => array(
171                                             'name' => 'Hersteller',
172                                             'width' => 100,
173                                             'specs' => "filterUI: 't'",
174                                             ),
175                               'abteilung' => array(
176                                             'name' => 'Abteilung',
177                                             'width' => 200,
178                                             'specs' => "filterUI: 's'",
179                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_abteilung','id','name').", 0, '')",
180                                             'distinct' => "SELECT DISTINCT abteilung,metro_abteilung.name FROM metro_artikel JOIN metro_abteilung ON abteilung = metro_abteilung.id ORDER BY name",
181                                             'visible' => false,
182                                             ),
183                               'gang' => array(
184                                             'name' => 'Gang',
185                                             'width' => 50,
186                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
187                                             'visible' => false,
188                                             ),
189                               'vpe' => array(
190                                             'name' => 'Einheit',
191                                             'width' => 70,
192                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
193                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_vpe','id','name').", 0, '')",
194                                             'distinct' => "SELECT DISTINCT vpe,metro_vpe.name FROM metro_artikel JOIN metro_vpe ON vpe = metro_vpe.id ORDER BY name",
195                                             ),
196                               ),
197               'edit' => array(
198                               'artnr' => array(
199                                             'name' => 'Artikelnummer',
200                                             'type' => 'number',
201                                             'size' => 15,
202                                             'required' => true,
203                                             ),
204                               'abteilung' => array(
205                                             'name' => 'Abteilung',
206                                             'type' => 'select',
207                                             'options' => 'SELECT id,name AS text FROM metro_abteilung ORDER BY name',
208                                             'option_empty' => '-- select --',
209                                             'required' => true,
210                                             ),
211                               'vpe' => array(
212                                             'name' => 'Verpackungseinheit',
213                                             'type' => 'select',
214                                             'options' => 'SELECT id,name AS text FROM metro_vpe ORDER BY name',
215                                             'option_empty' => '-- select --',
216                                             'required' => true,
217                                             ),
218                               'menge' => array(
219                                             'name' => 'Menge',
220                                             'type' => 'text',
221                                             'size' => 15,
222                                             'null' => true,
223                                             ),
224                               'bezeichnung' => array(
225                                             'name' => 'Bezeichnung',
226                                             'type' => 'text',
227                                             'size' => 21,
228                                             'required' => true,
229                                             ),
230                               'hinweis' => array(
231                                             'name' => 'Hinweis',
232                                             'type' => 'text',
233                                             'size' => 21,
234                                             'null' => true,
235                                             ),
236                               'hersteller' => array(
237                                             'name' => 'Hersteller',
238                                             'type' => 'text',
239                                             'size' => 21,
240                                             'null' => true,
241                                             ),
242                               'removed' => array(
243                                             'name' => 'Artikel nicht mehr im Programm',
244                                             'type' => 'boolean',
245                                             ),
246                               'buttons' => array(
247                                             'type' => 'html',
248                                             'code' => $buttons,
249                                             'sql' => false,
250                                             ),
251                               ),
252               'second' => array(
253                                 'title' => 'Preisverlauf',
254                                 'rows' => 5,
255                                 'table' => 'metro_artikel_preis',
256                                 'where' => 'artikel = {id} ORDER BY datum DESC',
257                                 'list' => array(
258                                                 'id' => array(
259                                                               'name' => 'ID',
260                                                               'visible' => false,
261                                                               ),
262                                                 'datum' => array(
263                                                               'name' => 'Datum',
264                                                               'width' => 180,
265                                                               ),
266                                                 'steuersatz' => array(
267                                                               'name' => 'Steuer',
268                                                               'width' => 60,
269                                                               'type' => 'number',
270                                                               'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
271                                                               ),
272                                                 'preis' => array(
273                                                               'name' => 'netto',
274                                                               'width' => 60,
275                                                               'type' => 'number',
276                                                               'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
277                                                               ),
278                                                 'brutto' => array(
279                                                               'name' => 'Preis',
280                                                               'width' => 60,
281                                                               'type' => 'number',
282                                                               'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
283                                                               'sql' => 'preis * ((100 + steuersatz)/100)',
284                                                               ),
285                                                 'empty' => array(
286                                                               'name' => '',
287                                                               'width' => 10,
288                                                               'sql' => "''",
289                                                               ),
290                                                 ),
291                                 ),
292               'callbacks' => array(
293                                    'price' => cb_price,
294                                 ),
295               );
296
297 function cb_price()
298 {
299   global $db;
300
301   if (empty($_POST['artikel']) || empty($_POST['datum']) || empty($_POST['preis']))
302     return array('error' => 'Nicht ausreichend Daten übermittelt');
303
304   $date = format_date($_POST['datum']);
305   $_POST['preis'] = str_replace(',','.',$_POST['preis']);
306
307   $sql = sprintf("INSERT INTO metro_artikel_preis (artikel,preis,steuersatz,datum,sys_user,sys_edit) " .
308                  "VALUES (%d,%.3f,%d,'%s',%s,now())",
309                  $_POST['artikel'],
310                  $_POST['preis'],
311                  $_POST['steuer'],
312                  $date,
313                  $db->quote($_SESSION['sys']['login']), $row['id']);
314
315   $sth = $db->query($sql);
316
317   return true;
318 }
319
320 ?>