<?php
/*=======================================================================
 *
 * tracking_ADSG.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/


include_once ("../include/mcglobal.inc.php");
include_once ("../include/inc_job.inc.php");
include_once ("../include/inc_tracking.inc.php");

// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "trackingID", "selectedLanguage"));


$trackingID = trim($trackingID);

// Write logdata into log file
if ($trackingID != "") :
   $currentTime = getDateTime("0");
   writeToFile("../log/tracking.log", $currentTime . " : " . $trackingID);
endif;

if ($selectedLanguage == "" || !is_numeric($selectedLanguage)) : $selectedLanguage = $languageSelected; endif;
$languageSelected = $selectedLanguage;

getLanguage(__FILE__);
getCurrentScript(__FILE__);

$debug = false;

if ($debug) :
   echo "trackingID: " . $trackingID . "<br>";
endif;

$pageTitel = getLngt("Sendungsverfolgung");


// *********************
// TEST INSERT INTO "metaobject"
// insertMetaobjectTrackingItem($moIdRefDB, $objType, $objId, $moValueTrId, $moValueTratSerialno = "")
// $tmpArr = insertMetaobjectTrackingItem("2", "track_tr", "1812157", "3712779", "");
// $tmpArr = insertMetaobjectTrackingItem("2", "track", "1812157", "1812157", "1042209022");
// $tmpArr = insertMetaobjectTrackingItem("2", "track_jb", "1812157", "", "1812157");
// *********************
// TEST SENDING TRACKING MAIL
// $tmpArr = sendTrackingHTMLMail("mv@assecutor.de", "e36bac56d55c8ea4afee7aa50ef9fd1c");
// print_r($tmpArr); die();
// *********************

// Get header image from database
// $imgFilename = ...
if ($imgFilename == "") : $imgFilename = "assecutor.png"; endif;

$trOut = "";
$csEndOut = "";
$cmpOut = "";
$cmp2Out = "";
$historyOut = "";

$out = getLngt("Ihre Sendung konnte unter der angegebenen Nummer nicht aufgefunden werden.") . "<br>" . getLngt("Bitte kontrollieren Sie noch einmal die Eingabe.");
if ($trackingID == "") :
   $out = getLngt("Bitte geben Sie Ihre Sendungsnummer ein.");

else :
   // Get the array for formatting the database-values for the output according to the defined type
   $outputFormatField = defineOutputFormats();

   $tmpArray = getTrackingdataByID($trackingID);
   $objType = $tmpArray[0];
   $trackingArray = $tmpArray[1];   
   $trackingArrayLen = count($trackingArray);

   if ($debug) : print_r($trackingArray); echo "\n<br>"; endif;

   if ($trackingArrayLen > 0) :

        // Header
        if ($objType == "tracking_jb" || $objType == "track_jb") :

           $mapViewLink = "[<a href=\"../locating/map.php?jb_id=" . $trackingArray[0]["jb_id"] . "&tracking=" . $trackingID . "\" target=\"_blank\">" . getLngt("Karte") . "</a>]";

           $historyOut .= "<tr>\n";
           $historyOut .= "  <td align=\"center\" colspan=\"5\">\n";
           $historyOut .= "    <span class=\"f12bp1_darkgreen\">" . getLngt("Auftrag") . ": " . $trackingArray[0]["jb_id"]  . "</span>" . "&nbsp;&nbsp;" . $mapViewLink . "\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "</tr>\n";
           $historyOut .= "<tr><td class=\"spacer25\"></td></tr>\n";

           $historyOut .= "<tr>\n";
           $historyOut .= "  <td align=\"center\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Station") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td align=\"center\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Artikel") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td align=\"center\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Ereigniszeit") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td align=\"center\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Ereignis") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td align=\"left\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Bemerkung") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "</tr>\n";
           $historyOut .= "<tr><td class=\"spacer05\"></td></tr>\n";

       elseif ($objType == "tracking_tr" || $objType == "track_tr") :

           $currAddress = $trackingArray[0]["ad_street"] . " " . $trackingArray[0]["tr_hsno"] . " - " . $trackingArray[0]["ad_zipcode"] . " " . $trackingArray[0]["ad_city"];

           $classCurrAddress = "f12bp1_red";
           if ($trackingArray[0]["tr_state"] == "1") : 
              $classCurrAddress = "f12bp1_darkgreen";
           endif;

           $historyOut .= "<tr>\n";
           $historyOut .= "  <td align=\"center\" colspan=\"4\">\n";
           $historyOut .= "    <span class=\"" . $classCurrAddress . "\">" . $currAddress  . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "</tr>\n";
           $historyOut .= "<tr><td class=\"spacer25\"></td></tr>\n";

           $historyOut .= "<tr>\n";
           $historyOut .= "  <td align=\"center\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Artikel") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td align=\"center\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Ereigniszeit") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td align=\"center\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Ereignis") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td align=\"left\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Bemerkung") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "</tr>\n";
           $historyOut .= "<tr><td class=\"spacer05\"></td></tr>\n";

       elseif ($objType == "tracking_trat" || $objType == "track_trat") :

           if ($trackingArray[0]["trat_serialno"] != "") :
              $historyOut .= "<tr>\n";
              $historyOut .= "  <td align=\"center\" colspan=\"3\">\n";
              $historyOut .= "    <span class=\"f12bp1_darkgreen\">" . "Sendung" . ": " . $trackingArray[0]["trat_serialno"]  . "</span>\n";
              $historyOut .= "  </td>\n";
              $historyOut .= "</tr>\n";
              $historyOut .= "<tr><td class=\"spacer25\"></td></tr>\n";
           endif;

           $historyOut .= "<tr>\n";
           $historyOut .= "  <td align=\"center\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Ereigniszeit") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td align=\"center\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Ereignis") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td align=\"left\">\n";
           $historyOut .= "    <span class=\"f10bp1\">" . getLngt("Bemerkung") . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "</tr>\n";
           $historyOut .= "<tr><td class=\"spacer05\"></td></tr>\n";
       endif;

       // Body
       $remAddress = "";
       $remTratSerialno = "";
       for ($i = 0; $i < $trackingArrayLen; $i++) :

           $currAddress = $trackingArray[$i]["ad_street"] . " " . $trackingArray[$i]["tr_hsno"] . "<br>" . $trackingArray[$i]["ad_zipcode"] . " " . $trackingArray[$i]["ad_city"];
           if ($currAddress == $remAddress) :
              $currAddress = "";
           else :
              $remAddress = $currAddress;
           endif;

           $currTratSerialno = $trackingArray[$i]["trat_serialno"];
           if ($currTratSerialno == $remTratSerialno) :
              $currTratSerialno = "";
           else :
              $remTratSerialno = $currTratSerialno;
           endif;

           if ($objType == "tracking_jb" || $objType == "track_jb") :

              $classCurrAddress = "f10np1_red";
              if ($trackingArray[$i]["tr_state"] == "1") : 
                 $classCurrAddress = "f10np1_darkgreen";
              endif;

              if ($trackingArray[$i]["trat_serialno"] != "") :
                 $historyOut .= "<tr>\n";
                 $historyOut .= "  <td align=\"center\">\n";
                 $historyOut .= "    <span class=\"" . $classCurrAddress . "\">" . $currAddress . "</span>\n";
                 $historyOut .= "  </td>\n";
                 $historyOut .= "  <td align=\"center\">\n";
                 $historyOut .= "    <span class=\"f10np1\">" . $currTratSerialno . "</span>\n";
                 $historyOut .= "  </td>\n";
                 $historyOut .= "  <td align=\"center\">\n";
                 $historyOut .= "    <span class=\"f10np1\">" . formatOutput($trackingArray[$i]["tratp_createtime"],$outputFormatField["tratp_createtime"]) . "</span>\n";
                 $historyOut .= "  </td>\n";
                 $historyOut .= "  <td align=\"center\">\n";
                 $historyOut .= "    <span class=\"f10np1\">" . $trackingArray[$i]["mt_value"] . "</span>\n";
                 $historyOut .= "  </td>\n";
                 $historyOut .= "  <td align=\"left\">\n";
                 $historyOut .= "    <span class=\"f10np1\">" . $trackingArray[$i]["tratp_remark"] . "</span>\n";
                 $historyOut .= "  </td>\n";
                 $historyOut .= "</tr>\n";
                 $historyOut .= "<tr><td class=\"spacer05\"></td></tr>\n";
              endif;

           elseif ($objType == "tracking_tr" || $objType == "track_tr") :

              if ($trackingArray[$i]["trat_serialno"] != "") :
                 $historyOut .= "<tr>\n";
                 $historyOut .= "  <td align=\"center\">\n";
                 $historyOut .= "    <span class=\"f10np1\">" . $currTratSerialno . "</span>\n";
                 $historyOut .= "  </td>\n";
                 $historyOut .= "  <td align=\"center\">\n";
                 $historyOut .= "    <span class=\"f10np1\">" . formatOutput($trackingArray[$i]["tratp_createtime"],$outputFormatField["tratp_createtime"]) . "</span>\n";
                 $historyOut .= "  </td>\n";
                 $historyOut .= "  <td align=\"center\">\n";
                 $historyOut .= "    <span class=\"f10np1\">" . $trackingArray[$i]["mt_value"] . "</span>\n";
                 $historyOut .= "  </td>\n";
                 $historyOut .= "  <td align=\"left\">\n";
                 $historyOut .= "    <span class=\"f10np1\">" . $trackingArray[$i]["tratp_remark"] . "</span>\n";
                 $historyOut .= "  </td>\n";
                 $historyOut .= "</tr>\n";
                 $historyOut .= "<tr><td class=\"spacer05\"></td></tr>\n";
              endif;

           elseif ($objType == "tracking_trat" || $objType == "track_trat") :

              $historyOut .= "<tr>\n";
              $historyOut .= "  <td align=\"center\">\n";
              $historyOut .= "    <span class=\"f10np1\">" . formatOutput($trackingArray[$i]["tratp_createtime"],$outputFormatField["tratp_createtime"]) . "</span>\n";
              $historyOut .= "  </td>\n";
              $historyOut .= "  <td align=\"center\">\n";
              $historyOut .= "    <span class=\"f10np1\">" . $trackingArray[$i]["tratp_state"] . "</span>\n";
              $historyOut .= "  </td>\n";
              $historyOut .= "  <td align=\"left\">\n";
              $historyOut .= "    <span class=\"f10np1\">" . $trackingArray[$i]["tratp_remark"] . "</span>\n";
              $historyOut .= "  </td>\n";
              $historyOut .= "</tr>\n";
              $historyOut .= "<tr><td class=\"spacer05\"></td></tr>\n";
           endif;

           /*
           $historyOut .= "<tr>\n";
           $historyOut .= "  <td width=\"30%\" align=\"left\">\n";
           $historyOut .= "    <span class=\"f10np1\">" . formatOutput($trackingArray[$i]["tratp_createtime"],$outputFormatField["tratp_createtime"]) . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td width=\"50%\" align=\"left\">\n";
           $historyOut .= "    <span class=\"f10np1\">" . $trackingArray[$i]["mt_value"] . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "  <td width=\"20%\" align=\"left\">\n";
           $historyOut .= "    <span class=\"f10np1\">" . $trackingArray[$i]["tratp_state"] . "</span>\n";
           $historyOut .= "  </td>\n";
           $historyOut .= "</tr>\n";
           $historyOut .= "<tr><td class=\"spacer05\"></td></tr>\n";
           */
       endfor;

       // ---------------------------------

       // Get contractor (seller) data (e.g. Amazon) of the carrier (e.g. Stadtbote) for visualisation for the end customer
       // Get process of all events of the tracking ID
/*
       $sqlquery = "SELECT cmp.cmp_comp, cmp.cmp_comp2, hq.hq_name, cmp2.cmp_comp AS cmp2_comp,"
                 . "       cmp2.cmp_comp2 AS cmp2_comp2, cmp2.cmp_logo AS cmp2_logo, cmp2.cmp_logo_width AS cmp2_logo_width, cmp2.cmp_logo_height AS cmp2_logo_height"
                 . "  FROM phoenix.job AS jb, phoenix.costcenter AS csc, phoenix.customer AS cs, phoenix.company AS cmp, phoenix.headquarters AS hq, phoenix.company AS cmp2"
                 . "  WHERE jb.jb_id = '" . $objId . "'   AND"
                 . "        jb.csc_id_payer = csc.csc_id AND"
                 . "        csc.cs_id = cs.cs_id         AND"
                 . "        cs.cmp_id = cmp.cmp_id       AND"
                 . "        cs.hq_id = hq.hq_id          AND"
                 . "        hq.cmp_id = cmp2.cmp_id";

       $result = $db_conn->query($sqlquery);
       if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

       while ($row = $result->fetch_assoc()):                          

             // $cmpOut .= $row["cmp_comp"] . "&nbsp;" . $row["cmp_comp2"] . "<br>\n";
             
             $cmpOut .= "<tr>\n";
             $cmpOut .= "  <td align=\"left\">\n";
             $cmpOut .= "    <span class=\"f12bp1\">" . $row["cmp_comp"] . "<br>" . $row["cmp_comp2"] . "</span>\n";
             $cmpOut .= "  </td>\n";
             $cmpOut .= "</tr>\n";
             $cmpOut .= "<tr><td class=\"spacer15\"></td></tr>\n";

             $row["hq_name"]   $row["cmp2_logo"]   $row["cmp2_logo_width"]   $row["cmp2_logo_height"]

             $cmp2Out .= "<tr>\n";
             $cmp2Out .= "  <td align=\"center\">\n";
             $cmp2Out .= "    <span class=\"f12bp1\">" . $row["cmp2_comp"] . "<br>" . $row["cmp2_comp2"] . "</span>\n";
             $cmp2Out .= "  </td>\n";
             $cmp2Out .= "</tr>\n";
             $cmp2Out .= "<tr><td class=\"spacer15\"></td></tr>\n";
       endwhile;
       $result->free();
*/

   endif;
endif;
?>

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset="ISO-8859-1">

<title><?php echo $pageTitel ?></title>

<style type="text/css">


.mcTable { border:0px; width:650px; background:#CDD9FD; }

.mcTable2 { border:0px; width:610px; background:#CDD9FD; }

.spacer05 { height:5px; background:#CDD9FD;}
.spacer15 { height:15px; background:#CDD9FD;}
.spacer25 { height:25px; background:#CDD9FD;}

.f10np1           { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: normal; padding: 1px; color: #000000;}
.f10bp1           { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: bold; padding: 1px; color: #000000;}
.f10np1_green     { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: normal; padding: 1px; color: #00BB00;}
.f10np1_darkgreen { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: normal; padding: 1px; color: #008800;}
.f10np1_red       { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: normal; padding: 1px; color: #FF0000;}
.f10np1_blue      { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; font-weight: normal; padding: 1px; color: #1b12b9;}
.f12np1           { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12pt; font-weight: normal; padding: 1px; color: #000000;}
.f12bp1           { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12pt; font-weight: bold; padding: 1px; color: #000000;}
.f12bp1_red       { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12pt; font-weight: bold; padding: 1px; color: #FF0000;}
.f12bp1_darkgreen { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12pt; font-weight: bold; padding: 1px; color: #008800;}
.f12np1_blue      { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12pt; font-weight: normal; padding: 1px; color: #1b12b9;}

</style>

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

function finishTrackingPage() {
	document.forms[0].submit();
}

function selectLanguage(lng) {
	document.forms[0].selectedLanguage.value = lng;
	 finishTrackingPage();
}

-->
</script>

</head>

<body>

<form action="tracking_ADSG.php" method="post">

<input type="hidden" name="f_act" value="">
<input type="hidden" name="trackingID" value="<?php echo $trackingID ?>">
<input type="hidden" name="selectedLanguage" value="<?php echo ec($selectedLanguage) ?>">

<center>
<table class="mcTable">
  <tr>
  	<td style="width:650px;" align="center">
  		<br><img src="../images/external/<?php echo $imgFilename; ?>" border="0" height="223" width="543">
    </td>
  </tr>
</table>
<table class="mcTable"><tr><td class="spacer05"></td></tr></table>

<table class="mcTable">  
	<tr>
		<td style="width:20px;">			
		</td>
			<td style="width:305px;" align="left">
			<span class="f10np1"><a href="javascript:selectLanguage('0');"><?php echo getLngt("[Deutsch]") ?></a></span>&nbsp;
			<span class="f10np1"><a href="javascript:selectLanguage('1');"><?php echo getLngt("[English]") ?></a></span>
		</td>
		<td style="width:305px;" align="right">
			<!-- <span class="f10np1"><a href="javascript:finishTrackingPage();"><?php echo getLngt("[Anfrage senden]") ?></a></span> -->
		</td>
		<td style="width:20px;">			
		</td>
  </tr>
</table>
<table class="mcTable"><tr><td class="spacer25"></td></tr></table>

<table class="mcTable">  
	<tr>
		<td style="width:20px;">			
		</td>
		<td style="width:610px;" align="left">
			<span class="f12np1_blue"><?php echo getLngt("Sendungsnummer") ?>:</span>&nbsp;&nbsp;&nbsp;
			<input type="text" name="trackingID" value="<?php echo $trackingID ?>" size="35">&nbsp;&nbsp;&nbsp;
			<span class="f10np1"><a href="javascript:finishTrackingPage();"><?php echo getLngt("[Anfrage senden]") ?></a></span>
		</td>
		<td style="width:20px;">
		</td>
  </tr>
</table>
<table class="mcTable"><tr><td class="spacer25"></td></tr></table>

<?php if ($csEndOut != "") : ?>
  <table class="mcTable">  
  	<tr>
  		<td style="width:20px;">
  		</td>
  		<td style="width:610px;" align="left">
  			<span class="f10np1_blue"><?php echo getLngt("Lieferadresse:") ?></span><br>
        <span class="f10np1"><?php echo $csEndOut ?></span>
  		</td>
  		<td style="width:20px;">
  		</td>
    </tr>
  </table>
  <table class="mcTable"><tr><td class="spacer25"></td></tr></table>
<?php endif; ?>

<?php if ($cmpOut != "") : ?>
  <table class="mcTable">  
  	<tr>
  		<td style="width:20px;">
  		</td>
  		<td style="width:610px;" align="left">
  			<span class="f10np1_blue"><?php echo getLngt("Lieferant:") ?></span><br>
        <span class="f10np1"><?php echo $cmpOut ?></span>
  		</td>
  		<td style="width:20px;">
  		</td>
    </tr>
  </table>
  <table class="mcTable"><tr><td class="spacer25"></td></tr></table>
<?php endif; ?>

<?php if ($historyOut == "" && $trOut == "") : ?>
  <table class="mcTable">  
  	<tr>
  		<td style="width:20px;">
  		</td>
  		<td style="width:610px;" align="center">
        <span class="f10np1_red"><?php echo $out ?></span>
  		</td>
  		<td style="width:20px;">
  		</td>
    </tr>
  </table>
  <table class="mcTable"><tr><td class="spacer25"></td></tr></table>
<?php endif; ?>

<?php if ($historyOut != "") : ?>
  <table class="mcTable">
  	<tr>
  		<td style="width:20px;">
  		</td>
  		<td style="width:610px;" align="center">
        <table class="mcTable2">

          <?php echo $historyOut ?>
        </table>
  		</td>
  		<td style="width:20px;">
  		</td>
    </tr>
  </table>
  <table class="mcTable"><tr><td class="spacer25"></td></tr></table>
<?php endif; ?>

<?php if ($trOut != "") : ?>
  <table class="mcTable">  
  	<tr>
  		<td style="width:20px;">
  		</td>
  		<td style="width:610px;" align="center">
        <table class="mcTable2">
          <tr>
            <td width="50%" align="left">
              <span class="f10bp1"><?php echo getLngt("Auslieferungsstationen") ?></span>
            </td>
            <td width="20%" align="left">
              <span class="f10bp1"><?php echo getLngt("Status") ?></span>
            </td>
            <td width="30%" align="left">
              <span class="f10bp1"><?php echo getLngt("Zeitpunkt") ?></span>
            </td>
          </tr>
          <tr><td class=\"spacer05\"></td></tr>
          <?php echo $trOut ?>
        </table>
  		</td>
  		<td style="width:20px;">
  		</td>
    </tr>
  </table>
  <table class="mcTable"><tr><td class="spacer15"></td></tr></table>
<?php endif; ?>

<table class="mcTable">
  <tr>
  	<td style="width:650px;" align="center">
  		<img src="../images/external/adsg_footer.png" border="0" height="51" width="640">
    </td>
  </tr>
</table>
</center>

</form>

</body>

</html>