<?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']);
 
$sql=mysqli_query($con,"SELECT * FROM expenses where Item='$tit'");
$num=mysqli_fetch_array($sql);
if($num>0)
{  
 echo 'Item already exist !';
}else
{
$sql=mysqli_query($con,"INSERT INTO expenses (Item,EntryDate) VALUES('$tit','$timenow2')");
if($sql)
{
    $msg='added new expenses item: '.'('.$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';
}
}
}else if(isset($_POST['idd']))
{

$tit =addslashes($_POST['tito']);
$id =addslashes($_POST['idd']);

$sql=mysqli_query($con,"SELECT * FROM expenses where Item='$tit' AND Count !='$id'");
$num=mysqli_fetch_array($sql);
if($num>0)
{  
 echo 'Category already exist !';
}else
{
$sql=mysqli_query($con,"UPDATE expenses SET Item='$tit' WHERE Count='$id'");
if($sql)
{
    $msg='updated expenditure category: '.'('.$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';
}
}
  
  
  
  
  
  
  
   
    
}else{
$id =addslashes($_POST['id']);
$sql=mysqli_query($con,"DELETE from expenses where Count='$id'");
if($sql)
{
    $msg='deleted expenses item';
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','delete','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
    echo 'Sorry. Error occured while saving data';
}

}


if(isset($_POST['countt']))
{
$id =addslashes($_POST['countt']);
$sql=mysqli_query($con,"DELETE from IncomeCat where count='$id'");
if($sql)
{
    $msg='deleted an income category item';
 $sql2=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month) VALUES('$iduser','$msg','$timenow','$timenow2','delete','$month') ");
 if($sql2)
 {
    echo 'success'; 
 }
}else
{
    echo 'Sorry. Error occured while saving data';
}

}
?>