<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
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");

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

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


$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist   = mysql_num_rows($projectresult);


$fromdate 		= $_GET['fromdate'];
$todate 		= $_GET['todate'];
$fromclient 	= $_GET['fromclient'];
$toclient 		= $_GET['toclient'];
$fromproject 	= $_GET['fromproject'];
$toproject 		= $_GET['toproject'];

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


print <<<HERE
<html>
<head><title>List of Tenders</title></head>
<style type="text/css" >
table.params td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                      }				 
table.master td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }
table.group td { font-size: 12px; 
                 font-weight: bold;
                 font-family:Times-New-Roman;
               }				 
				 
table.detail td { font-size: 10px; 
                  font-family:Times-New-Roman;
                 }				 
table.headerdetail td { font-size: 10px; 
                        font-weight: bold;
                        font-family:Times-New-Roman;
                      }				 
table.totals td { font-size: 12px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                 }					  

.header1 {
	font-size: 26px;
	font-weight: bold;
}
.header2 {
	font-size: 20px;
	font-weight: bold;
}
.header3 {
	font-size: 12px;
	font-weight: bold;
}	

.header4 {
	font-size: 14px;
	font-weight: bold;
}

.header5 {
	font-size: 14px;
	
}
.detail1 {
	font-size: 11px;
	font-weight: bold;
}
</style>
<body>

HERE;


// header
echo "<table border='0' width='100%' cellpadding='0' cellspacing='0'>
	<col width='85%'><col width='15%'>";
echo "<tr height='30px'><td ><span class='header2'>$CompanyName</span></td><td><span class='header3'>Print Date: $printdate[2]-$printdate[1]-$printdate[0]<br>Print Time: $dt[1]</span></td></tr>";
echo "<tr height='30px'><td colspan='2'></td></tr>";
echo "<tr height='30px'><td colspan='2' align='center'><span class='header1'>Tender Status Report</span></td></tr>";
echo "<tr height='30px'><td colspan='2'> </td></tr>";
echo "<tr height='10px'><td colspan='2'><span class='header3'>Filter By: </span></td></tr>";

echo "<tr height='10px'><td colspan='2'><span class='header3'>From/To Date: $fromdate / $todate </span></td></tr>";
if ($fromclient != '') 
	echo "<tr height='10px'><td colspan='2'><span class='header3'>From/To Item: $fromclient / $toclient</span></td></tr>";

if ($fromproject != '') 
	echo "<tr height='10px'><td colspan='2'><span class='header3'>From/To Group: $fromproject / $toproject</span></td></tr>";

echo "<tr height='20px'><td colspan='2'><span class='header3'></span></td></tr>";
echo "</table>";


$ndate = explode("/", $fromdate);	
$fromdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
$ndate = explode("/", $todate);	
$todate = $ndate[2]."-".$ndate[1]."-".$ndate[0];

$btop 		= "border-top: 1px solid #000";	
$bbottom 	= "border-bottom: 1px solid #000";	

echo "<table border='0' width='100%' cellpadding='0' cellspacing='4'>
	<col width='2%'>
	<col width='12%'>
	<col width='12%'>
	<col width='5%'>
	<col width='10%'>
	<col width='10%'>
	<col width='5%'>
	<col width='5%'>
	<col width='5%'>
	<col width='5%'>
	<col width='5%'>
	<col width='5%'>
	<col width='5%'>
	<col width='3%'>
	<col width='2%'>
	<col width='5%'>";
	
echo "<tr height='20px'>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>S/No</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Tender/Project</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Client</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Date Received</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Available Document</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Scope of Work</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Submission Due Date</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>To Be Ready For Review</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Urgent Action Req'd</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Status At</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Date Submitted</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Tender Amount</span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Direct Cost</span></td>
			<td align='center' colspan='2' style='$btop;$bbottom;'><span class='header4'>Margin<br><div style='float:left;width:50%;'>=N=</div><div style='float:right;width:50%;'>%</div></span></td>
			<td align='center' style='$btop;$bbottom;'><span class='header4'>Award Status</span></td>
	 </tr>";		

$query = " select tenders.project_code as project_code, project_name, client_code, ledger_name, date_format(tender_receivedDate,'%d/%m/%Y') as receivedDate, tender_documents, tender_scope,
			date_format(tender_dueDate,'%d/%m/%Y') as dueDate, if (ifnull(tender_reviewReady,0)=0, 'No', 'Yes'), tender_actionReq, tender_statusAt,	date_format(tender_submitDate,'%d/%m/%Y') as submitDate,  
			tender_amount, tender_directCost, tender_margin, tender_marginPer, if (ifnull(tender_awardStatus,0)=0, 'No', 'Yes') 
			from tenders
			inner join clients on Ledger_number = client_code
			inner join project on project.Project_Code = tenders.project_code ";  
			 
if ($projectexist >	0 && $FI_RepRestrictProject == 1)
  $query .= " Inner join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
		   " 			  where loginstk.user_account='".$_SESSION['useraccount']."' ".
		   "              and (project_code is not null and project_code != '') ".
		   "			 ) Q1 on tenders.project_code like concat(Q1.project_code,'%')   ";	 
						   
 $query .=  " Where (if ('$fromproject' = '', 1=1, tenders.Project_code between '$fromproject' and '$toproject' )) and 
			(if ('$fromclient' = '', 1=1, tenders.client_code between '$fromclient' and '$toclient' ))  and 
			Date_Format(tender_receivedDate,'%Y-%m-%d') between '$fromdate' and '$todate' ";  

$query .= " Order By tender_receivedDate Desc ";

$ct = 1;


$result = mysql_query($query, $connection);
while($row = mysql_fetch_array($result)){
	if (!($ct%2))
		$bgcolor = "bgcolor='#ABD4F1'";
	else 
		$bgcolor = "bgcolor='#EFF1F3'";	 
	
	echo "<tr $bgcolor>
				<td>$ct</td>
				<td><span class='header5'>$row[1]</span></td>
				<td><span class='header5'>$row[3]</span></td>
				<td><span class='header5'>$row[4]</span></td>
				<td><span class='header5'>$row[5]</span></td>
				<td><span class='header5'>$row[6]</span></td>
				<td><span class='header5'>$row[7]</span></td>
				<td><span class='header5'>$row[8]</span></td>
				<td><span class='header5'>$row[9]</span></td>
				<td><span class='header5'>$row[10]</span></td>
				<td><span class='header5'>$row[11]</span></td>
				<td><span class='header5'>".number_format($row[12],$DecimalBase)."</span></td>
				<td><span class='header5'>".number_format($row[13],$DecimalBase)."</span></td>
				<td><span class='header5'>".number_format($row[14],$DecimalBase)."</span></td>
				<td><span class='header5'>".number_format($row[15],$DecimalBase)."</span></td>
				<td><span class='header5'>$row[16]</span></td>
			</tr>";
	$ct++;		
}
echo "</table>";	
if ($printaccess==1)
print <<< HERE

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

echo "</body>
	</html>";

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














