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

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


// Overload function because of call by console
function authCheckEmployeeRights($emp_id, $menuModeId, $referer = "") {
  return true;
}


$errMsg = "";
$hq_id = "";
if (isset($argv[1]) && $argv[1] == "acapella7890") :
   if (isset($argv[2]) && $argv[2] != "") : 
      $hq_id = $argv[2];
      if (isset($argv[3]) && $argv[3] != "") : 
         $currentTimestamp = $argv[3];
      else :
         $errMsg = "average_increase failed by missing call timestamp";
      endif;
   else :
      $errMsg = "average_increase failed by missing headquarters ID";
   endif;
else :
   $errMsg = "average_increase failed by access code";
endif;

if ($errMsg != "") :
   sendInternalMail($errMsg . " [HQ_ID:" . $hq_id . "] " . " [TIMESTAMP:" . $currentTimestamp . "] ");
   die();
endif;


ini_set('memory_limit', '256M');

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


include_once ("../include/inc_stock.inc.php");
include_once ("../illt/inc_serviceunit.inc.php");


$fire = true;

// After the data are imported, compute the major probability regarding the service intervals
// Do this here and store the current states into "articleitem", because a realtime computation is not to be recommended
if ($fire) :

   // Get number of all article types
   $articleGroupPhrase = "(at_group LIKE '%,1,%') ";
   // $numOfAllArticleTypes = getCountOfTable("article", $articleGroupPhrase);  // Number of ALL article types
   $articleTypeArray = getColVectorFromDB2ArrayByClause("article", "at_id", $articleGroupPhrase, "", "at_id", "");
   $numOfAllArticleTypes = count($articleTypeArray);

   if ($numOfAllArticleTypes > 0) :
      $rootStockSU = getParameterValue("0", "SU_STK_ROOT", $hq_id);    // Get root stock where all service units belong to
      if ($rootStockSU != "" && is_numeric($rootStockSU)) :
         $suSerialNoArray = getColVectorFromDB2ArrayByClause("phoenix.articleitem", "ati_serialno", "at_id IN (" . implode(",", $articleTypeArray) . ")", "", "");
         $suSerialNoArrayLen = count($suSerialNoArray);
         for ($i = 0; $i < $suSerialNoArrayLen; $i++) :
             // $probabilityToNextServiceMax = getSuHistoryData("2", "AND stk.stk_pre_id = '" . $rootStockSU . "' AND atih.atih_serialno = '" . $suSerialNoArray[$i] . "'");
             // updateStmt("phoenix.articleitem", "ati_serialno", $suSerialNoArray[$i], array("ati_data_18", $probabilityToNextServiceMax));
             // // $outReportArray[] = $suSerialNoArray[$i] . " <- Serviceinterval neu berechnet: " . $probabilityToNextServiceMax;
      
             $serviceStatisticDataArray = getSuHistoryData("2", "AND stk.stk_pre_id = '" . $rootStockSU . "' AND atih.atih_serialno = '" . $suSerialNoArray[$i] . "'", "", "2");
             $probabilityToNextServiceMax = $serviceStatisticDataArray[0];
             $averageIncreaseValues = implode(",", $serviceStatisticDataArray[1]);
             updateStmt("phoenix.articleitem", "ati_serialno", $suSerialNoArray[$i], array("ati_data_18", $probabilityToNextServiceMax, "ati_data_26", $averageIncreaseValues));
         endfor;
      endif;
   endif;
endif;

?>