<?php error_reporting(0);
session_name('CAKEPHP'); session_start();
$PageSecurity = 3;
 include ('includes/session.inc');
include ('includes/function_lib.php');
 include('includes/fun-php.php');
include('includes/fun-HRClass.php');
// $_REQUEST['him'];
function getField($table,$condition,$val,$field)
{
	global $db;
	$sql=DB_query("select ".$field." from ".$table." where ".$condition." ='".$val."'",$db);
	$r=DB_fetch_array($sql);
	return ($r[$field]);
}

	global $db; 
	$Compamies=DB_query("select * from companies",$db);
	$CompamiesRow=DB_fetch_array($Compamies);
	$CompamiesRow['CompanyId'];
	$branch=getField('locations','CompanyId',$CompamiesRow['Branch_Permission'],'locationname'); 

?>
<script language="javascript" type="text/javascript">
  function toggle_visibility(id) {
	
	 
       var e = document.getElementById(id);
	      // e.style.display = 'none';
		       //alert(e.style.display);
			   window.print();
    }
  
</script>
<link href="css/professional/default.css" rel="stylesheet" type="text/css" />
<div class="body_contain">
<div class="tab_div">
  <div>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td align="left"><img src="images/cloud_logo.jpg" /></td>
        <td align="right"><img src="images/right_img.jpg" /></td>
      </tr>
      <tr>
        <td colspan="2"><h1>Report Stock Ledger</h1></td>
      </tr>
	  <tr><td colspan="2"><div id="foo">
    <p align="right"> <a href="#" onclick="javascript:toggle_visibility('foo');" >Print</a> </p>
  </div></td></tr>
    </table>
  </div>
  <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border-collapse:collapse;">
    <tr bgcolor="#CCCCCC">
      <td width="104">
	  <strong>Site :</strong></td>
      </td>
      <td width="96">
	  <?php  
				if($_REQUEST['loccode']!=''){
					 $loccode = "SELECT * FROM locations where loccode = '".$_REQUEST['loccode']."'";
					 $sitesql = DB_query($loccode,$db);
					 $sitefetch = DB_fetch_array($sitesql);
					 echo $sitefetch['locationname']."<br>  ";
					 
				}
			 
				 
			?>
	  </td>
	  <?php 
	  		if($_REQUEST['item_code']!=''){
				 
					 $itemname = "Select StockID,item_name from item_master where StockID = '".$_REQUEST['item_code']."'";
					 $itemsql = DB_query($itemname,$db);
					 $itemfetch = DB_fetch_array($itemsql);
					 echo "<strong>Item name : </strong> " .$itemfetch['item_name']."<br>";
					 
			 	}
	  ?>
      <td width="77"><strong>Item Code : </strong></td>
      <td><?php echo $itemfetch['StockID']; ?></td>
      <td width="77" ><strong>From Date:</strong></td>
      <td width="67" ><?php if($_REQUEST['fromdate'] !=''){?><?php echo $_REQUEST['fromdate']; ?><?php }?></td>
      
      <td width="40" ><strong>To Date:</strong></td>
      <td width="51" ><?php if($_REQUEST['todate']!=''){?><?php echo $_REQUEST['todate']; ?><?php } ?></td>
	  <td colspan="5">&nbsp;</td>
    </tr>
    <tr bgcolor="#CCCCCC">
      <td class="tabledata"><strong>Date</strong></td>
      <td class="tabledata"><strong>Item Code</strong></td>
      <td class="tabledata"><strong>Item Description</strong></td>
      <td class="tabledata"><strong>Opening Stock Qty</strong></td>
      <td class="tabledata"><strong>Received Qty</strong></td>
      <td class="tabledata"><strong>Issue Qty</strong></td>
      <td class="tabledata">Return Qty</td>
      <td class="tabledata">Vendor Return Qty</td>
      <td class="tabledata"><strong>Site Issue Qty</strong></td>
      <td class="tabledata"><strong>Site Receive Qty</strong></td>
      <td class="tabledata"><strong>Closing Balance Qty</strong></td>
      <td class="tabledata"><strong>UOM</strong></td>
      <td class="tabledata"><strong>Branch</strong></td>
    </tr>
    <p>
      <?php 
		
if($_REQUEST['fromdate']!='' ||$_REQUEST['todate']!='' || $_REQUEST['loccode']!='' || $_REQUEST['store1']!='' || $_REQUEST['item_code']!='')	
{
$fordate = $_REQUEST['fromdate'];
$todate = $_REQUEST['todate'];
$fromdate = explode("/", $fordate);
$daydate = $fromdate['0'];
$Monthdate = $fromdate['1'];
$yeardate = $fromdate['2'];
$fdate = $yeardate."-".$Monthdate."-".$daydate;
					   
$tredate   = explode("/", $todate);
$dayda     = $tredate['0'];
$Monthda   = $tredate['1'];
$yearda    = $tredate['2'];
$toyrdate  = $yearda."-".$Monthda."-".$dayda;
	$filter = array();
	
  		$filter[]=$_REQUEST['loccode'];
	
		$filter[]=$_REQUEST['store1'];
	
 		$filter[]=$fdate;
		$filter[]=$toyrdate;
	
		$filter[]=$_REQUEST['item_code'];
	//print_r($filter);
	$filter2=implode(",",$filter);
	//print_r($filter2);
	function OPENING($parameter)
	{	
		global $db;	
		$parameterBrk=explode(",",$parameter);	
		//print_r($parameterBrk);	
		if($parameterBrk[0]!='')
		{
			$subFilter[]="opening_stockitemwise.site='".$parameterBrk[0]."'";
		}
		if($parameterBrk[1]!='')
		{
			$subFilter[]="opening_stockitemwise.store='".$parameterBrk[1]."'";
		}
		if($parameterBrk[2]!='--' && $parameterBrk[3]!='--')
		{
			$subFilter[]="opening_stockitemwise.reveive_date between '".$parameterBrk[2]."' and '".$parameterBrk[3]."'";
		}
		if($parameterBrk[4]!='')
		{
			$subFilter[]="opening_stockitemwise.itemcode='".$parameterBrk[4]."'";
		}
		if($subFilter!='')
		{
			$subFilter1=implode(" and ", $subFilter);
		}
		   $sql="Select * from opening_stockitemwise  where $subFilter1";
		  $_SESSION['opening_stockitemwise']= $sql;

		$result=DB_query($sql,$db);
		//$data=array();
		while($row=DB_fetch_array($result))
		{
			$data[]=$row['reveive_date'].",".$row['itemcode'].",".$row['itemdesc'].",".$row['stock'].",".$row['uom'].",".$row['site'];         
			
			
		}
	
		return $data ;
	}

	
	
	
	
	
	function MRN($parameter)
	{
		global $db;	
		$parameterBrk=explode(",",$parameter);	
		//print_r($parameterBrk);	
		if($parameterBrk[0]!='')
		{
			$subFilter[]="addmrn.site='".$parameterBrk[0]."'";
		}
		if($parameterBrk[1]!='')
		{
			$subFilter[]="addmrn.StoreCode='".$parameterBrk[1]."'";
		}
		if($parameterBrk[2]!='--' && $parameterBrk[3]!='--')
		{
			$subFilter[]="addmrn.OrderDate between '".$parameterBrk[2]."' and '".$parameterBrk[3]."'";
		}
		if($parameterBrk[4]!='')
		{
			$subFilter[]="addmrnqty.itemcode='".$parameterBrk[4]."'";
		}
		if($subFilter!='')
		{
			$subFilter1=implode(" and ", $subFilter);
		}
		  $sql="Select * from addmrn inner join addmrnqty on (addmrn.id=addmrnqty.issue_id) where $subFilter1";
		  $_SESSION['mrn_session']=$sql;
		$result=DB_query($sql,$db);
		//$data=array();
		while($row=DB_fetch_array($result))
		{
			$data[]=$row['OrderDate'].",".$row['itemcode'].",".$row['description'].",".$row['reciveqty'].",".$row['UOM'].",".$row['site']
			;
			
		}
		return $data ;
	}


function ISSUE($parameter)
	{
		global $db;	
		$parameterBrk=explode(",",$parameter);	
		//print_r($parameterBrk);	
		if($parameterBrk[0]!='')
		{
			$subFilter[]="addissue.site='".$parameterBrk[0]."'";
		}
		if($parameterBrk[1]!='')
		{
			$subFilter[]="addissue.store='".$parameterBrk[1]."'";
		}
		if($parameterBrk[2]!='--' && $parameterBrk[3]!='--')
		{
			$subFilter[]="addissue.issuedate between '".$parameterBrk[2]."' and '".$parameterBrk[3]."'";
		}
		if($parameterBrk[4]!='')
		{
			$subFilter[]="issueqty.itemcode='".$parameterBrk[4]."'";
		}
		if($subFilter!='')
		{
			$subFilter1=implode(" and ", $subFilter);
		}
	 	 $sql="Select * from addissue inner join issueqty on (addissue.id=issueqty.issue_id) where $subFilter1";
		 $_SESSION['add_issue']= $sql;
		$result=DB_query($sql,$db);
		//$data=array();
		while($row=DB_fetch_array($result))
		{
			$data[]=$row['issuedate'].",".$row['itemcode'].",".$row['itemdesc'].",".$row['issueqty'].",".$row['uom'].",".$row['site'];
			//print_r($data);
		}
		return $data ;
	}


function RECEIPT($parameter)
	{	
		global $db;	
		$parameterBrk=explode(",",$parameter);	
		//print_r($parameterBrk);	
		if($parameterBrk[0]!='')
		{
			$subFilter[]="material_return.site='".$parameterBrk[0]."'";
		}
		if($parameterBrk[1]!='')
		{
		 
		//$store=$s->getField('storetype','StoreCode',$parameterBrk[1],'id');
			$subFilter[]="material_return.store='".$parameterBrk[1]."'";
		}
		if($parameterBrk[2]!='--' && $parameterBrk[3]!='--')
		{
			$subFilter[]="material_return.returndate between '".$parameterBrk[2]."' and '".$parameterBrk[3]."'";
		}
		if($parameterBrk[4]!='')
		{
			$subFilter[]="material_return_details.itemcode='".$parameterBrk[4]."'";
		}
		if($subFilter!='')
		{
			$subFilter1=implode(" and ", $subFilter);
		}
		   $sql="Select * from material_return inner join material_return_details on (material_return.id=material_return_details.parentId) where $subFilter1";
		   $_SESSION['material_return']=$sql;
		$result=DB_query($sql,$db);
		//$data=array();
		while($row=DB_fetch_array($result))
		{
			$data[]=$row['returndate'].",".$row['itemcode'].",".$row['itemdesc'].",".$row['receiveqty'].",".$row['uom'].",".$row['site'];
			
		}
	
		return $data ;
	}
	
	
	function VENDOR_RETURN($parameter)
	{	
		global $db;	
		$parameterBrk=explode(",",$parameter);	
		//print_r($parameterBrk);	
		if($parameterBrk[0]!='')
		{
			$subFilter[]="inv_material_return.sitecode='".$parameterBrk[0]."'";
		}
		if($parameterBrk[1]!='')
		{
		 
		//$store=$s->getField('storetype','StoreCode',$parameterBrk[1],'id');
			$subFilter[]="inv_material_return.storecode='".$parameterBrk[1]."'";
		}
		if($parameterBrk[2]!='--' && $parameterBrk[3]!='--')
		{
			$subFilter[]="inv_material_return.Reqdate between '".$parameterBrk[2]."' and '".$parameterBrk[3]."'";
		}
		if($parameterBrk[4]!='')
		{
			$subFilter[]="inv_material_return_details.itemCode='".$parameterBrk[4]."'";
		}
		if($subFilter!='')
		{
			$subFilter1=implode(" and ", $subFilter);
		}
		   $sql="Select * from inv_material_return inner join inv_material_return_details on (inv_material_return.id=inv_material_return_details.parentId) where $subFilter1";
		   $_SESSION['vendor_return']=$sql;
		   
		$result=DB_query($sql,$db);
		//$data=array();
		while($row=DB_fetch_array($result))
		{
			$data[]=$row['Reqdate'].",".$row['itemCode'].",".$row['itemdesc'].",".$row['ReturnQty'].",".$row['uom'].",
			".$row['sitecode'];
			
			
		}
	
		return $data ;
	}
	
	
	function SITEISSUE($parameter)
	{	
		global $db;	
		$parameterBrk=explode(",",$parameter);	
		//print_r($parameterBrk);	
		if($parameterBrk[0]!='')
		{
			$subFilter[]="transfer_issue.sitecode='".$parameterBrk[0]."'";
		}
		if($parameterBrk[1]!='')
		{
		 
		//$store=$s->getField('storetype','StoreCode',$parameterBrk[1],'id');
			$subFilter[]="transfer_issue.fromstore='".$parameterBrk[1]."'";
		}
		if($parameterBrk[2]!='--' && $parameterBrk[3]!='--')
		{
			$subFilter[]="transfer_issue.issuedate between '".$parameterBrk[2]."' and '".$parameterBrk[3]."'";
		}
		if($parameterBrk[4]!='')
		{
			$subFilter[]="transfer_issue_details.itemcode='".$parameterBrk[4]."'";
		}
		if($subFilter!='')
		{
			$subFilter1=implode(" and ", $subFilter);
		}
		     $sql="Select * from transfer_issue inner join transfer_issue_details on (transfer_issue.id=transfer_issue_details.transfer_issue_id) where $subFilter1";
			 $_SESSION['site_issue']=$sql;
		$result=DB_query($sql,$db);
		//$data=array();
		while($row=DB_fetch_array($result))
		{              
			$data[]=$row['issuedate'].",".$row['itemcode'].",".$row['itemdesc'].",".$row['issueqty'].",".$row['uom'].",
			".$row['sitecode'];
			
			
			
		
			
		}

	
		return $data ;
	}
	
	
	function SITEIRECEIPT($parameter)
	{	
		global $db;	
		$parameterBrk=explode(",",$parameter);	
		//print_r($parameterBrk);	
		if($parameterBrk[0]!='')
		{
			$subFilter[]="transfer_receipt.sitecode='".$parameterBrk[0]."'";
		}
		if($parameterBrk[1]!='')
		{
		 
		//$store=$s->getField('storetype','StoreCode',$parameterBrk[1],'id');
			$subFilter[]="transfer_receipt.store='".$parameterBrk[1]."'";
		}
		if($parameterBrk[2]!='--' && $parameterBrk[3]!='--')
		{
			$subFilter[]="transfer_receipt.MRNDate between '".$parameterBrk[2]."' and '".$parameterBrk[3]."'";
		}
		if($parameterBrk[4]!='')
		{
			$subFilter[]="transfer_receipt_details.itemcode='".$parameterBrk[4]."'";
		}
		if($subFilter!='')
		{
			$subFilter1=implode(" and ", $subFilter);
		}
		       $sql="Select * from transfer_receipt inner join transfer_receipt_details on (transfer_receipt.id=transfer_receipt_details.transfer_receipt_id) where $subFilter1";
			   $_SESSION['site_receipt']=$sql;
		$result=DB_query($sql,$db);
		//$data=array();
		while($row=DB_fetch_array($result))
		{              
			$data[]=$row['MRNDate'].",".$row['itemcode'].",".$row['itemdesc'].",".$row['reciveqty'].",".$row['uom'].",
			".$row['sitecode'];
			
			
			
			
		
			
		}

	
		return $data ;
	}
	
	
	
	
	



$t1=OPENING($filter2);


	$totalOpening=0;
	for($k=0;$k<count($t1);$k++)
	{
	//$stockbal=$s->getField('opening_stockitemwise','itemcode',$tt[1],'stock'); 	
		?>
    </p>
    <tr bgcolor="#FFFFFF" onmouseover="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onmouseout="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
      <?php $tt1=explode(",",$t1[$k]);?>
      <td><?php echo COnvertSQLDate($tt1[0]);?></td>
      <td><?php echo $tt1[1];?></td>
      <td><?php echo $tt1[2];?></td>
      <td><?php echo $tt1[3];?></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><?php echo $s->getField('unitsofmeasure','unitid',$tt1[4],'unitname');    ?></td>
      <td><?php
	  echo $s->getField('locations','loccode',$tt1[5],'locationname');?></td>
    </tr>
    <?php  $totalOpening=$totalOpening + $tt1[3];
 }  //echo $totalReceived;












	
	$t=MRN($filter2);


	$totalReceived=0;
	for($k=0;$k<count($t);$k++)
	{
	//$stockbal=$s->getField('opening_stockitemwise','itemcode',$tt[1],'stock'); 	
		?>
    <tr bgcolor="#FFFFFF" onmouseover="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onmouseout="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
      <?php $tt=explode(",",$t[$k]);?>
      <td><?php echo COnvertSQLDate($tt[0]);?></td>
      <td><?php echo $tt[1];?></td>
      <td><?php echo $tt[2];?></td>
      <td></td>
      <td><?php echo $tt[3];?></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><?php echo $s->getField('unitsofmeasure','unitid',$tt[4],'unitname');    ?></td>
      <td><?php echo $s->getField('locations','loccode',$tt[5],'locationname');?></td>
    </tr>
    <?php  $totalReceived=$totalReceived + $tt[3];
 }  //echo $totalReceived;
 ?>
    <?php
	$m=ISSUE($filter2);$totalIssued=0;
	for($k=0;$k<count($m);$k++){
	?>
    <tr bgcolor="#FFFFFF">
      <?php $mm=explode(",",$m[$k]);?>
      <td><?php echo COnvertSQLDate($mm[0]);?></td>
      <td><?php echo $mm[1];?></td>
      <td><?php echo $mm[2];?></td>
      <td></td>
      <td></td>
      <td><?php  echo $mm[3]; ?></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><?php echo $s->getField('unitsofmeasure','unitid',$mm[4],'unitname');    ?></td>
      <td><?php echo $s->getField('locations','loccode',$mm[5],'locationname');?></td>
    </tr>
    <?php $totalIssued=$totalIssued+$mm[3];
  } //echo $totalIssued;
 
 
 	$n=RECEIPT($filter2);$totalReturn=0;
	for($k=0;$k<count($n);$k++){
	?>
    <tr bgcolor="#FFFFFF">
      <?php $nn=explode(",",$n[$k]);?>
      <td><?php echo COnvertSQLDate($nn[0]);?></td>
      <td><?php echo $nn[1];?></td>
      <td><?php echo $nn[2];?></td>
      <td></td>
      <td></td>
      <td></td>
      <td><?php echo $nn[3];?></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><?php echo $s->getField('unitsofmeasure','unitid',$nn[4],'unitname');    ?></td>
      <td><?php echo $s->getField('locations','loccode',$nn[5],'locationname');?></td>
    </tr>
    <?php $totalReturn=$totalReturn+$nn[3];
 } //echo $totalReturn;
 
 
 	
 
 $n1=VENDOR_RETURN($filter2);$totalVendorReturn=0;
	for($k=0;$k<count($n1);$k++){
	?>
    <tr bgcolor="#FFFFFF">
      <?php $nn1=explode(",",$n1[$k]);?>
      <td><?php echo COnvertSQLDate($nn1[0]);?></td>
      <td><?php echo $nn1[1];?></td>
      <td><?php echo $nn1[2];?></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><?php echo $nn1[3];?></td>
      <td></td>
      <td></td>
      <td><?php echo $s->getField('unitsofmeasure','unitid',$nn1[4],'unitname');    ?></td>
      <td><?php echo $s->getField('locations','loccode',$nn1[5],'locationname');?></td>
    </tr>
    <?php $totalVendorReturn=$totalVendorReturn+$nn1[3];
 } //echo $totalReturn;
 
 
 
 
 $n2=SITEISSUE($filter2);$totalSiteIssue=0;
	for($k=0;$k<count($n2);$k++){
	?>
    <tr bgcolor="#FFFFFF">
      <?php $nn2=explode(",",$n2[$k]);?>
      <td><?php echo COnvertSQLDate($nn2[0]);?></td>
      <td><?php echo $nn2[1];?></td>
      <td><?php echo $nn2[2];?></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><?php echo $nn2[3];?></td>
      <td></td>
      <td></td>
      <td><?php echo $s->getField('unitsofmeasure','unitid',$nn2[4],'unitname');    ?></td>
      <td><?php 
	   
	  
	  echo $s->getField('locations','loccode',$nn2[5],'locationname');?></td>
    </tr>
    <?php   $totalSiteIssue=$totalSiteIssue+$nn2[3];
 } //echo $totalReturn;
 
 
 
 $n3=SITEIRECEIPT($filter2);$totalSiteReturn=0;
	for($k=0;$k<count($n3);$k++){
	?>
    <tr bgcolor="#FFFFFF">
      <?php $nn3=explode(",",$n3[$k]);?>
      <td><?php echo COnvertSQLDate($nn3[0]);?></td>
      <td><?php echo $nn3[1];?></td>
      <td><?php echo $nn3[2];?></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><?php echo $nn3[3];?></td>
      <td></td>
      <td><?php echo $s->getField('unitsofmeasure','unitid',$nn3[4],'unitname');    ?></td>
      <td width="17"><?php 	  
	  echo $s->getField('locations','loccode',$nn3[5],'locationname');?></td>
    </tr>
    <?php $totalSiteReturn=$totalSiteReturn+$nn3[3];
 } 

 
$Balance=(((($totalOpening+$totalReceived)-$totalIssued)+$totalReturn)-($totalVendorReturn)-($totalSiteIssue-$totalSiteReturn));
 
 ?>
    <tr bgcolor="#FFFFFF">
      <td colspan="10" align="right">&nbsp;</td>
      <td><strong><?php echo $Balance;?></strong></td>
    </tr>
    <?php }  
		?>
  </table>
  <br />
  <div class="authorize">
    <div style="float:left; padding-top:30px;"><img src="images/footer_bg_left.jpg" /></div>
    <div style="float:right"><img src="images/footer_bg_right.jpg" /></div>
    <div style="clear:both;"></div>
  </div>
  
</div>
<script type="text/javascript" language="javascript">
  function toggle_visibility(id) {
       var e = document.getElementById(id);
	      e.style.display = 'none';
		       //alert(e.style.display);
			   window.print();
    }
  
</script>
