<?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 = $con->query($alert_query);
				
	
	$email_address = '';
	$row = mysqli_fetch_assoc($alert_result);
		$user_name = $row["user_name"];
		$send_alert = $row["send_alert"];
		$send_email = $row["send_email"];
		$period = $row["condition_1"];
		
$table_name = $_SESSION['db_prefix']."_users";
$mail_query = "SELECT email  FROM $table_name WHERE  username = '".$user_name."' ";
$mail_result = $con->query($mail_query);
$mail_num = mysqli_num_rows($mail_result);
if ($mail_num>0) {
	$row = mysqli_fetch_assoc($mail_result);
	$email_address = $row['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 = $con->query($sheet_query);

$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='';
while($row = $sheet_result->fetch_assoc()) {
	$emp_id = $row["emp_id"];
	$attendance_status = $row["attendance_status"];
	//$attendance_date = $row["attendance_date"];
		
	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 = $con->query($late_query);
	$late_num = mysqli_num_rows($late_result);
	$row = mysqli_fetch_assoc($late_result);
	$attendance_date = $row["attendance_date"];
	$check_in = $row["check_in"];
	
	
	if ($late_num>2) {
	$report_text_2='';
	$report_text_3='';	
	
	
		
		
	$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 = $con->query($employee_query);
	$row = mysqli_fetch_assoc($employee_result);
	$emp_name = $row['emp_first_name'].' '.$row['emp_last_name'];
		
	if ($send_alert=='yes') {
	$table_name = $_SESSION['db_prefix']."_alerts_hr";
	$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."' ) ";
	$alert_result = $con->query($alert_query);
	}
		
	$report_text_1 = 'Employee: <b>'.$emp_name.'</b><br>Late recordings:<br>';
	
	
	$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 = $con->query($late_query);
	while($row = $late_result->fetch_assoc()) {
	$attendance_date = $row["attendance_date"];
	$check_in = $row["check_in"];
	
	$report_text_2 = $report_text_2.date("d-m-Y", strtotime($attendance_date)).': '.$check_in.'<br>';
	
	}
	$report_text_3='<br><br>';
	$report_text = $report_text.$report_text_1.$report_text_2.$report_text_3;
	
	}
	}
	
}

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

$subject = 'Radstone Phakalane 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=rad&a=late&r=&e=>Radstone Phakalane 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');
}
}
}


?>
