<?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"); 

$comp_date = date("Y-m-d");
//$comp_date = '2014-08-05';
if (date("l", strtotime($comp_date))=='Monday') {
  $select_date1 = strtotime(date("Y-m-d", strtotime($comp_date)) . "-3 day");
  $select_date2 = strtotime(date("Y-m-d", strtotime($comp_date)) . "-1 day");
  $select_date1 = date("Y-m-d", $select_date1);
  $select_date2 = date("Y-m-d", $select_date2);
}
else {
   $select_date = strtotime(date("Y-m-d", strtotime($comp_date)) . "-1 day");
   $select_date1 = date("Y-m-d", $select_date);
   $select_date2 = date("Y-m-d", $select_date);
}

$table_name2 = "man_quotation_notes";
$quotation_query = "SELECT quote_number, progress_status, note_date, note_text, seller FROM $table_name2 WHERE note_date BETWEEN '".$select_date1."' AND '".$select_date2."' ORDER BY quote_number 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_number");
	
	$table_name = "man_quotations";
	$quote_query = "SELECT cust_id, project, quotation_date FROM $table_name WHERE quote_no = '".$a1."' ";
	$quote_result = mySQL_query($quote_query);
	$quote_num = mysql_num_rows($quote_result);
	$cust_id = mysql_result($quote_result, 0, "cust_id");
	$a5 = mysql_result($quote_result, 0, "quotation_date");
	$a6 = mysql_result($quote_result, 0, "project");
	
	
	$a2 = $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 ='".$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") { 
					$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");
 				}
		 }
		 
	 $a7 = mysql_result($quotation_result, $i, "progress_status");
	 $a8 = mysql_result($quotation_result, $i, "note_date");
	 $a9 = mysql_result($quotation_result, $i, "note_text");
	 $a10 = mysql_result($quotation_result, $i, "seller");
	
	
		
$quotations_content = $quotations_content.'<tr><td valign="top">'.$a1.'</td><td valign="top">'.date("d-m-Y", strtotime($a5)).'<br>'.date("d-m-Y", strtotime($a8)).'</td><td valign="top">'.$a6.'<br>'.$a2a.'</td><td valign="top">'.$a7.'<br>'.$a10.'</td><td valign="top">'.$a9.'</td></tr>';	
    
  $i++;
}

$report_text = 'Daily quote notes report. (Compiled on '.date("d-m-Y", strtotime($comp_date)).')<br><br>Quotations notes for '.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=110 valign="top">Quote date<br>Note date</td>
<td width=150 valign="top">Project<br>Customer</td>
<td width=80 valign="top">Status<br>Sales rep</td>
<td width=300 valign="top" >Note</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 quote notes' ";
	$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 quote notes 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 quote note 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);
}

			
?>
