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