<?php

$table_name = $_SESSION['db_prefix']."_alerts";
	$alert_query = "SELECT user_name, send_alert, send_email FROM $table_name WHERE alert_name = 'purchase to authorise' ";
	$alert_result = mySQL_query($alert_query);
	$alert_num = mysql_num_rows($alert_result);


	$email_address = '';
	$i=0;
	while ($i<$alert_num) {
		$user_name = mysql_result($alert_result, $i, "user_name");
			
		
			$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."', 'stores', '".$entry_date."', '16', '".$purchase_order."', '".$_SESSION['username']."' ) ";
			execsql($message_query);
		
		
			$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_one = mysql_result($address_result, 0, "email");
			if ($email_address_one != '' ) {
				    $email_address = $email_address.', '.$email_address_one;
			}
		
	$i++;	
	}
	
$table_name2 = $_SESSION['db_prefix']."_suppliers";
$account_name_query = "SELECT account_name FROM $table_name2 WHERE supplier_id ='".$_SESSION['supplier_id']."' ";
$account_name_result = mySQL_query($account_name_query);
$account_name_num = mysql_num_rows($account_name_result);	
$account_name = mysql_result($account_name_result, 0, "account_name"); 

if ($email_address!='') {			
			$subject = 'Alert from Manase Management Program';
			$message = '<body><font face=Calibri size=3>Purchase order '.$purchase_order.' for '.$account_name.' requires your authorisation <br><br>Order value: P '.number_format($_SESSION['all_total'], 2, '.', ',').'<br><br>To authorise, 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');
		}
			

	
	
?>