<?php
/*=======================================================================
 *
 * overview.php
 *
 * Autor: Carsten Annacker
 *
 =======================================================================*/
/*
$HTTP_GET_VARS = !empty($HTTP_GET_VARS) ? $HTTP_GET_VARS : $_GET;
$ob = $HTTP_GET_VARS['ob'];
$od = $HTTP_GET_VARS['od'];
$oda = array ($od, $od, $od, $od, $od, $od, $od, $od, $od);
$om = $HTTP_GET_VARS['om'];
if ($ob != "")
  $oda[$ob] = ($od == "d" ? "a" : "d");

Event 102:        Sendung wurde vom Kurier übernommen.
Event 201:        Ankunft der Sendung im zentralen Hub.
Event 202:        Ausgang der Sendung aus dem zentralen Hub.
Event 301:        Sendung zugestellt.
Event 304:        Vergeblicher Zustellversuch.
  "401" => "Adresse falsch",
  "407" => "Annahme verweigert",
  "409" => "Paket verloren",
Event 411:        Auf der Packliste avisierte Sendung wurde vom Versender nicht mitgeliefert
Event 412:        Sendung steht nicht auf der Packliste/Übergabe an falschen Kurierservice
  "416" => "Nicht zustellbar"
*/

define("LOG_NAME", "overview");
include_once("../include/auth.inc.php");
include_once("../tools/edi.inc.php");

$status_codes = array(
  "101" => "Sendung hat Versandzentrum verlassen",
  "102" => "Abholung&nbsp;beim&nbsp;Versender",
  "201" => "Eingangs-Scan",
  "202" => "Ausgangs-Scan",
  "301" => "Zugestellt",
  "302" => "Ausgangs-Scan,&nbsp;Zustellung&nbsp;l&auml;uft",
  "304" => "Zustellungsversuch",
  "308" => "Liegt&nbsp;zur&nbsp;Abholung&nbsp;bereit",
  "320" => "Kunde&nbsp;hat&nbsp;abgeholt",
  "328" => "Zustellung&nbsp;auf&nbsp;den&nbsp;n&auml;chsten&nbsp;Werktag&nbsp;verschoben",
  "401" => "Adresse&nbsp;falsch",
  "403" => "Unbekannt&nbsp;verzogen",
  "407" => "Annahme&nbsp;verweigert",
  "409" => "Verlust&nbsp;durch&nbsp;Spediteur",
  "411" => "Auftrag&nbsp;ohne&nbsp;Sendung",
  "412" => "Sendung&nbsp;ohne&nbsp;Auftrag",
  "416" => "Nicht&nbsp;zustellbar",
  "451" => "Lagerfrist&nbsp;abgelaufen"
);

$finish_codes = array(
  "AS_INSTRUCTED"   => "laut&nbsp;Anweisung",
  "CARPORT"         => "Carport",
  "DECK"            => "Terasse",
  "DOCK"            => "Verladerampe",
  "FRONT_DESK"      => "Rezeption",
  "FRONT_DOOR"      => "Haust&uuml;r",
  "GARAGE"          => "Garage",
  "GUARD"           => "Wachmann",
  "MAILBOX"         => "Briefkasten",
  "MAIL_ROOM"       => "Poststelle",
  "MAIL_SLOT"       => "Briefschlitz",
  "MC_BOY"          => "Junge",
  "MC_GIRL"         => "M&auml;dchen",
  "MC_MAN"          => "Mann",
  "MC_WOMAN"        => "Frau",
  "NEIGHBOR"        => "Nachbar",
  "OFFICE"          => "B&uuml;ro",
  "OUTBUILDING"     => "Nebengeb&auml;ude",
  "PATIO"           => "Innenhof",
  "PORCH"           => "Veranda",
  "REAR_DOOR"       => "Hintereingang",
  "RECEIVER"        => "Bewohner",
  "RECEPTIONIST"    => "Empfang",
  "SECURE_LOCATION" => "sicherer&nbsp;Ort",
  "SIDE_DOOR"       => "Nebeneingang",
  "LEASING_OFFICE"  => "Leasing-B&uuml;ro",
  "CUSTOMER_PICKUP" => "Kundenabholung",
  " "               => "dritter Versuch"
);

$min_es_date = isset($_POST["f_min_es_date"]) ? $_POST["f_min_es_date"] : "";
$max_es_date = isset($_POST["f_max_es_date"]) ? $_POST["f_max_es_date"] : "";
$show_unfinished_only = isset($_POST["f_show_unfinished_only"]) ? $_POST["f_show_unfinished_only"] : "";
//$f_formsubmit = isset($_POST["formsubmit"]) ? $_POST["formsubmit"] : "";  
if (trim($min_es_date) == ""):
  $min_es_date = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 2, date("Y")));
else:
  $min_es_date = sqlDate($min_es_date);
endif;
if (trim($max_es_date) == ""):
  $max_es_date = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
else:
  $max_es_date = sqlDate($max_es_date);
endif;
if (trim($show_unfinished_only) == ""):
  $show_unfinished_only = "0";
endif;
$unfinished_only = "";
if ($show_unfinished_only == "1"):
  $unfinished_only =
      "  AND es.ec_track_id NOT IN"
    . "  (SELECT DISTINCT trat_serialno FROM phoenix.tourarticle, phoenix.tourarticleprocess"
    . "    WHERE phoenix.tourarticle.trat_id = phoenix.tourarticleprocess.trat_id AND tratp_type >= 301 AND tratp_type != 302 AND tratp_type != 304 AND tratp_type != 328)";
endif;
$track_ids = array();
$events = array();
$sqlquery = "SELECT es.ec_track_id, es.es_date,"
          . "       tr.jb_id, tr.tr_comp, tr.tr_person, tr.tr_phone, tr.tr_hsno,"
          . "       ad.ad_street, ad.ad_zipcode, ad.ad_city"
          . "  FROM edi_status AS es, phoenix.tourarticle AS trat, phoenix.tour AS tr, phoenix.address AS ad, phoenix.job AS jb"
          . "  WHERE es.es_date >= '$min_es_date 00:00:00' AND es.es_date <= '$max_es_date 23:59:59' AND es.es_status <= 2 AND"
          . "        es.ec_track_id = trat.trat_serialno AND trat.trat_name != 'freescan'"
          . "        AND trat.jb_id = tr.jb_id AND"
          . "        trat.tr_sort = tr.tr_sort AND tr.tr_sort > 1 AND LEFT (tr.tr_comp, 1) != '*' AND"
//          . "        tr.ad_id = ad.ad_id AND tr.jb_id = jb.jb_id AND jb.hq_id = $hq_id AND jb.csc_id_payer != " . $csc_id_lineA[$hq_id]
          . "        tr.ad_id = ad.ad_id AND tr.jb_id = jb.jb_id AND jb.csc_id_payer IN " . $csc_id_sql[$hq_id]
          . " " . $unfinished_only
          . "  ORDER BY LEFT(es_date, 10), ec_track_id";
//echo $sqlquery . "<br>\n";
$res = mysql_query($sqlquery) or die ($sqlquery . ": " . mysql_error());
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)):
  $retVal = "<a href=\"javascript:showInfo('" . $row["ec_track_id"] . "','" . $row["jb_id"] . "')\"><b>" . $row["ec_track_id"] . "</b></a>" . " (" . $row["es_date"] . ")<br>\n";
  $retVal .= "<a href=\"javascript:editJob('" . $row["ec_track_id"] . "','" . $row["jb_id"] . "')\">&nbsp;<b>" . $row["tr_comp"];
  if (trim($row["tr_person"]) != ""):
    $retVal .= ", " . $row["tr_person"];
  endif;
  $retVal .= "<br>\n&nbsp;" . $row["ad_zipcode"] . " " . $row["ad_city"] . ", ";
  $retVal .= $row["ad_street"] . " " . $row["tr_hsno"] . "</a></b><br>\n";
  if (trim($row["tr_phone"]) != ""):
    $retVal .= "&nbsp;Tel.: " . $row["tr_phone"] . "<br>\n";
  endif;
  $track_ids[$row["ec_track_id"]] = array($retVal, 0);
  $sqlquery = "SELECT trat.trat_id, trat.jb_id, trat.tr_sort,"
            . "       tratp.tratp_type, tratp.tratp_remark, tratp.tratp_createtime,"
            . "       tr.tr_signname, tr.tr_hsno,"
            . "       ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country"
            . "  FROM phoenix.tourarticle AS trat, phoenix.tourarticleprocess AS tratp, phoenix.tour AS tr, phoenix.address AS ad"
            . "  WHERE trat.trat_serialno = '" . $row["ec_track_id"] . "' AND trat.trat_name != 'freescan' AND"
            . "        trat.trat_id = tratp.trat_id AND"
            . "        trat.jb_id = tr.jb_id AND"
            . "        trat.tr_sort = tr.tr_sort AND"
            . "        tr.ad_id = ad.ad_id"
            . "  ORDER BY tratp.tratp_createtime";
  $res1 = mysql_query($sqlquery) or die ($sqlquery . ": " . mysql_error());
//echo "$sqlquery<br>\n";
//  $cur_time = date("Y-m-d H:i:s");
  while ($row1 = mysql_fetch_array($res1, MYSQL_ASSOC)):
    if ($row1["tratp_type"] >= 301 && $row1["tratp_type"] != 302 && $row1["tratp_type"] != 304 && $row1["tratp_type"] != 328)
      $track_ids[$row["ec_track_id"]][1] = 1;
    if ($row1["tratp_type"] == 201 || ($row1["tratp_type"] >= 401) || ($row1["tratp_type"] == 301 && (trim($row1["tratp_remark"]) == "CUSTOMER_PICKUP" || $row1["tratp_remark"] == " ")))
      $row1["tr_signname"] = "-/-";
    if (trim($row1["tratp_remark"]) != "" || $row1["tratp_remark"] == " ")
      $row1["tratp_remark"] = "&nbsp;(" . $finish_codes[$row1["tratp_remark"]] . ")";
    $events[$row["ec_track_id"]][] = array(
      $row1["jb_id"] . "/" . $row1["tr_sort"], $row1["ad_zipcode"] . " " . $row1["ad_city"] . ", " . $row1["ad_street"] . " " . $row1["tr_hsno"],
      $status_codes[$row1["tratp_type"]] . $row1["tratp_remark"], $row1["tratp_createtime"], $row1["tr_signname"]);
  endwhile;
  mysql_free_result($res1);
endwhile;
mysql_free_result($res);
//print_r($track_ids);
//print_r($events);
//die();

$content =
  "<table border=0 cellspacing=0 cellpadding=4 vspace=0 hspace=0>\n" .
  "  <tr>\n" .
  "    <td align=left><div class=\"f7np1\"><span style=\"color:#1b12b9\">&nbsp;<b>Lfd. Nr. (Datum)</b></span>&nbsp;</div></td>" .
  "    <td align=left><div class=\"f7np1\"><span style=\"color:#1b12b9\">&nbsp;<b>Auftrags-/Stationsnr.</b></span>&nbsp;</div></td>" .
  "    <td align=left><div class=\"f7np1\"><span style=\"color:#1b12b9\">&nbsp;<b>Adresse</b></span>&nbsp;</div></td>" . 
  "    <td align=left><div class=\"f7np1\"><span style=\"color:#1b12b9\">&nbsp;<b>Ereignis (Bemerkung)</b></span>&nbsp;</div></td>" . 
  "    <td align=left><div class=\"f7np1\"><span style=\"color:#1b12b9\">&nbsp;<b>Ereigniszeit</b>&nbsp;</span>&nbsp;</div></td>" . 
  "    <td align=left><div class=\"f7np1\"><span style=\"color:#1b12b9\">&nbsp;<b>Unterschrift Klartext</b>&nbsp;</span>&nbsp;</div></td>" .
  "  </tr>\n";
$cur_no = 1;
//$cnt_all = 0;
//$cnt_finished = 0;
//$cnt_open = 0;
foreach ($events as $ec_track_id => $event_list) {
	$span_open = "";
	$span_close = "";
  if ($track_ids[$ec_track_id][1] == 1):
//    $cnt_finished++;
    if ($cur_no % 2 == 0) : $cellColor = "#DDDDDD"; $cellColor2 = "#EEEEEE"; endif;
    if ($cur_no % 2 == 1) : $cellColor = "#BBBBFF"; $cellColor2 = "#CCCCFF"; endif;
  else:
//    $cnt_open++;
  	$span_open = "<span style=\"color:red\">";
	  $span_close = "</span>";
    if ($cur_no % 2 == 0) : $cellColor = "#FFBBBB"; $cellColor2 = "#FFCCCC"; endif;
    if ($cur_no % 2 == 1) : $cellColor = "#FF9999"; $cellColor2 = "#FFAAAA"; endif;
  endif;
  $content .=
    "  <tr>\n" .
    "    <td bgcolor=\"$cellColor\" align=left valign=top><div class=\"f7np1\">&nbsp;" . $span_open .
    $cur_no++ . ": " . $track_ids[$ec_track_id][0] .
//     "&nbsp;" . ($track_ids[$ec_track_id][1] == 1 ? "erledigt" : "offen") .
    $span_close .
    "</div></td>\n";
//  $cnt_all++;
	$tmp_content = array();
  foreach ($event_list as $event) {
  	$i = 0;
    foreach ($event as $event_item) {
    	if ($tmp_content[$i] != "")
    	  $tmp_content[$i] .= "<br>\n";
      $tmp_content[$i++] .= "&nbsp;" . $event_item . "&nbsp;";
    }
  }
  $content .=
    "    <td bgcolor=\"$cellColor2\" align=left valign=top><div class=\"f7np1\">" . $span_open . $tmp_content[0] . $span_close . "&nbsp;</div></td>\n" .
    "    <td bgcolor=\"$cellColor\" align=left valign=top><div class=\"f7np1\">" . $span_open . $tmp_content[1] . $span_close . "&nbsp;</div></td>\n" .
    "    <td bgcolor=\"$cellColor2\" align=left valign=top><div class=\"f7np1\">" . $span_open . $tmp_content[2] . $span_close . "&nbsp;</div></td>\n" .
    "    <td bgcolor=\"$cellColor\" align=left valign=top><div class=\"f7np1\">" . $span_open . str_replace(" ", "&nbsp;", $tmp_content[3]) . $span_close . "&nbsp;</div></td>\n" .
    "    <td bgcolor=\"$cellColor2\" align=left valign=top><div class=\"f7np1\">" . $span_open . $tmp_content[4] . $span_close . "&nbsp;</div></td>\n" .
    "  </tr>\n";
}
$content .= 
  "</table><br>\n";

function formDate($sqlDate) {
  return substr($sqlDate, 8, 2) . "." . substr($sqlDate, 5, 2) . "." . substr($sqlDate, 0, 4);
}

function sqlDate($germanDate) {
	if (trim($germanDate) == "")
	  return "";
  return substr($germanDate, 6, 4) . "-" . substr($germanDate, 3, 2) . "-" . substr($germanDate, 0, 2);
}

function span_date($date_val)
{
  if (check_date($date_val))
    return "<span style=\"color:red\">" . $date_val . "</span>";
  return $date_val;
}

/**************************************************************************************************************************************************/
// HTML-Output

?>

<html>
<head>
<title>AUFTRAGS&Uuml;BERSICHT</title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
<!--
.verysmall
{ font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 2pt; font-weight: normal; padding: 0px}
-->
</style>

<script type="text/javascript">
<!--

function check_jb_id(trackingID, jb_id) {
  if (jb_id == <?php echo $jb_idA[$hq_id]["STBX_SAME"]?> || jb_id == <?php echo $jb_idA[$hq_id]["STBX_TIMED"]?> || jb_id == <?php echo $jb_idA[$hq_id]["STBX_NEXT"]?>) {
    alert ("Das Paket mit der Tracking-Nummer " + trackingID + " befindet sich \nin der Scan-Liste und kann daher noch nicht bearbeitet werden.\n\n" +
    "Es muss erst auf einen normalen Auftrag gebucht worden sein,\nbevor hier eine Änderung vorgenommen werden kann!\n"
     );
    return false;
  }
  return true;
}

function showInfo(trackingID, jb_id) {
	if (check_jb_id(trackingID, jb_id)) {	
    var widthPopupWin = 380;
    var heightPopupWin = 280;
    var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
    var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
    var popupWin;  
    popupWin = window.open("../tools/edit_track_id.php?track_id=" + trackingID + "&jb_id=" + jb_id, "Info","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=no");
    popupWin.focus();
  }
};

function editJob(trackingID, jb_id) {
	if (check_jb_id(trackingID, jb_id)) {	
    var widthPopupWin = 1150;
    var heightPopupWin = screen.height - 100;
    var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
    var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
    var popupWin;
    popupWin = window.open("../tools/edit_job.php?track_id=" + trackingID, "AUFTRAGSBEARBEITUNG","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
    popupWin.focus();
  }
};

-->
</script>

</head>
<body>
  <table border="0" width="100%" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
    <tr>
      <td>
        &nbsp;
      </td>
      <td align="center">
        <table border="0" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
          <tr>
          	<td align="center"> <div class="f10bp1_blue">
              <br>
              Auftrags&uuml;bersicht<br>
              <br>
        <form name="overview" action="overview.php" method="post">
    Eingangsdatum ab:&nbsp;
    <input name="f_min_es_date" type="text" size="9" maxlength="10" class="input"
      value="<?php echo ($min_es_date != "" ? formDate($min_es_date) : "") ?>">&nbsp;
    bis:&nbsp;
    <input name="f_max_es_date" type="text" size="9" maxlength="10" class="input"
      value="<?php echo ($max_es_date != "" ? formDate($max_es_date) : "") ?>">&nbsp;
    <input type="checkbox" name="f_show_unfinished_only" value="1"
      title="Nur unerledigte Sendungen anzeigen" <?php echo ($show_unfinished_only == "1" ? "checked" : "") ?>>
    &nbsp;Nur&nbsp;unerledigte&nbsp;anzeigen&nbsp;&nbsp;
    <input type="submit" name="formsubmit" size="2" value="Anzeigen" class="submit">
        	
        </form>              
            </td>
          </tr>
          <tr>
          	<td align="center">
              <?php echo $content;?>
            </td>
          </tr>
        </table>
      </td>
      <td>
        &nbsp;
      </td>
    </tr>
  </table>
</body>
</html>
