<?php     

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



// Get jobs 
function getJobs4Jobprice($startDay) {
  global $db, $logFile;
  $retArray = array();

  $currentTime = getDateTime("0");

  $startTime = getDateTime("datetime_plus_offset", array(0,0,0,0,-($startDay),0), "Y-m-d H:i:s");
  $endTime = $currentTime;

  // Get jobs of the specified customer
  $sqlquery = "SELECT jb.jb_id, jb.hq_id, jb.jb_status, jb.jb_finishtime, jb.jb_totalprice, jb.cr_id, jb.cr_sid, jbprc_14.jbprc_price AS jbprc_price_14"
            . "  FROM job AS jb LEFT JOIN jobprice AS jbprc_14 ON jbprc_14.jb_id = jb.jb_id AND jbprc_14.mt_sort = '14',"
            . "       couriervehicle AS crvh"
            . "  WHERE jb.jb_finishtime > '" . $startTime . "'      AND"
            . "        jb.jb_finishtime <= '" . $endTime . "'       AND"
            . "        (ISNULL(jb.jb_storno) OR jb.jb_storno = '0') AND"
            . "        jb.jb_status = '2'                           AND"
            . "        jb.cr_id = crvh.cr_id                        AND"
            . "        jb.cr_sid = crvh.crvh_sid                       "
            . " ORDER BY jb.jb_id";
// echo $sqlquery . "\n\n";
// die();
  $result = $db->query($sqlquery);
  // $result = $db->query($sqlquery . " UNION " . $sqlquery2);
  if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
  while ($row = $result->fetch_assoc()):
        if ($row["jbprc_price_14"] == "" && $row["cr_id"] != "" && $row["cr_id"] > "0" && $row["cr_sid"] != "") :
           $retArray[$row["jb_id"]]["jb_status"]          = $row["jb_status"];           
           $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"]]["jb_finishtime"]      = $row["jb_finishtime"];
           $retArray[$row["jb_id"]]["jb_totalprice"]      = $row["jb_totalprice"];
        endif;
  endwhile;
  $result->free();
  return $retArray;
} 


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

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

   $path = getAbsoluteSystemPath();
   $passwd = "acapella7890";
   $logFile = $path . "/log/cron_jobprice.log";
   $logDebug = true;
   $emptyArgument = "\"\"";
   $currDatetime = getDateTime("0");  // Format: "YmdHi"
   $maxNumOfDays = 100;

   $numOfDays = $argv[2];
   if ($numOfDays == "" || !is_numeric($numOfDays) || $numOfDays <= 0  | $numOfDays > $maxNumOfDays) :
      $numOfDays = 2;
   endif;

   // Get jobs 
   $jbArr = getJobs4Jobprice($numOfDays);
   $jbkArr = array_keys($jbArr);
   $jbkArrLen = count($jbkArr);
   $sleepCount = 0;
   for ($i = 0; $i < $jbkArrLen; $i++) :

       $jbId = $jbkArr[$i];
       $jbFinishtime = $jbArr[$jbId]["jb_finishtime"];
       $jbTotalprice = $jbArr[$jbId]["jb_totalprice"];

       insertJobpriceByVehicleFieldvalue($jbId, "crvh_partner_commission", "14");

       if ($logDebug) :
          // Get (new) KM and CO2 values
          $pp = getFieldValueFromClause("jobprice","jbprc_price","jb_id = '" . $jbId . "' AND mt_sort = '14'");

          writeToFile($logFile, $currDatetime . " :: jb_id=" . $jbId . " :: jb_finishtime=" . $jbFinishtime . " :: pp=" . $pp . "    [jb_totalprice=" . $jbTotalprice . "]");
          // writeToFile($logFile, "------------------------------------------------------------------------------------");
          echo $jbId . " :: " . $pp . "\n";
       endif; 
       
       $sleepCount++;
       if ($sleepCount == 5) :
          $sleepCount = 0;
          sleep(1);
       endif;
   endfor;
endif;
?>



