<?php
if(!isset($_SESSION)) { 
  session_start(); 
} 

	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}
	$cmpmid=$_SESSION['linkarisecmpmid'];
	include("connect.php");
	
	date_default_timezone_set("Asia/Singapore");
	$mdate1= date('Y-m-d H:i:s');
	$mdate2= date('Y-m-d');
	
	$bookingmid=$_POST['bookingmid'];
	$pmode=$_POST['paymentmode'];
	$payamount=$_POST['paymentamount'];
	$payremark=$_POST['paymentremark'];
	$pendingamount=$_POST['pendingamount'];
	$bookingtotalamount=0;
		$eve = "select * from res_booking_master where bookingmid=$bookingmid";
	    $re = mysqli_query($conn, $eve);
		while($rt = mysqli_fetch_assoc($re))
		{
				$bookingtotalamount=$rt['bookingtotalamount'];
		}
	$pendingamount1=$pendingamount-$payamount;
	
			
	  $eve1 = "update res_booking_master set bookingadvanceamount = '$payamount',pendingamount='$pendingamount1' where bookingmid='$bookingmid' ";	
	
	if ($conn->query($eve1) === TRUE)
	{
		$last_id = $conn->insert_id;
		  $eve11 = "insert into res_bookingpayment_detail (bookingmid,bookingtotalamount,bookingreceivedamount,paymentmode,paymentremark,bookingpendingamount,cmpmid,paymentdate) VALUES($bookingmid,'$bookingtotalamount','$payamount','$pmode','$payremark','$pendingamount1','$cmpmid','$mdate2')";		
		$re11 = mysqli_query($conn, $eve11);
		$_SESSION['msg'] = "Record has been added successfully";
	}
	else
	{
		$_SESSION['msg1'] = "Error";
	}
	
header("Location: bookingdetail.php?id=$bookingmid");
?>