Stop editing when clicking on sum row
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / status.wml
index 0c92e24..7212551 100644 (file)
@@ -1,10 +1,13 @@
 #include <infocon.style>
+#include "common.inc"
+
+<future>
+<page func=InfoCon title="Stempeluhr">
+<script type="text/javascript" src="<root_prefix>jquery.editable.js"></script>
 
 <?
-  function min2hour($minutes)
-  {
-      return sprintf('%02d:%02d', $minutes/60, $minutes%60);
-  }
+  session_name('STEMPEL');
+  session_start();
 
   function update_db()
   {
                 $_POST["status"], $_POST["oid_".$idx]);
        $count++;
        $sth = pg_exec ($dbh, $query);
-echo '<br'.$query;
       }
     }
     return sprintf("<br>%d records updated.", $count);
   }
 
+  $stati = false;
   function display_tables()
   {
     global $month;
+    global $cust;
     global $status;
     global $dbh;
+    global $stati;
 
-    $table_head = '<h3 class="bar">Kunde: %s</h3><form action="status.php" method="POST">
-  <table class="font" width="100%%" border=0 cellpadding=2 cellspacing=0>
-  <tr bgcolor=#<colh>>
+    $name = load_customers();
+
+    $table_head = '<h3 class="bar">Kunde: %s</h3><script type="text/javascript">plant(%d,%.2f);</script>
+  <form action="status.php" method="POST">
+  <table class="smallfont border" id="table_%d" width="100%%" cellpadding=0 cellspacing=0>
+  <thead>
+  <tr class="head">
     <th width=15%%>Datum</th>
-    <th width=10%%>Dauer</th>
-    <th width=75%%>Verwendungszweck</th>
-  </tr>';
-    $table_foot = '</table><center>'.
-                 '<input type="radio" name="status" value="0" checked>unbekannt'.
-                 '<input type="radio" name="status" value="1">ignoriert'.
-                 '<input type="radio" name="status" value="2">wartung'.
-                 '<input type="radio" name="status" value="3">berechnet'.
-                 '</center>'.
-                 '<center><input type="submit" value="Aktualisieren"></center>'.
+    <th width=5%%>Dauer</th>
+    <th width=5%%>St.</th>
+    <th width=75%% align=left>Arbeitsbeschreibung</th>
+  </tr>
+  </thead>
+  <tbody>';
+
+    if (!is_array($stati))
+      $stati = find_status();
+
+    $table_foot = '</table><div align="center"><p>';
+
+    foreach ($stati as $k=>$v)
+      $table_foot .= sprintf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, 0==$k?' checked':'', $v);
+    $table_foot .= '<input type="submit" class="button" value="Aktualisieren" onclick="return status_change(event)"></p></div>'.
                  '<input type="hidden" name="fields" value="%d">'.
                  '</form>';
-    $table_row = '<tr bgcolor=#%s><td>%s</td><td align="center">%s</td><td>%s</td></tr>';
 
-    $query = "SELECT oid,start,customer,time,task FROM stempel WHERE time IS NOT NULL ";
-    if (isset($month) && $month !== 'all')
-      $query .= "AND start LIKE '".$month."-%' ";
-    if (isset($status)) {
+    $table_row = '<tr class="t%d" status="%d"><td>%s</td><td align="center" onclick="toggle_checkbox(%d,this)">%s</td>' .
+      '<td align="center" onclick="toggle_checkbox(%d,this)">%s</td><td><span route="Stempel/EditTask" item_id="%d">%s</span></td></tr>';
+    $table_sum = '</tbody><tfoot><tr class="t%d" onclick="edit_task_finish(this)">' .
+       '<td>&nbsp;</td><td align="center">%s</td><td>&nbsp;</td><td>Summe</td></tr></tfoot>';
+
+    $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 ";
+    if (isset($month) && strlen($month))
+      $query .= "AND cast(start AS TEXT) LIKE '".$month."-%' ";
+    if (isset($cust) && strlen($cust))
+      $query .= "AND customer ='".$cust."' ";
+
+    if (isset($status) && strlen($status)) {
       if ($status != 'all')
         $query .= "AND status = $status ";
     } else
@@ -59,27 +80,56 @@ echo '<br'.$query;
 
     $sth = pg_exec ($dbh, $query);
 
+    $form = 0;
     $customer = '';
     while ($row = pg_fetch_array ($sth)) {
 
       if ($customer != $row['customer']) {
-       if (strlen($customer))
+       if (strlen($customer)) {
+         printf($table_sum, $color, min2hour($sum));
          printf($table_foot, $fieldnr);
-       printf($table_head, $row['customer']);
+         $form++;
+       }
+       $cname = $name[$row['customer']]['name'];
+       if (!strlen($cname)) $cname = ucfirst($row['customer']);
+       printf($table_head, $cname, $form, $name[$row['customer']]['rate'], $form);
        $customer = $row['customer'];
        $fieldnr = 0;
        $sum = 0;
-       $color = "<cold>";
+       $color = 0;
       }
 
       $sum += $row['time'];
       $d = explode(' ', $row['start']);
 
-      $check = '<input type="checkbox" name="oid_'.$fieldnr++.'" value="'.$row['oid'].'">';
-      printf($table_row, $color, $check.$d[0], min2hour($row['time']), $row['task']);
-      $color = $color=="<cold>"?"<coln>":"<cold>";
+      $check = sprintf('<input type="checkbox" class="checkbox" name="oid_%d" value="%d" onclick="add_sum(%d,this)">',
+                      $fieldnr++,
+                      $row['oid'],
+                      $form);
+      $date = sprintf('<span onclick="toggle_checkbox(%d,this)">%s</span>', $form, $d[0]);
+      printf($table_row, $color, $row['status'], $check.$date, $form, min2hour($row['time']), $form, $row['kurz'],
+            $row['id'],
+            htmlspecialchars($row['task'], ENT_COMPAT | ENT_HTML401, 'ISO-8859-1'));
+      $color = !$color;
+    }
+    if (pg_num_rows($sth) > 0) {
+      printf($table_sum, $color, min2hour($sum));
+      printf($table_foot, $fieldnr);
     }
-    printf($table_foot, $fieldnr);
+  }
+
+  function find_customers()
+  {
+    global $dbh;
+
+    $a = array();
+    $query = "SELECT DISTINCT customer FROM stempel ORDER BY customer";
+    $sth = pg_exec ($dbh, $query);
+
+    while ($row = pg_fetch_array ($sth))
+      $a[] = $row[0];
+
+    return $a;
   }
 
   $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
@@ -89,19 +139,228 @@ echo '<br'.$query;
 
   if (isset($_POST["status"]) && isset($_POST["fields"])) {
     $msg = update_db();
+    $_SESSION['redirect'] = true;
+    header('Location: status.php');
+    exit;
+  } elseif ($_SESSION['redirect']) {
+    $month = $_SESSION['save']["month"];
+    $cust = $_SESSION['save']["customer"];
+    $status = $_SESSION['save']["status"];
+    $_SESSION['redirect'] = false;
   } else {
-    if (isset($_GET['month']))
+    if (isset($_GET['month'])) {
       $month = $_GET['month'];
-    else
+      $_SESSION['save']['month'] = $month;
+      unset($_SESSION['save']['customer']);
+      unset($_SESSION['save']['status']);
+    } else
       if (isset($_POST["filter"])) {
         $month = $_POST["month"];
+        $cust = $_POST["customer"];
        $status = $_POST["status"];
-      } else
+       $_SESSION['save']['month'] = $month;
+       $_SESSION['save']['customer'] = $cust;
+       $_SESSION['save']['status'] = $status;
+      } else {
         $month = date('Y-m');
+       $_SESSION['save']['month'] = $month;
+       unset($_SESSION['save']['customer']);
+       unset($_SESSION['save']['status']);
+      }
   }
 ?>
 
-<page func=InfoCon title="Stempeluhr">
+<script src="<root_prefix>basics.js"></script>
+<script type="text/javascript">
+<protect>
+var color_checked = '#c4ffc3';
+function format_int(value, width)
+{
+  var s = value.toString();
+
+  while (s.length < width)
+    s = '0' + s;
+
+  return s;
+}
+
+function timestr(duration)
+{
+    return format_int(Math.floor(duration / 60), 1) + ':'
+       + format_int(duration - (Math.floor(duration / 60) * 60), 2);
+}
+
+function strtime(time)
+{
+    var a = time.split(':');
+    return parseInt(a[0],10) * 60 + parseInt(a[1],10);
+}
+
+function check(id, value)
+{
+    var hours = $('#time_'+id);
+    var rate = $('#rate_'+id);
+    var sum = $('#sum_'+id);
+    var newval = 0;
+
+    $('table#table_'+id+' tbody tr:visible').each(function(i,e){
+       var checkbox = $(this).find('input[type="checkbox"]');
+       if (checkbox.prop('checked') != value) {
+           checkbox.prop('checked', !checkbox.prop('checked'));
+           if (checkbox.prop('checked'))
+               $(this).css('background-color', color_checked);
+           else
+               $(this).css('background-color', '');
+           add_sum(id, checkbox);
+       }
+    });
+
+    return false;
+}
+
+function plant(form, rate)
+{
+  document.write('<div class="jscode">');
+  document.write('<input class="filter" id="filter_'+form+'" size="10" value="">');
+  document.write('<img class="filter" id="img_'+form+'" src="/pix/Actions-edit-delete-icon.png" border="0" ' +
+                'title="Filter löschen">');
+  document.write('<input type="hidden" id="rate_'+form+'" value="'+rate+'">');
+  document.write('<a href="#" onclick="return check('+form+',true)">Check all</a>');
+  document.write("&nbsp;/&nbsp;");
+  document.write('<a href="#" onclick="return check('+form+',false)">Uncheck all</a>');
+  document.write("&nbsp;");
+  document.write('<span id="time_'+form+'" class="sum">0:00</span>' );
+  document.write('<span id="sum_'+form+'" class="sum">&euro;0.00</span>');
+  document.write("&nbsp;");
+  document.write('</div>');
+}
+
+function add_sum(form, checkbox)
+{
+    var hours = $('#time_'+form);
+    var rate = $('#rate_'+form);
+    var sum = $('#sum_'+form);
+    var time = strtime($(checkbox).parents('tr:first').find('td:nth-child(2)').text());
+    var newval;
+
+    if ($(checkbox).prop('checked')) {
+       newval = strtime(hours.text()) + time;
+       $(checkbox).parents('tr:first').css('background-color', color_checked);
+    } else {
+       newval = strtime(hours.text()) - time;
+       $(checkbox).parents('tr:first').css('background-color', '');
+    }
+    hours.text(timestr(newval));
+    sum.html('&euro;' + ((newval/60)*rate.val()).toFixed(2));
+}
+
+function toggle_checkbox(form, obj)
+{
+    var row = $(obj).parents('tr:first');
+    var checkbox = row.find('input[type="checkbox"]')
+    checkbox.prop('checked', !checkbox.prop('checked'));
+
+    checkbox.checked = !checkbox.checked;
+    add_sum(form, checkbox);
+}
+
+function filter_change(e)
+{
+    var form = $(this).attr('id').split('_')[1];
+    var filter = $('input#filter_'+form).val();
+
+    var cnum = 0;
+    $('table#table_'+form+' tbody tr').not('.deleted').each(function(i,e){
+       if (filter.length) {
+           var td = $(this).find('td:nth-child(4)');
+           if (td.text().toLowerCase().indexOf(filter) > -1) {
+               $(this).removeClass('t0').removeClass('t1').addClass('t'+cnum).show();
+               cnum = cnum ? 0 : 1;
+           } else {
+               if ($(this).find('input[type="checkbox"]').prop('checked')) {
+                   toggle_checkbox(form, td);
+               }
+               $(this).hide();
+           }
+       } else {
+           $(this).removeClass('t0').removeClass('t1').addClass('t'+cnum).show();
+           cnum = cnum ? 0 : 1;
+       }
+    });
+}
+
+function filter_clear(e)
+{
+    var form = $(this).attr('id').split('_')[1];
+    $('div.jscode input.filter#filter_'+form).val('').keyup();
+}
+
+function status_change(e)
+{
+    var ids = [];
+    var form = $(e.target).parents('form:first');
+    var p = $(e.target).parents('p:first');
+    form.find('tr').not('.deleted').find('input.checkbox').each(function(i,e){
+       if ($(this).prop('checked') == true)
+           ids.push($(this).val());
+    });
+
+    var new_status = 0;
+    p.find('input[name="status"]').each(function(i,e){
+       if ($(this).prop('checked') == true)
+           new_status = $(this).val();
+    });
+
+    $.invoke('Sales/SetStatus', {status: new_status, ids: ids}, function(data){
+       for (var i=0; i < ids.length; i++) {
+           show_message('Positionen aktualisiert');
+           var checkbox = form.find('input.checkbox[value="'+ids[i]+'"]');
+           var row = checkbox.parents('tr:first');
+
+           checkbox.prop('checked',false);
+           add_sum(form.find('table:first').attr('id').split('_')[1], checkbox);
+
+           if (row.attr('status') != new_status)
+               row.addClass('deleted').hide();
+       }
+    });
+
+    return false;
+}
+
+$(function(){
+    $('div.jscode input.filter').keyup(filter_change);
+    $('div.jscode img.filter').click(filter_clear);
+    make_editable('table.smallfont tr span');
+});
+</protect>
+</script>
+
+<style type="text/css">
+div.jscode {
+  text-align: right;
+  margin-top: -13px;
+  font-size: 12px;
+}
+div.jscode input.filter {
+  font-size: 90%;
+  position: relative;
+  bottom: 2px;
+}
+div.jscode img.filter {
+  position: relative;
+  bottom: -3px;
+  margin-left: 1px;
+  margin-right: 10px;
+}
+span.sum {
+  display: inline;
+  border: 1px solid #9b9b9b;
+  background: #d9e2ea;
+  width: 20px;
+  margin-left: 3px;
+}
+</style>
 
 <?
   if (!empty($msg))
@@ -114,28 +373,45 @@ echo '<br'.$query;
 
 <form action=status.php method=POST>
 <select name=month>
-<option value="all">alle</option>
 <?
+  if (!is_array($months))
+    $months = find_months();
+
+  printf('<option value=""%s>alle</option>', !strlen($month)?' selected':'');
   foreach ($months as $m) {
-    printf('<option value="%s">%s</option>', $m, $m);
+    printf('<option value="%s"%s>%s</option>', $m, $month==$m?' selected':'', $m);
+  }
+?>
+</select>
+
+<select name=customer>
+<?
+  if (!is_array($customers))
+    $customers = find_customers();
+
+  printf('<option value=""%s>alle</option>', !strlen($cust)?' selected':'');
+  foreach ($customers as $c) {
+    printf('<option value="%s"%s>%s</option>', $c, $cust==$c?' selected':'', $c);
   }
 ?>
 </select>
 
-<input type="radio" name="status" value="all">alle
-<input type="radio" name="status" value="0">unbekannt
-<input type="radio" name="status" value="1">ignoriert
-<input type="radio" name="status" value="2">wartung
-<input type="radio" name="status" value="3">berechnet
+<?
+  if (!is_array($stati))
+    $stati = find_status();
+
+  printf('<input type="radio" name="status" value="all"%s>alle&nbsp;', $status=='all'?' checked':'');
+  foreach ($stati as $k=>$v)
+    printf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, $status==$k?' checked':'', $v);
+?>
 
 <input type="hidden" name="filter" value="form">
-<input type="submit" value="Display">
+<input class="button" type="submit" value="Display">
 </form>
 
 <? } ?>
 </page>
 
 # Local variables:
-# mode: text
-# mode: auto-fill
+# mode: php
 # end: