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


include_once ("../include/services_func.inc.php");
include_once ("../include/mcglobal.inc.php");
if ($phpVersion >= "7.0") :
   require("../PEAR/HTML/Template/IT.php");
else :
   require("HTML/IT.php");
endif;


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

         
   // --- JOB -------------------
   
   // initialize
   $titles = "Nr.,Tourenname,Kostenstelle,Fahrz.,Bez.,Auslage,Gewicht,Auftragszeit,Reserv.,Kurier,Storno,Spät.Annahmez.,Annahme";
   $titlesArray = spliti(",",$titles);
   $titlesLength = count($titlesArray);   
   $fields = "jb_id,jb_tourname,csc_name,vht_value,csc_id_payer,jb_outlay,jb_weight,jb_ordertime,jb_reserv,cr_id,jb_storno,jb_latesttaketime,jb_taketime";
   $fieldsArray = spliti(",",$fields);
   $fieldsLength = count($fieldsArray);   
   
   $fieldClause = getDBFields("job") . "," . getDBFields("vehicletype") . "," . getDBFields("jobstatus") . "," . getDBFields("costcenter") . "," . getDBFields("jb_latesttaketime");
   $fromClause = "job AS jb, costcenter AS csc, metatype AS vht, metatype AS jbs ";
   $whereClause = "jb.hq_id = " . $hq_id . " AND ";
   $whereClause = "jb.jb_id = " . $f_jb_id . " AND ";
   $whereClause .= "(jb.csc_id = csc.csc_id) AND ";
   $whereClause .= "(jb.vht_id = vht.mt_sort AND vht.mt_type = 'vehicletype') AND ";
   $whereClause .= "(jb.jb_status = jbs.mt_sort AND jbs.mt_type = 'job_status') ";
   $orderByClause = "";             
   
   $sqlquery = generateStatement($fieldClause,$fromClause,$whereClause,$orderByClause);

   // Send request to database
   $result = $db->query($sqlquery);
   if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

   // Job-data
   $jobentry = "";
   $jobCourierId = "";
   while ($row = $result->fetch_assoc()):

          $jobentry .= "<tr>";
          $jobentry .= "<td align=\"left\">" . $titlesArray[$i] . "</td>";
          $tmpVarName = $fieldsArray[$i];
          ${$tmpVarName} = $row[$tmpVarName];
             
          // Get the current courier-ID of the job
          if ($tmpVarName == "cr_id") : $jobCourierId = $row[cr_id]; endif;             
                              
          $jobentry .= "<td bgcolor=\"" . $cellColor ."\" align=\"left\">" . $out . "</td>";
          $jobentry .= "</tr>"; 
   endwhile;
   $result->free();



   // --- TOUR ------------------
   
   // initialize
   $titles = "Firma,,Kostenstelle,Straße,PLZ,Ort,Hausnr.,Person,Tel.,Bemerk,Status";
   $titlesArray = spliti(",",$titles);
   $titlesLength = count($titlesArray);
   $fields = "tr_comp,tr_comp2,csc_name,ad_street,ad_zipcode,ad_city,tr_hsno,tr_person,tr_phone,tr_remark,tr_status";
   $fieldsArray = spliti(",",$fields);
   $fieldsLength = count($fieldsArray);   
         
   $fieldClause = getDBFields("tour") . "," . getDBFields("address") . "," . getDBFields("costcenter") . " ";
   $fromClause = "tour AS tr, address AS ad, costcenter AS csc ";
   $whereClause = "tr.jb_id = " . $f_jb_id . " AND tr.ad_id = ad.ad_id AND tr.csc_id = csc.csc_id";
   $orderByClause = "tr.tr_sort";

   $sqlquery = generateStatement($fieldClause,$fromClause,$whereClause,$orderByClause);


   // Send request to database
   $result = $db->query($sqlquery);
   if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

   // Tour-data
   $tourentry = "";
   $rowCounter = 0;
   while ($row = $result->fetch_assoc()):
         $rowCounter++;
         $tourentry .= "<td><table>";
         for ($i = 0; $i < $fieldsLength; $i++) :             

             $tourentry .= "<tr>";
             if ($rowCounter == 1) :
                $tourentry .= "<td align=\"left\">" . $titlesArray[$i] . "</td>";
             elseif ($i == floor($fieldsLength / 2)) :
                $tourentry .= "<td><img src=\"../images/arrow_right.jpg\" border=\"0\" height=\"10\" width=\"25\"></td>";
             else :
                $tourentry .= "<td>&nbsp;</td>";
             endif;
             $tmpVarName = $fieldsArray[$i];
             ${$tmpVarName} = $row[$tmpVarName];
             $out = formatOutput(${$tmpVarName},$outputFormatField[$tmpVarName]);
             
             // special formatting for tour-fields
             if ($tmpVarName == "tr_status") :
                switch($out) {
                   case "0" : $out = "offen"; break;
                   case "1" : $out = "erledigt"; break;
                };
             endif;
             
             $tourentry .= "<td bgcolor=\"" . $cellColor ."\" align=\"left\">" . $out . "</td>";
             $tourentry .= "</tr>";             
         endfor;         
         $tourentry .= "</table></td>";         
   endwhile;
   $result->free();

   // Output of the whole tour
   if ($rowCounter > 1) :
       $tpl->setCurrentBlock("tourentry");
       $tpl->setVariable("_tourentry_", $tourentry);
       $tpl->parseCurrentBlock("tourentry");	 
   endif;         



   // --- COURIER ------------------
   
   // Initialize for template-output
   $courierentry = "";
   
   if ($jobCourierId != "") :
      // initialize
      $titles = "Firma,,Name,Vorname,SID,Online";
      $titlesArray = spliti(",",$titles);
      $titlesLength = count($titlesArray);
      $fields = "cmp_comp,cmp_comp2,usr_name,usr_firstname,cr_sid,cr_available";
      $fieldsArray = spliti(",",$fields);
      $fieldsLength = count($fieldsArray);   
   
      $fieldClause = getDBFields("courier") . "," . getDBFields("company") . "," . getDBFields("user") . " ";
      $fromClause = "courier AS cr, company AS cmp, user AS usr ";
      $whereClause = "cr.cr_id = " . $jobCourierId . " AND cmp.cmp_id = cr.cmp_id AND usr.usr_id = cr.usr_id";
      $orderByClause = "";

      $sqlquery = generateStatement($fieldClause,$fromClause,$whereClause,$orderByClause);

      // Send request to database
      $result = $db->query($sqlquery);
      if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

      // Courier-data      
      $courierentry .= "<tr><td>KURIER:</td></tr>";
      while ($row = $result->fetch_assoc()):
            // $courierentry .= "<td><table>";
            for ($i = 0; $i < $fieldsLength; $i++) :             
                if ($i % 2 == 0) : $cellColor = "#BBBBFF"; $cellColor2 = "#DDDDDD"; endif;
                if ($i % 2 == 1) : $cellColor = "#CCCCFF"; $cellColor2 = "#EEEEEE"; endif;         
                if (trim($titlesArray[$i]) == "") : $titlesArray[$i] = "&nbsp;"; endif;
                $courierentry .= "<tr>";
                $courierentry .= "<td bgcolor=\"" . $cellColor2 ."\" align=\"left\">" . $titlesArray[$i] . "</td>";             
                $tmpVarName = $fieldsArray[$i];
                ${$tmpVarName} = $row[$tmpVarName];
                $out = formatOutput(${$tmpVarName},$outputFormatField[$tmpVarName]);
             
                // special formatting for tour-fields
                if ($tmpVarName == "cr_available") :
                   switch($out) {
                      case "0" : $out = "nicht online"; break;
                      case "1" : $out = "online"; break;
                   };
                endif;             
             
                $courierentry .= "<td bgcolor=\"" . $cellColor ."\" align=\"left\">" . $out . "</td>";
                $courierentry .= "</tr>";             
            endfor;         
            // $courierentry .= "</table></td>";         
      endwhile;
      $result->free();   
   endif;
   
   // Output of the courier-data
   $tpl->setCurrentBlock("courierentry");
   $tpl->setVariable("_courierentry_", $courierentry);
   $tpl->parseCurrentBlock("courierentry");	 
   
   
   
   // --- JOB-COSTS ------------------
   
   $jobcosts = "<tr><td>KOSTEN:</td></tr>";
   $jobcosts .= "<tr><td bgcolor=\"#BBBBFF\">";
   $jobCostArr = getJobCostsByID($f_jb_id);
   foreach ($jobCostArr as $costArr) {
     $jobcosts .= "Zahler: " . $costArr[0] . "<br>";
     for ($j = 0; $j < (count($costArr[1]) - 1); $j++):  
       $jobcosts .=  $costArr[1][$j][0] . ": " . number_format($costArr[1][$j][1], 2, ',', '.') . "<br>";
     endfor;
     $jobcosts .=  "<span style=\"color:red\"><i>" . $costArr[1][$j][0] . "</i></span>: ";
     $jobcosts .=  "<span style=\"color:red\"><i>" . number_format($costArr[1][$j][1], 2, ',', '.') . "</i></span>" . "<br>";
   }
   $jobcosts .= "</td></tr>";
   
   // Output of the job-costs
   $tpl->setCurrentBlock("jobcosts");
   $tpl->setVariable("_jobcosts_", $jobcosts);
   $tpl->parseCurrentBlock("jobcosts");   
   
   
   $tpl->show();    
            
// endif;
?>
   
