<?php
/*=======================================================================
 *
 * auto_trigger_check_metafield.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/

/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ACHTUNG * ACHTUNG * ACHTUNG * ACHTUNG
NICHT ausführen bei den folgenden IDs, da diese eigene Scripte aufweisen.
NOCHT NICHT EINGEBAUT!!!!
200,444,445,446,447,1630,1646,1710,1715
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/

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

$autoMode = false;
if (!isset($argv[1]) || $argv[1] == "") :
   include_once ("../include/auth.inc.php");
   $autoMode = false;
else :
   if ($argv[1] == "acapella7890") :
       $autoMode = true;
       $hqIdMail = trim($argv[2]);  // Check for headquarter ID
       $category = trim($argv[3]);  // Check for category
   endif;
endif;

include_once ('../include/email/htmlMimeMail.php');


if ($hqIdMail != "" && (is_numeric($hqIdMail) || $hqIdMail == "HQ_ALL")) :

   if ($autoMode || $usr_id == "00000") :
   
      // Get user
      if (!$autoMode) :
         $usrName = getFieldValueFromId("user","usr_id",$usr_id,"usr_name");
         $usrFirstname = getFieldValueFromId("user","usr_id",$usr_id,"usr_firstname");
      endif;
   
      // Init
      $hq_id = "";  // Preconditions: default "0" <=> all mandators
      $sendPerMail = false;
      $f_secretFileName = "";
   
      // Global or single HQ
      $tmpHqId = $hq_id;
      $gHqId = true;
      $constFormSingleHQ = getParameterValue("0", "SYSTEM_FORM_SINGLE_HQ_" . $category, "0");
      if ($constFormSingleHQ != "" && $constFormSingleHQ != "0") :
         $gHqId = false;
      else :
         $constFormSingleHQ = getParameterValue("0", "SYSTEM_FORM_SINGLE_HQ", "0");
         if ($constFormSingleHQ != "" && $constFormSingleHQ != "0") :
            $gHqId = false;
         endif;
      endif;
      if ($gHqId == true) :
         $tmpHqId = "0";
      endif;

      $whereClauseHq = "       hq.hq_id = '" . $hqIdMail . "' AND";
      if ($hqIdMail == "HQ_ALL") :
         $whereClauseHq = "";
      endif;

      $currentDate = getDateTime("1");
      $currentDateTime = getDateTime("6");
      
      $triggerArray = array();
   
      // "2. Transporteursseite", Kategorie = mtfc_id = 3
      $tmpCategory = "3";
      $sqlquery = "SELECT usr.usr_firstname, usr.usr_name, mtfk.mtfk_name, mtfv.mtfv_value, hq.hq_mnemonic"
                . " FROM metafieldcategory AS mtfc, metafieldkey AS mtfk, metafieldcategorykey AS mtfck,"
                . "      metafieldvalue AS mtfv, courier AS cr, user AS usr, headquarters AS hq"
                . " WHERE mtfk.mtfk_type IN ('triggerdate', 'triggerdatetime') AND"
                . "       mtfck.mtfc_id = '" . $tmpCategory . "' AND"
                . "       mtfck.mtfc_id = mtfc.mtfc_id AND"
                . "       mtfck.hq_id = '" . $tmpHqId . "' AND"
                . "       mtfck.mtfk_id = mtfk.mtfk_id AND"
                . "       mtfv.mtfck_id = mtfck.mtfck_id AND"
                . "       mtfv.mtfv_id = cr.cr_id AND"
                . "       cr.hq_id = hq.hq_id AND"
                .         $whereClauseHq
                . "       cr.usr_id = usr.usr_id AND"
                . "       mtfv.mtfv_value != '' AND"
                . "       " . $currentDate . " >= CONCAT(SUBSTRING(mtfv.mtfv_value,7,4),SUBSTRING(mtfv.mtfv_value,4,2),SUBSTRING(mtfv.mtfv_value,1,2))"
                . " ORDER BY hq.hq_mnemonic, mtfk.mtfk_name, usr.usr_name, usr.usr_firstname";
// echo $sqlquery . "\n\n"; die();
      $result = $db->query($sqlquery);   
      if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
      while ($row = $result->fetch_assoc()):
            $triggerArray[] = array($row["hq_mnemonic"], $row["usr_firstname"], $row["usr_name"], $row["mtfk_name"], $row["mtfv_value"]);
      endwhile;
      $result->free();
      
      // "2. Fahrzeugseite", Kategorie = mtfc_id = 4
      $tmpCategory = "4";
      $sqlquery = "SELECT usr.usr_firstname, usr.usr_name, mtfk.mtfk_name, mtfv.mtfv_value, hq.hq_mnemonic, crvh.crvh_sid, crvh.crvh_vh_sign"
                . " FROM metafieldcategory AS mtfc, metafieldkey AS mtfk, metafieldcategorykey AS mtfck,"
                . "      metafieldvalue AS mtfv, courier AS cr, couriervehicle AS crvh, user AS usr, headquarters AS hq"
                . " WHERE mtfk.mtfk_type IN ('triggerdate', 'triggerdatetime') AND"
                . "       mtfck.mtfc_id = '" . $tmpCategory . "' AND"
                . "       mtfck.mtfc_id = mtfc.mtfc_id AND"
                . "       mtfck.hq_id = '" . $tmpHqId . "' AND"
                . "       mtfck.mtfk_id = mtfk.mtfk_id AND"
                . "       mtfv.mtfck_id = mtfck.mtfck_id AND"
                . "       mtfv.mtfv_id = crvh.crvh_id AND"
                . "       crvh.cr_id = cr.cr_id AND"
                . "       cr.hq_id = hq.hq_id AND"
                .         $whereClauseHq
                . "       cr.usr_id = usr.usr_id AND"
                . "       mtfv.mtfv_value != '' AND"
                . "       " . $currentDate . " >= CONCAT(SUBSTRING(mtfv.mtfv_value,7,4),SUBSTRING(mtfv.mtfv_value,4,2),SUBSTRING(mtfv.mtfv_value,1,2))"
                . " ORDER BY hq.hq_mnemonic, mtfk.mtfk_name, usr.usr_name, usr.usr_firstname";
// echo $sqlquery . "\n\n"; die();
      $result = $db->query($sqlquery);   
      if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
      while ($row = $result->fetch_assoc()):
            $triggerArray[] = array($row["hq_mnemonic"], $row["crvh_sid"], $row["crvh_vh_sign"], $row["usr_firstname"], $row["usr_name"], $row["mtfk_name"], $row["mtfv_value"]);
      endwhile;
      $result->free();
      
      $triggerArrayLen = count($triggerArray);
      $triggerOut = "";
      if ($triggerArrayLen > 0) :
         $sendPerMail = true;  // At least one event to be mailed
         $triggerOut .= "<html>\n";
         $triggerOut .= "<body>\n";
         $triggerOut .= "<table>\n";
         for ($i = 0; $i < $triggerArrayLen; $i++) :
             $tmpArray = $triggerArray[$i];
             $tmpArrayLen = count($tmpArray);
             $triggerOut .= "<tr>\n";
             for ($j = 0; $j < $tmpArrayLen; $j++) :
                 $triggerOut .= "<td>" . $tmpArray[$j] . "&nbsp;&nbsp;&nbsp;</td>\n";
             endfor;
             $triggerOut .= "</tr>\n";
         endfor;
         $triggerOut .= "</table>\n";
         $triggerOut .= "</body>\n";
         $triggerOut .= "</html>\n";
      endif;
      
      
      // Send via email
      if ($sendPerMail) :

         $mailSenderAddress = "support@assecutor.de";

         // Receiver of the mail
         // HTG: $mailReceiverAddressArray = array("orga@assecutor.de","kennziffer95-bremen@hansetrans.de","kennziffer95-hamburg@hansetrans.de","kennziffer95-berlin@hansetrans.de","kennziffer95-hannover@hansetrans.de","kennziffer95-frankfurt@hansetrans.de","kennziffer95-dresden@hansetrans.de","kennziffer95-essen@hansetrans.de","kennziffer95-leipzig@hansetrans.de","kennziffer95-muenchen@hansetrans.de","kennziffer95-nuernberg@hansetrans.de","kennziffer95-stuttgart@hansetrans.de","kennziffer95-koeln@hansetrans.de");
         $mailReceiverAddress = "";  // Init
         $mailReceiverAddressArray = array("support@assecutor.de");
         if ($hqIdMail != "" && is_numeric($hqIdMail)) :
            $mailReceiverAddress = getParameterValue("0", "MAIL_CRON_100_TO_ADDRESS", $hqIdMail);
         endif;
         if ($mailReceiverAddress == "") :
            $mailReceiverAddress = getParameterValue("0", "MAIL_CRON_100_TO_ADDRESS", "0");
         endif;
         if ($mailReceiverAddress != "") :
            $mailReceiverAddressArray = spliti(",",$mailReceiverAddress);
         endif;      

         $mailSubject = "Überwachung von Ablaufzeiten";
         $mailCcAddress = "";
         $mailBccAddress = "orga@assecutor.de";

         $mailtext = "ÜBERWACHUNG VON ABLAUFZEITEN<br><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("../temp/download/" . $f_secretFileName)) :
               $attachment = $mailObj->getFile("../temp/download/" . $f_secretFileName);
               $mailObj->addAttachment($attachment, $f_secretFileName, $f_mimetype . $f_charset);
            endif;
         endif;

         $mailResult = $mailObj->send($mailReceiverAddressArray, 'smtp');

         $out = "Der Sendevorgang wurde abgeschlossen! Bitte prüfen Sie Ihren Email-Eingangsordner!";
      endif;
   else :   
      $out = "Leider haben Sie zur Ausführung keine Berechtigung!";   
   endif;
endif;
?>

<?php if (!$autoMode) : ?>

<html>

<head>

</head>
	
	
<body>

<center>
	
<br><br><br>

<b><?php echo $triggerOut ?></b>

</center>

</body>	
	
</html>

<?php endif; ?>