<?php
		include "../dbconfig.php";
	    include "../dbfunction.php";
   		if(isset($_POST))
		{   
		
		/*
		echo '<pre>'; 
		print_r($_POST);
		echo '</pre>'; 
		exit;
		*/
		  $passenger_name=$con->real_escape_string($_POST['passenger_name']);
		  $contact_phone=$con->real_escape_string($_POST['contact_mobile']);
		  $contact_email=$con->real_escape_string($_POST['contact_email']);
		 
		if(!empty($_POST['old_customer_id'])){
		$customer_id=$con->real_escape_string($_POST['old_customer_id']);
		
		
		}else{
//	$condition=array("name"=>$_POST['company_name'],"phone"=>$_POST['phone'],"email"=>$_POST['email'],"address1"=>$_POST['customer_address1'],"post_code"=>$_POST['post_code'],"client_reference"=>$_POST['client_reference'],);
	
	$condition=array("name"=>$_POST['company_name'],"phone"=>$_POST['phone'],"email"=>$_POST['email'],"address1"=>$_POST['customer_address1'],"post_code"=>$_POST['post_code'],);
		  $customer_id=insert($con,"customers",$condition,true);
		  
		 
		   $balance=0;
if(isset($_POST['balance']))
{
$balance=$_POST['balance'];
}
else
{
$balance=0;
}
		  $name=$_POST['company_name'];
		  $details="opening balance";
date_default_timezone_set ("Europe/London");
 $date = date('Y-m-d');
  $created = date('Y-m-d H:i:s');
  $sql = "INSERT INTO `creditors` (date, source_id,balance,details,created,name) VALUES('$date','$customer_id','$balance','$details','$created','$name')";
		 
				//	  echo $sql;
		  if (!mysqli_query($con,$sql)) {
			  echo $sql;
			  die('Create Transaction 3 INTO ORDER_Details -Error: ' . mysqli_error($con));
			  
		  }
		  
		}
		
		if(!empty($_POST['contact_id'])){
		$passenger_id=$con->real_escape_string($_POST['contact_id']);
		$sql="UPDATE company_contact SET mobile='$contact_phone', email='$contact_email' where id=$passenger_id";
			  if (!mysqli_query($con,$sql)) {
					echo $sql."Error is ". mysqli_error($con);
			  }
		}else{
		 // Details of Contact Name as well
		  
		 //Create a record in delivery address table as well
		  $condition=array("customer_id"=>$customer_id,"name"=>$passenger_name,"mobile"=>$contact_phone,"email"=>$contact_email,);
		  $passenger_id=insert($con,"company_contact",$condition,true);	
		
		 }
		 
	    
   		  $flight_no=$con->real_escape_string($_POST['flight_no']);
		 // Now insert all the details of the booking
		 $pickup_address=$con->real_escape_string($_POST['pickup_address']);
		 $pickup_post_code=$con->real_escape_string($_POST['pickup_post_code']);
		 $pickup_date=$con->real_escape_string($_POST['pickup_date']);
		 $pickup_date = str_replace('/', '-', $pickup_date);
		 $pickup_date = date("Y-m-d", strtotime($pickup_date));
		 
		 $passengers=$con->real_escape_string($_POST['passengers']);
		 $hours=$con->real_escape_string($_POST['hours']);
		 $mins=$con->real_escape_string($_POST['mins']);

		 $drop_address=$con->real_escape_string($_POST['drop_address']);
		 $drop_post_code=$con->real_escape_string($_POST['drop_post_code']);
		 
		 //$payment_type=$con->real_escape_string($_POST['payment_type']);
		 //$amount=$con->real_escape_string($_POST['amount']);
		  $start_date=$con->real_escape_string($_POST['start_date']);
		  $start_date = str_replace('/', '-', $start_date);
		 $b = explode('-',$start_date);
		 $start_date = $b[2].'-'.$b[1].'-'.$b[0];
		
		 $end_date=$con->real_escape_string($_POST['end_date']);
		 $end_date = str_replace('/', '-', $end_date);
		 $a = explode('-',$end_date);
		 $end_date = $a[2].'-'.$a[1].'-'.$a[0];
		 
		 $days=$con->real_escape_string($_POST['days']);
		 $vehicle_id=$con->real_escape_string($_POST['vehicle']);
		 $job_type=$con->real_escape_string($_POST['job_type']);
		 $rate=$con->real_escape_string($_POST['rate']);
		 $supp_cost=$con->real_escape_string($_POST['supp_cost']);
		 if (is_numeric($rate) && is_numeric($days)) {
			$total_fare = $rate * $days;
		} else {
			$total_fare = 0; // Fallback default
		}

		 //$total_fare=$rate*$days;
		$comments=$con->real_escape_string($_POST['comments']);
		$bags=$con->real_escape_string($_POST['bags']);
		 
		 
		 $client_reference=$con->real_escape_string($_POST['client_reference']);
   		 // Now let's build the query
		
		  $sql = "INSERT INTO `bookings` (
		  customer_id,
		  passenger_id,
		  job_status,
		  invoice_status,
		  pickup_address,
		  pickup_post_code,
		  pickup_date,
		  hours,
		  mins,
		  passengers,
		  flight_no,
		  drop_address,
		  drop_post_code,
		  start_date,
		  end_date,
		  days,
		  vehicle_id,
		  job_type,
		  rate,
		  supp_cost,
		  total_fare,
		  comments,
		  bags,
		  client_reference) 
		  
		  VALUES
		  (
		  $customer_id,
		  $passenger_id,
		  1,
		  1,
		  '$pickup_address',
		  '$pickup_post_code',
		  '$pickup_date',
		  '$hours',
		  '$mins',
		  '$passengers',
		  '$flight_no',
		  '$drop_address',
		  '$drop_post_code',
		  '$start_date',
		  '$end_date',
		  '$days',		  
		  '$vehicle_id',
		  '$job_type',
		  '$rate',		  
		  '$supp_cost',
		  '$total_fare',
		  '$comments',
		  '$bags',
		  '$client_reference')";
		 //echo $sql;
		 //exit;
		  if (!mysqli_query($con,$sql)) {
			  echo $sql."Error is ". mysqli_error($con);
			 
		  }else{echo "success";}
		//header("Location: ../list_bookings.php");
		}//if(isset($_POST['create_ordee
		
	?>