<?php
session_start(); 

	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}
	
	include("connect.php");
	
	date_default_timezone_set("Asia/Kolkata");
	 $cdate= date('Y-m-d');
	 $mdate= date('Y-m-d H:i:s');
	 $cdatetime= date('Y-m-d H:i:s');
	 
	 
	$brmid=$_SESSION['linkarisebranchid'];
	$lacrmumid=$_SESSION['linkariseumid'];
	$cmpmid=$_SESSION['linkarisecmpmid'];
	$lacrmusername = $_SESSION['linkariseufullname'];
	$ipaddress = "";

	if (getenv('HTTP_CLIENT_IP'))
		$ipaddress = getenv('HTTP_CLIENT_IP');
	else if(getenv('HTTP_X_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
	else if(getenv('HTTP_X_FORWARDED'))
		$ipaddress = getenv('HTTP_X_FORWARDED');
	else if(getenv('HTTP_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_FORWARDED_FOR');
	else if(getenv('HTTP_FORWARDED'))
		$ipaddress = getenv('HTTP_FORWARDED');
	else if(getenv('REMOTE_ADDR'))
		$ipaddress = getenv('REMOTE_ADDR');
	else
		$ipaddress = 'UNKNOWN';
	
	$userserver=$_SERVER['HTTP_USER_AGENT'];
	$eve = "insert into res_user_log (lid,mobile,lastupdatedtime,pagename,taskname,ipaddress,useragentdetail) values(NULL,'$lacrmusername','$mdate','Payment Receipt','Add Payment Receipt','$ipaddress','$userserver')";
	$re = mysqli_query($conn, $eve);
	
	$partymid=$_POST['partymid'];
	$invoicefordisplay=$_POST['invoicefordisplay'];
	$receiptrefrenceid=$_POST['receiptrefrenceid'];
	$receiptamount=$_POST['receiptamount'];
	//$cmpmid=$_POST['cmpmid'];
	$receiptmode=$_POST['receiptmode'];
	$receiptdetail=$_POST['receiptdetail'];

	if($receiptamount!=0)
	{
		$cmpcurrentyear='20-21';
		$prefix='PayIn-';
		$evelastyear = "select * from res_company_master where cmpmid=$cmpmid";
		$relastyear = mysqli_query($conn, $evelastyear);
		while($rtlastyear = mysqli_fetch_assoc($relastyear))
		{
			$cmpcurrentyear=$rtlastyear['cmpyear'];
			$prefix=$rtlastyear['payinprefix'];
		}
		
		
		$payinno=0;
		$evemaxq = "SELECT max( payinno ) AS maxpayinno FROM res_paymentin_master where payinyear='$cmpcurrentyear' and cmpmid=$cmpmid";
		$remaxq = mysqli_query($conn, $evemaxq);
		while($rtmaxq = mysqli_fetch_assoc($remaxq))
		{
			$maxpayinno=$rtmaxq['maxpayinno'];
		}
		$maxpayinno1=$maxpayinno+1;
		$maxpayinno2=$prefix.$maxpayinno1;
		
		$query = "INSERT INTO res_paymentin_master (partymid, receiptrefrenceid, receiptdate, receiptamont, cmpmid, receiptmode, receiptdetail,payinno,payinyear,payinprefix,payinnofordisplay,addedby,addedon,updatedby,updatedon,refrenceno) VALUES ('$partymid', '$receiptrefrenceid', '$mdate', '$receiptamount', '$cmpmid', '$receiptmode', '$receiptdetail','$maxpayinno1','$cmpcurrentyear','$prefix','$maxpayinno2','$lacrmumid','$cdatetime','$lacrmumid','$cdatetime','$invoicefordisplay')";
		if ($conn->query($query) === TRUE)
		{
			 $paymentinmid = $conn->insert_id;
			 
		 $eve41 ="insert into res_paymenttransaction_detail (partymid,cmpmid,paymenttype,Paymentrefid,paymentrefno,paymentdate,paymentinamount,paymentmode,paymentdetail,addedby,addedon,updatedby,updatedon)
		values('$partymid','$cmpmid','Payment In','$paymentinmid','$maxpayinno2','$mdate','$receiptamount','$receiptmode','$receiptdetail','$lacrmumid','$cdatetime','$lacrmumid','$cdatetime')";
		$re41 = mysqli_query($conn, $eve41); 
		}
					
		
	
		$_SESSION['msg'] = "Record has been added successfully";
	}
	else
	{
		$_SESSION['msg1'] = "Error";
	}
	
	header('Location: pendingreceiptamount.php?partymid='.$partymid);
?>