<?php
/*=======================================================================
 *
 * service_aaa_check.php
 *
 =======================================================================*/


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


if (!isset($argv[1]) || $argv[1] == "") :
   include_once ("../include/auth.inc.php");
   getSecHttpVars("1",array("check_global", "cr_id", "jb_id"));
   if ($check_global != "") :
      $tmpArr = explode("|", $check_global);
      $cr_id = $tmpArr[0];
      $jb_id = $tmpArr[1];
   endif;
else :
   if ($argv[1] == "acapella7890") :
      $jb_id = $argv[2];
   else :
      die();
   endif;
endif;


$retArray = array("0","OK");
$hqAll = "0";  // Use headquarters of the job
$crExcludedArr = array();
$debug = false;
$debugArr = array();
$debugOut = "";


if (!($jb_id != "" && is_numeric($jb_id) && $jb_id > "0")) : 
   $retArray = array("2","jb_id missing or wrong");
else :
   if (!checkCr4GlobalJob($jb_id)) :
      $retArray = array("10","check failed for some couriers");
   endif;
endif;

// Set job with status "0"
updateStmt("job", "jb_id", $jb_id, array("jb_status", "0"),"jb_status = '99'");


// echo implode("|", $retArray);

if ($debug) :
   $checkModeArr = array("----", "NATIONALITY CHECK", "QUARANTINE CHECK", "THIRD COUNTRY CHECK");
   // $debugArrLen = count($debugArr);
   $debugArrLen = 3;

   $debugOut .= "</br></br><b>DEBUG</b></br></br>";
   $debugOut .=  "<b>AUFTRAG " . $jb_id . "</b></br></br>";
   // Station countries
	 $jbTourdata = getFieldValueFromId("job", "jb_id", $jb_id, "jb_tourdata");
	 $jbTourdataArr = explode("|", $jbTourdata);
	 $jbCountries = $jbTourdataArr[2];
   $debugOut .=  "[Stationsländerkürzel: " . $jbCountries . "]</br></br>";
   for ($i = 1; $i <= $debugArrLen; $i++) :
       // FINAL GDC ARRAY
       // $gdcContent = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = 'jb' AND gdc_obj_id = '" . $jb_id . "' AND gdc_gen_fieldname = 'global_job_excluded_cr_list'");
       // $crIdArr = explode(",", $gdcContent);
       // $crIdArrLen = count($crIdArr);
       
       // DEBUG ARRAY
       $crIdArr = $debugArr[$i];
       $crIdArrLen = count($crIdArr);       
       
       $debugOut .=  "<b>" . $checkModeArr[$i] . "</b></br>";
       $debugOut .=  "<table>";
       $debugOut .=  "<tr><td>" . "EID" . "</td><td>" . "SID" . "</td><td>" . "LAND" . "</td></tr>\n";
       for ($j = 0; $j < $crIdArrLen; $j++) :
           $crEid = getFieldValueFromId("courier", "cr_id", $crIdArr[$j], "cr_eid");           
           // $crvhSid = getFieldValueFromId("couriervehicle", "cr_id", $crIdArr[$j], "crvh_sid");
           $crvhSid  = getFieldValueFromClause("couriervehicle AS crvh, courier AS cr","crvh_sid","crvh.cr_id = '" . $crIdArr[$j] . "' AND cr.cr_sid = crvh.crvh_sid");
           $usrId = getFieldValueFromId("courier", "cr_id", $crIdArr[$j], "usr_id");
           $usrCountry = getFieldValueFromId("user", "usr_id", $usrId, "usr_country");
           $debugOut .=  "<tr><td>" . $crEid . "</td><td>" . $crvhSid . "</td><td>" . $usrCountry . "</td></tr>\n";
       endfor;
       $debugOut .=  "</table>";
       $debugOut .=  "</br></br>";
   endfor;
   echo $debugOut;
endif;
?>
