Return error message when no products have been selected
[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
43     price_popup.titleDiv.childNodes[0].innerHTML = 'Preis hinzufügen';
44
45     var form_id = document.getElementById('edit_id');
46     var artikel = document.getElementById('form_artikel');
47     artikel.value = form_id.value;
48
49     price_popup.openPopup(100,300);
50
51     var datum = document.getElementById('form_datum');
52     datum.activate();
53 }
54
55 function open_popup()
56 {
57     var eid = document.getElementById('edit_id');
58     if (!eid || !eid.value.length) return false;
59
60     price_popup_open();
61     return false;
62 }
63
64 function add_price_callback(data)
65 {
66     info('Preis gespeichert');
67     grid_update(grid);
68 }
69
70 function add_price(obj)
71 {
72     price_popup.closePopup();
73
74     var datum = document.getElementById('form_datum');
75     if (!datum.value.length) {
76         error('Kein Rechnungsdatum angegeben');
77         return false;
78     }
79
80     var steuer = document.getElementById('form_steuer');
81     if (!steuer.value.length) {
82         error('Kein Steuersatz angegeben');
83         return false;
84     }
85
86     var preis = document.getElementById('form_preis');
87     if (!preis.value.length) {
88         error('Kein Preis angegeben');
89         return false;
90     }
91
92     var source = document.getElementById('source');
93     if (!source) return false;
94
95     var parms = 'source=' + source.innerHTML + '&callback=price&';
96     ajax_request('function', parms+Form.serialize(obj.form), add_price_callback);
97
98     preis.value = '';
99
100     return false;
101 }
102
103 EOC;
104
105 $jscode[] = str_replace('CONTENT_FORM', str_replace(array("\n"),
106                                                     array("\\\n"), $form), $javascript);
107
108
109 $buttons = <<<EOC
110 <p style="margin-top: 5px; margin-bottom: 4px; text-align: center;">
111 <button onclick="return open_popup()">Preis hinzufügen</button>
112 </p>
113 EOC;
114
115 $mask = array(
116               'table' => 'metro_artikel',
117               'title' => 'Artikelstammdaten',
118               'join' => array('metro_abteilung ON abteilung = metro_abteilung.id'),
119               'list' => array(
120                               'id' => array(
121                                             'name' => 'ID',
122                                             'visible' => false,
123                                             'sql' => 'metro_artikel.id',
124                                             ),
125                               'artnr' => array(
126                                             'name' => 'ArtNr',
127                                             'width' => 60,
128                                             ),
129                               'menge' => array(
130                                             'name' => 'Menge',
131                                             'width' => 60,
132                                             'specs' => "ClassName: 'alignrightpad'",
133                                             ),
134                               'bezeichnung' => array(
135                                             'name' => 'Bezeichnung',
136                                             'width' => 300,
137                                             'specs' => "filterUI: 't'",
138                                             ),
139                               'netto' => array(
140                                             'name' => 'netto',
141                                             'width' => 60,
142                                             'type' => 'number',
143                                             'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
144                                             'sql' => '(SELECT preis FROM metro_artikel_preis ' .
145                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
146                                             ),
147                               'preis' => array(
148                                             'name' => 'Preis',
149                                             'width' => 60,
150                                             'type' => 'number',
151                                             'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
152                                             'sql' => '(SELECT preis * ((100 + steuersatz)/100) FROM metro_artikel_preis ' .
153                                                      'WHERE artikel = metro_artikel.id ORDER BY datum DESC LIMIT 1)',
154                                             ),
155                               'hersteller' => array(
156                                             'name' => 'Hersteller',
157                                             'width' => 100,
158                                             'specs' => "filterUI: 't'",
159                                             ),
160                               'abteilung' => array(
161                                             'name' => 'Abteilung',
162                                             'width' => 200,
163                                             'specs' => "filterUI: 's'",
164                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_abteilung','id','name').", 0, '')",
165                                             'visible' => false,
166                                             ),
167                               'gang' => array(
168                                             'name' => 'Gang',
169                                             'width' => 50,
170                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
171                                             'visible' => false,
172                                             ),
173                               'vpe' => array(
174                                             'name' => 'Einheit',
175                                             'width' => 70,
176                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
177                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_vpe','id','name').", 0, '')",
178                                             ),
179                               ),
180               'edit' => array(
181                               'artnr' => array(
182                                             'name' => 'Artikelnummer',
183                                             'type' => 'number',
184                                             'size' => 15,
185                                             'required' => true,
186                                             ),
187                               'abteilung' => array(
188                                             'name' => 'Abteilung',
189                                             'type' => 'select',
190                                             'options' => 'SELECT id,name AS text FROM metro_abteilung ORDER BY name',
191                                             'option_empty' => '-- select --',
192                                             'required' => true,
193                                             ),
194                               'vpe' => array(
195                                             'name' => 'Verpackungseinheit',
196                                             'type' => 'select',
197                                             'options' => 'SELECT id,name AS text FROM metro_vpe ORDER BY name',
198                                             'option_empty' => '-- select --',
199                                             'required' => true,
200                                             ),
201                               'menge' => array(
202                                             'name' => 'Menge',
203                                             'type' => 'text',
204                                             'size' => 15,
205                                             'null' => true,
206                                             ),
207                               'bezeichnung' => array(
208                                             'name' => 'Bezeichnung',
209                                             'type' => 'text',
210                                             'size' => 21,
211                                             'required' => true,
212                                             ),
213                               'hersteller' => array(
214                                             'name' => 'Hersteller',
215                                             'type' => 'text',
216                                             'size' => 21,
217                                             'null' => true,
218                                             ),
219                               'buttons' => array(
220                                             'type' => 'html',
221                                             'code' => $buttons,
222                                             'sql' => false,
223                                             ),
224                               ),
225               'callbacks' => array(
226                                    'price' => cb_price,
227                                 ),
228               );
229
230 function cb_price()
231 {
232   global $db;
233
234   if (empty($_POST['artikel']) || empty($_POST['datum']) || empty($_POST['preis']))
235     return array('error' => 'Nicht ausreichend Daten übermittelt');
236
237   $date = format_date($_POST['datum']);
238   $_POST['preis'] = str_replace(',','.',$_POST['preis']);
239
240   $sql = sprintf("INSERT INTO metro_artikel_preis (artikel,preis,steuersatz,datum,sys_user,sys_edit) " .
241                  "VALUES (%d,%.3f,%d,'%s',%s,now())",
242                  $_POST['artikel'],
243                  $_POST['preis'],
244                  $_POST['steuer'],
245                  $date,
246                  $db->quote($_SESSION['sys']['login']), $row['id']);
247
248   $sth = $db->query($sql);
249
250   return true;
251 }
252
253 ?>