<?php
include('../core/kfunctions.php');
//-----------------PART 2 - CARRYING BALANCES FORWARD-----------------------//
$result_children2=$dbh->query("select * from children where status='2'");
$row_children2=$result_children2->fetchObject();
$count_children2=$result_children2->rowCount();

//echo $count_children;

if($count_children2>0){
	//remove anything that's not fullpay.
$clear_bravos=$dbh->query("delete from bravos where fsitemid!='fullpay' and autodate<'$twodaysbacknow'");
echo "daterrr".$twodaysbacknow;
	do{
		
$cid=$row_children2->cid;
$schoolyear=$row_children2->schoolyear;
$schoolterm=$row_children2->term;
$schoolclass=$row_children2->schoolclass;
//$fsitemid=$row_fsitems->fsitemid;

$result_bravoss=$dbh->query("select * from bravos where cid='$row_children2->cid' and fsitemid='fullpay' and balance>0");
$row_bravoss=$result_bravoss->fetchObject();
$count_bravoss=$result_bravoss->rowCount();

if ($count_bravoss>0) //yyyy: if child has balance actually
{//echo "Has balance " .$row_bravoss->balance."<br>";

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



//vvvv: if child hasnt had this term and year in homeland on this given item, put them in homeland and bravos.
if ($count_homeland<1){ 

//put to homeland first
$insert_homeland=$dbh->query("insert into homeland (cid,schoolyear,schoolclass,schoolterm,ouruser,paytype,fsitemid,amount) values('$cid','$schoolyear','$schoolclass','$schoolterm','$rolenumber','d','BalCarriedForward','$row_bravoss->balance')");
    echo "<br>".$cid." - ".$schoolyear." - class ".$schoolclass." - ".$schoolterm." - ".$row_bravoss->fsitemid." has balance - ".$row_bravoss->balance;

//then also put to bravos
$insert_bravos=$dbh->query("insert into bravos 
	(cid,fsitemid,payable,balance,lastuser,lastupdate,originalfee,schoolyear,schoolclass,schoolterm) 
	values($cid,'BalCarriedForward','$row_bravoss->balance','$row_bravoss->balance','$rolenumber','$todaynow','$row_bravoss->balance','$schoolyear','$schoolclass','$schoolterm')");
}
else {
	printColor($cid." BalCarriedForward already entered - ".$row_bravoss->balance." ".$todaynow, "green");}
//vvvv signed out with above else

}
else {
	printColor($cid." Has no balance from previous term - ".$todaynow, "red");}
//yyyy signed out with above else

      }while($row_children2=$result_children2->fetchObject());
   }//end of if children exist

?>