<?php
include('../core/kfunctions.php');
$result_children3=$dbh->query("select * from children where status='2'");
$row_children3=$result_children3->fetchObject();
$count_children3=$result_children3->rowCount();

if($count_children3>0){

do {
$cid=$row_children3->cid;
$schoolyear=$row_children3->schoolyear;
$schoolterm=$row_children3->term;
$schoolclass=$row_children3->schoolclass;
//PART 3 --------------ASSIGNING NEW AMOUNTS FROM FEES STRUCTURE------------------------
$result_fsitems=$dbh->query("select * from fsitems where schoolclass='$row_children3->schoolclass' and newitemname='' and (gender='all' or gender='$row_children3->gender') and (category='$row_children3->category' or category='all')");
$row_fsitems=$result_fsitems->fetchObject();
$count_fsitems=$result_fsitems->rowCount();

//aaaa: if this child actually has a fees structure, insert into homeland
if ($count_fsitems>0){
 // echo $row_children3->cid." - ".$row_fsitems->amount." ".$row_children3->schoolclass."<br>";

$cid=$row_children3->cid;
$schoolyear=$row_children3->schoolyear;
$schoolterm=$row_children3->term;
$schoolclass=$row_children3->schoolclass;
$fsitemid=$row_fsitems->fsitemid;
$amount=$row_fsitems->amount;

$result_homeland=$dbh->query("select * from homeland where schoolyear='$row_children3->schoolyear' and schoolterm='$schoolterm' and schoolclass='$row_children3->schoolclass' and cid='$row_children3->cid' and fsitemid='$row_fsitems->fsitemid'");
$row_homeland=$result_homeland->fetchObject();
$count_homeland=$result_homeland->rowCount();



//bbbb: 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('$cid','$schoolyear','$schoolclass','$schoolterm','$rolenumber','d','$fsitemid','$amount')");

//Also insert new fees to bravos
$insert_bravoss=$dbh->query("INSERT into bravos (cid,schoolyear,schoolclass,schoolterm,fsitemid,payable,balance,originalfee,lastuser,lastupdate) values ('$cid','$schoolyear','$schoolclass','$schoolterm','$fsitemid','$amount','$amount','$amount','$rolenumber','$todaynow')");

//and update fullpay total
/*
sum of this child in homeland where not balcarriedforward
add it to fullpay
*/

$homelandsum=$dbh->query("select sum(amount) from homeland where schoolyear='$row_children3->schoolyear' and schoolterm='$schoolterm' and schoolclass='$row_children3->schoolclass' and cid='$row_children3->cid' and paytype='d' and fsitemid!='BalCarriedForward' ");

$row_hom=$homelandsum->fetch();
$totalnewfee= $row_hom['0'];//all homeland fees except balcarriedforward
$totalnewfee1=intval($totalnewfee);//turn that homeland figure to int

//get current fullpay balance
$result_bravos2=$dbh->query("select * from bravos where cid='$row_children3->cid' and fsitemid='fullpay'");
$row_bravos2=$result_bravos2->fetchObject();
$count_bravos2=$result_bravos2->rowCount();
$curfullpaybal=$row_bravos2->balance;//current fullpay balance

$fullpaybal=$curfullpaybal+$totalnewfee1;

  $update_bravoss=$dbh->query("UPDATE bravos SET balance='$fullpaybal', payable='$fullpaybal', paid=0  where fsitemid='fullpay' and cid='$cid'");

printColor($cid." - Child hasnt had this term and year in homeland. Amount:".$row_fsitems->amount." Class: ".$schoolclass,"green");

printColor($cid." new:".$totalnewfee1." total:".$fullpaybal,"green");

}
else {
printColor($cid." - Child already had this term and year in homeland","red");
}
//bbbb ends above 

}
else {echo "<br>The learner has no fees structure: ".$row_children3->cid." Class:".$row_children3->schoolclass;}
//aaaa: ends above

       }while($row_children3=$result_children3->fetchObject());
   }

?>