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


include_once ("../include/mcglobal.inc.php");
include_once ("../include/ftp.inc.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");

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

// 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();

/*
t:m
cs_id   emp_id  usr_id  hq_id
832514 	833410  840785   1
203463 	203464  208443   2
833194 	834135  841591   3
819783 	819901  825754   4
823978 	824386  830734   5
819825 	819943  825800   6
823862 	824266  830604   7
835489 	836600  844393   7
823922 	824329  830671   8
839904 	841607  850094   9
847829 	851375  861224   9
835016 	836086  843818  11

ADMIN
usr_id  hq_id
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 = "1"; $hqName = "HB"; $customerId = "832514"; $usrId = "840785"; $f_act = "special_export_4";
   $exportPars["TM_HB_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);
/*
   $hqId = "2"; $hqName = "HH"; $customerId = "203463"; $usrId = "208443"; $f_act = "special_export_4";
   $exportPars["TM_HH_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);

   $hqId = "3"; $hqName = "B"; $customerId = "833194"; $usrId = "841591"; $f_act = "special_export_4";
   $exportPars["TM_B_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);

   $hqId = "4"; $hqName = "H"; $customerId = "819783"; $usrId = "825754"; $f_act = "special_export_4";
   $exportPars["TM_H_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);

   $hqId = "5"; $hqName = "F"; $customerId = "823978"; $usrId = "830734"; $f_act = "special_export_4";
   $exportPars["TM_F_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);

   $hqId = "6"; $hqName = "DD"; $customerId = "819825"; $usrId = "825800"; $f_act = "special_export_4";
   $exportPars["TM_DD_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);

   $hqId = "7"; $hqName = "E"; $customerId = "823862"; $usrId = "830604"; $f_act = "special_export_4";
   $exportPars["TM_E_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);
   $hqId = "7"; $hqName = "E2"; $customerId = "835489"; $usrId = "844393"; $f_act = "special_export_4";
   $exportPars["TM_E2_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);

   $hqId = "8"; $hqName = "L"; $customerId = "823922"; $usrId = "830671"; $f_act = "special_export_4";
   $exportPars["TM_L_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);

   $hqId = "9"; $hqName = "M"; $customerId = "839904"; $usrId = "850094"; $f_act = "special_export_4";
   $exportPars["TM_M_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);
   $hqId = "9"; $hqName = "M2"; $customerId = "847829"; $usrId = "861224"; $f_act = "special_export_4";
   $exportPars["TM_M2_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);

   $hqId = "8"; $hqName = "S"; $customerId = "835016"; $usrId = "843818"; $f_act = "special_export_4";
   $exportPars["TM_S_ABR"]   = array($hqId,$usrId,"_",$f_act,$emptyArgument,$emptyArgument,$emptyArgument,$emptyArgument,$fileNamePrefix . "_" . $hqName . ".csv",$toDay,$toMonth,$toYear,$emptyArgument,$fromDay,$fromMonth,$fromYear,$customerId);
*/
   $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_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;

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

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