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

include_once ("../include/mcglobal.inc.php");

//getSecHttpVars("1",array("autoExportDKT"));

$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/inc_stock.inc.php");
include_once ('../include/email/htmlMimeMail.php');


if ($autoMode || $usr_id == "826762" || $usr_id == "829457" || $usr_id == "833608" || $usr_id == "826767") :

   // Get user
   $usrName = getFieldValueFromId("user","usr_id",$usr_id,"usr_name");
   $usrFirstname = getFieldValueFromId("user","usr_id",$usr_id,"usr_firstname");

   // Globals
   $f_secretFileName = "";
   $f_stkat_export = "1";
   $stkIdRoot = 16;
   
   // Init
   $sendPerMail = true;
   
   // Locals
   $sqlStmt = "SELECT stk.stk_id, stk.stk_name,"
             . "      CONCAT(stkat.at_id, '|', at.at_eid, '|', at.at_serialno, '|', stk.stk_id) AS at_x,"
             . "      at.at_eid, at.at_name, at.at_match, at.at_serialno, stkat.stkat_maxquantity, stkat.stkat_quantity, stkat.stkat_itemquantity,"
             . "      CEILING(stkat.stkat_itemquantity / at.at_bundlequantity) AS bundlequantity, atg.atg_name " 
             . " FROM stock AS stk, article AS at, stockarticle AS stkat" 
             . "      LEFT JOIN articlegroupitem AS atgi ON stkat.at_id = atgi.at_id AND atgi.hq_id = '11'"
             . "      LEFT JOIN articlegroup AS atg ON atg.atg_id = atgi.atg_id"
             . " WHERE stk.stk_id = stkat.stk_id AND stkat.at_id = at.at_id AND (stk.stk_id = '16' OR stk.stk_path LIKE '%//16//%' )" 
             . " ORDER BY stk.stk_name, at.at_name";
   
   $titleArray = array("Lagerort", "Artikelnr.", "Bezeichnung", "Zusatz", "Stückzahl", "Artikelverbund");
   $fieldArray = array("stk_name", "at_x", "at_name", "at_match", "stkat_itemquantity", "atg_name");
   $aligns = "l,l,l,l,r,l";
   $filename = "LAGERBESTAND";
   $postParserField = "at_x";
   $postParserFunction = "postParseStockArticle";
   
   stockFilesOutputCSV($sqlStmt, $titleArray, $fieldArray, $aligns, $filename, $postParserField, $postParserFunction);
   
   
   // Send via email
   if ($sendPerMail && $f_secretFileName != "") :
   
      $mailSenderAddress = "support@assecutor.de";
      $mailReceiverAddressArray = array("abt@daskurierteam.de","angele@daskurierteam.de");
      $mailSubject = "Lagerartikelbestand DKT";
      $mailCcAddress = "";
      $mailBccAddress = "support@assecutor.de";
      
      $mailtext = "LAGERARTIKELBESTAND DKT<br><br>";
      if (!$autoMode) :
         $mailtext .= "Ausgeführt von: " . $usrFirstname . " " . $usrName;
      endif;
   
      $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>

</head>
	
	
<body>

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

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

</center>

</body>	
	
</html>

<?php endif; ?>