<?php     

/*
mc_cron_km_co2e_special_cs.php

MEGA, DHL
*/


include_once("../locating/xServer.inc.php"); 
include_once ("../tools/co2e_sendReq.php");


function dateCheck($date, $format = 'Y-m-d H:i:s') {
  $d = DateTime::createFromFormat($format, $date);
  return $d && $d->format($format) == $date;
}


// Get jobs for auto_response
function getJobsWithoutKM ($csEidArr = array(), $startTime = "2023-01-01 00:00:00", $format = 'Y-m-d H:i:s') {
  global $db, $outFile;
  $retArray = array();

  if (is_array($csEidArr) && count($csEidArr) > 0) :

     $csEidArrLen = count($csEidArr);

     // $startTime = getDateTime("datetime_plus_offset", array(0,0,0,0,-($startDay),0), "Y-m-d H:i:s");
     if (dateCheck($startTime, $format)) :

        $currentTime = getDateTime("0");
// echo "startTime = " . $startTime . " \n";
// echo "currentTime = " . $currentTime . " \n";
// echo "customerStr = ('" . implode("','", $csEidArr) . "') \n";
        // Get jobs of the specified customer
        $sqlquery = "SELECT cs.cs_eid, cs.cs_id, jb.jb_id, jb.hq_id, jb.jb_status, jb.jb_finishtime, jb.cr_id, jb.cr_sid, jbprc_11.jbprc_price AS jbprc_price_11,"
                  . "       LEFT(gdc_arr_tour.gdc_content,14) AS gdc_arr_tour_content, mt.mt_value AS mt_vht_order, mt2.mt_value AS mt_vht_real, tr.tr_id, tr.tr_status, tr.tr_finishtime, tr.tr_signname"
                  . "  FROM customer AS cs, costcenter AS csc,"
                  . "       job AS jb LEFT JOIN jobprice AS jbprc_11 ON jbprc_11.jb_id = jb.jb_id AND jbprc_11.mt_sort = '11',"
                  . "       tour AS tr LEFT JOIN genericdatacontainer AS gdc_arr_tour ON gdc_arr_tour.gdc_obj_id = tr.tr_id AND gdc_arr_tour.gdc_obj_type = 'tr' AND gdc_arr_tour.gdc_gen_fieldname = 'arr_tour',"
                  . "       metatype AS mt, metatype AS mt2"
                  . "  WHERE cs.cs_eid IN ('" . implode("','", $csEidArr) . "') AND"
                  . "        csc.cs_id = cs.cs_id                               AND"
                  . "        jb.csc_id_payer = csc.csc_id                       AND"
                  . "        jb.jb_finishtime >= '" . $startTime . "'           AND"
                  . "        jb.jb_finishtime <= '" . $currentTime . "'         AND"
                  . "        (ISNULL(jb.jb_storno) OR jb.jb_storno = '0')       AND"
                  . "        jb.jb_status = '2'                                 AND"
                  . "        jb.jb_id = tr.jb_id                                AND"
                  . "        tr.tr_sort = '1'                                   AND"
                  . "        tr.tr_status = '1'                                 AND"
                  . "        jb.vht_id = mt.mt_sort                             AND"
                  . "        mt.mt_type = 'vehicletype'                         AND"
                  . "        jb.vht_id_real = mt2.mt_sort                       AND"
                  . "        mt2.mt_type = 'vehicletype'                           "
                  . " ORDER BY jb.jb_id";
        // echo "\n" . $sqlquery . " \n\n";
        // die();
        $result = $db->query($sqlquery);
        if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
        while ($row = $result->fetch_assoc()):
              if ($row["jbprc_price_12"] == '' || $row["jbprc_price_12"] == '0') :
                 $retArray[$row["jb_id"]]["cs_eid"]               = $row["cs_eid"];
                 $retArray[$row["jb_id"]]["cs_id"]                = $row["cs_id"];
                 $retArray[$row["jb_id"]]["jb_status"]            = $row["jb_status"];
                 $retArray[$row["jb_id"]]["jb_finishtime"]        = $row["jb_finishtime"];
                 $retArray[$row["jb_id"]]["mt_vht_order"]         = $row["mt_vht_order"];
                 $retArray[$row["jb_id"]]["mt_vht_real"]          = $row["mt_vht_real"];
                 $retArray[$row["jb_id"]]["cr_id"]                = $row["cr_id"];
                 $retArray[$row["jb_id"]]["cr_sid"]               = $row["cr_sid"];
                 $retArray[$row["jb_id"]]["hq_id"]                = $row["hq_id"];
                 $retArray[$row["jb_id"]]["crvh_payload"]         = $row["crvh_payload"];
                 $retArray[$row["jb_id"]]["jbprc_price_11"]       = $row["jbprc_price_11"];
                 $retArray[$row["jb_id"]]["gdc_arr_tour_content"] = $row["gdc_arr_tour_content"];
                 $retArray[$row["jb_id"]]["tr_id"]                = $row["tr_id"];
                 $retArray[$row["jb_id"]]["tr_status"]            = $row["tr_status"];
                 $retArray[$row["jb_id"]]["tr_finishtime"]        = $row["tr_finishtime"];
                 $retArray[$row["jb_id"]]["tr_signname"]          = $row["tr_signname"];
                 $retArray[$row["jb_id"]]["tr_sign"]              = $row["tr_sign"];
              endif;
        endwhile;
        $result->free();
     else :
        echo "Wrong date! \n";
     endif;
  else :
     echo "No customer specified! \n";
  endif;
  return $retArray;
} 


if ($argv[1] == "acapella7890") :

   // Error reporting
   error_reporting(E_ERROR | E_WARNING | E_PARSE);

   $csIdString = $argv[2];

   if ($csIdString != "") :

      $csEidArr = explode("|", $csIdString);

      $path = getAbsoluteSystemPath();
      $passwd = "acapella7890";
      $outFile = $path . "/temp/cs_km.csv";
      $writeCSV = true;
      $emptyArgument = "\"\"";
      $currDatetime = getDateTime("0");

      $startTime = $argv[3];
      if ($startTime == "" || !dateCheck($startTime)) :
         $startTime = "2023-01-01 00:00:00";
      endif;

      // Write CSV header
      if ($writeCSV) :
         writeToFile($outFile, "EID;Auftrag;Ankunftszeit;Abholzeit;Erledigungszeit;Angeforderter Fzg.-Typ;Gefahrener Fzg.-Typ;SID;KM");
      endif;

      // Get jobs 
      $jbArr = getJobsWithoutKM($csEidArr, $startTime);
      $jbkArr = array_keys($jbArr);
      $jbkArrLen = count($jbkArr);
      for ($i = 0; $i < $jbkArrLen; $i++) :

          $jbId = $jbkArr[$i];
          $km  = $jbArr[$jbId]["jbprc_price_11"];
          $km_osm = "0";
          if ($km == "" || !is_numeric($km)) : 
             list($km, $km_osm) = get_job_distance($jbId);
          endif;
          $km = str_replace(".", ",", $km);

          if ($writeCSV) :
             // Format arrival time             
             $arrivalTime = "";
             $tmpA = $jbArr[$jbId]["gdc_arr_tour_content"];
             if ($tmpA != "") :
                $arrivalTime = substr($tmpA, 0, 4) . "-" . substr($tmpA, 4, 2) . "-" . substr($tmpA, 6, 2) . " " . substr($tmpA, 8, 2) . ":" . substr($tmpA, 10, 2) . ":" . substr($tmpA, 12, 2);
             endif;
          
             writeToFile($outFile, $jbArr[$jbId]["cs_eid"] . ";" . $jbId . ";" . $arrivalTime . ";" . $jbArr[$jbId]["tr_finishtime"] . ";" . $jbArr[$jbId]["jb_finishtime"] . ";" . $jbArr[$jbId]["mt_vht_order"] . ";" . $jbArr[$jbId]["mt_vht_real"] . ";" . $jbArr[$jbId]["cr_sid"] . ";" . $km);
          endif;
      endfor;
   else :
      echo "Please specify customer! [Format : \"EID_01|EID_02|EID_03|EID_04....\"]";
   endif;
endif;

// ******************************************************

// Call for one or more customers
/*
MEGA
php mc_cron_km_co2e_special_cs.php acapella7890 "STB914191|STB971150|STB971160|STB952079|STB952170|STB952171|STB980302|STB980915|STB919040|STB975016|STB971199|STB971488|STB971606|STB971913|STB924814|STB973018|STB973681|STB973722|STB974502|STB975058|STB975237|STB978411|STB976635"

DRK
php mc_cron_km_co2e_special_cs.php acapella7890 "STB920480|STB979048|STB976569|STB920783|STB912045|STB981117|STB972921|STB973302|STB917750|STB979131|STB976483|STB979801|STB965143|STB976060|STB976360|STB976370"
*/

// ******************************************************

// Call for single job
// sendReqCO2("21453716");
?>



