<?php
include('../include/config.php');
$gr=$_POST['claz'];
$yrr=$_POST['yr'];
$trm=$_POST['trm'];

if($trm=='Term One')
{
    $trmm='Term Three';
    $yrrr=(int)$yrr;
    $yr2=$yrrr-1;
}else if($trm=='Term Two')
{
    $trmm='Term One';
    $yr2=$yrr;
}else if($trm=='Term Three')
{
    $trmm='Term Two';
    $yr2=$yrr;
}


$sqll = "select * FROM Fees2 WHERE Grad='$gr' AND Term='$trm' AND Year='$yrr'";
$resultt = $con->query($sqll);
if ($resultt->num_rows > 0) 
{
$sql = "select Fees2.*,pupils.Names as pnames,pupils.Contact as conta,pupils.AdmNo as adm,pupils.Claz as clas, (SELECT SUM(PaidAmount) FROM feestatement where pid=Fees2.pid 
AND Year='$yrr' AND Term='$trm') as paid, (select Amount from Balances where Year='$yr2' AND Term='$trmm' AND SID=pupils.count) as cfb from Fees2 left JOIN pupils ON pupils.count=Fees2.pid
left join Balances ON Balances.SID=pupils.count LEFT JOIN feestatement ON Fees2.pid=feestatement.pid WHERE Fees2.Grad='$gr'
AND Fees2.Term='$trm' AND Fees2.Year='$yrr' GROUP BY pupils.count";
$result = $con->query($sql);
$cnt='1';
while($row = $result->fetch_assoc()) 
{
$tu=$row["TutionF"];    
$ad=$row["AdmF"];   
$bo=$row["BoF"];  
$tr=$row["TranF"];  
$un=$row["UniF"];  
$moti=$row["MotF"];
$pef=$row["PEF"];
$txt=$row["TxtF"];
$tab=$row["Tablet"];
$dev=$row["Dev"];

$tu= (int)$tu;
$ad= (int)$ad;
$bo= (int)$bo;
$tr= (int)$tr;
$un= (int)$un;
$moti= (int)$moti;
$pef= (int)$pef;
$txt= (int)$txt;
$tab= (int)$tab;
$dev= (int)$dev;

$tot=$tu+$bo+$tr+$un+$ad+$moti+$pef+$txt+$tab+$dev;
//$tot=$tu+$bo+$tr+$un+$act+$bf+$moti+$pe+$lt+$ewc+$rem+$ad;
?>
<tr class="row-selectbala">
<td><?php echo $cnt; ?></td>
<td><?php echo $row["pnames"]; ?></td>
<?php
$tel=$row["conta"];
$one=substr($tel, 0, 1);
if($one !='0')
{
 $tel='0'.$tel; 
}
$tel=substr($tel, 0, 10);

$bal=$tot-$row["paid"];
$ba=$bal+$row["cfb"];
?>

<td><?php echo $tel; ?></td>
<td><?php echo $row["adm"]; ?></td>
<td><?php echo $row["clas"]; ?></td>
<td class="alert-warning"><strong> <?php echo number_format($tot); ?></strong></td>
<td class="alert-success"><strong><?php echo number_format($row["paid"]); ?></strong></td>
<td class="bg-warning"><strong><?php echo $bal; ?></strong></td>
<td class="bg-secondary"><strong><?php echo $row["cfb"]; ?></strong></td>
<?php 
 if($ba<0)
{
 ?>
 <td class=""> <?php echo number_format($ba); ?></td>
 <?php
}else if($ba=='0')
{
?>
 <td class="bg-secondary"> <?php echo number_format($ba); ?> </td>
<?php
}else
{
?>
 <td class="alert-danger">  <?php echo number_format($ba); ?> </td>
<?php
}
?>
<td>
<input type="hidden" class="contacttt" value="<?php echo $tel; ?>" />
<input type="hidden" class="pupilid" value="<?php echo $row["pid"]; ?>" />
<?php
$phn=$row["conta"];
if($phn=='')
{
}else
{
?>
<input type="checkbox" name='checkbox[$rowid]' value="<?php echo $row["pid"]; ?>">
<?php
}
?>
</td>
</tr>
<?php
$cnt=$cnt+1;
}
}else
{
?>
<tr>
    <td colspan="8"> No records found !  </td>
</tr>
<?php
}
?>