<?php

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


//// Check HTTP-Parameters
//getSecHttpVars("1",array("crId", "fromDateRange", "toDateRange"));

list ($sel_cr_sid) = getHttpVars(array("cr_sid"));
$crId = $db->getOne("SELECT cr_id FROM couriervehicle WHERE crvh_sid = '" . $sel_cr_sid . "'");
check_daterange();

// Get the year of the begin of history data
$dbhistory = getDBNames(0);


// $logoIds <=> (3 = Annahme [Default], 6 = Automatisch vermittelt, 7 = Manuell vermittelt, 8 = Automatisch entzogen, 11 = Abgelehnt [Default])
function crHistory ($crId, $logoIds = array(), $fromDateRange = "", $toDateRange = "", $limitOfRowsDisplayed = "4000") {
  global $db, $dbh_log, $PHP_SELF, $hq_id;
  
  $pageTitle = getLngt("TRANSPORTEURSHISTORIE");
$debug = false;
if ($debug): 
   echo "crId = " . $crId . "<br>";
   print_r($logoIds); echo "<br><br>";
endif;
  $outPut = "";
  if (isset($crId) && is_numeric($crId) && $crId > 0 && is_array($logoIds)) :

     $outputFormatField = defineOutputFormats();

     if (count($logoIds) == 0) : $logoIds = array(3,6,7,8,11,35,37,86); endif;  // [3,11] [3,6,7,8,11] [3,6,7,8,11,35,37,86]
     if ($fromDateRange == "") : $fromDateRange = getDateTime("date_firstday_currentmonth_lastyear") . " 00:00:00"; endif;
     if ($toDateRange == "") : $toDateRange = getDateTime("0"); endif;
if ($debug): 
   echo "fromDateRange = " . $fromDateRange . "<br>";
   echo "toDateRange = " . $toDateRange . "<br><br>";
   print_r($logoIds); echo "<br><br>";
endif;
     // From table "log"
     $fieldClause = getDBFields("logoperation") . "," . getDBFields("log") . "," . getDBFields("user") . " ";

     $fromClause = "phoenix_log.logoperation AS logo, phoenix_log." . $dbh_log . " AS log " .
                   "LEFT JOIN phoenix.user AS usr ON log.usr_id = usr.usr_id ";

     // $whereClause = "log.cr_id = '" . $crId . "' AND log.hq_id = '" . $hq_id . "' AND log.logo_id = logo.logo_id AND ";
     $whereClause = "log.cr_id = '" . $crId . "' AND log.logo_id = logo.logo_id AND ";
     $whereClause .= "log.log_createtime >= '" . $fromDateRange . "' AND log.log_createtime <= '" . $toDateRange . "'";
     $whereClause .= " AND logo.logo_id IN (" . implode(",", $logoIds) . ")";

     $orderByClause = "log.log_createtime LIMIT 0," . $limitOfRowsDisplayed;

     $sqlquery = generateStatement($fieldClause,$fromClause,$whereClause,$orderByClause);
if ($debug): 
   echo $sqlquery . "<br><br>";
endif;
     // Send request to database
     $result = $db->query($sqlquery);

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

     // Generate list with header
     $titleArray = array(getLngt("Vorgang") . "&nbsp;&nbsp;",getLngt("Zeitpunkt") . "&nbsp;&nbsp;",getLngt("Auftrag") . "&nbsp;&nbsp;",getLngt("Fahrzeug") . "&nbsp;&nbsp;",getLngt("Name") . "&nbsp;&nbsp;",getLngt("Vorname") . "&nbsp;&nbsp;",getLngt("Telefon") . "&nbsp;&nbsp;",getLngt("Details"));
     $fieldArray = array("logo_op_shortname","log_createtime","jb_id","cr_sid","usr_name","usr_firstname","usr_phone","logo_description");
     $aligns = "l,c,c,c,l,l,l,l";
     $alignArray = spliti(",",$aligns);
     $alignTitles = "center";
     $widths = "330,130,50,50,150,150,80,200";
     $widthArray = spliti(",",$widths);
     $summationField = "";
     $postParserField = "logo_description";
     $mode = "1";             // Output from DB-RESULT
     $sortDBField = "";       // Used in following include-file for sorting per column;
     include ("../include/inc_list_defineoutput.inc.php");          
     $result->free();

     // Courier data
     $crSid = getFieldValueFromId("courier", "cr_id", $crId, "cr_sid");
     $crUsrId = getFieldValueFromId("courier", "cr_id", $crId, "usr_id");
     $tmpFields = getFieldsValueFromId("user","usr_id",$crUsrId,array("usr_firstname","usr_name"));
     $crUsrFirstname = $tmpFields[0];
     $crUsrName = $tmpFields[1];

     // Post parsing if necessary
     if ($postParserField != "") :
        // $tableBody = postParseCrHistory($tableBody, $crSid);
        $tableBody = postParseCrHistory($tableBody, $crSid, false);
     endif;

     $outPut .= "
       <html>
         <head>
           <link rel=\"stylesheet\" type=\"text/css\" href=\"../css/phoenix.css\">
           <script src=\"../include/lib_global.js\" type=\"text/javascript\"></script>
         </head>
         <body>
           <div>
             <div class=\"f10bp1_blue\">" . $pageTitle . "&nbsp;&nbsp;&nbsp;" . $crUsrFirstname . " " . $crUsrName . "</br></br></div>
             <div><table cellspacing=\"0\" cellpadding=\"0\" vspace=\"0\" hspace=\"0\">" . $tableHeader . $tableBody . "</table></div>
           </div>
         </body>
       </html>";
  endif;
  return $outPut;
}


// TEST
//$crId = "3001" ;    // SELECT * FROM courier WHERE cr_eid = 'STHB83551'

echo crHistory($crId, array(), $fromDateRange, $toDateRange);
?>
