<?php
header('Content-type: application/json');
include '../db/db.php';
session_start();
$log = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
$log_lang = htmlspecialchars(trim($_SESSION['lang']), ENT_QUOTES, 'UTF-8');
$menu = htmlspecialchars(trim($_SESSION['menu']), ENT_QUOTES, 'UTF-8');
$emp_id = htmlspecialchars(trim($_SESSION['emp_id']), ENT_QUOTES, 'UTF-8');

if (empty($log)) {
	$redirectUrl = "index.php";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";

} else {

	$sqlu = "SELECT * FROM user_priv_q WHERE emp_id='$emp_id' AND user='$log' AND menu_id='$menu'";
	$resultu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($resultu);

	if($countu == 1){
		$rowc = mysqli_fetch_assoc($resultu);
		$user_id = $rowc['user_id'];
		
		date_default_timezone_set('Asia/Colombo');
		$date_today = date("Y-m-d");

		$response = array();
		
		/****************************************** Insert *******************************************/		
		
		if(isset($_POST['veh_no'])){
			$date = htmlspecialchars(trim($_POST['date']), ENT_QUOTES, 'UTF-8');
			$dep_time = htmlspecialchars(trim($_POST['dep_time']), ENT_QUOTES, 'UTF-8');
			$arr_time = htmlspecialchars(trim($_POST['arr_time']), ENT_QUOTES, 'UTF-8');
			$dep_place = htmlspecialchars(trim($_POST['dep_place']), ENT_QUOTES, 'UTF-8');
			$veh_no = htmlspecialchars(trim($_POST['veh_no']), ENT_QUOTES, 'UTF-8');
			$distance = htmlspecialchars(trim($_POST['distance']), ENT_QUOTES, 'UTF-8');
			$paid_amount = htmlspecialchars(trim($_POST['paid_amount']), ENT_QUOTES, 'UTF-8');
			$travel_exp = htmlspecialchars(trim($_POST['travel_exp']), ENT_QUOTES, 'UTF-8');
			$comb_allow = htmlspecialchars(trim($_POST['comb_allow']), ENT_QUOTES, 'UTF-8');
			$emp_bata = htmlspecialchars(trim($_POST['emp_bata']), ENT_QUOTES, 'UTF-8');
			
			$sql = "INSERT INTO travel_expenses SET
`date` = '$date',
`dep_time` = '$dep_time',
`arr_time` = '$arr_time',
`dep_place` = '$dep_place',
`veh_no` = '$veh_no',
`distance` = '$distance',
`paid_amount` = '$paid_amount',
`travel_exp` = '$travel_exp',
`comb_allow` = '$comb_allow',
`emp_bata`= '$emp_bata'";	
			$result = mysqli_query($conn, $sql);
	
			if($result) {
				$response['status'] = 'ok';
			
			} else {
				$response['status'] = 'error';
			}
			echo json_encode($response);
		}

		/******************************************* Update ******************************************/
		
		if(isset($_POST['id_holder'])){
			$date = htmlspecialchars(trim($_POST['date_up']), ENT_QUOTES, 'UTF-8');
			$dep_time = htmlspecialchars(trim($_POST['dep_time_up']), ENT_QUOTES, 'UTF-8');
			$arr_time = htmlspecialchars(trim($_POST['arr_time_up']), ENT_QUOTES, 'UTF-8');
			$dep_place = htmlspecialchars(trim($_POST['dep_place_up']), ENT_QUOTES, 'UTF-8');
			$veh_no = htmlspecialchars(trim($_POST['veh_no_up']), ENT_QUOTES, 'UTF-8');
			$distance = htmlspecialchars(trim($_POST['distance_up']), ENT_QUOTES, 'UTF-8');
			$paid_amount = htmlspecialchars(trim($_POST['paid_amount_up']), ENT_QUOTES, 'UTF-8');
			$travel_exp = htmlspecialchars(trim($_POST['travel_exp_up']), ENT_QUOTES, 'UTF-8');
			$comb_allow = htmlspecialchars(trim($_POST['comb_allow_up']), ENT_QUOTES, 'UTF-8');
			$emp_bata = htmlspecialchars(trim($_POST['emp_bata_up']), ENT_QUOTES, 'UTF-8');
			$id = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
			
			$sql = "UPDATE travel_expenses SET
`date` = '$date',
`dep_time` = '$dep_time',
`arr_time` = '$arr_time',
`dep_place` = '$dep_place',
`veh_no` = '$veh_no',
`distance` = '$distance',
`paid_amount` = '$paid_amount',
`travel_exp` = '$travel_exp',
`comb_allow` = '$comb_allow',
`emp_bata`= '$emp_bata' WHERE id = $id";       	
			$result = mysqli_query($conn, $sql);

          	if ($result) {
				$response['status'] = 'ok';
			}else{
				$response['status'] = 'error';
			}

			echo json_encode($response);
		}
		
		/***************************************** Get Dataset ********************************************/		

		if (array_key_exists('get_dataset', $_POST)) {

			$reg_id = htmlspecialchars(strip_tags(trim($_POST['reg_id'])), ENT_QUOTES, 'UTF-8');
			
			$data = array();
			$sql = mysqli_query($conn, "SELECT * FROM travel_expenses WHERE id=$reg_id");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}
		
		/***************************************** Remove ********************************************/		

		if (array_key_exists('remove_reg', $_POST)) {

			$id = htmlspecialchars(trim($_POST['reg_id']), ENT_QUOTES, 'UTF-8');
			$sql2 = mysqli_query($conn, "DELETE FROM travel_expenses WHERE id=$id AND flag=0");

			if ($sql2) {
				$response['status'] = 'ok';
			}else{
				$response['status'] = 'error';
			}
			
			echo json_encode($response);
		}
		
		/******************************************* Create Voucher  ******************************************/
		
		if(isset($_POST['id_up'])){
			$chq_no = htmlspecialchars(trim($_POST['chq_no']), ENT_QUOTES, 'UTF-8');
			$exp_detail = htmlspecialchars(trim($_POST['exp_detail']), ENT_QUOTES, 'UTF-8');
			$off_name = htmlspecialchars(trim($_POST['off_name']), ENT_QUOTES, 'UTF-8');
			$position = htmlspecialchars(trim($_POST['position']), ENT_QUOTES, 'UTF-8');
			$department = htmlspecialchars(trim($_POST['department']), ENT_QUOTES, 'UTF-8');
			$annual_sal = htmlspecialchars(trim($_POST['annual_sal']), ENT_QUOTES, 'UTF-8');
			$approve_all = htmlspecialchars(trim($_POST['approve_all']), ENT_QUOTES, 'UTF-8');
			$combined_all = htmlspecialchars(trim($_POST['combined_all']), ENT_QUOTES, 'UTF-8');
			$departure = htmlspecialchars(trim($_POST['departure']), ENT_QUOTES, 'UTF-8');
			$reason = htmlspecialchars(trim($_POST['reason']), ENT_QUOTES, 'UTF-8');
			$req_amount = htmlspecialchars(trim($_POST['req_amount']), ENT_QUOTES, 'UTF-8');
			$adv_pay = htmlspecialchars(trim($_POST['adv_pay']), ENT_QUOTES, 'UTF-8');			
			$id = htmlspecialchars(trim($_POST['id_up']), ENT_QUOTES, 'UTF-8');
			
			$group_no = mysqli_fetch_assoc(mysqli_query($conn, "SELECT vou_id FROM group_id_tbl"));
			$vou_id = $group_no['vou_id'];			
			
			$sql = "UPDATE travel_expenses SET vou_no='$vou_id',
`chq_no` = '$chq_no',
`exp_detail` = '$exp_detail',
`officer_name` = '$off_name',
`position` = '$position',
`department` = '$department',
`annual_sal` = '$annual_sal',
`approve_all` = '$approve_all',
`combined_all` = '$combined_all',
`departure` = '$departure',
`reason` = '$reason', `req_amount`= '$req_amount', `adv_pay`= '$adv_pay' WHERE id = $id";

			$sqlp1 = mysqli_query($conn, "SELECT vou_no FROM travel_expenses WHERE vou_no='$vou_id'");
			$sqlp2 = mysqli_query($conn, "SELECT chq_no FROM travel_expenses WHERE chq_no='$chq_no'");
			
			if(mysqli_num_rows($sqlp1) == 0) {
				if(mysqli_num_rows($sqlp2) == 0) {
					$result = mysqli_query($conn, $sql);
		
					if ($result) {
						$response['status'] = 'ok';
						$vou_id2 = $vou_id+1;
						mysqli_query($conn, "UPDATE group_id_tbl SET vou_id='$vou_id2'");
						
					} else {
						$response['status'] = 'error';
					}
					
				} else {
					$response['status'] = 'ext2'; // chq_no exist
				}
				
			} else {
				$response['status'] = 'ext1'; // vou_no exist
			}

			echo json_encode($response);
		}
		
		/******************************************* Update Voucher  ******************************************/
				
		if(isset($_POST['vou_id_up'])){
			$chq_no = htmlspecialchars(trim($_POST['chq_no_up']), ENT_QUOTES, 'UTF-8');
			$exp_detail = htmlspecialchars(trim($_POST['exp_detail_up']), ENT_QUOTES, 'UTF-8');
			$off_name = htmlspecialchars(trim($_POST['off_name_up']), ENT_QUOTES, 'UTF-8');
			$position = htmlspecialchars(trim($_POST['position_up']), ENT_QUOTES, 'UTF-8');
			$department = htmlspecialchars(trim($_POST['department_up']), ENT_QUOTES, 'UTF-8');
			$annual_sal = htmlspecialchars(trim($_POST['annual_sal_up']), ENT_QUOTES, 'UTF-8');
			$approve_all = htmlspecialchars(trim($_POST['approve_all_up']), ENT_QUOTES, 'UTF-8');
			$combined_all = htmlspecialchars(trim($_POST['combined_all_up']), ENT_QUOTES, 'UTF-8');
			$departure = htmlspecialchars(trim($_POST['departure_up']), ENT_QUOTES, 'UTF-8');
			$reason = htmlspecialchars(trim($_POST['reason_up']), ENT_QUOTES, 'UTF-8');
			$req_amount = htmlspecialchars(trim($_POST['req_amount_up']), ENT_QUOTES, 'UTF-8');
			$adv_pay = htmlspecialchars(trim($_POST['adv_pay_up']), ENT_QUOTES, 'UTF-8');			

			$id = htmlspecialchars(trim($_POST['vou_id_up']), ENT_QUOTES, 'UTF-8');
			
			$sql = "UPDATE travel_expenses SET `chq_no` = '$chq_no',
`exp_detail` = '$exp_detail',
`officer_name` = '$off_name',
`position` = '$position',
`department` = '$department',
`annual_sal` = '$annual_sal',
`approve_all` = '$approve_all',
`combined_all` = '$combined_all',
`departure` = '$departure',
`reason` = '$reason',
`req_amount`= '$req_amount', `adv_pay`= '$adv_pay' WHERE id = $id"; 

			$sqlp1 = mysqli_query($conn, "SELECT chq_no FROM travel_expenses WHERE chq_no='$chq_no' AND id!=$id");
			if(mysqli_num_rows($sqlp1) == 0) {     	
				$result = mysqli_query($conn, $sql);
	
				if ($result) {
					$response['status'] = 'ok';
				}else{
					$response['status'] = 'error';
				}
			
			} else {
				$response['status'] = 'ext2'; // chq_no exist
			}

			echo json_encode($response);
		}			
		
		/*************************************************************************************/		

	} else{
		$redirectUrl = "index.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	
	}

}
?>