<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

print <<< HERE
<HTML>
<HEAD>
<TITLE> Item Auction Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<style>
.master-th {
	font-size: 11pt;
    font-weight: bold;
    font-family: arial,sans-serif;
    border-bottom: 1px solid #000000;
    text-align: center;
}

.master-td {
	font-size: 10pt;
    font-weight: bold;
    font-family: arial, sans-serif;
}
</style>
</HEAD>

HERE;



include("config.inc.php");
include 'function.inc.php';

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

$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");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}



if ($printaccess==0 or $printaccess=='')
echo'<BODY oncontextmenu="return false;">';
else
echo'<BODY>';


$querysetup="select grid_color1,grid_color2, DecimalBase, DecimalBase1, DecimalBase2, report_color1, report_color2, ifNull(InventoryRMaskqty,0) As InventoryRMaskqty, 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]);

print <<< HERE
<table width="100%" border="0" align="left" >
HERE;
$prtheader = 1;
if ($prtheader == 1){
	ShowReportHeader('Item Auction','portrait', $connection);
} else {
	ShowReportTitle('Item Auction','portrait', $connection);
}	

if ($showmin == '') $showmin = 0; 

$SelectedOptions = "Filter by: <br>";
if ($fromdate != '')      $SelectedOptions .= "-> Date From: ". $fromdate . "  &nbsp;&nbsp; To: ". $todate ."<br>";	
if ($showmin == 1)		  $SelectedOptions .= "-> Show Minimum Values";	


if (!empty($fromdate))   $fromdate = explodeword($fromdate);
if (!empty($todate))     $todate = explodeword($todate);  





print <<<HERE

</table>
<table width="100%" border="0" align="left" >
<tr><td  valign='top' width=100%><span style='font-size: 11pt;'><b>$SelectedOptions</b></span></td>
</tr>
<tr height='5'><td><hr></td></tr>
</table>

HERE;

if ($showmin == 1)
$query = " select auction_reference, auction_startDate,auction_endDate, item_code, item_description,ledger_name, min(bid_price), auction_id, bid_dateTime from (
			select auction_reference, date_format(auction_startDate,'%d-%m-%Y %h:%i %p') as auction_startDate, date_format(auction_endDate,'%d-%m-%Y %h:%i %p') as auction_endDate,
			item_code, item_description,ledger_name, min(bid_price) as bid_price, auction_id, bid_dateTime, supplier_code
			from (
				select auction_reference, auction_startDate,auction_endDate, items.item_code as item_code,
				item_description,ledger_name, bid_price as bid_price, item_auction.auction_id as auction_id, bid_dateTime,
				supplierBid.supplier_code as supplier_code
				from supplierBid
				left join suppliers on suppliers.ledger_number = supplierBid.supplier_code
				inner join item_auction on item_auction.auction_id = supplierBid.auction_id
				inner join items on items.item_code = item_auction.item_code
				where date_format(auction_startDate,'%Y-%m-%d %h:%i:%s') >= '$fromdate 00:00:00'
				and date_format(auction_endDate,'%Y-%m-%d %h:%i:%s') <= '$todate 23:59:59'
				order by auction_startDate, auction_endDate, auction_reference, bid_price, bid_dateTime
			) as QQ
			group by supplier_code, auction_id
			order by auction_startDate, auction_endDate, auction_reference asc, bid_price, bid_dateTime asc
			 ) as Q group by auction_id order by auction_reference asc, bid_price, bid_dateTime asc ";
			 
else			 
$query = " select auction_reference, auction_startDate,auction_endDate, item_code, item_description,ledger_name, bid_price, auction_id, bid_dateTime, supplier_code from (
			select auction_reference, date_format(auction_startDate,'%d-%m-%Y %h:%i %p') as auction_startDate, date_format(auction_endDate,'%d-%m-%Y %h:%i %p') as auction_endDate,
			item_code, item_description,ledger_name, min(bid_price) as bid_price, auction_id, bid_dateTime, supplier_code
			from (
				select auction_reference, auction_startDate,auction_endDate, items.item_code as item_code,
				item_description,ledger_name, bid_price as bid_price, item_auction.auction_id as auction_id, bid_dateTime,
				supplierBid.supplier_code as supplier_code
				from supplierBid
				left join suppliers on suppliers.ledger_number = supplierBid.supplier_code
				inner join item_auction on item_auction.auction_id = supplierBid.auction_id
				inner join items on items.item_code = item_auction.item_code
				where date_format(auction_startDate,'%Y-%m-%d %h:%i:%s') >= '$fromdate 00:00:00'
				and date_format(auction_endDate,'%Y-%m-%d %h:%i:%s') <= '$todate 23:59:59'
				order by auction_startDate, auction_endDate, auction_reference, bid_price, bid_dateTime
			) as QQ
				group by supplier_code, auction_id
				order by auction_startDate, auction_endDate, auction_reference, bid_price, bid_dateTime asc
			 ) as Q order by auction_reference asc, bid_price, bid_dateTime asc ";
			//echo $query;
$queryC = " select max(countSup) from (
			select count(auction_reference) as countSup from ( ".$query." ) as Q group by auction_reference
			) as QQ; ";			
$resC = mysql_query($queryC, $connection);
$rwC = mysql_fetch_row($resC);
$countSup = $rwC[0];
if($countSup == '') $countSup = 0;
echo "<br>";
echo "<table cellspacing='0' width='100%' border='0' align='left' >";
viewHeader($countSup, $connection);

//echo $query;
$res = mysql_query($query, $connection);
$col = 1;
$temRef = '';

$rb = "border-right: 1px solid black ";

while($rw = mysql_fetch_array($res)){
	if($col%2==0){
		$color = "style='background-color:".$report_color1."'";
	}else{
		$color = "style='background-color:".$report_color2."'";
	}
	if($temRef != $rw[0]){	
		if($col != 1) echo "</tr>";
		echo "<tr height='20px' $color>
				<td style='$rb;'><span class='master-td'>$col</span></td>
				<td style='$rb;'><span class='master-td'>$rw[0]</span></td>
				<td style='$rb;'><span class='master-td'>$rw[1]</span></td>
				<td style='$rb;'><span class='master-td'>$rw[2]</span></td>
				<td style='$rb;'><span class='master-td'>$rw[3]-$rw[4]</span></td>";
				
		$col++;
	}
	
	echo "<td style='$rb;'><span class='master-td'>$rw[5]</span></td><td style='$rb;'><span class='master-td'>$rw[6]</span></td>";
	$temRef = $rw[0];
}
echo "</table>";
function viewHeader($countSup, $connection){
	

echo "<tr>
		<td class='master-th'>S/N</span></td>
		<td class='master-th'>Auction Ref</span></td>
		<td class='master-th'>From Date</span></td>
		<td class='master-th'>To Date</span></td>
		<td class='master-th'>Item</span></td>";
		
for($i=1; $i<=$countSup; $i++)
	echo "<td class='master-th'>Sup $i</span></td><td class='master-th'>Price $i</span></td>";	
echo "</tr>";


	
}

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;
}


print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;

?>