<?php
  include_once ("../include/global.inc.php");
  include_once ("../include/inc_stock.inc.php");
 
  // Check HTTP-Parameters
  // Stocks
  getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdCurrent", "statusMessage", "deactivateMenu",
                           "mode", "scanCode", "stkIdRoot"));

  if ($mode != "") :
     header("Content-Type: text/html; charset=ISO-8859-1\n");
  endif;

// echo "alert('" . $mode . " ' + '" . $scanCode . " ');";

  if ($mode == "0") :
     echo "scanCodeIsSerialNo = false;";
     echo "scanCodeIsArticleBarcode = false;";
     echo "scanCodeIsStockBarcode = false;";

  elseif ($mode == "1") :
     // Check scan code is a serial no.
     $whereClause = "stkmv.stkmv_serialno = '" . $scanCode . "' AND stkmv.stk_id_to != '0'";  // Get the "outgoing good" entry of the journal with this special serial number
     // $orderByClause = "stkmv.stkmv_timestamp DESC";                                           // Get the youngest entry (current place), e.g. this article was put from stoch A to stock B ...

     $sqlStmt = getStmtStockJournal($whereClause, $orderByClause);
     $result = $db->query($sqlStmt);
     if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

     $count = 0;
     while ($row = $result->fetch_assoc()):
       $tmpStkMvData = "";
       $tmpStkMvData .= "'" . $row["stkmv_id"] . "',";
       $tmpStkMvData .= ($row["stk_id_from"] == "" ? "'0'," : "'" . $row["stk_id_from"] . "',");
       $tmpStkMvData .= "'" . $row["at_id"] . "',";
       $tmpStkMvData .= ($row["stk_id_to"] == "" ? "'0'," : "'" . $row["stk_id_to"] . "',");
       $tmpStkMvData .= "'" . $row["stkmv_tan"] . "',";
       $tmpStkMvData .= "'" . $row["stkmv_serialno"] . "'";
       $count++;
     endwhile;
     $result->free();
     if ($count > 0) :
        echo "scanCodeIsSerialNo = true;";
        echo "stkMvData = new Array (" . $tmpStkMvData . ");";
     endif;

  elseif ($mode == "2") :
     // Check scan code is an article barcode

     $whereClause = "at.at_barcode = '" . $scanCode . "'";  // Get the "outgoing good" entry of the journal with this special serial number
     // $orderByClause = "stkmv.stkmv_timestamp DESC";      // Get the youngest entry (current place), e.g. this article was put from stoch A to stock B ...

     $sqlStmt = getStmtArticle($whereClause, $orderByClause);
     $result = $db->query($sqlStmt);     
     if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

     $count = 0;     
     while ($row = $result->fetch_assoc()):
       $tmpStkMvData = "";
       $tmpStkMvData .= "'" . $row["at_id"] . "',";
       $tmpStkMvData .= "'" . $row["at_eid"] . "',";
       $tmpStkMvData .= "'" . $row["at_name"] . "',";
       $tmpStkMvData .= "'" . $row["at_match"] . "'";
       $count++;
     endwhile;
     $result->free();
     if ($count > 0) :
        echo "scanCodeIsArticleBarcode = true;";
        echo "stkMvArticleData = new Array (" . $tmpStkMvData . ");";
     endif;

  elseif ($mode == "3") :
     // Check scan code is a stock barcode

     if ($stkIdRoot != "" && is_numeric($stkIdRoot)) :
        $whereClause = "stk.stk_barcode = '" . $scanCode . "'";  // Get the stock entry according to the specified serial number

        $sqlStmt = getStmtAllStocksByStkId($stkIdRoot, $whereClause);
        $result = $db->query($sqlStmt);
        if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
        
        $count = 0;     
        while ($row = $result->fetch_assoc()):
          $tmpStkMvData = "";
          $tmpStkMvData .= "'" . $row["stk_id"] . "',";
          $tmpStkMvData .= "'" . $row["stk_name"] . "',";
          $tmpStkMvData .= "'" . $row["stk_barcode"] . "'";
          $count++;
        endwhile;
        $result->free();
        if ($count > 0) :
           echo "scanCodeIsStockBarcode = true;";
           echo "stkMvStockData = new Array (" . $tmpStkMvData . ");";
        endif;
     endif;

  elseif ($mode == "9") :
     // Get all stocks of the current root stock
     $output = "";
     if ($stkIdRoot != "" && is_numeric($stkIdRoot)) :
     
        $sqlStmt = getStmtAllStocksByStkId($stkIdRoot);
        $result = $db->query($sqlStmt);     
        if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
        
        $stkIdArray = array();
        $stkNameArray = array();
        $stkBarcodeArray = array();
        while ($row = $result->fetch_assoc()):
          $stkIdArray[]      = strtoupper($row["stk_id"]);
          $stkNameArray[]    = strtoupper($row["stk_name"]);
          $stkBarcodeArray[] = strtoupper($row["stk_barcode"]);
        endwhile;
        $result->free();
        
        $stkIdArrayLen = count($stkIdArray);
        $tmpListStkId = "";
        $tmpListStkName = "";
        $tmpListStkBarcode = "";
        for ($j = 0; $j < $stkIdArrayLen; $j++) :
            $tmpListStkId .= "\"" . $stkIdArray[$j] . "\"";
            $tmpListStkName .= "\"" . $stkNameArray[$j] . "\"";
            $tmpListStkBarcode .= "\"" . $stkBarcodeArray[$j] . "\"";
            if ($j < ($stkIdArrayLen - 1)) :
               $tmpListStkId .= ",";
               $tmpListStkName .= ",";
               $tmpListStkBarcode .= ",";
            endif;
        endfor;
        echo "listLenth = " . $stkIdArrayLen . ";";
        echo "stkIdList = new Array (" . $tmpListStkId . ");";
        echo "stkNameList = new Array (" . $tmpListStkName . ");";
        echo "stkBarcodeList = new Array (" . $tmpListStkBarcode . ");";
     endif;
  endif;
?>  