<?php error_reporting(0);?>
<script language="javascript" type="text/javascript">
function showsitename160(str)
{

var xmlhttp;
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)
    {
	var data=xmlhttp.responseText.split("__");	
	document.getElementById("storedata7").innerHTML=data[0];
	  }
  }
  xmlhttp.open("GET","get_sitenamedata3.php?site="+str,true);
	xmlhttp.send();
}

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();
}
function showItems(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?ledgerPartName="+str,true);
xmlhttp.send();
}
</script>
<h2 align="center">Stock Ledger Register</h2>
<form method="post" action="">
<table width="100%" border="0" cellpadding="2" cellspacing="1">
  <tr>
    <td colspan="2"><table width="30%" border="0" cellspacing="1" cellpadding="2" align="center" 
	style="border:1px solid #000;background-color:#FFF">
      <tr>
        <th><strong>BRANCH</strong></th>
        <td>
		<?php
		if($_SESSION['AssignedBranch']!='')
	  {	  	
  $ss1="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql1=DB_query($ss1,$db);
	  }
	  else
	  {
		$ss1="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql1=DB_query($ss1,$db);
	  }
	  ?>
		
		
		<select name="site" id="site" onchange="showsitename160(this.value)">
                <option value="0">Select Site</option>
                <?php 
				
				while($site_row = DB_fetch_array($site_sql1))
				{
					$p='';
					if($site_row['loccode'] == $_POST['site'])	
					$p="selected";
				?>
                <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
                <?php } ?>
        	</select>   </td>
      </tr>
      <tr>
        <th><strong>GODOWN</strong></th>
        <td><div id="storedata7">
            	<select name="store1" id="store1">
                <?php 
	 			$store_sql = "SELECT id, StoreName,storecode FROM storetype where Store_Site = '".$_POST['site']."'";
	 			$store_result = DB_query($store_sql,$db);
	 			while($store_row = DB_fetch_array($store_result))
				{
					$p='';
					if($store_row['id'] == $_REQUEST['store1'])
						$p="selected";
	 			?>
                <option value="<?php echo $store_row['id'] ?>"<?php echo $p; ?>><?php echo $store_row['storecode']?></option>
                <?php } ?>
                </select></div>     </td>
      </tr>
      <tr>
        <th><strong> DATE FROM : </strong></th>
        <td><input type="text" id="frmdate" name="from_date" value="<?php echo $_POST['from_date'];?>" class="datepicker" 
		style="width:100%"></td>
      </tr>
      <tr>
        <th><strong> DATE TO : </strong></th>
        <td><input type="text" id="todate" name="to_date" value="<?php echo $_POST['to_date'];?>" class="datepicker" 
		style="width:100%"></td>
      </tr>
	 
    </table></td>
  </tr>
  <tr><td colspan="2">&nbsp;</td></tr>
  <tr>
	  
	  
      <td width="49%" align="center" valign="top"><table width="90%" border="0" cellspacing="1" cellpadding="2" align="center" style="border:1px solid #000;background-color:#FFF">
    
	<?php 
	   $filt=''; 
	   if($_REQUEST['view']!='') 
	   {
	   		if($_REQUEST['site']!='0')
			$filt[]="site = '".$_REQUEST['site']."'";
			if($_REQUEST['store1']!='0')
	   		$filt[]="store = '".$_REQUEST['store1']."'";
			if($_REQUEST['item']!='0')
	   		$filt[]="itemcode = '".$_REQUEST['item']."'";
			if($_REQUEST['from_date']!='')
			$filt[]="reveive_date >= '".date("Y-m-d", strtodate($_REQUEST['from_date']))."'";
			if($_REQUEST['to_date']!='')
			$filt[]="reveive_date <= '".date("Y-m-d", strtodate($_REQUEST['to_date']))."'";
			
			if($filt!='')
	   		{
				$filt=implode(' and ',$filt);
			}
			
			//echo "Select stock,reveive_date from opening_stockitemwise where $filt";
			$op_sql=DB_fetch_array(DB_query("Select stock,reveive_date from opening_stockitemwise where $filt",$db));
			$stock=((float)$op_sql['stock']);
			
	   }
	   
	   ?>
   
      </table>
      </td>
  </tr>
  <tr>
  	<td colspan="2"><p align="center">
        <input type="submit" name="view" value="View" onclick="return checkdata();">
	
    <input type="reset" name="cancel" value="Cancel"></p></td>
  </tr>
</table>
</form>
<p>&nbsp;</p>
<form action="" method="post">
<p>
<?php 
$fordate = $_POST['from_date'];
$todate = $_POST['to_date'];
$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;
?>

<?php 
if($_POST['view'])
{

	$filter = array();
	if($_REQUEST['site']!='0')
	{
  	$filter[]=$_REQUEST['site'];
	}
	if($_REQUEST['store1']!='')
	{
	$filter[]=$_REQUEST['store1'];
	}
	if($_POST['from_date']!='')
	{
 	$filter[]=$fdate;
    }
	if($_POST['to_date']!='')
	{ 
	$filter[]=$toyrdate;
	}
	
	
	$filter2=implode(" AND ",$filter);
	
	
	echo $filter2;
	
	
	
	
	
	
	
	
	
	
	
	 
			
			
			
	
	
	
	
	if(count($data)<=0)
	{	
	$receive=array();
	$issueqty=array();	
	/**************************************  MRN *********************************************/
	
	$sqlmrn=DB_query("select sum(reciveqty) as reciveqty from addmrnqty 
	inner join addmrn on addmrn.id=addmrnqty.issue_id  where site='".$_REQUEST['site']."'
	 and StoreCode='".$_REQUEST['store1']."' and MRNDate <='".$fdate."' 
	 and MRNDate <='".$toyrdate."' and mst_itemid='".$_REQUEST['item']."'",$db);
	while($addmrn=DB_fetch_array($sqlmrn))
	{	
	$receive[]=$addmrn['reciveqty'];
	}	
	
	       
		   
	       /**************************************  Issue *********************************************/
	$sqlissue=DB_query("SELECT sum(issueqty) AS issueqty FROM 
                        issueqty INNER JOIN addissue ON addissue.id=issueqty.issue_id where site='".$_REQUEST['site']."' and
						mst_storeid='".$_REQUEST['store1']."' 
					and issuedate <='".$fdate."' and issuedate <='".$toyrdate."' and mst_itemid='".$_REQUEST['item']."' ",$db);	
	while($addissue=DB_fetch_array($sqlissue))
	{
	$issueqty[]=$addissue['issueqty'];
	}
			
	/**************************************  Receive *********************************************/
	$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='".$_REQUEST['site']."' and
						store='".$_REQUEST['store1']."' and returndate <='".$fdate."' and returndate <='".$toyrdate."' 
						and itemid='".$_REQUEST['item']."'",$db);
	while($addreceive=DB_fetch_array($sqlreceive))
	{
	$receive[]=$addreceive['reciveqty'];
	}
	
	
	/**************************************  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='".$_REQUEST['site']."' and
						fromstore='".$_REQUEST['store1']."' and issuedate <= '".$fdate."'  and  issuedate <='".$toyrdate."' 
						and itemid='".$_REQUEST['item']."'",$db);
	while($addsitetosite=DB_fetch_array($sqlsite_site))
	{
	$issueqty[]=$addsitetosite['issueqty'];
	}
	
	
	/**************************************  site to site receive *********************************************/
	
		$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='".$_REQUEST['site']."' and
						Received_store='".$_REQUEST['store1']."' and MRNDate <= '".$fdate."' and MRNDate <='".$toyrdate."' 
						and itemid='".$_REQUEST['item']."'",$db);
	while($addsitetositereceive=DB_fetch_array($sqlsite_site1))
	{
	$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='".$_REQUEST['site']."' and
						storecode='".$_REQUEST['store1']."' and Reqdate <= '".$fdate."' and  Reqdate <='".$toyrdate."' 
						and itemid='".$_REQUEST['item']."'",$db);
	while($addmrtov=DB_fetch_array($sqlsite_site1))
	{
	$issueqty[]=$addmrtov['issueqty'];
	
	}
	
	$sqlrgp=DB_query("SELECT sum(qty) AS issueqty
	                     FROM  rgp_tab_details INNER JOIN rgp_tab 
				        ON rgp_tab.id=rgp_tab_details.parent_id where branch='".$_REQUEST['site']."' and
						store='".$_REQUEST['store1']."' and date <= '".$fdate."'  and  date <='".$toyrdate."' 
						and mst_itemid='".$_REQUEST['item']."'",$db);
	while($addrgp=DB_fetch_array($sqlrgp))
	{
	$issueqty[]=$addrgp['issueqty'];
	}
	
	
	/**************************************  RGP Return  *********************************************/
	$sqlrgpreturn=DB_query("SELECT sum(qty) AS reciveqty
	                      FROM  rgp_return_details INNER JOIN rgp_return 
				        ON rgp_return.id=rgp_return_details.parent_id
						 where branch='".$_REQUEST['site']."' and
						store='".$_REQUEST['store1']."' and return_date <= '".$fdate."' and return_date <='".$toyrdate."' 
						and item_id='".$_REQUEST['item']."'",$db);
	while($addrgpreturn=DB_fetch_array($sqlrgpreturn))
	{
	$receive[]=$addrgpreturn['reciveqty'];
	}
	
	
	
	
	
	
	
	//$qty=array_sum($receive)-array_sum($issueqty);
	$total15=array_sum($receive);
	$total16=array_sum($issueqty);
	if($total15==$total16)
	$qty=0;
	else
	$qty=trim($total15)-trim($total16);
	/*******************************************  Opening ******************************************************/
	$sqlopen=DB_query("select reveive_date as date,itemcode as itemid,stock as reciveqty
	from opening_stockitemwise  where site='".$_REQUEST['site']."'
	 and store='".$_REQUEST['store1']."' and reveive_date <='".$fdate."' 
	 and reveive_date <='".$toyrdate."' and itemcode='".$_REQUEST['item']."'",$db);
	while($addopen=DB_fetch_array($sqlopen))
	{	
	$qty1=trim($addopen['reciveqty'])+trim($qty);
	//$vender=$s->getField('suppliers','id',$addmrn['vendor'],'vendor_name');
	$data[]=array('slipno'=>$addopen['slipno'],'itemid'=>$addopen['itemid'],
	'date'=>$addopen['date'],'reciveqty'=>$qty1,'issueqty'=>'','doc'=>'','description'=>'Opening');
	}	
	
	
	}
	
		
	
	foreach ($data as $key => $row1)
    {
    $volume[$key]= $row1['date'];
	$edition1[$key]=$row1['reciveqty'];
	
    }
    array_multisort($volume,$edition1,SORT_DESC,$data);
	?>
	 
  <p align="right"><a target="_blank" href="stockledger_print.php?site=<?php echo $_REQUEST['site']; ?>&store1=<?php echo $_REQUEST['store1']; ?>&item=<?php echo $_REQUEST['item']; ?>&from_date=<?php echo $_REQUEST['from_date']; ?>&to_date=<?php echo $_REQUEST['to_date']; ?>"><input type="button" name="print" value="Print" ></a></p>
 <?php $branch=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
$storedata=$s->getField('storetype','id',$_REQUEST['store1'],'StoreName');
  ?>
   <?php
    $confac=$s->getField('item_master','id',$_REQUEST['item'],'conv_factor');
  $itemName=$s->getField('item_master','id',$_REQUEST['item'],'item_name');
  $Unit=$s->getField('unitsofmeasure','unitid',$s->getField('item_master','id',$_REQUEST['item'],'unit'),'unitname');
  $message="Showing Records of Branch  ".$branch." and Store ".$storedata." For Item ".$itemName." And Unit  ".$Unit."  From  ".$fordate." to ".$todate."";
  $_SESSION['mess1']=$message;
   ?>
  <p align="center"><strong><?php echo $message; ?></strong></p>
 <table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#000000" style="border:#000000 solid 1px; background:#999999">
    <tr>
	 <th width="4%"><strong>S.R.</strong></th>
      <th width="5%"><strong>Date</strong></th>
	   <th width="17%"><strong>Receiving from / issue to</strong></th>
      <th width="15%"><strong>Transaction No.</strong></th>
      <th width="10%"><strong>Against Bill/Challan </strong></th>
      <th width="8%"><strong>Primary Received Qty </strong></th>
	   <th width="9%"><strong>Secondary Received Qty </strong></th>
      <th width="7%">Primary Issued Qty</th>
	  <th width="9%">Secondary Issued Qty</th>
	   <th width="7%">Primary Bal Qty</th>
	   <th width="9%">Secondary Bal Qty</th>
    </tr>
	  <?php
	if(count($data)>0)
	{
	$var=0;
	$i=1;
	foreach($data as $key=>$value)
	{	
	if($value['description']=='Opening')
	{
	$bgcolor='#999999';
	}
	else
	{
	$bgcolor='#FFFFFF';
	}
	?>		
	<tr bgcolor="<?php echo $bgcolor; ?>">
	<td><?php echo $i++;?></td>
	<td><?php echo ConvertSQLDate($value['date']);?></td>
	<td><?php echo $value['description'];?></td>
    <td><?php echo $value['slipno'];?></td>
    <td><?php echo $value['doc'];?></td>
     
      <td><?php   echo $value['reciveqty'];
	   $recieve[]=$value['reciveqty'];
	  //}
	  ?></td>
	  <td><?php   echo round($value['reciveqty']/$confac,2);
	   $recieve2[]=$value['reciveqty']/$confac;
	  //}
	  ?></td>
      <td><?php echo $value['issueqty']; $issue[]=$value['issueqty'];?></td>
	   <td><?php echo round($value['issueqty']/$confac,2); $issue2[]=$value['issueqty']/$confac;?></td>
      <td><?php $total12=array_sum($recieve);
	            $total13=array_sum($issue);
				$opentotal=trim($total12)-trim($total13);
	  
	   echo $opentotal; ?></td>
	   <td><?php $total12=array_sum($recieve2);
	            $total13=array_sum($issue2);
				$opentotal=trim($total12)-trim($total13);
	  
	   echo round($opentotal,2); ?></td
    ></tr>
 <?php }  ?>

<tr><td>&nbsp;</td>
 
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td><strong>Total</strong></td>
  <th><?php echo $stock1=array_sum($recieve);
 
  
    ?></th>
	 <th><?php echo $stock2=round(array_sum($recieve2),2);
 
  
    ?></th>
	 <th><?php echo $stock2=array_sum($issue);
 
  
    ?></th>
  <th><?php echo $stock1=round(array_sum($issue2),2);
 
  
    ?></th>
  <th><?php
     $receivedata=array_sum($recieve);
	 $issuedata=array_sum($issue);
      $total=trim($receivedata)-trim($issuedata);
   echo $stock1=$total;
 
  
    ?></th>
	<th><?php
     $receivedata1=array_sum($recieve2);
	 $issuedata1=array_sum($issue2);
      $total=trim($receivedata1)-trim($issuedata1);
   echo $stock1=round($total,2);
 
  
    ?></th>
	
	</tr>
	
 <tr><td colspan="5">&nbsp;</td>
 
 <th colspan="2">Total Primary in Store </th><th>
 <?php 
     $receivedata2=array_sum($recieve);
	 $issuedata2=array_sum($issue);
      $total2=trim($receivedata2)-trim($issuedata2);
 echo $stock1=$total2;
 
  
    ?></th></tr>
	<tr><td colspan="5">&nbsp;</td>
 
 <th colspan="2">Total Secondary in Store </th><th>
 <?php 
     $receivedata2=array_sum($recieve2);
	 $issuedata2=array_sum($issue2);
      $total2=trim($receivedata2)-trim($issuedata2);
 echo $stock1=round($total2,2);
 
  
    ?></th></tr>
<?php } else { ?>
<tr bgcolor="#FFFFFF">
<td  colspan="11">No Records</td></tr>

<?php } ?>
  </table>
  <?php } ?>
</form>

<script>

function checkdata()
{
if(document.getElementById("site").value=='0')
{
alert("please Select Site");
return false;
}
if(document.getElementById("store1").value=='')
{
alert("please Select Store");
return false;
}
if(document.getElementById("frmdate").value=='')
{
alert("please Select From Date");
return false;

}

if(document.getElementById("todate").value=='')
{
alert("please Select To date");
return false;
}
if(document.getElementById("item").value=='')
{
alert("please Select To date");
return false;
}
return true;

}

</script>

