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


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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdActual", "deactivateMenu",
               "stkIdRoot", "stkIdCurrent", "stkId", "atId", "searchField", "f_stkat_export"));


// Check authentication verifying emmployee an his/her costcenter- and customer-association
if ( !( ($userType == "1") || ($userType == "4") || authCheck($hq_id,$usr_id,$emp_id,$cscIdRoot,$customerId,$cscIdActual) ) ) :
   gotoReferer("1");
endif;

getLanguage(__FILE__);

$deactivateMenuStatic = "1";
$pageTitel = getLngt("SERIENNUMMERN");
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
include_once ("../include/inc_stock.inc.php");
getCurrentScript(__FILE__);


function postParseStockArticleItems ($textToParse) {
	global $f_stkmv_export;
  $textToParse = trim($textToParse);

  if ($textToParse != "") :

     // Iterate ALL occurrences of "<postparser>...</postparser>"          
     while (!(strpos($textToParse, "<postparser>") === FALSE)) {
     
        $beginTagPosBegin = strpos($textToParse, "<postparser>");
        $beginTagPosEnd = $beginTagPosBegin + 12;
        $endTagPosBegin = strpos($textToParse, "</postparser>");
        $endTagPosEnd = $endTagPosBegin + 13;

        if ($beginTagPosEnd < $endTagPosBegin) :
           $tagContent = substr($textToParse, $beginTagPosEnd, $endTagPosBegin - $beginTagPosEnd);
           $tagContent = trim($tagContent);

           if ($tagContent != "" && !(strpos($tagContent, "|") === FALSE)) :
              $tagContent = str_replace("|", "-,-", $tagContent);
              $parseArray = spliti("-,-", $tagContent);   // (stk.stk_id,'0',at.at_id,'',ati.ati_serialno,ati.ati_data_01,ati.ati_data_02,ati.ati_data_03,ati.ati_data_04, ..., ati.ati_data_15)
              // Special treatment
              if ($f_stkmv_export != "1") :
                 $tagContent = "<br><a href=\"javascript:finishPageArticleItemAction('" . $parseArray[0] . "', '" . $parseArray[1] . "', '" . $parseArray[2] . "', '" . $parseArray[3] . "', '" . $parseArray[4] . "', '" . $parseArray[5] . "', '" . $parseArray[6] . "', '" . $parseArray[7] . "', '" . $parseArray[8] . "', '" . $parseArray[9] . "', '" . $parseArray[10] . "', '" . $parseArray[11] . "', '" . $parseArray[12] . "', '" . $parseArray[13] . "', '" . $parseArray[14] . "', '" . $parseArray[15] . "', '" . $parseArray[16] . "', '" . $parseArray[17] . "', '" . $parseArray[18] . "', '" . $parseArray[19] . "')\">" . getLngt("Disponieren") . "</a>";
              else :
                 $tagContent = "";
              endif;

              // Remove last "<br>"
              // $tagContent = substr($tagContent, 0, -4);
           endif;

           // Substitute text fragment
           $textToParse = substr_replace($textToParse, $tagContent, $beginTagPosBegin, $endTagPosEnd - $beginTagPosBegin);
        endif;
      }
  endif;
  return $textToParse;  
}


$numOfRows = 0;
$output = "";

if ($atId != "") :

    $atEid = getFieldValueFromId("article","at_id",$atId,"at_eid");

    $searchField = trim($searchField);
    $tmpWhereClause = "";
    if ($searchField != "") :
       $tmpWhereClause = "(at.at_eid LIKE '%" . $searchField . "%' OR at.at_name LIKE '%" . $searchField . "%' OR "
                       . "at.at_match LIKE '%" . $searchField . "%' OR at.at_description LIKE '%" . $searchField . "%' OR "
                       . "ati.ati_serialno LIKE '%" . $searchField . "%' OR "
                       . "ati.ati_data_01 LIKE '%" . $searchField . "%' OR ati.ati_data_02 LIKE '%" . $searchField . "%' OR "
                       . "ati.ati_data_03 LIKE '%" . $searchField . "%' OR ati.ati_data_04 LIKE '%" . $searchField . "%' OR "
                       . "ati.ati_data_05 LIKE '%" . $searchField . "%' OR ati.ati_data_06 LIKE '%" . $searchField . "%' OR "
                       . "ati.ati_data_07 LIKE '%" . $searchField . "%' OR ati.ati_data_08 LIKE '%" . $searchField . "%' OR "
                       . "ati.ati_data_09 LIKE '%" . $searchField . "%' OR ati.ati_data_10 LIKE '%" . $searchField . "%' OR "
                       . "ati.ati_data_11 LIKE '%" . $searchField . "%' OR ati.ati_data_12 LIKE '%" . $searchField . "%' OR "
                       . "ati.ati_data_13 LIKE '%" . $searchField . "%' OR ati.ati_data_14 LIKE '%" . $searchField . "%' OR "
                       . "ati.ati_data_15 LIKE '%" . $searchField . "%')";
    endif;

    $tmpFieldClause = ", CONCAT(IFNULL(stk.stk_id,''),'|','0','|',at.at_id,'|','','|',ati.ati_serialno,'|',ati.ati_data_01,'|',ati.ati_data_02,'|',ati.ati_data_03,'|',ati.ati_data_04,'|',ati.ati_data_05,'|',ati.ati_data_06,'|',ati.ati_data_07,'|',ati.ati_data_08,'|',ati.ati_data_09,'|',ati.ati_data_10,'|',ati.ati_data_11,'|',ati.ati_data_12,'|',ati.ati_data_13,'|',ati.ati_data_14,'|',ati.ati_data_15) AS ati_x";
    $sqlStmt = getStmtStockArticleItems($atId, $stkId, $tmpWhereClause, $tmpFieldClause);

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


    // Get the root stock
    if ($stkIdRoot == "") :
       $stkIdRoot = getStkPathId($stkId);
    endif;
    // Get the headlines if exist
    $tmpHeadlines = getParameterValue("0", "MASK_STK_DATAFIELDHEADLINES_" . $stkIdRoot, "0");
    if ($tmpHeadlines != "") :
       $titleArray = array(getLngt("Disponieren&nbsp;"), getLngt("Lagerort&nbsp;"), getLngt("Artikel&nbsp;"), getLngt("Seriennummer&nbsp;"));
       $tmpHeadlinesArray = spliti(",", $tmpHeadlines);
       $tmpHeadlinesArrayLen = count($tmpHeadlinesArray);
       for ($i = 0; $i < $tmpHeadlinesArrayLen; $i++) :
           if ($tmpHeadlinesArray[$i] != "") :
              $titleArray[] = $tmpHeadlinesArray[$i];
           else :
              $titleArray[] = "";
           endif;
       endfor;
    else :
       $titleArray = array(getLngt("Disponieren&nbsp;"), getLngt("Lagerort&nbsp;"), getLngt("Artikel&nbsp;"), getLngt("Seriennummer&nbsp;"),
                           getLngt("Datenfeld&nbsp;01&nbsp;"), getLngt("Datenfeld&nbsp;02&nbsp;"), getLngt("Datenfeld&nbsp;03&nbsp;"), getLngt("Datenfeld&nbsp;04&nbsp;"), getLngt("Datenfeld&nbsp;05&nbsp;"), 
                           getLngt("Datenfeld&nbsp;06&nbsp;"), getLngt("Datenfeld&nbsp;07&nbsp;"), getLngt("Datenfeld&nbsp;08&nbsp;"), getLngt("Datenfeld&nbsp;09&nbsp;"), getLngt("Datenfeld&nbsp;10&nbsp;"),
                           getLngt("Datenfeld&nbsp;11&nbsp;"), getLngt("Datenfeld&nbsp;12&nbsp;"), getLngt("Datenfeld&nbsp;13&nbsp;"), getLngt("Datenfeld&nbsp;14&nbsp;"), getLngt("Datenfeld&nbsp;15&nbsp;"));
    endif;
    $aligns = "l,l,l,l,l,l,l,l,l,l,l,l,l,l,l,l,l,l,l";
    $fieldArray = array("ati_x", "stk_name", "at_eid", "ati_serialno", "ati_data_01", "ati_data_02", "ati_data_03", "ati_data_04", "ati_data_05", "ati_data_06", "ati_data_07", "ati_data_08", "ati_data_09", "ati_data_10", "ati_data_11", "ati_data_12", "ati_data_13", "ati_data_14", "ati_data_15");
    $postParserField = "ati_x";

    $alignArray = spliti(",",$aligns);
    $alignTitles = "left";
    $widths = "100";
    $widthArray = spliti(",",$widths);
    $summationField = "";
    $mode = "1";             // Output from DB-RESULT
    $javaScriptFunctionNameForSort = "";
    $sortDBField = "";    // Used in following include-file for sorting per column;
    // $fieldSortArray = array("YES","YES","YES","YES","YES","YES");
    include ("../include/inc_list_defineoutput.inc.php");
    // Post parsing if necessary
    if ($postParserField != "") :
       $tableBody = postParseStockArticleItems($tableBody);
    endif;

    $output .= "<table>\n";
    $output .= $tableHeader . $tableBody;
    $output .= "</table>\n";

    $result->free();
    
    $numOfRows = $rowCounter;
    
    // Optional output to file
    if ($f_stkat_export == "1") :
       array_shift($titleArray);
       array_shift($fieldArray);
       stockFilesOutputCSV($sqlStmt, $titleArray, $fieldArray, $aligns, getLngt("EINZELARTIKEL"), "", "postParseStockArticleItems");
    endif;
endif;
?>

<html>

<head>
<title><?php echo $pageTitel ?></title>

<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>

<script type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>

// Set page parameters according to the requested journal entry to move the article again
function finishPageArticleItemAction(stk_id, stk2_id, at_id, stkmv_tan, stkmv_serialno, stkmv_data_01, stkmv_data_02, stkmv_data_03, stkmv_data_04, stkmv_data_05, stkmv_data_06, stkmv_data_07, stkmv_data_08, stkmv_data_09, stkmv_data_10, stkmv_data_11, stkmv_data_12, stkmv_data_13, stkmv_data_14, stkmv_data_15) {

	frm = opener.document.forms[0];

  // * Prepare for outgoing disposition *
	opener.clearArticleMoveFields();

  if (frm.f_mv_stk_from) {frm.f_mv_stk_from.value = stk_id};
  // if (frm.f_mv_stk_to) {frm.f_mv_stk_to.value = '0'};
  <?php
  // Get parameter to set the "stock to" equal to "stock from" if link "Disposition" has been selected
  $tmpStkIdRoot = getStkPathId($stkIdRoot);  // Makes it safe to have a real root stock
  $maskStkatShowPermanent = getParameterValue("0", "MASK_LINK_DISPOSITION_STKTO_EQ_STKFROM_" . $tmpStkIdRoot, "0");
  if ($maskStkatShowPermanent == "1") :
     echo "if (frm.f_mv_stk_to) {frm.f_mv_stk_to.value = stk_id};";
  endif;
  ?>
    
  // if (frm.f_mv_at) {frm.f_mv_at.value = at_id};
  for(i=0;i<frm.f_mv_at.length;++i) {
    if (frm.f_mv_at.options[i].value == at_id) {
       frm.f_mv_at.options[i].selected = true;
    }
  }
  // if (frm.f_mv_stk_itemquantity) {frm.f_mv_stk_itemquantity.value = 0};
  // if (frm.f_mv_tan) {frm.f_mv_tan.value = stkmv_tan};
  // if (frm.f_mv_remark) {frm.f_mv_remark.value = ''};
  if (frm.f_mv_serialno) {
  	  frm.f_mv_serialno.value = stkmv_serialno;
  	  if (stkmv_serialno != '') {
  	  	 if (frm.f_mv_stk_itemquantity) {frm.f_mv_stk_itemquantity.value = '1'};
      };
      if (frm.f_mv_serialno_old) {
         frm.f_mv_serialno_old.value = stkmv_serialno;
      };
  };
  
  // Fill extra data fields if they are set
  frm.f_mv_datafield_01.value = stkmv_data_01;
  frm.f_mv_datafield_02.value = stkmv_data_02;
  frm.f_mv_datafield_03.value = stkmv_data_03;
  frm.f_mv_datafield_04.value = stkmv_data_04;
  frm.f_mv_datafield_05.value = stkmv_data_05;
  frm.f_mv_datafield_06.value = stkmv_data_06;
  frm.f_mv_datafield_07.value = stkmv_data_07;
  frm.f_mv_datafield_08.value = stkmv_data_08;
  frm.f_mv_datafield_09.value = stkmv_data_09;
  frm.f_mv_datafield_10.value = stkmv_data_10;
  frm.f_mv_datafield_11.value = stkmv_data_11;
  frm.f_mv_datafield_12.value = stkmv_data_12;
  frm.f_mv_datafield_13.value = stkmv_data_13;
  frm.f_mv_datafield_14.value = stkmv_data_14;
  frm.f_mv_datafield_15.value = stkmv_data_15;

  opener.checkSerialno();   // Serial number field has to be set before call
  
  window.close();
}

function downloadStockFile() {
  var widthPopupWin = 850;
  var heightPopupWin = 600;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;

  popupWin = window.open("../include/data_download.php?f_path=../temp/download/&f_fileName=<?php echo $f_secretFileName ?>", "","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function exportFinishPage() {
  // document.forms[0].f_act.value = '';
  document.forms[0].f_stkat_export.value = '1';
  document.forms[0].submit();
}
-->
</script>
</head>

<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?><?php if ($f_stkat_export) : echo "downloadStockFile();"; endif; ?>displayStatusMessage();">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="maincontent" name="maincontent" id="maincontent">

<form action="../stock/article_item.php" method="post">
  <input type="hidden" name="f_act" value="">  
  <input type="hidden" name="customerId" value="<?php echo $customerId ?>">
  <input type="hidden" name="cscIdRoot" value="<?php echo $cscIdRoot ?>">
  <input type="hidden" name="cscIdActual" value="<?php echo $cscIdActual ?>">
  
  <input type="hidden" name="stkIdRoot" value="<?php echo ec($stkIdRoot) ?>">
  <input type="hidden" name="stkIdCurrent" value="<?php echo ec($stkIdCurrent) ?>">
  <input type="hidden" name="stkId" value="<?php echo ec($stkId) ?>">
  <input type="hidden" name="atId" value="<?php echo ec($atId) ?>">
  <input type="hidden" name="searchField" value="<?php echo ec($searchField) ?>">
  <input type="hidden" name="f_stkat_export" value="">
   
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">

  <?php echo htmlDivLineSpacer("20px"); ?>

  <div>
  	<div <?php echo setStyleHtmlDiv("300px","left"); ?>><?php echo getLngt("Artikel") . ":&nbsp;" . $atEid ?></div>
  	<?php echo defineButtonType10(getLngt("Export"), "action_move", "exportFinishPage();", "150", "left", "2") ?>
  </div>
  <?php echo htmlDivLineSpacer("20px", "", "left"); ?>
  
  
  
  <div>
    <?php echo $output ?>
  </div>
  <?php echo htmlDivLineSpacer("15px"); ?>

  <div>
    <?php echo getLngt("Anzahl Einträge:") ?> <?php echo $numOfRows ?><?php if ($numOfRows == "0" && $f_act == "search" && $statusMessage == "") : echo " " . getLngt("(Keine Einträge gefunden.)"); endif; ?>
  </div>

</form>
</div>
</body>

</html>