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