<?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_min_stock_report";
$min_stock_query = "SELECT store_item_id, stock_date, supplied, new_stock_level, report, supplied_to, supply_no FROM $table_name2 WHERE stock_date BETWEEN '".$select_date1."' AND '".$select_date2."' ORDER BY store_item_id DESC ";
$min_stock_result = mySQL_query($min_stock_query);
$min_stock_num = mysql_num_rows($min_stock_result);


$i=0;
$a7tot=0;
$price=0;
$min_stock_content = '';
while ($i<$min_stock_num) {
	//$a1 = mysql_result($quotation_result, $i, "quote_no");
	$a2 = mysql_result($min_stock_result, $i, "store_item_id");
		$table_name = "man_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$a2."'  ";
		$name_result = mySQL_query($name_query);
		$a2a = mysql_result($name_result, 0, "item_name");
	
	$a5 = mysql_result($min_stock_result, $i, "stock_date");
	//$a6 = mysql_result($quotation_result, $i, "required_date");
	$a7 = mysql_result($min_stock_result, $i, "supplied");	
	$a9 = mysql_result($min_stock_result, $i, "new_stock_level");
	$a13 = mysql_result($min_stock_result, $i, "report");
	$a12 = mysql_result($min_stock_result, $i, "supplied_to");
	$a10 = mysql_result($min_stock_result, $i, "supply_no");
	//$a8 = mysql_result($quotation_result, $i, "seller");
		
$min_stock_content = $min_stock_content.'<tr><td>'.$a2a.'</td><td>'.date("d-m-Y", strtotime($a5)).'</td><td>'.$a13.'<br>'.$a12.'</td><td>'.$a2a.'</td><td>'.$a9.'<br>'.$a10.'</td><td align=right>'.number_format($a7, 2, '.', ',').'</td><td>'.$a8.'</td></tr>';	
    
  $i++;
}



$report_text = 'Min stock report. (Compiled on '.date("d-m-Y", strtotime($comp_date)).')<br><br>Min stock recordings 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">Stock item</td>
<td width=80>Date</td>
<td width=200 valign="top">Project</td>
<td width=150 valign="top">Customer</td>
<td width=100 valign="top">Contact</td>
<td width=75 valign="top" >Value</td>
<td width=75 valign="top">&nbsp;&nbsp;</td>
</tr>
'.$min_stock_content.'
</table>
<br><br>';


$table_name = "man_reports";
	$report_query = "SELECT user_name, send_report, send_email FROM $table_name WHERE report_name = 'min stock' ";
	$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_min_stock_reports";
		$result = mysql_query("INSERT INTO $table_name (department, saved_report_name, saved_report_date, saved_report_text, user_name ) VALUES ('Store', 'Min stock 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 = 'Min stock report';
			$message = '
<body style=font-face=Calibri; font-size=10px;><font size=14px;><b>Daily minimum stock 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);
}

			
?>
