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

 $user=$_SESSION['user_idd'];
 $iduser=$_SESSION['user_idd'];

if(isset($_POST['id']))
{
   
    $pid =addslashes($_POST['id']);
    $ch =addslashes($_POST['ch']);
    $am =addslashes($_POST['am']);
    $pa =addslashes($_POST['pa']);
    $da =addslashes($_POST['da']);
    $act =addslashes($_POST['act']);

    $yr=substr($da, 0, 4);
    $mm=substr($da, 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,"INSERT INTO prodata(proid,Dat,Amount,Item,Cheq, EntryDate,User,Year,Month,Bank) VALUES('$pid','$da','$am','$pa','$ch','$timenow2','$user','$yr','$mm','$act')");
if($sql)
{
    $msg='added new project expenditure';
 $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']))
{
    $pid =addslashes($_POST['idd']);
    $ch =addslashes($_POST['ch']);
    $am =addslashes($_POST['am']);
    $pa =addslashes($_POST['pa']);
    $da =addslashes($_POST['da']);
    $act =addslashes($_POST['act']);

    $yr=substr($da, 0, 4);
    $mm=substr($da, 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,"UPDATE prodata SET Dat='$da',Amount='$am',Item='$pa',Cheq='$ch',User='$user',Year='$yr',Month='$mm',Bank='$act' WHERE id='$pid'");
if($sql)
{
    $msg='updated a project expenditure';
 $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';
}
}
?>