Summarise time
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / status.wml
1 #include <infocon.style>
2 #include "common.inc"
3
4 <script type="text/javascript">
5 function check(id, value)
6 {
7   var form = document.forms[id];
8
9   for (i=0; i< form.elements.length; i++) {
10     if (form.elements[i].name.substr(0,4) == 'oid_')
11       form.elements[i].checked = value;
12   }
13
14   if (value == false) {
15       var sum = document.getElementById('sum_'+id.toString()); 
16       sum.innerHTML = '0';
17   }
18 }
19
20 function plant(form)
21 {
22   document.write('<div class="jscode">');
23   document.write('<a href="javascript:check('+form+',true)">Check all</a>');
24   document.write("&nbsp;/&nbsp;");
25   document.write('<a href="javascript:check('+form+',false)">Uncheck all</a>');
26   document.write("&nbsp;");
27   document.write('<span id="sum_'+form+'" class="sum">0</span>');
28   document.write("&nbsp;");
29   document.write('</div>');
30 }
31
32 var sum = array();
33 function add_sum(form, checkbox)
34 {
35     var sum = document.getElementById('sum_'+form.toString()); 
36     var newval;
37
38     var ta = checkbox.parentNode.nextSibling.innerHTML.split(':');
39     var time = parseInt(ta[0]) * 60 + parseInt(ta[1]);
40
41     if (checkbox.checked)
42         newval = parseInt(sum.innerHTML) + time;
43     else
44         newval = parseInt(sum.innerHTML) - time;
45     sum.innerHTML = newval.toString();
46 }
47 </script>
48
49 <style type="text/css">
50 div.jscode {
51   text-align: right;
52   margin-top: -13px;
53   font-size: 12px;
54 }
55 span.sum {
56   display: inline;
57   border: 1px solid #9b9b9b;
58   background: #d9e2ea;
59   width: 20px;
60 }
61 </style>
62
63 <?
64   function update_db()
65   {
66     global $dbh;
67
68     $count = 0;
69     for ($idx=0; $idx < $_POST["fields"]; $idx++) {
70       if (isset($_POST["oid_".$idx])) {
71         $query = sprintf('UPDATE stempel SET status=%d WHERE oid=%d',
72                  $_POST["status"], $_POST["oid_".$idx]);
73         $count++;
74         $sth = pg_exec ($dbh, $query);
75       }
76     }
77     return sprintf("<br>%d records updated.", $count);
78   }
79
80   $stati = false;
81   function display_tables()
82   {
83     global $month;
84     global $cust;
85     global $status;
86     global $dbh;
87     global $stati;
88
89     $table_head = '<h3 class="bar">Kunde: %s</h3><script type="text/javascript">plant(%d);</script>
90   <form action="status.php" method="POST">
91   <table class="smallfont border" width="100%%" cellpadding=0 cellspacing=0>
92   <tr class="head">
93     <th width=15%%>Datum</th>
94     <th width=5%%>Dauer</th>
95     <th width=5%%>St.</th>
96     <th width=75%% align=left>Verwendungszweck</th>
97   </tr>';
98
99     if (!is_array($stati))
100       $stati = find_status();
101
102     $table_foot = '</table><div align="center"><p>';
103
104     foreach ($stati as $k=>$v)
105       $table_foot .= sprintf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, 0==$k?' checked':'', $v);
106       $table_foot .= '<input type="submit" class="button" value="Aktualisieren"></p></div>'.
107                   '<input type="hidden" name="fields" value="%d">'.
108                   '</form>';
109
110     $table_row = '<tr class=t%d><td>%s</td><td align="center">%s</td><td align="center">%s</td><td>%s</td></tr>';
111     $table_sum = '<tr class="t%d"><td>&nbsp;</td><td align="center">%s</td><td>&nbsp;</td><td>Summe</td></tr>';
112
113     $query = "SELECT stempel.oid,start,customer,time,task,kurz FROM stempel JOIN stempel_status ON (stempel.status = stempel_status.id) WHERE time IS NOT NULL ";
114     if (isset($month) && strlen($month))
115       $query .= "AND cast(start AS TEXT) LIKE '".$month."-%' ";
116     if (isset($cust) && strlen($cust))
117       $query .= "AND customer ='".$cust."' ";
118
119     if (isset($status)) {
120       if ($status != 'all')
121         $query .= "AND status = $status ";
122     } else
123       $query .= "AND status = 0 ";
124     $query .= "ORDER BY customer,start";
125
126     $sth = pg_exec ($dbh, $query);
127
128     $form = 0;
129     $customer = '';
130     while ($row = pg_fetch_array ($sth)) {
131
132       if ($customer != $row['customer']) {
133         if (strlen($customer)) {
134           printf($table_sum, $color, min2hour($sum));
135           printf($table_foot, $fieldnr);
136           $form++;
137         }
138         printf($table_head, ucfirst($row['customer']), $form);
139         $customer = $row['customer'];
140         $fieldnr = 0;
141         $sum = 0;
142         $color = 0;
143       }
144
145       $sum += $row['time'];
146       $d = explode(' ', $row['start']);
147
148       $check = sprintf('<input type="checkbox" class="checkbox" name="oid_'.$fieldnr++.'" value="'.$row['oid'].'" onclick="add_sum(%d,this)">',$form);
149       printf($table_row, $color, $check.$d[0], min2hour($row['time']), $row['kurz'], $row['task']);
150       $color = !$color;
151     }
152     if (pg_num_rows($sth) > 0) {
153       printf($table_sum, $color, min2hour($sum));
154       printf($table_foot, $fieldnr);
155     }
156   }
157
158   function find_customers()
159   {
160     global $dbh;
161
162     $a = array();
163     $query = "SELECT DISTINCT customer FROM stempel ORDER BY customer";
164     $sth = pg_exec ($dbh, $query);
165
166     while ($row = pg_fetch_array ($sth))
167       $a[] = $row[0];
168
169     return $a;
170   }
171
172   $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
173          or die("Unable to connect to SQL server");
174
175   pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
176
177   if (isset($_POST["status"]) && isset($_POST["fields"])) {
178     $msg = update_db();
179   } else {
180     if (isset($_GET['month']))
181       $month = $_GET['month'];
182     else
183       if (isset($_POST["filter"])) {
184         $month = $_POST["month"];
185         $cust = $_POST["customer"];
186         $status = $_POST["status"];
187       } else
188         $month = date('Y-m');
189   }
190 ?>
191
192 <page func=InfoCon title="Stempeluhr">
193
194 <?
195   if (!empty($msg))
196     echo $msg;
197   else {
198     display_tables();
199 ?>
200
201 <h3 class=bar>Display</h3>
202
203 <form action=status.php method=POST>
204 <select name=month>
205 <?
206   if (!is_array($months))
207     $months = find_months();
208
209   printf('<option value=""%s>alle</option>', !strlen($_POST['month'])?' selected':'');
210   foreach ($months as $m) {
211     printf('<option value="%s"%s>%s</option>', $m, $_POST['month']==$m?' selected':'', $m);
212   }
213 ?>
214 </select>
215
216 <select name=customer>
217 <?
218   if (!is_array($customers))
219     $customers = find_customers();
220
221   printf('<option value=""%s>alle</option>', !strlen($_POST['month'])?' selected':'');
222   foreach ($customers as $c) {
223     printf('<option value="%s"%s>%s</option>', $c, $_POST['customer']==$c?' selected':'', $c);
224   }
225 ?>
226 </select>
227
228 <?
229   if (!is_array($stati))
230     $stati = find_status();
231
232   printf('<input type="radio" name="status" value="all"%s>alle&nbsp;', $status=='all'?' checked':'');
233   foreach ($stati as $k=>$v)
234     printf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, $status==$k?' checked':'', $v);
235 ?>
236
237 <input type="hidden" name="filter" value="form">
238 <input class="button" type="submit" value="Display">
239 </form>
240
241 <? } ?>
242 </page>
243
244 # Local variables:
245 # mode: text
246 # mode: auto-fill
247 # end: