<?php 

include("config.php");
include("config2.php");
$userid = isset($_SESSION['userid']) ? $_SESSION['userid'] : '';

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Get permissions for this user
$sql = "SELECT permissions FROM user WHERE userid='$userid'";
$result = mysqli_query($conn, $sql);
if (!$result || mysqli_num_rows($result) == 0) {
    die("No permissions found for this user.");
}

$row = mysqli_fetch_assoc($result);
$permissions = explode(',', $row['permissions']); // array of allowed page names

function can_access($page_name, $permissions) {
    return in_array($page_name, $permissions);
}



if (can_access('bill_verify', $permissions)) {
	
$bill_id = $conn->real_escape_string($_GET['id']);
$room_no = $conn->real_escape_string($_GET['room_no']);
$aid = $conn->real_escape_string($_GET['aid']);

if($_GET[id]!=""){
	
//----------------------delete entry -------------------------
mysql_query("delete from bookings WHERE bill_id='$bill_id' and room_no='$room_no' and id='$aid' limit 1");
	
	
$aresult=mysql_query("select  sum(rprice)as rprice from bookings WHERE bill_id='$bill_id'");
$arow=mysql_fetch_array($aresult);
	
$dresult=mysql_query("select * from bill WHERE bill_id='$bill_id' limit 1");
$drow=mysql_fetch_array($dresult);

$total_price=$arow[rprice];
$vat_amount=$drow[vat_amount];
//----
if($drow[vat]>'0'){
$vat_amount=($total_price/100)*$drow[vat];
}	
//-------
$total_payeble=($total_price-$drow[discount])+$vat_amount;
$due=$total_payeble-$drow[payment];

$last_due=$total_payeble-$drow[all_payment_total];
	
mysql_query("update bill set total_price='$total_price',vat_amount='$vat_amount'
,total_payeble='$total_payeble'
,due='$due'
,last_due='$last_due'

where  bill_id='$bill_id' limit 1");

$aresult=mysql_query("select  * from bookings WHERE bill_id='$bill_id'");
$arow=mysql_fetch_array($aresult);
if($arow == ""){
//----------------------delete entry -------------------------
$dresult=mysql_query("select * from bill WHERE bill_id='$bill_id' limit 1");
$drow=mysql_fetch_array($dresult);
$discount=0;
$vat_amount=0;
$vat=0;
$total_payeble=0;
$due=$total_payeble-$drow[payment];
$last_due=$total_payeble-$drow[all_payment_total];
	
mysql_query("update bill set 
vat_amount='$vat_amount'
,discount='$discount'
,vat='$vat'
,total_payeble='$total_payeble'
,due='$due'
,last_due='$last_due'

where  bill_id='$bill_id' limit 1");
//----------------------end delete  -------------------------
	
}
}
	
	//----------------------end delete  -------------------------
header("location:hotel_new_confirm.php?id=$bill_id&info=Payment Updated");
}else{
header("location:hotel_new_confirm.php?id=$bill_id&info=bil Not Found $bill_id");
}
?>