<?php
$entryDate = 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 = 'job invoiced' ";
	$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."', 'orders', '".$entryDate."', '19', '".$_SESSION['job_card_no']."', '".$_SESSION['invoice_no']."', '' ) ";
			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']."_customer";
		$customer_query = "SELECT * FROM $table_name2 WHERE cust_id ='".$_SESSION['cust_id']."' ";
		$customer_result = mySQL_query($customer_query);
		$customer_num = mysql_num_rows($customer_result);	
		$a1 = mysql_result($customer_result, 0, "account_type");
			if ($a1=="corporate") { 
				$name = mysql_result($customer_result, 0, "comp_name"); 
			} 
			else {
				$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 = 'Alert from Manase Management Program';
			$message = '<body><font face=Calibri size=3>Job '.$_SESSION['job_card_no'].' for '.$_SESSION['project_no'].' has been invoiced on '.$_SESSION['invoice_date'].' by: '.$_SESSION['invoiced_by'].'<br><br>Job card no: '.$_SESSION['job_card_no'].'<br>Customer: '.$name.'<br>Project: '.$_SESSION['project_description'].'<br><br>This job can be completed.<br><br>For more info, login at <a href=crmcustomized.manase.co.bw/login.php?e= >Manase Management Program</a>.<br><br><font color=red><b>This message comes from a non-responsive address, please do not reply</b></font><body>';
			include ('alert_to_email.php');
		}
			
?>