<?php include ("../core/kfunctions.php");
//role("dos,hmp");
?>
<html>
<head>
<?php kheader(); ?>
<?php 
require_once("../core/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>&nbsp;</div>
                <div class="row">
                    <form method='post'>
<div class='col-lg-2'>
<select class="form-control" name="schoolyear" required>
<option value="<?php echo date("y"); ?>"><?php echo date("Y"); ?></option>
<?php
$result_scrap=$dbh->query("select * from scrap where  type='ya'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap){
    do{
echo "<option value='".$row_scrap->item."'>".$row_scrap->item2."</option>";
}while($row_scrap=$result_scrap->fetchObject());
}else{
    echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
</div>
<div class='col-lg-2'>
<select class="form-control" name="schoolterm" required>
<?php
$cm=date("m");
if($cm>='1' and $cm<='4'){$oterm="Term 1"; $iterm="1";}
elseif($cm>='5' and $cm<='8'){$oterm="Term 2"; $iterm="2";}
elseif($cm>='9' and $cm<='12'){$oterm="Term 3"; $iterm="3";}
echo "
<option value='".$iterm."'>".$oterm."</option>";

$result_scrap=$dbh->query("select * from scrap where type='ts'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap){
    do{
echo "<option value='".$row_scrap->item."'>Term ".$row_scrap->item2."</option>";
}while($row_scrap=$result_scrap->fetchObject());
}else{
    echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
</div>
<div class='col-lg-2'>
<select class="form-control" name="schoolclass" required>
<option value="">All classes</option>
<?php
$role=$_SESSION["role"];
if($role=="doss"){$type_out="type='secclas'";}
elseif($role=="dosp"){$type_out="type='priclas'";}

$result_scrap=$dbh->query("select * from scrap where type='priclas'");
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap){
    do{
echo "<option value='".$row_scrap->item."'>".$row_scrap->item2."</option>";
}while($row_scrap=$result_scrap->fetchObject());
}else{
    echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
</div>

<div class='col-lg-2'>
<select class='form-control' name='category' required>
<option value=''>Category</option>
  <?php
include("../core/connect.php");

$result_scrap = $dbh->query("select * from scrap where type='category'");
$row_scrap = $result_scrap->fetchObject();
$count_scrap = $result_scrap->rowCount();
if($count_scrap>0){
do{
echo "<option value='".$row_scrap->item."'>".$row_scrap->item2."</option>";
}while($row_scrap = $result_scrap->fetchObject());
}else{
echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
    </div>



<div class='col-lg-2'>
<select class='form-control' name='fsitemid' required>
<option value=''>Fees Item</option>
  <?php
include("../core/connect.php");

$result_scrap = $dbh->query("select * from scrap where type='fsitem'");
$row_scrap = $result_scrap->fetchObject();
$count_scrap = $result_scrap->rowCount();
if($count_scrap>0){
do{
echo "<option value='".$row_scrap->item."'>".$row_scrap->item2."</option>";
}while($row_scrap = $result_scrap->fetchObject());
}else{
echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
    </div>
<div class='col-lg-2'>
<input type="text" class='form-control' name="amount" placeholder="Amount" >
</div>
</div>

<div class='col-lg-12'><p>&nbsp;
<div class='col-lg-12'>
    <input type='submit' name='submit_feesitem' class='btn btn-lg btn-success col-lg-12' value='Submit Fees Item'>
</div>
</form></div>
<?php
 

if(isset($_POST["submit_feesitem"])){
$schoolclass=$_POST["schoolclass"];    
$schoolterm=$_POST["schoolterm"];
$schoolyear=$_POST["schoolyear"];
$category=$_POST["category"];
$fsitemid=$_POST["fsitemid"];
$amount=$_POST["amount"];
$lastupdate=date("Y-m-d H:i:s");

include("../core/connect.php");
$result_children=$dbh->query("select * from children where schoolyear='$schoolyear' and term='$schoolterm' and schoolclass='$schoolclass' and category='$category' and status='2'");
$row_children=$result_children->fetchObject();
$count_children=$result_children->rowCount();

//echo $count_children;

if($count_children>0){
    do{
       
$result_homeland=$dbh->query("select * from homeland where schoolyear='$schoolyear' and schoolterm='$schoolterm' and schoolclass='$schoolclass' and cid='$row_children->cid' and fsitemid='$fsitemid'");
$row_homeland=$result_homeland->fetchObject();
$count_homeland=$result_homeland->rowCount();
$cid=$row_children->cid;


//if child hasnt had this term and year in homeland on this given item, put them in homeland.
if ($count_homeland<1){
 $insert_homeland=$dbh->query("insert into homeland (cid,schoolyear,schoolclass,schoolterm,ouruser,paytype,fsitemid,amount) values('$row_children->cid','$schoolyear','$schoolclass','$schoolterm','$rolenumber','d','$fsitemid','$amount')");

echo "<div class='col-lg-12'><br><div class='alert alert-success'>Success: Learners submitted to Homeland. </div></div>";

}
else {echo $cid." already has item ".$fsitemid." in Homeland <br>";
}

//check if child has this item in bravos. if he doesnt, add it for him and make the bravos total too
$result_bravos=$dbh->query("select * from bravos where cid='$cid' and fsitemid='$fsitemid'");
$row_bravos=$result_bravos->fetchObject();
$count_bravos=$result_bravos->rowCount();

if ($count_bravos<1){
        $insert_bravos=$dbh->query("insert into bravos (cid,fsitemid,payable,balance,lastuser,lastupdate,originalfee,schoolyear,schoolclass,schoolterm) values($cid,'$fsitemid','$amount','$amount','$rolenumber','$lastupdate','$amount','$schoolyear','$schoolclass','$schoolterm')");

//now check if he has fullpay total. if not, make for him. if yes already, add our item to his total.
$result_bravospay=$dbh->query("select * from bravos where cid='$cid' and fsitemid='fullpay'");
$row_bravospay=$result_bravospay->fetchObject();
$count_bravospay=$result_bravospay->rowCount();

if ($count_bravospay<1){

$fullpay=$dbh->query("select sum(payable) from bravos where cid='$cid'");
$countfullpay=$fullpay->rowCount();
if($countfullpay>0){$rowfullpay=$fullpay->fetch();} else {$rowfullpay='0';} 
$fullpay=$rowfullpay['0'];  //total target current month

    $insert_bravos=$dbh->query("insert into bravos (cid,fsitemid,payable,balance,lastuser,lastupdate,originalfee) values($cid,'fullpay','$fullpay','$fullpay','$rolenumber','$lastupdate','$fullpay')");


}
echo "<div class='col-lg-12'><br><div class='alert alert-success'>Success: Learners submitted to Bravos. </div></div>";

}
else {echo $cid." already has item ".$fsitemid." in Bravos <br>";}

    }while($row_children=$result_children->fetchObject());
}

echo "<div class='col-lg-12'><br><div class='alert alert-success'>Success: Fees item has been submitted for selected learners. </div></div>";
}


?>
<form method='post'>
<div class='col-lg-12'><br></div>
<div class="col-lg-12">
<div class='panel panel-primary'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Learners' list</h3></div>
<div class='panel-body'>
<table class='table table-bordered table-hover table-striped sortable'>
<tr>
    <th width='2%'>No</th>
    <th width='12%'>Computer No.</th>
    <th width='2%'></th>
    <th >Student name</th>
    </tr>

</table>
</div></div>
</form>

</div>

<div class="col-lg-4">
<div class='panel panel-primary'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Add New Fees Item</h3></div>
<div class='panel-body'>
<?php
                              if(isset($_POST["submit_item"])){
                                $itemname=$_POST["itemname"];

                                
                                  $yy=date("Y"); $fyy=substr($yy,2,2); $mm=date("m"); 
                                  $dd=date("d"); $hi=date("h"); $mi=date("i");
                                  $sa=date("sa"); $sa=date("s");
                                  
                                  
                                  $fsitemid="it".$fyy.$mm.$dd.$hi.$mi;
                                $insert_scrap=$dbh->query("insert into scrap (item,item2,type) values('$fsitemid','$itemname','fsitem')");
                                if($insert_scrap){echo "<div class='alert alert-success'>Success: Item has been added.</div>";}
                              }

                              ?>
        <form method='post'>
  <div class='col-lg-9'>                           
  <input type='text' class='form-control' name='itemname' placeholder='Enter Item' required>  
</div>                          
<div class='col-lg-2'>                           
  <input type='submit' name='submit_item' class='btn btn-sm btn-primary' value='Submit'>  
</div>                          

</form>
</div></div>
</div>



</div>

</div>


                            </div> 

                            


                <!-- /.row -->

               

            </div>
            <!-- /.container-fluid -->
<?php useractivity("on page", "56"); ?>
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->
    <script>
function checkall(){
<?php
include("../core/connect.php");
$result_citadel=$dbh->query("select * from citadel_o where schoolyear='$schoolyear' and term='$schoolterm' and schoolclass='$schoolclass'");
$row_citadel=$result_citadel->fetchObject();
$count_citadel=$result_citadel->rowCount();

if($count_citadel>0){
    do{
?>
if(document.getElementById('checkallz').checked){
document.getElementById('cidcheck<?php echo $row_citadel->autoid; ?>').checked=true;
}else{
document.getElementById('cidcheck<?php echo $row_citadel->autoid; ?>').checked=false; 
}
<?php
}while($row_citadel=$result_citadel->fetchObject());
}
?>
}
</script>

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