<?php

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s"); 

$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "SELECT user_name, condition_1, send_alert, send_email FROM $table_name WHERE alert_name = 'Late coming' ";
	$alert_result = mySQL_query($alert_query);
	$alert_num = mysql_num_rows($alert_result);
	
			
	$k=0;
	$email_address = '';
	while ($k<$alert_num) {
		$user_name = mysql_result($alert_result, $k, "user_name");
		$send_alert = mysql_result($alert_result, $k, "send_alert");
		$send_email = mysql_result($alert_result, $k, "send_email");
		$period = mysql_result($alert_result, $k, "condition_1");
		
$table_name = $_SESSION['db_prefix']."_users";
$mail_query = "SELECT email  FROM $table_name WHERE  username = '".$user_name."' ";
$mail_result = mySQL_query($mail_query);
$mail_num = mysql_num_rows($mail_result);
if ($mail_num>0) {
$email_address = mysql_result($mail_result, 0, "email");
}
else {
	$email_address = '';
}		
	
		

$table_name = $_SESSION['db_prefix']."_time_sheet";
$sheet_query = "SELECT emp_id, attendance_status FROM $table_name WHERE attendance_date = '".$recording_date."' ";
$sheet_result = mySQL_query($sheet_query);
$sheet_num = mysql_num_rows($sheet_result);


$search_date = strtotime(date("Y-m-d", strtotime($recording_date)) . " -".$period." day");
$search_date = date('Y-m-d', $search_date);

$report_text='';
$report_text_1='';
$report_text_2='';
$report_text_3='';
$i=0;
while ($i<$sheet_num) {
		
	$emp_id = mysql_result($sheet_result, $i, "emp_id");
	$attendance_status = mysql_result($sheet_result, $i, "attendance_status");
	
	if ($attendance_status=='late') {
	$table_name = $_SESSION['db_prefix']."_time_sheet";
	$late_query = "SELECT attendance_date, check_in FROM $table_name WHERE emp_id = '".$emp_id."' AND attendance_status = 'late'  AND attendance_date > '".$search_date."' ORDER BY attendance_date DESC";
	$late_result = mySQL_query($late_query);
	$late_num = mysql_num_rows($late_result);
	
	if ($late_num>2) {
	$report_text_2='';
	$report_text_3='';	
		
	$attendance_date = mysql_result($late_result, 0, "attendance_date");	
	$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 ($send_alert=='yes') {
	$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "INSERT INTO $table_name (user_name, generated, department, alert, detail_1, detail_2, detail_3, detail_4) VALUES ('".$user_name."', '".$entry_date."', 'hr', '21', '".$emp_id."', '".$attendance_date."', '".$period."', '".$late_num."' ) ";
	execsql($alert_query);
	}
	
		
	$report_text_1 = 'Employee: <b>'.$emp_name.'</b><br>Late recordings:<br>';
	$j=0;
	while ($j<$late_num) {
	   $attendance_date = mysql_result($late_result, $j, "attendance_date");
	   $check_in = mysql_result($late_result, $j, "check_in");
	   $report_text_2 = $report_text_2.date("d-m-Y", strtotime($attendance_date)).': '.$check_in.'<br>';
	   $j++;
	}
	$report_text_3='<br><br>';
	$report_text = $report_text.$report_text_1.$report_text_2.$report_text_3;
	
	}
	}
	$i++;
}

if ($report_text!='') {
if ($send_email=='yes') {
if ($email_address!='') {

$subject = 'Tlhomeso Timbers Francistow late reporting';
$message = '<body style="font-family:Trebuchet MS, Arial, Helvetica, sans-serif; font-size:12px;">The following employees have been late 3 times or more in the past '.$period.' days:<br><br>'.$report_text.'<br><br>Recording date: '.date("l, d-m-Y", strtotime($entry_date)).'<br><br><br>For more info, login at <a href=costing.tocotex.com/bypass.php?user='.$user_name.'&c=ftw&a=late&r=&e=>Thomeso Timbers Francistown Management Program</a>.<br><br><font face=Calibri size=3 color=red><b>This message comes from a non-responsive address, please do not reply</b></body>';

include('../include/alert_to_email.php');
}
}
}
$k++;	
}
?>
