<?php
$hostname = "localhost";
$username = "manase";
$password = "2014manase";

$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");

$selected = mysql_select_db("man2014",$dbhandle)
  or die("Could not select man2014");

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s"); 

   $select_date1 = date("Y-m-d"); 
   

$table_name2 = "man_quotations";
$quotation_query = "SELECT quote_no, cust_id, seller, priority, status, quotation_date, followup_date, project FROM $table_name2 WHERE followup_date <= '".$select_date1."' ORDER BY quote_no DESC ";
$quotation_result = mySQL_query($quotation_query);
$quotation_num = mysql_num_rows($quotation_result);



$i=0;
$quotations_content = '';
while ($i<$quotation_num) {
	$a1 = mysql_result($quotation_result, $i, "quote_no");
	$a2 = mysql_result($quotation_result, $i, "cust_id");
		 if ($a2==0) {
			    $a2a = 'Bill of quantity';
		 				 }
		 else {			 
	     $table_name2 = "man_customer";
		 $customer_query = "SELECT account_type, title, name_2, comp_name FROM $table_name2 WHERE cust_id ='".$a2."' ";
				$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") { 
					$a2a = mysql_result($customer_result, 0, "comp_name"); 
				} 
				else {
					$a2a = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 				}
		 }
	 $a3 = mysql_result($quotation_result, $i, "seller");
	 $a4 = mysql_result($quotation_result, $i, "priority");
	 $a5 = mysql_result($quotation_result, $i, "quotation_date");
	 $a6 = mysql_result($quotation_result, $i, "followup_date");	 
	 $a7 = mysql_result($quotation_result, $i, "project");
	 	
		
$quotations_content = $quotations_content.'<tr><td valign="top">'.$a1.'</td><td valign="top">'.date("d-m-Y", strtotime($a5)).'</td><td valign="top">'.date("d-m-Y", strtotime($a6)).'</td><td valign="top">'.$a7.'<br>'.$a2a.'</td><td valign="top">'.$a4.'</td><td valign="top">'.$a3.'</td></tr>';	
    
  $i++;
}

$report_text = 'Daily follow up report. (Compiled on '.date("d-m-Y", strtotime($select_date1)).')<br><br>Required follow up on '.date("l, d-m-Y", strtotime($select_date1)).'<br>
<table border=1 width=650 cellpadding=1 style=border-collapse:collapse; font-size:10px; >
<tr valign="top">
<td width=75 valign="top">Quote no</td>
<td width=90 valign="top">Quote date</td>
<td width=90 valign="top">Follow up date</td>
<td width=200 valign="top">Project<br>Customer</td>
<td width=80 valign="top">Priority</td>
<td width=100 valign="top" >Sales rep</td>
</tr>
'.$quotations_content.'
</table>
'; 


$table_name = "man_reports";
	$report_query = "SELECT user_name, send_report, send_email FROM $table_name WHERE report_name = 'daily follow up' ";
	$report_result = mySQL_query($report_query);
	$report_num = mysql_num_rows($report_result);
			
	$i=0;
	$email_address = '';
	while ($i<$report_num) {
		$user_name = mysql_result($report_result, $i, "user_name");
		$send_alert = mysql_result($report_result, $i, "send_report");
		$send_email = mysql_result($report_result, $i, "send_email");
		
		$table_name = "man_saved_sales_reports";
		$result = mysql_query("INSERT INTO $table_name (department, saved_report_name, saved_report_date, saved_report_text, user_name ) VALUES ('Sales', 'Daily follow up report', '".$entry_date."', '".			$report_text."', '".$user_name."' ") ;
		
		
		if ($send_email=='yes') {
			$table_name = "man_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++;
	}



if ($email_address!='') {	
$subject = 'Daily follow up';
			$message = '
<body style=font-face=Calibri; font-size=10px;><font size=14px;><b>Daily sales report</b></font><br><br>
'.$report_text.'
<br><br>
For more info, login at <a href=man.bwcustomized.com/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>';


$to = $email_address;
$subject = $subject;
$message = $message;
$from = 'manase-management@bwcustomized.com';
$headers = "From:" . $from. "\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$message,$headers);
}

			
?>

