<?php include ("kfunctions.php")?>
<html>
<head>
<?php kheader(); ?>
<?php 
require_once("connect.php");

?>
<link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<script src="js/sorttable.js"></script>
</head>
<body>


    <div id="wrapper">

        <!-- Navigation -->
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <?php ktopbar(); ?>
            <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
            <?php kleftbar(); ?>
            <!-- /.navbar-collapse -->
        </nav>

        <div id="page-wrapper">

            <div class="container-fluid">

 <!-- Page Heading -->
                

                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-body">
                            
                           

<?php

require_once("connect.php");
$term=$_POST["term"];
$feetype=$_POST["feetype"];
$level=$_POST["level"];
$amount=$_POST["amount"];
$schoolclass=$_POST["schoolclass"];
$schoolyear=$_POST["schoolyear"];
$feetype2=$feetype."bal";

include("class.php");
useractivity($feetype.$level.$amount."(class ".$schoolclass.")(term ".$term.")(year 20".$schoolyear.") filter results","3.5");

include ("connect.php");
 $result25 = $dbh->query("select * from scrap where item='$feetype'");
$row25 = $result25->fetchObject();

if($feetype=="ownfee"){
    $titlereq = "Fees";
$querier = "feesbalance $level'$amount'";
$querier2 = "feesbalance";

}else{
$titlereq = $row25->item2; 
$querier = "$feetype2$level'$amount'";   
$querier2 = $feetype2;
}

if($schoolyear=="01"){
$dayear = "";

}else{
  $dayear = "schoolyear='$schoolyear' and"; 
}



if($schoolclass=="11"){
    $daclass2 = "((cid>='10000')&&(cid<='49999')) and";

}elseif($schoolclass=="23"){

$daclass2 = "cid>='50000' and"; 
}else{
    $daclass2 = "schoolclass='$schoolclass' and";
}

if($term=="t4"){
$result=$dbh->query("
    select * from t1 where $daclass2 $dayear $querier 
    union 
    select * from t2 where $daclass2  $dayear $querier
    union
    select * from t3 where $daclass2  $dayear $querier");
$row=$result->fetchObject();
$count=$result->rowCount();

$result61=$dbh->query("select sum($querier2) from t1 where $daclass2 $dayear $querier");
$row61=$result61->fetch();

$result62=$dbh->query("select sum($querier2) from t2 where $daclass2 $dayear $querier");
$row62=$result62->fetch();

$result63=$dbh->query("select sum($querier2) from t3 where $daclass2 $dayear $querier");
$row63=$result63->fetch();

$row60[0] = $row61[0] + $row62[0] + $row63[0];

}else{

$result=$dbh->query("select * from $term where $daclass2 $dayear $querier");
$row=$result->fetchObject();
$count=$result->rowCount();

$result60=$dbh->query("select sum($querier2) from $term where $daclass2 $dayear $querier");
$row60=$result60->fetch();


}
echo "
<div class='panel panel-yellow'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Children meeting criteria (Search result(s) : ".$count.", Total sum of ".$titlereq." balance : ".number_format($row60[0]).")</h3></div>
<div class='panel-body'>


<table class='table table-bordered table-hover table-striped sortable'>
 <thead>
<tr>
<th>ID</th>
<th>Class</th>
<th>Names</th>
<th>".$titlereq."</th>
<th>".$titlereq." Paid</th>
<th>".$titlereq." Bal</th>
<th>Comment</th>

</tr>
</thead>";


if($count>0){ 
do {
	
     $result4 = $dbh->query("select * from children  where cid='$row->cid'");
$row4 = $result4->fetchObject();

if($feetype=="ownfee"){
$feetype3 = "feesbalance";
$sumtotake="p_fees";

}else{
$feetype3 = $feetype."bal";
$sumtotake="p_".$feetype;
}
$tid=substr($term, 1,2);

$resultsum_p_amount=$dbh->query("select sum(".$sumtotake.") from received where cid='$row->cid' and schoolyear='$schoolyear' and term='$tid'");
$countp_amount=$resultsum_p_amount->rowCount();
if($countp_amount>0){$sum_p_amount=$resultsum_p_amount->fetch();} else {$sum_p_amount='0';} 

$schoolclass = $row->schoolclass;
include ("class.php");

echo "
<tr>
<td>".$row->cid."</td>
<td>".$daclass."</td>
<td>".$row4->fname."&nbsp;".$row4->sname."</td>
<td>".number_format($row->$feetype)."</td>
<td>".number_format($sum_p_amount['0'])."</td>
<td>".number_format($row->$feetype3)."</td>
<td>".$row->comment."</td>";
//td><a href='detail.php?cid=".$row->cid."'>More details</td>
echo "</tr>";}
while($row=$result->fetchObject()); 

}else {echo "So far, no children in this list.";}
?>
</table>

<a href="feebalance.php">Back</a>
</div></div>

</div>
                            </div> </div>
                    </div>
                </div>
                <!-- /.row -->

               

            </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->
<?php useractivity("on page", "3.5"); ?>

    </div>
    <!-- /#wrapper -->

    <!-- jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js" charset="UTF-8"></script>
</body>

</html>
