<?php 
include ("../core/kfunctions.php");?>
<html>
<head>
<?php kheader(); 

include("../core/connect.php");





if(isset($_POST["item_filter"])){

$filteryear=$_POST["schoolyear"];
$filterterm=$_POST["term"];

$cy=date("y");
$yeartotake="schoolyear='$cy'";

$cm = date("m");
if ($cm>='1' and $cm<='4'){$termtotake="1";}
elseif ($cm>='5' and $cm<='8'){$termtotake="2";}
elseif ($cm>='9' and $cm<='12'){$termtotake="3";}

if(!empty($filteryear)&&!empty($filterterm)){
$dadate="AND schoolyear = '$filteryear'  AND schoolterm = '$filterterm'";
$prod_date=$dadate;  
$wd="<span style='color:maroon;'> For 20</span>".$filteryear.",<span style='color:maroon;'><i> Term </i></span>".$filterterm."; ";

$insert_scrapp=$dbh->query("INSERT into scrap (item,item2,item3,type) values('$rolenumber','$filteryear','$filterterm','payment_filter')");
$filter_show="$wd";}

else{

$dadate=" and schoolyear = '$cy' and schoolterm = '$termtotake'"; $wd='';
$filter_show="$wd";

"<div class='alert alert-warning' style='text-align:center;font-weight:bold;font-style:italic;'> ".$wd."</div>";
} }

else{

$cm = date("m");
if ($cm>='1' and $cm<='4'){$termtotake="1";}
elseif ($cm>='5' and $cm<='8'){$termtotake="2";}
elseif ($cm>='9' and $cm<='12'){$termtotake="3";}

$cy=date("y");
$dadate=" and schoolyear = '$cy' and schoolterm = '$termtotake'"; $wd='';
$filter_show="$wd";} 

?>


</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='col-lg-12'><br> &nbsp </div>
            
            <div class="container-fluid">

<!-- /.row profile-->
<?php

$year = date('y');

echo "
<div class='col-lg-12'>
<div class='panel panel-yellow'>
<div class='panel-heading'>
<h3 class='panel-title'>Requirements</h3>
</div>
<div class='panel-body'>
<form method='post'>
<div class='col-lg-2'>
<select name='schoolyear' class='form-control' required='required'>
<option value='$year'> 20$year </option>";

$result2 = $dbh->query("select * from scrap where type='ya'");
$row2 = $result2->fetchObject();

do{
echo "<option value='$row2->item'>".$row2->item2."</option>";
}while($row2 = $result2->fetchObject());

echo"
</select>
</div>

<div class='col-lg-2'>
<select name='term' class='form-control' required='required'>
<option value='$year'> Term $termtotake </option>";

$result3 = $dbh->query("select * from scrap where type='ts'");
$row3 = $result3->fetchObject();

do{
echo "<option value='$row3->item'>Term ".$row3->item2."</option>";
}while($row3 = $result3->fetchObject());

echo"
</select>
</div>
<div class='col-lg-1'>
<input type='submit' class='btn btn-sm btn-primary' name='item_filter' value='Filter'>
</div>

</form>
<div class='col-lg-12'><br> </div>";

echo "
<table class='table table-bordered table-hover table-striped sortable'>
<tr>
<th>Item</th>
<th>Expected</th>
<th>Collected</th>
<th>Achievement (%)</th>
</tr>";
$result_scrap=$dbh->query("SELECT * from scrap where type='fsitem' and school_id='$school_id'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowcount();


if ($count_scrap>0){
do {
 
$expectedsum=$dbh->query("select sum(amount) from $homelandtbl where fsitemid='$row_scrap->item' and paytype='d' $dadate ");

$row_expected=$expectedsum->fetch();
$totalnewfeeexpected= $row_expected['0'];//all homeland fees except balcarriedforward
$totalnewfeeexpected1=intval($totalnewfeeexpected);//turn that homeland figure to int

$collectedsum=$dbh->query("select sum(amount) from $homelandtbl where fsitemid='$row_scrap->item' and paytype='c'  $dadate ");

$row_collected=$collectedsum->fetch();
$totalnewfeecollected= $row_collected['0'];//all homeland fees except balcarriedforward
$totalnewfeecollected1=intval($totalnewfeecollected);//turn that homeland figure to int

if ($totalnewfeecollected1 != 0){
$achieved = (($totalnewfeecollected1/$totalnewfeeexpected1)*100);}
else{
$achieved = "Null";
}

echo "
<tr>
<td>".$row_scrap->item2."</td>
<td>".number_format($totalnewfeeexpected1)."</td>
<td>".number_format($totalnewfeecollected1)."</td>
<td>".intval($achieved)."%</td>
</tr>";

}while($row_scrap=$result_scrap->fetchObject());
}
echo "</table>

</div>
</div>
</div>";
?>      
    </div>
            <!-- /.container-fluid -->

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

    </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>

    <!-- Morris Charts JavaScript -->
    
    <!-- bursar tracker -->
    <?php
    useractivity("Dashboard","1");
    ?>
    <!-- bursar tracker -->
    

</body>

</html>