Fix character encoding differences
[misc/hallinta-metro] / ordersatz_pos.php
1 <?php
2   /*
3    Küchenbetreuer:
4    E-Mail-Adresse für Kopie
5    Telefonnr für Rückfragen:
6    Abholung am:
7    Abholung um Uhrzeit:
8
9    - Ordersatz auswählen || neuen Ordersatz erstellen || Ordersatz kopieren
10    - Ordersatz abschließen = PDF-Datei erzeugen, archivieren, per Mail, per Fax
11    */
12
13 include_once($_SESSION['sys']['basedir'].'masks/metro/utils.php');
14 include_once($_SESSION['sys']['basedir'].'lib/mail.php');
15
16 if (substr($_SERVER["SCRIPT_FILENAME"],-10) == '/index.php' &&
17     !empty($_GET['id']))
18   $_SESSION['ordersatz'] = $_GET['id'];
19
20 $jscode[] = <<<EOC
21 grid_column_edit[1] = {
22     default_menu: false,
23     width: '9em',
24     values: [
25              ['löschen', '', 0],
26              ['1 mal bestellen', '1', 1],
27              ['2 mal bestellen', '2', 2],
28              ['3 mal bestellen', '3', 3],
29              ['4 mal bestellen', '4', 4],
30              ['5 mal bestellen', '5', 5],
31              ['6 mal bestellen', '6', 6],
32              ['7 mal bestellen', '7', 7],
33              ['8 mal bestellen', '8', 8],
34              ['10 mal bestellen', '10', 10],
35              ['12 mal bestellen', '12', 12],
36              ['14 mal bestellen', '14', 14]
37              ]
38 };
39
40 function build_pdf_callback(data)
41 {
42     info('PDF erstellt');
43     window.open('ajax/ajax.php?tmpdir='+data.tmpdir+'&source=metro__ordersatz_pos&func=file&name=download','_blank');
44 }
45
46 function build_pdf()
47 {
48   var source = document.getElementById('source');
49   if (!source) return false;
50
51   var parms = 'source=' + source.innerHTML + '&callback=pdf&';
52   ajax_request('function', parms, build_pdf_callback);
53   return false;
54 }
55 EOC;
56
57 $buttons = <<<EOC
58 <p style="margin-top: 0px; margin-bottom: 4px; text-align: center;">
59 <button onclick="return build_pdf()">PDF erstellen</button>
60 &nbsp;
61 <button onclick="return finish()">Abschließen</button>
62 <br>Bei Abschluß wird ein Fax zu Metro und eine Kopie per Mail an den Koch geschickt.
63 </p>
64 EOC;
65
66 function get_title($id)
67 {
68   global $db;
69
70   $query = sprintf('SELECT name FROM metro_ordersatz WHERE id = %d', $id);
71   $sth = $db->query($query);
72   $row = $sth->fetch();
73   if ($row) return 'Ordersatz für ' . $row['name'];
74   else return 'Unbekannter Ordersatz';
75 }
76
77 # INSERT INTO metro_ordersatz_pos (ordersatz,artikel,sys_user,sys_edit) SELECT ORDERID,id,'joey',now() FROM metro_artikel;
78 # DELETE FROM metro_ordersatz_pos WHERE ordersatz = ORDERID AND anzahl IS NULL
79 $mask = array(
80               'table' => 'metro_ordersatz',
81               'join' => array('metro_ordersatz_pos ON ordersatz = metro_ordersatz.id',
82                               'metro_artikel ON artikel = metro_artikel.id'),
83               'title' => get_title($_SESSION['ordersatz']),
84               'where' => sprintf('ordersatz = %d AND owner = %d AND done IS NULL', $_SESSION['ordersatz'], $_SESSION['sys']['uid']),
85               'list' => array(
86                               'id' => array(
87                                             'name' => 'ID',
88                                             'sql' => 'metro_artikel.id',
89                                             'visible' => false,
90                                             ),
91                               'anzahl' => array(
92                                             'name' => 'Anzahl',
93                                             'width' => 60,
94                                             'specs' => "ClassName: 'alignrightpad', filterUI: 't'",
95                                             ),
96                               'artnr' => array(
97                                             'name' => 'Artnr',
98                                             'width' => 60,
99                                             'specs' => "filterUI: 't'",
100                                             ),
101                               'menge' => array(
102                                             'name' => 'Menge',
103                                             'width' => 60,
104                                             'specs' => "ClassName: 'alignrightpad', filterUI: 't'",
105                                             ),
106                               'bezeichnung' => array(
107                                             'name' => 'Bezeichnung',
108                                             'width' => 260,
109                                             'specs' => "filterUI: 't'",
110                                             ),
111                               'hersteller' => array(
112                                             'name' => 'Hersteller',
113                                             'width' => 80,
114                                             'specs' => "filterUI: 't'",
115                                             ),
116                               'vpe' => array(
117                                             'name' => 'Einheit',
118                                             'width' => 70,
119                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
120                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_vpe','id','name').", 0, '')",
121                                             ),
122                               'abteilung' => array(
123                                             'name' => 'Abteilung',
124                                             'width' => 80,
125                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
126                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_abteilung','id','name').", 0, '')",
127                                             ),
128                               'gang' => array(
129                                             'name' => 'Gang',
130                                             'width' => 50,
131                                             'sql' => 'abteilung',
132                                             'specs' => "ClassName: 'aligncenter', filterUI: 's'",
133                                             'control' => "new Rico.TableColumn.lookup(".grid_lookup_sql('metro_abteilung','id','gang').", 0, '')",
134                                             ),
135                               ),
136               'edit' => array(
137                               'buttons' => array(
138                                             'type' => 'html',
139                                             'code' => $buttons,
140                                             'sql' => false,
141                                             ),
142                               ),
143               'callbacks' => array(
144                                    'cellsave' => cb_cellsave,
145                                    'pdf' => cb_pdf,
146                                    'send' => cb_send,
147                                 ),
148               'files' => array(
149                                'download' => process_download,
150                                ),
151               );
152
153 function cb_cellsave()
154 {
155   global $db;
156
157   if ($_POST['value'] == 0)
158     $sql = sprintf("UPDATE metro_ordersatz_pos SET anzahl = NULL, sys_user = %s, sys_edit = now() WHERE ordersatz = %d AND artikel = %d",
159                    $db->quote($_SESSION['sys']['login']),
160                    $_SESSION['ordersatz'], $_POST['id']);
161   else
162     $sql = sprintf("UPDATE metro_ordersatz_pos SET anzahl = %d, sys_user = %s, sys_edit = now() WHERE ordersatz = %d AND artikel = %d",
163                    $_POST['value'], $db->quote($_SESSION['sys']['login']),
164                    $_SESSION['ordersatz'], $_POST['id']);
165
166   $sth = $db->query($sql);
167   error_log($sql);
168 }
169
170
171 function cb_pdf()
172 {
173   $tmpdir = mk_tempdir();
174   $result = ordersatz_write($_SESSION['ordersatz'], $tmpdir, false);
175
176   if ($result !== true)
177     return array('error' => $result);
178
179   return array('tmpdir' => substr($tmpdir,strlen($_SESSION['sys']['basedir'])));
180 }
181
182 function process_download()
183 {
184   if (strpos($_GET['tmpdir'], '..') !== false)
185     return false;
186
187   $fname = $_SESSION['sys']['basedir'] . $_GET['tmpdir'] . '/ordersatz.pdf';
188
189   if (!file_exists($fname) || !is_file($fname))
190     return 'File not found';
191
192   send_file($fname);
193   rm_tempdir($_SESSION['sys']['basedir'] . $_GET['tmpdir']);
194 }
195
196 function cb_send()
197 {
198   $tmpdir = mk_tempdir();
199   $result = ordersatz_write($_SESSION['ordersatz'], $tmpdir, true);
200
201   if ($result !== true)
202     return array('error' => $result);
203
204   $fname = $tmpdir . '/ordersatz.pdf';
205
206   rm_tempdir($tmpdir);
207 }
208
209 ?>