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

include_once ("../include/mcglobal.inc.php");
// include_once ("../include/inc_job.inc.php");
include_once ('../include/email/htmlMimeMail.php');


$autoMode = false;
$doExecute = false;
if (!isset($argv[1]) || $argv[1] == "") :
   include_once ("../include/auth.inc.php");
   $autoMode = false;
   $doExecute = true;
else :
   if ($argv[1] == "acapella7890") :
       $autoMode = true;
       $doExecute = true;
   endif;
endif;

$out = "";
$f_secretFileName = "";


// Get jobs for auto_export
function getDoneJobs ($startTime, $endTime, $parMaxPrice = -500) {
  global $db, $PHP_SELF;
  $retArray = array();

  // Today only
  if ($startTime == "") :
     $startTime = getDateTime("3") . " 00:00:00";
  endif;
  if ($endTime == "") :
     $endTime = getDateTime("3") . " 23:59:59";
  endif;

  // (jb_id_parent = 0) <=> Keine UN-Verrechnung !!!!
  $sqlquery = "SELECT jb.jb_id, jb.jb_id_parent, jb.jb_ordertime, jb.jb_finishtime, jb.jb_totalprice, jb.cr_sid, hq.hq_mnemonic, cs.cs_eid, cmp.cmp_comp, cmp.cmp_comp2"
            . " FROM job AS jb, headquarters AS hq, costcenter AS csc, customer AS cs, company AS cmp"
            . " WHERE (isnull(jb.jb_storno) OR jb.jb_storno = '0' OR jb.jb_storno = '3') AND"
            . "       jb.jb_status = '2'                         AND"
            . "       jb.jb_offer = '0'                          AND"
            . "       jb.jb_booktime >= '" . $startTime . "'     AND"
            . "       jb.jb_booktime <= '" . $endTime . "'       AND"
            . "       jb.jb_totalprice <= '" . $parMaxPrice . "' AND"
            . "       jb.hq_id = hq.hq_id                        AND"
            . "       jb.csc_id_payer = csc.csc_id               AND"
            . "       csc.cs_id = cs.cs_id                       AND"
            . "       cs.cmp_id = cmp.cmp_id                        "
            . " ORDER BY hq.hq_mnemonic, jb.jb_id";
// echo "sqlquery : " . $sqlquery . "\n\n";
  $result = $db->query($sqlquery);
  if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
  while ($row = $result->fetch_assoc()):
        $retArray[] = $row;
  endwhile;
  $result->free();
  return $retArray;
}


// **** MAIN ****

if ($doExecute) :

   // Parameter for (negative) max price
   $parMaxPrice = getParameterValue("0", "CRON_JOB_MAX_PRICE", "0");

   $startTime = getDateTime("3") . " 00:00:00";
   $endTime = getDateTime("3") . " 23:59:59";

   $jbArray = getDoneJobs($startTime,$endTime,$parMaxPrice);
   $jbArrayLen = count($jbArray);

   if ($jbArrayLen > 0) :

      $out .= "<html>\n";
      $out .= "<body>\n";
      /*
      $out .= "<br>";
      $out .= "<table>\n";
      $out .= "<tr>\n";
      $out .= "<td>&nbsp;HEUTIGE GUTSCHRIFTEN ÜBER " . abs($parMaxPrice) . " €</td>";
      $out .= "</tr>\n";
      $out .= "</table>\n";
      */
      $out .= "<br>";
      $out .= "<table>\n";
      $out .= "  <tr>\n";
      $out .= "    <td align=\"center\">&nbsp;<b>" . "NL" . "</b>&nbsp;</td>";
      $out .= "    <td align=\"center\">&nbsp;<b>" . "AUFTRAG" . "</b>&nbsp;</td>";
      $out .= "    <td align=\"center\">&nbsp;<b>" . "HAUPTAUFTRAG" . "</b>&nbsp;</td>";
      $out .= "    <td align=\"center\">&nbsp;<b>" . "BETRAG" . "</b>&nbsp;</td>";
      $out .= "    <td align=\"center\">&nbsp;<b>" . "AUSFÜHRUNGSDATUM" . "</b>&nbsp;</td>";
      $out .= "    <td align=\"center\">&nbsp;<b>" . "FAHRZEUG" . "</b>&nbsp;</td>";
      $out .= "    <td align=\"left\">&nbsp;<b>" . "EID" . "</b>&nbsp;</td>";
      $out .= "    <td align=\"left\">&nbsp;<b>" . "FIRMA" . "</b>&nbsp;</td>";
      $out .= "    <td align=\"left\">&nbsp;<b>" . "&nbsp;" . "</b>&nbsp;</td>";
      $out .= "  </tr>\n";
      for ($i = 0; $i < $jbArrayLen; $i++) :
          $out .= "  <tr>\n";
          // $out .= "<td style=\"background-color: #" . $rowColor . ";\">&nbsp;" . $tmpArray[$j] . ($rowColor == "FF0000" && $j == "6" ? "&nbsp;[überschritten]" : "") . "&nbsp;&nbsp;&nbsp;</td>\n";
          $out .= "    <td>&nbsp;" . $jbArray[$i]["hq_mnemonic"] . "&nbsp;</td>";
          $out .= "    <td align=\"right\">&nbsp;" . $jbArray[$i]["jb_id"] . "&nbsp;</td>";
          $out .= "    <td align=\"right\">&nbsp;" . ($jbArray[$i]["jb_id_parent"] > "0" ? $jbArray[$i]["jb_id_parent"] : "") . "&nbsp;</td>";
          $out .= "    <td align=\"right\">&nbsp;" . number_format(round($jbArray[$i]["jb_totalprice"],2), 2, ",", ".") . "&nbsp;</td>";
          $out .= "    <td align=\"center\">&nbsp;" . substr($jbArray[$i]["jb_ordertime"],8,2) . "." . substr($jbArray[$i]["jb_ordertime"],5,2) . "." . substr($jbArray[$i]["jb_ordertime"],0,4) . "&nbsp;</td>";
          $out .= "    <td>&nbsp;" . $jbArray[$i]["cr_sid"] . "&nbsp;</td>";
          $out .= "    <td>&nbsp;" . $jbArray[$i]["cs_eid"] . "&nbsp;</td>";
          $out .= "    <td>&nbsp;" . $jbArray[$i]["cmp_comp"] . "&nbsp;</td>";
          $out .= "    <td>&nbsp;" . $jbArray[$i]["cmp_comp2"] . "&nbsp;</td>";
          $out .= "  </tr>\n";
      endfor;
      $out .= "</table>\n";
      $out .= "</body>\n";
      $out .= "</html>\n";


      // Send via email
      $sendPerMail = true;
      if ($sendPerMail) :

         $mailSenderAddress = "support@assecutor.de";

         // Receiver of the mail
         $mailReceiverAddressArray = array("support@assecutor.de");
         $mailReceiverAddress = getParameterValue("0", "MAIL_CRON_105_TO_ADDRESS", "0");
         if ($mailReceiverAddress != "") :
            $mailReceiverAddressArray = spliti(",",$mailReceiverAddress);
         endif;
// $mailReceiverAddressArray = array("mv@assecutor.de");

         $mailSubject = "GUTSCHRIFTEN ÜBER " . abs($parMaxPrice) . " €";
         $mailCcAddress = "";
         $mailBccAddress = "";

         $mailtext = "<b>GUTSCHRIFTEN ÜBER " . abs($parMaxPrice) . " €</b><br>";
         $mailtext .= $out;

         $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');
      endif;
   endif;
endif;
?>
