<?php     

/*=======================================================================
 *
 * cron_sustainability_customer_report.php
 *
 =======================================================================*/


include_once ("../include/mcglobal.inc.php");
// include_once ("../include/inc_dbdata.inc.php");



// Get jobs for auto_response
function getCustomersForSustainabilityReport ($limit = "") {
  global $db, $logFile;
  $retArray = array();

  // Get customers by sustainability email address 
  $sqlquery = "SELECT cs.hq_id, cs.cs_id, cs.cs_eid, cmp.cmp_sustainability_email, cmp.cmp_sustainability_interval"
            . "  FROM customer AS cs, company AS cmp"
            . "  WHERE cs.cmp_id = cmp.cmp_id                     AND"
            . "        cmp.cmp_authenticated = '1'                AND"
            . "        cmp.cmp_archived = '0'                     AND"
            . "        cmp.cmp_sustainability_email != ''         AND"
            . "        (NOT ISNULL(cmp.cmp_sustainability_email))    "
            . " ORDER BY cs.cs_id " . (($limit != "" && is_numeric($limit)) ? "LIMIT 0, " . $limit : "");
// echo $sqlquery . "\n\n"; die();
  $result = $db->query($sqlquery);
  // $result = $db->query($sqlquery . " UNION " . $sqlquery2);
  if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
  while ($row = $result->fetch_assoc()):
        $retArray[$row["cs_id"]] = array($row["hq_id"], $row["cs_eid"], $row["cmp_sustainability_email"], $row["cmp_sustainability_interval"]);
  endwhile;
  $result->free();
  return $retArray;
} 


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

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


   $path = getAbsoluteSystemPath();
   $passwd = "acapella7890";
   $logFile = $path . "/log/sustainability.log";
   $logDebug = true;
   $emptyArgument = "\"\"";
   $currDatetime = getDateTime("0");  // Format: "YmdHi"

   $limit = $argv[2];  // Max number of customers to pe processesd
   if (!isset($limit) || !is_numeric($limit)) : $limit = ""; endif;

   // Get current month
   $curMonth = date("n");
   $curYear = date("Y");
   $quartals = array(1 => 1,2 => 1,3 => 1,4 => 2,5 => 2,6 => 2,7 => 3,8 => 3,9 => 3,10 => 4,11 => 4,12 => 4);
   $currentQuartal = $quartals[$curMonth];


   // Get jobs 
   $csArr = getCustomersForSustainabilityReport($limit);
   $cskArr = array_keys($csArr);
   $cskArrLen = count($cskArr);
   for ($i = 0; $i < $cskArrLen; $i++) :

       $csId = $cskArr[$i];
       $hqId = $csArr[$csId][0];
       $csEid = $csArr[$csId][1];
       $cmpSustainabilityEmail = $csArr[$csId][2];
       $cmpSustainabilityInterval = $csArr[$csId][3];

       $doContinue = false;
       switch ($cmpSustainabilityInterval) {
           case 0:  // monthly
               $doContinue = true;
               break;
           case 1:
               if ($curMonth == 1 || $curMonth == 4 || $curMonth == 7 || $curMonth == 10) :
                  $doContinue = true;
               endif;
               break;
           case 2:
               if ($curMonth == 1) :
                  $doContinue = true;
               endif;
               break;
       }

       if ($doContinue) :

          $csIsMeta = false;
          $tmpCsIdsSub = getColVectorFromDB2ArrayByClause("customer", "cs_id", "cs_id_parent = '" . $csId . "'");
          if (count($tmpCsIdsSub) > 0) :
             $csIsMeta = true;
          endif;
       
          // Call script for (meta-)customer
          $cmd = "php " . $path . "/tools/auto_trigger_jobs_sustainability_customer_report.php " . $passwd . " " . ($csIsMeta ? "cs_meta" : "cs") . " " . $csId . " " . $cmpSustainabilityInterval . " &";
// echo $cmd . "\n";
          $cmdOutput = null;
          exec($cmd, $cmdOutput);
// print_r($cmdOutput); echo "\n\n";

          sleep(3);
       endif;

       if ($logDebug) :
          writeToFile($logFile, $currDatetime . "[hq_id=" . $hqId . "][cs_id=" . $csId . "][cs_eid=" . $csEid . "][cmp_sustainability_email=" . $cmpSustainabilityEmail . "][cmpSustainabilityInterval=" . $cmpSustainabilityInterval . "]");
       endif;
   endfor;
endif;

?>



