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

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

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


// Check für headquarter ID in $argv[2]
$hqIdMail = trim($argv[2]);

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
      $sendPerMail = false;
      $f_secretFileName = "";

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

      $currentDate = getDateTime("1");
      $currentDateTime = getDateTime("6");
      $crvhTotalWeight = 0;            // 3490
      $crvhFilter = "";                // "crvh.vht_id >= '80' AND crvh.vht_id <= '990' AND ";
      $constAmStarttimeInDays  = 180;  // 6 months
      $constAmStarttimeInDays2 =  90;  // 3 months
      $constAmStarttimeInDays3 =  60;  // 2 months
      $constAmStarttimeInDays4 =  30;  // 1 month

      $checkTime  = getDateTime("datetime_plus_offset", array(0,0,0,0,+($constAmStarttimeInDays),0), "Y-m-d") . " 00:00:00";
      $checkTime2 = getDateTime("datetime_plus_offset", array(0,0,0,0,+($constAmStarttimeInDays2),0), "Y-m-d") . " 00:00:00";
      $checkTime3 = getDateTime("datetime_plus_offset", array(0,0,0,0,+($constAmStarttimeInDays3),0), "Y-m-d") . " 00:00:00";
      $checkTime4 = getDateTime("datetime_plus_offset", array(0,0,0,0,+($constAmStarttimeInDays4),0), "Y-m-d") . " 00:00:00";
      $checkTime5 = getDateTime("datetime_plus_offset", array(0,0,0,0,0,0), "Y-m-d") . " 00:00:00";
// echo $checkTime . "\n"; echo $checkTime2 . "\n"; echo $checkTime3 . "\n"; echo $checkTime4 . "\n"; echo $checkTime5 . "\n";

      $triggerArray = array();

      // Fahrzeug Transportversicherungsdatum "crvh_transport_insurance_date"
      if (true) :

         $fieldName = "crvh.crvh_transport_insurance_date";

         $sqlquery = "SELECT cr.cr_eid, crvh.crvh_sid, usr.usr_firstname, usr.usr_name, 'Transportversicherungsdatum' AS field_name, IFNULL(" . $fieldName . ",'') AS field_value, hq.hq_mnemonic, cr.cr_id"
                   . " FROM user AS usr, headquarters AS hq, company AS cmp,"
                   . "      courier AS cr, couriervehicle AS crvh"
                   . " WHERE cr.hq_id = hq.hq_id         AND"
                   .         $whereClauseHq
                   . "       cr.cmp_id = cmp.cmp_id      AND"
                   . "       cmp.cmp_archived = '0'      AND"
                   . "       cmp.cmp_authenticated = '1' AND"
                   . "       cmp.cmp_visible = '1'       AND"                   
                   . "       cr.usr_id = usr.usr_id      AND"
                   . "       crvh.cr_id = cr.cr_id          "
                   . " ORDER BY hq.hq_mnemonic, field_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()):

               // Get all vehicles of parent (UN) and his/her drivers
               $crvhIdArray1 = getColVectorFromDB2ArrayByClause("courier AS cr_parent, courier AS cr, couriervehicle AS crvh", "crvh.crvh_id", "cr_parent.cr_id = '" . $row["cr_id"] . "' AND cr.cr_id_parent = cr_parent.cr_id AND cr.cr_id = crvh.cr_id", "", "", "");
               $crvhIdArray2 = getColVectorFromDB2ArrayByClause("couriervehicle AS crvh", "crvh.crvh_id", "crvh.cr_id = '" . $row["cr_id"] . "'", "", "", "");
               $crvhIdArray = array_merge($crvhIdArray1, $crvhIdArray2);
               $crvhIdArrayLen = count($crvhIdArray);

               if ($crvhIdArrayLen > 0) :

                  $fieldValue = "";
                  if ($row["field_value"] != "") :
                     $fieldValue = $row["field_value"];   // Time to Date
                  endif;
// echo $fieldValue . "\n";
                  if ($fieldValue == "" || $fieldValue == "0000-00-00") :
                     // $triggerArray[] = array("66CCFF", $row["hq_mnemonic"], $row["cr_eid"], $row["crvh_sid"], $row["usr_firstname"], $row["usr_name"], $row["field_name"], $row["field_value"]);
                  elseif ($fieldValue <= $checkTime5) :
                     $triggerArray[] = array("FF0000", $row["hq_mnemonic"], $row["cr_eid"], $row["crvh_sid"], $row["usr_firstname"], $row["usr_name"], $row["field_name"], $row["field_value"]);
                  elseif ($fieldValue <= $checkTime4) :
                     $triggerArray[] = array("FF0044", $row["hq_mnemonic"], $row["cr_eid"], $row["crvh_sid"], $row["usr_firstname"], $row["usr_name"], $row["field_name"], $row["field_value"]);
                  elseif ($fieldValue <= $checkTime3) :
                     $triggerArray[] = array("FF6633", $row["hq_mnemonic"], $row["cr_eid"], $row["crvh_sid"], $row["usr_firstname"], $row["usr_name"], $row["field_name"], $row["field_value"]);
                  elseif ($fieldValue <= $checkTime2) :
                     $triggerArray[] = array("FFCC33", $row["hq_mnemonic"], $row["cr_eid"], $row["crvh_sid"], $row["usr_firstname"], $row["usr_name"], $row["field_name"], $row["field_value"]);
                  elseif ($fieldValue <= $checkTime) :
                     $triggerArray[] = array("FFFF33", $row["hq_mnemonic"], $row["cr_eid"], $row["crvh_sid"], $row["usr_firstname"], $row["usr_name"], $row["field_name"], $row["field_value"]);
                  endif;
               endif;
         endwhile;
         $result->free();

         $triggerArrayLen = count($triggerArray);
// echo $triggerArrayLen . "\n";
         $triggerOut = "";
         if ($triggerArrayLen > 0) :
            $sendPerMail = true;  // At least one event to be mailed
            $triggerOut .= "<html>\n";
            $triggerOut .= "<body>\n";

            $triggerOut .= "<br>";
            $triggerOut .= "<table>\n";
            $triggerOut .= "<tr>\n";
            // $triggerOut .= "<td style=\"background-color: #66CCFF;\">&nbsp;&nbsp;KEIN EINTRAG&nbsp;&nbsp;</td>";
            $triggerOut .= "<td style=\"background-color: #FFFF33;\">&nbsp;&nbsp;&nbsp;< 6 Monate&nbsp;&nbsp;&nbsp;</td>";
            $triggerOut .= "<td style=\"background-color: #FFCC33;\">&nbsp;&nbsp;&nbsp;< 3 Monate&nbsp;&nbsp;&nbsp;</td>";
            $triggerOut .= "<td style=\"background-color: #FF6633;\">&nbsp;&nbsp;&nbsp;< 2 Monate&nbsp;&nbsp;&nbsp;</td>";
            $triggerOut .= "<td style=\"background-color: #FF0033;\">&nbsp;&nbsp;&nbsp;< 1 Monat&nbsp;&nbsp;&nbsp;&nbsp;</td>";
            $triggerOut .= "</tr>\n";
            $triggerOut .= "</table>\n";

            $triggerOut .= "<br><br>";
            $triggerOut .= "<table>\n";
            for ($i = 0; $i < $triggerArrayLen; $i++) :
                $tmpArray = $triggerArray[$i];
                $tmpArrayLen = count($tmpArray);
                $rowColor = $tmpArray[0];
                $triggerOut .= "<tr>\n";
                for ($j = 1; $j < $tmpArrayLen; $j++) :
                    $triggerOut .= "<td style=\"background-color: #" . $rowColor . ";\">&nbsp;" . $tmpArray[$j] . ($rowColor == "FF0000" && $j == "6" ? "&nbsp;[überschritten]" : "") . "&nbsp;&nbsp;&nbsp;</td>\n";
                endfor;
                $triggerOut .= "</tr>\n";
            endfor;
            /*
            $triggerOut .= "<tr>\n";
            $triggerOut .= "<td colspan=\"5\">\n";
            $triggerOut .= "<br><br>\n";
            $triggerOut .= "Bitte prüfen Sie bei den Unternehmern die arbeitnehmerähnliche Selbständigkeit. Ist mindestens ein sozialversicherungspflichtiger Angestellter vorhanden?";
            $triggerOut .= "</td>\n";
            $triggerOut .= "</tr>\n";
            */
            $triggerOut .= "</table>\n";

            $triggerOut .= "</body>\n";
            $triggerOut .= "</html>\n";
         endif;

         // Send via email
         if ($sendPerMail) :  

            $mailSenderAddress = "support@assecutor.de";
           
            // Receiver of the mail
            $mailReceiverAddressArray = array("support@assecutor.de");
            if ($hqIdMail != "" && is_numeric($hqIdMail)) :
               $mailReceiverAddress = getParameterValue("0", "MAIL_CRON_102_TO_ADDRESS", $hqIdMail);
            endif;
            if ($mailReceiverAddress == "") :
               $mailReceiverAddress = getParameterValue("0", "MAIL_CRON_102_TO_ADDRESS", "0");
            endif;
            if ($mailReceiverAddress != "") :
               $mailReceiverAddressArray = spliti(",",$mailReceiverAddress);
            endif;
// echo "mailReceiverAddress = " . $mailReceiverAddress . "\n\n";
// $mailReceiverAddressArray = array("mv@assecutor.de");
// $mailReceiverAddressArray = array("mv@assecutor.de", "ingo.kublenz@stadtbote.de");

            $mailSubject = "Transportversicherungsdatum";
            $mailCcAddress = "";
            $mailBccAddress = "mv@assecutor.de";
// $mailBccAddress = "";
            $mailtext = "Transportversicherungsdatum<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;
      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; ?>