<?php 
include('../include/config.php');
$schfee='';
$paid=0;
if(!empty($_POST["id"])) 
{
    $id= $_POST["id"];
    $yr2= $_POST["yr"];
    $gr= $_POST["gr"];
    $trm2= $_POST["trm"];
    
    if($trm2=='Term One')
    {
        $trm='Term Three';
        $yrr=(int)$yr2;
        $yr=$yrr-1;
        
    }else if($trm2=='Term Two')
    {
        $trm='Term One';
        $yr=$yr2;
    }else if($trm2=='Term Three')
    {
        $trm='Term Two';
        $yr=$yr2;
    }
    
    
    
     $sqll = mysqli_query($con,"SELECT * FROM Fees2 where pid='$id' AND Term ='$trm' AND Year='$yr'");
    $num=mysqli_fetch_array($sqll);
    if($num>0)
    {
      $query2=mysqli_query($con,"SELECT * FROM Fees2 WHERE Year='$yr' AND Term='$trm' AND pid='$id'");
        while($row=mysqli_fetch_array($query2))
        {
        $tu=$row['TutionF'];
        $bo=$row['BoF'];
        $adm=$row['AdmF'];
        $tr=$row['TranF'];
        $moti=$row['MotF'];
        $un=$row['UniF'];
        $di=$row['DiariesF'];
        $bk=$row['TxtF'];
        $pef=$row['PEF'];
        $tab=$row['Tablet'];
        $dev=$row['Dev'];
            
        if($tr==''){$tr='0';}
        if($un==''){$un='0';}
        if($moti==''){$moti='0';}
        if($pef==''){$pef='0';}
        if($di==''){$di='0';}
        if($bk==''){$bk='0';}
        if($bo==''){$bo='0';}
        if($adm==''){$adm='0';}
        if($tab==''){$tab='0';}
        if($dev==''){$dev='0';}
        
        $schfee=$tu+$bo+$tr+$un+$di+$bk+$moti+$pef+$adm+$tab+$dev; ; 
        } 
        
    }else
    {
        $schfee='0.0';
    }
   
    //Previous paid
    $sql = mysqli_query($con,"SELECT * FROM feestatement where pid='$id' AND Term ='$trm' AND Year='$yr'");
    $num=mysqli_fetch_array($sql);
    if($num>0)
    {
    $result = mysqli_query($con,"SELECT sum(PaidAmount) FROM feestatement where pid='$id' AND Term ='$trm' AND Year='$yr'");
    while($row = $result->fetch_assoc()) 
    {
    $paid=$row['sum(PaidAmount)'];
    }
    }else
    {
        $paid='0.0';
    }
   
    $bal=$schfee-$paid;
    echo $bal;
    }
?>