<?php
session_start(); 
error_reporting(E_ALL);
	error_reporting(-1);
	ini_set('error_reporting', E_ALL);



	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}
	
	include("connect.php");
	include("removespecial.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['linkariseuname'];
	$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','Expense Detail','Add Expense Detail','$ipaddress','$userserver')";
	$re = mysqli_query($conn, $eve);
	
	$partymid=$_POST['cmpname'];
	$paddress=removespecialchar($_POST['paddress']);
	$expenseamount=$_POST['expenseamount'];

	$expensemode=$_POST['expensemode'];
	$expensedetail=removespecialchar($_POST['expensedetail']);

	if($expenseamount!=0)
	{
		
		$cmpcurrentyear='20-21';
		$prefix='Exp-';
		$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['expenseprefix'];
		}
		
		
		$maxexpno=0;
		 $evemaxq = "SELECT max( expno ) AS maxexpno FROM res_expense_detail where expyear='$cmpcurrentyear' and cmpmid=$cmpmid";
		$remaxq = mysqli_query($conn, $evemaxq);
		while($rtmaxq = mysqli_fetch_assoc($remaxq))
		{
			 $maxexpno=$rtmaxq['maxexpno'];
		}
		$maxexpno1=$maxexpno+1;
		$maxexpno2=$prefix.$maxexpno1;
		
		
		
		$query = "INSERT INTO res_expense_detail (expensetype, expenseremark, expensedate, expenseamount, paymentmode, paymentdetail,expno,expyear,expprefix,expfordisplay,addedby,addedon,updatedby,updatedon,cmpmid) VALUES ('$partymid', '$paddress', '$cdate', '$expenseamount',  '$expensemode', '$expensedetail','$maxexpno1','$cmpcurrentyear','$prefix','$maxexpno2','$lacrmumid','$cdatetime','$lacrmumid','$cdatetime','$cmpmid')";
		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','Expense','$paymentinmid','$maxexpno2','$cdate','$expenseamount','$expensemode','$expensedetail','$lacrmumid','$cdatetime','$lacrmumid','$cdatetime')";
		$re41 = mysqli_query($conn, $eve41); 
		}
					
		
	
		$_SESSION['msg'] = "Record has been added successfully";
	}
	else
	{
		$_SESSION['msg1'] = "Error";
	}

	header('Location: expensemanagement.php');
?>