<?php
require_once __DIR__ . '/db_config.php';
require_once __DIR__ . '/image_upload.php';

$response = array();
  
// check for required fields
if (isset($_POST['data']))  {
    
$json=json_decode($_POST['data'],true);
$img=new ImageUpload();

$mail= $json['mail'];
$cname=$json["cname"];
$role=$json["role"];
$id=$json["id"];
$from=$json["from"];
$to=$json["to"];
$amount=$json["amount"];
$type=$json["type"];
$path=$img->uploadImage($cname,$id,$json["image"]);

date_default_timezone_set('Asia/Calcutta');
$dt=date('Y-m-d H:i:s');

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

$idQuery="SELECT generateExpenseId('$cname') AS id";
$idResult=mysqli_query($con,$idQuery);
if(!empty($idResult) && mysqli_num_rows($idResult) > 0){
    $row = mysqli_fetch_array($idResult,MYSQLI_ASSOC);
    $expId=$row["id"];
}

$query="INSERT INTO hcm_expense(company,exp_id,tr_id,exp_from_location,exp_to_location,exp_amount,exp_type,img_path,exp_datetime, exp_created_by, exp_created_type) VALUES ('$cname','$expId','$id','$from','$to','$amount','$type','$path','$dt','$mail','$role')";
$response["s"]=mysqli_query($con,$query);

mysqli_close($con);

} 
    echo json_encode($response);

?>