<?php
$entry_date = date("Y-m-d H:i:s"); 

$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "SELECT user_name, condition_1, condition_2, condition_3, condition_4, condition_5, send_alert FROM $table_name WHERE alert_name = 'late' ";
	$alert_result = mySQL_query($alert_query);
	$alert_num = mysql_num_rows($alert_result);
	$user_name = mysql_result($alert_result, 0, "user_name");
	$days = mysql_result($alert_result, 0, "condition_1");
		
	$check_date = strtotime(date("Y-m-d", strtotime($entry_date)) . " -".$days." day");
	
	$check_date = date("Y-m-d", $check_date);
	
$table_name = $_SESSION['db_prefix']."_time_sheet";
$late_query = "SELECT emp_id FROM $table_name WHERE emp_id = '".$emp_id."' AND attendance_status = 'Late' AND attendance_date > '".$check_date."' ";
$late_result = mySQL_query($late_query);
$late_num = mysql_num_rows($late_result);


$table_name2 = $_SESSION['db_prefix']."_employee";
$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$emp_id."' ";
$employee_result = mySQL_query($employee_query);
$employee_num = mysql_num_rows($employee_result);	
$emp_name = mysql_result($employee_result, 0, "emp_first_name").' '.mysql_result($employee_result, 0, "emp_last_name"); 
					
if ($late_num>=3) {		
		$table_name = $_SESSION['db_prefix']."_alert_message";
		$message_query = "INSERT INTO $table_name (user_name, department, generated, alert, detail_1, detail_2, detail_3 ) VALUES ('".$user_name."', 'hr', '".$entry_date."', '40', '".$emp_id."', '".$check_date."', '".$late_num."' ) ";
		execsql($message_query);
	
	}
	
