<?php
include("connect.php");
if($_GET["search"] == "Search")
{	
	$year = $_GET["year"];
	$month = $_GET["month"];
	$day1 = $_GET["day1"];
	$day2 = $_GET["day2"];
	
	if($year==""){
	echo "<script language='javascript'>alert('Please select month and year.')</script>";
	echo "<script language='javascript'>window.location='totalsales.php'</script>"; 
	}else{
	if(($month=="")&&($day1=="")&&($day2=="")){
	$result = mysql_query("SELECT * FROM invoice WHERE year(invDate) = $year ORDER BY invNo");
	
	}
	else if(($day1=="")&&($day2=="")){
	$result = mysql_query("SELECT * FROM invoice WHERE year(invDate) = $year AND month(invDate) = $month ORDER BY invNo");
	
	}else{
	$result = mysql_query("SELECT * FROM invoice WHERE day(invDate)>= $day1 AND day(invDate)<= $day2 AND year(invDate) = $year AND month(invDate) = $month ORDER BY invNo");
	
}
}
}
else if($_GET["reset"] == "Reset Result")
{
$year = date("Y");
$month = date("m");
	$result = mysql_query("SELECT * FROM invoice WHERE year(invDate) = $year AND month(invDate) = $month ORDER BY invNo");
	
}
else
{
$year = date("Y");
$month = date("m");
	$result = mysql_query("SELECT * FROM invoice WHERE year(invDate) = $year AND month(invDate) = $month ORDER BY invNo");
	
}

$num_results = mysql_num_rows($result);

if($month == 1){
$mword = 'January';
}
else if($month == 2){
$mword = 'February';
}
else if($month == 3){
$mword = 'March';
}
else if($month == 4){
$mword = 'April';
}
else if($month == 5){
$mword = 'May';
}
else if($month == 6){
$mword = 'June';
}
else if($month == 7){
$mword = 'July';
}
else if($month == 8){
$mword = 'August';
}
else if($month == 9){
$mword = 'September';
}
else if($month == 10){
$mword = 'October';
}
else if($month == 11){
$mword = 'November';
}
else if($month == 12){
$mword = 'December';
}
if(($day1!="")&&($day2!="")){
echo "<p>Period:".$day1." to ".$day2." ".$mword." ".$year."</p>";
}else{
echo "<p>Period:".$mword." ".$year."</p>";
}
echo "<p>Number of inv List(s) : ".$num_results."</p>";

echo "<table border='1' cellspacing='0' cellpadding='5' bordercolor='white' width='100%'>";
echo "<tr bgcolor='black' style='color:white'>";
echo "<td>Inv No</td>";
echo "<td>Customer Name</td>";
echo "<td>Created Date</td>";
echo "<td>Order Total</td>";
echo "<td>View Order</td>";
echo "</tr>";

for($i=0; $i<$num_results; $i++)
{
	$row = mysql_fetch_array($result);
	if($row["invNo"]!=""){
	echo "<tr>";
	echo "<td>".$row["invNo"]."</td>";
	echo "<td>".$row["custName"]."</td>";
	echo "<td>".$row["invDate"]."</td>";
	echo "<td>RM ";
	printf("%10.2f",$row['invTotal']);
	echo "</td>";
	echo "<td><a href='invsalesview.php?invNo=".$row["invNo"]."'>View</a></td>";
	echo "</tr>";
	
	$total = $total + $row['invTotal'];
	}
}
if($num_results ==0){
echo "<tr>";
echo "<td colspan='5'><center>No Sales Made During This Period.</center></td>";
echo "</tr>";
}else{
echo "<tr bgcolor='black' style='color:white'>";
echo "<td colspan='3'>Total Sales</td>";
echo "<td>RM ";
printf("%10.2f",$total);
echo "</td>";
echo "<td></td>";
echo "</tr>";
}

echo "</table>";

echo "<hr/>";

$db = mysql_close();

?>