<?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];

mysqli_query($conn,"truncate incentive");
mysqli_query($conn,"truncate incentive2");

?>
<title>Age wise outstanding</title>
<form>
<body bgcolor="#ffffff"  topmargin=0>
<font face="Arial, Helvetica, sans-serif">
<?

$startdt = us($stdt);
$enddt = us($edt);
?>
</font>
<TABLE  width="619" 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">Age wise 
        outstanding From <? echo($stdt);?> To <?echo($edt);?></font></td>
    </tr>
<tr>

<td width="72" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Sl No</font></div></td>


<td width="73" align=center><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Date of Invoice</font></td>

<td width="119" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Inv No </font></div></td>

<td width="149" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Amount </font></div></td>
<td width="142" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">No of Days</font></div></td>

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


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

</thead>
<tbody>
<?

	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 colspan="100%"><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 ='Buyer' order by name") or die(mysqli_error());
		while($acrow = mysqli_fetch_row($acresult))
		{
			$acctcode = $acrow[0];
			$description=$acrow[1];
			$bname = $acrow[1];
			$tdbamt = 0;
					
			$result = mysqli_query($conn,"select sum(cramt),sum(dbamt) from ledger where compcode = '$compcode' && acctcode = '$acctcode' and date <= '$enddt' order by date ");
			
			$row = mysqli_fetch_row($result);
			$credit_amount = $row[0];			
			if($row[1] - $row[0] >0)
			{
		?>			
										<tr>
			<td align=left colspan="100%"><font face="Arial, Helvetica, sans-serif"><b><?=$description?></b></font></td></tr>
		
		<?
		
					$sl=1;
					$above180=0;
					$above90=0;
					$above60 =0;
					$above30 = 0;
					$below30 =0;
					
					$msresult = mysqli_query($conn,"select  invno,date,dbamt from ledger where compcode = '$compcode' && acctcode = '$acctcode' and dbamt >0 and date <='$enddt'  order by date ");
					while($msrow = mysqli_fetch_row($msresult))
					{
						$invno = $msrow[0];
						$date = $msrow[1];
						$dbamt = $msrow[2];
						$credit_amount = $credit_amount - $dbamt;	
		//echo("$credit_amount = $credit_amount - $dbamt<br>");
						$days = datediff($enddt,$date);
						if($credit_amount <= 0)
						{
							  $dbamt = $credit_amount*-1;
							  $credit_amount =0;
							  
							  if($days >=180 )
							  {
							  ?>
						  <tr bgcolor="#F80307">
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$sl?></font></td>		
									  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=british($date)?> 
							  </font></td>
									  
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$invno?></font></td>
			  
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$dbamt?>
							  </font></td>
		
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$days?>
							  </font></td>                
							  </tr>
							  <?
	mysqli_query($conn,"insert into incentive (invno,invdate,bcode,amount,area,name) values('$invno','$date',
'$bcode','$dbamt','$gname','$bname')") or die(mysqli_error());							  
							  $above180 = $above180+$dbamt;
							  }

							  if($days >=90 and $days < 180)
							  {
							  ?>
						  <tr bgcolor="#EB5C5E">
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$sl?></font></td>		
									  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=british($date)?> 
							  </font></td>
									  
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$invno?></font></td>
			  
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$dbamt?>
							  </font></td>
		
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$days?>
							  </font></td>                
							  </tr>
							  <?
	mysqli_query($conn,"insert into incentive (invno,invdate,bcode,amount,area,name) values('$invno','$date',
'$bcode','$dbamt','$gname','$bname')") or die(mysqli_error());								  
							  $above90 = $above90+$dbamt;
							  }


							  
							  if($days >=60 and $days < 90)
							  {
							  ?>
						  <tr bgcolor="#D5862C">
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$sl?></font></td>		
									  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=british($date)?> 
							  </font></td>
									  
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$invno?></font></td>
			  
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$dbamt?>
							  </font></td>
		
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$days?>
							  </font></td>                
							  </tr>
							  <?
							  $above60 =$above60+$dbamt;
	mysqli_query($conn,"insert into incentive (invno,invdate,bcode,amount,area,name) values('$invno','$date',
'$bcode','$dbamt','$gname','$bname')") or die(mysqli_error());								  
							  }
							  if($days >=30 and $days < 60)
							  {
							  ?>
						  <tr bgcolor="#BBB12C">
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$sl?></font></td>		
									  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=british($date)?> 
							  </font></td>
									  
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$invno?></font></td>
			  
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$dbamt?>
							  </font></td>
		
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$days?>
							  </font></td>                
							  </tr>
							  <?
	mysqli_query($conn,"insert into incentive (invno,invdate,bcode,amount,area,name) values('$invno','$date',
'$bcode','$dbamt','$gname','$bname')") or die(mysqli_error());								  
							  $above30 =$above30+$dbamt;
							  }				
			  
							  if($days < 30)
							  {
							  ?>
						  <tr bgcolor="#819A14">
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$sl?></font></td>		
									  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=british($date)?> 
							  </font></td>
									  
						<td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?=$invno?></font></td>
			  
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$dbamt?>
							  </font></td>
		
							  <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
							  <?=$days?>
							  </font></td>                
							  </tr>
							  <?
	mysqli_query($conn,"insert into incentive (invno,invdate,bcode,amount,area,name) values('$invno','$date',
'$bcode','$dbamt','$gname','$bname')") or die(mysqli_error());								  
							  $below30 =$below30+$dbamt;
							  }		
						} // If loop 
						$sl++;
					}
		
				
		
					
					?>
									<tr bgcolor="#CFC6EC">
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000"> Above 180 Days</font></td>
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000"> Above 90 Days</font></td>
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000">60 to 90 Days</b></font></td>
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000">30 t0 60 Days</b></font></td>	
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000"> Below 30 Days </b></font></td></tr>	
						<tr bgcolor="#CFC6EC">
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000">  Rs.<?=$above180?></font></td>
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000">  Rs.<?=$above90?></font></td>
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000"> Rs.<?=$above60?></b></font></td>
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000"> Rs.<?=$above30?></b></font></td>	
						<td><font face="Arial, Helvetica, sans-serif" color="#CC0000">Rs.<?=$below30?></b></font></td></tr>						
						<tr><td colspan="100%" align="right"><font face="Arial, Helvetica, sans-serif" color="#CC0000">Total Rs.<?= $above180+$above90+$above60+$above30+ $below30?></b></font></td></tr>	
							
					<?
		
			}
		} // If dbamt - cramt >0
	}

?>		
</table>

