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

include_once ("../include/mcglobal.inc.php");
include_once ('../include/email/htmlMimeMail.php');


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

$currentTime = getDateTime("0");


// Get jobs for autoembargo
function getDPFData() {
  global $db, $logFile, $currentTime;

  // Try to connect request server because of performance
  global $db2;
  getDb2Connection();

  $retArray = array();

  // Get all couriers with having jobs since $startTime
  $jobExportTimeInDays = 30;
  $startTime = getDateTime("datetime_plus_offset", array(0,0,0,0,-($jobExportTimeInDays),0), "Y-m-d") . " 00:00:00";
  $endTime = $currentTime;

  $sqlquery = "SELECT DISTINCT"
               . "   cmp.cmp_id, cmp.cmp_type, cmp.cmp_comp, cmp.cmp_comp2, cmp.cmp_iln, cmp.cmp_tax_idno,"
               . "   cmp.cmp_bank, cmp.cmp_bankno, cmp.cmp_bankacc, cmp.cmp_authenticated, cmp.cmp_visible,"
               . "   cmp.cmp_match, cmp.cmp_cashmode, cmp.cmp_hsno, cmp.cmp_dpf_checksum,"
               . "   cr.hq_id, cr.cr_id, cr.cr_eid, cr.hq_id,"
               . "   usr.usr_name, usr.usr_firstname, usr.usr_email, usr.usr_phone,"
               . "   usr.usr_phone2, usr.usr_fax, ad.ad_street, ad.ad_zipcode, ad.ad_city"
               . " FROM job AS jb, courier AS cr, company AS cmp, user AS usr, address AS ad"
               . " WHERE jb.jb_export_time >= '" . $startTime . "'   AND"
               . "       jb.jb_export_time <= '" . $endTime . "'   AND"
               . "       jb.cr_id = cr.cr_id    AND"
               . "       cmp.cmp_id = cr.cmp_id AND"
               . "       cmp.ad_id = ad.ad_id   AND"
               . "       cr.usr_id = usr.usr_id"
               . " ORDER BY cmp.cmp_comp";
// echo $sqlquery . "<br>";
  $result = $db2->query($sqlquery);

  if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
  while ($row = $result->fetch_assoc()):
        $retArray[] = array($row["cmp_id"], $row["cmp_comp"], $row["cmp_comp2"], $row["cmp_iln"], $row["cmp_tax_idno"], 
                            $row["usr_name"], $row["usr_firstname"], $row["usr_email"], $row["usr_phone"], 
                            $row["ad_street"], $row["cmp_hsno"], $row["ad_zipcode"], $row["ad_city"], $row["hq_id"], 
                            $row["cr_eid"], $row["cmp_dpf_checksum"], $row["cmp_authenticated"], $row["cmp_visible"]);
  endwhile;
  $result->free();

  return $retArray;
}  



// ********************************
// *** Send mails automatically ***
// ********************************

$constDPFCourierEnabled = getParameterValue("0", "DPF_CR_ENABLED", "0");  // "Meta-Global" <=> hq_id = 0
if ($constDPFCourierEnabled == '1') :

   // Log file
   $logFile = getParameterValue("0", "DPF_LOGFILE", "0");

   // Get data of the couriers to be checked
   $dataArray = getDPFData();
print_r($dataArray);
   // Loop all entries
   $lenDataArray = count($dataArray);

   // Mark all rows selected for embargo check
   // Do this here to avoid multiple rows
   for ($i = 0; $i < $lenDataArray; $i++) :
//       updateStmt("company", "cmp_id", $dataArray[$i][0], array("cmp_dpf_state", "99"), "");
   endfor;

   for ($i = 0; $i < $lenDataArray; $i++) :

       // Current data to be checked
       $cmpId = $dataArray[$i][0];
       $cmpComp = $dataArray[$i][1];
       $cmpComp2 = $dataArray[$i][2];
       $cmpILN = $dataArray[$i][3];
       $cmpTaxIdno = $dataArray[$i][4];
       $usrName = $dataArray[$i][5];
       $usrFirstname = $dataArray[$i][6];
       $usrEmail = $dataArray[$i][7];
       $usrPhone = $dataArray[$i][8];
       $adStreet = $dataArray[$i][9];
       $cmpHsno = $dataArray[$i][10];
       $adZipcode = $dataArray[$i][11];
       $adCity = $dataArray[$i][12];
       $hqId = $dataArray[$i][13];
       $eid = $dataArray[$i][14];
       $cmpDpfChecksum = $dataArray[$i][15];
       $cmpAuthenticated = $dataArray[$i][16];
       $cmpVisible = $dataArray[$i][17];

       // Define type to be checked (needed in the following INCLUDE)
       $autoembargo = "1";
       $DPFTypeToBeChecked = "cr";

       if ($cmpId != "") :

          // Start to generate the request
//          include_once ("../include/dpf.php");

          // Handling according to the result
          /*
          if ($dfpErrCode == "0") :
             $currentTime = getDateTime("0");
             if ($dfpMatchlevel == "1" || $dfpMatchlevel == "2") :
                updateStmt("company","cmp_id",$cmpId,array("cmp_dpf_state", $dfpMatchlevel, "cmp_dpf_datetime", $currentTime);
             endif;
          endif;
          */

          // Write logdata into log file
          // writeToFile($logFile, "[Job: " . $job_id . "] [Time: " . $currentTime . "] [From: " . $constMailSenderAddress . "] [To: " . $f_email . "] [SID: " . $job_crSid . "] [Erledigung] [Status: " . $statusSent . "]");
       endif;
       
       $cmpId = "";

   endfor;  // Loop all rows
endif;
?>
