Encode package size as well
[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 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 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                                             ),
134                               'menge' => array(
135                                             'name' => 'Menge',
136                                             'width' => 60,
137                                             'specs' => "ClassName: 'alignrightpad'",
138                                             ),
139                               'bezeichnung' => array(
140                                             'name' => 'Bezeichnung',
141                                             'width' => 300,
142                                             'specs' => "filterUI: 't'",
143                                             ),
144                               'netto' => array(
145                                             'name' => 'netto',
146                                             'width' => 60,
147                                             'type' => 'number',
148                                             'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
149                                             'sql' => '(SELECT preis FROM metro_artikel_preis ' .
150                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
151                                             ),
152                               'preis' => array(
153                                             'name' => 'Preis',
154                                             'width' => 60,
155                                             'type' => 'number',
156                                             'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
157                                             'sql' => '(SELECT preis * ((100 + steuersatz)/100) FROM metro_artikel_preis ' .
158                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
159                                             ),
160                               'datum' => array(
161                                             'name' => 'Datum',
162                                             'width' => 85,
163                                             'type' => 'text',
164                                             'sql' => '(SELECT datum FROM metro_artikel_preis ' .
165                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
166                                             'visible' => false,
167                                             ),
168                               'hersteller' => array(
169                                             'name' => 'Hersteller',
170                                             'width' => 100,
171                                             'specs' => "filterUI: 't'",
172                                             ),
173                               'abteilung' => array(
174                                             'name' => 'Abteilung',
175                                             'width' => 200,
176                                             'specs' => "filterUI: 's'",
177                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_abteilung','id','name').", 0, '')",
178                                             'visible' => false,
179                                             ),
180                               'gang' => array(
181                                             'name' => 'Gang',
182                                             'width' => 50,
183                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
184                                             'visible' => false,
185                                             ),
186                               'vpe' => array(
187                                             'name' => 'Einheit',
188                                             'width' => 70,
189                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
190                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_vpe','id','name').", 0, '')",
191                                             ),
192                               ),
193               'edit' => array(
194                               'artnr' => array(
195                                             'name' => 'Artikelnummer',
196                                             'type' => 'number',
197                                             'size' => 15,
198                                             'required' => true,
199                                             ),
200                               'abteilung' => array(
201                                             'name' => 'Abteilung',
202                                             'type' => 'select',
203                                             'options' => 'SELECT id,name AS text FROM metro_abteilung ORDER BY name',
204                                             'option_empty' => '-- select --',
205                                             'required' => true,
206                                             ),
207                               'vpe' => array(
208                                             'name' => 'Verpackungseinheit',
209                                             'type' => 'select',
210                                             'options' => 'SELECT id,name AS text FROM metro_vpe ORDER BY name',
211                                             'option_empty' => '-- select --',
212                                             'required' => true,
213                                             ),
214                               'menge' => array(
215                                             'name' => 'Menge',
216                                             'type' => 'text',
217                                             'size' => 15,
218                                             'null' => true,
219                                             ),
220                               'bezeichnung' => array(
221                                             'name' => 'Bezeichnung',
222                                             'type' => 'text',
223                                             'size' => 21,
224                                             'required' => true,
225                                             ),
226                               'hinweis' => array(
227                                             'name' => 'Hinweis',
228                                             'type' => 'text',
229                                             'size' => 21,
230                                             'null' => true,
231                                             ),
232                               'hersteller' => array(
233                                             'name' => 'Hersteller',
234                                             'type' => 'text',
235                                             'size' => 21,
236                                             'null' => true,
237                                             ),
238                               'buttons' => array(
239                                             'type' => 'html',
240                                             'code' => $buttons,
241                                             'sql' => false,
242                                             ),
243                               ),
244               'second' => array(
245                                 'title' => 'Preisverlauf',
246                                 'rows' => 5,
247                                 'table' => 'metro_artikel_preis',
248                                 'where' => 'artikel = {id} ORDER BY datum DESC',
249                                 'list' => array(
250                                                 'id' => array(
251                                                               'name' => 'ID',
252                                                               'visible' => false,
253                                                               ),
254                                                 'datum' => array(
255                                                               'name' => 'Datum',
256                                                               'width' => 180,
257                                                               ),
258                                                 'steuersatz' => array(
259                                                               'name' => 'Steuer',
260                                                               'width' => 60,
261                                                               'type' => 'number',
262                                                               'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
263                                                               ),
264                                                 'preis' => array(
265                                                               'name' => 'netto',
266                                                               'width' => 60,
267                                                               'type' => 'number',
268                                                               'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
269                                                               ),
270                                                 'brutto' => array(
271                                                               'name' => 'Preis',
272                                                               'width' => 60,
273                                                               'type' => 'number',
274                                                               'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
275                                                               'sql' => 'preis * ((100 + steuersatz)/100)',
276                                                               ),
277                                                 'empty' => array(
278                                                               'name' => '',
279                                                               'width' => 10,
280                                                               'sql' => "''",
281                                                               ),
282                                                 ),
283                                 ),
284               'callbacks' => array(
285                                    'price' => cb_price,
286                                 ),
287               );
288
289 function cb_price()
290 {
291   global $db;
292
293   if (empty($_POST['artikel']) || empty($_POST['datum']) || empty($_POST['preis']))
294     return array('error' => 'Nicht ausreichend Daten übermittelt');
295
296   $date = format_date($_POST['datum']);
297   $_POST['preis'] = str_replace(',','.',$_POST['preis']);
298
299   $sql = sprintf("INSERT INTO metro_artikel_preis (artikel,preis,steuersatz,datum,sys_user,sys_edit) " .
300                  "VALUES (%d,%.3f,%d,'%s',%s,now())",
301                  $_POST['artikel'],
302                  $_POST['preis'],
303                  $_POST['steuer'],
304                  $date,
305                  $db->quote($_SESSION['sys']['login']), $row['id']);
306
307   $sth = $db->query($sql);
308
309   return true;
310 }
311
312 ?>