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