<?php
ob_start();
$PageSecurity = 11;
include('includes/session.inc');
include('includes/function_lib.php');
function dateDiff1($d1, $d2) 
{
  return round(abs(strtotime($d1)-strtotime($d2))/86400)+1;
} 




function Cal_Existing_daysEdit($frmdate,$todate,$EId)
{
	global $db;
	$diff_dates=round(abs(strtotime($frmdate)-strtotime($todate))/86400)+1;
	$flag=0;
	for($i=0;$i<$diff_dates;$i++)
	{
		$date2= strtotime(date("Y-m-d", strtotime($frmdate)) . " +$i day");
		$date11= date("Y-m-d",$date2); 
		$row1=DB_query("SELECT * FROM emp_leave_apply 
		WHERE empid='".$_REQUEST['empid']."'
		AND (
		(FIND_IN_SET('".$date11."',between_date)) 
		OR (FIND_IN_SET('".$date11."',between_date))
		) and id!='".$EId."'",$db);
		
		$numrow=DB_num_rows($row1);
		
		if($numrow>0)
		{
		$flag=1;
		}
	}
	return $flag;
}
 



 
$difference=dateDiff1(ConvertSQLDate($_REQUEST['dfrom']),ConvertSQLDate($_REQUEST['dto']));
$data1="";
$data1.=$difference;

$data2="";
$ChkDate="";


$t1=ConvertSQLDate($_REQUEST['dfrom']);
$t2=ConvertSQLDate($_REQUEST['dto']);

$year_s = date("Y",strtotime(ConvertSQLDate($_REQUEST['dfrom'])));
$month_s = date("m",strtotime(ConvertSQLDate($_REQUEST['dfrom'])));
$month_e = date("m",strtotime(ConvertSQLDate($_REQUEST['dto'])));


$ToAdate=ConvertSQLDate($_REQUEST['dto']);

$workingdays=cal_days_in_month(CAL_GREGORIAN, $month_s, $year_s);
$abscl =0;

$emp_leave_query=DB_query("SELECT * FROM emp_leave where emp_id='".$_REQUEST['empid']."' and leave_type='".$_REQUEST['q']."'",$db);
$emp_leave_row=DB_fetch_array($emp_leave_query);

	
$emp_bm_query=DB_query("SELECT sum(pending_cl) AS pcl FROM cl_emp where emp_id='".$_REQUEST['empid']."'",$db);
$emp_bm_row=DB_fetch_array($emp_bm_query);

$EditId=$_REQUEST['Editid'];

$checkedit=Cal_Existing_daysEdit($t1,$t2,$EditId);

	if($checkedit==1)
	{
		$ChkDate.="1";
	}
	else
	{

		$SqlPreLeaave=DB_query("Select total_days,total_approval_days,approve_status from  emp_leave_apply WHERE empid='".$_REQUEST['empid']."' and leave_type='".$_REQUEST['q']."'",$db);
		while($RowEmpABSDatamm = DB_fetch_array($SqlPreLeaave))
		{
			if($RowEmpABSDatamm['approve_status']!='Y')
			{
				$abscl=$abscl+$RowEmpABSDatamm['total_days'];
			}
			else
			{
				$takedleave=$takedleave+$RowEmpABSDatamm['total_approval_days'];
			}
		}

		$ChkDate.="";
		//$abscl = 0.0;
	} 
 	
	
	
	$finalEarnleave=$takedleave+$abscl;
	$da = explode("/", $_REQUEST['doj']);
	if($da[2]==$year_s){
	//$linyear = $emp_leave_row['leave_days']/12;
	// 
	if($_REQUEST['pevlevereq']>$difference)
	{
		$renm=$_REQUEST['pevlevereq']-$difference;
		$absc2=$finalEarnleave-$renm;
	}
	else if($_REQUEST['pevlevereq']<$difference)
	{
		$renm=$difference-$_REQUEST['pevlevereq'];
		$absc2=$finalEarnleave+$renm;
	}
	elseif($_REQUEST['pevlevereq']==$difference)
	{
		$absc2=$finalEarnleave;
	}
	//$abscl=$abscl+$difference;
	$linyear = $emp_leave_row['leave_days'];
	//$lm = (12-$da[1])+1;
	//$levetake = $linyear*$lm;
	//$month_ee = ($month_e-$da[1])+1;
	
	$remaininday=$linyear-$absc2;
	$data2.=$remaininday;
	 }else{ 
	 $remaininday=$emp_leave_row['leave_days']-$abscl;
	 $data2.=$renm1;
} 


echo $data=$data1."__".$data2."__".$ChkDate;
?>