<?php $PageSecurity = 2;
include('includes/DefineSuppTransClass.php');
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
include('includes/session.inc');
include('includes/function_lib.php');
include('includes/fun-HRClass.php');
/* Get opening balance */


$function_filter[]=$_REQUEST['site1'];
//$function_filter[]=$_REQUEST['store'];
$function_filter[]=FormatDateForSQL($_REQUEST['date']);

							function openingBalance($function_filter,$item)
							{
								global $db;
								$site=$function_filter[0];
								$from=$function_filter[1];								
								if($site!='')
								$filter[]="site = '".$site."'";
								if($from!='--' && $to!='--')
								$filter[]="reveive_date <= '".$from."'";
								if($item!='')
								$filter[]="itemcode = '".$item."'";
								
								$filter=implode(' and ',$filter);
								$sql1="Select stock, rate from opening_stockitemwise where $filter";
								$result1=DB_query($sql1,$db);
								$myrow1=DB_fetch_array($result1);
								return $myrow1['stock'];
							}
                       ////////////Opening Balance End/////////////////////////
					   
					   

						////1. MRN/////
						function receivedMRN($function_filter,$item)
						{
							global $db;
							$site=$function_filter[0];
													
							if($site!='')
							$filter[]="addmrn.site = '".$site."'";
							if($item!='')
							$filter[]="addmrnqty.mst_itemid = '".$item."'";
							
							$filter=implode(' and ',$filter);
							$sql1="Select SUM(addmrnqty.reciveqty) from addmrn left join addmrnqty 
							       on(addmrn.id=addmrnqty.issue_id) where $filter";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(addmrnqty.reciveqty)'];
						}
						

							////1. Issue Slip/////
							function issueSlip($function_filter,$item)
							{
								global $db;
								$site=$function_filter[0];
								$store=$function_filter[1];
								
								if($site!='')
								$filter[]="addissue.site = '".$site."'";
								if($item!='')
								$filter[]="issueqty.mst_itemid = '".$item."'";
								
								$filter=implode(' and ',$filter);
							
								$sql1="Select SUM(issueqty.issueqty) from addissue left join 
								 issueqty on(addissue.id=issueqty.issue_id) where $filter";
								$result1=DB_query($sql1,$db);
								$myrow1=DB_fetch_array($result1);
								
								return $myrow1['SUM(issueqty.issueqty)'];
							}

  ?> 

	<div align="center">
  <div style="overflow:auto; width:600px; height:250px">
  <p align="right" style="padding-left:0px; padding-bottom:0px; margin:0px;"><a href="#" onclick="hidedata();" style="font-size:18px; font-weight:bold;border:#000000 solid 2px; padding:2px; text-decoration:none; background-color:#FFFFFF; color:#000000;" title="Close">X</a></p>
  <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border-collapse:collapse;">
 <tr>
        <th><strong>Select</strong></th>
        <th><strong>Item Code</strong></th>
        <th><strong>Item Name</strong></th>
		<th><strong>UOM</strong></th>		
		<th ><strong>Stock Balance</strong></th>
		</tr> 
		<?php   
	    $i=0;	 
		if($_REQUEST['site'])
	     $store_sql = "SELECT id,StockID,item_name,unit,item_no FROM item_master  where item_name like '%".$_REQUEST['site']."%'";
		  if($_REQUEST['site3'])
		  $store_sql = "SELECT id,StockID,item_name,unit,item_no FROM item_master  where  item_no like '%".$_REQUEST['site3']."%'";
		 // echo "SELECT id,StockID,item_name,unit,item_no FROM item_master  where  item_no like '%".$_REQUEST['site3']."%'";
        $Result=DB_query($store_sql,$db);
	  while($myrow =DB_fetch_array($Result))
	  {  	 		
		 $item_name= $s->GetFieldValue('item_master'," StockID='".$myrow['StockID']."'" ,'item_name');
		 $parameter=$myrow['id'];
		 $unitname= $s->GetFieldValue('unitsofmeasure',"unitid='".$myrow['unit']."'",'unitname');
				          
		$op=openingBalance($function_filter,$myrow['id']);
		$in_1=receivedMRN($function_filter,$myrow['id']);
		$out_1=issueSlip($function_filter,$myrow['id']);
		
		
		$total_current=$op+$in_1-$out_1;		
		
		$stock_summary=DB_query("select * from stock_summary where
		           site='".$_REQUEST['site1']."'and  item='".$myrow['id']."' 
				   and  date='".FormatDateForSQL($_REQUEST['date'])."'",$db);
		$rowdata=DB_fetch_array($stock_summary);
		if($rowdata['closing_rate']=='')
		{
		  $get_previous_closing="Select * from stock_summary where site='".$_REQUEST['site1']."' 
		     and item='".$myrow['id']."' and date <'".FormatDateForSQL($_REQUEST['date'])."'
			            ORDER BY DATE DESC LIMIT 0,1";
						$previous_closing_result=DB_query($get_previous_closing,$db);
						$previous_closing_row=DB_fetch_array($previous_closing_result);
			               $outward_rate=$previous_closing_row['closing_rate'];
									
		
		}
		else
		{
		 $outward_rate=$rowdata['closing_rate'];
		}
		if($outward_rate=='')
		 $outward_rate=0;
		 
		
 	  ?> 	  
	   <tr bgcolor="#FFFFFF">
        <td>
       <a href="javascript:getItemsList1('<?php echo $parameter;?>','<?php echo $myrow['StockID'];?>','<?php echo  
	   htmlspecialchars( $myrow['item_name'], ENT_QUOTES);?>','<?php echo $unitname;?>','<?php echo $total_current;?>','<?php echo $myrow['unit'];?>','<?php echo $myrow['item_no']; ?>','<?php echo $_REQUEST['data'];?>')"><?php echo $parameter;?></a>	
		 </td>
        <td > <?php echo $myrow['StockID'];?> </td>
        <td ><?php echo $item_name;?></td>		
		<td ><?php  echo $s->GetFieldValue('unitsofmeasure',"unitid='".$myrow['unit']."'",'unitname');
		?></td>
	     <td> <?php  echo $total_current;  ?>	
	    		</tr>
	  <?php $i++; } ?>	
		
	</table>
	</div></div>
	
<script type="text/javascript" language="javascript">

var ItemPreId=document.getElementById("preitemid").value;

</script>