<?php
include('../include/config.php');
//error_reporting(0);
session_start();
$iduser=$_SESSION['user_idd'];
$date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
$timenow=$date->format('Y-m-d H:i:s');
$timenow2=$date->format('Y-m-d');
$month=$date->format('M');

$fid='';

function generatecode() {
	$chars = "003232303232023232023456789";
	srand((double)microtime()*1000000);
	$i = 0;
	$pass = '' ;
	while ($i <= 5) {

		$num = rand() % 33;
		$tmp = substr($chars, $num, 1);
		$pass = $pass . $tmp;
		$i++;
	}
	return $pass;
}

$fid=generatecode();
$fid='BK_'.$fid;

if(isset($_POST['trm']))
{
$trm=addslashes($_POST['trm']);
$yr=addslashes($_POST['yr']);

$sqll=mysqli_query($con,"select * from Fees2 where Year='$yr' AND Term='$trm'");
$num=mysqli_fetch_array($sqll);
if($num>0)
{
  echo 'Sorry you have already created fee structure for '.$trm.' '.$yr;  
}else
{
    
if($trm=='Term One')
{
    $trmm='Term Three';
    $yrr=(int)$yr;
    $yr2=$yrr-1;
    
}else if($trm=='Term Two')
{
    $trmm='Term One';
    $yr2=$yr;
}else if($trm=='Term Three')
{
    $trmm='Term Two';
    $yr2=$yr;
}   
    
    
$sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='BABY CLASS'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{ 
  echo 'Make sure you save Fee balances for the previous Term for BABY CLASS before proceeing';  
}else
{
 $sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='PP1'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{ 
   echo 'Make sure you save Fee balances for the previous Term for PP1 before proceeing';   
}else
{
$sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='PP2'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{  
   echo 'Make sure you save Fee balances for the previous Term for PP2 before proceeing';   
}else
{
$sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='GRADE 1'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{  
   echo 'Make sure you save Fee balances for the previous Term for Grade 1 before proceeing';   
}else
{ 
 $sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='GRADE 2'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{  
   echo 'Make sure you save Fee balances for the previous Term for Grade 2 before proceeing';   
}else
{   
$sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='GRADE 3'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{  
   echo 'Make sure you save Fee balances for the previous Term for Grade 3 before proceeing';   
}else
{
$sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='GRADE 4'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{  
   echo 'Make sure you save Fee balances for the previous Term for Grade 4 before proceeing';   
}else
{
    
$sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='GRADE 5'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{  
   echo 'Make sure you save Fee balances for the previous Term for Grade 5 before proceeing';   
}else
{
$sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='GRADE 6'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{  
   echo 'Make sure you save Fee balances for the previous Term for Grade 6 before proceeing';   
}else
{
$sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='GRADE 7'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{  
   echo 'Make sure you save Fee balances for the previous Term for Grade 7 before proceeing';   
}else
{
$sqll=mysqli_query($con,"select * from Balances where Year='$yr2' AND Term='$trmm' AND Grad='GRADE 8'");
$num=mysqli_fetch_array($sqll);
if($num<1)
{  
   echo 'Make sure you save Fee balances for the previous Term for Grade 8 before proceeing';   
}else
{
$quer=mysqli_query($con,"select * from Fees");
$num=mysqli_fetch_array($quer);
if($num>0)
{
 $bo='';  
$queryy=mysqli_query($con,"select * from bofee");
while($row=mysqli_fetch_array($queryy))
{
$bo=htmlentities($row['Fee']);
}

$queryyy=mysqli_query($con,"select * from motifee where Grad='Lower'");
while($row=mysqli_fetch_array($queryyy))
{
$mo1=htmlentities($row['Amount']);
}

$que=mysqli_query($con,"select * from motifee where Grad='Upper'");
while($row=mysqli_fetch_array($que))
{
$mo2=htmlentities($row['Amount']);
}

$sq=mysqli_query($con,"UPDATE pupils SET Moti='$mo1' where Claz='GRADE 1'");
$sq=mysqli_query($con,"UPDATE pupils SET Moti='$mo1' where Claz='GRADE 2'");
$sq=mysqli_query($con,"UPDATE pupils SET Moti='$mo1' where Claz='GRADE 3'");
$sq=mysqli_query($con,"UPDATE pupils SET Moti='$mo1' where Claz='GRADE 4'");
$sq=mysqli_query($con,"UPDATE pupils SET Moti='$mo1' where Claz='GRADE 5'");
$sq=mysqli_query($con,"UPDATE pupils SET Moti='$mo1' where Claz='GRADE 6'");
$sq=mysqli_query($con,"UPDATE pupils SET Moti='$mo2' where Claz='GRADE 7'");
$sq=mysqli_query($con,"UPDATE pupils SET Moti='$mo2' where Claz='GRADE 8'");
$sq=mysqli_query($con,"UPDATE pupils SET Moti='$mo2' where Claz='GRADE 9'");


//Get auto increment No.
$queryyy=mysqli_query($con,"select * from Fees2 where count !='' order BY id DESC LIMIT 1");
while($row=mysqli_fetch_array($queryyy))
{
$auto=htmlentities($row['count']);
$mwa=htmlentities($row['Year']);
$tam=htmlentities($row['Term']);
}

$auto=$auto+1;
$sq=mysqli_query($con,"UPDATE pupils SET Year='$yr',Term='$trm',Cod='$fid',Dev='1500',AutoNo='$auto'");
if($sq)
{
   
}


//Working code to udate multiple rows data in one table from another table
$result = mysqli_query($con,"SELECT * FROM Fees2 where Year='$mwa' AND Term='$tam'");
$num_rows1 = mysqli_num_rows($result);
{
$count=htmlentities($num_rows1); 
}

for($i=0;$i<$count;$i++)
{
$query=mysqli_query($con,"SELECT * FROM Fees2 where Year='$mwa' AND Term='$tam'");
while($row=mysqli_fetch_array($query))
{
$tra=$row['TranF'];
$abks=$row['TxtF'];
$pid=$row['pid']; 

$querysql = mysqli_query($con,"UPDATE pupils SET Tran='$tra',ABKs='$abks' where count='$pid'");
if($querysql)
{
  echo 'good';
}
}
}

$sql=mysqli_query($con,"INSERT INTO Fees2(Year,Term,Grad,pid,Boarder,TutionF,TranF,Cod,count,MotF,Dev,TxtF) 
SELECT pupils.Year,pupils.Term,pupils.Claz,pupils.count,pupils.Boarder,Fees.Tution,pupils.Tran,pupils.Cod,pupils.AutoNo,pupils.Moti,pupils.Dev,pupils.ABKs from pupils LEFT join Fees ON Fees.Grade=pupils.Claz
WHERE pupils.Status='Active'");
if($sql)
{
$msg='created new fee payement period: ' .$trm.' '.$yr;
//$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','save','$month')");

echo "saved";
}else
{
echo 'Unable to save';
}
   

}else
{
 echo 'You must set Tution fees first for all classes';  
}
}
}
}
}
}
}
}
}
}
}
}
}
}
?>