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