<?php
session_start();

require_once('config.php');
require_once(DIR_SYSTEM.'class/class.inc.php');

$iddb=$db->dbString("id");


########################### Get Token ##############################
if(isset($_REQUEST['token'])){ $token=$_REQUEST['token']; }else{ $token=''; }

if(!$token)
{
	if(isset($_SESSION['snplq2l_token']))
	{
		header('location:'.HTTP_SERVER."stockhistory.php?id=".$iddb."&token=".$_SESSION['snplq2l_token']);
	}
	else{ header('location:'.HTTP_SERVER); }
}
else
{
require_once(DIR_SYSTEM.'inc/sign_verify.php');


$ressys=$db->select("*","store_master","id='1'","","");
$rowsys=$db->fetch($ressys);

$res=$db->select("*","inventory_items","id='".$iddb."'","","");
$row=$db->fetch($res);

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
body{ font-family:Arial, Helvetica, sans-serif; font-size:13px; }
td{ border:1px solid #DDD; }
</style>
</head>

<body>
<?php
echo $row['id'].' - '.$row['code'].' ('.$row['cost'].') ';
$avqty=0;

$stock_last_update=strtotime(date('Y-m-d',strtotime($rowsys['stock_last_update'])));
$open_bl_datestr=strtotime($row['opening_stock_date']);

if($open_bl_datestr>=$stock_last_update){ $avqty+=$row['opening_balance']; }

?>
O/BALANCE: <?php echo $avqty; ?>
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="97">Date</td>
    <td width="59">Note</td>
    <td width="79">Note Id</td>
    <td width="49">Type</td>
    <td width="75">Qty</td>
    <td width="66">Balance</td>
    <td width="66">Cost</td>
  </tr>
  <?php
  $ress=$db->select("*","stock_transactions","item='".$row['id']."' AND (added_date BETWEEN '".$rowsys['stock_last_update']."' AND '".$date->todayDate('Y-m-d')."')","","added_date ASC");
  while($rows=$db->fetch($ress))
  {
	  if($rows['trn_type']=='in'){ $avqty+=$rows['qty']; }
	  if($rows['trn_type']=='out'){ $avqty-=$rows['qty']; }
	  
	?>
  <tr>
    <td><?php echo $rows['added_date']; ?></td>
    <td><?php echo $rows['note_type']; ?></td>
    <td><?php echo $rows['note_id']; ?></td>
    <td><?php echo $rows['trn_type']; ?></td>
    <td><?php echo $rows['qty']; ?></td>
    <td><?php echo $avqty; ?></td>
    <td><?php echo $rows['amount']; ?></td>
  </tr>
  <?php } ?>
  <tr>
    <td colspan="5">BALANCE</td>
    <td><?php echo $avqty; ?></td>
  </tr>
  <tr>
    <td colspan="5">ITEM ROW BALANCE</td>
    <td><?php echo $row['closing']; ?></td>
  </tr>
</table>
</body>
</html>
<?php } ?>