<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
include ("../navigation/header.php");
$page_name = "Stores-recycle";
include ("../navigation/access.php");
require_once ('../include/tc_calendar.php');


$show = $_GET['show'];

$table_name = $_SESSION['db_prefix']."_product_store";
$table_name2 = $_SESSION['db_prefix']."_product_store_lines";
$product_query = "SELECT $table_name.product_item_id,  $table_name.item_name, $table_name2.product_store_line_id, $table_name2.batch_code FROM $table_name, $table_name2 WHERE $table_name.product_item_id=$table_name2.product_item_id AND ($table_name2.quantity_in - $table_name2.quantity_out) > 0 and subCategory = 'Production' ORDER BY $table_name.item_name DESC";
$product_result = $con->query($product_query);
	$w = 0;
	$arrId3 = array ();
	$arrName3 = array ();
	while($row = $product_result->fetch_assoc()) {
		$product_store_line_id = $row['product_store_line_id'];
		$product_item_name = $row['item_name'];
		$batch_code = $row['batch_code'];
		
		$arrId3[$w] = $product_store_line_id;
		$arrName3[$w] = $product_item_name.' '.$batch_code;
		$w++;
	}
	
$table_name = $_SESSION['db_prefix']."_product_store";
$table_name2 = $_SESSION['db_prefix']."_product_store_lines";
$product_query = "SELECT $table_name.product_item_id,  $table_name.item_name, $table_name2.product_store_line_id, $table_name2.batch_code FROM $table_name, $table_name2 WHERE $table_name.product_item_id=$table_name2.product_item_id AND ($table_name2.quantity_in - $table_name2.quantity_out) > 0 and subCategory = 'Sales product' ORDER BY $table_name.item_name DESC";
$product_result = $con->query($product_query);
	$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while($row = $product_result->fetch_assoc()) {
		$product_store_line_id = $row['product_store_line_id'];
		$product_item_name = $row['item_name'];
		$batch_code = $row['batch_code'];
		
			
		$arrId4[$w] = $product_store_line_id;
		$arrName4[$w] = $product_item_name.' '.$batch_code;
		$w++;
	}

$table_name = $_SESSION['db_prefix']."_list_recycle";
    $position_query = "SELECT item_name FROM $table_name ORDER BY item_name ASC";
	$arrId2 = array ();
	$arrName2 = array ();
	$position_result = $con->query($position_query);
	$b = 0;
	while($row = $position_result->fetch_assoc()) {
		$arrId2[$b] = $row["item_name"];
		$arrName2[$b] = $row["item_name"];
		$b++;
	}	

if ($show==1) {
$box_select = $_GET['box_select'];
if ($box_select=='A') {
$product_store_line_id = $_GET['production_product_no_select'];
}

if ($box_select=='B') {
$product_store_line_id = $_GET['sales_product_no_select'];
}
	
$table_name = $_SESSION['db_prefix']."_product_store";
$table_name2 = $_SESSION['db_prefix']."_product_store_lines";
$batch_no_query = "SELECT $table_name.product_item_id, $table_name.mix_design_id,  $table_name.item_name, $table_name2.batch_code, ($table_name2.quantity_in - $table_name2.quantity_out) AS stock_quantity FROM $table_name, $table_name2 WHERE $table_name.product_item_id=$table_name2.product_item_id AND $table_name2.product_store_line_id = '".$product_store_line_id."' ";
$batch_no_result = $con->query($batch_no_query);
$row = mysqli_fetch_assoc($batch_no_result);
$product_item_id = $row['product_item_id'];
$item_name = $row['item_name'];
$batch_code = $row['batch_code'];
$stock_quantity = $row['stock_quantity'];
$mix_design_id = $row['mix_design_id'];


$table_name = $_SESSION['db_prefix']."_product_store";
$base_name_query = "SELECT product_item_id, item_name FROM $table_name WHERE mix_design_id = '".$mix_design_id."' AND subCategory = 'Production' ";
$base_name_result = $con->query($base_name_query);
$row = mysqli_fetch_assoc($base_name_result);
$base_product_item_id = $row['product_item_id'];
$base_item_name = $row['item_name'];
}
?>

<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script>  
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>  
 
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/confirm_exit.js"  type="text/javascript"></script>  

<script>
function qty_check() {
	var stock_quantity = <?php echo $stock_quantity ?>;
	var qty = document.getElementById('recycle_text').value;
	
	if (qty > stock_quantity) {
		alertMessage(1, '', 'You entered more than available stock.<br>Please enter a lesser amount');
		document.getElementById('recycle_text').value = '';
	}
}
</script>

</head>
<?php
if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");
?>
<body onload="document.getElementById('box_select').value='';" >

<div class="main_frame">
<div class="page_head_frame">
        <label class="label_page_head">Recycle stock</label>
</div>

<?php 
if ($show==0) {
?>
<div class="page_middle_frame">
<form action="recycle.php" method="get" name="form1">
<table width="698" border="0">
<tr>
<td colspan="2">Select  batch code to recycle from:</td>
<td width="248"><input name="show" type="hidden" value="1" />
  <img src="../images/info_blue.png" width="16" height="16" />only  items with stock can be selected</td>
</tr>
<tr>
  <td width="130">Production product:</td>
  <td width="306">
    <select name="production_product_no_select" class="selectbox300" id="production_product_no_select"  onfocus="document.getElementById('box_select').value='A'; document.getElementById('sales_product_no_select').selectedIndex=0;">
      <option value=" " selected="selected" >&nbsp;</option>
      <?php
    	for ($j = 0; $j < count($arrId3); $j++) {
			echo "<option value=".$arrId3[$j].">".$arrName3[$j]."</option>";	
		}
     ?>
    </select>
    </td>
  <td rowspan="2"><input name="box_select" id="box_select" type="hidden" value="" /><input type="submit" name="date_select_button" id="date_select_button" value="Select" class="imagebutton" onclick="needToConfirm = false;" /></td>
</tr>
<tr>
  <td>Sales product:</td>
  <td><select name="sales_product_no_select" class="selectbox300" id="sales_product_no_select"  onfocus="document.getElementById('box_select').value='B'; document.getElementById('production_product_no_select').selectedIndex=0;">
    <option value=" " selected="selected" >&nbsp;</option>
    <?php
    	for ($j = 0; $j < count($arrId4); $j++) {
			echo "<option value=".$arrId4[$j].">".$arrName4[$j]."</option>";	
		}
     ?>
  </select></td>
  </tr>
</table>
<br />
</form>
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
  
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("box_select","req","- product.");
  
</script>
            
</div>
<?php 
}
?>

<?php 
if ($show==1) {
?>
<div class="page_middle_frame">
<div >
<form id="form2" name="form2" method="get" action="recycle_save.php">
<table width="600" border="0" cellpadding="2">
  <tr>
    <td width="187">Product to recycle:</td>
    <td width="407"><?php echo $item_name ?>
      <input type="hidden" name="product_item_id_text" id="product_item_id_text" value="<?php echo $product_item_id ?>" /></td>
  </tr>
  <tr>
    <td>Batch code:</td>
    <td><?php echo $batch_code ?> <input type="hidden" name="batch_code_text" id="batch_code_text"  value="<?php echo $batch_code ?>"/></td>
  </tr>
  <tr>
    <td>Current stock:</td>
    <td><?php echo $stock_quantity ?></td>
  </tr>
  <tr>
    <td>Number to recycle:</td>
    <td><input type="text" name="recycle_text" id="recycle_text" class="text40"  onkeypress="return numbersonly(this, event)" onkeyup="qty_check();" onChange="document.getElementById('p_edit').value='yes' "/></td>
  </tr>
  <tr>
    <td>Base product to recycle to:</td>
    <td><?php echo $base_item_name ?>
      <input type="hidden" name="base_product_item_id_text" id="base_product_item_id_text" value="<?php echo $base_product_item_id ?>" /></td>
  </tr>
  <tr>
    <td>Reason to recycle:</td>
    <td><span class="star">
      <select name="reason_select" id="reason_select" class="select160"  onChange="document.getElementById('p_edit').value='yes' ">
        <option value="" selected="selected"></option>
        <?php
        	for ($j = 0; $j < count($arrId2); $j++) {
    		echo "<option value=" . "\"" . $arrId2[$j] . "\"" . ">" . $arrName2[$j] . "</option>";
	}
?>
      </select>
    </span></td>
  </tr>
  <tr>
    <td>Notes:</td>
    <td><textarea name="notes_text" cols="50" rows="3" class="text_box"></textarea></td>
  </tr>
</table>
<br />
<input name="recycle_button" type="submit" value="Recycle" class=" imagebutton" onclick="needToConfirm = false;"/>
</form>

<div style="float:right">
<input name="p_edit" id="p_edit" type="hidden" value="no">
<input name="new_button" type="button" value="Select new" class="imagebutton" onclick="if (document.getElementById('p_edit').value=='yes') { cancel_confirmation(3, '../store/recycle.php?show=0');} else {needToConfirm = false; window.location='recycle.php?show=0';}"/></div>
  <br />
 
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form2");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("recycle_text","req","- qty to recycle.");
  frmvalidator.addValidation("reason_select","req","- reason to recycle.");
  
</script> 
</div>
</div>
<?php
} ?>


</div>

<?php include ('../navigation/side_bar.php'); ?>

</body>
<?php } else { ?>
<script> needToConfirm = false; </script>
<?php
    $_SESSION['denied_return'] = "store/return_select_store.php";	
    include ("../navigation/store_sidebar.php");?>
    <?php include ('../navigation/side_bar.php'); ?>
	<body onLoad="accessdenied(); return false">	
	</body>
<?php }
?>
</html>
