<?php
session_start();
$iduser=$_SESSION['user_idd'];
include('../include/config.php');
$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');
error_reporting(0);


if(isset($_POST['id']))
{
    $pid =addslashes($_POST['id']);
    $yr =addslashes($_POST['yr']);
    $trm =addslashes($_POST['trm']);
    
    $sql = "select * from pupils where count='$pid'";
    $result = $con->query($sql);
    while($row = $result->fetch_assoc()) 
    {
    $nam=$row["Names"];
    $adm=$row["AdmNo"];
    $grd=$row["Claz"];
    $bo=$row["Boarder"];
    }  
    
    $sql=mysqli_query($con,"SELECT * FROM Fees2 where Year='$yr' AND Term='$trm'");
    $num=mysqli_fetch_array($sql);
    if($num>0)
    {  
     $sqll=mysqli_query($con,"SELECT * FROM Fees2 where Year='$yr' AND Term='$trm' AND pid='$pid'");
    $num=mysqli_fetch_array($sqll);
    if($num>0)
    {
    echo 'This child already exist in the selected period.';
    }else
    {
    $sql = "select * from Fees2 where Grad='$grd' AND Year='$yr' AND Term='$trm'";
    $result = $con->query($sql);
    while($row = $result->fetch_assoc()) 
    {
     $tf=$row["TutionF"];
     $mot=$row["MotF"];
     $cod=$row["Cod"];
     $cou=$row["count"];
    }
      
     $sqlll=mysqli_query($con,"INSERT INTO Fees2(pid,Year,Term,Grad,TutionF,MotF,Cod,Boarder,count) VALUES('$pid','$yr','$trm','$grd','$tf','$mot','$cod','$bo','$cou')");
    if($sqlll)
    {
    $msg='added new student to fee statement for '.$trm.' '.$yr;
    $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','save','$month') ");  
        
    echo 'success';
    }else
    {
    echo 'System failed to process the request';
    }
    }
    }else
    {
        echo 'Fee statement for selected period not created !';
    }
}
?>