<?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>Area wise outstanding</title>
<form>
<body bgcolor="#ffffff"  topmargin=0>
<font face="Arial, Helvetica, sans-serif">
<?

$startdt = us($stdt);
$enddt = us($edt);
?>
</font>
<TABLE  width="780" 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"><font face="Arial, Helvetica, sans-serif"><?echo($compname);?></font></h4></td></tr>
<tr>
      <td colspan="100%"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Sales and Receipts From <? echo($stdt);?> To <?echo($edt);?></font></td>
    </tr>
<tr bgcolor="#DAF1A9">

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



<td width="79" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Sales</font></div></td>
<td width="91" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Receipts</font></div></td>

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


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

</tr>

</thead>
<tbody>
<?
if($reporttype =='Detailed')
{
	for ($grpind=0;$grpind < count($area);$grpind++)
	{
			$gresult = mysqli_query($conn,"select city from buy where city = '$area[$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 distinct bcode,name from buy where city = '$area[$grpind]' and buyertype ='$btype' order by name") 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;
	
	/*
			$result = mysqli_query($conn,"select sum(cramt), sum(dbamt) from ledger where compcode = '$compcode' && acctcode = '$acctcode' and date < '$startdt' order by date ");
			$row = mysqli_fetch_row($result);
			$opb = $opbal +($row[1]-$row[0]);
		*/	
			$msresult = mysqli_query($conn,"select sum(cramt+dbamt) from ledger where compcode = '$compcode' && acctcode = '$acctcode'    and date >='$startdt' and date <= '$enddt' order by date ");
			$msrow = mysqli_fetch_row($msresult);
			$chck_amt = $msrow[0];
			if($chck_amt>0)
			{				
				$description = substr($description,0,45);
				$result = mysqli_query($conn,"select sum(dbamt) from ledger where compcode = '$compcode' && acctcode = '$acctcode'  and date >='$startdt' and date <= '$enddt'  and taxtype = 'Sales' order by date ");
				$row = mysqli_fetch_row($result);
				$dbamt = $row[0];
				

				$result = mysqli_query($conn,"select sum(cramt) from ledger where compcode = '$compcode' && acctcode = '$acctcode'  and date >='$startdt' and date <= '$enddt' and taxtype = 'Receipts' order by date ");
				$row = mysqli_fetch_row($result);
				$cramt = $row[0];

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

				<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
				<? if($dbamt!=0) echo $dbamt;?> 
				</font></td>
				<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
				<? if($cramt!=0) echo $cramt;?>
				</font></td>

				</tr>
				</tbody>
				<?
				$dbamt=0;
				$cramt=0;
			}  // end of check amount if loop
			
		} // accthead For loop end
		
			
			?>
				<tr>
				<td>&nbsp;</td>

						
				<td align=right><font face="Arial, Helvetica, sans-serif" color="#CC0000"><b>  <? echo(number_format($dbtotal,2).'&nbsp;'.$ntype);?></b></font></td>

<td align=right><font face="Arial, Helvetica, sans-serif" color="#CC0000"><b>  <? echo(number_format($crtotal,2).'&nbsp;'.$ntype);?></b></font></td>
                
                </tr>
					
			<?
			$dbtotal=0;	
			$crtotal =0;
	
	}
}
elseif($reporttype == 'Summarised')
{
	
	
	
	
	
	
	
	
$netdebit =0;
$netcredit=0;
	
	for ($grpind=0;$grpind < count($area);$grpind++)
	{
			$gresult = mysqli_query($conn,"select city from buy where city = '$area[$grpind]'") or die(mysqli_error());
			$grow = mysqli_fetch_row($gresult);
			$gname = $grow[0];

	
	
			$tdbamt = 0;
			$tcramt = 0;
			$opening=0;
			$opbal=0;
			$clb=0;
			$amt=0;		
		$acresult = mysqli_query($conn,"select distinct bcode,name from buy where city = '$area[$grpind]' order by name") or die(mysqli_error());
		while($acrow = mysqli_fetch_row($acresult))
		{
			$acctcode = $acrow[0];
			$description=$acrow[1];

				$result = mysqli_query($conn,"select sum(dbamt) from ledger where compcode = '$compcode' && acctcode = '$acctcode'  and date >='$startdt' and date <= '$enddt'  and taxtype = 'Sales' order by date ");
				$row = mysqli_fetch_row($result);
				$dbamt = $row[0];
				

				$result = mysqli_query($conn,"select sum(cramt) from ledger where compcode = '$compcode' && acctcode = '$acctcode'  and date >='$startdt' and date <= '$enddt' and taxtype = 'Receipts' order by date ");
				$row = mysqli_fetch_row($result);
				$cramt = $row[0];
				
			$tdbamt = $tdbamt+$dbamt;
			$tcramt = $tcramt+$cramt;
			$netdebit = $netdebit+$dbamt;
			$netcredit = $netcredit+$cramt;
		}

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

				<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
				<? if($tdbamt!=0) echo $tdbamt;?> 
				</font></td>
				<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
				<? if($tcramt!=0) echo $tcramt;?>
				</font></td>

				</tr>
				</tbody>
				<?


			
		} // accthead For loop end
		
			
			?>
				<tr>
				<td>&nbsp;</td>

					
				<td align=right><font face="Arial, Helvetica, sans-serif" color="#CC0000"><b>  <? echo(number_format($netdebit,2));?></b></font></td>
				<td align=right><font face="Arial, Helvetica, sans-serif" color="#CC0000"><b>  <? echo(number_format($netcredit,2));?></b></font></td>                
                </tr>
					
			<?
			$cls=0;	
	
	
}
?>		
</table>

