<?php
 	session_start();

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

	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');


	
	$idate=date("Y-m-d", strtotime($_POST['idate']));
	$istatus="Pending";
	//$idetails=$_POST['idetails'];
	$isname=$_POST['isname'];
	$inquirystagename=$_POST['inquirystagename'];
	
	$lacrmumid=$_SESSION['linkariseumid'];
	if(isset($_POST['umid']))
	{
		$umid=$_POST['umid'];
	}
		
		$cmpname=removespecialchar($_POST['cmpname']);
		$cmobile=$_POST['pmobile'];
		$cemail=$_POST['pemail'];
		$address=$_POST['paddress'];
		$cstate=$_POST['state'];
		$function=$_POST['function'];
		$spotmid=$_POST['spotmid'];
		$pax=$_POST['pax'];
		
		
		$followup=$_POST['followup'];
		$inqfdate=date("Y-m-d", strtotime($_POST['inqfdate'])); 
		$fstartdate=date("Y-m-d", strtotime($_POST['functionstartdate'])); 
		$fenddate=date("Y-m-d", strtotime($_POST['functionenddate'])); 
		$inqfremarks=removespecialchar($_POST['remarks']);
		$stageremark="";
		$ibudget=$_POST['ibudge'];
		$partyinqno=0;
		$last_id_partymid = '0';
		$partyno=0;
		$ccity="";
		$cstatecode="";
		$cpincode="";
	
		$partyno=0;
		$evemaxpartyno = "SELECT max(partyno) AS maxpartyinqno FROM res_party_master where partytype='Inquiry' and pyear='$lacrmcmpyear' and cmpmid=$cmpmid";
		$remaxpartyno = mysqli_query($conn, $evemaxpartyno);
		if(mysqli_num_rows($remaxpartyno) > 0)
		{
			while($rtmaxpartyno = mysqli_fetch_assoc($remaxpartyno))
			{
				$partyno=$rtmaxpartyno['maxpartyinqno'];
			}
		}
		$partyno=$partyno+1;		
		$eve1 = "INSERT INTO res_party_master ( umid,cmpmid,partyno,partyname,partyaddress,partycity,partystate,partystatecode,partypincode,partymobile,partyemail,partytype,partyinqsource,partyinqdate,partyinqbudget,partyinqstatus,partystage,pyear,partyaddedby,partyaddedon,partyupdatedby,partyupdatedon,brmid,followup,stageremark,followupremark,function,spotmid,pax,fstartdate,fenddate) VALUES
			('$umid','$cmpmid','$partyno','$cmpname', '$address','$ccity','$cstate','$cstatecode','$cpincode','$cmobile','$cemail','Inquiry','$isname','$idate','$ibudget','Open','$inquirystagename','$lacrmcmpyear','$lacrmumid','$cdatetime','$lacrmumid','$cdatetime','$brmid','$followup','$stageremark','$inqfremarks','$function','$spotmid','$pax','$fstartdate','$fenddate')";		
		if ($conn->query($eve1) === TRUE)
		{
			$partymid = $conn->insert_id;
			if($followup=='Yes')
			{
				 $eve1 = "INSERT INTO res_followup_detail (partymid,ftype,fdate, fremarks, fstatus, cmpmid, faddedby, fupdatedby, faddedon, fupdatedon,inquirystage,stageremark) VALUES ('$partymid','Inquiry','$inqfdate', '$inqfremarks', 'Open', $cmpmid, '$umid', '$umid', '$cdatetime', '$cdatetime','$inquirystagename','$stageremark')";
				//$re1 = mysqli_query($conn, $eve1);
				if ($conn->query($eve1) === TRUE)
				{
					 $eve2 = "update res_party_master set followupdate='$inqfdate',followupremark='$inqfremarks', partyupdatedon='$mdatetime', partyupdatedby='$umid' where partymid=$partymid";
					 $re2 = mysqli_query($conn, $eve2);
				}
			}
			$_SESSION['msg'] = "Record saved successfully";
		}
		else{
			$_SESSION['msg1'] = "Error";
		}
	
		header('Location: inquiry.php');

?>
