<?php
header("Content-type: text/html; charset=utf-8");
require_once('Spreadsheet/Excel/Writer.php');

session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$querysetup="select grid_color1,grid_color2, DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, Company_Name As CompanyName from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
}

$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);

//Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();

//Sending HTTP headers
$workbook->send('Exp_BidHistory.xls');
 
//Creating a worksheet
$worksheet=&$workbook->addWorksheet('Bid History');
$worksheet->setLandscape();
 
//set all columns same width
$columnWidth = 10;

//Title Format 
$sheetTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'size'=>10));

//Column Title Format  
$columnTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'top'=>1,
  'bottom'=>1 ,
  'size'=>15,
  'color'=>0x0A,
  ));
  
 
  
// Regular Format
$regularleftFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'left'  ));
$regularrightFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'right'  ));  
$regularcenterFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'center'  ));    
  
$regularleftFormat1 =& $workbook->addFormat(array('size'=>8,
  'align'=>'left'  ));
$regularrightFormat1 =& $workbook->addFormat(array('size'=>8,
  'align'=>'right'  ));  
$regularcenterFormat1 =& $workbook->addFormat(array('size'=>8,
  'align'=>'center'  ));      
  
$TitleFormat =& $workbook->addFormat(array('size'=>11,
  'align'=>'left',
  'bold'=>1
  ));

$boldleftFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left'  ,
  'valign'=>'top'  ,
  'bold'=>1
  ));  
$boldrightFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'right'  ,
  'bold'=>1
  )); 
$boldcenterFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'center'  ,
  'valign'=>'top'  ,
  'bold'=>1
  ));  

$grouplevel1Format =& $workbook->addFormat(array('size'=>10,
  'align'=>'left',
  'bold'=>1,
  'color'=>0x0C
  )); 
  
$grouplevel1totalFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'right',
  'bold'=>1,
  'color'=>0x0C
  ));    
  
  
$seloptionFormat =& $workbook->addFormat(array('size'=>9,
  'align'=> 'left'  ,
  'valign'=> 'top'  ,
  'bold'=> 1
  ));
$seloptionFormat->setTextWrap();

$query = "select auction_reference, concat(item_auction.item_code, ' - ', item_description) as itemcode, date_format(auction_startDate,'%d-%m-%Y %h:%i %p'),
			date_format(auction_endDate,'%d-%m-%Y %h:%i %p')
			from item_auction
			left join items on items.item_code = item_auction.item_code
			where item_auction.auction_id = '$auction_id'; "; 
$res = mysql_query($query,$connection);
$row = mysql_fetch_row($res);

$SelectedOptions = "Auction REF: $row[0] \n";	
$SelectedOptions .= "Item Name: $row[1] \n";	
$SelectedOptions .= "Start Date: $row[2] \n";	
$SelectedOptions .= "End Date: $row[3] \n";	

if($orderBid == '1') $oderby = 'Price';
else if($orderBid == '2') $oderby = 'Customer';
else if($orderBid == '3') $oderby = 'Date';
$SelectedOptions .= "Order by: $oderby ";


$RepTitle = 'Bid History';   

//Display Report Header
$worksheet->write(1,0,$CompanyName, $TitleFormat);
$worksheet->write(1,3,$RepTitle, $TitleFormat);
               
$worksheet->write(1,6,"Printed Date:", $boldleftFormat);
$worksheet->write(1,7,$printdate[2]."/".$printdate[1]."/".$printdate[0], $regularleftFormat);
$worksheet->write(2,6,"Time:", $boldleftFormat);		
$worksheet->write(2,7,$dt[1]." ".$dt[2], $regularleftFormat);

//Displaying Selected Options	
// Merge cells from row 3, col 0 to row 3, col 0
$worksheet->setMerge(3, 0, 8, 6);
$worksheet->write(3,0,$SelectedOptions, $seloptionFormat);

$RowNum    = 10;
$ColumnNum = 0;	



	$worksheet->setColumn ($ColumnNum, $ColumnNum, 3);
	$worksheet->write($RowNum,$ColumnNum,"No.", $boldleftFormat);
	$ColumnNum += 1;
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
	$worksheet->write($RowNum,$ColumnNum,"Alias", $boldleftFormat);
	$ColumnNum += 1;
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 20);
	$worksheet->write($RowNum,$ColumnNum,"Company NAme", $boldcenterFormat);
	$ColumnNum += 1;
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 15);
	$worksheet->write($RowNum,$ColumnNum,"Bid Price", $boldcenterFormat);
	$ColumnNum += 1;
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
	$worksheet->write($RowNum,$ColumnNum,"Quantity", $boldcenterFormat);
	$ColumnNum += 1;
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"Date", $boldcenterFormat);
	$ColumnNum += 1;
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"Time", $boldcenterFormat);
	$ColumnNum += 1;

	

$query = "select item_auctiondt.supplier_code, suppliers.ledger_name, bid_price, qty_supplied, date_format(bid_dateTime,'%d-%m-%Y'), date_format(bid_dateTime,'%H:%i:%s')
			from item_auctiondt
			inner join item_auction on item_auction.auction_id = item_auctiondt.auction_id
			left join suppliers on suppliers.ledger_number = item_auctiondt.supplier_code
			inner join supplierBid on supplierBid.auction_id = item_auction.auction_id
			and supplierBid.supplier_code = item_auctiondt.supplier_code
			where item_auction.auction_id=$auction_id ";
if($orderBid =='1')	 		 $query .= " order by bid_price, bid_dateTime asc ";	
else if($orderBid =='2')	 $query .= " order by item_auctiondt.supplier_code asc ";	
else if($orderBid =='3')	 $query .= " order by bid_dateTime desc ";	

$RowNum    = 12;
$ColumnNum = 0;	

$result=mysql_query($query,$connection) or die(mysql_error());
$ct = 1;	
while($rows=mysql_fetch_row($result)){

	$worksheet->write($RowNum, $ColumnNum, $ct, $regularleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, $rows[0]." ", $regularleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, $rows[1], $regularleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, number_format($rows[2], $DecimalBase), $regularrightFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, number_format($rows[3], $DecimalBase), $regularrightFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, $rows[4], $regularcenterFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, $rows[5], $regularcenterFormat);
	$ColumnNum += 1;
	
$RowNum++;	
$ColumnNum = 0;	
$ct++;
}




function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}


function explodewordtime($date)
{

$trimtime = explode(' ',$date);
$date = $trimtime[0];
$time = $trimtime[1];

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim." ".$time;
}


$workbook->close();

?>