<?php
ob_start();
$PageSecurity = 11;
include('includes/session.inc');
include('includes/function_lib.php');
$data1="";
$data3='';
$data2="";

$fin_yr_start=$_SESSION['financialYrStart'];
$fin_yr=explode("-",$_SESSION['financialYrStart']);
$get_fin_yr=$fin_yr[0];

$deduct_half_day=0;
$deduct_other_half_day=0;
$deduct_half_same_day=0;


	function Cal_Existing_days($frmdate,$todate){
		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 approve_status!='C'
			AND (
			(FIND_IN_SET('".$date11."',between_date)) 
			OR (FIND_IN_SET('".$date11."',between_date))
			)",$db);
			
			$numrow=DB_num_rows($row1);
			
			if($numrow>0){	$flag=1;}
		  }
		return $flag;
	}
  
 
	function dateDiff1($d1, $d2) {  return round(abs(strtotime($d1)-strtotime($d2))/86400)+1;}  

    $difference=dateDiff1(ConvertSQLDate($_REQUEST['dfrom']),ConvertSQLDate($_REQUEST['dto']));
     
     //echo $difference."###".$_REQUEST['first_half']."###".$_REQUEST['second_half'];   
      if($difference>1)
      {
	       if($_REQUEST['first_half']!='0')
	     	{ 
	     		$deduct_half_day=0.5; 
	     	}
	       if($_REQUEST['second_half']!='0') 
	     	{ 
	     		$deduct_other_half_day=0.5;
	        }
	  }
	  else
	   {
	   	 if($_REQUEST['first_half']!='0' && $_REQUEST['second_half']!='0')
	   	  {
	   	      $deduct_half_same_day=0.5;
	   	  }    
	   }

      //$data1=$difference;
     $data1=$difference-$deduct_half_day-$deduct_other_half_day-$deduct_half_same_day;

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

	$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);
	$check1=Cal_Existing_days($t1,$t2);







	if($check1==1){ $ChkDate="1";}
	else {
		$SqlPreLeaave=DB_query("Select total_days,total_approval_days,approve_status from  emp_leave_apply
		 WHERE empid='".$_REQUEST['empid']."' AND period_from>='".$_SESSION['financialYrStart']."' and period_to<='".$_SESSION['financialYrEnd']."' and leave_type='".$_REQUEST['q']."'",$db);
		while($RowEmpABSDatamm = DB_fetch_array($SqlPreLeaave)){
			if($RowEmpABSDatamm['approve_status']=='N' ){
			$abscl=$abscl+$RowEmpABSDatamm['total_days'];}
			else if($RowEmpABSDatamm['approve_status']=='Y'){
			$takedleave=$takedleave+$RowEmpABSDatamm['total_approval_days'];}
		}

		$ChkDate="";
		
	} 
	
	$finalEarnleave=$takedleave;	
	$abscl=$finalEarnleave+$difference;
	$linyear = $emp_leave_row['leave_days'];
	
	
	      $privious_opening = "Select opening as total_opening from hr_leave_opening where empid='".$_REQUEST['empid']."' 
                              and leave_type='".$_REQUEST['q']."' and opening_year='".$get_fin_yr."' group by leave_type";	   
	      $ResultData1 = DB_query($privious_opening,$db);
	      $RowData1 = DB_fetch_array($ResultData1);
		
          if($RowData1['total_opening']==''){ $opening_leave=0;}else { $opening_leave=$RowData1['total_opening'];}	
	      $remaindays=($linyear+$opening_leave)-$finalEarnleave;
	//$remaindays=$abscl;
	
    
   $leave_flag=$s->GetFieldValue("leavemaster","id='".$_REQUEST['q']."'","leave_flag");
   if($leave_flag==1)
   $data2=$s->GetFieldValue("leavemaster","id='".$_REQUEST['q']."'","max_leave");
   else
   $data2=$remaindays;	
	//}
	//else
	//{ 
	//$data2.=$remaindays;
//} 
		$doj=$s->GetFieldValue("empmaster","id='".$_REQUEST['empid']."'","doj");
		$servicedays='365';
		$allowd_date=date('Y-m-d', strtotime($doj. ' + '.$servicedays.' days'));

 
	if($s->GetFieldValue("leavemaster","id='".$_REQUEST['q']."'","leavecode")=='CL')
	{
		
	/*$sql_check=DB_fetch_array(DB_query("Select sum(total_days) as days from  emp_leave_apply
			                  WHERE empid='".$_REQUEST['empid']."' AND YEAR(period_from)='".$year_s."'
			                 and leave_type='".$_REQUEST['q']."' and ( Month(period_from)='".$month_s."' || Month(period_from)='".$month_e."') ",$db));*/
$sql_check=DB_fetch_array(DB_query("Select sum(total_days) as days from  emp_leave_apply
			                  WHERE empid='".$_REQUEST['empid']."' AND period_from>='".$_SESSION['financialYrStart']."' and period_to<='".$_SESSION['financialYrEnd']."'
			                 and leave_type='".$_REQUEST['q']."' and ( Month(period_from)='".$month_s."' || Month(period_from)='".$month_e."') ",$db));
		      $totalleave=$remaindays-($sql_check['days']+$data1);
			  if($totalleave<='0'){	$data3=''; $data2=$remaindays-($sql_check['days']);	}

			if($leave_flag==1 && $sql_check['days']>=1 && (date('m')==$month_s || date('m')==$month_e))	{ $data3=0;	}
			if(date('m')!=$month_s || date('m')!=$month_e){	$data3=1;}
	}

	if($s->GetFieldValue("leavemaster","id='".$_REQUEST['q']."'","leavecode")=='EL')
	{ 
		 /*  $sql_check=DB_fetch_array(DB_query("Select count(*) as days,sum(total_days) as leavecount from  emp_leave_apply
			          WHERE empid='".$_REQUEST['empid']."' AND YEAR(period_from)='".$year_s."'
			          and leave_type='".$_REQUEST['q']."' and approve_status not in('Y','C') ",$db));*/
  $sql_check=DB_fetch_array(DB_query("Select count(*) as days,sum(total_days) as leavecount from  emp_leave_apply
			          WHERE empid='".$_REQUEST['empid']."' AND period_from>='".$_SESSION['financialYrStart']."' and period_to<='".$_SESSION['financialYrEnd']."'
			          and leave_type='".$_REQUEST['q']."' and approve_status not in('Y','C') ",$db));
		    $levetype=$s->GetFieldValue("leavemaster","id='".$_REQUEST['q']."'","leavecode");
	        $totalleave=$remaindays-($sql_check['leavecount']+$data1);
	        $ffdate=explode("/",$_REQUEST['dfrom']);
	        $ttdate=explode("/",$_REQUEST['dto']);
	        $fdate=$ffdate[2]."-".$ffdate[1]."-".$ffdate[0];
	        $tdate=$ttdate[2]."-".$ttdate[1]."-".$ttdate[0];
	        $alldate=strtotime($allowd_date);
	        $ffdate1=strtotime($fdate);
	        $ttdate1=strtotime($tdate);
	       //$alldate."--".$ffdate1."--".$ttdate1;
	      // echo $remaindays;
	           
	        /* if($totalleave<='0')
	        	{ $data3=''; echo "hi"; $data2=$remaindays-($sql_check['leavecount']);	
	            }
			if($levetype=='EL'  && $totalleave >0 && ($ffdate1<=$alldate 
				 	&& $ttdate1<=$alldate))		
				{ 
					echo "hello";	  $data3=2;			
		        }
			if($sql_check['days']>=15)
				{	  
					$data3=3;				
				} */
                 $data3=''; $data2=$remaindays-($sql_check['leavecount']);	


	}

	if($s->GetFieldValue("leavemaster","id='".$_REQUEST['q']."'","leavecode")=='SL')
	{

		   /* $sql_check=DB_fetch_array(DB_query("Select sum(total_days) as  days from  emp_leave_apply
			 WHERE empid='".$_REQUEST['empid']."' AND YEAR(period_from)='".$year_s."'
			 and leave_type='".$_REQUEST['q']."' ",$db)); */
           $sql_check=DB_fetch_array(DB_query("Select sum(total_days) as  days from  emp_leave_apply
			 WHERE empid='".$_REQUEST['empid']."' period_from>='".$_SESSION['financialYrStart']."' and period_to<='".$_SESSION['financialYrEnd']."'
			 and leave_type='".$_REQUEST['q']."' ",$db));
			  $totalleave=$remaindays-($sql_check['days']+$data1);
			  if($totalleave<='0'){	$data3=''; $data2=$remaindays-($sql_check['days']);	}
			  if($sql_check['days']>5){  $data3=4;}
			
	}

	if($s->GetFieldValue("leavemaster","id='".$_REQUEST['q']."'","leavecode")=='ML')
	{
	    $children1=$s->getField('empmaster','id',$_REQUEST['empid'],'sons');
	    $children2=$s->getField('empmaster','id',$_REQUEST['empid'],'daughters');
	    $sumofchildrens=$children2+$children1;
	    $allowed_leave=$s->GetFieldValue("leavemaster","id='".$_REQUEST['q']."'","max_leave");
		/*$sql_check=DB_fetch_array(DB_query("Select sum(total_days) as  days from  emp_leave_apply
			 WHERE empid='".$_REQUEST['empid']."' AND YEAR(period_from)='".$year_s."'
			  and leave_type='".$_REQUEST['q']."' ",$db));*/ 
           $sql_check=DB_fetch_array(DB_query("Select sum(total_days) as  days from  emp_leave_apply
			 WHERE empid='".$_REQUEST['empid']."' AND period_from>='".$_SESSION['financialYrStart']."' and period_to<='".$_SESSION['financialYrEnd']."'
			  and leave_type='".$_REQUEST['q']."' ",$db));
	            $totalleave=$remaindays-($sql_check['days']+$data1);
			    if($totalleave<='0'){	$data3=''; $data2=$remaindays-($sql_check['days']);	}		
				if($sql_check['days']>$allowed_leave && $sumofchildrens<2){ $data3=5;	}
				else if($sumofchildrens>=2)	{  $data3=6;}
			
			
	}

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