<?php      
/*=======================================================================
 *
 * cron_export_timematters.php
 *
  =======================================================================*/


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


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

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

   $path = getAbsoluteSystemPath();
   $passwd = "acapella7890";
   $logFile = $path . "/log/sys_auto_export.log";
   $emptyArgument = "\"\"";
   $fileNamePrefix = substr(getDateTime("6"), 0, 12);  // Format: "YmdHi"


   $year = trim($argv[2]);
     if ($year == "" || strlen($year) != 4) : $year = getDateTime("year"); endif;
   $month = trim($argv[3]);
     if ($month == "") : $month = getDateTime("month"); endif;
     $month = pad($month, "2");
   $day = trim($argv[4]);
     if ($day == "") : $day = getDateTime("day"); endif;
     $day = pad($day, "2");

   $fromYear = $year;
   $fromMonth = $month;
   $fromDay = $day;
   
   $toYear = $year;      
   $toMonth = $month;
   // $toDay = date("t",mktime(0,0,0,$toMonth,1,$toYear));
   // $toDay = getDateTime("day");
   //  $toDay = pad($toDay, "2");
   $toDay = $day;

// echo $fromYear . "-" . $fromMonth . "-" . $fromDay . " bis " . $toYear . "-" . $toMonth . "-" . $toDay . "\n\n"; // die();
         
   // Arguments
   // $hq_id $usr_id $dummy_01 $f_act $f_exp_category $f_delimiter $f_parname $f_parname_export $f_fileName $day_to $month_to $year_to $f_cs_eid_filter $day_from $month_from $year_from $f_jbp_filter $specialJbPayment $jbpc_id

   $exportPars = array();

/*
Tims Kanadische Backwaren Gesellschaft :: STB975894 :: tkbg
cs_id   emp_id  usr_id  hq_id
835103 	836184  843925   3


ADMIN
usr_id  hq_id
300001   3  admin_b
826760 	 9	admin_m
826761 	10 	admin_n
826762 	11 	admin_s
826884 	90 	admin_sl
*/

   // STB975597 (cs_id = 833194, emp_id = 834135, usr_id = 841591)   

   // $hqId = "3"; $hqName = "B"; $customerId = "835103"; $usrId = "843925"; $f_act = "export";    // STB975894
   $hqId = "3"; $hqName = "B"; $customerId = "853977"; $usrId = "870970"; $f_act = "export";    // STB981135
   $csEid = getFieldValueFromId("customer", "cs_id", $customerId, "cs_eid");
   $fileNamePrefix = $csEid . "_" . $fileNamePrefix;
   $f_headline = "1";
   $exportPars["TKBG_B_JBC"]   = array($hqId,$usrId,"_",$f_act,"120",";","TKBG_Kundenauftragsservices","TKBG_Kundenauftragsservices",$fileNamePrefix . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId,$f_headline);

   $exportParsKeys = array_keys($exportPars);
   $exportParsKeysLen = count($exportParsKeys);

   if ($exportParsKeysLen != "" && is_numeric($exportParsKeysLen)) :

      // Exports
      for ($i = 0; $i < $exportParsKeysLen; $i++) :

          list($hq_id,$usr_id,$dummy_01,$f_act,$f_exp_category,$f_delimiter,$f_parname,$f_parname_export,$f_fileName,
               $day_to,$month_to,$year_to,$f_cs_eid_filter,$day_from,$month_from,$year_from,$customerId,$f_headline) = $exportPars[$exportParsKeys[$i]];

          $cmd = "php " . $path . "/export/export.php " . $passwd . " " . $hq_id . " " . $usr_id . " " . $dummy_01 . " " . $f_act . " " . $f_exp_category . " " . "\"" . $f_delimiter. "\"" . " " 
                                                        . "\"" . $f_parname . "\"" . " " . "\"" . $f_parname_export . "\"" . " " . "\"" . $f_fileName . "\"" . " " . $day_to . " " . $month_to . " " . $year_to . " " 
                                                        . $f_cs_eid_filter . " " . $day_from . " " . $month_from . " " . $year_from . " " . $customerId . " " . $f_headline;
// echo $exportParsKeys[$i] . ":\n";
// echo $cmd . "\n\n\n";
// echo $cmd . "\n\n\n"; die();
          $cmdOutput = null;
          exec($cmd, $cmdOutput);
          // list($errNo, $errDesc) = $cmdOutput;

          // Get secret filename generated by export process automatically
          $f_secretFileName = getFieldValueFromId("exportfiles", "expf_name", $f_fileName, "expf_cryptname");
          if ($f_secretFileName != "") :

             // Send file per email to customer
             $mailSenderAddress = "support@assecutor.de";
 
             // Receiver of the mail
             $mailReceiverAddressArray = array("support@assecutor.de");  // Init
             
             $mailReceiverAddress = getFieldValueFromId("user", "usr_id", $usrId, "usr_email");
             if ($mailReceiverAddress != "") :
                $mailReceiverAddressArray = explode(",",$mailReceiverAddress);
             endif;
// $mailReceiverAddressArray = array("reimann-gellrich@stadtbote.de");  // TEST IT !!!!!!!!!!!!!!!!!!!!!!!!!
// $mailReceiverAddressArray = array("support@assecutor.de");  // TEST IT !!!!!!!!!!!!!!!!!!!!!!!!!
             $mailSubject = "Stadtbote :: Tims Kanadische Backwaren GmbH";
$mailCcAddress = "reimann-gellrich@stadtbote.de";
             $mailBccAddress = "admin@assecutor.de";
// $mailBccAddress = "";
             $mailtext = "<b>" . $mtfkKeyname . "</b><br><br>";
/*
$mailtext .= "currentDate = " . $currentDate . "<br>";
$mailtext .= "dateLastDayOfMonthAfterNextMonth = " . $dateLastDayOfMonthAfterNextMonth . "<br>";
$mailtext .= "dateLastDayOfNextMonth = " . $dateLastDayOfNextMonth . "<br>";
$mailtext .= "dateIn2Weeks = " . $dateIn2Weeks . "<br>";
$mailtext .= "dateIn1Week = " . $dateIn1Week . "<br>";
*/
             /*
             if (!$autoMode) :
                $mailtext .= "Ausgeführt von: " . $usrFirstname . " " . $usrName;
             endif;
             $mailtext .= $triggerOut;
             */
 
             $mailObj = new htmlMimeMail();
 
             // Set From address
             $mailObj->setFrom($mailSenderAddress);
 
             // Set Cc address
             if ($mailCcAddress != "") :
                $mailObj->setCc($mailCcAddress);
             endif;
 
             // Set Bcc address
             if ($mailBccAddress != "") :
                $mailObj->setBcc($mailBccAddress);
             endif;
 
             // Subject of the mail
             $mailObj->setSubject($mailSubject);
 
             // Mail text
             $mailObj->setHtml($mailtext, null, "./");
 
             if ($f_secretFileName != "") :
                if (file_exists("../export/download/" . $f_secretFileName)) :
                   $attachment = $mailObj->getFile("../export/download/" . $f_secretFileName);
                   $mailObj->addAttachment($attachment, $f_fileName, $f_mimetype . $f_charset);
                endif;
             endif;
 
             $mailResult = $mailObj->send($mailReceiverAddressArray, 'smtp');
          endif;

          $currentTime = getDateTime("0");
          writeToFile($logFile, "OP=AUTO_EXPORT|DATETIME=" . $currentTime . "|CUST=" . $exportParsKeys[$i] . "|EMAIL=" . $mailResult);

          // sleep(5);  // Avoid too many tasks
      endfor;
   endif;
endif;      
?>