<?php
/*=======================================================================
 *
 * jb_scan_check.php 
 *
 =======================================================================*/


include_once ("../include/mcglobal.inc.php");
include_once ("../include/auth.inc.php");
include_once ("../include/inc_file.inc.php");

getSecHttpVars("1",array("f_act", "statusMessage", "deactivateMenu", "numOfStations", "f_costcenter", "checkFile1", "checkFile2"));

$deactivateMenuStatic = "1";

getLanguage(__FILE__);

$pageTitel = getLngt("Aufträge");
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");

getCurrentScript(__FILE__);


// Init
$jobDataSrc = array();
$jobDataTarget = array();

if ($numOfStations == "") : $numOfStations = 0; endif;

$trSortOffset = 50;
$specialJbStatusNo = 999;
$specialJbStatusEnabled = false;
$driverPool = array();
$htmlOut = "";

$cscIdPayerArrayOfIFTMIN = array();
$jobArrayOfIFTMIN = array();

$currentTimestamp = getDateTime(6);

$write2logfile = "1";
$tempPath = "../log/";
$logFile = $tempPath . "stations_" . $currentTimestamp;

$output = "";


function getCurrentJobData($jobArrayOfIFTMIN) {
	global $db, $PHP_SELF;
	global $hq_id;

  $numOfStations = 0;
  $retArray = array();

  $jobArrayOfIFTMINLen = count($jobArrayOfIFTMIN);

  if ($jobArrayOfIFTMINLen > 0) :

    $whereClauseJobs = "(" . implode(",", $jobArrayOfIFTMIN) . ")";

    // Get data from "job", "tour", "tourarticle"
    $sqlquery = "SELECT jb.jb_id, jb.cr_id, csc.csc_id, csc.csc_name,"
              . "       tr.tr_sort, tr.tr_signname, tr.tr_hsno,"
              . "       ad.ad_id, ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country,"
              . "       crvh.crvh_sid"
              . "  FROM phoenix.job AS jb LEFT JOIN phoenix.couriervehicle AS crvh ON jb.cr_id = crvh.cr_id,"
              . "       phoenix.tour AS tr, phoenix.address AS ad, phoenix.costcenter AS csc"
              . "  WHERE jb.jb_id IN " . $whereClauseJobs . " AND"
              . "        jb.jb_id = tr.jb_id AND"
              . "        tr.ad_id = ad.ad_id AND"
              . "        jb.csc_id_payer = csc.csc_id"
              . "  ORDER BY jb.jb_id, tr.tr_sort";

// echo $sqlquery . "<br>";
    $result = $db->query($sqlquery);
    if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

    $numOfStations = $result->numRows();

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

          // Get the tracking IDs of the stations
          $trackingIDs = getColVectorFromDB2ArrayByClause("tourarticle", "trat_serialno", "jb_id = '" . $row["jb_id"] . "' AND tr_sort = '" . $row["tr_sort"] . "'", "", "");
          $trackingIDsString = implode(",", $trackingIDs);

          // Get number of stations of the job
          $numOfStationsOfCurrentJob = getCountOfTable("tour", "jb_id = '" . $row["jb_id"] . "'");

          $retArray[] = array($row["csc_id"],$row["csc_name"],$row["ad_id"],$row["ad_street"],$row["tr_hsno"],$row["ad_zipcode"],$row["ad_city"],$row["trat_id"],$trackingIDsString);
                           // $row["jb_id"],$row["tr_sort"],$row["crvh_sid"]
    endwhile;
    $result->free();
  endif;

  return $retArray;
}


if ($checkFile1 == "" || $checkFile2 == "") :

   // Get costcenters of all current IFTMIN orders
   $sqlquery = "SELECT DISTINCT jb.csc_id_payer, csc.csc_name"
             . "  FROM phoenix.job AS jb, phoenix.costcenter AS csc"
             . "  WHERE jb.jb_status = '" . $specialJbStatusNo . "' AND"
             . "        jb.csc_id_payer = csc.csc_id"
             . "  ORDER BY jb.csc_id_payer";
   
   $result = $db->query($sqlquery);
   if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
   
   while ($row = $result->fetch_assoc()):
         $cscIdPayerArrayOfIFTMIN[$row["csc_id_payer"]] = $row["csc_name"];
   endwhile;
   $result->free();
   
   
   // Get job data array of specified IFTMIN orders accorting to the selected costcenter
   $whereClauseCostcenter = "";
   if ($f_costcenter != "" && is_numeric($f_costcenter)) :
      $whereClauseCostcenter = " AND jb.csc_id_payer = '" . $f_costcenter . "'";
   endif;
   
   $sqlquery = "SELECT jb.jb_id"
             . "  FROM phoenix.job AS jb"
             . "  WHERE jb.jb_status = '" . $specialJbStatusNo . "'"
             .          $whereClauseCostcenter
             . "  ORDER BY jb.jb_id";
   
   $result = $db->query($sqlquery);
   if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
   
   while ($row = $result->fetch_assoc()):
         $jobArrayOfIFTMIN[] = $row["jb_id"];
   endwhile;
   $result->free();

   // Get current state of the station data with their package informations
   $stationArray = getCurrentJobData($jobArrayOfIFTMIN);
   $stationArrayLen = count($stationArray);
   for ($i = 0; $i < $stationArrayLen; $i++) :

       $logText = implode(";", $stationArray[$i]);
       writeImportLog($logText);
   endfor;
   
   $filesInFolder = getFilenamesInFolder($tempPath);
   $filesInFolderLen = count($filesInFolder);
   for ($i = 0; $i < $filesInFolderLen; $i++) :
       if (substr($filesInFolder[$i], 0, 8) == "stations") :
          $output .= $filesInFolder[$i] . "<br>";
       endif;
   endfor;
   $output .= "<br><br><br>";
else :

   $matchArray = array();
   $noMatchArray = array();
   
   // Check specified files
   $data = importCSV($tempPath . $checkFile1);
   $dataLen = count($data);

   $data2 = importCSV($tempPath . $checkFile2);
   $data2Len = count($data2);

   // loop all rows
   for ($j = 0; $j < $dataLen; $j++) {
      $isFound = false;
   
      // loop for all fields of $fields of the row
      for ($i = 0; $i < $data2Len; $i++) {
          
          if ($data[$j] == $data2[$i]) :
             $isFound = true;
             break;
          endif;
      }

      if ($isFound) :
         $matchArray[] = implode(";", $data[$j]);
      else :
         $noMatchArray[] = implode(";", $data[$j]);
      endif;
   }
   
   // Output of the Arrays
   $matchArrayLen = count($matchArray);
   $output .= "<b>OK:</b><br><br>";
   for ($i = 0; $i < $matchArrayLen; $i++) {
   	   $output .= $i . ".: " . $matchArray[$i] . "<br>";
   }
   $output .= "<br><br><br>";
   
   $noMatchArrayLen = count($noMatchArray);
   $output .= "<b>NOT OK:</b><br><br>";
   for ($i = 0; $i < $noMatchArrayLen; $i++) {
   	   $output .= $i . ".: " . $noMatchArray[$i] . "<br>";
   }
    $output .= "<br><br><br>";
endif;
?>


<html>

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

<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>

<script src="../include/checkFormTags.js" type="text/javascript"></script>

<script type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>


var numOfStations = <?php echo $numOfStations ?>;

function execBodyOnLoad() {
<?php 
  echo $phpCurrentNavigationOnLoad;
?>
// initForm();
displayStatusMessage();
}


function finishPage(f_act) {
	document.forms[0].f_act.value = f_act;
	document.forms[0].submit();
}	

function changeCostcenter() {
	finishPage('');
}
-->
</script>


</head>

<body onLoad="execBodyOnLoad();">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="maincontent" name="maincontent" id="maincontent">

  <form name="jb_scan" action="../tools/jb_scan_check.php" method="post">

    <input type="hidden" name="f_act" value="">
    <?php echo $phpCurrentNavigationInputHidden ?>
    <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">
    <input type="hidden" name="statusMessage" value="">

    <input type="hidden" name="numOfStations" value="<?php echo $numOfStations ?>">

    <br><br>    

    <?php echo $output; ?>
    
    <input type="text" name="checkFile1" value="" size="20"> [Datei 1]<br>
    <input type="text" name="checkFile2" value="" size="20"> [Datei 2]<br>
    <br>
    <input type="button" name="compare" value="Vergleich" onclick="javascript:finishPage('');">
<!--
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="button" name="button_save_changes" value="<?php echo getLngt("Alle Aufträge freigeben") ?>" onclick="javascript:finishFinalSave();">
-->

  </div>

  </form>

</div>

</body>

</html>
