<?php
include("includes/config.php");
if(isset($_GET['exportExcel']))
{
	$start_date = $_GET['start_date'];
	$end_date = $_GET['end_date'];
	$select_user = $_GET['user'];
	
	$host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : dirname(__FILE__);
	if($host == 'localhost')
	$database = ' inventory_system_updated';
	else
	$database = '';
	$table = 'day_managment';
	
	$equalSign = " = ";
	if($select_user == 0)
	{
		$AllUserIDs = '';
		$UserSelectQuery = "Select id from users where usertypeid != 1";
		$UserSelectExecute = mysql_query($UserSelectQuery);
		while($UserSelectRecords = mysql_fetch_array($UserSelectExecute))
		{
			$AllUserIDs .= $UserSelectRecords['id'].",";
		}
		$AllUserIDs = rtrim($AllUserIDs, ",");
		$select_user = "IN($AllUserIDs)";
	}else{
		$equalSign .= $select_user;
		$select_user = $equalSign;
	}
	

	$dayManageQuery = "Select day_managment.*, users.username 
						FROM day_managment
						Inner Join users ON day_managment.userid = users.id
						Where date between '$start_date' AND '$end_date' AND userid $select_user";
						//echo $dayManageQuery;
	$dayManageExecute = mysql_query($dayManageQuery);
	$dayManageNumRows = mysql_num_rows($dayManageExecute);
	
	
	$query = "SELECT * FROM $table";
	$results = mysql_query($query);
	//$out = array();
	$out = '';
	$columnNames = array('ID', 'Date', 'Hours', 'All Day', 'On Leave', 'User Name');
	$columns = count($columnNames);
	/*$SelectAllColumnsQuery = "Show Columns from $table";
	$SelectAllColumnsExecute = mysql_query($SelectAllColumnsQuery);
	$columns = mysql_num_rows($SelectAllColumnsExecute);
	$i = 0;
	while($SelectAllColumnsRows = mysql_fetch_array($SelectAllColumnsExecute))
	{
		$field_name = $SelectAllColumnsRows[0];
		//$out[] .= $field_name;
		$out .= '"'.$field_name.'",';
		//$i++;
	}*/
	foreach($columnNames as $names)
	{
		$out .= '"'.$names.'",';
	}
	$out .= "\n";
	//print_r ($out);
//	exit;
	
	// Putting the data as row wise //
	while($rows = mysql_fetch_array($dayManageExecute))
	{
		for($j=0; $j<$columns; $j++)
		{
			if($j == 1)
			{
				$date_explode = explode("-", $rows[$j]);
				if($date_explode[1] < 10)
				{
					$date_explode[1] = ltrim($date_explode[1], 0);
				}
			//	print_r($date_explode);
				$date = $date_explode[1]."/".$date_explode[2]."/".$date_explode[0];
				$out .= '" '.$date.' ",';
			}
			else if($j == 3 || $j == 4)
			{
				if($rows[$j] == 1){
					$rows[$j] = 'Yes';
				}else{
					$rows[$j] = 'No';
				}
				$out .= '"'.$rows[$j].'",';
			}else if($j == 5)
			{
				$out .= '"'.$rows["username"].'",';
			}
			else{		
				//$out[] .= $rows[$j];
				
				$out .= '"'.$rows[$j].'",';
			}
		}
		$out .= "\n";
	}
	//print_r($out);
	//exit;
	
	$exportFileName = $table.'_'.date('Y-m-d').'.csv';
	// Open file export.csv
	$fileHandler = fopen($exportFileName, 'w');
	// Put all values form $out to export.csv
	/*foreach($out as $index => $values)
	{*/
		fputs($fileHandler, $out);
	
	fclose($fileHandler);
	header('Content-type: application/csv');
	header('Content-Disposition: attachment; filename="'.$exportFileName.'"');
	readfile($exportFileName);
	//exit;
	
}
?>