<?php      
/*=======================================================================
 *
 * cron_export_cs_jb_email_csv.php
 *
  =======================================================================*/


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


$currentTime = getDateTime("0");
$constExpcId = 120;

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"

   // Get customer from call
   $customerId  = trim($argv[2]);   
   if ($customerId != "" && is_numeric($customerId)) :
      $csEid = getFieldValueFromId("customer", "cs_id", $customerId, "cs_eid");
   endif;
   if ($csEid != "") :
      $hqId = getFieldValueFromId("customer", "cs_id", $customerId, "hq_id");
      // $hqName = getFieldValueFromId("headquarters", "hq_id", $hqId, "hq_mnemonic");
      $empId = getFieldValueFromId("customer", "cs_id", $customerId, "cs_admin");
      $usrId = getFieldValueFromId("employee", "emp_id", $empId, "usr_id");
      
      // Name of the export parameter form
      $parCsExportParName = getParameterValue("0", "EXPORT_PAR_NAME_" . $constExpcId . "_CS_" . $customerId, "0");

      $mode = trim($argv[3]);
      if ($mode == "") :

         $fromYear = trim($argv[4]);
           if ($fromYear == "" || strlen($fromYear) != 4 || !is_numeric($fromYear)) : $fromYear = getDateTime("year"); endif;
         $fromMonth = trim($argv[5]);
           if ($fromMonth == "" || !is_numeric($fromMonth) || $fromMonth < 1 || $fromMonth > 12) : $fromMonth = getDateTime("month"); endif;
           $fromMonth = pad($fromMonth, "2");
         $fromDay = trim($argv[6]);
           if ($fromDay == "" || !is_numeric($fromDay) || $fromDay < 1 || $fromDay > 31) : $fromDay = getDateTime("day"); endif;
           $fromDay = pad($fromDay, "2");
         $toYear = trim($argv[7]);
           if ($toYear == "" || strlen($toYear) != 4 || !is_numeric($toYear)) : $toYear = getDateTime("year"); endif;
         $toMonth = trim($argv[8]);
           if ($toMonth == "" || !is_numeric($toMonth) || $toMonth < 1 || $toMonth > 12) : $toMonth = getDateTime("month"); endif;
           $toMonth = pad($toMonth, "2");
         $toDay = trim($argv[9]);
           if ($toDay == "" || !is_numeric($toDay) || $toDay < 1 || $toDay > 31) : $toDay = getDateTime("day"); endif;
           $toDay = pad($toDay, "2");

      elseif ($mode == "yesterday") :

         $fromYear = getDateTime("year_yesterday");
         $fromMonth = getDateTime("month_yesterday");
         $fromDay = getDateTime("day_yesterday");
         $toYear = getDateTime("year_yesterday");
         $toMonth = getDateTime("month_yesterday");
         $toDay = getDateTime("day_yesterday");

      elseif ($mode == "currentmonth") :

         $currentMonthFrom = getDateTime("date_firstday_currentmonth_currentyear");
         $fromYear = substr($currentMonthFrom,0,4);
         $fromMonth = substr($currentMonthFrom,5,2); 
         $fromDay = substr($currentMonthFrom,8,2);
         $currentMonthTo = getDateTime("date_lastday_currentmonth_currentyear");
         $toYear = substr($currentMonthTo,0,4);
         $toMonth = substr($currentMonthTo,5,2); 
         $toDay = substr($currentMonthTo,8,2);

      elseif ($mode == "lastmonth") :

         $lastMonthFrom = getDateTime("date_firstday_lastmonth_currentyear");
         $fromYear = substr($lastMonthFrom,0,4);
         $fromMonth = substr($lastMonthFrom,5,2); 
         $fromDay = substr($lastMonthFrom,8,2);
         $lastMonthTo = getDateTime("date_lastday_lastmonth_currentyear");
         $toYear = substr($lastMonthTo,0,4);
         $toMonth = substr($lastMonthTo,5,2); 
         $toDay = substr($lastMonthTo,8,2);
      endif;

// $toDay = date("t",mktime(0,0,0,$toMonth,1,$toYear));
// $toDay = pad($toDay, "2");

// echo $parCsExportParName . " :: " . $customerId . " :: " . $csEid . " :: " . $hqId . " :: " . $usrId . "\n\n";
// echo $fromYear . "-" . $fromMonth . "-" . $fromDay . " => " . $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();

      $f_act = "export";
      $fileNamePrefix = $csEid . "_" . $fileNamePrefix;
      $exportPars["GEN_CS_JBC"]   = array($hqId,$usrId,"_",$f_act,$constExpcId,";",$parCsExportParName,$parCsExportParName,$fileNamePrefix . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);

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

      if ($parCsExportParName != "" && $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_jbp_filter,$specialJbPayment,$jbpc_id) = $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_jbp_filter . " " . $specialJbPayment . " " . $jbpc_id;
// 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 = getParameterValue("0", "MAIL_SENDER_ADDRESS", $hqId);
                if ($mailSenderAddress == "") :
                   $mailSenderAddress = "support@assecutor.de";
                endif;

                // 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("support@assecutor.de");  // TEST IT !!!!!!!!!!!!!!!!!!!!!!!!!
// $mailReceiverAddressArray = array("reimann-gellrich@stadtbote.de");  // TEST IT !!!!!!!!!!!!!!!!!!!!!!!!!

                $mailSubject = getParameterValue("0", "EXPORT_MAIL_SUBJECT_CS_" . $customerId, "0");
                if ($mailSubject == "") :
                   $mailSubject = getLngt("Ihre Transportaufträge");
                endif;
                $mailCcAddress = "";
                $mailBccAddress = "support@assecutor.de";


                // Mail content

                $mailCssFontType = getParameterValue("0", "MAIL_CSS_FONT_TYPE", $currentHqId);
                if ($mailCssFontType == "") : $mailCssFontType = "Verdana, Arial, Helvetica, sans-serif"; endif;

                $mailtext  = "<html><head><meta text/html>";
                $mailtext .= "<style type=\"text/css\">";
                $mailtext .= ".f8np1 { font-family: " . $mailCssFontType . "; font-size: 8pt; font-weight: normal; padding: 1px}";
                $mailtext .= ".f8bp1 { font-family: " . $mailCssFontType . "; font-size: 8pt; font-weight: bold; padding: 1px}";
                $mailtext .= ".f10np1 { font-family: " . $mailCssFontType . "; font-size: 10pt; font-weight: normal; padding: 1px}";
                $mailtext .= ".f10bp1 { font-family: " . $mailCssFontType . "; font-size: 10pt; font-weight: bold; padding: 1px}";
                $mailtext .= ".f8np1_red { font-family: " . $mailCssFontType . "; font-size: 8pt; font-weight: normal; padding: 1px; color: #FF0000;}";
                $mailtext .= "</style>";
                $mailtext .= "</head><body>";

                $mailPreSalutationText = getParameterValue("0", "MAIL_PRE_SALUTATION_TEXT", $hqId);
                if ($mailPreSalutationText == "") : $mailPreSalutationText = getLngt("Sehr geehrte Damen und Herren,"); endif;
                $mailtext .= "<div class=\"" . $mailCssFontClass . "\">" . $mailPreSalutationText . "</div>";
                $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";

                $mailTextJobdata = getParameterValue("0", "MAIL_TEXT_JOBDATA", $hqId);
                if ($mailTextJobdata == "") : $mailTextJobdata = getLngt("Anbei übersenden wir Ihnen die Auftragsdaten."); endif;
                $mailtext .= "<div class=\"" . $mailCssFontClass . "\">" . $mailTextJobdata . "</div>";
                $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";                

                // Signature
                $mailTextRegards = getParameterValue("0", "MAIL_TEXT_REGARDS", $currentHqId);
                if ($mailTextRegards == "") : $mailTextRegards = getLngt("Mit freundlichem Gruß,"); endif;
                $mailtext .= "<div class=\"" . $mailCssFontClass . "\">" . $mailTextRegards . "</div>";
                if ($mailDisplayModeEmployeeData == "1" && $mailEmployeeData[0] != "" && $mailEmployeeData[1] != "") :
                   $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
                   $mailtext .= "<div class=\"" . $mailCssFontClass . "\">" . $mailEmployeeData[0] . " " . $mailEmployeeData[1] . "</div>";
                else :
                   $mailSalutationText = getParameterValue("0", "MAIL_SALUTATION_TEXT", $currentHqId);
                   $mailtext .= "<div class=\"" . $mailCssFontClass . "\">" . getLngt($mailSalutationText) . "</div>";
                endif;
                $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
                
                // HOMEPAGE
                $mailFooterWebPage = getParameterValue("0", "MAIL_FOOTER_WEB_PAGE", $currentHqId);
                
                // LOGO
                $logoName = getParameterValue("0", "IMG_LOGO_NAME", $currentHqId);
                $logoHeight = getParameterValue("0", "IMG_LOGO_HEIGHT", $currentHqId);
                $logoWidth = getParameterValue("0", "IMG_LOGO_WIDTH", $currentHqId);
                if ($logoName != "" && $logoHeight != "" && $logoWidth != "") :
                   $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
                   if ($mailFooterWebPage != "") : 
                      $mailtext .= "<div><a href=\"http://" . $mailFooterWebPage . "\"><img src=\"../images/external/" . $logoName . "\" border=\"0\" height=\"" . $logoHeight . "\" width=\"" . $logoWidth . "\"></a></div>";
                   else :
                      $mailtext .= "<div><img src=\"../images/external/" . $logoName . "\" border=\"0\" height=\"" . $logoHeight . "\" width=\"" . $logoWidth . "\"></div>";
                   endif;
                   $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
                endif;

                $mailFooterEnabled = getParameterValue("0", "MAIL_FOOTER_ENABLED", $currentHqId);
                if ($mailFooterEnabled == "1") :
                   $mailFooterAddress = getParameterValue("0", "MAIL_FOOTER_ADDRESS", $currentHqId);
                   if ($mailFooterAddress != "") :
                      $mailtext .= "<table style=\"font-family: Arial; font-size: 10pt; font-weight: normal; padding: 1px; color: #000000;\">";
                      $mailtext .= $mailFooterAddress;
                      $mailtext .= "</table>";
                      $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
                   endif;
                   $mailFooterContact = getParameterValue("0", "MAIL_FOOTER_CONTACT", $currentHqId);
                   if ($mailFooterContact != "") :                  
                      if ($mailDisplayModeEmployeeData == "1" && ($mailEmployeeData[2] != "" || $mailEmployeeData[3] != "" || $mailEmployeeData[4] != "")) :
                         $mailtext .= "<table>";
                         if ($mailEmployeeData[2] != "") : 
                            $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\">" . getLngt("Telefon") . ": " . $mailEmployeeData[2] . "</span></td></tr>";
                         endif;
                         if ($mailEmployeeData[4] != "") : 
                            $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\">" . getLngt("Fax") . ": " . $mailEmployeeData[4] . "</span></td></tr>";
                         endif;
                         if ($mailEmployeeData[3] != "") : 
                            $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\">" . getLngt("Email") . ": " . $mailEmployeeData[3] . "</span></td></tr>";
                         endif;
                         // $mailFooterWebPage = getParameterValue("0", "MAIL_FOOTER_WEB_PAGE", $currentHqId);
                         if ($mailFooterWebPage != "") : 
                            // $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\">" . $mailFooterWebPage . "</span></td></tr>";
                            $mailtext .= "<tr><td><span class=\"" . $mailCssFontClass . "\"><a href=\"http://" . $mailFooterWebPage . "\">" . $mailFooterWebPage . "</a></span></td></tr>";
                         endif;
                         $mailtext .= "</table>";
                      else :
                         $mailtext .= "<table style=\"font-family: Arial; font-size: 10pt; font-weight: normal; padding: 1px; color: #000000;\">";
                         $mailtext .= $mailFooterContact;
                         $mailtext .= "</table>";
                      endif;                  
                      $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
                   endif;
                   $mailFooterImpressum = getParameterValue("0", "MAIL_FOOTER_IMPRESSUM", $currentHqId);
                   if ($mailFooterImpressum != "") :
                      $mailtext .= "<table style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">";
                      $mailtext .= $mailFooterImpressum;
                      $mailtext .= "</table>";
                      $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
                   endif;
                   $mailFooterResponsibility = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY", $currentHqId);
                   if ($mailFooterResponsibility == "") : $mailFooterResponsibility = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY", "0"); endif;
                   if ($mailFooterResponsibility != "") : 
                      $mailtext .= "<div style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">" . $mailFooterResponsibility . "</div>";
                      // Extended text
                      $mailFooterResponsibility2 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_2", $currentHqId);
                      if ($mailFooterResponsibility2 == "") : $mailFooterResponsibility2 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_2", "0"); endif;
                      if ($mailFooterResponsibility2 != "") : 
                         $mailtext .= "<div style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">" . $mailFooterResponsibility2 . "</div>";
                         $mailFooterResponsibility3 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_3", $currentHqId);
                         if ($mailFooterResponsibility3 == "") : $mailFooterResponsibility3 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_3", "0"); endif;
                         if ($mailFooterResponsibility3 != "") : 
                            $mailtext .= "<div style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">" . $mailFooterResponsibility3 . "</div>"; 
                            $mailFooterResponsibility4 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_4", $currentHqId);
                            if ($mailFooterResponsibility4 == "") : $mailFooterResponsibility4 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_4", "0"); endif;
                            if ($mailFooterResponsibility4 != "") : 
                               $mailtext .= "<div style=\"font-family: Arial; font-size: 7pt; font-weight: normal; padding: 1px; color: #000000;\">" . $mailFooterResponsibility4 . "</div>";
                               $mailFooterResponsibility5 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_5", $currentHqId);
                               if ($mailFooterResponsibility5 == "") : $mailFooterResponsibility5 = getParameterValue("0", "MAIL_FOOTER_RESPONSIBILITY_5", "0"); endif;
                               if ($mailFooterResponsibility5 != "") : 
                                  $mailtext .= "<div class=\"" . $mailFooterImpressum . "\">" . $mailFooterResponsibility5 . "</div>";
                               endif;
                            endif;
                         endif;
                         $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
                      endif;
                   endif;
                   $mailFooterThinkGreen = getParameterValue("0", "MAIL_FOOTER_THINK_GREEN", $currentHqId);
                   if ($mailFooterThinkGreen != "") :
                      $mailtext .= $mailFooterThinkGreen;
                      $mailtext .= "<div style=\"width:100%; height:10px;\"> </div>";
                   endif;
                   $mailtext .= "<div style=\"width:100%; height:30px;\"> </div>";               
                endif;
                
                $mailtext .= "</body></html>";

                $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;

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

             // sleep(5);  // Avoid too many tasks
         endfor;
      endif;
   endif;
else :
   writeToFile($logFile, "OP=AUTO_EXPORT|DATETIME=" . $currentTime . "|CUST=" . $exportParsKeys[$i] . "|EMAIL=" . $mailResult);
endif;      
?>