<?php

$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "SELECT user_name, send_alert, send_email FROM $table_name WHERE alert_name = 'cutting_timber' AND condition_1 > ".$result_value." ";
	$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']."_alert_message";
			$message_query = "INSERT INTO $table_name (user_name, department, generated, alert, detail_1, detail_2, detail_3 ) VALUES ('".$user_name."', 'production', '".$entry_date."', '13', '".$_SESSION['job_no']."', '".$job_value."', '".$captured_value."' ) ";
			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_name = $_SESSION['db_prefix']."_captured_jobs";
$project_query = "SELECT cust_id, project_description, project_no, project_place FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' ";
$project_result = mySQL_query($project_query);
$project_num = mysql_num_rows($project_result);
$cust_id = mysql_result($project_result, 0, "cust_id");
$project_description = mysql_result($project_result, 0, "project_description");	
$project_no = mysql_result($project_result, 0, "project_no");	
$project_place = mysql_result($project_result, 0, "project_place");

$table_name2 = $_SESSION['db_prefix']."_customer";
$customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
$customer_result = mySQL_query($customer_query);
$customer_num = mysql_num_rows($customer_result);	
$account_type = mysql_result($customer_result, 0, "account_type");
if ($account_type=="corporate") { 
		$cust_name = mysql_result($customer_result, 0, "comp_name"); 
} 
else {
		$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
}	
	
		
if ($email_address!='') {
			$subject = 'Cutting bill Tlhomeso Timbers';
			$message = '<body><font face=Calibri size=3>The result on captured quote '.$_SESSION['job_no'].' for '.$project_description.' made on '.date("d-m-Y, H:m", strtotime($entry_date )).' by '.$_SESSION['username'].' is <b>'.number_format($result_value, 2, '.', ',').'%</b> <br><br>Job value: P '.number_format($job_value, 2, '.', ',').'<br>Captured value:: P '.number_format($captured_value, 2, '.', ',').'<br><br>Customer: '.$cust_name.'<br>Project: &nbsp;&nbsp;&nbsp;&nbsp;'.$project_no.', '.$project_place.'</font><br><br>For more info, login at <a href=costing.tocotex.com/login.php?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');
	echo '<br>*****'.'mailed';		
		}
			
	
?>
captured_job_alert