<?php 

include("config.php");
include("config2.php");

if($_POST[total_price]>"0"){
	
	
$tboking = $conn->real_escape_string($_POST['tboking']);
$vat_amount = $conn->real_escape_string($_POST['vat_amount']);	
$vat = $conn->real_escape_string($_POST['vat']);	
$total_price = $conn->real_escape_string($_POST['total_price']);	
$discount = $conn->real_escape_string($_POST['discount']);	
	
$total_payeble = $conn->real_escape_string($_POST['total_payeble']);	
$payment_amount = $conn->real_escape_string($_POST['payment_amount']);	
$pay_methode = $conn->real_escape_string($_POST['pay_methode']);	
$due = $conn->real_escape_string($_POST['due']);
	
	$refr = $conn->real_escape_string($_POST['refr']);
	$refr_id = $conn->real_escape_string($_POST['refr_id']);
	$rcom = $conn->real_escape_string($_POST['rcom']);
	$unote = $conn->real_escape_string($_POST['unote']);
 
$cdate=dtformat($_POST[cdate]);
//----------------------customer entry -------------------------
	
if($_POST[nid2]!=""){
$dresult=mysql_query("select * from customer WHERE nid='$_POST[nid2]' ");
$drow=mysql_fetch_array($dresult);
}else{
$dresult=mysql_query("select * from customer WHERE nid='' ");
$drow=mysql_fetch_array($dresult);
}
	

$cid=$drow[id];	
	
if($drow == ""){

$dresult=mysql_query("select * from customer WHERE personal_phn='$_POST[personal_phn]' ");
$drow=mysql_fetch_array($dresult);	
$cid=$drow[id];	

if($drow == ""){
	
mysql_query("ANALYZE TABLE customer");
	
$kresult = mysql_query("SHOW TABLE STATUS LIKE 'customer'");
$krow = mysql_fetch_array($kresult);
$cid=$krow['Auto_increment'];

	
$uploadFolder = "uploads/";	
if (!file_exists($uploadFolder)) {
    mkdir($uploadFolder, 0777, true);
}
	
	
$filename = $_FILES['file1']['name'];
if($filename!=""){

$ext = substr($filename, strrpos($filename, '.') + 1); 
$a=$id."_1.".$ext;

copy ($_FILES['file1']['tmp_name'], "uploads/".$a);
}else{$a="";}
	
$filename = $_FILES['file2']['name'];
if($filename!=""){

$ext = substr($filename, strrpos($filename, '.') + 1); 
$b=$id."_2.".$ext;

copy ($_FILES['file2']['tmp_name'], "uploads/".$b);
}else{$b="";}

	
mysql_query("INSERT INTO customer (customer_name, contact_person, personal_phn, nid, personal_email, address,file1,file2) VALUES ( '$_POST[customer_name]','$_POST[contact_person]','$_POST[personal_phn]','$_POST[nid2]','$_POST[personal_email]','$_POST[address]','$a','$b')");
	
}
}
//----------------------end customer entry -------------------------

	//----------------------bill id -------------------------
	$eresult=mysql_query("select * from bill_id order by id asc limit 1");
	$erow=mysql_fetch_array($eresult);
	$bill_id=$erow[bill_id]+1;
	mysql_query("update bill_id set bill_id='$bill_id' where id='$erow[id]' limit 1");
	//----------------------end bill id -------------------------

 

mysql_query("update bookings set customer_id='$cid',ostatus='1', bill_id='$bill_id' where customer_id='0' and ostatus='0'");
	
$user_ip = getUserIP();
 
 mysql_query("INSERT INTO bill (
 
 id ,bill_id,cdate ,customer_id ,total_price ,discount ,vat ,vat_amount ,total_payeble ,payment ,due ,pay_methode ,all_payment_total ,last_due,created_at,suser,refr,rcom,refr_id,unote,user_ip

 
 )VALUES (
 
 NULL , '$bill_id','$cdate', '$cid', '$total_price', '$discount', '$vat', '$vat_amount', '$total_payeble', '$payment_amount', '$due', '$pay_methode', '$payment_amount', '$due',CURRENT_TIMESTAMP, '$_SESSION[uid]','$refr','$rcom','$refr_id','$unote', '$user_ip')");

 
//----------------------Booking  -------------------------
	
 
//----------------------end Booking  -------------------------

header("location:show_billv.php?id=$bill_id");
}else{
header("location:hotel_new.php?info=Please Add room ... total Amount is 0");
}
?>