<?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 Out','Add Payment Out','$ipaddress','$userserver')";
	$re = mysqli_query($conn, $eve);
	
	$partymid=$_POST['partymid'];
	$outrefrenceid=$_POST['outrefrenceid'];
	$outamount=$_POST['outamount'];
	//$cmpmid=$_POST['cmpmid'];
	$outmode=$_POST['outmode'];
	$outdetail=$_POST['outdetail'];

	if($outamount!=0)
	{
		
		$cmpcurrentyear='20-21';
		$prefix='PayOut-';
		$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['payoutprefix'];
		}
		
		
		$payoutno=0;
		$evemaxq = "SELECT max( payoutno ) AS maxpayoutno FROM res_paymentout_master where payoutyear='$cmpcurrentyear' and cmpmid=$cmpmid";
		$remaxq = mysqli_query($conn, $evemaxq);
		while($rtmaxq = mysqli_fetch_assoc($remaxq))
		{
			$maxpayoutno=$rtmaxq['maxpayoutno'];
		}
		$maxpayoutno1=$maxpayoutno+1;
		$maxpayoutno2=$prefix.$maxpayoutno1;
		
		$query = "INSERT INTO res_paymentout_master (partymid, outrefrenceid, outdate, outamount, cmpmid, outmode, outdetail,payoutno,payoutyear,payoutprefix,payoutfordisplay,addedby,addedon,updatedby,updatedon) VALUES ('$partymid', '$outrefrenceid', '$mdate', '$outamount', '$cmpmid', '$outmode', '$outdetail','$maxpayoutno1','$cmpcurrentyear','$prefix','$maxpayoutno2','$lacrmumid','$cdatetime','$lacrmumid','$cdatetime')";
		if ($conn->query($query) === TRUE)
		{
			 $paymentinmid = $conn->insert_id;
			 
		 $eve41 ="insert into res_paymenttransaction_detail (partymid,cmpmid,paymenttype,Paymentrefid,paymentrefno,paymentdate,paymentoutamount,paymentmode,paymentdetail,addedby,addedon,updatedby,updatedon)
		values('$partymid','$cmpmid','Payment Out','$paymentinmid','$maxpayoutno2','$mdate','$outamount','$outmode','$outdetail','$lacrmumid','$cdatetime','$lacrmumid','$cdatetime')";
		$re41 = mysqli_query($conn, $eve41); 
		}
					
		
	
		$_SESSION['msg'] = "Record has been added successfully";
	}
	else
	{
		$_SESSION['msg1'] = "Error";
	}
	
	header('Location: paymentout.php');
?>