Location: PHPKode > projects > TheoPlan > pm/ist.php
<?php
/*
 * Modul:        ist.php
 * Zweck:        Verwaltet die IST-Erfassung, Eingabe in Stunden pro Tag
 * Aufruf:       Durch Menuepunkt "IST-Erfassung" in "forms.php->menu.inc"
 * Beschreibung: Dieses Modul verwaltet die IST-Erfassung in Stunden pro Tag.
 */
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('helper.inc');

$drm = array(true, false, false, false, true, true, true, true);
$headline = 10;

require_once('menu.inc');
require_once('knumber.inc');

$func = $_REQUEST['func'];

# In diesem Modul global verwendete Variablen
$sumparr = array(0,0,0,0,0,0,0,0,0);	// Spaltensummen der Eingabezeilen
$sumiarr = array(0,0,0,0,0,0,0,0,0);
$sumallp = 0.0;		// Spaltensumme der Tasks
$sumalli = 0.0;
$TempExist = false;	// Temporaere Tabelle existiert?

# Folgende Funktion gibt die IST-Daten bzw. Eingabezeilen fuer einen
# Mitarbeiter innerhalb der Tabelle aus.
#
function ShowMitarbeiter($db, $minum, $anz, $von, $bis, $pr_num, $pl_num, $mi=true) {
	global $menu;
	global $headline;
	global $rstufe;
	global $leneinheit;
	global $periode;
	global $dtformatshort;
	global $ist_erledigung;
	global $ist_future;
	global $verrmodul;
	global $sumparr;
	global $sumiarr;
	global $sumallp;
	global $sumalli;
	global $TempExist;

	# Ermitteln ab wann noch erfasst werden darf.
	$query = "select ab_datum from abschluss where ab_prnum = $pr_num ";
	$query .= "and ab_plnum = $pl_num order by ab_datum desc";

	if (!($result = QueryDB($db, $query)))
	   return false;

	if (numrowsDB($result) > 0) {
	   $data = fetchDB($result, 0);
	   $ab_datum = $data[0];
	} else
	   $ab_datum = 0;

	# Projekt oder Konto?
	$query = "select pr_status from project where pr_num = $pr_num";

	if (!($result = QueryDB($db, $query)))
	   return false;

	$data = fetchDB($result, 0);
	$pr_status = $data[0];

	# Auslesen der Tasks welche bebucht werden koennen. Tasks die einem
	# aelteren Plan angehoeren auf dem bereits IST-Buchungen existieren,
	# werden ebenfalls ausgegeben, koennen aber nicht bebucht werden.
	# Nur Plaene mit Status "Aktiv" koennen bebucht werden!
	$akt = $von;

	if ($pr_status == 0) {
	   # Um das zu bekommen was wir wollen, muessen wir mit Hilfe einer
	   # temporaeren Tabelle arbeiten, welche wir nur in einer Schleife
	   # korrekt erstellen koennen.
	   if (!$TempExist) {
	      $query = "create local temporary table TempTask (";
	      $query .= "tmp_num integer not null unique, tmp_tanum integer, ";
	      $query .= "tmp_taname varchar(50), tmp_plan float8, tmp_ist float8,";
	      $query .= "tmp_datum integer, tmp_start integer,tmp_lfd smallint,";
	      $query .= "tmp_duration smallint, tmp_hash char(34), tmp_edit bool,";
	      $query .= "constraint \"temptask_pkey\" primary key (\"tmp_num\"))";
	      $TempExist = true;
	   } else {
	      $query = "delete from TempTask";
	   }

	   if (!($result = QueryDB($db, $query)))
	      return false;

	   $query = "select distinct on (ta_num) ta_name, ta_num, ta_start, ";
	   $query .= "ta_duration, ta_hash from task, allocation ";
	   $query .= "where al_task = ta_num and al_ressource = $minum and ";
	   $query .= "ta_meeting = 0 and ta_level > 1 and ";
	   $query .= "(ta_plnum = $pl_num or ta_plnum in ( ";
	   $query .= "select distinct pl_num from wdone, plan, task where ";
	   $query .= "ta_num = wd_task and pl_num = ta_plnum and ";
	   $query .= "wd_prnum = $pr_num and wd_minum = $minum and ";
	   $query .= "wd_datum between $von and $bis and ";
	   $query .= "pl_status in (2,3,4,5) order by pl_num desc)) and ";

	   if (!$ist_future) {
	      $query .= "(ta_start <= $bis or ";
	   } else {
	      $query .= "(ta_start between $von and $bis or ";
	      $query .= "ta_start+ta_duration*86400 between $von and $bis or ";
	      $query .= "$von between ta_start and ta_start+ta_duration*86400 or ";
	      $query .= "$bis between ta_start and ta_start+ta_duration*86400 or ";
	   }

	   $query .= "ta_num in (select distinct wd_task from wdone where ";
	   $query .= "wd_minum = $minum and wd_prnum = $pr_num and ";
	   $query .= "wd_datum between $von and $bis)) ";
	   $query .= "order by ta_num desc";

	   if (!($result = QueryDB($db, $query)))
	      return false;

	   $menge = numrowsDB($result);
	   $i = 0;
	   $oldhash = "";
	   $counter = 1;

	   while ($i < $menge) {
	      $data = fetchDB($result, $i);
	      $ta_name = $data[0];
	      $ta_num = $data[1];
	      $ta_start = $data[2];
	      $ta_duration = $data[3];
	      $ta_hash = $data[4];

	      # Fuer jeden Tag der angezeigt werden soll, muss ein Datensatz
	      # eingefuegt werden.
	      $akt = $von;
	      $z = 0;

	      while ($z < $anz) {
	         # Nachsehen, ob schon ein Satz existiert. Wenn ja, dann muessen
	         # wir die IST-Werte aufaddieren. Beim Planwert gilt immer der
	         # Neueste.
	         $query = "select tmp_num from TempTask where ";
	         $query .= "tmp_hash = '$ta_hash' and tmp_datum = $akt";

	         if (!($resco = QueryDB($db, $query)))
	            return false;

		 if (numrowsDB($resco) > 0) {
		    $data = fetchDB($resco, 0);
		    $tmp_num = $data[0];
		 } else
		    $tmp_num = 0;

		 $query = "select sum(wd_hours) from wdone where ";
		 $query .= "wd_prnum = $pr_num and wd_minum = $minum and ";
		 $query .= "wd_task = $ta_num and wd_datum = $akt";

		 if (!($ressu = QueryDB($db, $query)))
		    return false;

		 if (numrowsDB($ressu) > 0) {
		    $data = fetchDB($ressu, 0);
		    $wd_hours = $data[0];

		    if (!isset($wd_hours))
		       $wd_hours = 0.0;
		 } else
		    $wd_hours = 0.0;

		 # Ermitteln, ob der Eintrag editierbar ist...
		 $query = "select wd_edit from wdone where ";
		 $query .= "wd_task = $ta_num and wd_minum = $minum and ";
		 $query .= "wd_datum = $akt ";
		 $query .= "order by wd_datum desc";

		 if (!($ressu = QueryDB($db, $query)))
		    return false;

		 if (numrowsDB($ressu) > 0) {
		    $data = fetchDB($ressu, 0);
		    $wd_edit = $data[0];

		    if ($wd_edit == 1 || $wd_edit == 't')
		       $wd_edit = "true";
		    else
		       $wd_edit = "false";
		 } else
		    $wd_edit = "true";

		 # Planstatus ermitteln
		 $query = "select pl_status from plan, task where ";
		 $query .= "pl_num = ta_plnum and ta_num = $ta_num";

		 if (!($ressu = QueryDB($db, $query)))
		   return false;

		 $data = fetchDB($ressu, 0);
		 $wstatus = $data[0];

		 if ($tmp_num == 0) {
		    if ($wstatus == 5) {
		       $wd_edit = "false";
		    }

		    $ta_name = addslashes($ta_name);
		    $query = "insert into TempTask (tmp_num, tmp_tanum, ";
		    $query .= "tmp_taname, tmp_start, tmp_duration, ";
		    $query .= "tmp_hash, tmp_datum, tmp_plan, tmp_ist, tmp_edit) ";
		    $query .= "values ($counter, $ta_num, '$ta_name', $ta_start, ";
		    $query .= "$ta_duration, '$ta_hash', $akt, 0.0, $wd_hours,";
		    $query .= "$wd_edit)";
		    $counter++;
		 } else {
		    $query = "update TempTask set tmp_ist = tmp_ist + $wd_hours ";
		    $query .= "where tmp_num = $tmp_num";
		 }

		if (!QueryDB($db, $query))
		   return false;

		 $akt += 86400;
		 $z++;
	      }

	      $oldhash = $ta_hash;
	      $i++;
	   }

	   $query = "select tmp_taname, tmp_plan, tmp_tanum, tmp_start, ";
	   $query .= "tmp_duration, tmp_hash, tmp_ist, tmp_datum, tmp_edit ";
	   $query .= "from TempTask order by tmp_num";
	} else {
	   $query = "select ta_name, ta_id, ta_num, ta_start, ta_duration, ta_hash from task ";
	   $query .= "where ta_plnum = $pl_num";
	}

	if (!($res = QueryDB($db, $query)))
	   return false;

	$rows = numrowsDB($res);
	$r = 0;
	$oldhash = "";

	while ($r < $rows) {
	   $data = fetchDB($res, $r);
	   $ta_name = $data[0];
	   $plan = $data[1];
	   $ta_num = $data[2];
	   $ta_start = $data[3];
	   $ta_duration = $data[4];
	   $ta_hash = $data[5];

	   if ($pr_status == 0) {
	      $ist = $data[6];
	      $tmp_datum = $data[7];
	      $tmp_edit = $data[8];
	   }

	   # Ermitteln des Planstatus zum aktuellen Task. Ist dieser
	   # "Inaktiv" dann darf nur dann eine Planausgabe erfolgen, wenn
	   # es einen IST-Wert in der entsprechenden Periode gibt.
	   $query = "select pl_status, pl_lfd from plan, task where ";
	   $query .= "pl_num = ta_plnum and ta_num = $ta_num";

	   if (!($resta = QueryDB($db, $query)))
	      return false;

	   $data = fetchDB($resta, 0);
	   $pl_status = $data[0];
	   $pl_lfd = $data[1];

	   # Taskname
	   echo "<tr><td class=\"sel\">$ta_name</td>";

	   # Plannummer und Planstatus
	   switch ($pl_status) {
	      case 0: $sstat = "Konto"; break;
	      case 1: $sstat = "In Erstellung"; break;
	      case 2: $sstat = "Freigegeben"; break;
	      case 4: $sstat = "Aktiv"; break;
	      case 5: $sstat = "Inaktiv"; break;
	      default: $sstat = "$pl_status";
	   }

	   if ($pr_status == 0)
	      echo "<td class=\"sel\">$pl_lfd:&nbsp;$sstat</td>\n";
	   else
	      echo "<td class=\"sel\">&nbsp;</td>\n";

	   # Mitarbeiter oder Projektleiter
	   if ($rstufe != 3 && $mi) {
	      $query = "select mi_nname, mi_vname from mitarbeiter where ";
	      $query .= "mi_num = $minum";

	      if (!($resmi = QueryDB($db, $query)))
	         return false;

	      $data = fetchDB($resmi, 0);

	      if (!$data) {
	         Error("Mitarbeiter konnte nicht ermittelt werden!");
	         return false;
	      }

	      $mi_nname = $data[0];
	      $mi_vname = $data[1];
	      echo "<td class=\"sel\">$mi_nname $mi_vname</td>\n";
	   } else {
	      echo "<td class=\"sel\">&nbsp;</td>\n";
	   }

	   $sumi = 0.0;
	   $sump = 0.0;
	   $z = 0;
	   $akt = $von;

	   while ($z < $anz) {
	      if ($pr_status == 1) {
	         $query = "select wd_hours, wd_edit, wd_task, wd_rk, wd_verr,";
		 $query .= "wd_text, wd_num from wdone, task where ";
	         $query .= "wd_minum = $minum and wd_prnum = $pr_num and ";
	         $query .= "ta_num = wd_task and ta_hash = '$ta_hash' and ";
	         $query .= "wd_datum = $akt";

	         if (!($reswd = QueryDB($db, $query)))
	            return false;

	         if (numrowsDB($reswd) > 0) {
	            $data = fetchDB($reswd, 0);
	            $wd_hours = $data[0];
	            $wd_edit = $data[1];
	            $wd_task = $data[2];
		    $wd_rk = $data[3];
		    $wd_verr = $data[4];
		    $wd_text = $data[5];
		    $wd_num = $data[6];

		    if (CheckTrue($wd_verr))
		       $wd_verr = true;
		    else
		       $wd_verr = false;
	         } else {
		    $wd_num = 0;
	            $wd_hours = 0;
	            $wd_edit = 1;
	            $wd_task = $ta_num;
		    $wd_rk = 0;
		    $wd_verr = false;
		    $wd_text = "";
	         }
	      } else {
	         $query = "select wd_num, wd_rk, wd_verr, wd_text from wdone where ";
	         $query .= "wd_minum = $minum and wd_prnum = $pr_num and ";
	         $query .= "wd_task = $ta_num and wd_datum = $akt";

	         if (!($reswd = QueryDB($db, $query)))
	            return false;

	         if (numrowsDB($reswd) > 0) {
	            $data = fetchDB($reswd, 0);
		    $wd_num = $data[0];
		    $wd_rk = $data[1];
		    $wd_verr = $data[2];
		    $wd_text = $data[3];

		    if (CheckTrue($wd_verr))
		       $wd_verr = true;
		    else
		       $wd_verr = false;
	         } else {
		    $wd_num = 0;
		    $wd_rk = 0;
		    $wd_verr = true;
		    $wd_text = "";
	         }

	         $wd_hours = $ist;
	         $wd_edit = $tmp_edit;
	         $wd_task = $ta_num;
	      }

	      if ($pr_status == 1)
	         $dcomp = true;
	      else {
	         if ($ist_future)
	            $dcomp = ($akt >= $ta_start && $akt <= ($ta_start+$ta_duration*86400)) ? true : false;
	         else
	            $dcomp = ($akt >= $ta_start) ? true : false;
	      }

	      # Heraus finden, ob der aktuelle Task dem neuesten Plan gehoert,
	      # oder nicht. Wenn es sich um einen aelteren Plan handelt
	      # (Status = Inaktiv) dann darf keine IST-Eingabe moeglich sein.
	      if ($dcomp || $pr_status == 1) {
	         $query = "select pl_status from plan, task where ";
	         $query .= "pl_num = ta_plnum and ta_num = $ta_num";

	         if (!($resdc = QueryDB($db, $query)))
	            return false;

		 $data = fetchDB($resdc, 0);
		 $akt_status = $data[0];

		 if ($pl_status < $akt_status) {
		    $dcomp = false;
		    $pplan = true;
		 }
	      } else
	         $pplan = false;

	      if (gmdate("w", $akt) == 6 || gmdate("w", $akt) == 0)
	         $class = "seldkgray";
	      else
	         $class = "sel";

	      if ($akt > $ab_datum && $dcomp && CheckTrue($wd_edit)) {
	         $name = "dt_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $akt;
		 echo "<td class=\"$class\"><table border=0 cellspacing=0 cellpadding=0><tr><td>";
	         printf("<input type=\"text\" name=\"$name\" value=\"%s\" size=6 maxlength=6 onChange=\"javascript:document.getElementById('message').style.visibility = 'visible';\"></td>",
			($wd_hours <= 0) ? "" : FormatNum($wd_hours, 3));

		 if ($verrmodul) {
		    $dr_num = 0;
		    $dr_date_start = "";
		    $dr_time_start = "";
		    $dr_date_end = "";
		    $dr_time_end = "";
		    $dr_target = "";
		    $dr_purpose = "";
		    $dr_pnightr = false;
		    $dr_nightr = 0;
		    $dr_amount = 0;
		    $dr_kmcar = 0;
		    $dr_kfzkenn = "";
		    $dr_kmdrain = 0;
		    $dr_pkmplain = false;
		    $dr_kmplain = 0;
		    $dr_costloc = 0;
		    $dr_whg = $stdwhg;

		    if ($wd_num > 0) {
		       $dtdb = GetDBDateFormat();
		       $query = "select dr_num, to_char(dr_date_start, '$dtdb'),";
		       $query .= "to_char(dr_date_start, 'HH24:MI'), ";
		       $query .= "to_char(dr_date_end, '$dtdb'), to_char(dr_date_end, 'HH24:MI'),";
		       $query .= "dr_target, dr_purpose, dr_pnightr, dr_nightr, dr_amount,";
		       $query .= "dr_kmcar, dr_kfzkenn, dr_kmdrain, dr_pkmplain,";
		       $query .= "dr_kmplain, dr_costloc, dr_whg, dr_remark from dration ";
		       $query .= "where dr_wdnum = $wd_num";

		       if (!($resdr = QueryDB($db, $query)))
		          return false;

		       if (numrowsDB($resdr) > 0) {
		          $data = fetchDB($resdr, 0);
			  $dr_num = $data[0];
			  $dr_date_start = $data[1];
			  $dr_time_start = $data[2];
			  $dr_date_end = $data[3];
			  $dr_time_end = $data[4];
			  $dr_target = $data[5];
			  $dr_purpose = $data[6];
			  $dr_pnightr = $data[7];
			  $dr_nightr = $data[8];
			  $dr_amount = $data[9];
			  $dr_kmcar = $data[10];
			  $dr_kfzkenn = $data[11];
			  $dr_kmdrain = $data[12];
			  $dr_pkmplain = $data[13];
			  $dr_kmplain = $data[14];
			  $dr_costloc = $data[15];
			  $dr_whg = $data[16];
			  $dr_remark = $data[17];
		       }
		    }

		    echo "<td>";
		    ButtonImagePrint("pencil.png", "Ergaenzungen", "verrmist.php",
		 	"menu=$menu&headline=$headline&minum=$minum&pr_num=$pr_num&".
		 	"ta_num=$wd_task&datum=$akt&header=2&func=AddIst&".
			"dr_whg=$dr_whg", 725, 720, true);
		 }

		 echo "</td></tr></table>";

		 if ($verrmodul) {
		    if (!isset($dr_date_start) || strlen($dr_date_start) < 6) {
		       $dr_date_start = date($dtformatshort, $akt);
		       $dr_time_start = "08:00";
		    }

		    if (!isset($dr_date_end) || strlen($dr_date_end) < 6) {
		       $dr_date_end = $dr_date_start;
		       $dr_time_end = "18:00";
		    }

		    $hidden = "vr_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$wd_verr\">\n";
		    $hidden = "rk_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$wd_rk\">\n";
		    $hidden = "am_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_amount\">\n";
		    $hidden = "nu_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_num\">\n";
		    $hidden = "wh_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_whg\">\n";
		    $hidden = "tx_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$wd_text\">\n";
		    $hidden = "ds_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_date_start\">\n";
		    $hidden = "de_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_date_end\">\n";
		    $hidden = "ts_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_time_start\">\n";
		    $hidden = "te_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_time_end\">\n";
		    $hidden = "ta_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_target\">\n";
		    $hidden = "pu_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_purpose\">\n";
		    $hidden = "pn_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_pnightr\">\n";
		    $hidden = "ni_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_nightr\">\n";
		    $hidden = "mc_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_kmcar\">\n";
		    $hidden = "kf_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_kfzkenn\">\n";
		    $hidden = "md_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_kmdrain\">\n";
		    $hidden = "pp_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_pkmplain\">\n";
		    $hidden = "mp_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_kmplain\">\n";
		    $hidden = "cl_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_costloc\">\n";
		    $hidden = "rm_" . $minum . "_" . $pr_num . "_" . $ta_num . "_" . $akt;
		    echo "<input type=\"hidden\" name=\"$hidden\" value=\"$dr_remark\">\n";
		 }

		 echo "</td>\n";
	      }
	      else if ($pplan)
	         printf("<td class=\"$class\" align=\"right\">%s</td>\n", FormatNum($wd_hours, 3));
	      else
	         printf("<td class=\"$class\" align=\"right\">%s</td>\n", ($wd_hours <= 0) ? "" : FormatNum($wd_hours, 3));

	      $akt += 86400;
	      $sumi += $wd_hours;
	      $sumiarr[$z] += $wd_hours;
	      $z++;

	      if ($pr_status == 0 && $z < $anz) {
		 $r++;

		 if ($r >= $rows) {
		    $r--;
		 } else {
		    $data = fetchDB($res, $r);
		    $ta_name = $data[0];
		    $plan = $data[1];
		    $ta_num = $data[2];
		    $ta_start = $data[3];
		    $ta_duration = $data[4];
		    $ta_hash = $data[5];
		    $ist = $data[6];
		    $tmp_datum = $data[7];
		 }
	      }
	   }

	   printf("<td class=\"selakt\" align=\"right\"><b>%s</b></td>\n", FormatNum($sumi, 3));

	   # Um die Summen des gesamten Task anzeigen zu koennen, muessen wir
	   # erst alles summieren. Das gilt jedoch nur fuer Projekte und nicht
	   # fuer Konten!
	   if ($pr_status == 0) {
	      $query = "select sum(al_hours) from allocation where ";
	      $query .= "al_task = $ta_num and al_ressource = $minum";

	      if (!($ressu = QueryDB($db, $query)))
	         return false;

	      $data = fetchDB($ressu, 0);
	      $plan = $data[0];

	      $query = "select sum(wd_hours) / $leneinheit from wdone, task where ";
	      $query .= "ta_num = wd_task and ta_hash = '$ta_hash' and ";
	      $query .= "wd_minum = $minum and wd_prnum = $pr_num";

	      if (!($ressu = QueryDB($db, $query)))
	         return false;

	      $data = fetchDB($ressu, 0);
	      $ist = $data[0];
	   } else {
	      $plan = 0.0;
	      $ist = 0.0;
	   }

	   printf("<td class=\"selnum\"><b>%s</b></td>\n", FormatNum($plan, 3));

	   if (!$ist_erledigung && $ist > $plan && $pr_status == 0) {
	      printf("<td class=\"five\" align=\"right\" valign=\"top\"><b>%s</b></td>\n", FormatNum($ist, 3));
	   } else {
	      printf("<td class=\"selnum\"><b>%s</b></td>\n", FormatNum($ist, 3));
	   }

	   $sumallp += $plan;
	   $sumalli += $ist;

	   if ($ist_erledigung) {
	      if ($plan != 0)
	         $proz = $ist / $plan * 100.0;
	      else
	         $proz = 0.0;

	      if ($proz > 100.0 && $pr_status == 0)
	         printf ("<td class=\"five\" align=\"right\">%s%%</td></tr>\n", FormatNum($proz, 1));
	      else
	         printf ("<td class=\"selnum\">%s%%</td></tr>\n", FormatNum($proz, 1));
	   }

	   $r++;
	}

	return true;
}

# Folgende Funktion verwaltet und visualisiert die Auswahlmoeglichkeiten
# fuer die IST-Erfassung. Fuer die Darstellung und Eingabefelder der
# eigentlichen Isterfassung fuer einen Mitarbeiter, siehe ShowMitarbeiter()
#
function ManageTime() {
	global $phase;
	global $rstufe;
	global $unum;
	global $einheit;
	global $leneinheit;
	global $periode;
	global $ist_erledigung;
	global $ist_future;
	global $menu;
	global $headline;
	global $sumparr;
	global $sumiarr;
	global $sumallp;
	global $sumalli;
	global $dtformatshort;

	$db = OpenDB();
	$nav = "menu=$menu&headline=$headline&project='+";

	if ($rstufe != 3) {
	   $nav .= "document.ist.project.value+'&mitarbeiter='+";
	   $nav .= "document.ist.mitarbeiter.value+'&von='+";
	} else
	   $nav .= "document.ist.project.value+'&von='+";

	$nav .= "document.ist.von.value+'&bis='+document.ist.bis.value+'";

	# Per default waehlen wir alle Projekte (eines Mitarbeiters) aus,
	# alle Mitarbeiter die diesem Projekt zugeordnet sind, sofern es sich
	# um den Projektleiter handelt und die im Zeitraum der aktuellen
	# Periode sind, aus.
	#
	if ($rstufe == 2 || $rstufe == 3) {
	   $query = "select distinct on (pr_num) pr_num, pr_name, pr_pl, mi_nname, mi_vname, pl_num, pl_lfd, pl_status ";
	   $query .= "from project ";
	   $query .= "inner join plan on pl_prnum = pr_num and pl_status in (0,2,3,4) ";
	   $query .= "left join task on ta_plnum = pl_num ";
	   $query .= "left join allocation on ta_num = al_task ";
	   $query .= "left join mitarbeiter on mi_num = pr_pl ";

	   if ($rstufe == 2)
	      $query .= "where pr_pl = $unum or pr_status = 1 or al_ressource = $unum ";
	   else
	      $query .= "where pr_status = 1 or al_ressource = $unum";

//	   $query = "select distinct on (pr_num) pr_num, pr_name, pr_pl, ";
//	   $query .= "mi_nname, mi_vname, pl_num, pl_lfd, pl_status ";
//	   $query .= "from project, plan, mitarbeiter, task, allocation ";
//	   $query .= "where pl_prnum = pr_num and mi_num = pr_pl ";
//	   $query .= "and pl_status in (0, 2, 3, 4, 5) and pl_num = ta_plnum and ";
//	   $query .= "ta_num = al_task and ";

//	   if ($rstufe == 2)
//	      $query .= "(al_ressource = $unum or pr_pl = $unum or pr_status = 1) ";
//	   else
//	      $query .= "(al_ressource = $unum or pr_status = 1)";
	} else {
	   $query = "select distinct on (pr_num) pr_num, pr_name, pr_pl, mi_nname, mi_vname, pl_num, pl_lfd, pl_status ";
	   $query .= "from project ";
	   $query .= "inner join plan on pl_prnum = pr_num and pl_status in (0,2,3,4) ";
	   $query .= "left join task on ta_plnum = pl_num ";
	   $query .= "left join allocation on ta_num = al_task ";
	   $query .= "left join mitarbeiter on mi_num = pr_pl ";

//	   $query = "select distinct on (pr_num) pr_num, pr_name, pr_pl, mi_nname, mi_vname, ";
//	   $query .= "pl_num, pl_lfd, pl_status ";
//	   $query .= "from project, plan, mitarbeiter where ";
//	   $query .= "pl_prnum = pr_num and mi_num = pr_pl and ";
//	   $query .= "pl_status in (0, 2, 3, 4, 5) ";
	}

	$query .= "order by pr_num, pl_lfd desc";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$rows = numrowsDB($result);

	if ($rows <= 0) {
	   Error(GetMessage($db, 238, "Es wurden keine bebuchbaren Projekte gefunden!"));
	   closeDB($db);
	   return;
	}

	# Auswahlfelder fuer die IST-Buchungen anzeigen
	$project = $_REQUEST['project'];
	$mitarbeiter = $_REQUEST['mitarbeiter'];
	$von = $_REQUEST['von'];
	$bis = $_REQUEST['bis'];
	$func = $_REQUEST['func'];

	if (!isset($mitarbeiter))
	   $mitarbeiter = $unum;

	if (isset($von) && isset($bis)) {
	   $dxa = explode(GetDateSep(), $von);
	   $dxb = explode(GetDateSep(), $bis);
	   $dta = gmmktime(0, 0, 0, $dxa[1], $dxa[0], $dxa[2]);
	   $dtb = gmmktime(0, 0, 0, $dxb[1], $dxb[0], $dxb[2]);

	   if ($dta > $dtb) {
	      Error(GetMessage($db, 239, "Das \"Datum von\" darf nicht gr&ouml;&szlig;er als das \"Datum bis\" sein!"));
	      unset($von);
	      unset($bis);
	   } else {
	      if (isset($func) && $func == "PrevWeek") {
	         $dta -= 86400 * 7;
	         $dtb -= 86400 * 7;
	      } else if (isset($func) && $func == "NextWeek") {
	         $dta += 86400 * 7;
		 $dtb += 86400 * 7;
	      }

	      if (isset($func) && $func == "Today") {
	         unset($von);
		 unset($bis);
	      } else {
	         $von = gmdate($dtformatshort, $dta);
	         $bis = gmdate($dtformatshort, $dtb);
	      }
	   }

	   unset($dxa);
	   unset($dxb);
	   unset($dta);
	   unset($dtb);
	}


	echo "<form name=\"ist\" action=\"ist.php\" method=\"post\" onChange=\"javascript:AktDocument=this\">\n";
	echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
	echo "<input type=\"hidden\" name=\"headline\" value=\"$headline\">\n";
	echo "<input type=\"hidden\" name=\"func\" value=\"save\">\n";
	# Hier werden zwei Tabellen ineinander verschachtelt. Die aeussere
	# Tabelle beinhaltet in der linken Spalte die Tabelle zum Setzen
	# der Grundeinstellungen.
	# Die rechte Spalte beinhaltet optional Hinweise fuer den Benutzer,
	# wie z.B. dass Daten veraendert wurden, aber noch nicht gespeichert
	# sind.
	echo "<table width=\"98%\"border=0 cellpadding=0 cellspacing=0><tr><td>";
	# Auswahlmoeglichkeiten in der linken Spalte:
	echo "<table class=\"input\">\n<tr>\n";
	# Projekte anzeigen
	echo "<td>" . GetMessage($db, 200, "Projekte") . "</td>\n";
	echo "<td><select name=\"project\" ";
	echo "onChange=\"javascript:MoveTo('ist.php','$nav')\">\n";
	echo "<option value=\"0\">" . GetMessage($db, 241, "Alle") . "</option>\n";
	$r = 0;
	$flag = false;

	while ($r < $rows) {
	   $data = fetchDB($result, $r);
	   $pr_num = $data[0];
	   $pr_name = $data[1];
	   $plnum = $data[5];
	   $pl_lfd = $data[6];
	   $pl_status = $data[7];

	   if ($pl_status == 6) {
	      $r++;
	      continue;
	   }

	   $part = ($pl_status == 0) ? GetMessage($db, 242, "K:") : GetMessage($db, 243, "P:");

	   if ($pr_num == $project) {
	      echo "<option value=\"$pr_num\" selected>$part $pr_num $pr_name</option>\n";
	      $pl_selnum = $plnum;
	      $flag = true;
	   } else
	      echo "<option value=\"$pr_num\">$part $pr_num $pr_name</option>\n";

	   $r++;
	}

	echo "</select></td>\n";

	if (!$flag)
	   $project = 0;

	# optional Mitarbeiter anzeigen
	if ($rstufe == 1 || $rstufe == 2) {
	   echo "<td>" . GetMessage($db, 66, "Mitarbeiter") . "</td><td><select name=\"mitarbeiter\" ";
	   echo "onChange=\"javascript:MoveTo('ist.php','$nav')\">\n";
	   echo "<option value=\"0\">" . GetMessage($db, 241, "Alle") . "</option>\n";

	   $r = 0;

	   while ($r < $rows) {
	      $data = fetchDB($result, $r);
	      $pl_num[$r] = $data[5];
	      $pl_status[$r] = $data[7];
	      $r++;
	   }

	   if (isset($pl_selnum) && $pl_selnum > 0) {
	      $query = "select pl_status from plan where pl_num = $pl_selnum";

	      if (!($res = QueryDB($db, $query))) {
	         closeDB($db);
	         return;
	      }

	      $data = fetchDB($res, 0);
	      $pstat = $data[0];
	   } else
	      $pstat = -1;

	   if ($pstat != 0) {
	      $query = "select distinct al_ressource, mi_nname,";
	      $query .= "mi_vname from ";
	      $query .= "allocation, task, mitarbeiter where ";
	      $query .= "ta_num = al_task and mi_num = al_ressource and ";
	      $query .= "ta_plnum in (";

	      if (isset($pl_selnum) && $pl_selnum > 0) {
	         $query .= "$pl_selnum";
	      } else {
	         $r = 0;
	         $flag = false;

	         while ($r < $rows) {
	            if ($pl_status[$r] == 6) {
	               $r++;
	               continue;
	            }

	            if ($flag)
	               $query .= "," . $pl_num[$r];
	            else
	               $query .= $pl_num[$r];

		    $flag = true;
	            $r++;
	         }
	      }

	      $query .= ") order by mi_nname, mi_vname";
	   } else {
	      $query = "select mi_num, mi_nname, mi_vname from mitarbeiter ";
	      $query .= "where mi_plan = true order by mi_nname, mi_vname";
	   }

	   if (!($resmi = QueryDB($db, $query))) {
	      closeDB($db);
	      return;
	   }

	   $nr = numrowsDB($resmi);
	   $r = 0;
	   $flag = false;

	   while ($r < $nr) {
	      $data = fetchDB($resmi, $r);
	      $al_ressource = $data[0];
	      $mi_nname = $data[1];
	      $mi_vname = $data[2];

	      if ($al_ressource == $mitarbeiter) {
	         echo "<option value=\"$al_ressource\" selected>$mi_nname $mi_vname</option>\n";
	         $flag = true;
	      } else
	         echo "<option value=\"$al_ressource\">$mi_nname $mi_vname</option>\n";

	      $r++;
	   }

	   echo "</select></td></tr>\n";

	   if (!$flag)
	      $mitarbeiter = 0;
	} else
	   echo "<td colspan=2>&nbsp;</td>\n";

	# Zeitraum von anzeigen
	$vd = GetVisualDate($dtformatshort, false);
	echo "<tr><td>" . GetMessage($db, 373, "Zeitraum von (%s):", $vd) . "</td>\n";

	if (isset($von) && strlen($von) >= 4) {
	   $sd = explode(GetDateSep(), $von);
	   $dat = gmmktime(0, 0, 0, $sd[1], $sd[0], $sd[2]);
	   unset($sd);
	} else {
	   $dat = time() - 86400 * 6;
	}

	$mon = gmdate("n", $dat);
	$year = gmdate("Y", $dat);
	$day = gmdate("j", $dat);

	$dvon = gmmktime(0, 0, 0, $mon, $day, $year);
	echo "<td colspan=3><table border=0 cellpadding=0><tr><td><input type=\"text\" name=\"von\" value=\"";
	$vd = gmdate($dtformatshort, $dvon);
	echo "$vd\" size=10 maxlength=10 onBlur=\"javascript:CheckDate(this,0)\">";

	# Zeitraum bis anzeigen
	if (isset($bis) && strlen($bis) >= 4) {
	   $sd = explode(GetDateSep(), $bis);
	   $dat = gmmktime(0, 0, 0, $sd[1], $sd[0], $sd[2]);
	   unset($sd);
	} else {
	   $dat = time();
	}

	$mon = gmdate("n", $dat);
	$year = gmdate("Y", $dat);
	$day = gmdate("j", $dat);
	$dbis = gmmktime(0, 0, 0, $mon, $day, $year);
	echo " " . GetMessage($db, 246, "Bis:") . " <input type=\"text\" name=\"bis\" value=\"";
	$vd = gmdate($dtformatshort, $dbis);
	echo "$vd\" size=10 maxlength=10 onBlur=\"javascript:CheckDate(this,0)\"></td>\n";
	echo "<td>";
	ButtonImageMove("left.png", GetMessage($db, 374, "Woche zurück"), "ist.php", "func=PrevWeek&$nav");
	echo "</td><td>";
	ButtonImageMove("right.png", GetMessage($db, 375, "Woche vor"), "ist.php", "func=NextWeek&$nav");
	echo "</td><td>";
	ButtonImageMove("today.png", GetMessage($db, 249, "Heute"), "ist.php", "func=Today&$nav");
	echo "</td><td>";
	ButtonLink(GetMessage($db, 250, "Zeitraum setzen"), "ist.php","$nav");
	echo "</td></tr></table></td>";

	if ($rstufe == 1 || $rstufe == 2)
	   echo "<td colspan=2>&nbsp;</td>";

	echo "</tr></table>\n";
	# Ende der linken Spalte.
	echo "</td>\n";
	# Textmitteilungen in der rechten Spalte:
	echo "<td align=\"center\" valign=\"middle\"><div id=\"message\">\n";
	echo "<table border=0 cellspacing=0 cellpadding=0><tr><td>";
	echo "<img src=\"image/exclamation.jpeg\" alt=\"Exclamation mark\" width=30 height=30></td><td>";
	Output ($db, 376, "Bitte speichern Sie die ver&auml;nderten Daten!");
	echo "</td></tr></table>";
	# Ende der rechten Spalte und der Kopftabelle:
	echo "</div></td></tr></table>\n";

	# Aufbau der Tabelle zur IST-Erfassung
	#
	# Tabellenkopf
	echo "<table class=\"sel\"><tr>\n";
	echo "<th class=\"sel\" colspan=3>";
	# Button zum Senden / Rueckgaengig machen
	echo "<table border=0 cellpadding=0 cellspacing=0><tr><td>";
	Button("Speichern", "ist");
	echo "</td><td>";
	ButtonProg(GetMessage($db, 251, "Zur&uuml;cksetzen"), "if (!DocChanged) document.getElementById('message').style.visibility = 'hidden'; document.ist.reset()");
	echo "</td></tr></table>\n";
	echo "</th>\n";

	# Ermitteln des gewuenschten Zeitraums
	$anz = GetAnzEntries($dvon, $dbis);

	if ($anz > 1)
	   echo "<th class=\"sel\" colspan=$anz>";
	else
	   echo "<th class=\"sel\">";

	Output ($db, 377, "Stunden pro PT");
	echo "</th>\n";
	echo "<th class=\"sel\">" . GetMessage($db, 253, "Summe&nbsp;Zeile") . "</th>\n";

	if ($ist_erledigung)
	   echo "<th class=\"sel\" colspan=3>" . GetMessage($db, 254, "Summe&nbsp;(PT)/Task") . "</th></tr>\n";
	else
	   echo "<th class=\"sel\" colspan=2>" . GetMessage($db, 254, "Summe&nbsp;(PT)/Task") . "</th></tr>\n";

	echo "<tr><th class=\"sel\">" . GetMessage($db, 255, "Projekt&nbsp;/&nbsp;Task") . "</th>\n";
	echo "<th class=\"sel\">" . GetMessage($db, 256, "PlNr./Status") . "</th>\n";

	if ($rstufe != 3 && isset($mitarbeiter) && $mitarbeiter == 0)
	   echo "<th class=\"sel\">" . GetMessage($db, 257, "PL&nbsp;/&nbsp;Mitarbeiter") . "</th>\n";
	else
	   echo "<th class=\"sel\">" . GetMessage($db, 258, "Projektleiter") . "</th>\n";

	$r = 0;
	$dt = $dvon;

	while ($r < $anz) {
	   $datum = GetShortDateYear($dt);
	   $dt += 86400;
	   echo "<th class=\"sel\">$datum</th>\n";
	   $r++;
	}

	echo "<th class=\"sel\">" . GetMessage($db, 279, "IST") . "</th>\n";
	echo "<th class=\"sel\">" . GetMessage($db, 259, "Plan") . "</th>\n";
	echo "<th class=\"sel\">" . GetMessage($db, 279, "IST") . "</th>\n";

	if ($ist_erledigung)
	   echo "<th class=\"sel\">" . GetMessage($db, 261, "Erl.") . "</th>\n";

	echo "</tr>\n";
	# Tabelle mit Inhalten fuellen...
	$r = 0;

	while ($r < $rows) {
	   $data = fetchDB($result, $r);
	   $pr_num = $data[0];
	   $pr_name = $data[1];
	   $pr_pl = $data[2];
	   $mi_nname = $data[3];
	   $mi_vname = $data[4];
	   $pl_num = $data[5];
	   $pl_lfd = $data[6];
	   $pl_status = $data[7];

	   # Projekte die einen Plan mit Status = 6 (Abgeschlossen) haben,
	   # ignorieren! Sollten nicht vorkommen!
	   $query = "select count(*) from plan where pl_prnum = $pr_num and pl_status = 6";

	   if (!($resco = QueryDB($db, $query))) {
	      closeDB($db);
	      return;
	   }

	   $data = fetchDB($resco, 0);
	   $menge = $data[0];

	   if ($pl_status == 6 || $menge > 0) {
	      $r++;
	      continue;
	   }

	   # Wenn ein bestimmtes Projekt gewaehlt wurde, nur dieses anzeigen.
	   if (isset($project) && $project > 0 && $pr_num != $project) {
	      $r++;
	      continue;
	   }

	   # Pruefen, ob es fuer das aktuelle Projekt auch Tasks zum Anzeigen
	   # gibt. Falls nicht, untersuchen wir das naechste Projekt.
	   if ($pl_status > 0) {
	      $query = "select count(*) from task, plan, allocation where ";
	      $query .= "pl_num = ta_plnum and al_task = ta_num and ";

	      if (!$ist_future) {
	         $query .= "ta_start <= $dbis and ";
	      } else {
	         $query .= "(ta_start between $dvon and $dbis or ";
	         $query .= "ta_start+ta_duration*86400 between $dvon and $dbis or ";
	         $query .= "$dvon between ta_start and ta_start+ta_duration*86400 or ";
	         $query .= "$dbis between ta_start and ta_start+ta_duration*86400) and ";
	      }

//	      $query .= "ta_plnum = $pl_num and ";
	      $query .= "pl_prnum = $pr_num ";

	      if ($rstufe == 3)
	         $query .= " and al_ressource = $unum";
	      else if (isset($mitarbeiter) && $mitarbeiter > 0)
	         $query .= " and al_ressource = $mitarbeiter";

	      if (!($resta = QueryDB($db, $query))) {
	         closeDB($db);
	         return;
	      }

	      $data = fetchDB($resta, 0);

	      if ($data[0] == 0) {
	         $r++;
	         continue;
	      }
	   }

	   echo "<tr><td class=\"selakt\">$pr_num&nbsp;$pr_name</td>\n";

	   if ($pl_status > 0) {
	      echo "<td class=\"selakt\">" . GetMessage($db, 262, "Projekt") . "</td>\n";
	      echo "<td class=\"selakt\">$mi_nname&nbsp;$mi_vname</td>\n";
	   } else {
	      echo "<td class=\"selakt\"><i>" . GetMessage($db, 234, "Konto") . "</i></td>\n";
	      echo "<td class=\"selakt\">&nbsp;</td>\n";
	   }

	   echo "<td class=\"selakt\"></td>\n";
	   echo "<td class=\"selakt\" colspan=$anz></td>\n";
	   echo "<td class=\"selakt\" colspan=5></td></tr>\n";
	   # Abhaengig von der Rolle des angemeldeten Users, werden alle
	   # Tasks pro Mitarbeiter angezeigt.
	   #
	   if ($rstufe == 3) { 		// Mitarbeiter
	      if (!ShowMitarbeiter($db, $unum, $anz, $dvon, $dbis, $pr_num, $pl_num, false)) {
	         closeDB($db);
	         return;
	      }
	   } else if (isset($mitarbeiter) && $mitarbeiter > 0) {
	      if (!ShowMitarbeiter($db, $mitarbeiter, $anz, $dvon, $dbis, $pr_num, $pl_num, false)) {
		 closeDB($db);
		 return;
	      }
	   } else {
	      if ($pl_status > 0) {
	         $query = "select distinct on (al_ressource) al_ressource, mi_nname, mi_vname, pl_lfd ";
		 $query .= "from allocation ";
		 $query .= "left join mitarbeiter on mi_num = al_ressource ";
		 $query .= "left join task on ta_num = al_task and ";

		 if (!$ist_future) {
		    $query .= "ta_start <= $dbis ";
		 } else {
		    $query .= "(ta_start between $dvon and $dbis or ";
		    $query .= "ta_start+ta_duration*86400 between $dvon and $dbis or ";
		    $query .= "$dvon between ta_start and ta_start+ta_duration*86400 or ";
		    $query .= "$dbis between ta_start and ta_start+ta_duration*86400) ";
		 }

		 $query .= "inner join plan on pl_num = ta_plnum and pl_prnum = $pr_num and pl_status in (2,3,4,5,6) ";
		 $query .= "order by al_ressource, pl_lfd desc, mi_nname, mi_vname";
	      } else {
		 $query = "select mi_num, mi_nname, mi_vname from mitarbeiter ";
		 $query .= "where mi_plan = true order by mi_nname, mi_vname";
	      }

	      if (!($resal = QueryDB($db, $query))) {
		 closeDB($db);
		 return;
	      }

	      $cnt = numrowsDB($resal);
	      $z = 0;

	      while ($z < $cnt) {
	         $data = fetchDB($resal, $z);
	         $al_ressource = $data[0];

	         if (!ShowMitarbeiter($db, $al_ressource, $anz, $dvon, $dbis, $pr_num, $pl_num)) {
	            closeDB($db);
	            return;
	         }

	         $z++;
	      }
	   }

	   $r++;
	}

	# Endsummen schreiben
	echo "<tr><td class=\"selnum\" colspan=3>" . GetMessage($db, 378, "Summen:") . "</td>\n";
	$z = 0;
	$sumitot = 0.0;

	while ($z < $anz) {
	   printf("<td class=\"selltgray\" align=\"right\"><b>%s</b></td>", FormatNum($sumiarr[$z], 3));
	   $sumitot += $sumiarr[$z];
	   $z++;
	}

	printf("<td class=\"selgray\" align=\"right\"><b>%s</b></td>", FormatNum($sumitot, 3));
	printf("<td class=\"selnum\"><b>%s</b></td>", FormatNum($sumallp, 3));
	printf("<td class=\"selnum\"><b>%s</b></td>", FormatNum($sumalli, 3));

	if ($ist_erledigung) {
	   if ($sumallp == 0)
	      printf("<td class=\"selnum\"><b>%s%%</b></td>", FormatNum(0.0, 1));
	   else
	      printf("<td class=\"selnum\"><b>%s%%</b></td>", FormatNum($sumalli / $sumallp * 100.0, 1));
	}

	echo "</tr>\n";
	echo "</table>\n";
	echo "</form>\n";
	closeDB($db);
}

# Folgende Funktion prueft die uebergebenen Parameter und speichert die
# neuen oder veraenderten IST-Zeiten in die Datenbank.
#
function SaveIST() {
	global $unum;
	global $leneinheit;
	global $verrmodul;
	global $stdwhg;

	$kill = false;
	$db = OpenDB();

	if (!($result = QueryDB($db, "select co_wdone from counter"))) {
	   closeDB($db);
	   return false;
	}

	$data = fetchDB($result, 0);
	$co_wdone = $data[0];

	foreach ($_REQUEST as $key => $hours) {
	   $fr = substr($key, 0, 3);

	   if ($verrmodul) {
	      if ($fr != "dt_" && $fr != "vr_" && $fr != "rk_" && $fr != "am_" &&
	          $fr != "nu_" && $fr != "wh_" && $fr != "tx_" && $fr != "ds_" &&
		  $fr != "de_" && $fr != "ts_" && $fr != "te_" && $fr != "ta_" &&
		  $fr != "pu_" && $fr != "pn_" && $fr != "ni_" && $fr != "mc_" &&
		  $fr != "kf_" && $fr != "md_" && $fr != "pp_" && $fr != "mp_" &&
		  $fr != "cl_" && $fr != "rm_")
	         continue;
	   } else {
	      if ($fr != "dt_")
	         continue;
	   }

	   $pars = explode("_", $key);
	   $minum = $pars[1];
	   $pr_num = $pars[2];
	   $wd_task = $pars[3];
	   $dt = $pars[4];

	   if ($verrmodul) {
	      $name = "vr_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $wd_verr = $_REQUEST[$name];
	      $name = "rk_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $wd_rk = $_REQUEST[$name];
	      $name = "am_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_amount = $_REQUEST[$name];
	      $name = "nu_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_num = $_REQUEST[$name];
	      $name = "wh_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_whg = $_REQUEST[$name];
	      $name = "tx_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $wd_text = $_REQUEST[$name];
	      $name = "ds_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_date_start = $_REQUEST[$name];
	      $name = "de_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_date_end = $_REQUEST[$name];
	      $name = "ts_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_time_start = $_REQUEST[$name];
	      $name = "te_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_time_end = $_REQUEST[$name];
	      $name = "ta_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_target = $_REQUEST[$name];
	      $name = "pu_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_purpose = $_REQUEST[$name];
	      $name = "pn_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_pnightr = $_REQUEST[$name];
	      $name = "ni_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_nightr = $_REQUEST[$name];
	      $name = "mc_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_kmcar = $_REQUEST[$name];
	      $name = "kf_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_kfzkenn = $_REQUEST[$name];
	      $name = "md_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_kmdrain = $_REQUEST[$name];
	      $name = "pp_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_pkmplain = $_REQUEST[$name];
	      $name = "mp_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_kmplain = $_REQUEST[$name];
	      $name = "cl_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_costloc = $_REQUEST[$name];
	      $name = "rm_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $dt;
	      $dr_remark = $_REQUEST[$name];

	      if (!isset($dr_amount))
		 $dr_amount = 0.0;
	      else
		 $dr_amount = str_replace(",", ".", $dr_amount);

	      if (!isset($dr_whg) || $dr_whg <= 0)
		 $dr_whg = $stdwhg;

	      if (!isset($dr_nightr) || strlen($dr_nightr) <= 0)
		 $dr_nightr = 0.0;
	      else
		 $dr_nightr = str_replace(",", ".", $dr_nightr);

	      if (!isset($dr_kmdrain) || strlen($dr_kmdrain) == 0 || !ctype_digit($dr_kmdrain))
	         $dr_kmdrain = 0;

	      if (!isset($dr_kmplain) || strlen($dr_kmplain) <= 0)
		 $dr_kmplain = 0.0;
	      else
		 $dr_kmplain = str_replace(",", ".", $dr_kmplain);

	      if (!isset($dr_kmcar) || strlen($dr_kmcar) == 0 || !ctype_digit($dr_kmcar))
	         $dr_kmcar = 0;

	      if (CheckTrue($dr_pnightr))
		 $dr_pnightr = "true";
	      else
		 $dr_pnightr = "false";

	      if (CheckTrue($dr_pkmplain))
		 $dr_pkmplain = "true";
	      else
		 $dr_pkmplain = "false";

	      if (isset($dr_date_start) && strlen($dr_date_start) >= 6)
		 $dr_date_start = DateToDBDate($dr_date_start) . " " . $dr_time_start;
	      else
		 $dr_date_start = "";

	      if (isset($dr_date_end) && strlen($dr_date_end) >= 6)
		 $dr_date_end = DateToDBDate($dr_date_end) . " " . $dr_time_end;
	      else
		 $dr_date_end = "";

	      if (!isset($dr_costloc) || $dr_costloc <= 0)
	         $dr_costloc = "NULL";
	   }

	   if ($fr != "dt_")
	      continue;

	   if (!isset($hours) || $hours == "")
	      $hours = 0.0;
	   else
	      $hours = str_replace(",", ".", $hours);

	   if ($minum <= 0 || $pr_num <= 0 || $wd_task <= 0 || $dt <= 0) {
	      Error("Interner Fehler: Fehlender Parameter!");
	      closeDB($db);
	      return false;
	   }

	   $query = "select ta_phase, ta_hash from task where ta_num = $wd_task";

	   if (!($resta = QueryDB($db, $query))) {
	      closeDB($db);
	      return false;
	   }

	   $data = fetchDB($resta, 0);
	   $ta_phase = $data[0];
	   $ta_hash = $data[1];

	   if (!isset($ta_phase))	// Task eines Kontos?
	      $ta_phase = 0;

/*	   $query = "select wd_num from wdone where ";
	   $query .= "wd_minum = $minum and wd_task = $wd_task and ";
	   $query .= "wd_datum = $dt ";
*/
	   $query = "select wd_num from wdone, task where ";
	   $query .= "wd_minum = $minum and wd_task = ta_num and ";
	   $query .= "wd_datum = $dt and ta_hash = '$ta_hash'";

	   if (!($reswd = QueryDB($db, $query))) {
	      closeDB($db);
	      return false;
	   }

	   $anz = numrowsDB($reswd);

	   if (!QueryDB($db, "begin")) {
	      closeDB($db);
	      return false;
	   }

	   if ($anz > 0) {
	      $data = fetchDB($reswd, 0);
	      $wd_num = $data[0];

	      # Kosten loeschen oder schreiben ...
	      if ($verrmodul) {
	         if ($hours <= 0)
	            $query = "delete from dration where dr_wdnum = $wd_num";
	         else {
	            $query = "select count(*) from dration where dr_wdnum = $wd_num";

		    if (!($resco = QueryDB($db, $query))) {
		       QueryDB($db, "rollback");
		       closeDB($db);
		       return false;
		    }

		    $data = fetchDB($resco, 0);

		    if ($data[0] > 0) {
		       if ($wd_rk == "false") {
		          $query = "update dration set dr_amount = $dr_amount, ";
			  $query .= "dr_whg = $dr_whg, dr_costloc = $dr_costloc ";
		       } else {
		          $query = "update dration set dr_amount = $dr_amount, ";
		          $query .= "dr_whg = $dr_whg, dr_date_start = '$dr_date_start',";
		          $query .= "dr_date_end = '$dr_date_end', dr_target = '$dr_target',";
		          $query .= "dr_purpose = '$dr_purpose', dr_pnightr = $dr_pnightr,";
		          $query .= "dr_kmcar = $dr_kmcar, dr_kfzkenn = '$dr_kfzkenn',";
		          $query .= "dr_kmdrain = $dr_kmdrain, dr_pkmplain = $dr_pkmplain,";
		          $query .= "dr_kmplain = $dr_kmplain, dr_costloc = $dr_costloc,";
		          $query .= "dr_nightr = $dr_nightr, dr_remark = '$dr_remark' ";
		       }

		       $query .= "where dr_wdnum = $wd_num";
		    } else {
		       if ($wd_rk == false) {
		          $query = "insert into dration (dr_num, dr_nightr, ";
			  $query .= "dr_amount, dr_kmcar, dr_kmdrain, ";
			  $query .= "dr_kmplain, dr_wdnum, dr_whg, dr_costloc) ";
			  $query .= "values ($wd_num, 0, $dr_amount, 0, 0, 0,";
			  $query .= "$wd_num, $dr_whg, $dr_costloc)";
		       } else {
			  $dr_target = addslashes($dr_target);
			  $dr_purpose = addslashes($dr_purpose);
			  $dr_remark = addslashes($dr_remark);
		          $query = "insert into dration (dr_num, dr_date_start, ";
		          $query .= "dr_date_end, dr_target, dr_purpose, dr_pnightr,";
		          $query .= "dr_nightr, dr_amount, dr_kmcar, dr_kfzkenn, ";
		          $query .= "dr_kmdrain, dr_pkmplain, dr_kmplain, dr_wdnum,";
		          $query .= "dr_whg, dr_costloc, dr_remark) values ($wd_num, '$dr_date_start',";
		          $query .= "'$dr_date_end', '$dr_target', '$dr_purpose',";
		          $query .= "$dr_pnightr, $dr_nightr, $dr_amount, $dr_kmcar,";
		          $query .= "'$dr_kfzkenn', $dr_kmdrain, $dr_pkmplain, ";
		          $query .= "$dr_kmplain, $wd_num, $dr_whg, $dr_costloc, '$dr_remark')";
		       }
		    }
	         }

	         if (!QueryDB($db, $query)) {
	            QueryDB($db, "rollback");
		    closeDB($db);
		    return false;
	         }
	      }

	      # IST-Daten loeschen oder schreiben...
	      if ($hours <= 0)
		 $query = "delete from wdone where wd_num = $wd_num";
	      else {
		 $query = "update wdone set wd_hours = $hours";

		 if ($verrmodul) {
		    if (!isset($wd_verr) || CheckTrue($wd_verr))
		       $wd_verr = "true";
		    else
		       $wd_verr = "false";

		    if (CheckTrue($wd_rk))
		       $wd_rk = 1;
		    else
		       $wd_rk = 0;

		    $query .= ", wd_verr = $wd_verr, wd_rk = $wd_rk, ";
		    $query .= "wd_text = '$wd_text'";
		 }

		 $query .= " where wd_num = $wd_num";
	      }

	      if (!QueryDB($db, $query)) {
	         QueryDB($db, "rollback");
	         closeDB($db);
	         return false;
	      }

	      QueryDB($db, "commit");
	   } else if ($hours > 0) {
	      $co_wdone++;

	      if ($verrmodul) {
		 if (!isset($wd_verr) || CheckTrue($wd_verr))
		    $wd_verr = "true";
		 else
		    $wd_verr = "false";

		 if (CheckTrue($wd_rk))
		    $wd_rk = 1;
		 else
		    $wd_rk = 0;
	      }

	      $query = "insert into wdone (wd_num, wd_prnum, wd_minum, ";
	      $query .= "wd_datum, wd_hours, wd_task, wd_rk, wd_phase, ";
	      $query .= "wd_edit, wd_verr, wd_text) values ($co_wdone, $pr_num, ";

	      if ($verrmodul) {
	         $query .= "$minum, $dt, $hours, $wd_task, $wd_rk, $ta_phase,";
	         $query .= "true, $wd_verr, '$wd_text')";
	      } else {
	         $query .= "$minum, $dt, $hours, $wd_task, 0, $ta_phase,";
	         $query .= "true, true, NULL)";
	      }

	      if (!QueryDB($db, $query)) {
	         QueryDB($db, "rollback");
		 closeDB($db);
		 return false;
	      }

	      if (!QueryDB($db, "update counter set co_wdone = $co_wdone")) {
		 QueryDB($db, "rollback");
		 closeDB($db);
		 return false;
	      }

	      if ($verrmodul) {
	         if ($wd_rk == "false") {
		    $query = "insert into dration (dr_num, dr_nightr, ";
		    $query .= "dr_amount, dr_kmcar, dr_kmdrain, ";
		    $query .= "dr_kmplain, dr_wdnum, dr_whg, dr_costloc) ";
		    $query .= "values ($co_wdone, 0, $dr_amount, 0, 0, 0,";
		    $query .= "$co_wdone, $dr_whg, $dr_costloc)";
		 } else {
		    $dr_target = addslashes($dr_target);
		    $dr_purpose = addslashes($dr_purpose);
		    $dr_remark = addslashes($dr_remark);
		    $query = "insert into dration (dr_num, dr_date_start, ";
		    $query .= "dr_date_end, dr_target, dr_purpose, dr_pnightr,";
		    $query .= "dr_nightr, dr_amount, dr_kmcar, dr_kfzkenn, ";
		    $query .= "dr_kmdrain, dr_pkmplain, dr_kmplain, dr_wdnum,";
		    $query .= "dr_whg, dr_costloc, dr_remark) values ($co_wdone, '$dr_date_start',";
		    $query .= "'$dr_date_end', '$dr_target', '$dr_purpose',";
		    $query .= "$dr_pnightr, $dr_nightr, $dr_amount, $dr_kmcar,";
		    $query .= "'$dr_kfzkenn', $dr_kmdrain, $dr_pkmplain, ";
		    $query .= "$dr_kmplain, $co_wdone, $dr_whg, $dr_costloc, '$dr_remark')";
		 }

	         if (!QueryDB($db, $query)) {
	            QueryDB($db, "rollback");
		    closeDB($db);
		    return false;
	         }
	      }

	      QueryDB($db, "commit");
	   }
	}

	Journal(302, "ist.php: SaveIST: Projekt: $pr_num, Mitarbeiter: $minum", $db);
	closeDB($db);
	return true;
}

if ($func == "save")
   SaveIST();

ManageTime();

require_once('footer.inc');
?>
Return current item: TheoPlan