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


if(isset($_POST['id']))
{
 $name =addslashes($_POST['nam']);
 $act =addslashes($_POST['aka']);   
 $id =addslashes($_POST['id']);
 
  $da =addslashes($_POST['dat']);
  $ba =addslashes($_POST['bal']);
 
 $sql=mysqli_query($con,"SELECT * FROM bankdata where Bname='$name' AND Account='$act' AND id !='$id'");
$num=mysqli_fetch_array($sql);
if($num>0)
{
echo 'Bank details already exist!';
}else
{
    
    
 $sql=mysqli_query($con,"SELECT * FROM Bankreports where bid='$id' AND Year='$da'");
$num=mysqli_fetch_array($sql);
if($num>0)
{
$sql=mysqli_query($con,"UPDATE Bankreports SET Amount='$ba', Dat='$timenow2' where bid='$id' AND Year='$da'");
 
}else
{
  $sql=mysqli_query($con,"INSERT INTO Bankreports (Amount,Dat,bid,Year) values ('$ba','$timenow2','$id','$da')");  
}
    
    
$sql=mysqli_query($con,"UPDATE bankdata SET Bname='$name',Account='$act',Lastupdate='$timenow2',Balance='$ba',Dat='$da' where id='$id'");
if($sql)
{
   
$msgg='updated bank account 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 'Sorry! System Unable to update bank details';
}
}
}else
{
if($_POST['name'] != '')
{
$name =addslashes($_POST['name']);
$act =addslashes($_POST['act']);

$sql=mysqli_query($con,"SELECT * FROM bankdata where Bname='$name' AND Account='$act'");
$num=mysqli_fetch_array($sql);
if($num>0)
{
echo 'Bank details already exist!';
}else
{
$sql4=mysqli_query($con,"INSERT INTO bankdata(Bname,Account,Lastupdate) VALUES ('$name','$act','$timenow2')");
if($sql4)
{
$msgg='added new bank 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 submit data";	
}	
}
} 
}
?>