e26008d6ba2690a76dcb0d072424499ce2063dc6
[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                               'hersteller' => array(
161                                             'name' => 'Hersteller',
162                                             'width' => 100,
163                                             'specs' => "filterUI: 't'",
164                                             ),
165                               'abteilung' => array(
166                                             'name' => 'Abteilung',
167                                             'width' => 200,
168                                             'specs' => "filterUI: 's'",
169                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_abteilung','id','name').", 0, '')",
170                                             'visible' => false,
171                                             ),
172                               'gang' => array(
173                                             'name' => 'Gang',
174                                             'width' => 50,
175                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
176                                             'visible' => false,
177                                             ),
178                               'vpe' => array(
179                                             'name' => 'Einheit',
180                                             'width' => 70,
181                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
182                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_vpe','id','name').", 0, '')",
183                                             ),
184                               ),
185               'edit' => array(
186                               'artnr' => array(
187                                             'name' => 'Artikelnummer',
188                                             'type' => 'number',
189                                             'size' => 15,
190                                             'required' => true,
191                                             ),
192                               'abteilung' => array(
193                                             'name' => 'Abteilung',
194                                             'type' => 'select',
195                                             'options' => 'SELECT id,name AS text FROM metro_abteilung ORDER BY name',
196                                             'option_empty' => '-- select --',
197                                             'required' => true,
198                                             ),
199                               'vpe' => array(
200                                             'name' => 'Verpackungseinheit',
201                                             'type' => 'select',
202                                             'options' => 'SELECT id,name AS text FROM metro_vpe ORDER BY name',
203                                             'option_empty' => '-- select --',
204                                             'required' => true,
205                                             ),
206                               'menge' => array(
207                                             'name' => 'Menge',
208                                             'type' => 'text',
209                                             'size' => 15,
210                                             'null' => true,
211                                             ),
212                               'bezeichnung' => array(
213                                             'name' => 'Bezeichnung',
214                                             'type' => 'text',
215                                             'size' => 21,
216                                             'required' => true,
217                                             ),
218                               'hinweis' => array(
219                                             'name' => 'Hinweis',
220                                             'type' => 'text',
221                                             'size' => 21,
222                                             'null' => true,
223                                             ),
224                               'hersteller' => array(
225                                             'name' => 'Hersteller',
226                                             'type' => 'text',
227                                             'size' => 21,
228                                             'null' => true,
229                                             ),
230                               'buttons' => array(
231                                             'type' => 'html',
232                                             'code' => $buttons,
233                                             'sql' => false,
234                                             ),
235                               ),
236               'callbacks' => array(
237                                    'price' => cb_price,
238                                 ),
239               );
240
241 function cb_price()
242 {
243   global $db;
244
245   if (empty($_POST['artikel']) || empty($_POST['datum']) || empty($_POST['preis']))
246     return array('error' => 'Nicht ausreichend Daten übermittelt');
247
248   $date = format_date($_POST['datum']);
249   $_POST['preis'] = str_replace(',','.',$_POST['preis']);
250
251   $sql = sprintf("INSERT INTO metro_artikel_preis (artikel,preis,steuersatz,datum,sys_user,sys_edit) " .
252                  "VALUES (%d,%.3f,%d,'%s',%s,now())",
253                  $_POST['artikel'],
254                  $_POST['preis'],
255                  $_POST['steuer'],
256                  $date,
257                  $db->quote($_SESSION['sys']['login']), $row['id']);
258
259   $sth = $db->query($sql);
260
261   return true;
262 }
263
264 ?>