<!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-main";
include ("../navigation/access.php");
require_once ('../include/tc_calendar.php');

$stock_take_date = $_GET['stock_take_date'];

$cal_stock_take_date = strtotime($stock_take_date);
$d = date("d", $cal_stock_take_date );
$m = date("m", $cal_stock_take_date );
$y = date("Y", $cal_stock_take_date );

$table_name = $_SESSION['db_prefix']."_general_stock_count";
$check_query = "SELECT stock_take_date  FROM $table_name WHERE stock_take_date = '".$stock_take_date."' ";
$check_result = $con->query($check_query);
$check_num = mysqli_num_rows($check_result);


$table_name = $_SESSION['db_prefix']."_store_items";
$item_query = "SELECT store_item_id, item_name, category, subCategory, unit, quantity FROM $table_name WHERE category = 'stores' ORDER BY subCategory, item_name ";
$item_result = $con->query($item_query);
$item_num = mysqli_num_rows($item_result);
?>

<link href="../css/main.css" rel="stylesheet" type="text/css" />
<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/numbers_only.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>  
<script language="javascript" src="../jscript/calendar.js"></script>  
</head>
<?php
if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");
?>
<body onload="<?php if ($check_num>0) { ?>alertMessage(1, '', 'There is already a stock take for<br><?php echo date("l, d-m-Y", strtotime($stock_take_date)) ?>.<br><br>Please select another date.'); document.getElementById('list_div').hidden=true; <?php } ?>">

<div class="main_frame">
<table width="630" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td height="52" colspan="4" align="left" valign="top"><div class="page_head_frame">
        <table width="630" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="385"><label class="label_page_head">General stores stock take</label></td>
            <td align="right">&nbsp;</td>
          </tr>
        </table>
      </div>
       <br />
<br /><br />
<br />
<br />
<br /></td>
    </tr>
    </table>


<div class="page_middle_frame">
<form name="form1" id="form1" action="general_stock_take.php" method="get">
<div style="float:left;">
<img src="../images/exclamation_mark_blue.png" width="48" height="48">
</div>
<div >
<label style="font-size:18px;">Important:</label><br>
Stock count should be done at day end.<br>
Stock count values to be entered AFTER all day entries for production and stores are entered.
<br>
<br>
<div style="float:left;">
Stock take date: 
 &nbsp;&nbsp;
</div>
<div style="float:left;">
 <?php
		$myCalendar = new tc_calendar("stock_take_date", true);
		$myCalendar->setIcon("../images/iconCalendar.gif");
		$myCalendar->setDate($d, $m, $y);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2018, 2025);
		$myCalendar->writeScript();
?>
</div>
&nbsp;&nbsp;&nbsp;&nbsp;<input name="date_button" type="submit" value="Select date" class="imagebutton"/>
</div>
</form>



<div class="page_middle_frame" id="list_div" style="width:650px;">
<form name="form2" id="form2" action="general_stock_take_save.php" method="get">
<table width="593" id="dataTable">
  <thead>
  <tr>
    <td width="199">Item</td>
    <td width="221" align="left">Sub category</td>
    <td width="71" align="left">Quantity</td>
    <td width="82">Counted qty</td>
  </tr>
  </thead>
  <?php
  $i=0;
  while($row = $item_result->fetch_assoc()) {
		$store_item_id = $row["store_item_id"];
		$subCategory = $row["subCategory"];
		$unit = $row["unit"];
		$quantity = $row["quantity"];
		
				
		$table_name = $_SESSION['db_prefix']."_store_items";
		$item_name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
		$item_name_result = $con->query($item_name_query);
		$row = mysqli_fetch_assoc($item_name_result);
		$item_name = $row['item_name'];
	  	
	  	
	?>
  <tr>
    <td><?php echo $item_name ?><input name="item_name_text<?php echo $i ?>" id="item_name_text<?php echo $i ?>" type="hidden" value="<?php echo  $item_name ?>" />
        <input name="store_item_id_text<?php echo $i ?>" type="hidden" value="<?php echo $store_item_id ?>" >
        </td>
    <td align="left"><?php echo $subCategory ?><input name="batch_code_text<?php echo $i ?>" type="hidden" value="<?php echo $subCategory ?>" /></td>
    <td align="left"><?php echo $quantity ?>
      <input type="hidden" name="quantity_text<?php echo $i ?>" id="quantity_text<?php echo $i ?>" value="<?php echo $quantity ?>" /></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"></td>
    
  </tr>
  <?php
  $i++;
  }
  ?>
</table>
<br>
<br>

 <div>
      <table width="645" border="0">
        <tr>
          <td width="405"><div align="left"> <img src="../images/info_blue.png" width="16" height="16">
            <label class="label_info">Quantity for all items need to be entered before saving.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Only one stock count per day can be done.</label>
          </div></td>
          <td width="230" align="right">
            <input name="stock_take_date" type="hidden" value="<?php echo $stock_take_date ?>" />
            <input name="line_num" id="line_num" type="hidden" value="<?php echo $item_num ?>">
            <input name="button2" type="button" class="imagebutton" id="button2" onClick="cancel_confirmation(3, '../store/store_main.php');" value="Close"/>
            <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("form2");
  frmvalidator.EnableMsgsTogether();
     
  for (i = 0; i < 50; i++) {
	frmvalidator.addValidation("counted_qty_text"+i,"req","- qty for "+document.getElementById("item_name_text"+i).value+".");
  }
  </script> 
</div>  
</div>
</div>

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

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