<?php
session_start();
include '../db_connect.php';
include '../db_table_select.php';

$table = "$bookingreg";

if(isset($_GET['reptype']))
$reptype = $_GET['reptype'];

if(isset($_GET['report_type']))
$Subreptype = $_GET['report_type'];

if($reptype=='bill_info')
{
	$table = $billreg;
	$datefield = 'billdate';
	$columns = "srno, billno, billdate, billto, total, sramount, educess, seducess,  bookregno";
	$headers = "SELECT  'SRNO' AS srno, 'BILLNO' AS billno, 'BILLDATE' AS billdate, 'NAME' AS billto, 'TOTAL' as total, 'STAX' as  sramount, 'SBCESS' as educess, 'KKCESS' as seducess,  'BOOKID' as bookregno  UNION ALL  ";
	$i=9;	//number of Columns
	
}
else if($reptype=='book_info')
{
	if($Subreptype=='allbookings')
{

	$table = $bookingreg;
	$datefield = 'dutydate';
	$columns = "bookid, ref, billedto, bookedby, usedby, repdate, city, vehicle, vehno, driver, drivermob, vendor, status, billno, remarks, sttime, stkm, clgtime, clgkm, tottime, totkm, parking, exhr, exhrrate, exkm,  dutytype, insstatus, days, reptime";
	
	$headers = "SELECT  'BRN NO' as bookid,  'B REF No' as ref,  'CLIENT NAME' as billedto, 'BOOKED BY' as bookedby, 'USED BY' as usedby,  'REP DATE' as repdate, 'LOCATION' as city, 'VEHICLE' as vehicle, 'VEHICLE NO' as vehno, 'DRIVER NAME' as driver,  'DRIVER MOBILE NO' as drivermob, 'VENDOR' as vendor, 'STATUS' as status, 'BILL NO' as billno, 'REMARKS' as remarks,  'START TIME' as  sttime, 'START KMS' as stkm, 'CLOSE TIME' as clgtime, 'CLOSE KMS' as clgkm, 'TOTAL TIME' as tottime, 'TOTAL KMS' as totkm, 'PARKING' as parking, 'EX. HR.' as exhr, 'EX. HR. RATE' as exhrrate, 'EX. KM' as exkm, 'DUTYTYPE' as dutytype, 'INSSTATUS' as insstatus, 'DAYS' as days, 'REP. TIME' as reptime UNION ALL  ";	
	$i=28;
	
}

	if($Subreptype=='allbookings')
{

	$table = $bookingreg;
	$datefield = 'dutydate';
	$columns = "bookid, ref, billedto, bookedby, usedby, repdate, city, vehicle, vehno, driver, drivermob, vendor, status, billno, remarks, sttime, stkm, clgtime, clgkm, tottime, totkm, parking, exhr, exhrrate, exkm,  dutytype, insstatus, days, reptime";
	
	$headers = "SELECT  'BRN NO' as bookid,  'B REF No' as ref,  'CLIENT NAME' as billedto, 'BOOKED BY' as bookedby, 'USED BY' as usedby,  'REP DATE' as repdate, 'LOCATION' as city, 'VEHICLE' as vehicle, 'VEHICLE NO' as vehno, 'DRIVER NAME' as driver,  'DRIVER MOBILE NO' as drivermob, 'VENDOR' as vendor, 'STATUS' as status, 'BILL NO' as billno, 'REMARKS' as remarks,  'START TIME' as  sttime, 'START KMS' as stkm, 'CLOSE TIME' as clgtime, 'CLOSE KMS' as clgkm, 'TOTAL TIME' as tottime, 'TOTAL KMS' as totkm, 'PARKING' as parking, 'EX. HR.' as exhr, 'EX. HR. RATE' as exhrrate, 'EX. KM' as exkm, 'DUTYTYPE' as dutytype, 'INSSTATUS' as insstatus, 'DAYS' as days, 'REP. TIME' as reptime UNION ALL  ";	
	$i=28;
	
}

}


$file = 'Database';



$filename = $file."_".date("Y-m-d"); 

/*$result = mysqli_query($con,"SHOW COLUMNS FROM ".$table);
$i = 0;
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$csv_output .= "\"$row[Field]\"".",";
$i++;
}
}
$csv_output .= "\n";*/


if(trim($_GET[billedto])!='')
$values = mysqli_query($con,"$headers   SELECT $columns FROM ".$table." where $datefield between STR_TO_DATE('$_GET[reportdfrom]','%d/%m/%Y') and STR_TO_DATE('$_GET[reportdto]','%d/%m/%Y') and billedto like '$_GET[billedto]'");
else
$values = mysqli_query($con,"$headers  SELECT $columns FROM ".$table." where $datefield between STR_TO_DATE('$_GET[reportdfrom]','%d/%m/%Y') and STR_TO_DATE('$_GET[reportdto]','%d/%m/%Y');");

while ($rowr = mysqli_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= "\"$rowr[$j]\"".",";
}
$csv_output .= "\n";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/octet-stream");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
$csv_output=stripcslashes($csv_output);
print $csv_output;
exit;
?>