<?php include("../../include/dbconnect.php");

?>
<script language="javascript">
function OpenWindow(url)
{
	window.open(url,'ledg','width=900,height=400,scrollbars=yes,resizable=0');
}
</script>
<STYLE TYPE='text/css'>
	a {text-decoration:none}
	a { color: black; }
P.pagebreakhere {page-break-after: always}
</STYLE>

<title>Balance Sheet  Printing</title>
<form>
<span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" >

<?



$delfile = "../../../tmp/invoice.txt";

if(file_exists($delfile))
 {
  unlink($delfile);
 }

if(!($myfile=fopen("../../../invoice.txt","w")))
{
  print("file couldnot be opened");
}


$fname = 'invoice.txt';

$totcr=0;
$totdr=0;
$enddt = actdate($enddt);
$enddt = us($enddt);
date_belong($enddt,$account_year);

//$query = "delete from pledger";
//mysqli_query($conn,$query) or die (mysqli_error());

$linecnt=0;
$pgbrk=5;
$mcnt=1;



$intotal = 0;
$incometotal = 0;
$trade_account = trading($rawmaterial,$packingmaterial,$finishedgoods,$enddt,$account_year,$branch,$compcode);
$profit_loss = pandl($trade_account,$enddt,$compcode,$branch,$account_year);
$pl = $profit_loss;






$result=mysqli_query($conn,"select distinct name,address1,address2,city,state from company where compcode = '$compcode' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$compname = $row[0];
$address = $row[1].'&nbsp;'.$row[2].'&nbsp;'.$row[3].'&nbsp;'.$row[4];






?>

</span>
<table  width="914" border="0" align="center" cellpadding="0" cellspacing="0"    style="padding:5px; border:1px solid #666666">

    <tr>
      <td height="80" colspan="4"  align="center" bgcolor="#095F92">
      <div style="padding:10px;"><img src="../../images/hapbco_logo.png" alt="logo" width="82" height="87" style="float:left"/></div>
      <span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#FFFFFF; font-weight:bold;" > <?=$compname?> </span><br />
          <span style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FFFFFF; font-weight:bold;" >ADMIN OFFICE: <?=$address?><br />
</td>
    </tr>

    <tr>
      <td height="30" colspan="4"  align="center" bgcolor="#EAEAEA"><span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#000; font-weight:bold;" >Balance Sheet for the year ended upto  
      <?=british($enddt);?></span></td>
    </tr>
    <tr>
    <tr>
      <td height="30" colspan="4"  align="center"  >
      
      <table  width="100%" border="0" cellpadding="4" rules="cols"  >
              <colgroup>
          </colgroup>
        <colgroup>
          </colgroup>
        <colgroup>
          </colgroup>
        <colgroup>
          </colgroup>          
 

<thead>
  <tr bgcolor="#095F92"> 
      <td  ><div align="center"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FFFFFF; font-weight:bold;" >Liabilities</span></div></td>
        <td  ><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Rs&nbsp;</span></div></td>
    <td><div align="center"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FFFFFF; font-weight:bold;" >Asset</span></div></td>
        <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FFFFFF; font-weight:bold;" >Rs&nbsp;</span></div></td>

  </tr>


  <tr>
  <td><hr></td>
    <td><hr></td>
	  <td><hr></td>
	  <td><hr></td>
  </tr>
  </thead>
  <?

$exptotal = 0;
$x=0;

$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='11' and type ='12' order by sequence ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{
	$chk='T';
	$subchk='';
	$subtotal = 0;
//	$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' order by description ") or die(mysqli_error());
		if($branch=='ALL')
		{	
			$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]'   order by description") or die(mysqli_error());
		}
		else
		{
			$aresult = mysqli_query($conn,"select distinct ledger.acctcode,acctmast.description from acctmast,ledger where acctgroup= '$row[0]' and ledger.acctcode = acctmast.acctcode and ledger.branch_code = '$branch'   union select distinct openbalance.acctcode,acctmast.description from acctmast,openbalance where acctgroup= '$row[0]' and openbalance.acctcode = acctmast.acctcode and openbalance.branch_code = '$branch'  order by description") or die(mysqli_error());
		}
	while($arow = mysqli_fetch_row($aresult))
	{
	

			
	
		if($branch=='ALL')
		{
			$amt=0;	
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0]  and compcode = '$compcode' and account_year = '$account_year' ") or die(mysqli_error());

		}
		else
		{
			$amt=0;			
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] and branch_code = '$branch' and compcode = '$compcode'  and account_year = '$account_year'  ") or die(mysqli_error());
			
		}
			while($oprow = mysqli_fetch_row($opresult))
			{
			//$amt = $oprow[0];
				if($oprow[1] == 'DR')
				{
					$amt = $amt+$oprow[0];
				}
				elseif($oprow[1] == 'CR')
				{
					$amt = $amt+($oprow[0]*-1);
				}	
			}
		
		if($branch=='ALL')
		{
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and compcode = '$compcode'  and account_year = '$account_year'  ") or die(mysqli_error());
		}
		else
		{
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and branch_code = '$branch' and compcode = '$compcode'  and account_year = '$account_year'  ") or die(mysqli_error());		
		}
		$ledrow = mysqli_fetch_row($ledresult);	
		$ledgamt = $ledrow[0];
		$ledtotal = $amt + $ledgamt;
		$exptotal = $exptotal + $ledtotal;

		if($ledtotal != 0)
		{
			if($ledtotal > 0)
			{
				$type = '';
			}
			elseif($ledtotal < 0)
			{
				$type = '(-)';
				$ledtotal = $ledtotal*-1;
			}
			if($chk=='T')
			{	
				$expense_des[$x]=$row[1];
				$expense_amt[$x]=0;
				$expense_type[$x]='GR';
				$x++;
				$chk = 'F';
			}
			if($arow[2] != $subchk)
			{

			 if($arow[2] != '000')
			 {	
				$expense_des[$x]=$srow[0];
				$expense_amt[$x]=0;
				$expense_type[$x]='SB';
				$x++;
				$subchk = $arow[2];
			  }
			}

			$expense_head[$x]=$arow[0];
			$expense_des[$x]=$arow[1];
			$expense_amt[$x]=$ledtotal;
			$expense_type[$x]=$type;
			$x++;
						
		}		
	}	
	
}



// Print Expense Total



	if($pl <0)
	{
			$exptotal = $exptotal +$pl*-1;
			$expense_des[$x]='Profit & Loss A/c for LS ('.$account_year.')';
			$expense_amt[$x]=$pl*-1;
			$expense_type[$x]=$type;
			$x++;	
	}


$m=0;

//  INCOME PART OF STATEMENTS.


	if($pl >0)
	{

			$intotal = $intotal +$pl*-1;
			$income_des[$m]='Profit & Loss A/c for pr ('.$account_year.')';
			$income_amt[$m]=$pl;
			$income_type[$m]='';
			$m++;
	}




$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='11' and type ='13' order by sequence ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{
	$chk='T';
	$subchk ='';
	$subtotal = 0;
	//$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' order by description ") or die(mysqli_error());
	if($branch=='ALL')
	{	
		$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]'   order by description") or die(mysqli_error());
	}
	else
	{
		$aresult = mysqli_query($conn,"select distinct ledger.acctcode,acctmast.description from acctmast,ledger where acctgroup= '$row[0]' and ledger.acctcode = acctmast.acctcode and ledger.branch_code = '$branch'   union select distinct openbalance.acctcode,acctmast.description from acctmast,openbalance where acctgroup= '$row[0]' and openbalance.acctcode = acctmast.acctcode and openbalance.branch_code = '$branch'  order by description") or die(mysqli_error());
	}
	while($arow = mysqli_fetch_row($aresult))
	{
	

		
		if($branch=='ALL')
		{
			$amt=0;	
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0]  and compcode = '$compcode'  and account_year = '$account_year'  ") or die(mysqli_error());
		}
		else
		{
			$amt=0;			
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] and branch_code = '$branch' and compcode = '$compcode'  and account_year = '$account_year'  ") or die(mysqli_error());
		}
		while($oprow = mysqli_fetch_row($opresult))
		{
		//$amt = $oprow[0];
			if($oprow[1] == 'DR')
			{
				$amt = $amt+$oprow[0];
			}
			elseif($oprow[1] == 'CR')
			{
				$amt = $amt+($oprow[0]*-1);
			}	
		}		
		
		if($branch=='ALL')
		{
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and compcode = '$compcode'  and account_year = '$account_year'  ") or die(mysqli_error());
		}
		else
		{
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and branch_code = '$branch' and compcode = '$compcode'  and account_year = '$account_year' ") or die(mysqli_error());		
		}
		$ledrow = mysqli_fetch_row($ledresult);	
		$ledgamt = $ledrow[0];
		$ledtotal = $amt + $ledgamt;
		$subtotal = $subtotal + $ledtotal;
		$intotal = $intotal + $ledtotal;
		

		if($ledtotal != 0)
		{
			if($ledtotal > 0)
			{
				$type = '(-)';

			}
			elseif($ledtotal < 0)
			{
				$type = '';
				$ledtotal = $ledtotal*-1;

			}
			
			if($chk=='T')
			{
				$income_des[$m]=$row[1];
				$income_amt[$m]=0;
				$income_type[$m]='GR';
				$m++;			
				$chk = 'F';
			}
			if($arow[2] != $subchk)
			{
			
			 if($arow[2] != '000')
			 {			
				$income_des[$m]=$srow[0];
				$income_amt[$m]=0;
				$income_type[$m]='SB';
				$m++;			
				$subchk = $arow[2];
			 }
			}			

			$income_head[$m]=$arow[0];
			$income_des[$m]=$arow[1];
			$income_amt[$m]=$ledtotal;
			$income_type[$m]=$type;
			$m++;

		}		
	}	
	
}


	
	
if(count($expense_amt)>count($income_amt))
{
	$max= count($expense_amt);
}
else
{
	$max = count($income_amt);
}
$tasst='';
$tlib='';
for($ind = 0;$ind <=$max;$ind++)
{
?>
  <tr> 
  
     	<?
	if($income_amt[$ind]==0)
	{
		 if($income_type[$ind] == 'GR')
		 {
			?><tr b> 
			<td ><div align="left"><div align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#2222FF; font-weight:bold;" ><b><u><?   echo ucwords(strtolower($income_des[$ind]));?></u></b></span></div></td>
			 <?
		 }
		 elseif($income_type[$ind]=='SB')
		 {
			?>
			<td ><div align="left"><div align="left"><span style="font-family:Arial, Helvetica, sans-serif; color:#974B00; font-size:12px;  font-weight:bold;" >&nbsp;&nbsp;&nbsp;&nbsp;<b><u><?   echo ucwords(strtolower($income_des[$ind]));?></u></b></span></div></td>
			 <?	 
		 }
	 }
	 else
	 {
			if($branch != 'ALL')
			{
				?>
				
				<td ><div align="left"><a onClick="javascript:OpenWindow('../ledgers/ledg_print.php?acctcode[0]=<?echo($income_head[$ind]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&status=tledg&prnt=All')" href ="#"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><?   echo ucwords(strtolower($income_des[$ind]));?></span></div></td>
				<?
			}
			else
			{
				?>
				
				<td ><div align="left"><a onClick="javascript:OpenWindow('../ledgers/allbr_ledg_print.php?acctcode[0]=<?echo($income_head[$ind]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&status=tledg&prnt=All')" href ="#"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><?   echo ucwords(strtolower($income_des[$ind]));?></span></div></td>
				<?				
			}
	  }
	  
	  if($income_des[$ind]=='')
	  {
	  ?>
      <td ><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" >&nbsp;</span></div></td>
      <?
	  }
	  ?>   
      

 	   <td ><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><? if($income_amt[$ind]>0) {echo($income_type[$ind]); echo  number_format($income_amt[$ind],2);}?></span></div></td>

  	<?

	if($expense_amt[$ind]==0)
	{
		if($expense_type[$ind]=='GR')
		{
			?>
			<td ><div align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#2222FF; font-weight:bold;" ><b><u><?  echo ucwords(strtolower($expense_des[$ind]));?></u></b></span></div></td>
			<?		
		}
		elseif($expense_type[$ind] =='SB')
		{
			?>
			<td ><div align="left"><span style="font-family:Arial, Helvetica, sans-serif;color:#974B00; font-size:12px;  font-weight:bold;" >&nbsp;&nbsp;&nbsp;&nbsp;<b><u><?  echo ucwords(strtolower($expense_des[$ind]));?></u></b></span></div></td>
			<?		
		}
	

	 }
	 else
	 {
	 	if($branch != 'ALL')
		{
			?>
			<td ><div align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('../ledgers/ledg_print.php?acctcode[0]=<?echo($expense_head[$ind]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&status=tledg&prnt=All')" href ="#"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><?  echo ucwords(strtolower($expense_des[$ind]));?></span></div></td>
			<?
		}
		else
		{
			?>
			<td ><div align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('../ledgers/allbr_ledg_print.php?acctcode[0]=<?echo($expense_head[$ind]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&status=tledg&prnt=All')" href ="#"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><?  echo ucwords(strtolower($expense_des[$ind]));?></span></div></td>
			<?
			
		}
	  }
	  ?>
    <td ><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><? if($expense_amt[$ind]>0) {  echo($expense_type[$ind]); echo number_format($expense_amt[$ind],2);}?></span></div></td>
    

  </tr>
<?


}



if($exptotal <0)
{
	$exptotal = $exptotal * -1;

}
	
if($intotal <0)
{
	$intotal = $intotal*-1;
}
	
?>
	<tr  bgcolor="#095F92"> 

	<td  align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FFFFFF; font-weight:bold;" >Grant Total </span></td>

		<td align=right><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" ><b><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" >
        <?=number_format($intotal,2)?>
		</b></span></td>

 	<td  align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Grant Total </span></td>	     
		<td align=right><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FFFFFF; font-weight:bold;" ><b><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" >
        <?=number_format($exptotal,2)?>
		</b></span></td>

	     

</tr>
	</tbody>
</table>

<?




function pandl($trade_account,$enddt,$compcode,$branch,$account_year)
{
    global $conn;
	$expensetotal = 0;
	$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='22' and type ='15' ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	{
		//$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' ") or die(mysqli_error());
		if($branch=='ALL')
		{	
			$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]'   order by description") or die(mysqli_error());
		}
		else
		{
			$aresult = mysqli_query($conn,"select distinct ledger.acctcode,acctmast.description from acctmast,ledger where acctgroup= '$row[0]' and ledger.acctcode = acctmast.acctcode and ledger.branch_code = '$branch'   union select distinct openbalance.acctcode,acctmast.description from acctmast,openbalance where acctgroup= '$row[0]' and openbalance.acctcode = acctmast.acctcode and openbalance.branch_code = '$branch'  order by description") or die(mysqli_error());
		}
		while($arow = mysqli_fetch_row($aresult))
		{
	
		if($branch=='ALL')
		{
			$amt=0;	
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0]  and compcode = '$compcode'  and account_year = '$account_year'  ") or die(mysqli_error());
		}
		else
		{
			$amt=0;
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] and branch_code = '$branch' and compcode = '$compcode'  and account_year = '$account_year'  ") or die(mysqli_error());
		}
			$oprow = mysqli_fetch_row($opresult);
			$amt = $oprow[0];
			if($oprow[1] == 'DR')
			{
				$amt = $oprow[0];
			}
			elseif($oprow[1] == 'CR')
			{
				$amt = $oprow[0]*-1;
			}	
		
		
		if($branch=='ALL')
		{
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and compcode = '$compcode' and account_year = '$account_year' ") or die(mysqli_error());
		}
		else
		{
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and branch_code = '$branch' and compcode = '$compcode' and account_year = '$account_year' ") or die(mysqli_error());		
		}
		$ledrow = mysqli_fetch_row($ledresult);	
			$ledgamt = $ledrow[0];
			$ledtotal = $amt + $ledgamt;
			$expensetotal = $expensetotal + $ledtotal;

			
		}	
	}

	//  INCOME PART OF STATEMENTS.
	
	$incometotal = $trade_account;
	
	$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='22' and type ='14' ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	{
		//$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' ") or die(mysqli_error());
		if($branch=='ALL')
		{	
			$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]'   order by description") or die(mysqli_error());
		}
		else
		{
			$aresult = mysqli_query($conn,"select distinct ledger.acctcode,acctmast.description from acctmast,ledger where acctgroup= '$row[0]' and ledger.acctcode = acctmast.acctcode and ledger.branch_code = '$branch'   union select distinct openbalance.acctcode,acctmast.description from acctmast,openbalance where acctgroup= '$row[0]' and openbalance.acctcode = acctmast.acctcode and openbalance.branch_code = '$branch'  order by description") or die(mysqli_error());
		}
		while($arow = mysqli_fetch_row($aresult))
		{
	
		if($branch=='ALL')
		{
			$amt=0;	
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0]  and compcode = '$compcode' and account_year = '$account_year' ") or die(mysqli_error());

		}
		else
		{
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] and branch_code = '$branch' and compcode = '$compcode' and account_year = '$account_year' ") or die(mysqli_error());
		}
			$oprow = mysqli_fetch_row($opresult);
			$amt = $oprow[0];
			if($oprow[1] == 'DR')
			{
				$amt = $oprow[0];
			}
			elseif($oprow[1] == 'CR')
			{
				$amt = $oprow[0]*-1;
			}	
		
		
		if($branch=='ALL')
		{
			$ledresult = mysqli_query($conn,"select sum(cramt)-sum(dbamt) from ledger where acctcode = $arow[0] and date <= '$enddt' and compcode = '$compcode' and account_year = '$account_year' ") or die(mysqli_error());
		}
		else
		{
			$ledresult = mysqli_query($conn,"select sum(cramt)-sum(dbamt) from ledger where acctcode = $arow[0] and date <= '$enddt' and branch_code = '$branch' and compcode = '$compcode'  and account_year = '$account_year'") or die(mysqli_error());		
		}
			$ledrow = mysqli_fetch_row($ledresult);	
			$ledgamt = $ledrow[0];
			$ledtotal = $amt + $ledgamt;
			$incometotal = $incometotal + $ledtotal;
		}	
	}
	

	$netprofit = $incometotal - $expensetotal;
	return $netprofit;
}


function trading($rawmaterial,$packingmaterial,$finishedgoods,$enddt,$account_year,$branch,$compcode)
{
    global $conn;
	$expensetotal = 0;
	$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='33' and type ='15' ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	{
		$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' order by description ") or die(mysqli_error());
		while($arow = mysqli_fetch_row($aresult))
		{
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] and branch_code = '$branch' and compcode = '$compcode'   and account_year = '$account_year' ") or die(mysqli_error());
			$oprow = mysqli_fetch_row($opresult);
			$amt = $oprow[0];
			if($oprow[1] == 'DR')
			{
				$amt = $oprow[0];
			}
			elseif($oprow[1] == 'CR')
			{
				$amt = $oprow[0]*-1;
			}	
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and account_year = '$account_year'  and branch_code = '$branch' and compcode = '$compcode'") or die(mysqli_error());
			$ledrow = mysqli_fetch_row($ledresult);	
			$ledgamt = $ledrow[0];
			$ledtotal = $amt + $ledgamt;
			$expensetotal = $expensetotal + $ledtotal;
	
		}	
	}
	//  INCOME PART OF STATEMENTS.
	
	$incometotal = 0;
	$closingstock = (float)$rawmaterial+(float)$packingmaterial+(float)$finishedgoods;
	
	if($closingstock>0)
	{
		$incometotal = $closingstock;
	}
	$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='33' and type ='14' ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	{
		$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' ") or die(mysqli_error());
		while($arow = mysqli_fetch_row($aresult))
		{
		
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] and branch_code = '$branch' and compcode = '$compcode'   and account_year = '$account_year' ") or die(mysqli_error());
			$oprow = mysqli_fetch_row($opresult);
			$amt = $oprow[0];
			if($oprow[1] == 'DR')
			{
				$amt = $oprow[0];
			}
			elseif($oprow[1] == 'CR')
			{
				$amt = $oprow[0]*-1;
			}
	
			$ledresult = mysqli_query($conn,"select sum(cramt)-sum(dbamt) from ledger where acctcode = $arow[0] and date <= '$enddt' and account_year = '$account_year' and branch_code = '$branch' and compcode = '$compcode' ") or die(mysqli_error());
			$ledrow = mysqli_fetch_row($ledresult);	
			$ledgamt = $ledrow[0];
			$ledtotal = $amt + $ledgamt;
			$incometotal = $incometotal + $ledtotal;
		}	
	}

	$netprofit = $incometotal - $expensetotal;
	return $netprofit;
}



?>