isLoggedIn())
{
    header ("location: ../login.php");
    exit();
}
if(isset($_POST['date_range']))
{
	$date_range=$_POST['date_range'];
 	$dates=explode(':',$date_range);
	$date1=$dates[0];
	$date2=$dates[1];
	
	$categories_name=array();
	$categories_id=array();
	$categories_total=array();
	$categories_subtotal=array();
}
$categories_table=$cfg_tableprefix.'categories';
$sales_table=$cfg_tableprefix.'sales';
$sales_items_table=$cfg_tableprefix.'sales_items';
$display=new display($dbf->conn,$cfg_theme,$cfg_currency_symbol,$lang);
$display->displayTitle("$cfg_company $lang->allCategoriesReport");
$tableheaders=array("$lang->category","$lang->totalWithOutTax","$lang->totalWithTax","$lang->tax");
$result=mysql_query("SELECT * FROM $sales_table WHERE date between \"$date1\" and \"$date2\" ORDER BY id DESC",$dbf->conn);
$result2=mysql_query("SELECT * FROM $sales_table WHERE date between \"$date1\" and \"$date2\" ORDER BY id ASC",$dbf->conn);
$row=mysql_fetch_assoc($result);
$high_id=$row['id'];
$row=mysql_fetch_assoc($result2);
$low_id=$row['id'];
$result3=mysql_query("SELECT * FROM $sales_items_table WHERE sale_id BETWEEN \"$low_id\" and \"$high_id\" ORDER BY id DESC",$dbf->conn);
echo "$lang->totalsForCategories
$lang->between $date1 $lang->and $date2
";
echo '
';
		echo "border_style $display->border_color $display->border_width px\" align='center'>
		
		header_rowcolor>\n\n";
		for($k=0;$k< count($tableheaders);$k++)
		{
			echo "| \n$tableheaders[$k]\n | \n";
		}
		echo '
'."\n\n";	
		$rowCounter=0;
		$subtotal=0;
		$total=0;
		
		$category_result=mysql_query("SELECT * FROM $categories_table order by category");
		while($row=mysql_fetch_assoc($category_result))
		{
			$categories_id[]=$row['id'];
			$categories_name[]=$row['category'];
			$categories_total[$row['id']]=0;
			$categories_subtotal[$row['id']]=0;
		}
		
		while($row=mysql_fetch_assoc($result3))
		{
			$category_of_item=$dbf->idToField($cfg_tableprefix.'items','category_id',$row['item_id']);
			$categories_subtotal[$category_of_item]+=$row['item_total_cost']-$row['item_total_tax'];
			$categories_total[$category_of_item]+=$row['item_total_cost'];
		
		}
		
		for($k=0;$krowcolor1>\n";
			}
			else
			{
				echo "\nrowcolor2>\n";
			}
			
			$id=$categories_id[$k];
			$name=$categories_name[$k];
			$subtotal=number_format($categories_subtotal[$id],2,'.', '');
			$total=number_format($categories_total[$id],2,'.', '');
			$tax=number_format($total-$subtotal,2,'.', '');
			
			echo "\n| \n$name\n | \n";
			echo "\n\n\$$subtotal\n | \n";
			echo "\n\n\$$total\n | \n";
			echo "\n\n\$$tax\n | \n";
			
			$rowCounter++;
		
		}
		
		echo '
';
?>