<?php 
session_start();
ob_start();
$idauto= $_SESSION['user_idd'];
include('../include/config.php');

?>
<html>
    <title><?php echo ' '.$_SESSION['cll'].' - '.$_SESSION['tmm'].' '.$_SESSION['yrr'];  ?></title>
    <head>
         <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../plugins/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css">
  <link rel="stylesheet" href="../plugins/fontawesome-free/css/all.min.css">
  <!-- SweetAlert2 -->
  <link rel="stylesheet" href="../plugins/sweetalert2-theme-bootstrap-4/bootstrap-4.min.css">
  <link rel="stylesheet" href="../plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
  <link rel="stylesheet" href="../plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
  <link rel="stylesheet" href="../plugins/datatables-buttons/css/buttons.bootstrap4.min.css">
  <!-- Toastr -->
  <link rel="stylesheet" href="../plugins/toastr/toastr.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  
  <link rel="stylesheet" href="../dist/css/adminlte.min.css">
    <link rel="stylesheet" href="../plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <link rel="stylesheet" href="../plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
  <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker.css">
  <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
  <link rel="shortcut icon" href="../images/logo.ico">
  
 <script	src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="schedule/js/bootstrap.min.js"></script> 
    </head>
    <body onload="window.print()">
        <?php
          $yrr="";  
          $trm="";
          $grad="";
         ?>
<div class="container">
    <!-- Content Header (Page header) -->
     <center><h4><strong></strong>BISHOP KAIRO EDUCATION CENTRE</strong></h4></center>
    <center><h5 style="text-transform: uppercase;">School Fees statement for: <strong class=""> 
    <?php echo ' '.$_SESSION['cll'].' - '.$_SESSION['tmm'].' '.$_SESSION['yrr'];  ?></strong></h5>
    </center>
         <hr />
         
   <section class="content" style="text-decoration:none;font-weight:normal;font-family:Times;font-size:12px">
          <div class="row">
          <div class="col-md-12 col-sm-12 col-12">
             <div class="table-responsive" id="divpaidfees">
                 <table class="table table-bordered table-sm" id="tablefees" >
                     <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 <?php echo $cla; ?></th>
                             <th>Contact</th>
                             <th>Adm No.</th>
                             <th>Class</th>
                             <th title="Fees Include Carry Forward Balances" class="bg-info">Total Fee (KES)</th>
                             <th class="">Term Fees </th>
                             <th class="">Dev</th>
                             <th class="">Trans</th>
                             <th class="">Uniform</th>
                             <th class="">Others</th>
                             <th class="">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';}
                   // $int = (int)$num;
                   
                    $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"><strong> <?php echo $tot; ?></strong></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 style="color:green"><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
                    }
                    ?>
                    </tr>
                    <?php
                    $cnt=$cnt+1;
                    }
                    }
                    }
                    ?>
                     </tbody>
                     <tfoot>
                         <tr style="background-color:">
                             <?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 colspan="5"><strong class="float-right">TOTAL: </strong></td>  
                           <td><strong><?php echo number_format($tofee); ?></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 class="bg-info"><strong><?php echo number_format($totpaid); ?></strong></td>
                           <td class="bg-danger"><strong><?php echo number_format($tobala); ?></strong></td>
                         </tr>
                     </tfoot>
                 </table> 
              </div>
            </div>
            </div>
    </section>
</div>
    </body>
</html>