<?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');
$yr=$date->format('Y');

if(isset($_POST['bus']))
{
$bus=addslashes($_POST['bus']);
$cont=addslashes($_POST['cont']);
$cost=addslashes($_POST['cost']);
$dat=addslashes($_POST['dat']);
$cli=addslashes($_POST['client']);
$info=addslashes($_POST['info']);
$hfro=addslashes($_POST['hfro']);
$hto=addslashes($_POST['hto']);
$bnk=addslashes($_POST['bk']);

$yrr=substr($hfro, 0, 4);
$mm=substr($hfro, 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';}

$query=mysqli_query($con,"SELECT * FROM busincome WHERE busid='$bus' AND Hiredate='$dat' AND client='$cli'");
$num=mysqli_fetch_array($query);
if($num>0)
{
echo "Simiral details already exist in the system";
}else
{
$sql=mysqli_query($con,"INSERT INTO busincome(busid,client,contact,Hiredate,cost,EntryDate,user,Month,Year,Des,Hfrom,Hto,Bank) values('$bus','$cli','$cont','$dat','$cost','$timenow2','$iduser','$mm','$yrr','$info','$hfro','$hto','$bnk')");
if($sql)
{
$msg='added new bus hire details';
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','save','$month')");

echo "Successfully saved";
}else
{
echo 'Unable to save';
}
}
} else if(isset($_POST['id']))
{
$bus=addslashes($_POST['bid']);
$cont=addslashes($_POST['cont']);
$cost=addslashes($_POST['cost']);
$dat=addslashes($_POST['dat']);
$cli=addslashes($_POST['client']);
$info=addslashes($_POST['info']);
$hfro=addslashes($_POST['hfro']);
$hto=addslashes($_POST['hto']);
$id=addslashes($_POST['id']);
$bk=addslashes($_POST['bk']);

$yrr=substr($hfro, 0, 4);
$mm=substr($hfro, 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';}

$query=mysqli_query($con,"SELECT * FROM busincome WHERE busid='$bus' AND Hiredate='$dat' AND client='$cli' AND id !='$id'");
$num=mysqli_fetch_array($query);
if($num>0)
{
echo "Simiral details already exist in the system";
}else
{
$sql=mysqli_query($con,"UPDATE busincome SET busid='$bus',client='$cli',contact='$cont',Hiredate='$dat',cost='$cost',Month='$mm',Year='$yrr',Des='$info',Hfrom='$hfro',Hto='$hto',Bank='$bk' where id='$id'");
if($sql)
{
$msg='updated bus hire details';
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','save','$month')");

echo "update successful";
}else
{
echo 'Unable to update';
}
}
} else if(isset($_POST['idd']))
{
$id=$_POST['idd'];

$sql=mysqli_query($con,"DELETE FROM busincome WHERE id='$id'");
if($sql)
{
$msg='deleted vehicle hiring data';
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','save','$month')");

echo "successful";
}
}
?>