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

$result=mysqli_query($conn,"select distinct name from company where compcode = '$compcode' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$compname = $row[0];

?>



<title>Ledger Printing</title>
<form>
<body bgcolor="#ffffff"  topmargin=0>

<font face="Arial, Helvetica, sans-serif">
<?


$stdt = $startdt;
$startdt = us($startdt);
$edt = $enddt;
$enddt = us($enddt);


$linecnt=0;
$pgbrk=5;
$mcnt=1;
?>
</font>
<TABLE  width="706" BORDER=1 CELLPADDING=4 RULES=COLS  >

<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>

				
<thead>
<tr>
<td height="34" colspan="100%" ALIGN ="left"><h4><font face="Arial, Helvetica, sans-serif"><?echo($compname);?></font></h4></td></tr>
<tr>
      <td colspan="100%"><font face="Arial, Helvetica, sans-serif">Grupwise 
        Summary From <? echo($stdt);?> To <?echo($edt);?></font></td>
    </tr>
<tr bgcolor="#DAF1A9">

<td width="468" align=center><font face="Arial, Helvetica, sans-serif">Particulars</font></td>

<td width="210" align=right><div align="right"><font face="Arial, Helvetica, sans-serif">Closing</font></div></td>
</tr>
<tr>
<td><hr></td>



<td><hr></td>
</tr>

</thead>
<tbody>
<?

for ($grpind=0;$grpind < count($acctgrp);$grpind++)
{

		$gresult = mysqli_query($conn,"select groupname from acctgroup where acctcode = '$acctgrp[$grpind]'") or die(mysqli_error());
		$grow = mysqli_fetch_row($gresult);
		$gname = $grow[0];
?>
			<tr>
			<td align=left><font face="Arial, Helvetica, sans-serif" color="#CC0000"><b><u><?echo($gname);?></u></b></font></td></tr>

<?
		$nettotal=0;
	$acresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$acctgrp[$grpind]' order by description") or die(mysqli_error());
	while($acrow = mysqli_fetch_row($acresult))
	{
		$acctcode = $acrow[0];
		$description=$acrow[1];
	
	
		$tdbamt = 0;
		$tcramt = 0;
		$opening=0;
		$opbal=0;
		$clb=0;
		$amt=0;

		$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance  where acctcode = '$acctcode' and account_year = '$account_year' ") or die(mysqli_error());
		$oprow = mysqli_fetch_row($opresult);
		$amt = $oprow[0];
		
	
		if($oprow[1] =='DR')
		{
			$opbal = $amt;
		}
		elseif($oprow[1] == 'CR')
		{

			$opbal = $amt*-1;
		}
		
		$result = mysqli_query($conn,"select sum(cramt), sum(dbamt) from ledger where compcode = '$compcode' && acctcode = '$acctcode' and date < '$startdt' and account_year = '$account_year' order by date ");
		$row = mysqli_fetch_row($result);
		$opb = $opbal +($row[1]-$row[0]);
		
				
		$description = substr($description,0,45);
		$result = mysqli_query($conn,"select sum(cramt),sum(dbamt) from ledger where compcode = '$compcode' && acctcode = '$acctcode'  and date >='$startdt' and date <= '$enddt' order by date ");
		$row = mysqli_fetch_row($result);
		$cramt = $row[0];
		$dbamt = $row[1];
		$clb = $opb + $dbamt - $cramt;		
		if($clb!=0)
		{
			
			if($opb >0)
			{
				$opening = $opb;
				$type = 'DR';
			}
			elseif($opb < 0)
			{
				$opening = $opb*-1;
				$type = 'CR';
			}
			
			$nettotal = $nettotal + $clb;
			if($clb >0)
			{
				$closing = $clb;
				$ctype = 'DR';
			}
			elseif($clb < 0)
			{
				$closing = $clb*-1;
				$ctype = 'CR';
			}			
	

			?>
			
			<tr>
			<td align=left><font face="Arial, Helvetica, sans-serif"><?echo($description);?></font></td>
			

			<td align=right><font face="Arial, Helvetica, sans-serif">
		    <? echo(number_format($closing,2).'&nbsp;'.$ctype);?>
			</font></td>
			</tr>
  </tbody>
			<?

			$opening=0;
			$closing=0;			
			$opening=0;
		} // end of closing balance check loop.
	} // accthead For loop end
			if($nettotal >0)
			{
				$cls = $nettotal;
				$ntype = 'DR';
			}
			elseif($nettotal < 0)
			{
				$cls = $nettotal*-1;
				$ntype = 'CR';
			}		
		
		?>
			<tr>
			<td>&nbsp;</td>						
			<td align=right><font face="Arial, Helvetica, sans-serif" color="#CC0000"><b>  <?echo(number_format($cls,2).'&nbsp;'.$ntype);?></b></font></td></tr>
					
		<?
		
		

	$cls=0;
	
}

?>		

</table>

