<link rel="stylesheet" href="css/new_mxerp.css">

<?php
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
$title = _('Stock Trial');
$date = date($_SESSION['DefaultDateFormat']);
unset($_SESSION['query']);
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmView']=$frmpermission[3];
if($_SESSION['frmView']=='0')
{echo "<strong>You are not authorised to view this form. Contact your administrator</strong>"; exit;}
?>
<script language="javascript" type="text/javascript">
function get_itemsubgroup(str)
{
if(str=="")
  {
  document.getElementById("sub_id").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?main="+str,true);
xmlhttp.send();
}
function get_item(str)
{
if(str=="")
  {
  document.getElementById("itemid").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?sub="+str,true);
xmlhttp.send();
}
</script>

<div class="body_contain">
<div class="tab_div">

<h1><?php echo $title; ?></h1>
<form method="post" action="">

	<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#999999 solid 1px;">
      <tr>
        <td class="tabledata1">SITE</td>
        <td width="72%">
		<select name="site" id="site" style="width:100%">
		<option value="000">Select Site</option>
		<?php 
		if($_SESSION['AssignedSites']!='0'){
		$site_sql = "SELECT loccode, locationname from locations where loccode in (".$_SESSION['AssignedSites'].") and CompanyId='".$_SESSION['ACGCompanyId']."'"; }
		else
		{$site_sql = "SELECT loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'"; }
		$site_result = DB_query($site_sql,$db);
		while($site_row = DB_fetch_array($site_result))
		{?>
		<option value="<?php echo $site_row['loccode']; ?>"><?php echo $site_row['locationname']; ?></option>
		<?php } ?>
		</select>		</td>
      </tr>
      <tr>
        <td class="tabledata1">STORE</td>
        <td>
		<select name="store" id="store" style="width:100%">
		<option value="000">Select Store</option>
		<?php 
		$store_sql = "SELECT distinct StoreCode, StoreName FROM storetype";
		$store_result = DB_query($store_sql,$db);
		while($store_row = DB_fetch_array($store_result))
		{?>
		<option value="<?php echo $store_row['StoreCode']; ?>"><?php echo $store_row['StoreName']; ?></option>
		<?php } ?>
		</select>		</td>
      </tr>
      <tr>
        <td class="tabledata1" width="28%"> DATE FROM : </td>
        <td width="72%"><input type="text" name="Input32" value="<?php echo date('d/m/Y'); ?>" class="datepicker" style="width:100%"></td>
      </tr>
    <tr>
        <td class="tabledata1" width="28%"> DATE TO : </td>
        <td width="72%"><input type="text" name="Input32" value="<?php echo date('d/m/Y'); ?>" class="datepicker" style="width:100%"></td>
      </tr>
	</table>
	
	<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#999999 solid 1px;">
      <tr>
        <td><input type="checkbox" name="monthly" value="" id="monthly" /></td>
		<td>Moved Only </td>
         <td><input type="checkbox" name="rate_amount" value="" id="rate_amount" /></td>
		<td>Negative Only </td>
      </tr>
	  <tr>
        <td><input type="checkbox" name="monthly" value="" id="monthly" /></td>
		<td>Summary</td>
         <td><input type="checkbox" name="rate_amount" value="" id="rate_amount" /></td>
		<td>Skip Zero Balance Items</td>
      </tr>
    </table>
	
	<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#999999 solid 1px;">
  <tr>
    <td class="tabledata1">ITEM MAIN GROUP </td>
    <td>
	<select name="main_group" id="main_group" onchange="get_itemsubgroup(this.value)" style="width:100%">
	<option value="0">Select</option>
	<?php 
		$main_cat_sql=DB_query("select * from stockmaincategory where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
		while($row1=DB_fetch_array($main_cat_sql)){
	?>  
	<option value="<?php echo $row1['stockmaincategoryid'];?>"<?php echo($_REQUEST['main_group']==$row1['stockmaincategoryid']?'selected':'')?>><?php echo $row1['stockmaincategoryname'];?></option>
	<?php }?>
	</select>
	</td>
  </tr>
  <tr>
    <td class="tabledata1">ITEM SUB GROUP </td>
    <td>
	<div id="sub_id">
	<select name="sub_group" id="sub_group" onchange="get_item(this.value)" style="width:100%">
	<option value="0">Select</option>
	<?php 
	$sub_cat_sql=DB_query("select categoryid,categorydescription from stockcategory where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	$c2=0;
	while($row2=DB_fetch_array($sub_cat_sql)){	
	?>
	<option value="<?php echo $row2['categoryid'];?>"<?php echo($_REQUEST['sub_group']==$row2['categoryid']?'selected':'')?>><?php echo $row2['categorydescription'];?></option>
	<?php }?>
	</select></div>
	</td> 
  </tr>
  <tr>
    <td class="tabledata1">ITEM</td>
    <td>
    	<div id="itemid">
	<div style="overflow:auto; height:150px; width:635px; border:1px solid; " >
	<?php 
	$item_sql=DB_query("select StockID,item_name from item_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	$c3=0;
	while($row3=DB_fetch_array($item_sql))
	{ 
		$c='';
		if($_REQUEST['item'][$c3]==$row3['StockID'])
		{$c="checked";}
	?>
	<input type="checkbox" name="item[<?php echo $c3;?>]" id="item_<?php echo $c3;?>" value="<?php echo $row3['StockID'];?>" <?php echo $c;?>/><?php echo $row3['StockID']." - ".$row3['item_name'];?><br />
	<?php 	
	$c3++;
	}
	?>
	</div>
		
	</div></td>
  </tr>
</table>
      <p align="center"><input type="submit" name="add" value="Add In Schedule">
	<input type="submit" name="view" value="View">
	<input type="submit" name="print" value="Print">
    <input type="submit" name="cancel" value="Cancel"></p>
	
</form>
<p>&nbsp;</p>
<table width="100%" border="1" cellspacing="0" cellpadding="3">
  <tr>
    <td class="tabledata" colspan="2">Item</td>
    <td class="tabledata">Stock</td>
    <td class="tabledata" scope="col">Closing</td>
    <td class="tabledata">Opening</td>
	<td class="tabledata">Received</td>
	<td class="tabledata">Issued</td>
  </tr>
  <tr>
    <td class="tabledata">Code</td>
    <td class="tabledata">Description</td>
    <td class="tabledata">Unit</td>
    <td class="tabledata">Stock Qty. </td>
    <td class="tabledata">Stock Qty. </td>
    <td class="tabledata">Stock Qty. </td>
    <td class="tabledata">Stock Qty. </td>
  </tr>
  
  <?php
if(isset($_POST['view']))	
{   $flag=0;
	$fordate = $_POST['fromdate'];
	$todate = $_POST['todate'];
	$fromdate = explode("/", $fordate);
	$daydate = $fromdate['0'];
	$Monthdate = $fromdate['1'];
	$yeardate = $fromdate['2'];
	$fdate = $yeardate."-".$daydate."-".$Monthdate;
					   
	$tredate   = explode("/", $todate);
	$dayda     = $tredate['0'];
	$Monthda   = $tredate['1'];
	$yearda    = $tredate['2'];
	$toyrdate  = $yearda."-".$dayda."-".$Monthda;
	
	$filter=array();

	if(isset($_POST['site']) && $_POST['site'] != '0')
  	$filter[]="addmrn.OrderFromSite='".$_POST['site']."'";
 	if(isset($_POST['fromdate']) && $_POST['fromdate'] != '' && isset($_POST['todate']) && $_POST['todate'] != '')
 	$filter[]="addmrn.MRNDate between '".$fdate."' and '".$toyrdate."' ";
 	if(isset($_REQUEST['storecode']) && $_REQUEST['storecode'] != '0')
 	$filter[]="addmrn.StoreName = '".$_POST['storecode']."'";
	if(isset($_POST['supplierid']) && $_POST['supplierid'] != 'select')
	$filter[].="addmrn.ReceivedFrom = '".$_POST['supplierid']."'";
	
	$itemArray=array();
	if($_REQUEST['item']!=''){
	$itemArray=$_REQUEST['item'];
	$itemString="'".implode("','",$itemArray)."'"; }
 
	if($_REQUEST['main_group'] != '0')
	{
		if($_REQUEST['sub_group'] != '0')
		{
			if($_REQUEST['item'] != '')
			{$filter[]="addmrnqty.itemcode in (".$itemString.")";}
			else
			{$filter[]="addmrnqty.itemcode LIKE '".$_REQUEST['sub_group']."%' ";}
		}
		elseif($_REQUEST['item'] != '')
		{$filter[]="addmrnqty.itemcode in (".$itemString.")";}
		else
		{$filter[]="addmrnqty.itemcode LIKE '".$_REQUEST['main_group']."%'  ";}
	}
	else 
	if($_REQUEST['sub_group'] != '0')
	{
		if($_REQUEST['item'] != '')
		$filter[]="addmrnqty.itemcode in (".$itemString.")";
		else
		$filter[]="addmrnqty.itemcode LIKE '".$_REQUEST['sub_group']."%' ";
	}
	else
	if($_REQUEST['item'] != '')
	{$filter[]="addmrnqty.itemcode in (".$itemString.")";}
	
	if($filter != '')
	{
		$filter1 = implode(" and ",$filter);
	}
	
	
	$sql="SELECT addmrnqty.*, addmrn.* from addmrn INNER JOIN addmrnqty on(addmrnqty.issue_id = addmrn.id) and $filter1";
	//echo $sql;
	$_SESSION['query']=$sql;
		
$result = DB_query($sql, $db);
if(DB_num_rows($result) > 0)
	{
	$flag=1;	
while($myrow = DB_fetch_array($result))
{ ?>
<tr bgcolor="#c1c1c1">
    <td><?php echo $myrow['itemcode'];?></td>
    <td><?php echo $myrow['description'];?></td>
    <td><?php echo $unit=$s->getField('unitsofmeasure','unitid',$myrow['UOM'],'unitname'); ?></td>
    <td></td>
    <td></td>
    <td><?php 
	  /*if($_REQUEST['item']!='' || $_REQUEST['sub_group']!='0' || $_REQUEST['main_group']!='0')
	  {*/
	  ?>
	  <input name="received[<?php echo $k;?>]" type="text" id="received_<?php echo $k;?>" value="<?php echo $myrow['reciveqty']; ?>" size="10" readonly="true"/><?php /*} else {?>
	  <input name="received[<?php echo $k;?>]" type="text" id="received_<?php echo $k;?>" value="<?php echo $myrow['totalqty']; ?>" size="10" readonly="true"/><?php }*/ ?></td>
    <td></td>
  </tr>
<?php				
}}
else
	{
	echo '<td><div align="center"><font color="#FF0000"> Record Not Found </font></div></td>';}
}
?>
<?php if($flag==1){ ?>
<tr><td colspan="6"style="text-align:center;"><a href="stocktril_export.php">Export</a></td></tr> 	
<?php }?>
 
</table>
</div></div>