<?php
$PageSecurity = 1;
session_name('CAKEPHP'); session_start();
include('config.php');
include('includes/ConnectDB.inc');
include('includes/function_lib.php');
include('includes/LanguageSetup.php');
include('includes/DateFunctions.inc');
?>
<link rel="stylesheet" href="css/purchase_requision_print.css">
<div class="body_contain">
<div class="tab_div">
<div>
<form action="" method="post">
<table width="100%" border="0">
   <tr>
    <td>Item Name</td><td>Currnet Stock</td><td>Del. Quantity</td><td>Reason</td>
  </tr>
  <?php 
              $function_filter[]=$_REQUEST['site'];
			   $function_filter[]=$_REQUEST['store'];
			
                 $flag=0;
				// echo "select * from addmrnqty where issue_id='".$_REQUEST['id']."' and reciveqty!='0.00'";
  $itemdata=DB_query("select * from addmrnqty where issue_id='".$_REQUEST['id']."' and reciveqty!='0.00'",$db);
				  while($itemid1=DB_fetch_array($itemdata))
				  {    	
				 			
                      $privious_open=privious_opening($function_filter,$itemid1['mst_itemid']);  	
				     
					   $receiveqty=$itemid1['reciveqty'];
					   if($privious_open<$receiveqty)
					{
					$flag=1;
					}
					?>
  <tr>
    <td><?php echo $s->getField('item_master','id',$itemid1['mst_itemid'],'item_name'); ?></td>
	<td><?php echo $privious_open; ?></td><td><?php echo $receiveqty; ?></td>
	<td><?php if($flag==1) { echo "Stock Balance not Sufficient"; }  ?></td>
  </tr>
  <?php } ?>
 
</table>
  <p align="center"><input type="submit" name="Close" value="Close" /></p>
  </form>  

  
</div>
<?php


         
				   if(isset($_POST['Close']))
					{
echo "<script>parent.window.close();
parent.window.opener.location.href='index.php?route=Inventory/transation/material_receipt_note_exo_index&modid=".$_REQUEST['modid'].                      "&frmid=".$_REQUEST['frmid']."'</script>";			
				    }
				
	function privious_opening($function_filter,$item)
	{
			 global $db;
			 $receive=array();
			 $issueqty=array();	
			 $site=$function_filter[0];
			 $store=$function_filter[1];	
	/**************************************  MRN *********************************************/
			$sqlmrn=DB_query("select sum(reciveqty) as reciveqty from addmrnqty 
			inner join addmrn on addmrn.id=addmrnqty.issue_id  where site='".$site."'
			and StoreCode in ('".$store."')
			and Cancel_status='N' and reciveqty!='0.00' 
			and mst_itemid in ('".$item."')",$db);		 
				while($addmrn=DB_fetch_array($sqlmrn))
				{
				if($addmrn['reciveqty']!='')
				{
				$receive[]=$addmrn['reciveqty'];
				$total[]=$addmrn['total'];
				}
				}
			$sqlissue=DB_query("SELECT sum(issueqty) AS issueqty FROM 
			issueqty INNER JOIN addissue ON addissue.id=issueqty.issue_id where site='".$site."' and
			mst_storeid in ('".$store."')  and issueqty!='0.00' 
			and mst_itemid in ('".$item."') ",$db);	
				while($addissue=DB_fetch_array($sqlissue))
				{
				if($addissue['issueqty']!='')
				{
				$issueqty[]=$addissue['issueqty'];
				$total1[]=$addissue['total'];
				}
				}			
	    	$sqlreceive=DB_query("SELECT sum(receiveqty) AS reciveqty
	        FROM  material_return_details INNER JOIN material_return 
			ON material_return.id=material_return_details.parentId where site='".$site."' and
			store in ('".$store."') and  receiveqty!='0.00'
			and itemid in ('".$item."')",$db);
				while($addreceive=DB_fetch_array($sqlreceive))
				{
				if($addreceive['reciveqty']!='')
				{
				$receive[]=$addreceive['reciveqty'];
				$total[]=$addreceive['total'];
				}
				}	
	/**************************************  site to site *********************************************/
	      $sqlsite_site=DB_query("SELECT sum(issueqty) AS issueqty
	      FROM  transfer_issue_details INNER JOIN transfer_issue 
		  ON transfer_issue.id=transfer_issue_details.transfer_issue_id where sitecode='".$site."' and
		  fromstore in ('".$store."')   and issueqty!='0.00'
		  and itemid in ('".$item."')",$db);
			while($addsitetosite=DB_fetch_array($sqlsite_site))
			{
			if($addsitetosite['issueqty']!='')
			{
			$issueqty[]=$addsitetosite['issueqty'];
			$total1[]=$addsitetosite['total'];
			}
			}
			$sqlsite_site1=DB_query("SELECT sum(reciveqty) AS reciveqty
			FROM  transfer_receipt_details INNER JOIN transfer_receipt 
			ON transfer_receipt.id=transfer_receipt_details.transfer_receipt_id
			where Received_to='".$site."' and
			Received_store in ('".$store."')  and reciveqty!='0.00'
			and itemid in ('".$item."')",$db);
				while($addsitetositereceive=DB_fetch_array($sqlsite_site1))
			{
			if($addsitetositereceive['reciveqty']!='')
			{
			$receive[]=$addsitetositereceive['reciveqty'];
			}
			}
	/**************************************  Material Return *********************************************/
		  $sqlsite_site1=DB_query("SELECT sum(ReturnQty) AS issueqty
		  FROM  inv_material_return_details INNER JOIN inv_material_return 
		  ON inv_material_return.id=inv_material_return_details.parentId where sitecode='".$site."' and
		  storecode in ('".$store."')  and ReturnQty!='0.00'
		  and itemid in ('".$item."')",$db);
				while($addmrtov=DB_fetch_array($sqlsite_site1))
			{
			if($addmrtov['issueqty']!='')
			{
			$issueqty[]=$addmrtov['issueqty'];
			}
			}	
	/*******************************************  Opening ******************************************************/
		$sqlopen=DB_query("select reveive_date as date,itemcode as itemid,stock as reciveqty,amount
		from opening_stockitemwise  where site='".$site."'
		and store in ('".$store."')  
		and itemcode in ('".$item."')",$db);
				while($addopen=DB_fetch_array($sqlopen))
			{
			if($addopen['reciveqty']!='')
			{
			$receive[]=$addopen['reciveqty'];	
			}
			}
	
	
	$qty=array_sum($receive)-array_sum($issueqty);
	

	return $qty;   
	}
		
?>