<?php
/*=======================================================================
 *
 * auto_trigger_vop.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');


if ($autoMode || $usr_id == "00000") :

   // Filter by headquarters
   $hqId = $argv[2];

   // 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 = "";

   $whereClauseCrId = "";
   if ($objId != "" && is_numeric($objId)) :
      $whereClauseCrId = " cr.cr_id = '" . $objId . "' AND ";
   endif;

   $whereClauseHq1 = "";
   $whereClauseHq2 = "";
   if ($hqId != "" && is_numeric($hqId)) :
      $whereClauseHq1 = " AND jb.hq_id = '" . $hqId . "' ";
      $whereClauseHq2 = " cr.hq_id = '" . $hqId . "' AND ";
   endif;

   // [1.] Es wird nach allen Fahrzeugen geschaut, die in den letzten 90 Tagen mindestens einmal aktiv waren.
   // Für diese Fahrzeuge werden die Paarungen (cr_id, crvh_sid) als zu prüfende Obermenge gebildet.
   // [2.] Im Anschluss werden alle Paarungen aus der Aufstellung "Vorort-Prüfung Kundenberichte" auf das
   // Berichtsdatum geprüft. 
   // [3.] Wenn dieses innerhalb NICHT in der festgelegten n Monate liegt, wird der Posten in einem 
   // NEUEN Array festgehalten und hinterher per Mail (an die jeweilige NL) geschickt werden.

   $currentDate = getDateTime("1");
   $currentDateTime = getDateTime("6");
   $numOfVehicles = 2;
   $crvhTotalWeight = 0;  // 3490
   $constAmStarttimeInDays = 90;

   // [1.]
   // Get all active vehicles
   $startTime = getDateTime("datetime_plus_offset", array(0,0,0,0,-($constAmStarttimeInDays),0), "Y-m-d") . " 00:00:00";
   // $crIdMatchArray = getColVectorFromDB2ArrayByClause("couriervehicle AS crvh, courier AS cr_parent, courier AS cr, job AS jb", "cr.cr_id AS cr_id", "crvh.crvh_totalweight > '" . $crvhTotalWeight . "' AND crvh.cr_id = cr_parent.cr_id AND cr.cr_id_parent = cr_parent.cr_id AND cr.cr_id = jb.cr_id AND jb.jb_taketime >= '" . $startTime . "' " . $whereClauseHq1, "", "", "DISTINCT");
   $crCrvhArray = getColVectorFromDB2ArrayByClause("couriervehicle AS crvh, courier AS cr_parent, courier AS cr, job AS jb", "CONCAT(cr_parent.cr_id,',',crvh.crvh_sid) AS cr_crvh", "crvh.vht_id <= '990' AND crvh.cr_id = cr_parent.cr_id AND cr.cr_id_parent = cr_parent.cr_id AND cr.cr_id = jb.cr_id AND jb.jb_taketime >= '" . $startTime . "' " . $whereClauseHq1, "", "", "DISTINCT");
   $crCrvhArrayLen = count($crCrvhArray);

   // [2.]
   // "Vorort-Prüfung Kundenberichte", Kategorie = mtfc_id = 102
   $vopArray = array();
   $sqlquery = "SELECT rp.rp_id, rp.rp_createtime, cr.cr_id, crvh.crvh_id, 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, phoenix_group.report_process AS rp"
             . " WHERE mtfck.mtfc_id = '102' AND"
             . "       mtfck.mtfk_id = '426' AND"
             . "       mtfck.mtfc_id = mtfc.mtfc_id AND"
             . "       mtfck.mtfk_id = mtfk.mtfk_id AND"
             . "       LEFT(mtfk.mtfk_type, 11) = 'select:crvh' AND"
             . "       mtfv.mtfck_id = mtfck.mtfck_id AND"
             . "       mtfv.mtfv_id = rp.rp_id AND"
             . "       mtfv.mtfv_value != '' AND"
             . "       crvh.crvh_sid COLLATE latin1_general_ci = mtfv.mtfv_value COLLATE latin1_general_ci AND"
             . "       crvh.cr_id = cr.cr_id AND"
             .         $whereClauseCrId
             .         $whereClauseHq2
             . "       cr.hq_id = hq.hq_id AND"
             . "       cr.usr_id = usr.usr_id"
             . " ORDER BY cr.cr_id, crvh.crvh_sid, rp.rp_createtime DESC";

   $result = $db->query($sqlquery);   
   if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
   while ($row = $result->fetch_assoc()):
         $vopArray[] = array($row["rp_id"], $row["rp_createtime"], $row["crvh_sid"], $row["crvh_vh_sign"], $row["cr_id"]);
   endwhile;
   $result->free();
   $vopArrayLen = count($vopArray);

   // [3.]
   // Generate array to be mailed
   $constEvaluationTimeInDays = 120;
   $evaluationTime = getDateTime("datetime_plus_offset", array(0,0,0,0,-($constEvaluationTimeInDays),0), "Y-m-d") . " 00:00:00";
   $triggerArray = array();
   for ($i = 0; $i < $crCrvhArrayLen; $i++) :
       $tmpArray = spliti(",", $crCrvhArray[$i]);
       $tmpCrId = $tmpArray[0];
       $tmpCrvhSid = $tmpArray[1];
       for ($j = 0; $j < $vopArrayLen; $j++) :
           if ($tmpCrId == $vopArray[$j][4] && $tmpCrvhSid == $vopArray[$j][2]) :
              if ($vopArray[$j][1] < substr($evaluationTime,0,10)) :
                 $triggerArray[] = array($vopArray[$j][0], $vopArray[$j][1], $vopArray[$j][2], $vopArray[$j][3]);   // rp_id, rp_createtime, crvh_sid, cr_id
              endif;
              break 1;
           endif;
       endfor;
   endfor;
   $triggerArrayLen = count($triggerArray);

   $triggerOut = "";
   if ($triggerArrayLen > 0) :
      $sendPerMail = true;

      $triggerOut .= "<html>\n";
      $triggerOut .= "<body>\n";
      $triggerOut .= "<table>\n";
      $triggerOut .= "<tr>\n";
      $triggerOut .= "<td align=\"center\">" . getLngt("Bericht") . "&nbsp;&nbsp;&nbsp;</td>\n";
      $triggerOut .= "<td align=\"center\">" . getLngt("Datum") . "&nbsp;&nbsp;&nbsp;</td>\n";
      $triggerOut .= "<td align=\"center\">" . getLngt("Fahrzeug") . "&nbsp;&nbsp;&nbsp;</td>\n";
      $triggerOut .= "<td align=\"center\">" . getLngt("Kennzeichen") . "&nbsp;&nbsp;&nbsp;</td>\n";
      $triggerOut .= "</tr>\n";
      for ($i = 0; $i < $triggerArrayLen; $i++) :
          $tmpArray = $triggerArray[$i];
          $tmpArrayLen = count($tmpArray);
          $triggerOut .= "<tr>\n";
          $triggerOut .= "<td align=\"center\"><a href=\"../groupware/report.php?rpObjType=cs&rpIdsFilter=" . $tmpArray[0] . "\">" . $tmpArray[0] . "</a>&nbsp;&nbsp;&nbsp;</td>\n";
          $triggerOut .= "<td align=\"center\">" . substr($tmpArray[1],8,2) . "." . substr($tmpArray[1],5,2) . "." . substr($tmpArray[1],0,4) . "</td>\n";
          // for ($j = 2; $j < $tmpArrayLen; $j++) :
          for ($j = 2; $j <= 3; $j++) :
              $triggerOut .= "<td align=\"center\">" . $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";

      if ($hqId == "1") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "2") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "3") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "4") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "5") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "6") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "7") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "8") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "9") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "10") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "11") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "12") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "90") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      elseif ($hqId == "101") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-bremen@hansetrans.de");
      elseif ($hqId == "102") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-hamburg@hansetrans.de");
      elseif ($hqId == "103") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-berlin@hansetrans.de");
      elseif ($hqId == "104") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-hannover@hansetrans.de");
      elseif ($hqId == "105") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-frankfurt@hansetrans.de");
      elseif ($hqId == "106") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-dresden@hansetrans.de");
      elseif ($hqId == "107") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-essen@hansetrans.de");
      elseif ($hqId == "108") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-leipzig@hansetrans.de");
      elseif ($hqId == "109") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-muenchen@hansetrans.de");
      elseif ($hqId == "110") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-nuernberg@hansetrans.de");
      elseif ($hqId == "111") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-stuttgart@hansetrans.de");
      elseif ($hqId == "112") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","kennziffer95-koeln@hansetrans.de");
      elseif ($hqId == "203") :
             $mailReceiverAddressArray = array("mail-cron@assecutor.de","");
      endif;
$mailReceiverAddressArray = array("rainer.westphal@hansetrans.de","jaenecke@hansetrans.de");

      $mailSubject = "Überwachung Vorort-Prüfungen";
      $mailCcAddress = "";
      $mailBccAddress = "";
$mailBccAddress = "support@assecutor.de";

      $mailtext = "ÜBERWACHUNG VORORT-PRÜFUNGEN<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;
?>

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

<html>

<head>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
</head>
	
<body class="menu_bgcol" >

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

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

</center>

</body>	
	
</html>

<?php endif; ?>