<?
ob_clean();
include("../../startup.php"); 
###########################################  	END  FILTER CONITIONS 	#####################################
$data = "Timesheet Data\n\n";
$header = "consultant,date,client ref,organisation,activity ref,start time,end time,total time,description,units,timesheet_option\n";
$data  .= $header;
//$sql = "SELECT * FROM timesheet $where ORDER BY job_id ASC";
$sql = "SELECT id,job_id,admin_id,timesheet_text,timesheet_option,timesheet_date,timesheet_time,from_time,to_time FROM timesheet $where ORDER BY job_id ASC";
$rs = mysqli_query($conn,$sql);

while($row = mysqli_fetch_assoc($rs)){
	
	$admin_id = explode(",",$row['admin_id']);
	$consultant_string = "";
	foreach($admin_id as $a_id){
		$consultant_string .= ucfirst(utility::getTableFieldById("admin","name","id",$a_id))."#";
	}
	$consultant_string = substr($consultant_string,0,-1);
	
	$timesheet_date = ($row['timesheet_date'] != "0000-00-00 00:00:00") ? date("d-m-Y",strtotime($row['timesheet_date'])) : "";
	$oganisation = utility::getTableFieldById('job','first_name','id',$row['job_id']);
	//$project = utility::getTableFieldById('job_project_mapping','project_title','id',$row['project_id']);
	$timesheet_option = Keycode::getkeycodeById($row['timesheet_option']);
	$unit = $gsi->getUnitByTime($row['timesheet_time']);
	
	$data .= '"'.$consultant_string.'"'.",".'"'.$timesheet_date.'"'.",,".'"'.$oganisation.'"'.",,".'"'.$row['from_time'].'"'.",".'"'.$row['to_time'].'"'.",".'"'.$row['timesheet_time'].'"'.",".'"'.trim($row['timesheet_text']).'"'.",".'"'.$unit.'"'.",".'"'.$timesheet_option.'"'.",\n";
}

////////////////////////// Mileage Date Export ///////////////////////////////////
$sql1 = "SELECT * FROM mileage $where ORDER BY job_id,project_id ASC";
$rs1 = mysqli_query($conn,$sql1);
if(mysqli_num_rows($rs1) > 0){
	$data .= "\n\nMileage Data\n\n";
	$header = "consultant,date,organisation,project,description,mileage rate,mileage (In miles),cost\n";
	$data  .= $header;

	while($row1 = mysqli_fetch_assoc($rs1)){
		$admin_id = explode(",",$row1['admin_id']);
		$consultant_string = "";
		foreach($admin_id as $a_id){
			$consultant_string .= ucfirst(utility::getTableFieldById("admin","name","id",$a_id))."#";
		}
		$consultant_string = substr($consultant_string,0,-1);
		
		$mileage_date = ($row1['mileage_date'] != "0000-00-00 00:00:00") ? date("d-m-Y",strtotime($row1['mileage_date'])) : "";
		$oganisation = utility::getTableFieldById('job','first_name1','id',$row1['job_id']);
		$project = utility::getTableFieldById('job_project_mapping','project_title','id',$row1['project_id']);
		$vat = utility::getTableFieldById('keycode','code','name','vat');
		$mileage_cost = $row1['mileage']*$vat*$row1['mileage_rate'];
		
		$data .= '"'.$consultant_string.'"'.",".'"'.$mileage_date.'"'.",".'"'.$oganisation.'"'.",".'"'.$project.'"'.",".'"'.trim($row1['mileage_text']).'"'.",".'"'.number_format($row1['mileage_rate'],2).'"'.",".'"'.$row1['mileage'].'"'.",".'"'.$mileage_cost.'"'."\n";
	}
}
////////////////////////// End Mileage Date Export ///////////////////////////////////

////////////////////////// Expenses Date Export ///////////////////////////////////
$sql2 = "SELECT * FROM expenses $where ORDER BY job_id,project_id ASC";
$rs2 = mysqli_query($conn,$sql2);
if(mysqli_num_rows($rs2) > 0){

	$data .= "\n\nExpenses Data\n\n";
	$header = "consultant,date,organisation,project,description,expense type,cost\n";
	$data  .= $header;

	while($row2 = mysqli_fetch_assoc($rs2)){
		$admin_id = explode(",",$row2['admin_id']);
		$consultant_string = "";
		foreach($admin_id as $a_id){
			$consultant_string .= ucfirst(utility::getTableFieldById("admin","name","id",$a_id))."#";
		}
		$consultant_string = substr($consultant_string,0,-1);
		
		$expenses_date = ($row2['expenses_date'] != "0000-00-00 00:00:00") ? date("d-m-Y",strtotime($row2['expenses_date'])) : "";
		$oganisation = utility::getTableFieldById('job','first_name1','id',$row2['job_id']);
		$project = utility::getTableFieldById('job_project_mapping','project_title','id',$row2['project_id']);
		$expenses_type = utility::getTableFieldById('keycode','code','id',$row2['expenses_type']);
		
		$data .= '"'.$consultant_string.'"'.",".'"'.$expenses_date.'"'.",".'"'.$oganisation.'"'.",".'"'.$project.'"'.",".'"'.trim($row2['expenses_text']).'"'.",".'"'.$expenses_type.'"'.",".'"'.number_format($row2['expenses'],2).'"'."\n";
	}
}
////////////////////////// End Expenses Date Export ///////////////////////////////////

$file_name = "timesheet_details_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;
?>