<?php include("../include/dbconnect.php");?>
<title>Trial Balance  Print for Customer</title>
<body bgcolor="#ffffff"  topmargin=0>
<?

$todate = $startdt;
$temp = $startdt;
$startdt = substr($temp,6,4).substr($temp,3,2).substr($temp,0,2);
$edt =$enddt;
$temp = $enddt;
$enddt = substr($temp,6,4).substr($temp,3,2).substr($temp,0,2);

ledgtit($branch,$todate,$edt);
$areatotal=0;
$areadebit=0;
$areacredit=0;
$grtotal=0;

$x=0;
$result1=mysql_query("select distinct bcode from buy order by buy.areacode,buy.name") or die(mysql_error());
while($row1=mysql_fetch_row($result1))
{
	$buye[$x]=$row1[0];
	$x++;
}
$debit=0;
$credit=0;
$x=0;
for($ind=0;$ind<count($buye);$ind++)
{
	$by=substr($buye[$ind],0,4);

	$result = mysql_query("select sum(dbamt)-sum(cramt) from custledger where bcode= '$buye[$ind]' and compcode='$compcode'  ") or die(mysql_error());
	$row = mysql_fetch_row($result);
	$buyamt=$row[0];

	$netamt=$selamt - $buyamt;
	if($netamt !=0)
	 {
	 	$buyers[$x]=$buye[$ind];
		$x++;
	}
}


for($ind=0;$ind<count($buyers);$ind++)
{
	$by=substr($buyers[$ind],0,4);

;

	$result = mysql_query("select  sum(dbamt)-sum(cramt) from custledger where bcode= '$buyers[$ind]' and compcode='$compcode'") or die(mysql_error());
	$row = mysql_fetch_row($result);
	$buyamt=$row[0];

	$netamt= $selamt - $buyamt;
	
	
	$bresult=mysql_query("select buy.name,area.location,buy.areacode from buy,area where buy.bcode = '$buyers[$ind]' and buy.areacode=area.areacode");
	$brow=mysql_fetch_row($bresult);
	$bname = $brow[0];
	$area=$brow[1];
	if($netamt !=0)
	{
		$areatotal=$areatotal+$netamt;
		if($netamt >0)
		{
			$debit=$debit+$netamt;
			$areadebit=$areadebit+$netamt;
		}
		if($netamt < 0)
		{
			$credit=$credit+$netamt;
			$areacredit=$areacredit+$netamt;
		}
		
		$grtotal=$grtotal+$netamt;
		
		?>
		
		<table width=700>
		<td width=50 align=center><? echo($ind+1);echo(")");?></td>
		<td width=350 align=left><? echo($bname .'( '.$area.' )');?></td>
		<td align=right width=100><b><? if($netamt>0) echo(number_format($netamt,2,'.',''));?></b> </td>
		<td align=right width=100><b><? if($netamt<0) echo(number_format($netamt*-1,2,'.',''));?></b> </td>
		</table>
		<?
 			

		if(substr($buyers[$ind],0,4)!= substr($buyers[$ind+1],0,4))
		{
		
			?>
			<table width=855>
			<td width=393 align=right><font color="#0000FF"><? echo($area.'  Total');?></td>
			
			<td align=right width=118><b><font color="#0000FF"><?  echo(number_format($areadebit,2,'.',''));?></b> </td>
			<td align=right width=101><font color="#0000FF"><b><? echo(number_format($areacredit*-1,2,'.',''));?></b> </td>
			<?
			if($areatotal>=0)
			{
				?>
				<td align=right width=135><font color="#990099"><b><? echo(number_format($areatotal,2,'.',''));?></b>&nbsp;CR </td>
				<?
			}
			elseif($areatotal <0)
			{
				?>
				
				<td align=right width=135><font color="#990099"><b><? echo(number_format($areatotal*-1,2,'.',''));?></b>&nbsp;DB</td>
				<?
			}
				?>
			</table>
			<?
			$areatotal=0;
			$areadebit=0;
			$areacredit=0;
		}			
	}
}
 


?></hr>
<table width=700>
<td width=400 align=right><font  color="#990000"><? echo(' Grand Total');?></td>
<td align=right width=100><b><font color="#990000"><? echo(number_format($debit,2,'.',''));?></b> </td>
<td align=right width=100><font color="#990000"><b><? echo(number_format($credit*-1,2,'.',''));?></b> </td>
</table>



<hr align = left width = 750 ></hr>
<table width=700>
<td width=400 align=right><font color="#990000"><? echo('');?></td>
<td align=right width=100><b><font color="#990000"><? if($grtotal>0) echo(number_format($grtotal,2,'.',''));?></b> </td>
<td align=right width=100><font color="#990000"><b><? if($grtotal<0) echo(number_format($grtotal*-1,2,'.',''));?></b> </td>
</table>

<hr align = left width = 750 ></hr>

<br>


<?
function ledgtit($branch,$todate,$edt)
 {
?>


<table>
<table width=400 border =0>
<td align=left><b> Trial Balance as on date &nbsp;&nbsp;&nbsp;</b></td></table>

<hr align = left width = 750 ></hr>
<table width=700>
<td width=400 align=center>Customer Name</td>
<td align=right width=100><b>Received</b> </td>
<td align=right width=100><b>Due from Party</b> </td>
</table>

<hr align = left width = 750 ></hr>
<?
}
?>

<br><br><font size=1><center><a href="index.html">Return to Index page</center></font>
