<?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, send_alert, send_email FROM $table_name WHERE alert_name = 'Warning' ";
	$alert_result = mySQL_query($alert_query);
	$alert_num = mysql_num_rows($alert_result);
	
			
	$i=0;
	$email_address = '';
	while ($i<$alert_num) {
		$user_name = mysql_result($alert_result, $i, "user_name");
		$send_alert = mysql_result($alert_result, $i, "send_alert");
		$send_email = mysql_result($alert_result, $i, "send_email");
		
		if ($send_alert=='yes') {
			$table_name = $_SESSION['db_prefix']."_alerts_hr";
			$message_query = "INSERT INTO $table_name (user_name, department, generated, alert, detail_1, detail_2, detail_3 ) VALUES ('".$user_name."', 'hr', '".$entry_date."', '22', '".$warning_id."', '".$_SESSION['username']."', '".$_SESSION['emp_id']."' ) ";
			execsql($message_query);
		}
		
		
		if ($send_email=='yes') {
			$table_name = $_SESSION['db_prefix']."_users";
			$address_query = "SELECT email FROM $table_name WHERE username = '".$user_name."' ";
			$address_result = mySQL_query($address_query);
			$email_address = mysql_result($address_result, 0, "email").', '.$email_address;
		}
	$i++;	
	}

$table_name2 = $_SESSION['db_prefix']."_employee";
$employee_query = "SELECT emp_first_name, emp_last_name FROM $table_name2 WHERE emp_id ='".$_SESSION['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 ($email_address!='') {			
			$subject = "Final warning given at Tlhomeso Timbers";
			$message = "<body><font face=Calibri size=3>A final warning was given to ".$emp_name."<br><br>For more info, login at <a href=costing.tocotex.com/bypass.php?c=tlh&a=warning&r=".$warning_id."&e=>Tlhomeso Timbers 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></font><body>";
			include ('alert_to_email.php');
		}
			

	
	
?>