<?php

include_once ("../include/mcglobal.inc.php");
include_once ("../include/html.inc.php");
include_once ("../include/inc_html2pdf.inc.php");
// include_once ("htmlMimeMail.php");
// include_once ("../include/barcode_BCGcode39_saveImage.php");
// include_once ("../include/barcode_BCGcode128_saveImage.php");

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


// getSecHttpVars("0", array("bo_id", "magic", "subject", "body", "filename_suffix"));


$currentClientIP = trim($_SERVER['REMOTE_ADDR']);

$currentTime = date("Y-m-d H:i:s");
$currentDate = date("d.m.Y");
$currentTimeSpecial_01 = date("Ymd_His");

$absoluteSystemPath = getAbsoluteSystemPath();
$outputFormatField = defineOutputFormats();

$subject = trim($subject);
$body = trim($body);
$filename_suffix = trim($filename_suffix);

$logFile = "../log/HLS_Safebags_Forgotten" . ($filename_suffix != "" ? "_" . $filename_suffix : "") . ".log";
$csvFile = $currentTimeSpecial_01 . ($filename_suffix != "" ? "_" . $filename_suffix : "") . ".csv";
$csvPathAndFile = "../temp/download/" . $csvFile;
$hqId = "1";
$title = "HHA :: Vermeintlich vergessene Safebags";
$opState = "";
$mailResultMsg = "";


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

   // echo "currentClientIP = " . $currentClientIP . "\n";
   
   if (true || $currentClientIP != "") :

      $f_article_type = "4285";  // Safebags Geld

      // ATTENTION: Currently without stock ["....AND atih.stk_id = '" . $stkIdObject . "'...."]
      $sqlquery = "SELECT atih.csc_id, csc.csc_name, atih.usr_id, usr.usr_name, usr.usr_firstname, atih.atih_serialno, atih.atih_data_04, atih.atih_data_05, atih.atih_data_28, atih.atih_data_29, atih.atih_data_29"
                . "  FROM phoenix_log.articleitemhistory AS atih, phoenix.costcenter AS csc, phoenix.user AS usr"
                . "  WHERE atih.at_id = '" . $f_article_type . "' AND atih_data_28 = '1' AND atih.atih_createtime <= DATE_SUB(NOW(), INTERVAL 7 DAY) AND"
                . "        atih.csc_id = csc.csc_id AND atih.usr_id = usr.usr_id"
                . "  ORDER BY atih.csc_id, atih.atih_data_04";

      $result = $db->query($sqlquery);
      if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
      $i = 0;
      while ($row = $result->fetch_assoc()):

            $output .= "<tr>\n";
            $output .= "<td align=\"right\">" . ($i + 1) . "&nbsp;</td>\n";
            $output .= "<td>" . $row["csc_name"] . "&nbsp;</td>\n";
            $output .= "<td>" . $row["atih_serialno"] . "&nbsp;</td>\n";
            $output .= "<td>" . substr($row["atih_data_04"], 0, 10) . "&nbsp;</td>\n";
            $output .= "<td align=\"right\">" . number_format($row["atih_data_05"], 2, ",", "") . "&nbsp;</td>\n";
            $output .= "<td>" . $row["usr_name"] . ", " . $row["usr_firstname"] . "&nbsp;</td>\n";
            $output .= "</tr>\n";
            $i++;
      endwhile;
      $result->free();

      // Send mail if there is at least one forgotten safebag
      if ($i > 0) :

         $mailtext  = "<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">"
                    . "<style>"
                    . " .table1 {border-collapse: collapse; border: 1px solid black;}"
                    . " .td1 {border: 1px solid black;}"
                    . "</style>"
                    . "</head><body>";
    
         $headline = "";
         $headline .= "<tr>";
         $headline .= "<td class=\"td1\">&nbsp;" . getLngt("Pos.") . "&nbsp;</td>";
         $headline .= "<td class=\"td1\">&nbsp;" . getLngt("Verkaufsstelle") . "&nbsp;</td>";
         $headline .= "<td class=\"td1\">&nbsp;" . getLngt("Safebag") . "&nbsp;</td>";
         $headline .= "<td class=\"td1\">&nbsp;" . getLngt("Datum") . "&nbsp;</td>";
         $headline .= "<td class=\"td1\">&nbsp;" . getLngt("Betrag") . "&nbsp;</td>";
         $headline .= "<td class=\"td1\">&nbsp;" . getLngt("Mitarbeiter") . "&nbsp;</td>";
         $headline .= "</tr>";
   
         $mailtext .= "<table border=\"0\">";
         $mailtext .= $headline;
         $mailtext .= $output;
         $mailtext .= "</table>";
         $mailtext .= "</body>";
   
         // Generate PDF document
         // $fileNameTarget = generatePDF($mailtext, $title, "0", "0", "", "", "");
   
         // Send mail with attached (generated) PDF document
         if (true) :
   
            $mailSubject = $title;
            if ($subject != "") :
               $mailSubject = $subject;
            endif;
            $mailToAddresses = "ASSECUTOR-INFO@hochbahn.de";
// $mailToAddresses = "marcelo.juliao_de_seixas@hochbahn.de";
            $mailAttachedFileType = "";
            $mailCcAddresses = "";
            $mailBccAddresses = "proj.hha@assecutor.de";
// $mailBccAddresses = "";
            $mailSenderAddress = getParameterValue("0", "MAIL_SENDER_ADDRESS", $hqId);
            if ($mailSenderAddress == "") :
               $mailSenderAddress = "sys-op@assecutor.de";
            endif;
            // Generate mail (object)
            $mailObj = new htmlMimeMail();
            $mailObj->setFrom($mailSenderAddress);
            if ($mailCcAddresses != "") :
               $mailObj->setCc($mailCcAddresses);
            endif;
            if ($mailBccAddresses != "") :
               $mailObj->setBcc($mailBccAddresses);
            endif;
            $mailObj->setSubject($mailSubject);
   
            // Mail content
            $mailObj->setHtml($mailtext, null, "./");
            // $mailObj->setText($mailtext);
   
            /*
            if (file_exists($csvPathAndFile)) :
               $attachment = $mailObj->getFile($csvPathAndFile);
               $mailObj->addAttachment($attachment, $csvFile, 'text/csv');
            endif;
            */
   
            $mailResult = $mailObj->send(array($mailToAddresses), 'smtp');
            // $mailResult = sendPDFMail($hqId, $mailtext, $title, $mailToAddresses, $fileNameTarget, $mailSubject, $mailText, $mailAttachedFileType, $mailCcAddresses, $mailBccAddresses, $mailSenderAddress, $parKeyForLogFile, $objId, $cascadingObjType);
   
            if ($mailResult) :
               $mailResultMsg = "OK";
            else : 
               $mailResultMsg = "NOK";
            endif;
            $opState = $mailResultMsg;
         endif;
      endif;
   else :
      $opState = "ERR 101 : Unknown client IP!";
   endif;
else :                                       
   // $opState = "ERR 100 : Authentication!";
endif;


// Append to log file
writeToFile($logFile, "datetime=" . $currentTime . "|IP=" . $currentClientIP . "|cr_id=" . $boExtId1 . "|day=" . $boExtId0 . "|bo_id=" . $boId . "|mail_result=" . $mailResultMsg . "|op_state=" . $opState);

// echo "OK";
// echo $opState;
?>
