<?php
include('../include/config.php');
//error_reporting(0);
session_start();
$iduser= $_SESSION['user_idd'];


if($_POST['grd'] != '')
{
    
 if(isset($_POST['id'])) 
 {
$id =addslashes($_POST['id']); 
$sir =addslashes($_POST['sir']);
$nm =addslashes($_POST['nm']);
$adm =addslashes($_POST['adm']);
$gen = $_POST['gen'];
$disa =addslashes($_POST['disa']);
$clas = addslashes($_POST['clas']);
$pt = addslashes($_POST['pt']);
$pn = addslashes($_POST['pn']);
$pc = addslashes($_POST['pc']);
$pe = addslashes($_POST['pe']);
$info=addslashes($_POST["info"]);

//$tax='';
$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');

$sql=mysqli_query($con,"SELECT * FROM pupils where AdmNo='$adm' AND count !='$id'");
$num=mysqli_fetch_array($sql);
if($num>0)
{
  echo "Sorry! You can't assign two pupils same admission number"; 
}else
{
$sql=mysqli_query($con,"UPDATE pupils SET Sirname='$sir',Names='$nm',Claz='$clas',Gender='$gen',AdmNo='$adm',PType='$pt',PName='$pn',Disability='$disa',Contact='$pc',Email='$pe',Des='$info' where count='$id'");
if($sql)
{
//$msgg='updated company profile details';
//$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msgg','$timenow','$timenow2','update','$month')");
// if($sql3)
// { }
echo "success";
}else
{
echo "Unable to update data";	
}	
}
 }else if(isset($_POST['iddd']))
 {
 $tu = addslashes($_POST['tu']);
 $id=addslashes($_POST["iddd"]);
 
 echo 'received';

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


$sql=mysqli_query($con,"UPDATE Fees SET Tution='$tu' WHERE count='$id'");
if($sql)
{
//$msgg='updated company profile details';
//$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msgg','$timenow','$timenow2','update','$month')");
// if($sql3)
// { }
echo "updated";
}else
{
echo "Unable to save data";	
}	
   
     
     
 }else
  {
$tu = addslashes($_POST['tu']);
$grd=addslashes($_POST["grd"]);

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

$sql=mysqli_query($con,"SELECT * FROM Fees where Grade='$grd'");
$num=mysqli_fetch_array($sql);
if($num>0)
{
  echo "Records already exist. You can only update"; 
}else
{
$sql=mysqli_query($con,"INSERT INTO Fees(Grade,Tution) values('$grd','$tu')");
if($sql)
{
//$msgg='updated company profile details';
//$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msgg','$timenow','$timenow2','update','$month')");
// if($sql3)
// { }
echo "success";
}else
{
echo "Unable to save data";	
}	
}
}
//echo $insert?'ok':'err';
}else if($_POST['idd'] != '')
{
$ad =addslashes($_POST['ad']);
$tu =addslashes($_POST['tu']);
$bo =addslashes($_POST['bo']);
$tr = $_POST['tr'];
$dia =addslashes($_POST['dia']);
$mo = addslashes($_POST['mo']);
$un = addslashes($_POST['un']);
$pe = addslashes($_POST['pe']);
$bk = addslashes($_POST['bk']);
$idd=addslashes($_POST["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');

//$tot=$ad+$tu+$bo+$tr+$dia+$mo+$un+$pe+$bk;


$sql=mysqli_query($con,"UPDATE Fees SET Tution='$tu',Adm='$ad',Boarding='$bo',Transport='$tr',Uniform='$mo',Diaries='$dia',Motivation='$mo',PExpenses='$pe',Texts='$bk',LastUpdate='$timenow',User='$iduser' where count='$idd'");
if($sql)
{
    echo 'success'; 
}else
{
    echo 'Sorry. Error occured while updating data';
}
}else if(isset($_POST['typ']))
{
$amt =addslashes($_POST['amt']); 
$cate =addslashes($_POST['cate']);
$typ =addslashes($_POST['typ']);
$txt =addslashes($_POST['txt']);

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

$sql=mysqli_query($con,"SELECT * FROM Fees3 where Type='$typ' AND Cat ='$cate'");
$num=mysqli_fetch_array($sql);
if($num>0)
{
  echo "Sorry! Fee type already exist"; 
}else
{
$sql=mysqli_query($con,"INSERT INTO Fees3(Type,Cat,Amount,LastUpdate,Infor) VALUES('$typ','$cate','$amt','$timenow2','$txt')");
if($sql)
{
 $msgg='added new fees details';
 $sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msgg','$timenow','$timenow2','save','$month')");
// if($sql3)
// { }
echo "success";
}else
{
echo "Unable to update data";	
}	
}
}
?>