Damn, a checkbox returns 'on' for true
[misc/kostenrechnung] / masks / arbeitsberichte.php
index b98b48c..cfd7656 100644 (file)
 
 $jscode[] = <<<EOC
 
+Rico.loadModule('Calendar');
+
+function get_number(name)
+{
+  var elems = name.split(':');
+  if (elems.length == 1) return 0;
+  return Number(elems[elems.length-1]);
+}
+
 function select_gebiet_callback(data)
 {
-    var obj = document.getElementById('edit_kostenstelle');
-    if (!obj) return;
+    select_update('edit_kostenstelle',data.info,1);
+}
+
+function select_gebiet(obj)
+{
+    get_info('kostenstellen', {gebiet: obj.options[obj.selectedIndex].value}, select_gebiet_callback);
+}
+
+function select_geraet_callback(data)
+{
+    var num = get_number(data.parameter.id);
 
-    obj.options.length = 0;
+    var id = 'edit_anbaugeraet';
+    if (num > 0) id += ':' + num;
+
+    var obj = document.getElementById(id);
+
+    if (data.info.length) {
+      if (obj.options.length == 0)
+       obj.options[0] = new Option('Bitte wählen Sie','',false,false);
+      select_update(id,data.info, 1);
+    } else
+      obj.options.length = 0;
 
-    for (var i=0; i < data.info.length; i++)
-      obj.options[i] = new Option(data.info[i].kostenstelle,data.info[i].id,false,false);
 }
 
-function select_group(obj)
+function select_geraet(obj)
 {
-  setvar(obj,'gebiet',false,false);
+    get_info('anbaugeraete',
+            {geraet: obj.options[obj.selectedIndex].value, id: obj.id},
+            select_geraet_callback);
+}
 
-  get_info('kostenstellen', {gebiet: obj.options[obj.selectedIndex].value}, select_gebiet_callback);
+function form_onload()
+{
+    get_info('gebiete', {}, function (data) { select_update('edit_gebiet',data.info, 1); });
+    get_info('ordnungen', {}, function (data) { select_update('edit_ordnung',data.info,1); });
+    get_info('mitarbeiter', {}, function (data) { select_update('edit_personal',data.info, 1); });
+    get_info('geraete', {}, function (data) { select_update('edit_geraet',data.info, 1); });
+    get_info('arbeitsarten', {}, function (data) { select_update('edit_arbeitsart',data.info,1); });
 }
 
-function select_geraet_callback(data)
+function replicate_personal()
 {
-    var obj = document.getElementById('edit_anbaugeraet');
-    if (!obj) return;
+    var elem;
+    var personal = document.getElementById('edit_personal');
+    var stunden = document.getElementById('edit_stunden');
 
-    obj.options.length = 0;
+    var number = get_number(personal.parentNode.children[personal.parentNode.children.length-1].name) + 1;
 
-    for (var i=0; i < data.info.length; i++)
-      obj.options[i] = new Option(data.info[i].anbaugeraet,data.info[i].id,false,false);
+    elem = document.createElement('br');
+    personal.parentNode.appendChild(elem);
+
+    elem = document.createElement('select');
+    elem.id = 'edit_personal:' + number;
+    elem.name = 'personal:' + number;
+    elem.options[0] = new Option('Bitte wählen Sie','',false,false);
+    personal.parentNode.appendChild(elem);
+    get_info('mitarbeiter', {}, function (data) { select_update('edit_personal:'+number,data.info, 1); });
+
+    elem = document.createElement('br');
+    stunden.parentNode.appendChild(elem);
+
+    elem = document.createElement('input');
+    elem.id = 'edit_stunden:' + number;
+    elem.name = 'stunden:' + number;
+    elem.type = 'text';
+    elem.size = 5;
+    stunden.parentNode.appendChild(elem);
+
+    return false;
 }
 
-function select_geraet(obj)
+function replicate_geraete()
+{
+    var elem;
+    var geraet = document.getElementById('edit_geraet');
+    var anbaugeraet = document.getElementById('edit_anbaugeraet');
+    var geraetstunden = document.getElementById('edit_geraetstunden');
+
+    var number = get_number(geraet.parentNode.children[geraet.parentNode.children.length-1].name) + 1;
+
+    elem = document.createElement('br');
+    geraet.parentNode.appendChild(elem);
+
+    elem = document.createElement('select');
+    elem.id = 'edit_geraet:' + number;
+    elem.name = 'geraet:' + number;
+    elem.options[0] = new Option('Bitte wählen Sie','',false,false);
+    elem.onchange = function() { select_geraet(this); };
+    geraet.parentNode.appendChild(elem);
+    get_info('geraete', {}, function (data) { select_update('edit_geraet:'+number,data.info, 1); });
+
+    elem = document.createElement('br');
+    anbaugeraet.parentNode.appendChild(elem);
+
+    elem = document.createElement('select');
+    elem.id = 'edit_anbaugeraet:' + number;
+    elem.name = 'anbaugeraet:' + number;
+    anbaugeraet.parentNode.appendChild(elem);
+
+    elem = document.createElement('br');
+    geraetstunden.parentNode.appendChild(elem);
+
+    elem = document.createElement('input');
+    elem.id = 'edit_geraetstunden:' + number;
+    elem.name = 'geraetstunden:' + number;
+    elem.type = 'text';
+    elem.size = 5;
+    geraetstunden.parentNode.appendChild(elem);
+
+    return false;
+}
+
+function einsatz_check()
+{
+    var fields = {
+       datum: 'Datum',
+       gebiet: 'Gebiet',
+       kostenstelle: 'Gewässer',
+       ordnung: 'Gewässerordnung',
+       arbeitsart: 'Arbeiten',
+       personal: 'Mitarbeiter',
+       stunden: 'Stunden'};
+
+    for (id in fields) {
+       var elem = document.getElementById('edit_' + id);
+
+       if (elem.nodeName.toLowerCase() == 'input') {
+           if (!elem.value.length) {
+               alert('Das Feld ' + fields[id] + ' muß ausgefüllt werden');
+               return false;
+           }
+           continue;
+       }
+
+       if (elem.nodeName.toLowerCase() == 'select') {
+           if (!elem.options[elem.selectedIndex].value.length) {
+               alert('Das Feld ' + fields[id] + ' muß ausgefüllt werden');
+               return false;
+           }
+           continue;
+       }
+    }
+
+    return true;
+}
+
+function einsatz_callback(data)
+{
+    info('Neuer Arbeitsbericht gespeichert');
+    window.location.reload();
+}
+
+function einsatz_submit()
 {
-  setvar(obj,'geraet',false,false);
+    if (!einsatz_check()) return false;
 
-  get_info('anbaugeraete', {geraet: obj.options[obj.selectedIndex].value}, select_geraet_callback);
+    var form = document.getElementById('edit_form');
+    var source = document.getElementById('source');
+    if (!source || !form) return false;
+
+    ajax_request('insert', Form.serialize(form) + '&source=' + source.innerHTML, einsatz_callback);
+
+    return false;
 }
+
+Rico.onLoad(form_onload);
+
+EOC;
+
+$formular = <<<EOC
+<div class="form">
+<form id="edit_form">
+
+<table width="100%" cellspacing="2" border="0">
+<tr>
+ <td width="25%"><label for="edit_datum">Datum</label></td>
+ <td width="25%"><label for="edit_gebiet">Gebiet</label></td>
+ <td width="25%"><label for="edit_kostenstelle">Gewässer</label></td>
+ <td width="25%"><label for="edit_ordnung">Gewässerordnung</label></td>
+</tr>
+<tr>
+ <td width="25%"><input id="edit_datum" name="datum" size="8">&nbsp;<img class="calendar" src="images/icons/calendar.gif" onclick="calendar('edit_datum',event)"></td>
+ <td width="25%"><select id="edit_gebiet" name="gebiet" onchange="select_gebiet(this)"><option value="">Bitte wählen Sie</option></select></td>
+ <td width="25%"><select id="edit_kostenstelle" name="kostenstelle"><option value="">Bitte wählen Sie</option></select></td>
+ <td width="25%"><select id="edit_ordnung" name="ordnung"><option value="">Bitte wählen Sie</option></select></td>
+</tr>
+</table>
+
+<table width="100%" cellspacing="2" border="0" style="border: 1px solid #CCC; border-bottom: none; background: #e5e5e5;">
+<tr>
+ <td width="30%"><label for="edit_personal">Mitarbeiter</label></td>
+ <td width="30%"><label for="edit_stunden">Stunden</label></td>
+ <td width="30%">&nbsp;</td>
+ <td width="10%">&nbsp;</td>
+</tr>
+<tr>
+ <td width="30%"><select id="edit_personal" name="personal"><option value="">Bitte wählen Sie</option></select></td>
+ <td width="30%"><input id="edit_stunden" name="stunden" size="5" type="text"></td>
+  <td width="30%">&nbsp;</td>
+ <td width="10%" align="right"valign="bottom"><button onclick="return replicate_personal()" style="color: red; font-weight: bold;">+&nbsp;</button></td>
+</tr>
+</table>
+
+<table width="100%" cellspacing="2" border="0" style="border: 1px solid #CCC; background: #e5e5e5;">
+<tr>
+ <td width="30%"><label for="edit_geraet">Gerät</label></td>
+ <td width="30%"><label for="edit_anbaugeraet">Anbaugerät</label></td>
+ <td width="30%"><label for="edit_geraetstunden">Stunden</label></td>
+ <td width="10%">&nbsp;</td>
+</tr>
+<tr>
+ <td width="30%"><select id="edit_geraet" name="geraet" onchange="select_geraet(this)"><option value="">Bitte wählen Sie</option></select></td>
+ <td width="30%"><select id="edit_anbaugeraet" name="anbaugeraet"></select></td>
+ <td width="30%"><input id="edit_geraetstunden" name="geraetstunden" size="5" type="text"></td>
+ <td width="10%" align="right"valign="bottom"><button onclick="return replicate_geraete()" style="color: red; font-weight: bold;">+&nbsp;</button></td>
+</tr>
+</table>
+
+<table width="100%" cellspacing="2" border="0" style="margin-top: 5px;">
+<tr>
+ <td width="50%"><label for="edit_bemerkung">Bemerkung</label></td>
+ <td width="25%"><label for="edit_arbeitsart">Arbeiten</label></td>
+ <td width="25%"><label for="edit_akkord">Akkord</label></td>
+</tr>
+<tr>
+ <td width="50%" valign="top"><textarea id="edit_bemerkung" name="bemerkung" cols="66" rows="5"></textarea></td>
+ <td width="25%" valign="top"><select id="edit_arbeitsart" name="arbeitsart"><option value="">Bitte wählen Sie</option></select></td>
+ <td width="25%" valign="top"><input id="edit_akkord" name="akkord" type="checkbox"></td>
+</tr>
+</table>
+
+
+<div align="center" style="margin-bottom: 5px;"><button onclick="return einsatz_submit()">Hinzufügen</button></div>
+</form>
+</div>
 EOC;
 
-function first_gebiet()
+function einsatz_insert($mask)
 {
-  $sql = "SELECT id FROM gebiet ORDER BY gebiet LIMIT 1";
+  $date = explode('.',$_POST['datum']);
+  $date = sprintf('%04d-%02d-%02d', $date[2], $date[1], $date[0]);
+
+  $sql = sprintf("INSERT INTO einsatz (datum,ordnung,gebiet,kostenstelle,arbeiten,akkord,bemerkung,sys_user,sys_edit) " .
+                "VALUES ('%s',%d,%d,%d,%d,%d,%s,'%s',now())",
+                $date,
+                $_POST['ordnung'],
+                $_POST['gebiet'],
+                $_POST['kostenstelle'],
+                $_POST['arbeitsart'],
+                $_POST['akkord']==='on'?1:0,
+                empty($_POST['bemerkung']) ? 'NULL' : "'" . pg_escape_string($_POST['bemerkung']) . "'",
+                pg_escape_string($_SESSION['sys']['login']));
 
   $sth = pg_query($sql);
 
-  if ($sth === false) return 0;
+  if ($sth === false) {
+    error_log($sql . ': ' . pg_last_error());
+    return array('error' => pg_last_error(),
+                 'sql' => $sql);
+  }
 
-  if ($row = pg_fetch_assoc($sth))
-    return $row['id'];
+  $sql = "SELECT currval('einsatz_id_seq')";
+  $sth =  pg_query($sql);
 
-  return 0;
-}
+  if ($sth === false) {
+    error_log($sql . ': ' . pg_last_error());
+    return array('error' => pg_last_error(),
+                 'sql' => $sql);
+  }
+
+  $row = pg_fetch_assoc($sth);
+  $einsatz = $row['currval'];
+
+  $num = 0;$app='';
+  while (true) {
+    if ($num) {
+      $app = ':'.$num;
+      if (empty($_POST['personal'.$app])) break;
+    }
+    $_POST['stunden'.$app] = str_replace(',','.',$_POST['stunden'.$app]);
+    $sql = sprintf("INSERT INTO einsatzpersonal (einsatz,personal,stunden,sys_user,sys_edit) " .
+                  "VALUES (%d,%d,%.2f,'%s',now())",
+                  $einsatz,
+                  $_POST['personal'.$app],
+                  $_POST['stunden'.$app],
+                  pg_escape_string($_SESSION['sys']['login']));
+
+    $sth = pg_query($sql);
+
+    if ($sth === false) {
+      error_log($sql . ': ' . pg_last_error());
+      return array('error' => pg_last_error(),
+                   'sql' => $sql);
+    }
+
+    $num++;
+  }
+
+  $num = 0;$app='';
+  while (true) {
+    if ($num) $app = ':'.$num;
+    if (empty($_POST['geraet'.$app])) break;
+    $_POST['geraetstunden'.$app] = str_replace(',','.',$_POST['geraetstunden'.$app]);
+    $sql = sprintf("INSERT INTO einsatzgeraete (einsatz,geraet,anbaugeraet,stunden,sys_user,sys_edit) " .
+                  "VALUES (%d,%d,%s,%.2f,'%s',now())",
+                  $einsatz,
+                  $_POST['geraet'.$app],
+                  empty($_POST['anbaugeraet'.$app]) ? 'NULL' : intval($_POST['anbaugeraet'.$app]),
+                  $_POST['geraetstunden'.$app],
+                  pg_escape_string($_SESSION['sys']['login']));
+
+    $sth = pg_query($sql);
+
+    if ($sth === false) {
+      error_log($sql . ': ' . pg_last_error());
+      return array('error' => pg_last_error(),
+                   'sql' => $sql);
+    }
 
-if (empty($_SESSION['arbeitsberichte.gebiet'])) $_SESSION['arbeitsberichte.gebiet'] = first_gebiet();
-if (empty($_SESSION['arbeitsberichte.geraet'])) $_SESSION['arbeitsberichte.geraet'] = 0;
+    $num++;
+  }
+
+  return array('status' => true);
+}
 
 $mask = array(
              'table' => 'einsatz',
              'title' => 'Arbeitsberichte',
-             'subtitle' => 'Eingabe und Übersicht',
-             'join' => array(
-                             'einsatzpersonal ON einsatzpersonal.einsatz = einsatz.id',
-                             'personal ON einsatzpersonal.personal = personal.id',
-                             'einsatzgeraete ON einsatzgeraete.einsatz = einsatz.id',
-                             'gebiet ON einsatz.gebiet = gebiet.id',
-                             'gerate ON einsatzgeraete.geraet = gerate.id',
-                             'anbaugeraete ON einsatzgeraete.anbaugeraet = anbaugeraete.id',
-                             'arbeitsarten ON einsatz.arbeiten = arbeitsarten.id',
-                             'kostenstellen ON einsatz.kostenstelle = kostenstellen.id',
-                             ),
-             'where' => "arbeitsarten.arbeitsart <> 'Urlaub' AND arbeitsarten.arbeitsart <> 'Krank' AND arbeitsarten.arbeitsart <> 'Sonstige Fehltage'",
-             'variables' => array(
-                                  'gebiet' => array(),
-                                  'geraet' => array(),
-                                  ),
-             'list' => array(
-                             'id' => array(
-                                           'name' => 'ID',
-                                           'visible' => false,
-                                           'sql' => 'einsatz.id',
-                                           ),
-                             'datum' => array(
-                                           'name' => 'Datum',
-                                           'type' => 'date',
-                                           'width' => 70,
-                                           ),
-                             'kostenstelle' => array(
-                                           'name' => 'Gewässer',
-                                           'sql' => "kostenstellen.kostenstelle",
-                                           'width' => 200,
-                                           'specs' => "filterUI: 's'",
-                                           ),
-                             'gebiet' => array(
-                                           'name' => 'Gebiet',
-                                           'sql' => "gebiet.gebiet",
-                                           'width' => 150,
-                                           'specs' => "filterUI: 's'",
-                                           'visible' => false,
-                                           ),
-                             'name' => array(
-                                           'name' => 'Personal',
-                                           'sql' => "personal.vorname || ' ' || personal.name",
-                                           'width' => 150,
-                                           'specs' => "filterUI: 's'",
-                                           'visible' => false,
-                                           ),
-                             'geraet' => array(
-                                           'name' => 'Gerät',
-                                           'sql' => 'gerate.geraet',
-                                           'width' => 150,
-                                           'specs' => "filterUI: 's'",
-                                           ),
-                             'anbaugeraet' => array(
-                                           'name' => 'Anbaugerät',
-                                           'sql' => 'anbaugeraete.anbaugeraet',
-                                           'width' => 150,
-                                           'specs' => "filterUI: 's'",
-                                           ),
-                             'personalstunden' => array(
-                                           'name' => 'Mitarbeiterstunden',
-                                           'type' => 'number',
-                                           'specs' => "decPlaces: 2, ClassName: 'alignright', suffix: '&nbsp;', canSort: false",
-                                           'sql' => 'einsatzpersonal.stunden',
-                                           'width' => 60,
-                                           'visible' => false,
-                                           ),
-                             'geraetstunden' => array(
-                                           'name' => 'Gerätstunden',
-                                           'type' => 'number',
-                                           'specs' => "decPlaces: 2, ClassName: 'alignright', suffix: '&nbsp;', canSort: false",
-                                           'sql' => 'einsatzgeraete.stunden',
-                                           'width' => 60,
-                                           'visible' => false,
-                                           ),
-                             ),
-             'edit' => array(
-                             'datum' => array(
-                                           'name' => 'Datum',
-                                           'type' => 'date',
-                                           ),
-                             'personal' => array(
-                                           'name' => 'Mitarbeiter',
-                                           'type' => 'select',
-                                           'options' => "SELECT id,vorname || ' ' || name AS text FROM personal ORDER BY text",
-                                           ),
-                             'gebiet' => array(
-                                           'name' => 'Gebiet',
-                                           'type' => 'select',
-                                           'options' => "SELECT id,gebiet AS text FROM gebiet ORDER BY text",
-                                           'selected' => $_SESSION['arbeitsberichte.gebiet'],
-                                           'onchange' => 'select_group(this)',
-                                           ),
-                             'kostenstelle' => array(
-                                           'name' => 'Gewässer',
-                                           'type' => 'select',
-                                           'options' => "SELECT id,kostenstelle AS text FROM kostenstellen WHERE gebiet = " .
-                                                        intval($_SESSION['arbeitsberichte.gebiet']) .
-                                                        " ORDER BY text",
-                                           ),
-                             'geraet' => array(
-                                           'name' => 'Gerät',
-                                           'type' => 'select',
-                                           'options' => "SELECT id,geraet AS text FROM gerate ORDER BY text",
-                                           'selected' => $_SESSION['arbeitsberichte.geraet'],
-                                           'onchange' => 'select_geraet(this)',
-                                           ),
-                             'anbaugeraet' => array(
-                                           'name' => 'Anbaugerät',
-                                           'type' => 'select',
-                                           'options' => "SELECT anbaugeraete.id,anbaugeraet AS text FROM anbaugeraete " . 
-                                                        "JOIN gerate ON anbaugeraete.geraetetyp = gerate.geraetetyp " .
-                                                        "WHERE gerate.id = " . intval($_SESSION['arbeitsberichte.geraet']) . " " .
-                                                        "ORDER BY text",
-                                           ),
-                             'arbeiten' => array(
-                                           'name' => 'Arbeitsart',
-                                           'type' => 'select',
-                                           'options' => "SELECT id,arbeitsart AS text FROM arbeitsarten ORDER BY text",
-                                           ),
-                             'stunden' => array(
-                                           'name' => 'Stunden',
-                                           'type' => 'decimal',
-                                           'size' => 5,
+             'html' => $formular,
+             'insert' => einsatz_insert,
+             'info' => array(
+                             'gebiete' => array(
+                                           'sql' => 'SELECT id,gebiet AS text FROM gebiet ORDER BY text',
                                            ),
-                             'geraetstunden' => array(
-                                           'name' => 'Gerätstunden',
-                                           'type' => 'decimal',
-                                           'size' => 5,
+                             'ordnungen' => array(
+                                           'sql' => 'SELECT id,name AS text FROM ordnungen ORDER BY id',
                                            ),
-                             'ordnung' => array(
-                                           'name' => 'Gewässerordnung',
-                                           'type' => 'select',
-                                           'options' => "SELECT id,name AS text FROM ordnungen ORDER BY text",
+                             'mitarbeiter' => array(
+                                           'sql' => 'SELECT id,name || \', \' || vorname AS text FROM personal WHERE aktiv = 1 ORDER BY text',
                                            ),
-                             'akkord' => array(
-                                           'name' => 'Akkord',
-                                           'type' => 'boolean',
+                             'geraete' => array(
+                                           'sql' => 'SELECT id,geraet AS text FROM gerate ORDER BY text',
                                            ),
-                             'bemerkung' => array(
-                                           'name' => 'Bemerkung',
-                                           'type' => 'textarea',
+                             'arbeitsarten' => array(
+                                           'sql' => 'SELECT id,arbeitsart AS text FROM arbeitsarten ORDER BY text',
                                            ),
-                             ),
-             'info' => array(
                              'kostenstellen' => array(
-                                           'sql' => 'SELECT id,kostenstelle FROM kostenstellen WHERE gebiet = {gebiet}',
+                                           'sql' => 'SELECT id,kostenstelle AS text FROM kostenstellen WHERE gebiet = {gebiet} ORDER BY text',
                                            ),
                              'anbaugeraete' => array(
-                                           'sql' => 'SELECT anbaugeraete.id,anbaugeraet FROM anbaugeraete ' .
+                                           'sql' => 'SELECT anbaugeraete.id,anbaugeraet as text FROM anbaugeraete ' .
                                                     'JOIN gerate ON anbaugeraete.geraetetyp = gerate.geraetetyp ' .
-                                                    'WHERE gerate.id = {geraet}',
+                                                    'WHERE gerate.id = {geraet} ORDER BY text',
                                            ),
                                   ),
              );