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

do {
//aaaa: if children exist
if($count_children4>0){
//PART 4 ---------DEDUCTING DISCOUNTS AND ADDING INCREMENTS. PAYCHANGER STUFF-----------
$result_paychange=$dbh->query("SELECT * from paychanger where cid='$cid'");
$row_paychange=$result_paychange->fetchObject();
$count_paychange=$result_paychange->rowCount();

//bbbb: if the child has a discount/increment in the table
if ($count_paychange>0){
	$adj_type2=$row_paychange->adjust_type;
	$adjustment2=$row_paychange->adjustment;
	$fsitemid2=$row_paychange->fsitemid;
echo $cid." Child has discount<br>";
$insert_homeland_c=$dbh->query("insert into homeland (cid,schoolyear,schoolclass,schoolterm,ouruser,paytype,fsitemid,amount) values('$cid','$schoolyear','$schoolclass','$schoolterm','$rolenumber','$row_paychange->adjust_type','$row_paychange->fsitemid','$adjustment2')");

//cccc: determining the paychange type, if the discount type is a deduction or increment
if($adj_type2=='d'){
$update_bravoss=$dbh->query("UPDATE bravos SET balance = balance + $adjustment2, payable= payable + $adjustment2  where fsitemid='$fsitemid2' and cid='$cid' and schoolyear='$schoolyear' and schoolterm='$schoolterm'");
$update_bravoss=$dbh->query("UPDATE bravos SET balance = balance + $adjustment2, payable= payable + $adjustment2  where fsitemid='fullpay' and cid='$cid' and schoolyear='$schoolyear' and schoolterm='$schoolterm'");
} 

elseif ($adj_type2=='c'){
$update_bravoss=$dbh->query("UPDATE bravos SET balance = balance - $adjustment2, payable= payable - $adjustment2 where fsitemid='$fsitemid2' and cid='$cid'");
$update_bravoss=$dbh->query("UPDATE bravos SET balance = balance - $adjustment2, payable= payable - $adjustment2 where fsitemid='fullpay' and cid='$cid'");
}
else {}//end bravos updates
//end cccc: paychange type decider

} //end bbbb: checker if child exists in paychange

   }while($row_children4=$result_children4->fetchObject());
}//end aaaa: if children exist 

