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

include_once ("import.php");

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


// Select user-type for mode of security check 
$userType = getFieldValueFromId("user","usr_id",$usr_id,"usr_type");
$userTypeName = getUserTypeName($userType);

// Get the rights of the employee logged in and check the accessibility
if (!authCheckEmployeeRights($emp_id, "7")) : die ("$PHP_SELF: Access denied!"); endif;


// SPEZIELLER DATEINAME
$importPath = "../temp/";
$f_importFile = "hha_objekte.csv";

$currentDate = getDateTime("3") . " 03:00:00";
$fire = false;

// Import process
if (true) :

   if (true) :

      // 1547 	BS_01
      // 1742 	FABS-AT-LAGER
      $stkId = "1742";
 
      $bekCountINS = 0;
      $bekCountEXIST = 0;
      $mekCountINS = 0;
      $mekCountEXIST = 0;
      $hop1CountINS = 0;
      $hop1CountEXIST = 0;
      $hop2CountINS = 0;
      $hop2CountEXIST = 0;
      $hop3CountINS = 0;
      $hop3CountEXIST = 0;
 
      $atIdMappingArr = array();
      $atIdMappingArr["BEK"] = "4281";
      $atIdMappingArr["MEK"] = "4282";
      $atIdMappingArr["HOP1"] = "4289";
      $atIdMappingArr["HOP2"] = "4287";
      $atIdMappingArr["HOP3"] = "4288";

      echo "<br><br><br><br><span class=\"f12bp1_red\" colspan=\"2\">Verarbeitungsprotokoll:</span><br><br>";
      echo "Import-Datei: " . $f_importFile . "<br><br>";

      $rowToImport = array();
      $rowToImport[0]  = "bek";
      $rowToImport[1]  = "mek";
      $rowToImport[2]  = "hop1";
      $rowToImport[3]  = "hop2";
      $rowToImport[4]  = "hop3";
      $rowToImport[5]  = "dummy";

      $rowToImportLen = count($rowToImport);

      // read file to import
      $data = importCSV($importPath . $f_importFile);
      $dataLen = count($data);

      // loop all rows
      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];
         }         

         // $logJbId = getFieldValueFromClause("phoenix_log.import","jb_id","imp_id = '" . $f_jb_tourname . "' AND imp_pos = '" . $f_position . "'");

         if (true) :
         
            $bek = trim($bek);
            $mek = trim($mek);
            $hop1 = trim($hop1);
            $hop2 = trim($hop2);
            $hop3 = trim($hop3);
            
            echo "[" . pad($j,3) . "] " . "BEK=" . $bek . " " . "MEK=" . $mek . " " . "HOP1=" . $hop1 . " " . "HOP2=" . $hop2 . " " . "HOP3=" . $hop3 . "<br>";
            
            $bek = substr($bek,3);
            $mek = substr($mek,3);
            $hop1 = substr($hop1,3);
            $hop2 = substr($hop2,3);
            $hop3 = substr($hop3,3);
            
            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . "BEK=" . $bek . " " . "MEK=" . $mek . " " . "HOP1=" . $hop1 . " " . "HOP2=" . $hop2 . " " . "HOP3=" . $hop3 . "<br>";
            
            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

            if ($bek != "") :
                $bekId = getFieldValueFromId("articleitem","ati_serialno",$bek,"ati_id");
                if ($bekId == "") : 
                   echo "BEK=INS "; 
                   $bekCountINS++;
                else : 
                   echo "BEK=EXIST ";
                   $bekCountEXIST++;
                endif;
            endif;

            if ($mek != "") :
                $mekId = getFieldValueFromId("articleitem","ati_serialno",$mek,"ati_id");
                if ($mekId == "") : 
                   echo "MEK=INS ";
                   $mekCountINS++;
                else : 
                   echo "MEK=EXIST ";
                   $mekCountEXIST++;
                endif;
            endif;

            if ($hop1 != "") :
                $hop1Id = getFieldValueFromId("articleitem","ati_serialno",$hop1,"ati_id");
                if ($hop1Id == "") : 
                   echo "HOP1=INS ";
                   $hop1CountINS++;
                else : 
                   echo "HOP1=EXIST ";
                   $hop1CountEXIST++;
                endif;
            endif;

            if ($hop2 != "") :
                $hop2Id = getFieldValueFromId("articleitem","ati_serialno",$hop2,"ati_id");
                if ($hop2Id == "") : 
                   echo "HOP2=INS ";
                   $hop2CountINS++;
                else : 
                   echo "HOP2=EXIST ";
                   $hop2CountEXIST++;
                endif;
            endif;

            if ($hop3 != "") :
                $hop3Id = getFieldValueFromId("articleitem","ati_serialno",$hop3,"ati_id");
                if ($hop3Id == "") : 
                   echo "HOP3=INS ";
                   $hop3CountINS++;
                else : 
                   echo "HOP3=EXIST ";
                   $hop3CountEXIST++;
                endif;
            endif;

            echo "<br><br>";

            if ($fire) :

               if ($bek != "" && $bekId == "") : 
                  insertStmt("articleitem", array("at_id", $atIdMappingArr["BEK"], "stk_id", $stkId, "ati_serialno", $bek, "ati_data_04", $currentDate));
               endif;

               if ($mek != "" && $mekId == "") : 
                  insertStmt("articleitem", array("at_id", $atIdMappingArr["MEK"], "stk_id", $stkId, "ati_serialno", $mek, "ati_data_04", $currentDate));
               endif;

               if ($hop1 != "" && $hop1Id == "") : 
                  insertStmt("articleitem", array("at_id", $atIdMappingArr["HOP1"], "stk_id", $stkId, "ati_serialno", $hop1, "ati_data_04", $currentDate));
               endif;

               if ($hop2 != "" && $hop2Id == "") : 
                  insertStmt("articleitem", array("at_id", $atIdMappingArr["HOP2"], "stk_id", $stkId, "ati_serialno", $hop2, "ati_data_04", $currentDate));
               endif;

               if ($hop3 != "" && $hop3Id == "") : 
                  insertStmt("articleitem", array("at_id", $atIdMappingArr["HOP3"], "stk_id", $stkId, "ati_serialno", $hop3, "ati_data_04", $currentDate));
               endif;
            endif;   
         endif;
      }

      echo "BEK::INS=" . $bekCountINS . "<br>";
      echo "BEK::EXIST=" . $bekCountEXIST . "<br>";
      echo "MEK::INS=" . $mekCountINS . "<br>";
      echo "MEK::EXIST=" . $mekCountEXIST . "<br>";
      echo "HOP1::INS=" . $hop1CountINS . "<br>";
      echo "HOP1::EXIST=" . $hop1CountEXIST . "<br>";
      echo "HOP2::INS=" . $hop2CountINS . "<br>";
      echo "HOP2::EXIST=" . $hop2CountEXIST . "<br>";
      echo "HOP3::INS=" . $hop3CountINS . "<br>";
      echo "HOP3::EXIST=" . $hop3CountEXIST . "<br>";
      echo "<br>";
      $sumExist = $bekCountEXIST + $mekCountEXIST + $hop1CountEXIST + $hop2CountEXIST + $hop3CountEXIST;
      $sumIns = $bekCountINS + $mekCountINS + $hop1CountINS + $hop2CountINS + $hop3CountINS;
      echo "SUMME EXISTENTER OBJEKTE: " . $sumExist . "<br>";
      echo "SUMME NEUER OBJEKTE: " . $sumIns . "<br>";
      

      // Remove import file
      // $retVal = removeFile($f_importFile, $importPath, $pathSeparator);
   endif;      
endif;
?>

