<?
ob_clean();
include("../../startup.php"); 
###########################################  	END  FILTER CONITIONS 	#####################################

$header = "job_id,company_name,contact_name,mileage,description,mileage_date,mileage_rate,cost\n";

$data  = $header;
$sql = "SELECT * FROM mileage $where ORDER BY mileage_date DESC";

$rs = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($rs)){
	$job_id = $row['job_id'];
	$company_name = utility::getTableFieldById("job","company_name","id",$row['job_id']);
	$first_name1 = utility::getTableFieldById("job","first_name1","id",$row['job_id']);
	$last_name1 = utility::getTableFieldById("job","last_name1","id",$row['job_id']);
	$contact_name = ucfirst($first_name1)." ".ucfirst($last_name1);
	$mileage = $row['mileage'];
	$mileage_text = $row['mileage_text'];
	$mileage_rate = $row['mileage_rate'];
	
	$mileage_date = ($row['mileage_date'] != "0000-00-00 00:00:00") ? date("d-m-Y",strtotime($row['mileage_date'])) : "";
	$mileage_cost = $row['mileage']*$row['mileage_rate'];
	
	$data .= '"'.$job_id.'"'.",".'"'.$company_name.'"'.",".'"'.$contact_name.'"'.",".'"'.$mileage.'"'.",".'"'.$mileage_text.'"'.",".'"'.$mileage_date.'"'.",".'"'.$mileage_rate.'"'.",".'"'.number_format($mileage_cost,2).'"'."\n";
}

$file_name = "mileage_export.csv";
ob_clean();
echo $data;
header("Content-type: application/csv"); 
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");
die;
?>