Subversion Repositories public

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/*
 * Modul:        istpm.php
 * Zweck:        Verwaltet die IST-Erfassung, Eingabe in Monaten
 * Aufruf:       Durch Menuepunkt "IST-Erfassung" in "forms.php->menu.inc"
 * Beschreibung: Dieses Modul verwaltet die IST-Erfassung in ganzen Monaten.
 *               Es koennen also nur PT's fuer jeweils 1 Monat eingegeben
 *               werden. Dabei werden diese Eingaben intern so gespeichert,
 *               dass die PT's auf die Tage des Monats aufgeteilt werden,
 *               so das es scheint als waeren die Eingaben pro Tag in Stunden
 *               erfolgt.
 */
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');

/*
0: Verwalter
1: Administrator
2: Projektleiter
3: Mitarbeiter
4: Kontroller
5: Kein Zugang!
6: Ressourcenmanager
7: Revisor
*/
$drm = array(true, false, false, false, false, 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 $unum;
        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 .= "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_periode 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,6) order by pl_num desc)) and ";

           if (!$ist_future) {
              $query .= "ta_start <= $bis ";
           } 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 jede Periode die 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_periode = $akt";

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

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

                 $mon = gmdate("n", $akt);
                 $year = gmdate("Y", $akt);
                 $per_bis = gmmktime(0, 0, 0, $mon, daysinmonth($mon, $year), $year);

                 if ($oldhash != $ta_hash) {
                    $query = "select sum(al_hours) from allocation where ";
                    $query .= "al_task = $ta_num and al_ressource = $minum and ";
                    $query .= "al_pstart between $akt and $per_bis";

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

                    if (numrowsDB($ressu) > 0) {
                       $data = fetchDB($ressu, 0);
                       $al_hours = $data[0];
                    } else
                       $al_hours = 0.0;

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

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

                 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...
                 # Da wir hier ein ganzes Monat bewerten, koennen wir nicht
                 # anhand des Kennzeichens in wdone (wd_edit) ermitteln, ob
                 # ein Eintrag editierbar ist oder nicht. Statt dessen muessen
                 # wir das anhand des letzten Abschlussdatums feststellen.
                 #
                 if ($akt > $ab_datum)
                    $wd_edit = "true";
                 else
                    $wd_edit = "false";

                 # 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 || $wstatus == 6) {
                       $wd_edit = "false";

                       if ($wstatus == 5 && $wd_hours <= 0.0)
                          $al_hours = 0.0;
                    }

                    $slash_name = addslashes($ta_name);
                    $query = "insert into TempTask (tmp_num, tmp_tanum, ";
                    $query .= "tmp_taname, tmp_start, tmp_duration, ";
                    $query .= "tmp_hash, tmp_periode, tmp_plan, tmp_ist, tmp_edit) ";
                    $query .= "values ($counter, $ta_num, '$slash_name', $ta_start, ";
                    $query .= "$ta_duration, '$ta_hash', $akt, $al_hours, $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 = IncDate($periode, $akt);
                 $z++;
              }

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

           $query = "select tmp_taname, tmp_plan, tmp_tanum, tmp_start, ";
           $query .= "tmp_duration, tmp_hash, tmp_ist, tmp_periode, 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_periode = $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 = GetMessage($db, 234, "Konto"); break;
              case 1: $sstat = GetMessage($db, 85, "In Erstellung"); break;
              case 2: $sstat = GetMessage($db, 86, "Freigegeben"); break;
              case 4: $sstat = GetMessage($db, 87, "Aktiv"); break;
              case 5: $sstat = GetMessage($db, 88, "Inaktiv"); break;
              case 6: $sstat = GetMessage($db, 89, "Abgeschlossen"); break;
              default: $sstat = "$pl_status";
           }

           if ($pr_status == 0 && $ab_datum > 0)
              $sstat .= "<br>" . GetMessage($db, 392, "Abg. bis: %s", gmdate($dtformatshort, $ab_datum));
           else if ($ab_datum > 0)
              $sstat = GetMessage($db, 392, "Abg. bis: %s", gmdate($dtformatshort, $ab_datum));

           if ($pr_status == 0)
              echo "<td class=\"sel\">$pl_lfd: $sstat</td>\n";
           else
              echo "<td class=\"sel\">$sstat</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(GetMessage($db, 235, "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";

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

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

              $data = fetchDB($reser, 0);

              if (!$data) {
                 Error(GetMessage($db, 236, "Erledigungsgrad konnte nicht ermittelt werden!"));
                 return false;
              }

              $ist = $data[0];

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

           echo "<td class=\"sel\">" . GetMessage($db, 237, "Plan:<br>IST:") . "</td>\n";
           $sumi = 0.0;
           $sump = 0.0;
           $z = 0;
           $akt = $von;

           while ($z < $anz) {
              $mon = gmdate("n", $akt);
              $year = gmdate("Y", $akt);
              $dstart = gmmktime(0, 0, 0, $mon, 1, $year);
              $dend = gmmktime(0, 0, 0, $mon, daysinmonth($mon, $year), $year);

              if ($pr_status == 1) {
                 $query = "select sum(wd_hours)/$leneinheit, wd_task from wdone, task ";
                 $query .= "where wd_minum = $minum and ta_num = wd_task and ";
                 $query .= "ta_hash = '$ta_hash' and wd_prnum = $pr_num and ";
                 $query .= "wd_datum >= $dstart and wd_datum <= $dend ";
                 $query .= "group by wd_task";

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

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

                    if ($akt > $ab_datum)
                       $wd_edit = true;
                    else
                       $wd_edit = false;

                    $query = "select wd_num, wd_rk, wd_verr, wd_text from ";
                    $query .= "wdone, task where wd_minum = $minum and ";
                    $query .= "wd_prnum = $pr_num and ta_num = wd_task and ";
                    $query .= "ta_hash = '$ta_hash' and wd_datum between $dstart and $dend ";
                    $query .= "order by wd_datum";

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

                    $data = fetchDB($resdr, 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_hours = 0;
                    $wd_edit = 1;
                    $wd_task = $ta_num;
                    $wd_num = 0;
                    $wd_rk = 0;
                    $wd_verr = true;
                    $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 between $dstart and $dend ";
                 $query .= "order by wd_datum";

                 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 == 0) {
                 $tmon = gmdate("n", $ta_start);
                 $tyear = gmdate("Y", $ta_start);
                 $td = gmmktime(0, 0, 0, $tmon, 1, $tyear);
              } else
                 $td = 0;

              if ($ist_future) {
                 if ($pr_status == 0) {
                    $ta_end = $ta_start + $ta_duration * 86400;
                    $temon = gmdate("n", $ta_end);
                    $teyear = gmdate("Y", $ta_end);
                    $te = gmmktime(0, 0, 0, $temon, daysinmonth($temon, $teyear), $teyear);
                    $dcomp = ($akt >= $td && $akt <= $te) ? true : false;
                 } else {
                    $te = 0;
                    $dcomp = true;
                 }
              } else
                 $dcomp = ($akt >= $td) ? true : false;

              if ($pr_status == 0) {
                 $al_hours = $plan;
                 $sump += $al_hours;
                 $sumparr[$z] += $al_hours;
              } else
                 $al_hours = 0;

              # 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) {
                 $query = "select pl_status from plan where ";
                 $query .= "pl_num = $pl_num";

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

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

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

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

              $akt = IncDate($periode, $akt);
              $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_periode = $data[7];
                 }
              }
           }

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

           if ($sumi > $sump && $pr_status == 0)
              printf("<td class=\"five\" align=\"right\"><br><b>%s</b></td>\n", FormatNum($sumi, 3));
           else
              printf("<td class=\"selakt\" align=\"right\"><br><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\"><br><b>%s</b></td>\n", FormatNum($ist, 3));
           else
              printf("<td class=\"selnum\"><b><br>%s</b></td>\n", FormatNum($ist, 3));

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

           if ($ist_erledigung) {
              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++;
        }

//      if ($pr_status == 0)
//         QueryDB($db, "drop table TempTask");

        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,6) ";
           $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";

        } else {
           $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 left join mitarbeiter on mi_num = pr_pl ";
           $query .= "left join plan on pl_prnum = pr_num ";
           $query .= "where pr_num in (select pl_prnum from plan where ";
           $query .= "pl_status in (0, 2, 3, 4, 5, 6)) and pl_status in (0,2,3,4,5,6) ";
        }

        $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[0], 1, $dxa[1]);
           $dtb = gmmktime(0, 0, 0, $dxb[0], 1, $dxb[1]);

           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") {
                 $dxa[0]--;

                 if ($dxa[0] < 1) {
                    $dxa[0] = 12;
                    $dxa[1]--;
                 }

                 $dta = gmmktime(0, 0, 0, $dxa[0], 1, $dxa[1]);
                 $dxb[0]--;

                 if ($dxb[0] < 1) {
                    $dxb[0] = 12;
                    $dxb[1]--;
                 }

                 $dtb = gmmktime(0, 0, 0, $dxb[0], 1, $dxb[1]);
              } else if (isset($func) && $func == "NextWeek") {
                 $dxa[0]++;

                 if ($dxa[0] > 12) {
                    $dxa[0] = 1;
                    $dxa[1]++;
                 }

                 $dta = gmmktime(0, 0, 0, $dxa[0], 1, $dxa[1]);
                 $dxb[0]++;

                 if ($dxb[0] > 12) {
                    $dxb[0] = 1;
                    $dxb[1]++;
                 }

                 $dtb = gmmktime(0, 0, 0, $dxb[0], 1, $dxb[1]);
              }

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

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

        echo "<form name=\"ist\" action=\"istpm.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, 240, "Projekte") . "</td>\n";
        echo "<td><select name=\"project\" ";
        echo "onChange=\"javascript:MoveTo('istpm.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></tr>\n";

        if (!$flag)
           $project = 0;

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

           $r = 0;

           while ($r < $rows) {
              $data = fetchDB($result, $r);
              $pr_pl[$r] = $data[2];
              $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 ($rstufe == 2 && $pr_pl[$r] != $unum) {
                       $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;
        }

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

        if (isset($von) && strlen($von) >= 4) {
           $sd = explode(GetDateSep(), $von);
           $dat = gmmktime(0, 0, 0, $sd[0], 1, $sd[1]);
           unset($sd);
        } else {
           $dat = time();
           $day = gmdate("j", $dat);
           $mon = gmdate("n", $dat);
           $year = gmdate("Y", $dat);

           if ($day > (daysinmonth($mon, $year) / 2)) {
              $mon++;

              if ($mon > 12) {
                 $mon = 1;
                 $year++;
              }
           }

           if ($mon <= 3) {
              $mon = 12 - (3 - $mon) + 1;

              if ($mon == 13)
                 $mon = 1;
              else
                 $year--;
           } else
              $mon -= 2;

           $dat = gmmktime(0, 0, 0, $mon, 1, $year);
        }

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

        $dvon = gmmktime(0, 0, 0, $mon, $day, $year);
//      $anz = ($pmlight)? 3 : 7;
        echo "<td><table border=0 cellpadding=0><tr><td><input type=\"text\" name=\"von\" value=\"";

        $vd = GetShortDate($dvon);
        echo "$vd\" size=7 maxlength=7 onBlur=\"javascript:CheckDate(this,1)\">";
        $dfrom = $dvon;

        # Zeitraum bis anzeigen
        if (isset($bis) && strlen($bis) >= 4) {
           $sd = explode(GetDateSep(), $bis);
           $dat = gmmktime(0, 0, 0, $sd[0], daysinmonth($sd[0], $sd[1]), $sd[1]);
           unset($sd);
        } else {
           $mon = gmdate("n");
           $year = gmdate("Y");
           $aday = gmdate("j");
           $day = daysinmonth($mon, $year);

           if ($aday > ($day / 2)) {
              $mon++;

              if ($mon > 12) {
                 $mon = 1;
                 $year++;
              }
           }

           $dat = gmmktime(0, 0, 0, $mon, $day, $year) - 1;
        }

        $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 = GetShortDate($dbis);
        echo "$vd\" size=7 maxlength=7 onBlur=\"javascript:CheckDate(this,1)\"></td>\n";
        $dto = $dbis;

        echo "<td>";
        ButtonImageMove("left.png", GetMessage($db, 247, "Woche zurück"), "istpm.php", "func=PrevWeek&$nav");
        echo "</td><td>";
        ButtonImageMove("right.png", GetMessage($db, 248, "Woche vor"), "istpm.php", "func=NextWeek&$nav");
        echo "</td><td>";
        ButtonImageMove("today.png", GetMessage($db, 249, "Heute"), "istpm.php", "func=Today&$nav");
        echo "</td><td>";
        ButtonLink(GetMessage($db, 250, "Zeitraum setzen"), "istpm.php","$nav");
        echo "</td></tr></table></td></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=4></td>\n";
        # Button zum Senden / Rueckgaengig machen
        echo "<table border=0 cellpadding=0 cellspacing=0><tr><td>\n";
        Button(GetMessage($db, 177, "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><td>&nbsp;&nbsp;&nbsp;</td><td>";

        if ($pl_selnum > 0 && $mitarbeiter == 0)
           $sort = 1;           // Projektsicht
        else
           $sort = 2;           // Mitarbeitersicht

        $pr_nav = "menu=$menu&headline=$headline&func=pauswertung&";
        $pr_nav .= "pjclosed=1&pl_num=$pl_selnum&mi_num=$mitarbeiter&dfrom=$dfrom&dto=$dto&";
        $pr_nav .= "fields_0=1&fields_4=5&fields_5=6&sort=$sort&header=2";
        ButtonPrint(GetMessage($db, 383, "Drucken"), "auswertpdf.php", $pr_nav, true);
        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\">";

        echo GetMessage($db, 252, "PT pro Periode") . "</th>\n";
        echo "<th class=\"sel\" colspan=2>" . 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)
        if ($rstufe != 3)
           echo "<th class=\"sel\">" . GetMessage($db, 257, "PL&nbsp;/&nbsp;Mitarbeiter") . "</th>\n";
        else
           echo "<th class=\"sel\">" . GetMessage($db, 258, "Projektleiter") . "</th>\n";

        echo "<th class=\"sel\"></th>\n";
        $r = 0;
        $dt = $dvon;

        while ($r < $anz) {
           $datum = GetShortDate($dt);
           $dt = IncDate($periode, $dt);
           echo "<th class=\"sel\">$datum</th>\n";
           $r++;
        }

        echo "<th class=\"sel\">" . GetMessage($db, 259, "Plan") . "</th>\n";
        echo "<th class=\"sel\">" . GetMessage($db, 260, "IST") . "</th>\n";
        echo "<th class=\"sel\">" . GetMessage($db, 259, "Plan") . "</th>\n";
        echo "<th class=\"sel\">" . GetMessage($db, 260, "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];

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

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

           # Wenn der angemeldete User PL ist ($rstufe == 2) und ein bestimmter
           # Mitarbeiter ausgewaehlt wurde, dann duerfen nur mehr die Projekte
           # des PL angezeigt werden!
           if ($rstufe == 2 && $mitarbeiter != $unum && $mitarbeiter > 0 && $pr_pl != $unum && $pl_status != 0) {
              $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 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 ";

              if ($rstufe == 3 || ($rstufe == 2 && $mitarbeiter == 0 && $pr_pl != $unum))
                 $query .= "wd_minum = $unum and ";
              else
                 $query .= "wd_minum = $mitarbeiter and ";

              $query .= "wd_datum between $dvon and $dbis and ";
              $query .= "pl_status in (2,3,4,5,6))) and ";
              $query .= "pl_prnum = $pr_num ";

              if ($rstufe == 3 || ($rstufe == 2 && $mitarbeiter == 0 && $pr_pl != $unum))
                 $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\">$mi_nname&nbsp;$mi_vname</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.
           #
           # Auch bei Konten duerfen Projektleiter nur sich selbst eintragen
           # duerfen.
           #
           if ($rstufe == 3 || ($rstufe == 2 && $pl_status == 0)) {             // Mitarbeiter
              if ($rstufe == 2)
                 $mi_flag = true;
              else
                 $mi_flag = false;

              if (!ShowMitarbeiter($db, $unum, $anz, $dvon, $dbis, $pr_num, $pl_num, $mi_flag)) {
                 closeDB($db);
                 return;
              }
           } else if (isset($mitarbeiter) && $mitarbeiter > 0) {
              if ($rstufe == 2)
                 $mi_flag = true;
              else
                 $mi_flag = false;

              if (!ShowMitarbeiter($db, $mitarbeiter, $anz, $dvon, $dbis, $pr_num, $pl_num, $mi_flag)) {
                 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];

                 # In Projekten in denen der User kein PL ist, darf er nur sich
                 # selbst editieren!
                 #
                 if ($rstufe == 2 && $pr_pl != $unum && $al_ressource != $unum) {
                    $z++;
                    continue;
                 }

                 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=4>" . GetMessage($db, 263, "Summen Plan:") . "<br>";
        echo GetMessage($db, 264, "Summen IST:") . "</td>\n";
        $z = 0;
        $sumptot = 0.0;
        $sumitot = 0.0;

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

        printf("<td class=\"selgray\" align=\"right\"><b>%s</b></td>", FormatNum($sumptot, 3));
        printf("<td class=\"selgray\" align=\"right\"><b>%s</b></td>", FormatNum($sumitot, 3));
        echo "<td class=\"sel\" colspan=2>&nbsp;</td>";

        if ($ist_erledigung)
           echo "<td class=\"sel\">&nbsp;</td>\n";

        echo "</tr>\n";
        echo "</table></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, co_dration from counter"))) {
           closeDB($db);
           return false;
        }

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

        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 (CheckTrue($wd_rk))
                 $wd_rk = 1;
              else
                 $wd_rk = 0;

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

              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))
                 $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))
                 $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(GetMessage(-1, 265, "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, task where ";
           $query .= "wd_minum = $minum and wd_task = ta_num and ";
           $query .= "ta_hash = '$ta_hash' and ";

           $day = 1;
           $mon = gmdate("n", $dt);
           $year = gmdate("Y", $dt);
           $fdate = gmmktime(0, 0, 0, $mon, 1, $year);
           $ldate = gmmktime(0, 0, 0, $mon, daysinmonth($mon, $year), $year);
           $query .= "wd_datum between $fdate and $ldate ";

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

           $anz = numrowsDB($reswd);

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

           if ($anz > 0) {
              # Bevor wir IST-Daten loeschen koennen, muessen wir eventuell
              # vorhandene Resekosten und/oder Spesen loeschen.
              $query = "select wd_num from wdone where wd_minum = $minum and ";
              $query .= "wd_datum between $fdate and $ldate and ";
              $query .= "wd_task in (select ta_num from task where ";
              $query .= "ta_hash = '$ta_hash')";

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

              $rows = numrowsDB($result);
              $j = 0;

              while ($j < $rows) {
                 $data = fetchDB($result, $j);
                 $num = $data[0];

                 $query = "delete from dration where dr_wdnum = $num";

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

                 $j++;
              }

              $query = "delete from wdone where wd_minum = $minum and ";
              $query .= "wd_datum between $fdate and $ldate and ";
              $query .= "wd_task in (select ta_num from task where ";
              $query .= "ta_hash = '$ta_hash')";

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

           $j = 0;
           $h = $hours * $leneinheit;
           $days = daysinmonth($mon, $year);

           while ($j < $h) {
              $datum = gmmktime(0, 0, 0, $mon, $day, $year);
              $co_wdone++;

              if (($h - $j) < $leneinheit)
                    $ho = $h - $j;
              else
                 $ho = $leneinheit;

              if (!$verrmodul) {
                 $wd_rk = 0;
                 $wd_verr = "true";
              }

              $wd_text = addslashes($wd_text);
              $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, ";
              $query .= "$minum, $datum, $ho, $wd_task, $wd_rk, $ta_phase,";
              $query .= "true, $wd_verr, '$wd_text')";

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

              if ($verrmodul && $j == 0) {
                 $co_dration++;

                 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_dration, 0, $dr_amount, 0, 0, 0,";
                    $query .= "$co_wdone, $dr_whg, $dr_costloc)";
                 } else {
                    $dr_purpose = addslashes($dr_purpose);
                    $dr_target = addslashes($dr_target);
                    $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_dration, '$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;
                 }
              }

              $j += $leneinheit;
              $day++;

              if ($day > $days)
                 $day = 1;
           }

           if ($verrmodul)
              $query = "update counter set co_wdone = $co_wdone, co_dration = $co_dration";
           else
              $query = "update counter set co_wdone = $co_wdone";

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

           QueryDB($db, "commit");
        }

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

if (!$pmlight) {
   Error(GetMessage(-1, 266, "Periodensicht ist nicht aktiviert!"));
   require_once('footer.inc');
   return;
}

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

ManageTime();

require_once('footer.inc');
?>