<?php      
/*=======================================================================
 *
 * cron_job_to_job2.php
 *
  =======================================================================*/


include_once ("../include/mcglobal.inc.php");
include_once ("../include/html.inc.php");
include_once ("../include/jb_detail_history.inc.php");


// Execution-Time for script
set_time_limit(30000);


/// Because of cron mode
function authCheckEmployeeRights($emp_id, $menuModeId, $referer = "") {
  return true;
}


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

   $dbh_jb = "job";
   $dbh_tr = "tour";
   $dbh_trs = "tourservice";
   $dbh_log = "log";
   $dbh_cdt = "courierdatatransfer";
   $dbh_trat = "tourarticle";
   $dbh_tratp = "tourarticleprocess";
   $dbh_jbc = "jobcalculator";
   $dbh_jbp = "jobpayment";

   $dateYesterday = getDateTime("date_plus_offset", array(0,-3,0), "Y-m-d");
   $currentTime = date("Y-m-d H:i:s");
   // $currentClientIP = trim($_SERVER['REMOTE_ADDR']);
   $currentClientIP = "";

   // $path = getAbsoluteSystemPath();
   $path = "../log/";
   $logFilename = $path . "export_job_to_job2.log";
   writeToFile($logFilename, $currentTime);


   // Get exported jobs NOT existing in job2
   $sqlquery = "SELECT jb.jb_id FROM job AS jb"
             . " WHERE (NOT ISNULL(jb.jb_export_time)) AND"
             . "       jb.jb_export_time >= '" . $dateYesterday . " 00:00:00' AND"
             . "       jb.jb_export_time != '9999-12-31 23:59:59' AND"
             . "       (NOT EXISTS (SELECT * FROM job2 AS jb2 WHERE jb2.jb_id = jb.jb_id))"
             . " ORDER BY jb.jb_id";

   $result = $db->query($sqlquery);
   if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
   $arrayOfExportedIds = array();
   while ($row = $result->fetch_assoc()):
         $arrayOfExportedIds[] = $row["jb_id"];
   endwhile;
   $result->free();


   // Write historic job data
   $arrayOfExportedIdsLen = count($arrayOfExportedIds);
   if ($arrayOfExportedIdsLen > 0) :

      $f_act = "";
      for ($x = 0; $x < $arrayOfExportedIdsLen; $x++) :
// if ($x == 1) : break; endif;
          $job_id = $arrayOfExportedIds[$x];
          $tmpDelimiter = "-;-";

          // [1.] Store view of the headquarters and store raw data of the job (also headquarters view)
          $jobentry = ""; $tourentry = ""; $jobcosts = ""; $courierentry = ""; $employeeentry = "";
          $userTypeName = "hq";
          $tmpJobDataArray = getJobSnapshot($job_id, "1");
          $currentJobRawData = $tmpJobDataArray[0];
          $currentJobParameterState = $tmpJobDataArray[1];
          $currentJobRawData = str_replace('\'', '\\\'', $currentJobRawData);
          $jbHistoryData = $currentJobParameterState . $tmpDelimiter . $jobentry . $tmpDelimiter . $tourentry
                         . $tmpDelimiter . $jobcosts . $tmpDelimiter . $courierentry . $tmpDelimiter . $employeeentry;
          $jbHistoryData = str_replace('\'', '\\\'', $jbHistoryData);

          // $sqlquery = "UPDATE job SET jb_history_data = '" . $jbHistoryData . "' WHERE jb_id = '" . $job_id . "'";
          // $result = $db->query($sqlquery);
          // if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

          $currentJobRawData = escapeMysqlForBinary(compress($currentJobRawData));
          $jbHistoryData = escapeMysqlForBinary(compress($jbHistoryData));

          if (existsEntry("job2",array("jb_id",$job_id))) :
              updateStmt("job2", "jb_id", $job_id, array("jb2_history_rawdata", $currentJobRawData));
          else:
              insertStmt("job2", array("jb_id", $job_id, "jb2_history_rawdata", $currentJobRawData));
          endif;
          if (existsEntry("job2",array("jb_id",$job_id))) :
              updateStmt("job2", "jb_id", $job_id, array("jb2_history_hq", $jbHistoryData));
          else:
              insertStmt("job2", array("jb_id", $job_id, "jb2_history_hq", $jbHistoryData));
          endif;
          

          // [2.] Store view of a mail
          $jobentry = ""; $tourentry = ""; $jobcosts = ""; $courierentry = ""; $employeeentry = "";
          $userTypeName = "hq";

          $f_act = "export";
          $currentJobParameterState = getJobSnapshot($job_id);
          $f_act = "";
          $jbHistoryData = $currentJobParameterState . $tmpDelimiter . $jobentry . $tmpDelimiter . $tourentry
                         . $tmpDelimiter . $jobcosts . $tmpDelimiter . $courierentry . $tmpDelimiter . $employeeentry;
          $jbHistoryData = str_replace('\'', '\\\'', $jbHistoryData);

          $jbHistoryData = escapeMysqlForBinary(compress($jbHistoryData));

          if (existsEntry("job2",array("jb_id",$job_id))) :
              updateStmt("job2", "jb_id", $job_id, array("jb2_history_mail", $jbHistoryData));
          else:
              insertStmt("job2", array("jb_id", $job_id, "jb2_history_mail", $jbHistoryData));
          endif;
          

          // [3.] Store view of the customer
          $jobentry = ""; $tourentry = ""; $jobcosts = ""; $courierentry = ""; $employeeentry = "";
          $userTypeName = "cs";
          $currentJobParameterState = getJobSnapshot($job_id);
          $jbHistoryData = $currentJobParameterState . $tmpDelimiter . $jobentry . $tmpDelimiter . $tourentry
                         . $tmpDelimiter . $jobcosts . $tmpDelimiter . $courierentry . $tmpDelimiter . $employeeentry;
          $jbHistoryData = str_replace('\'', '\\\'', $jbHistoryData);                  

          $jbHistoryData = escapeMysqlForBinary(compress($jbHistoryData));   

          if (existsEntry("job2",array("jb_id",$job_id))) :
              updateStmt("job2", "jb_id", $job_id, array("jb2_history_cs", $jbHistoryData));
          else:
              insertStmt("job2", array("jb_id", $job_id, "jb2_history_cs", $jbHistoryData));
          endif;
          
          writeToFile($logFilename, $job_id);
      endfor;
   endif;
   
   writeToFile($logFilename, "----------------------------------------------------------------------------------------------");
endif;      
?>