<table class="table table-bordered table-sm table-hover" id="tabletable" >
                     <thead>
                         <tr class="bg-info">
                             <th colspan="14" style="font-size:16px">School Fees for: <strong class=""> 
                                 <?php echo $_SESSION['cll'].' >>'.$_SESSION['tmm'].' >>'.$_SESSION['yrr'];  ?></strong>
                             </th>
                         </tr>
                         <tr>
                             <th>#</th>
                             <th>Pupils Name </th>
                             <th>Contact</th>
                             <th>Adm No.</th>
                             <th>Class</th>
                             <th class="alert-warning">Previous Bal</th>
                             <th class="alert-success">Term Fees </th>
                             <th class="alert-success">Dev</th>
                             <th class="alert-success">Trans</th>
                             <th class="alert-success">Uniform</th>
                             <th class="alert-success">Others</th>
                              <th title="Fees Include Carry Forward Balances" class="bg-info">Total Fee (KES)</th>
                             <th class="alert-success">Total Paid</th>
                             <th class="bg-danger">Balance</th>
                         </tr>
                     </thead>
                     <tbody>
                    <?php
                    $gr=$_SESSION['cll'];
                    $trm=$_SESSION['tmm'];
                    $yrr=$_SESSION['yrr']; 
                 
                    if($trm !='' || $yrr !='')
                    {
                    $sqlll=mysqli_query($con,"DELETE FROM feebaa"); 
               
                    if($gr==''){$gr='0';}
                    if($trm==''){$trm='0';}
                    if($yrr==''){$yrr='0';}   
                    
                    $sql = "select Fees2.*,pupils.Names as pnames,pupils.Contact as conta,pupils.AdmNo as adm,pupils.Claz as clas,pupils.count, (SELECT SUM(PaidAmount) FROM feestatement where pid=Fees2.pid AND Year='$yrr' AND Term='$trm') as paid, 
                    (SELECT SUM(TutionF+MotF+AdmF+DiariesF+BoF+TranF+UniF+PEF+TxtF+Tour+Tablet+Dev) from Fees2 where Year='$yr2' AND Term='$trmm' AND pid=pupils.count) as totto,
                    (SELECT SUM(TuFee) FROM feestatement where pid=Fees2.pid AND Year='$yrr' AND Term='$trm') as tfep, (SELECT SUM(Dev) FROM feestatement where pid=Fees2.pid AND Year='$yrr' AND Term='$trm') as tdevp,
                    (SELECT SUM(UnFee) FROM feestatement where pid=Fees2.pid AND Year='$yrr' AND Term='$trm') as tunifo,
                    (SELECT SUM(BoFee+AdFee+MoFee+DiFee+BookFee+PeFee+Tablet) FROM feestatement where pid=Fees2.pid AND Year='$yrr' AND Term='$trm') as totherp,
                    (SELECT SUM(TrFee) FROM feestatement where pid=Fees2.pid AND Year='$yrr' AND Term='$trm') as trap, (SELECT SUM(PaidAmount) from feestatement where Year='$yr2' AND Term='$trmm' AND pid=pupils.count) as totpaidlast
                    from Fees2  JOIN pupils ON pupils.count=Fees2.pid JOIN feestatement ON Fees2.pid=feestatement.pid WHERE Fees2.Grad='$gr' AND Fees2.Term='$trm' AND Fees2.Year='$yrr' GROUP BY pid ORDER BY pnames ASC";
                    $result = $con->query($sql);
                    if ($result->num_rows > 0) 
                    {
                    $cnt='1';
                    while($row = $result->fetch_assoc()) 
                    {
                    $tu=$row["TutionF"];    
                    $ad=$row["AdmF"];   
                    $di=$row["DiariesF"];  
                    $bo=$row["BoF"];  
                    $tr=$row["TranF"];  
                    $un=$row["UniF"];  
                    $moti=$row["MotF"];  
                    $pef=$row["PEF"];  
                    $bk=$row["TxtF"]; 
                    $tou=$row["Tour"];
                    $tab=$row["Tablet"]; 
                    $dev=$row["Dev"]; 
                        
                    if($ad==''){$ad='0';}
                    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($tou==''){$tou='0';}
                    if($tab==''){$tab='0';}
                    if($tu==''){$tu='0';}
                    if($dev==''){$dev='0';}
                   
                    $tof=$row["totto"];
                    $top=$row["totpaidlast"];
                    $babala=$tof-$top;
              
                    $tot=$tu+$bo+$tr+$un+$di+$bk+$moti+$pef+$ad+$tou+$tab+$dev+$babala;
                  // $tot=100000;
                    ?>	
                    <tr>
                    <td><?php echo $cnt; ?></td>
                    <td><?php echo $row["pnames"]; ?></td>
                    <td><?php echo $row["conta"]; ?></td>
                    <td><?php echo $row["adm"]; ?></td>
                    <td><?php echo $row["clas"]; ?></td>
                    <td class="alert-warning"><?php echo $babala; ?></td>
                    <td class="bg-secondary"><?php echo $row["tfep"]; ?></td>
                    <td class="bg-secondary"><?php echo $row["tdevp"]; ?></td>
                    <td class="bg-secondary"><?php echo $row["trap"]; ?></td>
                    <td class="bg-secondary"><?php echo $row["tunifo"]; ?></td>
                    <td class="bg-secondary"><?php echo $row["totherp"]; ?></td>
                      <td class="alert-warning"><strong><?php echo $tot; ?></strong></td>
                    <td class="alert-success"><strong><?php echo $row["paid"]; ?></strong></td>
                    <?php 
                     $ba=$tot-$row["paid"]; 
                     
                    if($ba<0)
                    {
                     ?>
                     <td class=""><strong><?php echo number_format($ba); ?> </strong> </td>
                     <?php
                    }else if($ba=='0')
                    {
                    ?>
                     <td class="bg-secondary"> <strong> <?php echo number_format($ba); ?> </strong> </td>
                    <?php
                    }else
                    {
                    ?>
                     <td class="alert-warning"> <strong> <?php echo number_format($ba); ?> </strong> </td>
                    <?php
                    }
                    ?>
                    <input type="hidden" value="<?php echo $row["count"]; ?>" class="thecount" />
                    <input type="hidden" value="<?php echo $ba; ?>" class="thebalan" />
                    </tr>
                    <?php
                    $cnt=$cnt+1;
                    }
                    }
                    }
                    ?>
                     </tbody>
                     <tfoot>
                         <tr style="background-color:#00001a;color:#FFFFFF">
                             <?php
                             $gr=$_SESSION['cll'];
                             $trm=$_SESSION['tmm'];
                             $yrr=$_SESSION['yrr']; 
                             $income='';
                            
                             if($trm !='' || $yrr !='')
                             { 
                            $result2 = mysqli_query($con,"SELECT SUM(TutionF+AdmF+DiariesF+BoF+TranF+UniF+MotF+PEF+TxtF+Tour+Tablet+Dev) as fees FROM Fees2 where Year='$yrr' AND Term='$trm' AND Grad='$gr'");
                            while($row = $result2->fetch_assoc()) 
                            {
                            $feess=$row["fees"];
                            }
                            
                            $result3 = mysqli_query($con,"SELECT SUM(TutionF+AdmF+DiariesF+BoF+TranF+UniF+MotF+PEF+TxtF+Tour+Tablet+Dev) as feess FROM Fees2 where Year='$yr2' AND Term='$trmm' AND Grad='$gr'");
                            while($row = $result3->fetch_assoc()) 
                            {
                            $feelast=$row["feess"];
                            }
                            
                            $result = mysqli_query($con,"SELECT SUM(PaidAmount) as tolast FROM feestatement where Year='$yr2' AND Term='$trmm' AND Grad='$gr'");
                            while($row = $result->fetch_assoc()) 
                            {
                            $incomelast=$row["tolast"];
                            }
                            
                            $result5 = mysqli_query($con,"select feestatement.*,(SELECT SUM(TuFee) FROM feestatement where Year='$yrr' AND Term='$trm' AND Grad='$gr') as totufee,
                            (SELECT SUM(Dev) FROM feestatement where Year='$yrr' AND Term='$trm' AND Grad='$gr') as totdev,
                            (SELECT SUM(TrFee) FROM feestatement where Year='$yrr' AND Term='$trm' AND Grad='$gr') as tottran,
                            (SELECT SUM(UnFee) FROM feestatement where Year='$yrr' AND Term='$trm' AND Grad='$gr') as totuni,
                            (SELECT SUM(BoFee+AdFee+MoFee+DiFee+BookFee+PeFee+Tablet) FROM feestatement where Year='$yrr' AND Term='$trm' AND Grad='$gr') as totothers,
                            (SELECT SUM(PaidAmount) FROM feestatement where Year='$yrr' AND Term='$trm' AND Grad='$gr') as totpaid FROM feestatement where Year='$yrr' AND Term='$trm' AND Grad='$gr'");
                            while($row = $result5->fetch_assoc()) 
                            {
                            $tufee=$row["totufee"];
                            $todev=$row["totdev"];
                            $totra=$row["tottran"];
                            $totuni=$row["totuni"];
                            $totothers=$row["totothers"];
                            $totpaid=$row["totpaid"];
                            }
                        
                            $lastbala=$feelast-$incomelast;
                            $tofee=$lastbala+$feess;
                            $tobala=$tofee-$totpaid;
                             }
                            ?>
                           <td></td>
                           <td></td>
                           <td></td>
                           <td></td>
                           <td><strong class="float-right">TOTAL: </strong></td>
                           <td><strong><?php echo number_format($lastbala); ?></strong></td>
                           <td><strong><?php echo number_format($tufee); ?></strong></td>
                           <td><strong><?php echo number_format($todev); ?></strong></td>
                           <td><strong><?php echo number_format($totra); ?></strong></td>
                           <td><strong><?php echo number_format($totuni); ?></strong></td>
                           <td><strong><?php echo number_format($totothers); ?></strong></td>
                           <td><strong><?php echo number_format($tofee); ?></strong></td>
                           <td class="bg-info"><strong><?php echo number_format($totpaid); ?></strong></td>
                           <td class="bg-danger"><strong><?php echo number_format($tobala); ?></strong></td>
                         </tr>
                         
                         <tr>
                             <td colspan="3"><a href="Fee_Statement.php" target="_blank" class="btn btn-warning btn-sm ">Download PDF Report <i class="fa fa-cloud-download"></i></a></td>
                             <td colspan="6"><button class="btn btn-info btn-sm " id="daexcel">Download Excel Report <img style="max-height:20px" src="images/exc.png" /> </button></td>
                             <td colspan="3"><label for="messaging" id="thesavemsg"></label></td>
                             <td colspan="2">
                             <button class="btn btn-secondary btn-sm pull-right" title="Save before creating next term Fees Structure" id="savebalances">Save the Balances <i class="fa fa-save"></i></button>
                             </td>
                         </tr>
                     </tfoot>
                 </table> 