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


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


function mcStrWrap($aStr)
{
	$aStr = str_replace("'", "", $aStr);
	$aStr = removeFieldSigns($aStr);
  // $aStr = str_replace("'", "\'", $aStr);
  /*
  $aStr = utf8_decode($aStr);
  $aStr = str_replace("\"", '', $aStr);
  $aStr = str_replace("ÃŸ", "ß", $aStr);
  $aStr = str_replace("Ã¤", "ä", $aStr);
  $aStr = str_replace("Ã¶", "ö", $aStr);
  $aStr = str_replace("Ã¼", "ü", $aStr);
  $aStr = str_replace("Ã„", "Ä", $aStr);
  $aStr = str_replace("Ã–", "Ö", $aStr);
  $aStr = str_replace("Ãœ", "Ü", $aStr);
  */
  return $aStr;
}

// *****************************************************************************


$hasAccess = false;
if (!isset($argv[1]) || $argv[1] == "") : 
   getSecHttpVars("0", array("auth"));
   if ($auth == "acapella7890") :
      $hasAccess = true;
   endif;
else :
   if ($argv[1] == "acapella7890") :
      $hasAccess = true;
   endif;
endif;

if ($hasAccess) :

   $attachFile = false;
   $currentTime = getDateTime("0");
   $currentTimeFile = getDateTime("6");  
   $tmpPdfPath = "../log/";
   $csvFileDefault = "CSV_Request_Log.csv";
   $csvFile = "CSV_Request_Log_" . $currentTimeFile . ".csv";

   // Remove old files
   $csvFilePrefix = substr($csvFileDefault,0,15);
   foreach (glob($tmpPdfPath . $csvFilePrefix . "_2*") as $f) {
     unlink($f);
   }

   // Send mail to target address
   $mailObj = new htmlMimeMail();
   $mailObj->setFrom("auto-mailer@assecutor.de");
   // $mailObj->setCc("admin@assecutor.de");
   $mailObj->setBcc("auto-mailer@assecutor.de");
   $mailObj->setSubject("IKEA :: FAILED REQUESTS! (" . $currentTime . ")");

   $doSend = false;
   $mailtext = "";

   if (file_exists($tmpPdfPath . $csvFileDefault)) :
      // Rename file with timestamp
      rename($tmpPdfPath . $csvFileDefault, $tmpPdfPath . $csvFile);

      if (file_exists($tmpPdfPath . $csvFile)) :
         // Attachement
         if ($attachFile) :
            $attachment = $mailObj->getFile($tmpPdfPath . $csvFile);
            $mailObj->addAttachment($attachment, $csvFile, 'text/csv');

         else :
            $rowToImport = array();
            $rowToImport[0]  = "f_cs_eid";
            $rowToImport[1]  = "f_ordertime";
            $rowToImport[2]  = "f_no_commission";
            $rowToImport[3]  = "f_no_related";
            $rowToImport[4]  = "f_no_service";
            $rowToImport[5]  = "f_op";
            $rowToImport[6]  = "f_jb_id_cancellation";
            $rowToImport[7]  = "f_jb_id_update";
            $rowToImport[8]  = "f_jb_id_parent";
            $rowToImport[9]  = "f_dummy_4";

            $rowToImportLen = count($rowToImport);

            $data = importCSV($tmpPdfPath . $csvFile, ";", "1");
            $dataLen = count($data);

            $rem_no_commission = array();

            // Loop all rows (EXCEPT header row [$j = 1])
            for ($j = 0; $j < $dataLen; $j++) {

               // Loop for all fields of $fields of the row
               for ($i = 0; $i < $rowToImportLen; $i++) {
                   ${$rowToImport[$i]} = $data[$j][$i];
               }

               $f_cs_eid = mcStrWrap($f_cs_eid);
               $f_ordertime = mcStrWrap($f_ordertime);
               $f_no_commission = mcStrWrap($f_no_commission);
               $f_no_related = mcStrWrap($f_no_related);
               $f_no_service = mcStrWrap($f_no_service);

               $f_jb_id = mcStrWrap($f_jb_id_cancellation);
               if ($f_jb_id == "") :
                  $f_jb_id = mcStrWrap($f_jb_id_update);
                  if ($f_jb_id == "") :
                     $f_jb_id = mcStrWrap($f_jb_id_parent);
                  endif;
               endif;

               // $f_cmp_id = getFieldValueFromId("customer","cs_eid",$f_cs_eid,"cmp_id");
               // $f_cmp_comp = getFieldValueFromId("company","cmp_id",$f_cmp_id,"cmp_id");

               $f_tr_comp1 = getFieldValueFromClause("tour","tr_comp","jb_id = '" . $f_jb_id . "' AND tr_sort = '1'");
               $f_tr_comp2 = getFieldValueFromClause("tour","tr_comp","jb_id = '" . $f_jb_id . "' AND tr_sort = '2'");
               // if (!(strpos($f_tr_comp, "IKEA") === FALSE)) :		
               //    $f_tr_comp1 = getFieldValueFromClause("tour","csc_id","jb_id = '" . $f_jb_id . "' AND tr_sort = '1'");
               // endif;

               if ($rem_no_commission[$f_no_commission] != "1")  :
                  $mailtext .= $f_tr_comp1 . " -> " . $f_tr_comp2 . " | Auftrag: " . $f_jb_id . " | Nr.: " . $f_no_commission . " | Service-Nr. " . $f_no_service . "\r\n";
               endif;

               $rem_no_commission[$f_no_commission] = "1";
            }
         endif;
         $doSend = true;
      endif;
   endif;

   if ($doSend) :
      // PLAIN-TEXT
      if ($mailtext != "") :
         $mailObj->setText($mailtext);
      endif;

      $mailResult = $mailObj->send(array("hh-mt.dispo@hansetrans.de"), 'smtp');

      if ($mailResult) :
         echo "MAIL SENT!";
      else :
         echo "MAIL NOT SENT!";
      endif;
   endif;
   $mailObj = NULL;

else :
   $randNum = rand(1, 10);
   if ($randNum == 1) :
      echo "SO NICHT!";
   elseif ($randNum == 2) :
      echo "ONCE MORE AGAIN!";
   elseif ($randNum == 3) :
      echo "LMAA! ;-)";
   elseif ($randNum == 4) :
      echo "OCH KOMM...!";
   elseif ($randNum == 5) :
      echo "HASTES BALD...?!";
   elseif ($randNum == 6) :
      echo "TRY IT AGAIN, BABY!";
   elseif ($randNum == 7) :
      echo "COME GET SOME!";
   elseif ($randNum == 8) :
      echo "SHAKE IT, BABY!";
   elseif ($randNum == 9) :
      echo "LOOOOOOS.......ER! ;-) ;-)";
   elseif ($randNum == 10) :
      echo "I WILL RIP OFF YOUR HEAD AND SHIT DOWN YOUR NECK! ;-)";
   else :
      echo "NU ABER...!";
   endif;
endif;
?>
