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

include_once ("../include/global.inc.php");
include_once ("../include/auth.inc.php");
include_once ("../include/html.inc.php");
include_once ("../include/image.inc.php");
include_once ("../include/bwv.inc.php");


getSecHttpVars("1",array("f_act", "mapSizeMode", "federalStateMode", "transparency", "distance", "singleZipcode", "specialJob", 
                         "displayDirectionArrows", "displayJobIDs", "displayCrSIDs", "displayZipcodes", 
                         "displayCouriers", "f_hq_id", "statusMessage", "useRemoteDB"));

// Check for authentication access and granted rights
$usrAccessArray["hq"] = "1";
authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
authCheckEmployeeRights($emp_id, "10", "1");

// ******************************************************
// Very special treatment for access to remote database
$remoteDBisActive = false;
if (BWV2_checkEmpRemoteDBAccess($useRemoteDB, $emp_id)) :
   $remoteDBisActive = BWV2_setRemoteDBAccessParameter();
endif;
$constLonghaulActive = "";
if ($remoteDBisActive) :   
   if ($f_hq_id == "") : $f_hq_id = array(); endif;
   if (count($f_hq_id) == 0) : array_push($f_hq_id, getMinOfField("headquarters", "hq_id", "")); endif;
   $constLonghaulActive = getParameterValue("0", "LONGHAUL_ACTIVE_REMOTE_DB", "0");
endif;
// ******************************************************

if ($mapSizeMode != "1" && $mapSizeMode != "2" && $mapSizeMode != "3") : $mapSizeMode = "2"; endif;
if ($federalStateMode == "" || count($federalStateMode) == 0) : 
   $federalStateMode = "0";
   $federalStateMode_box = "";
else: 
   $federalStateMode = "1";
   $federalStateMode_box = "checked";
endif;
if ($transparency == "") : $transparency = "0"; endif;
if ($distance == "") : $distance = "80"; endif;
if ($displayDirectionArrows == "" || count($displayDirectionArrows) == 0) :
   $displayDirectionArrows = "0";
   $displayDirectionArrows_box = "";
else: 
   $displayDirectionArrows = "1";
   $displayDirectionArrows_box = "checked";
endif;
if ($displayJobIDs == "" || count($displayJobIDs) == 0) : 
   $displayJobIDs = "0";
   $displayJobIDs_box = "";
else: 
   $displayJobIDs = "1";
   $displayJobIDs_box = "checked";
endif;
if ($displayCrSIDs == "" || count($displayCrSIDs) == 0) : 
   $displayCrSIDs = "0";
   $displayCrSIDs_box = "";
else: 
   $displayCrSIDs = "1";
   $displayCrSIDs_box = "checked";
endif;
if ($displayZipcodes == "" || count($displayZipcodes) == 0) : 
   $displayZipcodes = "0";
   $displayZipcodes_box = "";
else: 
   $displayZipcodes = "1";
   $displayZipcodes_box = "checked";
endif;
if ($displayCouriers == "" || count($displayCouriers) == 0) : 
   $displayCouriers = "0";
   $displayCouriers_box = "";
else: 
   $displayCouriers = "1";
   $displayCouriers_box = "checked";
endif;

// Get status of activation of longhaul mediation
if ($constLonghaulActive == "") :
   $constLonghaulActive = getParameterValue("0", "LONGHAUL_ACTIVE", "0");
endif;

// Mandator filter
if ($f_hq_id == "") : $f_hq_id = array(); endif;
if (count($f_hq_id) == 0) : array_push($f_hq_id, $hq_id); endif;
// Special conversion for map script (array => string)
$f_hq_id_string = implode(",", $f_hq_id);

// Current number of headquarters of the mandator
$numOfHq = getCountOfTable("mandatorheadquarters", "md_id = '" . $md_id . "'");

// $singleZipcode = trim($singleZipcode);
$singleZipcode = pad(trim($singleZipcode), 5, "0");   // Use "pad()" because e.g. "01067" is interpreted as "1067" !!!
$zipGeoCoord = getGeoCoordinatesFromZipcode($singleZipcode);
$zipLong = $zipGeoCoord[0];
$zipLat = $zipGeoCoord[1];
if ($zipLong == "" || $zipLat == "") : $singleZipcode = ""; endif;

// Return job to headquarters
if ($f_act == "returnJobToHeadquarters") :
   if ($specialJob != "") :
      $res = updateStmt("job", "jb_id", $specialJob, array("jb_longhaul", "2"), "jb_longhaul = '1'");
      if ($db->affected_rows == 0) :
         $statusMessage = "Der Auftrag kann nicht zurückgegeben werden, da er manuell den Status einer Ferntour zugewiesen bekommen hat!";
      endif;
      $specialJob = "";  // Init to avoid other potential operations according this value
   endif;
endif;

$currentTime = getDateTime("0");

$outListOfLongHaulJobs = "";

// Generate a list of long haul jobs to be mediated
$outListOfLongHaulJobs .= "<b>Zu vermittelnde Ferntouren:</b><br><br>";
$jbArray = getLongHaulJobData("9", $distance);
$jbArrayLen = count($jbArray);
for ($i = 0; $i < $jbArrayLen; $i++) {   	   

	  $jbId = $jbArray[$i][0];           // Get ID of the tour
	  $crSid = $jbArray[$i][1];          // Get SID of the courier
	  $jbTotalprice = $jbArray[$i][2];   // Get total price
    $jbZipcodes = $jbArray[$i][3];     // Get zipcodes (and cities) of the tour
    $jbZipcodes = trim(substr($jbZipcodes, 0, strpos($jbZipcodes, "|")));   // Remove cities
    if (substr($jbZipcodes, -1) == ";") :    // Remove last character if ";" again
       $jbZipcodes = substr($jbZipcodes, 0, -1);
    endif;                      
    $jbZipcodes = str_replace (";", ",", $jbZipcodes);    
    $jbZipcodesArray = spliti(",", $jbZipcodes);
    $jbZipcodesArrayLen = count($jbZipcodesArray);
    $vhtJobID = $jbArray[$i][6];
    $vhtJobName = $jbArray[$i][7];
    // $vhtPayload = $jbArray[$i][8];
    // $vhtMeasures = $jbArray[$i][9];
    $jbLonghaul = $jbArray[$i][10];

	  // Long haul jobs
	  if ($remoteDBisActive) :
	     // $outListOfLongHaulJobs .= $jbId . "&nbsp;&nbsp;";    // Do not display because ONLY view mode !!!
	     $outListOfLongHaulJobs .= "<a href=\"javascript:popupWindow('../longhaul/jb_detail.php?job_id=" . ec($jbId) . "&dbhistory=job&useRemoteDB=" . ec($useRemoteDB) . "','Auftrag','');\">" . $jbId . "</a>&nbsp;&nbsp;";
	  else :
	     $outListOfLongHaulJobs .= "<a href=\"javascript:popupWindow('../longhaul/jb_detail.php?job_id=" . ec($jbId) . "&dbhistory=job&useRemoteDB=" . ec($useRemoteDB) . "','Auftrag','');\">" . $jbId . "</a>&nbsp;&nbsp;";
	  endif;
	  $outListOfLongHaulJobs .= "<a href=\"javascript:showSpecialJob('" . ec($jbId) . "');\">A</a>&nbsp;&nbsp;";
	  // $outListOfLongHaulJobs .= "[" . $crSid . ", " . $vhtName . "]&nbsp;&nbsp;[" . $jbTotalprice . "]&nbsp;&nbsp;";
	  // $outListOfLongHaulJobs .= "(" . $jbZipcodes . ")<br><br>";
	  $outListOfLongHaulJobs .= "<span class=\"f8np1_red\">[" . $vhtJobName . "]</span>&nbsp;&nbsp;";
	  $outListOfLongHaulJobs .= "(";
	  for ($j = 0; $j < $jbZipcodesArrayLen; $j++) {
        $outListOfLongHaulJobs .= "<a href=\"javascript:showSpecialZipcode('" . ec($jbZipcodesArray[$j]) . "');\">" . $jbZipcodesArray[$j] . "</a>";
        if ($j < ($jbZipcodesArrayLen - 1)) : $outListOfLongHaulJobs .= ",&nbsp;"; endif;
    }
    $outListOfLongHaulJobs .= ")&nbsp;&nbsp;&nbsp;";
    if ($constLonghaulActive == "1") :
       if ($jbLonghaul == "1") :
          $outListOfLongHaulJobs .= "<a href=\"javascript:returnJobToHeadquarters('" . ec($jbId) . "', '" . $jbId . "');\">Rückgabe</a>&nbsp;&nbsp;&nbsp;";
       else :
          $outListOfLongHaulJobs .= "Man. FT &nbsp;";
       endif;
    else :
       $outListOfLongHaulJobs .= "Keine Verm. &nbsp;";
    endif;
    $outListOfLongHaulJobs .= "<br><br>";
}
$outListOfLongHaulJobs .= "<br>";

// Generate a list of long haul jobs taken by couriers
$outListOfLongHaulJobs .= "<b>Angenommene Ferntouren:</b><br><br>";
$jbArray = getLongHaulJobData("1", $distance);
$jbArrayLen = count($jbArray);
for ($i = 0; $i < $jbArrayLen; $i++) {

	  $jbId = $jbArray[$i][0];           // Get ID of the tour
	  $crSid = $jbArray[$i][1];          // Get SID of the courier
	  $jbTotalprice = $jbArray[$i][2];   // Get total price
    $jbZipcodes = $jbArray[$i][3];     // Get zipcodes (and cities) of the tour
    $jbZipcodes = trim(substr($jbZipcodes, 0, strpos($jbZipcodes, "|")));   // Remove cities
    if (substr($jbZipcodes, -1) == ";") :    // Remove last character if ";" again
       $jbZipcodes = substr($jbZipcodes, 0, -1);
    endif;                      
    $jbZipcodes = str_replace (";", ",", $jbZipcodes);
    $jbZipcodesArray = spliti(",", $jbZipcodes);
    $jbZipcodesArrayLen = count($jbZipcodesArray);
    $vhtName = $jbArray[$i][4];   // Get (current) vehicle of the courier on the roadtrip
    $vhtPayload = $jbArray[$i][8];
    $vhtMeasures = $jbArray[$i][9];
	
	  // Long haul jobs 
	  if ($remoteDBisActive) :
	     // $outListOfLongHaulJobs .= $jbId . "&nbsp;&nbsp;";    // Do not display because ONLY view mode !!!
	     $outListOfLongHaulJobs .= "<a href=\"javascript:popupWindow('../longhaul/jb_detail.php?job_id=" . ec($jbId) . "&dbhistory=job&useRemoteDB=" . ec($useRemoteDB) . "','Auftrag','');\">" . $jbId . "</a>&nbsp;&nbsp;";
	  else :
	     $outListOfLongHaulJobs .= "<a href=\"javascript:popupWindow('../longhaul/jb_detail.php?job_id=" . ec($jbId) . "&dbhistory=job&useRemoteDB=" . ec($useRemoteDB) . "','Auftrag','');\">" . $jbId . "</a>&nbsp;&nbsp;";
	  endif;
	  $outListOfLongHaulJobs .= "<a href=\"javascript:showSpecialJob('" . ec($jbId) . "');\">A</a>&nbsp;&nbsp;";
	  $outListOfLongHaulJobs .= "[<a href=\"../locating/locating.php?f_cr_sid=" . $crSid . "&useRemoteDB=" . ec($useRemoteDB) . "\" target=\"_blank\">" . $crSid . "</a>, " . $vhtName . ", " . $vhtMeasures . ", (" . $vhtPayload . ")]&nbsp;&nbsp;";
	  // $outListOfLongHaulJobs .= "(" . $jbZipcodes . ")<br><br>";
	  $outListOfLongHaulJobs .= "(";
	  for ($j = 0; $j < $jbZipcodesArrayLen; $j++) {
        $outListOfLongHaulJobs .= "<a href=\"javascript:showSpecialZipcode('" . ec($jbZipcodesArray[$j]) . "');\">" . $jbZipcodesArray[$j] . "</a>";
        if ($j < ($jbZipcodesArrayLen - 1)) : $outListOfLongHaulJobs .= ",&nbsp;"; endif;
    }
    $outListOfLongHaulJobs .= ") &nbsp;";
    $outListOfLongHaulJobs .= "<br><br>";
}
$outListOfLongHaulJobs .= "<br>";

// Generate a list of done long haul jobs with couriers on the way home
$outListOfLongHaulJobs .= "<b>Rücktouren:</b><br><br>";
$jbArray = getLongHaulJobData("2", $distance);
$jbArrayLen = count($jbArray);
for ($i = 0; $i < $jbArrayLen; $i++) {

	  $jbId = $jbArray[$i][0];           // Get ID of the tour
	  $crSid = $jbArray[$i][1];          // Get SID of the courier
	  $jbTotalprice = $jbArray[$i][2];   // Get total price
    $jbZipcodes = $jbArray[$i][3];     // Get zipcodes (and cities) of the tour
    $jbZipcodes = trim(substr($jbZipcodes, 0, strpos($jbZipcodes, "|")));   // Remove cities
    if (substr($jbZipcodes, -1) == ";") :    // Remove last character if ";" again
       $jbZipcodes = substr($jbZipcodes, 0, -1);
    endif;                      
    $jbZipcodes = str_replace (";", ",", $jbZipcodes);
    $jbZipcodesArray = spliti(",", $jbZipcodes);
    $jbZipcodesArrayLen = count($jbZipcodesArray);
    $vhtName = $jbArray[$i][4];   // Get (current) vehicle of the courier on the roadtrip
    $vhtPayload = $jbArray[$i][8];
    $vhtMeasures = $jbArray[$i][9];

	  // Long haul jobs 
    if ($remoteDBisActive) :
	     // $outListOfLongHaulJobs .= $jbId . "&nbsp;&nbsp;";    // Do not display because ONLY view mode !!!
	     $outListOfLongHaulJobs .= "<a href=\"javascript:popupWindow('../longhaul/jb_detail.php?job_id=" . ec($jbId) . "&dbhistory=job&useRemoteDB=" . ec($useRemoteDB) . "','Auftrag','');\">" . $jbId . "</a>&nbsp;&nbsp;";
	  else :
	     $outListOfLongHaulJobs .= "<a href=\"javascript:popupWindow('../longhaul/jb_detail.php?job_id=" . ec($jbId) . "&dbhistory=job&useRemoteDB=" . ec($useRemoteDB) . "','Auftrag','');\">" . $jbId . "</a>&nbsp;&nbsp;";
	  endif;
	  $outListOfLongHaulJobs .= "<a href=\"javascript:showSpecialJob('" . ec($jbId) . "');\">A</a>&nbsp;&nbsp;";
	  $outListOfLongHaulJobs .= "[<a href=\"../locating/locating.php?f_cr_sid=" . $crSid . "&useRemoteDB=" . ec($useRemoteDB) . "\" target=\"_blank\">" . $crSid . "</a>, " . $vhtName . ", " . $vhtMeasures . ", (" . $vhtPayload . ")]&nbsp;&nbsp;";
	  // $outListOfLongHaulJobs .= "(" . $jbZipcodes . ")<br><br>";
	  $outListOfLongHaulJobs .= "(";
	  for ($j = 0; $j < $jbZipcodesArrayLen; $j++) {
        $outListOfLongHaulJobs .= "<a href=\"javascript:showSpecialZipcode('" . ec($jbZipcodesArray[$j]) . "');\">" . $jbZipcodesArray[$j] . "</a>";
        if ($j < ($jbZipcodesArrayLen - 1)) : $outListOfLongHaulJobs .= ",&nbsp;"; endif;
    }
    $outListOfLongHaulJobs .= ") &nbsp;";
    $outListOfLongHaulJobs .= "<br><br>";    
}

?>

<html lang="de">

<head>
<meta http-equiv="expires" content="Sat, 01 Dec 2003 00:00:00 GMT">
<title>BUNDESWEITE VERMITTLUNG</title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<script type="text/javascript">
<!--
var statusMessage = '<?php echo $statusMessage ?>';

function displayStatusMessage() {
 if (statusMessage != "") {
    alert(statusMessage);
 }
}

function showSpecialJob(jobId) {
  this.document.forms[0].specialJob.value = jobId;
  this.document.forms[0].submit();
}

function returnJobToHeadquarters(jobIdEncrypted, jobId) {
	if (confirm('Möchten Sie den Auftrag ' + jobId + ' an die Niederlassung zurückgeben?')) {
     this.document.forms[0].specialJob.value = jobIdEncrypted;
     this.document.forms[0].f_act.value = 'returnJobToHeadquarters';
     this.document.forms[0].submit();
  }
}

function showSpecialZipcode(zipcode) {
  this.document.forms[0].singleZipcode.value = zipcode;
  this.document.forms[0].submit();
}

function clearSingleZipcode() {	
  document.forms[0].singleZipcode.value='';
};

function hqCheckAll(numOfHq) {
  for (i = 0; i < numOfHq; i++) {
	  document.getElementsByName('f_hq_id[]')[i].checked = true;
	}
}

function hqUncheckAll(numOfHq) {
  for (i = 0; i < numOfHq; i++) {
	  document.getElementsByName('f_hq_id[]')[i].checked = false;
  }
}

// Opens a new (popup-)window with specified parameters
function popupWindow(url,title,config,noFrame) {

  // popup = window.open(url,title,config);

  var widthPopupWin = screen.width;
  var heightPopupWin = screen.height;
  if (!noFrame) {
     widthPopupWin -= 80;
     heightPopupWin -= 180;
  }
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2);
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2);
  var popup;
  popup = window.open(url,title,
    "dependent=yes,width=" + widthPopupWin + ",height=" +
    heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin +
    ",scrollbars=yes,resizable=yes");  
  
}

-->
</script>
</head>

<body onLoad="displayStatusMessage()">

<table border="0">
  <tr>
    <td valign="top" align="center">
      <img src="../longhaul/jb_longhaul_map.php?mapSizeMode=<?php echo ec($mapSizeMode) ?>&federalStateMode=<?php echo ec($federalStateMode) ?>&transparency=<?php echo ec($transparency) ?>&distance=<?php echo ec($distance) ?>&singleZipcode=<?php echo ec($singleZipcode) ?>&specialJob=<?php echo ec($specialJob) ?>&displayDirectionArrows=<?php echo ec($displayDirectionArrows) ?>&displayJobIDs=<?php echo ec($displayJobIDs) ?>&displayCrSIDs=<?php echo ec($displayCrSIDs) ?>&displayZipcodes=<?php echo ec($displayZipcodes) ?>&f_hq_id_string=<?php echo $f_hq_id_string ?>&displayCouriers=<?php echo $displayCouriers ?>&useRemoteDB=<?php echo ec($useRemoteDB) ?>" border="0">
    </td>

    <td width="10">

    </td>

    <td valign="top" align="center">
      <form action="../longhaul/jb_longhaul.php" method="post">
        <input type="hidden" name="f_act" value="">
        <input type="hidden" name="specialJob" value="">
        <input type="hidden" name="useRemoteDB" value="<?php echo ec($useRemoteDB) ?>">
      
        <table border="0">        
          <tr>
            <td valign="top" align="left">
            	
            	<table border="0">
                <tr>
                  <td valign="top" align="left" width="50%">

                      Kartengröße: <select name="mapSizeMode"><?php echo addOptionsFromRange("1", "3", $mapSizeMode, "", "", " ") ?></select>
                      <br><br>
                      
                      Transparenz: <select name="transparency"><?php echo addOptionsFromArray(array("0","10","20","30","40","50","60","70","80","90","100"), $transparency, "") ?></select> [%]
                      <br><br>
                      
                      Mindestdistanz: <select name="distance"><?php echo addOptionsFromArray(array("50","60","70","80","90","100","150","200","300","400","500"), $distance, "") ?></select> [KM]
                      <br><br>
                      
                      Anzeige Bundesländer: <input type="checkbox" name="federalStateMode[]" value="1" <?php echo $federalStateMode_box ?>>
                  </td>
                  
                  <td valign="top" align="left">
                  	
                  	  Anzeige Fahrtrichtung: <input type="checkbox" name="displayDirectionArrows[]" value="1" <?php echo $displayDirectionArrows_box ?>>
                      <br><br>
                  	  Anzeige Auftragsnummern: <input type="checkbox" name="displayJobIDs[]" value="1" <?php echo $displayJobIDs_box ?>>
                      <br><br>
                      Anzeige Fahrzeugdaten: <input type="checkbox" name="displayCrSIDs[]" value="1" <?php echo $displayCrSIDs_box ?>>
                      <br><br>
                      Anzeige Postleitzahlen: <input type="checkbox" name="displayZipcodes[]" value="1" <?php echo $displayZipcodes_box ?>>
                      <br><br>
                      Anzeige Kuriere: <input type="checkbox" name="displayCouriers[]" value="1" <?php echo $displayCouriers_box ?>>
                  </td>
                </tr>
              </table>

              <br><br><br><br>

              Lokalisiere PLZ: <input type="text" name="singleZipcode" value="<?php echo $singleZipcode ?>" size="5">
              <a href="javascript:clearSingleZipcode();"><img src="../images/waste.png" border="0" height="15" width="12"></a>
              <br><br>        

              <br><br><br><br>
              <input type="submit" value="Aktualisieren">&nbsp;&nbsp;
              (Letzte Aktualisierung: <?php echo substr($currentTime, -8) ?> Uhr)
              <br><br>

              <input type="button" value="Listenansicht" onClick="popupWindow('../longhaul/jb_list_fs.php?maskNumOfWins=4&useRemoteDB=<?php echo ec($useRemoteDB) ?>','Ferntouren','',true);">&nbsp;&nbsp;
              <br><br>

              <br><br><br><br>

              Niederlassungen:<br><br>
              <?php echo addCheckboxesFromTable("f_hq_id","headquarters","hq_id","hq_mnemonic","hq_mnemonic","",$f_hq_id, "&nbsp;&nbsp;&nbsp;"); ?>
              <input type="button" name="f_hq_check_all" value="Alle" onClick="hqCheckAll(<?php echo $numOfHq; ?>);">
              <input type="button" name="f_hq_uncheck_all" value="Keine" onClick="hqUncheckAll(<?php echo $numOfHq; ?>);">
              <br><br>

              <br><br><br><br>
              <?php echo $outListOfLongHaulJobs ?>
            </td>
          </tr>
        </table>       
      
      </form>


    </td>
  </tr>
</table>

</body>

</html>

