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

if(isset($_POST['tit']))
{
    $tit =addslashes($_POST['tit']);
    $bnk =addslashes($_POST['bnk']);
    $amt =addslashes($_POST['amt']);
    $cheq =addslashes($_POST['cheq']);
    $dat =addslashes($_POST['dat']);
    $des =addslashes($_POST['des']);
    
    
$sql=mysqli_query($con,"SELECT * FROM Imprest where Title='$tit'");
$num=mysqli_fetch_array($sql);
if($num>0)
{  
 echo 'Imprest Title already exist. The TITLE should be Unique.';
}else
{
$sql=mysqli_query($con,"INSERT INTO Imprest(Title,Amount,Date,Bank,Cheque,Des,User,EntryDate) VALUES('$tit','$amt','$dat','$bnk','$cheq','$des','$iduser','$timenow2')");
if($sql)
{
    $msg='created new Imprest '.'('.$tit.')';
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','save','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
    echo 'Sorry. Error occured while saving data';
}
}
}


if(isset($_POST['id']))
{
    $tit =addslashes($_POST['ti']);
    $bnk =addslashes($_POST['bnk']);
    $amt =addslashes($_POST['amt']);
    $cheq =addslashes($_POST['cheq']);
    $dat =addslashes($_POST['dat']);
    $des =addslashes($_POST['des']);
    $id =addslashes($_POST['id']);
    
     $yrr=substr($dat, 0, 4);
     $mm=substr($dat, 5, 2);

if($mm=='01'){$mm='January';}
if($mm=='02'){$mm='February';}
if($mm=='03'){$mm='March';}
if($mm=='04'){$mm='April';}
if($mm=='05'){$mm='May';}
if($mm=='06'){$mm='June';}
if($mm=='07'){$mm='July';}
if($mm=='08'){$mm='August';}
if($mm=='09'){$mm='September';}
if($mm=='10'){$mm='October';}
if($mm=='11'){$mm='November';}
if($mm=='12'){$mm='December';}
    
    
  $sql=mysqli_query($con,"SELECT * FROM Imprest where Title='$tit' AND id !='$id'");
$num=mysqli_fetch_array($sql);
if($num>0)
{  
 echo 'Imprest Title already exist. The TITLE should be Unique.';
}else
{
$sql=mysqli_query($con,"UPDATE Imprest SET Title='$tit',Amount='$amt',Date='$dat',Bank='$bnk',Cheque='$cheq',Des='$des',Month='$mm',Year='$yrr' WHERE id='$id'");
if($sql)
{
    $msg='updated Imprest: '.'('.$tit.')';
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','save','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
    echo 'Sorry. Error occured while saving data';
}
}
}


if(isset($_POST['idno']))
{
$id =addslashes($_POST['idno']);


$sql = "select * from Imprest where id='$id'";
$result = $con->query($sql);
while($row = $result->fetch_assoc()) 
{
  $tit=$row["Title"];  
}

$sql=mysqli_query($con,"DELETE FROM Imprest WHERE id='$id'");
if($sql)
{
    $msg='deleted Imprest >>> '.'('.$tit.')';
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','save','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
    echo 'Sorry. Error occured while saving data';
}
}


if(isset($_POST['impid']))
{
$id =addslashes($_POST['impid']);


$sql = "select * from Imprest2 where count='$id'";
$result = $con->query($sql);
while($row = $result->fetch_assoc()) 
{
  $amt=$row["Amount"];  
}

$sql=mysqli_query($con,"DELETE FROM Imprest2 WHERE count='$id'");
if($sql)
{
    $msg='deleted Imprest record of amount KES >>> '.'('.$amt.')';
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','save','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
    echo 'Sorry. Error occured while saving data';
}
}
?>