<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include ("../navigation/header.php");
$page_name = "Stores-stock count edit";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");

$show = $_GET['show'];


if ($show==0) {
$table_name = $_SESSION['db_prefix']."_product_stock_count";
$stock_date_query = "SELECT count_id, stock_take_date FROM $table_name WHERE category = 'Product' AND sub_category = 'Production' GROUP BY stock_take_date ORDER BY stock_take_date DESC";
$stock_date_result = $con->query($stock_date_query);
$stock_date_num = mysqli_num_rows($stock_date_result);

$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while ($w < $stock_date_num) {
		$row = mysqli_fetch_assoc($stock_date_result);
		$arrId4[$w] = $row['stock_take_date'];
		$arrName4[$w] = $row['stock_take_date'];
		$w++;
	}
}


if ($show==1) {
$stock_take_date = $_GET['stock_take_date'];

$table_name = $_SESSION['db_prefix']."_product_store";
$table_name2 = $_SESSION['db_prefix']."_product_stock_count";
$item_query = "SELECT $table_name.product_item_id, $table_name.item_name, $table_name.category, $table_name.subCategory, $table_name.unit, $table_name2.count_id, $table_name2.batch_code, $table_name2.new_stock  FROM $table_name, $table_name2 WHERE $table_name.category = 'Product' AND $table_name.subCategory = 'Production' AND $table_name.product_item_id = $table_name2.product_item_id AND $table_name2.stock_take_date = '".$stock_take_date."' ORDER BY $table_name.item_name ASC, $table_name2.batch_code DESC  ";
$item_result = $con->query($item_query);;
$item_num = mysqli_num_rows($item_result);
}


?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/main.css" />

<!--jscript-->
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script>  
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
</head>

<body>
<div class="main_frame">
<div class="page_head_frame">
	<label class="label_page_head">Edit stock count</label>
  &nbsp;&nbsp;&nbsp;</div>

<?php 
if ($show==0) { ?>
<div class="page_middle_frame">
Select stock count date.
<form name="form2" id="form2" action="" method="get">
  <select name="date_select" autofocus class="select200" id="date_select" onchange="window.location.href= this.form.date_select.options[this.form.date_select.selectedIndex].value">
    <option value=" " selected="selected" >&nbsp;</option>
    <?php
    	for ($j = 0; $j < count($arrId4); $j++) {
			echo "<option value=edit_stock_count.php?stock_take_date=".$arrId4[$j]."&show=1>".date("d-m-Y", strtotime($arrName4[$j]))."</option>";	
		}
     ?>
  </select>
  </form>
  </div>
<?php } ?>

<?php 
if ($show==1) { ?>
<div class="page_middle_frame">
<form name="form1" id="form1" action="edit_stock_count_save.php" method="get">
<label style="font-size:18px;">Stock count of: <?php echo date("d-m-Y", strtotime($stock_take_date)) ?></label><br><br>


<table width="615" id="dataTable">
  <thead>
  <tr>
    <td width="184">Item</td>
    <td width="192" align="left">Batch code</td>
    <td width="80" align="center">Unit</td>
    <td width="93">Counted qty</td>
    <td width="42">&nbsp;</td>
  </tr>
  </thead>
  <?php
  $i=0;
  while($row = $item_result->fetch_assoc()) {
	  $count_id = $row["count_id"];
	  $product_item_id = $row["product_item_id"];
	  $item = $row["item_name"];
	  $batch_code = $row["batch_code"];
	  $category = $row["category"];
	  $subCategory = $row["subCategory"];	  
	  $unit = $row["unit"];
	  $new_stock = $row["new_stock"];
	?>
  <tr>
    <td><?php echo $item ?>
        <input name="item_name<?php echo $i ?>" id="item_name<?php echo $i ?>" type="hidden" value="<?php echo $item ?>">
        <input name="item_name_text<?php echo $i ?>" type="hidden" value="<?php echo $item ?>">
        <input name="batch_code_text<?php echo $i ?>" type="hidden" value="<?php echo $batch_code ?>">
        <input name="product_item_id_text<?php echo $i ?>" type="hidden" value="<?php echo $product_item_id ?>">
        <input name="category_text<?php echo $i ?>" type="hidden" value="<?php echo $category ?>">
        <input name="subCategory_text<?php echo $i ?>" type="hidden" value="<?php echo $subCategory ?>">
        <input name="count_id_text<?php echo $i ?>" type="hidden" value="<?php echo $count_id ?>">
    </td>
    <td align="left"><?php echo $batch_code ?></td>
    <td align="center"><?php echo $unit ?></td>
    <td align="center"><input type="text" name="counted_qty_text<?php echo $i ?>" id="counted_qty_text<?php echo $i ?>" class="text60"   onkeypress="return numbersonly(this, event)" style="text-align:right" autocomplete="off" onChange="document.getElementById('p_edit').value='yes' " onfocus = "this.select();" value="<?php echo $new_stock ?>"></td>
    <td>&nbsp;</td>
  </tr>
  <?php
  $i++;
  }
  ?>
</table>
<br>
<br>

 <div class="page_inside_frame">
      <table width="645" border="0">
        <tr>
          <td width="164">&nbsp;</td>
          <td width="471" align="right">
            <input name="p_edit" id="p_edit" type="hidden" value="no">
            <input name="line_num" id="line_num" type="hidden" value="<?php echo $item_num ?>">
            <input name="stock_take_date_text" id="stock_take_date_text" type="hidden" value="<?php echo $stock_take_date ?>">
            <input name="button2" type="button" class="imagebutton" id="button2" onClick="if (document.getElementById('p_edit').value=='yes') { cancel_confirmation(3, '../store/store_main.php');} else {needToConfirm = false; window.location='../store/store_main.php';}" value="Close"/>
            <input name="save_button"  id="save_button" type="submit" class="imagebutton" value="Save" onClick="needToConfirm = false; " />
<input name="save_close_button"  id="save_close_button" type="submit" class="imagebutton" value="Save &amp; Close" onClick="needToConfirm = false; " /></td>
        </tr>
      </table>
    </div> 
</form>
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
     
  for (i = 0; i < 150; i++) {
	frmvalidator.addValidation("counted_qty_text"+i,"req","- qty for "+document.getElementById('item_name_text'+i).value+".");
  }
  </script>   
</div>
<?php } ?>

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


