<?php      
/*=======================================================================
 *
 * cron_export.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"

   if ($argv[2] == "today") :
      $toDay = getDateTime("day");   
      $toMonth = getDateTime("month");
      $toYear = getDateTime("year");
   else :
      $toDay = getDateTime("day_yesterday");   
      $toMonth = getDateTime("month_yesterday");
      $toYear = getDateTime("year_yesterday");
   endif;


   // 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 $f_jbp_filter $specialJbPayment $jbpc_id

   $exportPars = array();

   $exportPars["SB_HB_DEB"]  = array("1","100001","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".039.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_HB_KRED"] = array("1","100001","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".039.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_HB_FZG"]  = array("1","100001","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".039.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_HB_ABR"]  = array("1","100001","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".039.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_HB_GUT"]  = array("1","100001","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".039.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");

   $exportPars["SB_HH_DEB"]  = array("2","200001","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".041.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_HH_KRED"] = array("2","200001","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".041.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_HH_FZG"]  = array("2","200001","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".041.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_HH_ABR"]  = array("2","200001","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".041.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_HH_GUT"]  = array("2","200001","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".041.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");

   $exportPars["SB_B_DEB"]   = array("3","300002","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".077.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_B_KRED"]  = array("3","300002","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".077.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_B_FZG"]   = array("3","300002","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".077.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_B_ABR"]   = array("3","300002","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".077.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_B_GUT"]   = array("3","300002","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".077.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
                              
   $exportPars["SB_H_DEB"]   = array("4","400001","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".093.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_H_KRED"]  = array("4","400001","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".093.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_H_FZG"]   = array("4","400001","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".093.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_H_ABR"]   = array("4","400001","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".093.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_H_GUT"]   = array("4","400001","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".093.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
                              
   $exportPars["SB_F_DEB"]   = array("5","500001","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".024.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_F_KRED"]  = array("5","500001","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".024.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_F_FZG"]   = array("5","500001","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".024.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_F_ABR"]   = array("5","500001","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".024.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_F_GUT"]   = array("5","500001","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".024.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");

   $exportPars["SB_DD_DEB"]  = array("6","600001","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".055.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_DD_KRED"] = array("6","600001","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".055.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_DD_FZG"]  = array("6","600001","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".055.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_DD_ABR"]  = array("6","600001","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".055.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_DD_GUT"]  = array("6","600001","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".055.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");

   $exportPars["SB_E_DEB"]   = array("7","700001","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".026.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_E_KRED"]  = array("7","700001","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".026.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_E_FZG"]   = array("7","700001","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".026.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_E_ABR"]   = array("7","700001","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".026.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_E_GUT"]   = array("7","700001","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".026.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");

   $exportPars["SB_L_DEB"]   = array("8","800001","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".088.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_L_KRED"]  = array("8","800001","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".088.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_L_FZG"]   = array("8","800001","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".088.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_L_ABR"]   = array("8","800001","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".088.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_L_GUT"]   = array("8","800001","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".088.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");

   $exportPars["SB_M_DEB"]   = array("9","826760","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".043.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_M_KRED"]  = array("9","826760","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".043.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_M_FZG"]   = array("9","826760","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".043.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_M_ABR"]   = array("9","826760","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".043.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_M_GUT"]   = array("9","826760","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".043.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");

   $exportPars["SB_S_DEB"]   = array("11","826762","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".052.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_S_KRED"]  = array("11","826762","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".052.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_S_FZG"]   = array("11","826762","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".052.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_S_ABR"]   = array("11","826762","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".052.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_S_GUT"]   = array("11","826762","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".052.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");

   /*
   $exportPars["SB_SL_DEB"]  = array("90","826884","_","export","1","|","RETRANS Debitorenstammsatz","RETRANS Debitorenstammsatz",$fileNamePrefix . ".060.deb",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_SL_KRED"] = array("90","826884","_","export","2","|","RETRANS Kreditorenstammsatz","RETRANS Kreditorenstammsatz",$fileNamePrefix . ".060.kred",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   $exportPars["SB_SL_FZG"]  = array("90","826884","_","export","3","|","RETRANS Fahrzeugstammsatz","RETRANS Fahrzeugstammsatz",$fileNamePrefix . ".060.fzg",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_SL_ABR"]  = array("90","826884","_","export","5","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".060.abr",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   // $exportPars["SB_SL_GUT"]  = array("90","826884","_","export","6","|","RETRANS Abrechnungen","RETRANS Abrechnungen",$fileNamePrefix . ".060.gut",$toDay,$toMonth,$toYear,$emptyArgument,$emptyArgument,"","");
   */

   $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,$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 . " " . $f_jbp_filter . " " . $specialJbPayment . " " . $jbpc_id . " &";
// echo $exportParsKeys[$i] . ":\n";
// echo $cmd . "\n\n\n";
          $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;

      // FTP Uploads
      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,$f_jbp_filter,$specialJbPayment,$jbpc_id) = $exportPars[$exportParsKeys[$i]];

          $cmd = "php " . $path . "/export/exportdata_ftp_auto.php " . $passwd . " " . $f_fileName . " " . $hq_id . " " . "0" . " &";

          $cmdOutput = null;
          exec($cmd, $cmdOutput);
          // list($errNo, $errDesc) = $cmdOutput;

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

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