<?php
 	session_start();

	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}
		$brmid=$_SESSION['linkarisebranchid'];
		$cmpmid=$_SESSION['linkarisecmpmid'];
	$umid = $_SESSION['linkariseumid'];
	

	include("connect.php");
	include("removespecial.php");

	date_default_timezone_set("Asia/Singapore");
	 $mdate1= date('Y-m-d');
	 $mdatetime= date('Y-m-d H:i:s');
	 $cdatetime= date('Y-m-d H:i:s');


	
	$bookingdate=date("Y-m-d", strtotime($_POST['bookingdate']));
	$partymid=$_POST['partymid'];
	$function=$_POST['function'];
$lacrmumid=$_SESSION['linkariseumid'];

		$pax=$_POST['pax'];
		$startdate=date("Y-m-d", strtotime($_POST['startdate']));
		$enddate=date("Y-m-d", strtotime($_POST['enddate']));
		$spotmid=$_POST['spotmid'];
		$slotmid=$_POST['slotmid'];
		$totalamount=$_POST['totalamount'];
		$advanceamount=$_POST['advanceamount'];
		$pendingamount=$_POST['pendingamount'];
		
		$eve1 = "INSERT INTO res_booking_master ( partymid,functiontype,pax,functionstartdate,functionenddate,spotmid,bookingtotalamount,bookingadvanceamount,pendingamount,bookingdate,cmpmid) VALUES
			('$partymid','$function','$pax','$startdate', '$enddate','$spotmid','$totalamount','$advanceamount','$pendingamount','$bookingdate','$cmpmid')";		
			if ($conn->query($eve1) === TRUE)
				{
					$last_id = $conn->insert_id;
					$delete_checkbox_array = $_POST['slotmid'];
				foreach ($delete_checkbox_array as $one_by_one_sid)
				{		
					$slotmid=$one_by_one_sid;
					
					$eve3 = "INSERT INTO res_booking_detail (bookingmid,spotmid,slotmid,cmpmid) VALUES($last_id,$spotmid,$slotmid,'$cmpmid')";
					if ($conn->query($eve3) === TRUE)
					{
							$bookingmid = $conn->insert_id;
						$eve22 = "select * from res_slots_master where slotmid in(select slotmid from res_booking_detail where bookingdid=$bookingmid)";
				$re22= mysqli_query($conn, $eve22);
				while($rt22 = mysqli_fetch_assoc($re22))
					{
							$starttime = $rt22['slotstarttime'];
							$endtime = $rt22['slotendtime'];
					}
						 $eve = "UPDATE res_availibility_detail SET availablestatus = 'Booked' 
                WHERE spotmid = $spotmid AND cmpmid = '$cmpmid' 
                AND availabledate BETWEEN '$startdate' AND '$enddate' 
               AND slotmid=$slotmid";

	if ($conn->query($eve) === TRUE)
	{				
		 $eve11 = "UPDATE res_availibility_detail 
          SET availablestatus = 'Not Available' 
          WHERE spotmid = $spotmid 
          AND cmpmid = '$cmpmid' 
          AND availablestatus = 'Available' 
          AND (
            (availabledate = '$startdate' AND (CAST(starttime AS TIME) >= '$starttime' AND CAST(starttime AS TIME) <= '23:59:59'))
            OR (availabledate = '$enddate' AND (CAST(endtime AS TIME) >= '00:00:00' AND CAST(endtime AS TIME) <= '$endtime'))
            OR (availabledate > '$startdate' AND availabledate < '$enddate')
            OR (availabledate = '$startdate' AND availabledate = '$enddate' AND (CAST(starttime AS TIME) >= '$starttime' AND CAST(endtime AS TIME) <= '$endtime'))
          )";


	if ($conn->query($eve11) === TRUE)
	{				
	}
	}
						
					}
				}
		if($advanceamount>0 && $advanceamount!="" && $advanceamount!=0)
		{
			 $eve4 ="insert into res_bookingpayment_detail (bookingmid,bookingtotalamount,bookingreceivedamount,bookingpendingamount,paymentdate,cmpmid,paymentmode,paymentremark)
				values('$last_id','$totalamount','$advanceamount','$pendingamount','$mdate1','$cmpmid','Cash','Paid')";
				$re4 = mysqli_query($conn, $eve4); 
		}
				$_SESSION['msg'] = "Data successfully added.";
			
				}
		else{
			$_SESSION['msg1'] = "Error";
		}
	
		header('Location: booking.php');

?>
