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