<?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="" 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" id="tablefees" style="font-size:12px;font-family:Arial">
                     <thead>
                         
                         <tr>
                             <th>#</th>
                             <th>Pupils Name</th>
                             <th>Contact</th>
                             <th>Adm No.</th>
                             <th>Class</th>
                             <th>Total Fee (KES)</th>
                             <th>Total Paid</th>
                             <th>Balance</th>
                         </tr>
                     </thead>
                     <tbody>
                    <?php
                    $gr=$_SESSION['cll'];
                    $trm=$_SESSION['tmm'];
                    $yrr=$_SESSION['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, (SELECT SUM(PaidAmount)  FROM feestatement where pid=Fees2.pid AND Year='$yrr' AND Term='$trm') as paid from Fees2 JOIN pupils ON pupils.count=Fees2.pid LEFT JOIN feestatement ON Fees2.pid=feestatement.pid WHERE Fees2.Grad='$gr' AND Fees2.Term='$trm' group by id";

                    $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"];  
                        
                    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==''){$bo='tou';}
                    //$tot=$tu+$bo+$tr+$un+$moti+$pef+$di+$bk;
                    $tot=$tu+$bo+$tr+$un+$di+$bk+$moti+$pef+$ad+$tou;
                    ?>	
                    <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 ><strong> <?php echo $tot; ?></strong></td>
                    <td class="bg-gray"><strong><?php echo $row["paid"]; ?></strong></td>
                    <?php 
                     $ba=$tot-$row["paid"]; 
                    if($ba<0)
                    {
                     ?>
                     <td class=""> <?php echo $ba; ?> (Over Paid)</td>
                     <?php
                    }else if($ba=='0')
                    {
                    ?>
                     <td class="bg-secondary"> <?php echo $ba; ?> (Cleared)</td>
                    <?php
                    }else
                    {
                    ?>
                     <td class=""> <?php echo $ba; ?> (Not cleared)</td>
                    <?php
                    }
                    ?>
                    </tr>
                    <?php
                    $cnt=$cnt+1;
                    }
                    }
                    ?>
                    
                     <tr class="">
                            <?php
                            $gr=$_SESSION['cll'];
                            $trm=$_SESSION['tmm'];
                            $yrr=$_SESSION['yrr']; 
                            $income='';
                            
                            $result = mysqli_query($con,"SELECT SUM(PaidAmount) as tincome FROM feestatement where Year='$yrr' AND Term='$trm' AND Grad='$gr'");
                            while($row = $result->fetch_assoc()) 
                            {
                            $income=$row["tincome"];
                            }
                            
                            
                            $result2 = mysqli_query($con,"SELECT SUM(TutionF+AdmF+DiariesF+BoF+TranF+UniF+	MotF+PEF+TxtF+Tour+Tablet) as fees FROM Fees2 where Year='$yrr' AND Term='$trm' AND Grad='$gr'");
                            while($row = $result2->fetch_assoc()) 
                            {
                            $fees=$row["fees"];
                            }
                            ?>
                            <td colspan="5" style=""><strong style="float:right">TOTAL:</strong></td>
                            <td style=""><strong><?php echo $fees; ?></strong></td>
                            <td><strong><?php echo $income; ?></strong></td>
                            <td><strong><?php echo $fees-$income; ?></strong></td>
                         </tr>
                     </tbody>
                 </table> 
              
              </div>
            </div>
            </div>
    </section>
</div>


    </body>
</html>