<?php
//NewYear Term1 changer
/*in summary, this does 3 things 
1-update school year term, update child new classes
2-carry BalCarriedForward from previous terms
3-insert fees from structure in homeland and bravos
4-Apply recurrent discounts
important to make sure each class has fees set already!!!
*/
include('../core/kfunctions.php');

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

//---------------PART 1 - CHANGING YEAR, TERM AND PROMOTING CHILDREN---------------------//

$schoolyear=$_POST['schoolyear'];
$schoolterm=$_POST['schoolterm'];


//update current term and year in scrap
$update_term=$dbh->query("UPDATE scrap SET item='$schoolterm',item2='$schoolyear' WHERE type='currentterm' ");

//update current term and year in children
$update_children=$dbh->query("UPDATE children SET term='$schoolterm',schoolyear='$schoolyear' WHERE status='2' ");

//add a class to all children 8,9,1-6, change students in 10 to 1
$result_children1=$dbh->query("select * from children where status='2'");
$row_children1=$result_children1->fetchObject();
$count_children1=$result_children1->rowCount();

if($count_children1>0){
    do{
    	$schoolclass=$row_children1->schoolclass;
    	$cid=$row_children1->cid;
//if top class(8), move to p1 but for the rest just +1
 if ($schoolclass==10){$newschoolclass='1'; $newstatus='2';} //keep active
 elseif ($schoolclass==7){$newschoolclass='7'; $newstatus='2';}//if p7, stay in p7, status inactive
 else {$newschoolclass=$schoolclass+1; $newstatus='2';} //all other classes add 1 and active
    	
    	
    	echo $row_children1->fname." - ".$cid." - curclass".$schoolclass."- newclass:".$newschoolclass."-".$newstatus."<br>";

$update_childclass=$dbh->query("update children set schoolclass='$newschoolclass',status='$newstatus' where cid='$cid'");

       }while($row_children1=$result_children1->fetchObject());
   }

//get all children from children.sql and inside that loop, 
//2-copy their fsitems.sql where not new child items and 
//3-for each child, check if they have discount and apply and
//4-copy them to homeland
//5-do bravos math

   //approach 2
   /*
   select from children
   ----select from bravos where balance is not 0 for this child and fullpay
   ----copy fullpay as bal carried forward item to homeland
   ----rename bravos fullpay as balcarriedfwd
   ----delete all others not fullpay
   ----insert new items
   ----insert new fullpay
   */


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

//echo "<div class='col-lg-12'><br><div class='alert alert-success'>Success: Learners submitted to Homeland and Bravos updated. </div></div>";
/*
//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();

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')");


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 paychange if insert

*/


 }//end of isset

echo "
<form method='post'>
<p>
 <div class='col-lg-9'><b style='color:maroon;'>Select Current Year</b>
<select class='form-control' name='schoolyear' required>
<option value=''>Select Year</option>
<option value='22'>2022</option>
<option value='23'>2023</option>

</select>
</div><p>

<div class='col-lg-9'><b style='color:maroon;'>Current Term</b>
<select class='form-control' name='schoolterm' required>
<option value=''>Select Term</option>
<option value='1'>Term 1</option>
<option value='2'>Term 2</option>
<option value='3'>Term 3</option>
</select>

<div class='col-lg-9'>
<input type='submit' class='btn btn-sm btn-warning' name='submit_termyear' value='Next'>
</div>
</div>


</form>
";

/*if year selected is similar to current year in scrap but term is different, 
1-update term for all students with goodstatus in children 
2-update current term in scrap.
3-show current fees structure in editable format
4-confirm submission. update structure table
5-show current ongoing discounts in editable format
6-confirm submission. update discounts table
7-insert children to homeland with structure+-discount
8-update bravos with homeland+currentfee
*/

/*if year selected is different to current year in scrap and term is 1, 
qqqq 1-update term for all students with goodstatus in children 
qqqq 2-update current term in scrap.
3-show current fees structure in editable format
4-confirm submission. update structure table
5-show current ongoing discounts in editable format
6-confirm submission. update discounts table
7-insert children to homeland with structure+-discount
8-update bravos with homeland+currentfee
*/


?>