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

$action = $_GET['act'];
if($action == "list")
{
	$cat		= $_GET['cat'];
	$result		=  $db->fetchQuery("SELECT notify_days FROM notification");
	$interval 	=  $result["0"]['notify_days']?$result["0"]['notify_days']:"30";
	
	if($cat=="passport"){
		
		$result = $db->fetchQuery("SELECT e_fname,e_mname,e_lname,passport_exp_date as exp_date ,
								   CASE 
								   	   WHEN STR_TO_DATE(passport_exp_date, '%d-%m-%Y') >curdate() THEN  `passport_exp_date`
								       WHEN STR_TO_DATE(passport_exp_date, '%d-%m-%Y') <curdate() THEN 'Passport Expired'  
								   ELSE 'Passport will expire on Today' END as message
								   FROM employee 
								   WHERE curdate()>=DATE_SUB(STR_TO_DATE(passport_exp_date, '%d-%m-%Y') , INTERVAL ".$interval." DAY)");
		echo '{"notification":'.json_encode($result).'}';
	}
	
	if($cat=="visa"){
		
		$result = $db->fetchQuery("SELECT e_fname,e_mname,e_lname,visa_exp_date as exp_date ,
								   CASE 
								   	   WHEN STR_TO_DATE(visa_exp_date, '%d-%m-%Y') >curdate() THEN  `visa_exp_date`
								       WHEN STR_TO_DATE(visa_exp_date, '%d-%m-%Y') <curdate() THEN 'Visa Expired'  
								   ELSE 'Visa will expire on Today' END as message
								   FROM employee  
								   WHERE curdate()>=DATE_SUB(STR_TO_DATE(visa_exp_date, '%d-%m-%Y') , INTERVAL ".$interval." DAY)");
		echo '{"notification":'.json_encode($result).'}';
	}
	
	if($cat=="probation"){
		
		$res	= mysql_query("SELECT e_id,emp_prob_period 	FROM employee");
		while($row = mysql_fetch_array($res)){
			$emp_list[] = $row['e_id'];
			$emp_pb[]   = $row['emp_prob_period'];
		}
		$emp_id		= join(',',$emp_list);
		$emp_prob	= join(',',$emp_pb);
		
		$result = $db->fetchQuery("SELECT e_fname,e_mname,e_lname,hist_comp_prob as exp_date,
								   CASE 
								   	   WHEN STR_TO_DATE(hist_comp_prob, '%d-%m-%Y') >curdate() THEN 'Probation will complete on' 
								       WHEN STR_TO_DATE(hist_comp_prob, '%d-%m-%Y') <curdate() THEN 'Probation completed'  
								   ELSE 'Probation complete on Today' END as message 
								   FROM employee  
								   WHERE curdate()>=DATE_SUB(STR_TO_DATE(hist_comp_prob, '%d-%m-%Y') , INTERVAL ".$interval." DAY) AND e_id IN('$emp_id') AND emp_prob_period IN('$emp_prob')");
		echo '{"notification":'.json_encode($result).'}';
	}
	
	
	if($cat=="contract"){
		
		$result = $db->fetchQuery("SELECT e_fname,e_mname,e_lname,hist_comp_prob as exp_date 
									FROM employee  
									WHERE curdate()>=DATE_SUB(STR_TO_DATE(visa_exp_date, '%d-%m-%Y') , INTERVAL ".$interval." DAY)");
		echo '{"notification":'.json_encode($result).'}';
	}
	
	if($cat=="leave"){
		
		$result = $db->fetchQuery("SELECT e_fname,e_mname,e_lname,hist_comp_prob as exp_date 
									FROM employee  
									WHERE curdate()>=DATE_SUB(hist_comp_prob, INTERVAL ".$interval." DAY)");
		echo '{"notification":'.json_encode($result).'}';
	}
}
elseif($action == "listNotifi"){
	//////////////////////////////user rights checking//////////////////////////////////////

	$userid=$_SESSION['admin_id'];

	$sub_rights=mysql_fetch_array(mysql_query("select * from menu  where  menu_name='Notifications'  "));

	$view_rights=mysql_fetch_array(mysql_query("select * from tbl_user_rights where user_id='$userid' and m_menu_id='$sub_rights[menu_id]'"));

	//////////////////////////////user rights checking//////////////////////////////////////

	$result		=  $db->fetchQuery("SELECT notify_days FROM notification");
	$interval 	=  $result["0"]['notify_days']?$result["0"]['notify_days']:"30";
	$noti_rows='';
	$i=1;
	$current_date = date('d-m-Y');
	
	/*if($userid!=1)
	{
	    if($view_rights['view_right']==0 )
        {
		
        	
	$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where  e_id='$userid'");
	    }
		else
		{
			
	$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where user_type!='Superadmin'");
		}
	}
	else
	{
		
	$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where user_type!='Superadmin'");
	}*/
	
	if($_SESSION['usertype']=='Superadmin')
	{
		$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where user_type!='Superadmin'");
	}
	elseif($_SESSION['branchadmin']=='Y')
	{
		$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where user_type!='Superadmin' and branchid=".$_SESSION['branch_id']."");
	}
	else
	{
		if($view_rights['view_right']==0 )
        {
			$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where  e_id='$userid'");
	    }
		else
		{
			$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where user_type!='Superadmin' and branchid=".$_SESSION['branch_id']."");	
		}
	}
	

	foreach($result as $row)
	{
			$pass_exp_date =  date('d-m-Y', strtotime($row['pass_exp_dat']. ' - '.$interval.' days'));
			$visa_exp_date =  date('d-m-Y', strtotime($row['vsa_exp_dat']. ' - '.$interval.' days'));
			$emirates_exp_date =  date('d-m-Y', strtotime($row['emirates_expiry_date']. ' - '.$interval.' days'));
			$labour_exp_date =  date('d-m-Y', strtotime($row['labour_card_expiry_date']. ' - '.$interval.' days'));
			
			if($row['emp_prob_period']=='No probation')
			{
				$probation_date = date('d-m-Y', strtotime($row['date_join']. ' + 0 days'));
				$probation_date =  date('d-m-Y', strtotime($probation_date. ' - '.$interval.' days'));
				$probation_column='<td>-</td>';
			}
			elseif($row['emp_prob_period']=='6 Month')
			{
				$probation_comp_date = date('d-m-Y', strtotime($row['date_join']. ' + 6 months'));
				$probation_date =  date('d-m-Y', strtotime($probation_comp_date. ' - '.$interval.' days'));
				if($row['date_join'])
				{
					if(strtotime($probation_date) > strtotime($current_date))
					{
						$probation_column='<td style="background-color:#33AD33;color:white">'.$probation_comp_date.'</td>';
					}
					else
					{
						$probation_column='<td style="background-color:#FF4D4D;color:white">'.$probation_comp_date.'</td>';
					}
				}
				else
				{
					$probation_column='<td>-</td>';
				}
			}
			elseif($row['emp_prob_period']=='1 Year')
			{
				$probation_comp_date = date('d-m-Y', strtotime($row['date_join']. ' + 1 year'));
				$probation_date =  date('d-m-Y', strtotime($probation_comp_date. ' - '.$interval.' days'));
				if($row['date_join'])
				{
					if(strtotime($probation_date) > strtotime($current_date))
					{
						$probation_column='<td style="background-color:#33AD33;color:white">'.$probation_comp_date.'</td>';
					}
					else
					{
						$probation_column='<td style="background-color:#FF4D4D;color:white">'.$probation_comp_date.'</td>';
					}
				}
				else
				{
					$probation_column='<td>-</td>';
				}
			}
			else
			{
				$probation_column='<td>-</td>';
			}
			if($row['pass_exp_dat'])
			{
				if(strtotime($pass_exp_date) > strtotime($current_date))
				{
					$pass_column='<td style="background-color:#33AD33;color:white">'.date('d-m-Y', strtotime($row['pass_exp_dat'])).'</td>';
				}
				else
				{
					$pass_column='<td style="background-color:#FF4D4D;color:white">'.date('d-m-Y', strtotime($row['pass_exp_dat'])).'</td>';
				}
			}
			else
			{
				$pass_column='<td>-</td>';
			}
			
			if($row['vsa_exp_dat'])
			{
				if(strtotime($visa_exp_date) > strtotime($current_date))
				{
					$visa_column='<td style="background-color:#33AD33;color:white">'.date('d-m-Y', strtotime($row['vsa_exp_dat'])).'</td>';
				}
				else
				{
					$visa_column='<td style="background-color:#FF4D4D;color:white">'.date('d-m-Y', strtotime($row['vsa_exp_dat'])).'</td>';
				}
			}
			else
			{
				$visa_column='<td>-</td>';
			}
			
			
			
			if($row['emp_contract']=='U')
			{
				$contract_column='<td>-</td>';
			}
			elseif($row['emp_contract']=='L')
			{
				if($row['contract_end_date'])
				{
					$contract_exp_date = date('d-m-Y', strtotime($row['contract_end_date']. ' - '.$interval.' days'));
					if(strtotime($contract_exp_date) > strtotime($current_date))
					{
						$contract_column='<td style="background-color:#33AD33;color:white">'.$row['contract_end_date'].'</td>';
					}
					else
					{
						$contract_column='<td style="background-color:#FF4D4D;color:white">'.$row['contract_end_date'].'</td>';
					}
				}
				else
				{
					$contract_column='<td>-</td>';
				}
			}
			else
			{
				$contract_column='<td>-</td>';
			}
			
			$explode_date_of_join=explode('-',$row['date_join']);
			//$join_year=$explode_date_of_join[2];
			$year=date("Y");
			if($explode_date_of_join[2]==$year)
			{
				$month=$explode_date_of_join[1];
				$firstOfLastmth=$explode_date_of_join[2].'-'.$explode_date_of_join[1].'-'.$explode_date_of_join[0];
			}
			else
			{
				$month='01';
				$firstOfLastmth=$year.'-01-01';
			}
			$availedleaves=0;
			
			
			$endOfLastmth=date('Y-m-d',strtotime("-1 days"));
			
			//$lastMtnHolidays = $db->fetchQuery("SELECT * FROM payroll_holiday_calendar WHERE MONTH(date)='$month' and YEAR(date)='$year' and is_blocked='0' group by date");
			$lastMtnHolidays = $db->fetchQuery("SELECT * FROM payroll_holiday_calendar WHERE date between '".$firstOfLastmth."' and subdate(CURDATE(),1) and is_blocked='0' ");
			$holidays=array();
			foreach($lastMtnHolidays as $lastMtnHolidays){
			   $holidays[]=$lastMtnHolidays['date'];
			}
			
			$noofholidays=count($holidays);

			$workingDays=  $db->getWorkingDays($firstOfLastmth,$endOfLastmth,$holidays);
			//$lastyrattendance = $db->fetchQuery("SELECT count(distinct(e_date)) FROM time WHERE e_date>='$firstOfLastmth' and e_date<='$endOfLastmth' and e_id='".$row['e_id']."'");
			$lastyrattendance = $db->fetchQuery("SELECT count(distinct(e_date)) FROM time WHERE e_date>='$firstOfLastmth' and e_date<=subdate(CURDATE(),1) and e_id='".$row['e_id']."'");
			$yrattendance=$lastyrattendance[0]['count(distinct(e_date))'];
			
			//$nolossleaveapplied = $db->fetchQuery("Select count(*) as nolossleave from payroll_user_leave_requests where employee_id=$employees and is_cancelled=0 and imd_status=1 and day_from>='$firstOfLastmth2' and day_from<='$endOfLastmth' and (leave_id='3' or leave_id='1') ");
			
			/*$approved_leaves_array =array();
			$approved_leaves_array = $db->fetchQuery("Select day_from,half_full_day from payroll_user_leave_requests where employee_id='".$row['e_id']."' and is_cancelled=0 and imd_status=1 and is_blocked=0 and day_from>='$firstOfLastmth2' and day_from<=CURDATE() ");
			
			$approved_leaves=0;
			
			if(!empty($approved_leaves_array))
			{
				$leave_date_array = array_keys($approved_leaves_array);
				$leave_day_concept_array = array_values($approved_leaves_array);
				
				$leave_date_clause = join(',',$leave_date_array); 
				$approved_leaves_query=$db->fetchQuery("SELECT count(distinct(e_date)) as leave_count FROM time WHERE e_date in (".$leave_date_clause.") and e_id='".$row['e_id']."' ");
				
				$approved_leaves_present=$approved_leaves_query[0]['leave_count']; 
			}*/
			
			
			
			$availedleaves=$workingDays-$yrattendance;
			//$availedleaves=$workingDays-($yrattendance+$approved_leaves_present);
			
			//$db->fetchQuery("SELECT count(distinct(e_date)) FROM time WHERE e_date>='$firstOfLastmth' and e_date<=CURDATE() and e_id='".$row['e_id']."' and ");
			
			if($availedleaves)
			{
				$leave_column='<td style="background-color:#33AD33;color:white">'.$availedleaves.'</td>';
			}
			else
			{
				$leave_column='<td>-</td>';
			}
			
			if($row['emirates_expiry_date'])
			{
				if(strtotime($emirates_exp_date) > strtotime($current_date))
				{
					$emirates_column='<td style="background-color:#33AD33;color:white">'.$row['emirates_expiry_date'].'</td>';
				}
				else
				{
					$emirates_column='<td style="background-color:#FF4D4D;color:white">'.$row['emirates_expiry_date'].'</td>';
				}
			}
			else
			{
				$emirates_column='<td>-</td>';
			}
			
			if($row['labour_card_expiry_date'])
			{
				if(strtotime($labour_exp_date) > strtotime($current_date))
				{
					$labour_column='<td style="background-color:#33AD33;color:white">'.$row['labour_card_expiry_date'].'</td>';
				}
				else
				{
					$labour_column='<td style="background-color:#FF4D4D;color:white">'.$row['labour_card_expiry_date'].'</td>';
				}
			}
			else
			{
				$labour_column='<td>-</td>';
			}

			$noti_rows.='<tr>
				<td>'.$i.'</td>
				<td>'.$row['employee_id'].'</td>
				<td>'.$row['employee_name'].'</td>
				'.$pass_column.$visa_column.$probation_column.$contract_column.$emirates_column.$labour_column.$leave_column.'
			</tr>';
			$i++;
	}
	
	$employee_array = $db->fetchQuery("SELECT e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,employee_id FROM employee where user_type!='Superadmin'");
	$employee_combo='<option value="0">Select Employee</option>';
	if(!empty($employee_array))
	{
		foreach($employee_array as $employee_row)
		{
			$employee_combo.='<option value='.$employee_row['e_id'].'>'.$employee_row['employee_name'].'</option>';
		}
	}
	
	$employee_code_combo='<option value="0">Select Code</option>';
	if(!empty($employee_array))
	{
		foreach($employee_array as $employee_row)
		{
			if($employee_row['employee_id'])
			{
				$employee_code_combo.='<option value='.$employee_row['e_id'].'>'.$employee_row['employee_id'].'</option>';
			}
		}
	}
	echo json_encode(array('noti_list' => $noti_rows,'employee_combo' => $employee_combo,'employee_code' => $employee_code_combo));
}
elseif($action=="searchlist")
{
	$where_cond="where user_type!='Superadmin'";
	if($_GET['employee_id'])
	{
		$where_cond.=" and e_id='".$_GET['employee_id']."'";
	}
	if($_GET['employee_code'])
	{
		$where_cond.=" and e_id='".$_GET['employee_code']."'";
	}
	
	//////////////////////////////user rights checking//////////////////////////////////////

$userid=$_SESSION['admin_id'];

$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  m.menu_name='Notifications' and m.menu_id=s.menu_id "));

$view_rights=mysql_fetch_array(mysql_query("select * from tbl_user_rights where user_id='$userid' and s_submenu_id='$sub_rights[s_id]'"));

//////////////////////////////user rights checking//////////////////////////////////////
		/*if($userid!=1)
	{
	    if($view_rights['view_right']==0 )
        {
        $cond.=' and e_id='.$userid.'';
	    }
	}
	*/
	$result		=  $db->fetchQuery("SELECT notify_days FROM notification");
	$interval 	=  $result["0"]['notify_days']?$result["0"]['notify_days']:"30";
	$noti_rows='';
	$i=1;
	$current_date = date('d-m-Y');
	
	//$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee ".$where_cond." ");
	if($_SESSION['usertype']=='Superadmin')
	{
		$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where user_type!='Superadmin'");
	}
	elseif($_SESSION['branchadmin']=='Y')
	{
		$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where user_type!='Superadmin' and branchid=".$_SESSION['branch_id']."");
	}
	else
	{
		if($view_rights['view_right']==0 )
        {
			$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where  e_id='$userid'");
	    }
		else
		{
			$result = $db->fetchQuery("SELECT employee_id,e_id,CONCAT_WS(' ',e_fname, e_lname) as employee_name,pass_exp_dat,vsa_exp_dat,date_join,emp_prob_period,contract_end_date,emp_contract,emirates_expiry_date,labour_card_expiry_date FROM employee where user_type!='Superadmin' and branchid=".$_SESSION['branch_id']."");	
		}
	}
	
	
	foreach($result as $row)
	{
			$pass_exp_date =  date('d-m-Y', strtotime($row['pass_exp_dat']. ' - '.$interval.' days'));
			$visa_exp_date =  date('d-m-Y', strtotime($row['vsa_exp_dat']. ' - '.$interval.' days'));
			$emirates_exp_date =  date('d-m-Y', strtotime($row['emirates_expiry_date']. ' - '.$interval.' days'));
			$labour_exp_date =  date('d-m-Y', strtotime($row['labour_card_expiry_date']. ' - '.$interval.' days'));
			
			if($row['emp_prob_period']=='No probation')
			{
				$probation_date = date('d-m-Y', strtotime($row['date_join']. ' + 0 days'));
				$probation_date =  date('d-m-Y', strtotime($probation_date. ' - '.$interval.' days'));
				$probation_column='<td>-</td>';
			}
			elseif($row['emp_prob_period']=='6 Month')
			{
				$probation_comp_date = date('d-m-Y', strtotime($row['date_join']. ' + 6 months'));
				$probation_date =  date('d-m-Y', strtotime($probation_comp_date. ' - '.$interval.' days'));
				if($row['date_join'])
				{
					if(strtotime($probation_date) > strtotime($current_date))
					{
						$probation_column='<td style="background-color:#33AD33;color:white">'.$probation_comp_date.'</td>';
					}
					else
					{
						$probation_column='<td style="background-color:#FF4D4D;color:white">'.$probation_comp_date.'</td>';
					}
				}
				else
				{
					$probation_column='<td>-</td>';
				}
			}
			elseif($row['emp_prob_period']=='1 Year')
			{
				$probation_comp_date = date('d-m-Y', strtotime($row['date_join']. ' + 1 year'));
				$probation_date =  date('d-m-Y', strtotime($probation_comp_date. ' - '.$interval.' days'));
				if($row['date_join'])
				{
					if(strtotime($probation_date) > strtotime($current_date))
					{
						$probation_column='<td style="background-color:#33AD33;color:white">'.$probation_comp_date.'</td>';
					}
					else
					{
						$probation_column='<td style="background-color:#FF4D4D;color:white">'.$probation_comp_date.'</td>';
					}
				}
				else
				{
					$probation_column='<td>-</td>';
				}
			}
			else
			{
				$probation_column='<td>-</td>';
			}
			if($row['pass_exp_dat'])
			{
				if(strtotime($pass_exp_date) > strtotime($current_date))
				{
					$pass_column='<td style="background-color:#33AD33;color:white">'.date('d-m-Y', strtotime($row['pass_exp_dat'])).'</td>';
				}
				else
				{
					$pass_column='<td style="background-color:#FF4D4D;color:white">'.date('d-m-Y', strtotime($row['pass_exp_dat'])).'</td>';
				}
			}
			else
			{
				$pass_column='<td>-</td>';
			}
			
			if($row['vsa_exp_dat'])
			{
				if(strtotime($visa_exp_date) > strtotime($current_date))
				{
					$visa_column='<td style="background-color:#33AD33;color:white">'.date('d-m-Y', strtotime($row['vsa_exp_dat'])).'</td>';
				}
				else
				{
					$visa_column='<td style="background-color:#FF4D4D;color:white">'.date('d-m-Y', strtotime($row['vsa_exp_dat'])).'</td>';
				}
			}
			else
			{
				$visa_column='<td>-</td>';
			}
			
			
			
			if($row['emp_contract']=='U')
			{
				$contract_column='<td>-</td>';
			}
			elseif($row['emp_contract']=='L')
			{
				if($row['contract_end_date'])
				{
					$contract_exp_date = date('d-m-Y', strtotime($row['contract_end_date']. ' - '.$interval.' days'));
					if(strtotime($contract_exp_date) > strtotime($current_date))
					{
						$contract_column='<td style="background-color:#33AD33;color:white">'.$row['contract_end_date'].'</td>';
					}
					else
					{
						$contract_column='<td style="background-color:#FF4D4D;color:white">'.$row['contract_end_date'].'</td>';
					}
				}
				else
				{
					$contract_column='<td>-</td>';
				}
			}
			else
			{
				$contract_column='<td>-</td>';
			}
			
			$explode_date_of_join=explode('-',$row['date_join']);
			//$join_year=$explode_date_of_join[2];
			$year=date("Y");
			if($explode_date_of_join[2]==$year)
			{
				$month=$explode_date_of_join[1];
				$firstOfLastmth=$explode_date_of_join[2].'-'.$explode_date_of_join[1].'-'.$explode_date_of_join[0];
			}
			else
			{
				$month='01';
				$firstOfLastmth=$year.'-01-01';
			}
			$availedleaves=0;
			
			
			$endOfLastmth=date('Y-m-d',strtotime("-1 days"));
			
			$lastMtnHolidays = $db->fetchQuery("SELECT * FROM payroll_holiday_calendar WHERE date between '".$firstOfLastmth."' and subdate(CURDATE(),1) and is_blocked='0' ");
			$holidays=array();
			foreach($lastMtnHolidays as $lastMtnHolidays){
			   $holidays[]=$lastMtnHolidays['date'];
			}
			$noofholidays=count($holidays);
			$workingDays=  $db->getWorkingDays($firstOfLastmth,$endOfLastmth,$holidays);
			$q="SELECT count(distinct(e_date)) FROM time WHERE e_date>='$firstOfLastmth' and e_date<=subdate(CURDATE(),1) and e_id='".$row['e_id']."'";
			$lastyrattendance = $db->fetchQuery("SELECT count(distinct(e_date)) FROM time WHERE e_date>='$firstOfLastmth' and e_date<=subdate(CURDATE(),1) and e_id='".$row['e_id']."'");
			$yrattendance=$lastyrattendance[0]['count(distinct(e_date))'];
			$availedleaves=$workingDays-$yrattendance;
			if($availedleaves)
			{
				$leave_column='<td style="background-color:#33AD33;color:white">'.$availedleaves.'</td>';
			}
			else
			{
				$leave_column='<td>-</td>';
			}
			
			if($row['emirates_expiry_date'])
			{
				if(strtotime($emirates_exp_date) > strtotime($current_date))
				{
					$emirates_column='<td style="background-color:#33AD33;color:white">'.$row['emirates_expiry_date'].'</td>';
				}
				else
				{
					$emirates_column='<td style="background-color:#FF4D4D;color:white">'.$row['emirates_expiry_date'].'</td>';
				}
			}
			else
			{
				$emirates_column='<td>-</td>';
			}
			
			if($row['labour_card_expiry_date'])
			{
				if(strtotime($labour_exp_date) > strtotime($current_date))
				{
					$labour_column='<td style="background-color:#33AD33;color:white">'.$row['labour_card_expiry_date'].'</td>';
				}
				else
				{
					$labour_column='<td style="background-color:#FF4D4D;color:white">'.$row['labour_card_expiry_date'].'</td>';
				}
			}
			else
			{
				$labour_column='<td>-</td>';
			}

			$noti_rows.='<tr>
				<td>'.$i.'</td>
				<td>'.$row['employee_id'].'</td>
				<td>'.$row['employee_name'].'</td>
				'.$pass_column.$visa_column.$probation_column.$contract_column.$emirates_column.$labour_column.$leave_column.'
			</tr>';
			$i++;
	}
	echo json_encode(array('noti_list' => $noti_rows));
}
if($action=="notify_settings")
{
	$result		=  $db->fetchQuery("SELECT notify_days FROM notification");
	echo '{"notify_day":'.json_encode($result).'}';
}

if($action=="insert")
{
	$result		=  $db->fetchQuery("SELECT id,notify_days FROM notification");
	
	if ($result['0']['id'] ==""){
		$data['notify_days'] = $_POST['notify_days'];
		$db->Insert($data,'notification');
	}
	else {
		$where['id']        = $result['0']['id'];
		$set['notify_days'] = $_POST['notify_days'];
		$db->Update('notification',$set,$where);
	}	
}

if($action=="get")
{

$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  s.s_menu_name='Notification' and m.menu_id=s.menu_id "));
	
 $userid=$_SESSION['admin_id'];

	$where['user_id']	= $userid;
	$where['s_submenu_id']	= $sub_rights['s_id'];
	
	

	/*$result		= $db->fetchQuery("SELECT id,notify_days FROM notification");
	$id         = $result['0']['notify_days']?$result['0']['notify_days']:"";
	echo $id;	*/
	
		$companyDetails = $db->Select('notification');
	
			if($userid==1)
	{
	$companyDetails[0]['permissions'][0] = array("user_id"=>$userid);
	}
	else
	{
	$companyDetails[0]['permissions']  =  $db->fetchAll("tbl_user_rights",$where);
	}
	
	echo json_encode($companyDetails);
	
	
	
	
}


?>