<?php
/*=======================================================================
 *
 * auto_mtf_VOP.php
 *
 *  php ../tools/auto_mtf_VOP.php acapella7890 102 > ../log/auto_mtf_VOP.stdout+err 2>&1 &
 *
 * 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;
   if (isset($argv[2]) && $argv[2] != "") : $hq_id = $argv[2]; endif;
endif;

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

// require_once ("../include/WkHtmlToPdf.php");


// Execution-Time for script
set_time_limit(120);


if (!$autoMode) :
   $httpVars = array("category", "objId", "cascadingObjType", "specialPageTitle", "sendToClientInlineDisplay", "sendToClientFileDownload", "fileTargetType", 
                     "auth", "mailToAdresses", "mailCcAdresses", "mailBccAdresses");

   getSecHttpVars("1", $httpVars);
endif;


getLanguage(__FILE__);

if ($specialPageTitle == "") : $specialPageTitle = $objId; endif;

$pageTitel = getLngt($specialPageTitle);
include_once ("../include/html.inc.php");
include_once ("../include/email/htmlMimeMail.php");

// "$objId" and "$category" (and additional "$cascadingObjType") have to be defined HERE !!!
include_once ("../include/inc_metafield.inc.php");

getCurrentScript(__FILE__);
if (!$autoMode) :
   // Check for authentication access and granted rights
   $usrAccessArray["hq"] = "1";
   authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
   if (!(authCheckEmployeeRights($emp_id, "0") || authCheckEmployeeRights($emp_id, "1") || authCheckEmployeeRights($emp_id, "14"))) :
      gotoReferer("1");
   endif;
   $hqId = $hq_id;
endif;




// -------------------------------------------



/*
$auth             = trim($argv[1]);
$path             = trim($argv[2]);
$script           = trim($argv[3]);
$objId            = trim($argv[4]);
$cascadingObjType = trim($argv[5]);
$specialPageTitle = trim($argv[6]);
*/
function callMetafieldSpecial ($objId) {
	if ($objId != "") :
     $dirName = dirname(__FILE__);     
     $dirName = str_replace("\\", "/", $dirName);
     $lastSlashPos = strrpos($dirName,"/");
     $path = substr($dirName, 0, $lastSlashPos);
     $result = "";
     $cmd = "php " . $path . "/tools/call_script.php acapella7890 " . $path . " " . $path . "  " . $path . "  " . $path . "  " . $path . "  " . $path . "  " . $path . "  &";
     exec($cmd . " > /dev/null &");
  endif;
}


if (true || $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 = "";

   // Preconditions:
   // hq_id (default "0" wg. mandantenübergreifender Nutzung)   

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

   $fromDate = getDateTime("date_plus_offset",array(0,-1,-1), "Ymd");

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// $hq_id = "102";
// echo "hq_id " . $hq_id . "<br>";
// echo "fromDate " . $fromDate . "<br>";
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

   $triggerArray = array();

   // "VOP" = Vorortprüfung
   $tmpCategory = "102";  // VOP
   $tmpField = "309";     // Special field for this form containing the create time
   $sqlquery = "SELECT mtfv.mtfv_id"
             . " FROM metafieldcategory AS mtfc, metafieldkey AS mtfk, metafieldcategorykey AS mtfck, metafieldvalue AS mtfv"
             . " WHERE mtfck.mtfc_id = '" . $tmpCategory . "' AND"
             . "       mtfck.mtfc_id = mtfc.mtfc_id AND"
             . "       mtfck.hq_id = '" . $tmpHqId . "' AND"
             . "       mtfk.mtfk_id = '" . $tmpField . "' AND"
             . "       mtfck.mtfk_id = mtfk.mtfk_id AND"
             . "       mtfv.mtfck_id = mtfck.mtfck_id AND"             
             . "       mtfv.mtfv_value != '' AND"
             . "       CONCAT(SUBSTRING(mtfv.mtfv_value,7,4),SUBSTRING(mtfv.mtfv_value,4,2),SUBSTRING(mtfv.mtfv_value,1,2)) >= '" . $fromDate . "'";
// echo "<br><br>" . $sqlquery . "<br>";
   $result = $db->query($sqlquery);   
   if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
   while ($row = $result->fetch_assoc()):
         $csId = getFieldValueFromId("phoenix_group.report_process", "rp_id", $row["mtfv_id"], "rp_objid");  // Get cs_id from object ID
         $csHqId = getFieldValueFromId("phoenix.customer", "cs_id", $csId, "hq_id");
         if ($hq_id == "" || ($hq_id >= "0" && $csHqId == $hq_id)) :
            echo $row["mtfv_id"] . "<br>";
            $triggerArray[] = $csId;
         endif;
   endwhile;
   $result->free();
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// print_r($triggerArray);
// die();
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   $triggerArrayLen = count($triggerArray);

   if ($triggerArrayLen > 0) :
      $sendPerMail = true;  // At least one event to be mailed

      // for ($i = 0; $i < $triggerArrayLen; $i++) :      
for ($i = 0; $i < 1; $i++) :
          $csId = $triggerArray[$i];
          
          include_once ("../include/inc_metafield.inc.php");
      endfor;
   endif;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$sendPerMail = false;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   
   // Send via email
   if ($sendPerMail) :

      $mailSenderAddress = "support@assecutor.de";
      $mailReceiverAddressArray = array("orga@assecutor.de");
      $mailSubject = "VORORTPRÜFUNG";
      $mailCcAddress = "";
      $mailBccAddress = "support@assecutor.de";
      
      $mailtext = "VORORTPRÜFUNG<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 (false && !$autoMode) : ?>

<html>

<head>

</head>
	
	
<body>

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

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

</center>

</body>	
	
</html>

<?php endif; ?>
