<?php
@session_start();
include_once("../class/class.MySQL.php");
$db	=new MySQL();

$action = $_GET['act'];

if($action == "list"){
	$where['is_blocked'] = 0;
	$leaves  = $db->fetchAll("payroll_leave",$where); 
	
	$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  s.s_menu_name='Leave' and m.menu_id=s.menu_id "));
	
	 $userid=$_SESSION['admin_id']; 

	$wheres['user_id']	= $userid;
	$wheres['s_submenu_id']	= $sub_rights['s_id'];
	if(count($leaves) > 0){
	if($userid==1)
	{
	$leaves[count($leaves)-1]['permissions'] = array("user_id"=>$userid);
	}
	else
	{
	$leaves[count($leaves)-1]['permissions'] =  $db->fetchAll("tbl_user_rights",$wheres);
	}
	}
	
	
	echo '{"leaves":'.json_encode($leaves).'}';
	
}elseif($action == "update"){

}elseif($action == "remove"){

$where['lid']	=$_GET['lid'];
$set['is_blocked'] = 1;
$db->Update('payroll_leave',$set,$where);
	

}
elseif($action == "insert"){
	
	$vars['title'] 			= $_POST['leaveTitle'];
	$vars['description'] 	= $_POST['leaveDescription'];
	$vars['number_of_days'] = $_POST['days'];
	$vars['based_on']		= $_POST['based_on'];
	//$vers['unit']			= $_POST['unit'];
	
	$db->Insert($vars,'payroll_leave');
	
	$vars['id']				=$db->LastInsertID();
	
	echo '{"leaves":'.json_encode($vars).'}';
	
}
elseif($action == "emp"){

	$where['employee_id']	= $_GET['emp_id'];
	$where['is_blocked']	= 0;
	$leaves = $db->fetchAll('payroll_user_leave',$where);
	 echo '{"leaves":'.json_encode($leaves).'}';
}
elseif($action == "removeded"){
	$where['employee_id']	= $_GET['emp_id'];
	$where['leave_id']	= $_GET['leave_id'];

	$deductions = $db->Delete('payroll_user_leave',$where);

}
elseif($action == "addtoemp"){
	 $vars['employee_id']	= $_GET['emp_id'];
	 $vars['leave_id']	= $_GET['leave_id'];

	$leaves = $db->Insert($vars,'payroll_user_leave');
	
}elseif($action == "newRequest"){

	$vars['employee_id']	= $_GET['emp_id'];
	$vars['day_from']		= date('Y-m-d',strtotime($_POST['day_from']));
	 $vars['day_to']		= date('Y-m-d',strtotime($_POST['day_to']));
	 
	 $firstdate=$day_from = date('Y-m-d',strtotime($_POST['day_from'])); // or your date as well
     $lastdate=$day_to = date('Y-m-d',strtotime($_POST['day_to']));
	// echo "SELECT * FROM payroll_holiday_calendar WHERE MONTH(date)='$day_from' and YEAR(date)='$day_to'";
	  $lastMtnHolidays = $db->fetchQuery("SELECT * FROM payroll_holiday_calendar WHERE MONTH(date)='$day_from' and YEAR(date)='$day_to'");
		 $holidays=array();
		 foreach($lastMtnHolidays as $lastMtnHolidays){
			$holidays[]=$lastMtnHolidays['date'];
		 }
	 
	$vars['no_of_days']	= 1;//floor($datediff/(60*60*24));
	$vars['leave_id']	= $_POST['leave_id'];
	$vars['reason']		= $_POST['reason'];
	$vars['half_full_day']		= $_POST['day_type'];
	//$vars['leave_stat']		= $_POST['empstat'];
	//$vars['HRstatus']		=0;
	
	$vars['imd_status']		=0;
	// print_r($holidays);
     $datediff = $db->getWorkingDays($day_from,$day_to,$holidays);
	 $day_from = date('Y-m-d',strtotime($_POST['day_from']));
	
	for($i=1;$i<=$datediff;$i++)
	{
	
	 
	  // or your date as well
    $vars['day_from']		= $day_from;
	 $vars['day_to']		= $day_from;
	
	$db->Insert($vars,'payroll_user_leave_requests');
	$leav_req = mysql_insert_id();
	$day_from = date('Y-m-d',strtotime("+1 day", strtotime($day_from)));
	}
	
	$varsemp['emp_status']	= $_POST['empstat'];
	$where['e_id'] = $_GET['emp_id'];
	$db->Update('employee',$varsemp,$where);
	
	//subha for date from and date to
	 $vard['datefrom']		= date('Y-m-d',strtotime($_POST['day_from']));
	 $vard['dateto']		= date('Y-m-d',strtotime($_POST['day_to']));
	 $vard['emp_id'] = $_GET['emp_id'];
	 $vard['leav_req_id']=$leav_req;
     $db->Insert($vard,'payroll_leaveformat');
	
	
	$username=$_GET['emp_id'];
	$sel_qry		="SELECT * FROM employee where e_id ='$username'";
	$sel_res = $db->fetchQuery($sel_qry);
	
	$e_fname=$sel_res[0]['e_fname'];
	$imd_supervisor_id=$sel_res[0]['imd_supervisor_id'];
	$fromemail=$sel_res[0]['e_mail'];
					
	$sel_qry_imm		="SELECT * FROM employee where e_id ='$imd_supervisor_id'";
	$sel_res_imm = $db->fetchQuery($sel_qry_imm);
	
	$email=$sel_res_imm[0]['e_mail'];				
	
		$message=$e_fname.' has applied for leave from '.date('d-m-Y',strtotime($firstdate)).' to '.date('d-m-Y',strtotime($lastdate)).' .Please do check the same <br/><br/>www.groupmukkadan.com/HRMS';	
		
		$headers  	= "MIME-Version: 1.0\r\n";
		$headers 	.= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
		$headers 	.= "From:  ".$fromemail."\r\n";
		
		mail($email,'Leave Request',$message,$headers,parameters);
		
		/*$mail1=	new PHPMailer();
		$mail1->IsHTML(true); 
	    $mail1->Host = '208.109.217.134';	
		$mail1->AddAddress("$email");	
		$mail1->From="arunsab82@gmail.com";
		$mail1->FromName = "yummybay";
		$mail1->Subject = "test";
		
		$mail1->Body=$e_fname.' has applied for leave from '.date('d-m-Y',strtotime($firstdate)).' to '.date('d-m-Y',strtotime($lastdate)).' .Please do check the same <br/><br/>www.groupmukkadan.com';*/
		
		
		
	

	
	
	
	$lastid= $vars['id']	= $db->LastInsertID();
	
	
	$leaves = $db->fetchQuery("SELECT * FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								WHERE A.id=$lastid");
								
								
	echo '{"leaves":'.json_encode($leaves).'}';

}elseif($action == "getLeaves"){
	$emp_id = $_GET['emp_id'];
	
	//echo "SELECT * FROM payroll_user_leave A LEFT JOIN payroll_leave B ON A.leave_id = B.lid WHERE A.employee_id='$emp_id'";
	$leaves = $db->fetchQuery("SELECT * FROM payroll_user_leave A LEFT JOIN payroll_leave B ON A.leave_id = B.lid WHERE A.employee_id='$emp_id'");

	echo '{"leaves":'.json_encode($leaves).'}';

}
elseif($action == "loadRequests"){
	$cond='';$memberid='';$add_date1='';$from_date1 = '';$to_date1 = '';$branch = '';$designation = '';
	$userid=$_GET['userid'];
	$depart_id=$_GET['depart_id'];

	if(!empty($_REQUEST['add_date'])){
		$add_date=$_REQUEST['add_date'];
		$add_date1 = date('Y-m-d', strtotime($add_date));
	}

	if(!empty($_REQUEST['from_date'])){
		$from_date=$_REQUEST['from_date'];
		$from_date1 = date('Y-m-d', strtotime($from_date));
	}

	if(!empty($_REQUEST['to_date'])){
		$to_date=$_REQUEST['to_date'];
		$to_date1 = date('Y-m-d', strtotime($to_date));
	}

	if(!empty($_REQUEST['memberid'])){
		$memberid=$_REQUEST['memberid'];
	}

	if(!empty($_REQUEST['branch'])){
		$branch=$_REQUEST['branch'];
	}
	
	if(!empty($_REQUEST['designation'])){
		$designation=$_REQUEST['designation'];
	}
	if(isset($_REQUEST['immapproval'])){
		$immapproval=$_REQUEST['immapproval'];
	}

	if(isset($_REQUEST['finalapproval'])){
		$finalapproval=$_REQUEST['finalapproval'];
	}
	if(!empty($add_date1))
	{
		$cond.='and A.day_from<="'.$add_date1.'" and A.day_to>="'.$add_date1.'"';
	}
	if(!empty($from_date1) && !empty($to_date1))
	{
		$cond.='and A.day_from >= "'.$from_date1.'" and A.day_to <= "'.$to_date1.'"';
	}

	if(!empty($memberid))
	{
		$cond.='and A.employee_id="'.$memberid.'"';
	}
	if(!empty($branch))
	{
		$cond.='and B.branchid="'.$branch.'"';
	}
	if(!empty($designation))
	{
		$cond.='and B.e_post="'.$designation.'"';
	}

	if($immapproval!='')
	{
		if($immapproval=='2'){
			$cond.='and A.is_blocked="1"';
		}else{
			$cond.='and A.imd_status="'.$immapproval.'" and A.is_blocked="0"';
		}
	}

	if($finalapproval!='')
	{
		if($finalapproval==1)
			$cond.='and A.imd_status="'.$finalapproval.'"';
		else
			$cond.='and (A.imd_status="'.$finalapproval.'")';
	}
	
	if($_SESSION['usertype']=='Superadmin')
	{
		$query = "SELECT *,A.is_blocked as blocked FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid WHERE A.is_cancelled!=1 $cond";
	
	}
	elseif($_SESSION['branchadmin']=='Y')
	{
		$query = "SELECT *,A.is_blocked as blocked FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid WHERE A.is_cancelled!=1 and branchid=".$_SESSION['branch_id']." $cond";
	}
	 else {
		$query = "SELECT *,A.is_blocked as blocked  FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								WHERE B.imd_supervisor_id=$userid and A.is_cancelled!=1 $cond";
	
	}
	include "pagingserch.php";

	$cond.=" order by A.id desc LIMIT $start, $limit";

	if(isset($_REQUEST['page'])){
		$page=$_REQUEST['page'];
	}else{
		$page=15;
	}

	$pagecnt=$start;

	if($_SESSION['usertype']=='Superadmin')
	{
		$leaves = $db->fetchQuery("SELECT *,A.is_blocked as blocked FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid WHERE A.is_cancelled!=1  $cond ");
	}
	elseif($_SESSION['branchadmin']=='Y') {
		$leaves = $db->fetchQuery("SELECT *,A.is_blocked as blocked FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid WHERE A.is_cancelled!=1 and branchid=".$_SESSION['branch_id']." $cond ");
	} else {
		$leaves = $db->fetchQuery("SELECT *,A.is_blocked as blocked FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								WHERE B.imd_supervisor_id=$userid and A.is_cancelled!=1  $cond ");
	}
	
	
	
	$count = count($leaves);
	$leaves[$count-1]['totalrows'] = $total_pages;
	$leaves[$count-1]['lastPage'] = $lastpage;
	
	echo '{"leaves":'.json_encode($leaves).'}';
}
elseif($action == "loadpersonalRequests"){

		$cond='';$add_date1='';$from_date1 = '';$to_date1 = '';
		$userid=$_GET['userid'];

if(!empty($_REQUEST['add_date'])){
$add_date=$_REQUEST['add_date'];
$add_date1 = date('Y-m-d', strtotime($add_date));
}

if(!empty($_REQUEST['from_date'])){
$from_date=$_REQUEST['from_date'];
$from_date1 = date('Y-m-d', strtotime($from_date));
}

if(!empty($_REQUEST['to_date'])){
$to_date=$_REQUEST['to_date'];
$to_date1 = date('Y-m-d', strtotime($to_date));
}


if(isset($_REQUEST['immapproval'])){
$immapproval=$_REQUEST['immapproval'];
}
/*if(isset($_REQUEST['hrapproval'])){
$hrapproval=$_REQUEST['hrapproval'];
}*/
if(isset($_REQUEST['finalapproval'])){
$finalapproval=$_REQUEST['finalapproval'];
}
if(!empty($add_date1))
{

$cond.='and A.day_from<="'.$add_date1.'" and A.day_to>="'.$add_date1.'"';
}
if(!empty($from_date1) && !empty($to_date1))
{
$cond.='and A.day_from >= "'.$from_date1.'" and A.day_to <="'.$to_date1.'"';
}


if($immapproval!='')
{
	if($immapproval=='2'){
	$cond.='and A.is_blocked="1"';
	}else{
	$cond.='and A.imd_status="'.$immapproval.'" and A.is_blocked="0"';
	}
}
/*if($hrapproval!='')
{
$cond.='and A.HRstatus="'.$hrapproval.'"';
}*/
if($finalapproval!='')
{
if($finalapproval==1)
$cond.='and A.imd_status="'.$finalapproval.'"';
else
$cond.='and (A.imd_status="'.$finalapproval.'")';
}

if($_SESSION['usertype']=='Superadmin'){
	$query = "SELECT *,A.is_blocked as blocked FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								WHERE A.is_cancelled='0' $cond";

}else{
	$query = "SELECT *,A.is_blocked as blocked FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								WHERE A.employee_id=$userid AND A.is_cancelled='0' $cond";

}
		
include "pagingserch.php";

$limit=" LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=15;
}

$pagecnt=$start;

/*echo "SELECT * FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								WHERE A.employee_id=$userid $cond";
exit;*/
	
	if($_SESSION['usertype']=='Superadmin'){
		$leaves = $db->fetchQuery("SELECT *,A.is_blocked as blocked FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								WHERE A.is_cancelled='0' $cond ORDER BY A.id DESC $limit");
	
	} else {
		$leaves = $db->fetchQuery("SELECT *,A.is_blocked as blocked FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								WHERE A.employee_id=$userid AND A.is_cancelled='0' $cond  ORDER BY A.id DESC $limit");
	} 
								
	$count = count($leaves);
	

	$leaves[$count-1]['totalrows'] = $total_pages;
	$leaves[$count-1]['lastPage'] = $lastpage;
	echo '{"leaves":'.json_encode($leaves).'}';
}elseif($action == "changeStatus"){

	$set['imd_status'] = $_GET['set'];
	$where['id'] = $_GET['reqid'];
	$db->Update('payroll_user_leave_requests',$set,$where);
	
}

/*elseif($action == "HRchangeStatus"){

	$set['HRstatus'] = $_GET['set'];
	$where['id'] = $_GET['reqid'];
	$db->Update('payroll_user_leave_requests',$set,$where);
	
}*/
elseif($action == "cancelStatus"){

	$set['is_cancelled'] = $_GET['set'];
	$where['id'] = $_GET['reqid'];
	$db->Update('payroll_user_leave_requests',$set,$where);
	
}
elseif($action == "PFRequests"){

	$cond='';$memberid='';$add_date1='';$from_date1 = '';$to_date1 = '';$branch = '';$designation = '';
	$userid=$_GET['userid'];
	$depart_id=$_GET['depart_id'];
	
		
	if(!empty($_REQUEST['from_date'])){
	$from_date=$_REQUEST['from_date'];
	$from_date1 = date('Y-m-d', strtotime($from_date));
	}
	
	if(!empty($_REQUEST['to_date'])){
	$to_date=$_REQUEST['to_date'];
	$to_date1 = date('Y-m-d', strtotime($to_date));
	}
	
	if(!empty($_REQUEST['memberid'])){
	$memberid=$_REQUEST['memberid'];	
	}
	
	
	/*$set['is_cancelled'] = $_GET['set'];
	$where['id'] = $_GET['reqid'];
	$db->Update('payroll_user_leave_requests',$set,$where);*/
	//echo "select * from pf_esi_details ";
	//echo "select pf.*,emp.e_fname from pf_esi_details as pf join employee as emp  on emp.e_id = pf.emp_id ";
	
	
	if(!empty($from_date1))
	{
		$mnt1 = date('m',strtotime($from_date1));
		$yr1 = date('Y',strtotime($from_date1));
		
		//$day = date('j',strtotime($attsql[$j-1]['edate']));
		$cond.='and MONTH(pf.pay_period)='.$mnt1.' and YEAR(pf.pay_period)='.$yr1.' ';
	}
	
	if(!empty($memberid))
	{
		$cond.='and pf.emp_id ='.$memberid.'';
	}
	$query = "select pf.*,emp.e_fname from pf_esi_details as pf join employee as emp  on emp.e_id = pf.emp_id where 1 $cond";
	include "pagingserch.php";


	$cond.=" LIMIT $start, $limit";
	
	if(isset($_REQUEST['page'])){
	$page=$_REQUEST['page'];
	}else{
	$page=15;
	}
	
	$pagecnt=$start;
	
	
	//echo "select pf.*,emp.e_fname from pf_esi_details as pf join employee as emp  on emp.e_id = pf.emp_id where 1 $cond";
	$get_pfdetails  = $db->fetchQuery("select pf.*,emp.e_fname from pf_esi_details as pf join employee as emp  on emp.e_id = pf.emp_id where 1 $cond"); 
	
	$count = count($get_pfdetails);
	
	
	$get_pfdetails[$count-1]['totalrows'] = $total_pages;
	$get_pfdetails[$count-1]['lastPage'] = $lastpage;
	//}
	echo '{"get_pfdetails":'.json_encode($get_pfdetails).'}';	
	
}
elseif($action == "esiRequests"){

	$cond='';$memberid='';$add_date1='';$from_date1 = '';$to_date1 = '';$branch = '';$designation = '';
	$userid=$_GET['userid'];
	$depart_id=$_GET['depart_id'];
	
		
	if(!empty($_REQUEST['from_date'])){
	$from_date=$_REQUEST['from_date'];
	$from_date1 = date('Y-m-d', strtotime($from_date));
	}
	
	if(!empty($_REQUEST['to_date'])){
	$to_date=$_REQUEST['to_date'];
	$to_date1 = date('Y-m-d', strtotime($to_date));
	}
	
	if(!empty($_REQUEST['memberid'])){
	$memberid=$_REQUEST['memberid'];	
	}
	
	
	
	
	
	if(!empty($from_date1))
	{
		$mnt1 = date('m',strtotime($from_date1));
		$yr1 = date('Y',strtotime($from_date1));
		
		
		$cond.='and MONTH(esi.pay_period)='.$mnt1.' and YEAR(esi.pay_period)='.$yr1.' ';
	}
	
	if(!empty($memberid))
	{
		$cond.='and esi.emp_id ='.$memberid.'';
	}
	 $query = "select esi.*,emp.e_fname from pf_esi_details as esi join employee as emp  on emp.e_id = esi.emp_id where 1 $cond";
	include "pagingserch.php";

	$cond.=" LIMIT $start, $limit";
	
	if(isset($_REQUEST['page'])){
	$page=$_REQUEST['page'];
	}else{
	$page=15;
	}
	
	$pagecnt=$start;
	
	
	$get_esidetails  = $db->fetchQuery("select esi.*,emp.e_fname from pf_esi_details as esi join employee as emp  on emp.e_id = esi.emp_id where 1 $cond"); 
	
	$count = count($get_esidetails);
	
	
	$get_esidetails[$count-1]['totalrows'] = $total_pages;
	$get_esidetails[$count-1]['lastPage'] = $lastpage;
	
	echo '{"get_esidetails":'.json_encode($get_esidetails).'}';	
	
}

elseif($action == "loadTimesheetApproval"){

		$cond='';$from_date1 = '';$to_date1 = '';$immapproval='';
		$userid=$_GET['employee_id'];


if(!empty($_REQUEST['from_date'])){
$from_date=$_REQUEST['from_date'];
$from_date1 = date('Y-m-d', strtotime($from_date));
}

if(!empty($_REQUEST['to_date'])){
$to_date=$_REQUEST['to_date'];
$to_date1 = date('Y-m-d', strtotime($to_date));
}

if($_REQUEST['immapproval']!=''){
 $immapproval=$_REQUEST['immapproval'];

}
if(!empty($from_date1) && !empty($to_date1))
{
$cond.='and time_approval.edate>="'.$from_date1.'" and time_approval.edate<="'.$to_date1.'"';
}
if($immapproval!='')
{
$cond.='and time_approval.is_blocked="'.$immapproval.'"';
}

if($_SESSION['usertype']=='Superadmin')
{
 $query = "select * from employee join time_approval on employee.e_id = time_approval.eid where 1 $cond";
}
else
{
  $query = "select * from employee join time_approval on employee.e_id = time_approval.eid where employee.imd_supervisor_id =$userid $cond";
}	
include "pagingserch.php";

$cond.="order by time_approval.apprv_id desc LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=15;
}

$pagecnt=$start;

/*echo "SELECT * FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								WHERE A.employee_id=$userid $cond";
exit;*/
if($_SESSION['usertype']=='Superadmin')
{
	$gettimeapproval = $db->fetchQuery("select * from employee join time_approval on employee.e_id = time_approval.eid where 1 $cond" );
}
else
{	
	$gettimeapproval = $db->fetchQuery("select * from employee join time_approval on employee.e_id = time_approval.eid where employee.imd_supervisor_id =$userid $cond" );
}	
//echo $gettimeapproval[$count-1]['totalrows']= "select * from employee join time_approval on employee.e_id = time_approval.eid where 1 $cond";						
	$count = count($gettimeapproval);
	if($count>=1)
	{	
		$gettimeapproval[$count-1]['totalrows'] = $total_pages;
		$gettimeapproval[$count-1]['lastPage'] = $lastpage;
		echo '{"gettimeapproval":'.json_encode($gettimeapproval).'}';
	}
}
elseif($action == "loadTimesheetview"){

		$cond='';$from_date1 = '';$to_date1 = '';$immapproval='';
		$userid=$_GET['userid'];


if(!empty($_REQUEST['from_date'])){
$from_date=$_REQUEST['from_date'];
$from_date1 = date('Y-m-d', strtotime($from_date));
}

if(!empty($_REQUEST['to_date'])){
$to_date=$_REQUEST['to_date'];
$to_date1 = date('Y-m-d', strtotime($to_date));
}

if($_REQUEST['immapproval']!=''){
 $immapproval=$_REQUEST['immapproval'];

}
if(!empty($from_date1) && !empty($to_date1))
{
$cond.='and approval.edate>="'.$from_date1.'" and approval.edate<="'.$to_date1.'"';
}
if($immapproval!='')
{
$cond.='and approval.is_blocked="'.$immapproval.'"';
}



 $employee_id=$_GET['employee_id'];

  $query = "SELECT emp.e_fname as e_fname,approval.* FROM time_approval as approval join employee as emp on approval.eid = emp.e_id where approval.eid=$employee_id $cond";
		
include "pagingserch.php";

$cond.=" order by apprv_id desc LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=15;
}

$pagecnt=$start;

/*echo "SELECT * FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid 
								LEFT JOIN designation D ON B.e_post = D.desig_id 
								WHERE A.employee_id=$userid $cond";
exit;*/
	$gettimeapproval = $db->fetchQuery("SELECT emp.e_fname as e_fname,approval.* FROM time_approval as approval join employee as emp on approval.eid = emp.e_id where approval.eid=$employee_id $cond" );
		
		
								
	$count = count($gettimeapproval);
	
	
	$gettimeapproval[$count-1]['totalrows'] = $total_pages;
	$gettimeapproval[$count-1]['lastPage'] = $lastpage;
	echo '{"gettimeapproval":'.json_encode($gettimeapproval).'}';
}
elseif($action == "changeTimeStatus"){
	
	
	$set['is_blocked'] = $_GET['set'];
	$where['apprv_id'] = $_GET['reqid'];
	$db->Update('time_approval',$set,$where);
	
	$gettime = $db->fetchQuery("SELECT * FROM time_approval where apprv_id='".$_GET['reqid']."'" );
	//echo "<pre>";
	$dts = $gettime[0][edate];
	$eid = $gettime[0][eid];
	$timerin = $gettime[0][ein];
	$timerout = $gettime[0][eout];
	$totalWorkTime = $gettime[0][ework];
	$LogTimeIn = $gettime[0][timein];
	$dt_frtIn = $gettime[0][timeinloc];
	$LogTimeOut = $gettime[0][timeout];
	$dt_frtOut = $gettime[0][timeoutloc];
	$totalWorkTime = $gettime[0][totalworked_hours];
	
	if($_GET['set'] == 0)
	{
		mysql_query("delete from time where e_date = '$dts' and e_id = '$eid'");		
		//echo "update time_approval set is_blocked =  where apprv_id ='".$_GET['reqid']."'" ;
		mysql_query("update time_approval set is_blocked = '1' where apprv_id ='".$_GET['reqid']."'" );
		//$db->Update('time_approval',$set,$where);
	}
	else
	{
		
		mysql_query("delete from time where e_date = '$dts' and e_id = '$eid'");
		mysql_query("INSERT INTO time (e_date,e_id,deviceids,e_in,e_out,time_in,time_inloc,time_out,time_outloc,total_worked_hours) VALUES ('$dts','$eid','44','$timerin','$timerout','$LogTimeIn','$dt_frtIn','$LogTimeOut','$dt_frtOut','$totalWorkTime')");
		
		//echo "update time_approval set is_blocked = 0 where apprv_id ='".$_GET['reqid']."'" ;
		mysql_query("update time_approval set is_blocked = '0' where apprv_id ='".$_GET['reqid']."'" );
		//$db->Update('time_approval',$set,$where);
	}
		
}
elseif($action == "updateleave"){
	
	$vars['title'] 			= $_POST['leaveTitle'];
	$vars['description'] 	= $_POST['leaveDescription'];
	$vars['number_of_days'] = $_POST['days'];
	$vars['based_on']		= $_POST['based_on'];
	$where['lid']			= $_POST['lid'];
	$db->Update('payroll_leave',$vars,$where);
	
	
	
}
elseif($action == "getleavebyId"){
	
	$getleave = $db->fetchQuery("SELECT * FROM payroll_leave where lid 	='".$_GET['lid']."'" );
	echo '{"editleaves":'.json_encode($getleave).'}';
}
elseif($action == "cancelpersonalleave"){

	$set['is_cancelled'] = 1;
	$where['id'] = $_POST['id'];
	$db->Update('payroll_user_leave_requests',$set,$where);
	
}
elseif($action == "approveleave"){

	$status = $_POST['status'];
	if($status==1){
		$set['imd_status'] = 1;
		$set['is_blocked'] = 0;
	}
	if($status==0){	
		$set['is_blocked'] = 1;
		$set['imd_status'] = 0;
	}	
		if($status==''){	
		$set['is_blocked'] = 0;
		$set['imd_status'] = 0;
	}
	$where['id'] = $_POST['id'];
	$db->Update('payroll_user_leave_requests',$set,$where);
	
}
?>