<?php
/*=======================================================================
 *
 * jb_list_defineoutput.inc.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/   

   include_once ("../include/image.inc.php");
   
   // Initialisation
   $sumOfSummationField = 0;
   if (!isset($summationField)) : $summationField = ""; endif;
   if (!isset($newline)) : $newline = ""; endif;
   if (!isset($emptylines)) : $emptylines = ""; endif;
   if (!isset($noColoredCells)) : $noColoredCells = ""; endif;
   if (!isset($noTableHeader)) : $noTableHeader = ""; endif;
   if (!isset($jobEditPath)) : $jobEditPath = "jobs"; endif;
   if (!isset($jobEditParameter)) : $jobEditParameter = ""; endif;

   // Header of the list
   $tableHeader = "";
   if ($noTableHeader == "") :
      $tableHeader = "<tr>";
      for ($i = 0; $i < $fieldsLength; $i++) :
          if (trim($titlesArray[$i]) == "") : $titlesArray[$i] = "&nbsp;"; endif;
          // $tableHeader .= "<td align=\"center\">" . $titlesArray[$i] . "</td>";       
          $tableHeader .= "<td align=\"center\"><a href=\"javascript:document.forms[0].jb_sort.value=" . $i . "; finishPage();\">" . $titlesArray[$i] . "</a></td>";
          
          // Check for newline ("linefeed")
          if ($newline != ""):
             $nlValue = substr($newline, $i, 1);
             if ($nlValue == "1") :
                 $tableHeader .= "</tr><tr>";
             endif;
          endif;       
      endfor;
      $tableHeader .= "</tr>";
   endif;
   
   $tpl->setCurrentBlock("tableHeader");
   $tpl->setVariable("_tableHeader_", $tableHeader);
   $tpl->parseCurrentBlock("tableHeader");   
   
   // Get date (today plus offset)
   $offsetDate = getDateTime("date_plus_offset",array(0,MASK_DATE_PLUSOFFSETDAYS,0),"Y-m-d");
   $offsetDateOut = getDateTime("date_plus_offset",array(0,MASK_DATE_PLUSOFFSETDAYS,0),"d.m.Y");
   
   // Create filter for vehicles
   $vehicleFilter = spliti(",",MASK_JOBLIST_VEHICLE_FILTER);
   $vehicleFilterLen = count($vehicleFilter);

   // Entries of the list
   $rowCounter = 0;
   $showFutureJobsFlag = TRUE;
   $showPermanentJobsFlag = TRUE;
   if (isset($filter_job_future) && $filter_job_future == "") :
      $showFutureJobsFlag = FALSE;
   endif;
   if (isset($filter_job_permanent) && $filter_job_permanent == "") :
      $showPermanentJobsFlag = FALSE;
   endif;
   while ($row = $result->fetch_assoc()):         
         $jobentry = "";

         $jbPermanent = "0";
         if ($row[jb_permanent] != "") : $jbPermanent = $row[jb_permanent]; endif;
         $jbLonghaul = "0";
         if ($row[jb_longhaul] != "") : $jbLonghaul = $row[jb_longhaul]; endif;

         // Filter for displaying jobs. Do not show:
         // - "sub-jobs" (jb_id_parent is not empty)
         // - permanent jobs if not requested
         

         // if (($jbPermanent == "0" || ($jbPermanent > 0 && $showPermanentJobsFlag)) &&
         //     ($userTypeName == "hq" || ($userTypeName == "cs" && ($row[jb_id_parent] == "" || $row[jb_id_parent] == "0" || $row[jb_id_parent] == "-1")) ) ) :         
         
         // Rules to display subjobs: 
         // - CS never see subjobs, - HQ only see subjobs if jb_status == "2" and jb_totalprice > 0 (no credit notes) because of reference to main job
         if ( ($jbPermanent == "0" || ($jbPermanent > 0 && $showPermanentJobsFlag)) &&    
              ( ($userTypeName == "hq" && 
                   ( ($row[jb_id_parent] == "" || $row[jb_id_parent] == "0" || $row[jb_id_parent] == "-1") ||          
                      ($row[jb_status] == "2" && $row[jb_totalprice] >= "0" && $row[jb_id_parent] > "0") ) ) || 
                ($userTypeName == "cs" && ($row[jb_id_parent] == "" || $row[jb_id_parent] == "0" || $row[jb_id_parent] == "-1") ) ) ):
         
            $rowCounter++;

            // Split output per date (ONLY if sorted by ORDERTIME)
            if ($showFutureJobsFlag && $row[jb_ordertime] >= $offsetDate) :
               if ($fieldsArray[$jb_sort] == "jb_ordertime" && ($row[jb_status] == "0" || $row[jb_status] == "8" || $row[jb_status] == "9")) :
                  $jobentry .= "<tr><td class=\"f8np1\" colspan=\"" . $fieldsLength . "\">";
                  $jobentry .= "<br><br>Aufträge ab dem " . $offsetDateOut;
                  $jobentry .= "</td></tr>";
                  $showFutureJobsFlag = FALSE;
               endif;
            endif;         

            // Do not generate an output for empty table rows
            $outIsNotEmpty = FALSE;

            $jobentry .= "<tr>";
            for ($i = 0; $i < $fieldsLength; $i++) :
         
                if ($rowCounter % 2 == 0 && $i % 2 == 0) : $cellColor = "#BBBBFF"; endif;
                if ($rowCounter % 2 == 0 && $i % 2 == 1) : $cellColor = "#CCCCFF"; endif;
                if ($rowCounter % 2 == 1 && $i % 2 == 0) : $cellColor = "#DDDDDD"; endif;
                if ($rowCounter % 2 == 1 && $i % 2 == 1) : $cellColor = "#EEEEEE"; endif;

                // Change colour if job is not associated with a courier (not placed into the global market)
                if ($row[jb_status] == 9 || $row[jb_status] == 8) :
                   if ($rowCounter % 2 == 0 && $i % 2 == 0) : $cellColor = "#FFBBBB"; endif;
                   if ($rowCounter % 2 == 0 && $i % 2 == 1) : $cellColor = "#FFCCCC"; endif;
                   if ($rowCounter % 2 == 1 && $i % 2 == 0) : $cellColor = "#DDDDDD"; endif;
                   if ($rowCounter % 2 == 1 && $i % 2 == 1) : $cellColor = "#EEEEEE"; endif;
                endif;
         
                // Job is for all couriers
                if ($row[jb_status] == 0 && $row[jb_type] == 1) : 
                   if ($rowCounter % 2 == 0 && $i % 2 == 0) : $cellColor = "#BBFFBB"; endif;
                   if ($rowCounter % 2 == 0 && $i % 2 == 1) : $cellColor = "#CCFFCC"; endif;
                   if ($rowCounter % 2 == 1 && $i % 2 == 0) : $cellColor = "#BBFFBB"; endif;
                   if ($rowCounter % 2 == 1 && $i % 2 == 1) : $cellColor = "#CCFFCC"; endif;
                endif;
             
                // Job is incomplete and has to be updated before invoicing
                if ($row[jb_incomplete] == 1 && $row[jb_status] == 2) :
                   if ($rowCounter % 2 == 0 && $i % 2 == 0) : $cellColor = "#FFBBBB"; endif;
                   if ($rowCounter % 2 == 0 && $i % 2 == 1) : $cellColor = "#FFCCCC"; endif;
                   if ($rowCounter % 2 == 1 && $i % 2 == 0) : $cellColor = "#FFBBBB"; endif;
                   if ($rowCounter % 2 == 1 && $i % 2 == 1) : $cellColor = "#FFCCCC"; endif;
                endif;             

                $tmpVarName = $fieldsArray[$i];
                ${$tmpVarName} = $row[$tmpVarName];

                // *** Special treatment of fields ***             
                // Job is permanent
                if ($tmpVarName == "jb_id" && $jbPermanent > 0 && ($row[jb_status] == "0" || $row[jb_status] == "1" || $row[jb_status] == "8" || $row[jb_status] == "9")) :
                   $cellColor = "#01C025";    // #01C025   #66FFCC
                endif;
                
                // Job is generated by customer
                if ($userTypeName == "hq") :
                   if ($tmpVarName == "business_volume" && $row[jb_csc_id] > 0 && ($row[jb_status] == "0" || $row[jb_status] == "1" || $row[jb_status] == "8" || $row[jb_status] == "9")) :
                      $cellColor = "#FFFF00";    // Yellow
                   endif;
                endif;
                
                // Ordertime
                if ($tmpVarName == "jb_ordertime") :
                   $tmp = ${$tmpVarName};
                   if ($row[jb_reserv] == 1) :
                       // mktime(hour,minute,second,month,day,year)
                       $latestTakeTime = LATEST_TAKETIME_IN_MINUTES;
                       if (is_numeric($row[srvpt_traveltime]) && $row[srvpt_traveltime] > 0) :
                          $latestTakeTime = $row[srvpt_traveltime];
                       endif;
                       $compareTime = date("Y-m-d H:i:s", mktime(substr($tmp,11,2),substr($tmp,14,2)-($latestTakeTime),substr($tmp,17,2),substr($tmp,5,2),substr($tmp,8,2),substr($tmp,2,2)));
                   else :
                       $compareTime = date("Y-m-d H:i:s", mktime(substr($tmp,11,2),substr($tmp,14,2)+5,substr($tmp,17,2),substr($tmp,5,2),substr($tmp,8,2),substr($tmp,2,2)));
                   endif;             
                   if ((date("Y-m-d H:i:s") > $compareTime) && ($row[jb_status] == "0" || $row[jb_status] == "8" || $row[jb_status] == "9") && $row[jb_offer] != "1") :
                      if ($rowCounter % 2 == 0 && $i % 2 == 0) : $cellColor = "#FF1133"; endif;
                      if ($rowCounter % 2 == 0 && $i % 2 == 1) : $cellColor = "#FF3333"; endif;
                      if ($rowCounter % 2 == 1 && $i % 2 == 0) : $cellColor = "#FF4444"; endif;
                      if ($rowCounter % 2 == 1 && $i % 2 == 1) : $cellColor = "#FF5555"; endif;
                   endif;                
                endif;                
                
                // Latest taketime
                if ($tmpVarName == "jb_latesttaketime") :
                   if ($row[jb_waittime] != "") :
                      ${$tmpVarName} = $row[jb_waittime];
                   else :
                      $tmp = ${$tmpVarName};
                      if ($row[jb_reserv] == 1) :
                          // mktime(hour,minute,second,month,day,year)
                          $latestTakeTime = LATEST_TAKETIME_IN_MINUTES;
                          if (is_numeric($row[srvpt_traveltime]) && $row[srvpt_traveltime] > 0) :
                             $latestTakeTime = $row[srvpt_traveltime];
                          endif;
                          ${$tmpVarName} = date("Y-m-d H:i:s", mktime(substr($tmp,11,2),substr($tmp,14,2)-($latestTakeTime),substr($tmp,17,2),substr($tmp,5,2),substr($tmp,8,2),substr($tmp,2,2)));
                      else :
                          ${$tmpVarName} = date("Y-m-d H:i:s", mktime(substr($tmp,11,2),substr($tmp,14,2)+5,substr($tmp,17,2),substr($tmp,5,2),substr($tmp,8,2),substr($tmp,2,2)));
                      endif;             
                   endif;
                   if ((date("Y-m-d H:i:s") > ${$tmpVarName}) && ($row[jb_status] == "0" || $row[jb_status] == "8" || $row[jb_status] == "9") && $row[jb_offer] != "1") :
                      if ($rowCounter % 2 == 0 && $i % 2 == 0) : $cellColor = "#FF1133"; endif;
                      if ($rowCounter % 2 == 0 && $i % 2 == 1) : $cellColor = "#FF3333"; endif;
                      if ($rowCounter % 2 == 1 && $i % 2 == 0) : $cellColor = "#FF4444"; endif;
                      if ($rowCounter % 2 == 1 && $i % 2 == 1) : $cellColor = "#FF5555"; endif;
                   endif;                
                endif;

                // All zipcodes of the tour
                if ($tmpVarName == "jb_tourzipcodes") :
                   if ($row[jb_tourzipcodes] != "" && !(strpos($row[jb_tourzipcodes], "|") === false)) :
                      $jbZipcodes = substr($row[jb_tourzipcodes], 0, strpos($row[jb_tourzipcodes], "|"));
                      $jbZipcodes = trim($jbZipcodes);
                      if (substr($jbZipcodes, -1) == ";") :    // Remove last character if ";" again
                         $jbZipcodes = substr($jbZipcodes, 0, -1);
                      endif;                      
                      $jbZipcodes = str_replace (";", ", ", $jbZipcodes);
                      ${$tmpVarName} = $jbZipcodes;
                   else :
                      ${$tmpVarName} = $row[ad_zipcode];
                      if ($row[ad2_zipcode] != "") : ${$tmpVarName} .= ", " . $row[ad2_zipcode]; endif;
                   endif;
                endif;
                
                // All cities of the tour
                if ($tmpVarName == "jb_tourcities") :
                   if ($row[jb_tourcities] != "" && !(strpos($row[jb_tourcities], "|") === false)) :
                      $jbCities = substr($row[jb_tourcities], strpos($row[jb_tourcities], "|") + 1);
                      $jbCities = trim($jbCities);
                      if (substr($jbCities, -1) == ";") :    // Remove last character if ";" again
                         $jbCities = substr($jbCities, 0, -1);
                      endif;                      
                      $jbCities = str_replace (";", ", ", $jbCities);
                      ${$tmpVarName} = $jbCities;
                   else :
                      ${$tmpVarName} = $row[ad_city];
                      if ($row[ad2_city] != "") : ${$tmpVarName} .= ", " . $row[ad2_city]; endif;
                   endif;
                endif;

                // Optional summation of one or more fields
                if ($summationField != "" && ($tmpVarName == $summationField) && is_numeric(${$tmpVarName})) :
                   // Check for row is NOT an subjob
                   if (!($row[jb_id_parent] > "0")) :
                      $sumOfSummationField += ${$tmpVarName};
                   endif;
                endif;

                // Convert output from "float" to "float_rounded_2"
                if ($tmpVarName == "jb_cr_price" && is_numeric($row[jb_cr_price])) :
                   $outputFormatField[$tmpVarName] = "float_rounded_2";
                endif;         

                $out = formatOutput(${$tmpVarName},$outputFormatField[$tmpVarName]);

                if ($tmpVarName == "jb_tourname") :
                   $out = substr($out,0,MASK_JOBLIST_LENGTH_TOURNAME);
                endif;
                if ($tmpVarName == "jb_id" || $tmpVarName == "jb_tourname") :

                   // Check job for being a subjob
                   $tmpJbId = $row[jb_id];
                   if ($row[jb_id_parent] > "0") : $tmpJbId = $row[jb_id_parent]; endif;
                   
                   $out = "<a href=\"javascript:popupWindow('../" . $targetPath . "/jb_detail.php?job_id=" . 
                          ec($tmpJbId) . "&dbhistory=" . $dbhistory . $jobEditParameter . "','Auftrag','');\">" . $out . "</a>";                   
                endif;
                if ($tmpVarName == "jb_storno") :
                   if (($row[jb_export_time] == "" || $row[jb_export_time] == "0000-00-00 00:00:00") &&
                       ($row[jb_id_parent] == "" || $row[jb_id_parent] == "0" || $row[jb_id_parent] == "-1")) :

                      if (${$tmpVarName} == "" || ${$tmpVarName} == "0") :
                
                         // if ($row[jb_status] == 0 || $row[jb_status] == 9) :
                            $out = "<a href=\"javascript:makeStorno(" . $row[jb_id] . "," . $jbPermanent . ",'" . $row[cr_id] . "');\">" . "Storno" . "</a>";
                         // else :
                         //    $out = "";
                         // endif;
                      else :
                         if ($rowCounter % 2 == 0 && $i % 2 == 0) : $cellColor = "#FF2222"; endif;
                         if ($rowCounter % 2 == 0 && $i % 2 == 1) : $cellColor = "#FF3333"; endif;
                         if ($rowCounter % 2 == 1 && $i % 2 == 0) : $cellColor = "#FF4444"; endif;
                         if ($rowCounter % 2 == 1 && $i % 2 == 1) : $cellColor = "#FF5555"; endif;                   
                   
                         if (${$tmpVarName} == "1") :
                            $out = "St.m.K.";
                         elseif (${$tmpVarName} == "2") :
                            $out = "St.o.K.";
                         endif;
                      endif;
                   else :
                      $out = "";
                   endif;
                endif;
                // Only jobs generated by customers or jobs generated by headquarters can be modified
                if (($userTypeName == "hq" || ($row[jb_csc_id] > 0 && MASK_JB_CS_EDITJOB == "1")) && $tmpVarName == "jb_edit") :    // $userTypeName == "hq" &&
                   if ($row[jb_export_time] == "" || $row[jb_export_time] == "0000-00-00 00:00:00") :
                      // Check job for being a subjob
                      $tmpJbId = $row[jb_id];
                      if ($row[jb_id_parent] > "0") : $tmpJbId = $row[jb_id_parent]; endif;
                       
                      $out = "<a href=\"../" . $jobEditPath . "/job_edit.php?jb_id=" . ec($tmpJbId) . $jobEditParameter . "\" target=\"_blank\">" . $row[jb_id] . "</a>";
                      // if ($row[jb_id_parent] == "-1") :
                      //    $out = "*&nbsp;" . $out . "&nbsp;*";
                      // endif;
                      if ($row[jb_id_parent] > "0") :
                         $out = "*&nbsp;" . $out . "&nbsp;*";
                      endif;                      
                   else :
                      $out = "";
                      if ($row[jb_id_parent] == "-1") :
                         $out = "*&nbsp;HA&nbsp;*";
                      endif;
                      if ($row[jb_id_parent] > "0") :
                         $out = "*&nbsp;UA&nbsp;*";
                      endif;
                   endif;
                endif;
                if ($userTypeName == "hq" && $tmpVarName == "cr_sid") :
                   // Check for subjob, if it is then no output ...
                   if ($row[jb_id_parent] == "" || $row[jb_id_parent] == "0" || $row[jb_id_parent] == "-1") :
                      if (($row[jb_export_time] == "" || $row[jb_export_time] == "0000-00-00 00:00:00") &&
                          ($jbLonghaul == "0" || ($jbLonghaul == "1" || $jbLonghaul == "3") && authCheckEmployeeRights($emp_id, "10")) ) :
                         if ($row[cr_sid] == "") :
                            $out = "<a href=\"javascript:mk_cr_id_list('". $row[jb_id] . "','" . $row[ad_zipcode] . "','" . $f_hq_id_string . "');\">" . "???" . "</a>";
                         else :
                            $out = "<a href=\"javascript:mk_cr_id_list('". $row[jb_id] . "','" . $row[ad_zipcode] . "','" . $f_hq_id_string . "');\">" . $row[cr_sid] . "</a>";
                            
                            // Show red brackets if at least one stop of the tour (the first one) is done (ONLY taken jobs!)
                            if ($row[jb_status] == 1) :
                               if ($row[tr_status] == "1") :
                                  $out = "<span class=\"f8np1_red\">(</span>" . $out . "<span class=\"f8np1_red\">)</span>";
                               endif;
                            endif;
                            
                            if ($row[jb_status] == 2) :
                               $out = $row[cr_sid];
                            endif;
                            
                            // Show alert if time between the legs of the tour
                            if (MASK_JOBLIST_ALERT_LEG_TIME_SECONDS != "" && is_numeric(MASK_JOBLIST_ALERT_LEG_TIME_SECONDS)) :
                               $maxDeltaLegTime = MASK_JOBLIST_ALERT_LEG_TIME_SECONDS;
                               $deltaTime1 = ($row[time2] - $row[time1]);
                               $deltaTime2 = ($row[time3] - $row[time2]);
                               $deltaTime3 = ($row[time4] - $row[time3]);
                               $deltaTime4 = ($row[time5] - $row[time4]);
                               $deltaTime5 = ($row[time6] - $row[time5]);
                               if ( (($deltaTime1 < $maxDeltaLegTime) && $deltaTime1 > 0) || (($deltaTime2 < $maxDeltaLegTime) && $deltaTime2 > 0) ||
                                    (($deltaTime3 < $maxDeltaLegTime) && $deltaTime3 > 0) || (($deltaTime4 < $maxDeltaLegTime) && $deltaTime4 > 0) ||
                                    (($deltaTime5 < $maxDeltaLegTime) && $deltaTime5 > 0) ) :
                                    $out .= "<span class=\"f8np1_red\">!</span>";
                               endif;
                            endif;
                         endif;
                      else :
                         if (($jbLonghaul == "1" || $jbLonghaul == "3") && !authCheckEmployeeRights($emp_id, "10")) :
                            $out = "FT";  // Longhaul job not to be mediated by employees without mandatory right ("FT" = "Ferntour")
                         else :
                            $out = $row[cr_sid];
                         endif;
                      endif;
                      // Check job is an offer
                      if ($row[jb_offer] == "1") :
                         $out = "Angebot";
                      endif;
                   else :
                     $out = $row[cr_sid];
                   endif;
                endif;

                if ($tmpVarName == "vht_value" && $row[jb_cr_filter] != "") :
                   for ($j = 0; $j < $vehicleFilterLen; $j++) :
                       if (!(strpos($row[jb_cr_filter], $vehicleFilter[$j]) === FALSE)) :
                          $out .= " " . $vehicleFilter[$j];
                       endif;
                   endfor;
                endif;

               // Set the colour of the credit notes
               if ($userTypeName == "hq" && MASK_JOBLIST_CREDITNOTE_COLOUR == "1") :
                  if (($tmpVarName == "business_volume" && is_numeric($row[business_volume]) && $row[business_volume] < 0) || 
                      ($tmpVarName == "jb_cr_price" && is_numeric($row[jb_cr_price]) && $row[jb_cr_price] < 0)) :
                     $out = "<span class=\"f8np1_red\">" . $out . "</span>";
                  endif;
               endif;

                // Customer-sign
                if ($tmpVarName == "tour_1_sign" || $tmpVarName == "tour_2_sign" || $tmpVarName == "tour_3_sign" || 
                    $tmpVarName == "tour_4_sign" || $tmpVarName == "tour_5_sign" || $tmpVarName == "tour_6_sign") :
                   if ($row[tr_sign] != "") :
                      $rawCoord = splitRawCoordinates ($row[$tmpVarName]);
                      $maxCoord = checkMaxCoordinates($rawCoord);

                      $out = "<img src=\"../admin/jb_detail_sign2.php?tr_sign=" . $row[$tmpVarName] . "\" border=\"0\" height=\"" . $maxCoord[1] . "\" width=\"" . $maxCoord[0] . "\">";
                   endif;
                endif;

                if (trim($out) != ""):
                   $outIsNotEmpty = TRUE;
                endif;

                $jobentry .= "<td " . ($noColoredCells == "" ? "bgcolor=\"" . $cellColor . "\"" : "") . " align=\"" . setAlign($alignsArray[$i]) . "\" valign=\"" . setAlign($valignsArray[$i]) . "\">&nbsp;" . ($cellTextPreArray[$i] != "" ? $cellTextPreArray[$i] : "")  . $out . "&nbsp;</td>";

                // Check for newline ("linefeed")
                if ($newline != ""):
                   $nlValue = substr($newline, $i, 1);
                   if ($nlValue == "1") :
                       $jobentry .= "</tr><tr>";
                   endif;
                endif;
            endfor;
            $jobentry .= "</tr>";

            // Check for newline ("linefeed")
            if (is_numeric($emptylines) && $emptylines >= "1"):
               for ($k = 1; $k <= $rowArrayLen; $k++) :
                  $jobentry .= "<tr><td>&nbsp;<br>&nbsp;</td></tr>";
               endfor;
            endif;

            // Do not generate an output for emty table rows
            if ($outIsNotEmpty == FALSE) :
                $jobentry = "";
            endif;

            $tpl->setCurrentBlock("jobentry");
            $tpl->setVariable("_jobentry_", $jobentry);
            $tpl->parseCurrentBlock("jobentry");
         
         endif;  // Filter for displaying jobs
   endwhile;      
?>