822d46e7dd1b6923f087b63b62c7d217e8cbdcb2
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / status.wml
1 #include <infocon.style>
2 #include "common.inc"
3
4 <future>
5 <page func=InfoCon title="Stempeluhr">
6
7 <?
8   session_name('STEMPEL');
9   session_start();
10
11   function update_db()
12   {
13     global $dbh;
14
15     $count = 0;
16     for ($idx=0; $idx < $_POST["fields"]; $idx++) {
17       if (isset($_POST["oid_".$idx])) {
18         $query = sprintf('UPDATE stempel SET status=%d WHERE oid=%d',
19                  $_POST["status"], $_POST["oid_".$idx]);
20         $count++;
21         $sth = pg_exec ($dbh, $query);
22       }
23     }
24     return sprintf("<br>%d records updated.", $count);
25   }
26
27   $stati = false;
28   function display_tables()
29   {
30     global $month;
31     global $cust;
32     global $status;
33     global $dbh;
34     global $stati;
35
36     $name = load_customers();
37
38     $table_head = '<h3 class="bar">Kunde: %s</h3><script type="text/javascript">plant(%d,%.2f);</script>
39   <form action="status.php" method="POST">
40   <table class="smallfont border" id="table_%d" width="100%%" cellpadding=0 cellspacing=0>
41   <thead>
42   <tr class="head" onclick="edit_task_finish(this)">
43     <th width=15%%>Datum</th>
44     <th width=5%%>Dauer</th>
45     <th width=5%%>St.</th>
46     <th width=75%% align=left>Arbeitsbeschreibung</th>
47   </tr>
48   </thead>
49   <tbody>';
50
51     if (!is_array($stati))
52       $stati = find_status();
53
54     $table_foot = '</table><div align="center"><p>';
55
56     foreach ($stati as $k=>$v)
57       $table_foot .= sprintf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, 0==$k?' checked':'', $v);
58     $table_foot .= '<input type="submit" class="button" value="Aktualisieren" onclick="return status_change(event)"></p></div>'.
59                   '<input type="hidden" name="fields" value="%d">'.
60                   '</form>';
61
62     $table_row = '<tr class="t%d" status="%d"><td>%s</td><td align="center" onclick="toggle_checkbox(%d,this)">%s</td>' .
63       '<td align="center" onclick="toggle_checkbox(%d,this)">%s</td><td onclick="edit_task(this)">%s</td></tr>';
64     $table_sum = '</tbody><tfoot><tr class="t%d"><td>&nbsp;</td><td align="center">%s</td><td>&nbsp;</td><td>Summe</td></tr></tfoot>';
65
66     $query = "SELECT stempel.oid,stempel.status,start,customer,time,task,kurz FROM stempel JOIN stempel_status ON (stempel.status = stempel_status.id) WHERE time IS NOT NULL ";
67     if (isset($month) && strlen($month))
68       $query .= "AND cast(start AS TEXT) LIKE '".$month."-%' ";
69     if (isset($cust) && strlen($cust))
70       $query .= "AND customer ='".$cust."' ";
71
72     if (isset($status) && strlen($status)) {
73       if ($status != 'all')
74         $query .= "AND status = $status ";
75     } else
76       $query .= "AND status = 0 ";
77     $query .= "ORDER BY customer,start";
78
79     $sth = pg_exec ($dbh, $query);
80
81     $form = 0;
82     $customer = '';
83     while ($row = pg_fetch_array ($sth)) {
84
85       if ($customer != $row['customer']) {
86         if (strlen($customer)) {
87           printf($table_sum, $color, min2hour($sum));
88           printf($table_foot, $fieldnr);
89           $form++;
90         }
91         $cname = $name[$row['customer']]['name'];
92         if (!strlen($cname)) $cname = ucfirst($row['customer']);
93         printf($table_head, $cname, $form, $name[$row['customer']]['rate'], $form);
94         $customer = $row['customer'];
95         $fieldnr = 0;
96         $sum = 0;
97         $color = 0;
98       }
99
100       $sum += $row['time'];
101       $d = explode(' ', $row['start']);
102
103       $check = sprintf('<input type="checkbox" class="checkbox" name="oid_%d" value="%d" onclick="add_sum(%d,this)">',
104                        $fieldnr++,
105                        $row['oid'],
106                        $form);
107       $date = sprintf('<span onclick="toggle_checkbox(%d,this)">%s</span>', $form, $d[0]);
108       printf($table_row, $color, $row['status'], $check.$date, $form, min2hour($row['time']), $form, $row['kurz'], htmlspecialchars($row['task'], ENT_COMPAT | ENT_HTML401, 'ISO-8859-1'));
109       $color = !$color;
110     }
111     if (pg_num_rows($sth) > 0) {
112       printf($table_sum, $color, min2hour($sum));
113       printf($table_foot, $fieldnr);
114     }
115   }
116
117   function find_customers()
118   {
119     global $dbh;
120
121     $a = array();
122     $query = "SELECT DISTINCT customer FROM stempel ORDER BY customer";
123     $sth = pg_exec ($dbh, $query);
124
125     while ($row = pg_fetch_array ($sth))
126       $a[] = $row[0];
127
128     return $a;
129   }
130
131   $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
132          or die("Unable to connect to SQL server");
133
134   pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
135
136   if (isset($_POST["status"]) && isset($_POST["fields"])) {
137     $msg = update_db();
138     $_SESSION['redirect'] = true;
139     header('Location: status.php');
140     exit;
141   } elseif ($_SESSION['redirect']) {
142     $month = $_SESSION['save']["month"];
143     $cust = $_SESSION['save']["customer"];
144     $status = $_SESSION['save']["status"];
145     $_SESSION['redirect'] = false;
146   } else {
147     if (isset($_GET['month'])) {
148       $month = $_GET['month'];
149       $_SESSION['save']['month'] = $month;
150       unset($_SESSION['save']['customer']);
151       unset($_SESSION['save']['status']);
152     } else
153       if (isset($_POST["filter"])) {
154         $month = $_POST["month"];
155         $cust = $_POST["customer"];
156         $status = $_POST["status"];
157         $_SESSION['save']['month'] = $month;
158         $_SESSION['save']['customer'] = $cust;
159         $_SESSION['save']['status'] = $status;
160       } else {
161         $month = date('Y-m');
162         $_SESSION['save']['month'] = $month;
163         unset($_SESSION['save']['customer']);
164         unset($_SESSION['save']['status']);
165       }
166   }
167 ?>
168
169 <script src="<root_prefix>basics.js"></script>
170 <script type="text/javascript">
171 <protect>
172 var color_checked = '#c4ffc3';
173 function format_int(value, width)
174 {
175   var s = value.toString();
176
177   while (s.length < width)
178     s = '0' + s;
179
180   return s;
181 }
182
183 function timestr(duration)
184 {
185     return format_int(Math.floor(duration / 60), 1) + ':'
186         + format_int(duration - (Math.floor(duration / 60) * 60), 2);
187 }
188
189 function strtime(time)
190 {
191     var a = time.split(':');
192     return parseInt(a[0],10) * 60 + parseInt(a[1],10);
193 }
194
195 function check(id, value)
196 {
197     var hours = $('#time_'+id);
198     var rate = $('#rate_'+id);
199     var sum = $('#sum_'+id);
200     var newval = 0;
201
202     $('table#table_'+id+' tbody tr:visible').each(function(i,e){
203         var checkbox = $(this).find('input[type="checkbox"]');
204         if (checkbox.prop('checked') != value) {
205             checkbox.prop('checked', !checkbox.prop('checked'));
206             if (checkbox.prop('checked'))
207                 $(this).css('background-color', color_checked);
208             else
209                 $(this).css('background-color', '');
210             add_sum(id, checkbox);
211         }
212     });
213
214     return false;
215 }
216
217 function plant(form, rate)
218 {
219   document.write('<div class="jscode">');
220   document.write('<input class="filter" id="filter_'+form+'" size="10" value="">');
221   document.write('<img class="filter" id="img_'+form+'" src="/pix/Actions-edit-delete-icon.png" border="0" ' +
222                  'title="Filter löschen">');
223   document.write('<input type="hidden" id="rate_'+form+'" value="'+rate+'">');
224   document.write('<a href="#" onclick="return check('+form+',true)">Check all</a>');
225   document.write("&nbsp;/&nbsp;");
226   document.write('<a href="#" onclick="return check('+form+',false)">Uncheck all</a>');
227   document.write("&nbsp;");
228   document.write('<span id="time_'+form+'" class="sum">0:00</span>' );
229   document.write('<span id="sum_'+form+'" class="sum">&euro;0.00</span>');
230   document.write("&nbsp;");
231   document.write('</div>');
232 }
233
234 function add_sum(form, checkbox)
235 {
236     var hours = $('#time_'+form);
237     var rate = $('#rate_'+form);
238     var sum = $('#sum_'+form);
239     var time = strtime($(checkbox).parents('tr:first').find('td:nth-child(2)').text());
240     var newval;
241
242     if ($(checkbox).prop('checked')) {
243         newval = strtime(hours.text()) + time;
244         $(checkbox).parents('tr:first').css('background-color', color_checked);
245     } else {
246         newval = strtime(hours.text()) - time;
247         $(checkbox).parents('tr:first').css('background-color', '');
248     }
249     hours.text(timestr(newval));
250     sum.html('&euro;' + ((newval/60)*rate.val()).toFixed(2));
251 }
252
253 var task_parent = null;
254 var task_title = '';
255 var task_oid = 0;
256 function edit_task(obj)
257 {
258     if (task_oid) {
259         if (task_oid != obj.parentNode.children[0].children[0].value)
260             edit_task_save();
261     }
262
263     if (!task_oid) {
264         task_parent = obj;
265         task_title = obj.innerHTML;
266         task_oid = obj.parentNode.children[0].children[0].value;
267
268         var input = $('<input>');
269         input.val(obj.innerHTML.replace('&gt;', '>').replace('&lt;', '<').replace('&amp;', '&'));
270         input.css('fontSize', '100%').css('width', '100%').css('background', 'yellow');
271         obj.innerHTML = '';
272         $(obj).append(input);
273         input.focus();
274     }
275 }
276
277 function edit_task_save()
278 {
279     if (!task_parent) return;
280
281     if (task_parent.children[0].value != task_title)
282         $.invoke('Sales/Text', {id: task_oid, text: task_parent.children[0].value});
283
284     task_parent.innerHTML = task_parent.children[0].value;
285     task_parent = null;
286     task_title = '';
287     task_oid = 0;
288 }
289
290 function edit_task_finish(obj)
291 {
292     if (task_oid)
293         edit_task_save();
294 }
295
296 function toggle_checkbox(form, obj)
297 {
298     edit_task_finish(obj);
299
300     var row = $(obj).parents('tr:first');
301     var checkbox = row.find('input[type="checkbox"]')
302     checkbox.prop('checked', !checkbox.prop('checked'));
303
304     checkbox.checked = !checkbox.checked;
305     add_sum(form, checkbox);
306 }
307
308 function filter_change(e)
309 {
310     var form = $(this).attr('id').split('_')[1];
311     var filter = $('input#filter_'+form).val();
312
313     var cnum = 0;
314     $('table#table_'+form+' tbody tr').not('.deleted').each(function(i,e){
315         if (filter.length) {
316             var td = $(this).find('td:nth-child(4)');
317             if (td.text().toLowerCase().indexOf(filter) > -1) {
318                 $(this).removeClass('t0').removeClass('t1').addClass('t'+cnum).show();
319                 cnum = cnum ? 0 : 1;
320             } else {
321                 if ($(this).find('input[type="checkbox"]').prop('checked')) {
322                     toggle_checkbox(form, td);
323                 }
324                 $(this).hide();
325             }
326         } else {
327             $(this).removeClass('t0').removeClass('t1').addClass('t'+cnum).show();
328             cnum = cnum ? 0 : 1;
329         }
330     });
331 }
332
333 function filter_clear(e)
334 {
335     var form = $(this).attr('id').split('_')[1];
336     $('div.jscode input.filter#filter_'+form).val('').keyup();
337 }
338
339 function status_change(e)
340 {
341     var ids = [];
342     var form = $(e.target).parents('form:first');
343     var p = $(e.target).parents('p:first');
344     form.find('tr').not('.deleted').find('input.checkbox').each(function(i,e){
345         if ($(this).prop('checked') == true)
346             ids.push($(this).val());
347     });
348
349     var new_status = 0;
350     p.find('input[name="status"]').each(function(i,e){
351         if ($(this).prop('checked') == true)
352             new_status = $(this).val();
353     });
354
355     $.invoke('Sales/SetStatus', {status: new_status, ids: ids}, function(data){
356         for (var i=0; i < ids.length; i++) {
357             show_message('Positionen aktualisiert');
358             var checkbox = form.find('input.checkbox[value="'+ids[i]+'"]');
359             var row = checkbox.parents('tr:first');
360
361             checkbox.prop('checked',false);
362             add_sum(form.find('table:first').attr('id').split('_')[1], checkbox);
363
364             if (row.attr('status') != new_status)
365                 row.addClass('deleted').hide();
366         }
367     });
368
369     return false;
370 }
371
372 $(function(){
373     $('div.jscode input.filter').keyup(filter_change);
374     $('div.jscode img.filter').click(filter_clear);
375 });
376 </protect>
377 </script>
378
379 <style type="text/css">
380 div.jscode {
381   text-align: right;
382   margin-top: -13px;
383   font-size: 12px;
384 }
385 div.jscode input.filter {
386   font-size: 90%;
387   position: relative;
388   bottom: 2px;
389 }
390 div.jscode img.filter {
391   position: relative;
392   bottom: -3px;
393   margin-left: 1px;
394   margin-right: 10px;
395 }
396 span.sum {
397   display: inline;
398   border: 1px solid #9b9b9b;
399   background: #d9e2ea;
400   width: 20px;
401   margin-left: 3px;
402 }
403 </style>
404
405 <?
406   if (!empty($msg))
407     echo $msg;
408   else {
409     display_tables();
410 ?>
411
412 <h3 class=bar>Display</h3>
413
414 <form action=status.php method=POST>
415 <select name=month>
416 <?
417   if (!is_array($months))
418     $months = find_months();
419
420   printf('<option value=""%s>alle</option>', !strlen($month)?' selected':'');
421   foreach ($months as $m) {
422     printf('<option value="%s"%s>%s</option>', $m, $month==$m?' selected':'', $m);
423   }
424 ?>
425 </select>
426
427 <select name=customer>
428 <?
429   if (!is_array($customers))
430     $customers = find_customers();
431
432   printf('<option value=""%s>alle</option>', !strlen($cust)?' selected':'');
433   foreach ($customers as $c) {
434     printf('<option value="%s"%s>%s</option>', $c, $cust==$c?' selected':'', $c);
435   }
436 ?>
437 </select>
438
439 <?
440   if (!is_array($stati))
441     $stati = find_status();
442
443   printf('<input type="radio" name="status" value="all"%s>alle&nbsp;', $status=='all'?' checked':'');
444   foreach ($stati as $k=>$v)
445     printf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, $status==$k?' checked':'', $v);
446 ?>
447
448 <input type="hidden" name="filter" value="form">
449 <input class="button" type="submit" value="Display">
450 </form>
451
452 <? } ?>
453 </page>
454
455 # Local variables:
456 # mode: php
457 # end: