<?php

$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "SELECT user_name, send_alert, send_email FROM $table_name WHERE alert_name = 'quotation' AND condition_1 < ".$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 ) VALUES ('".$user_name."', 'sales', '".$entry_date."', '11', '".$_SESSION['quote_no']."', '".$user_name."' ) ";
			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']."_quotations";
$project_query = "SELECT project_no, project_place FROM $table_name WHERE quote_no = '".$_SESSION['quote_no']."' ";
$project_result = mySQL_query($project_query);
$project_num = mysql_num_rows($project_result);
$project_no = mysql_result($project_result, 0, "project_no");	
$project_place = mysql_result($project_result, 0, "project_place");	
	
		
if ($email_address!='') {			
			$subject = 'Alert from Manase Management Program';
			$message = '<body><font face=Calibri size=3>A new quotation no '.$_SESSION['quote_no'].' for '.$_SESSION['project'].' was made on '.date("d-m-Y, H:m", strtotime($entry_date )).' by '.$user_name.'.<br><br>Quote value: P '.number_format($value, 2, '.', ',').'<br><br>Customer: '.$_SESSION['company_name'].'<br>Project: &nbsp;&nbsp;&nbsp;&nbsp;'.$project_no.', '.$project_place.'</font><br><br>For more info, login at <a href=crmcustomized.manase.co.bw/login.php?e=>Manase 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');
		}
			

	
	
?>