<!DOCTYPE>
<?php

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



print <<< HERE
<HTML>
<HEAD>
<TITLE> Ticket Visit Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<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: 13px;
	font-weight: bold;
}	

.detail1 {
	font-size: 12px;
	font-weight: bold;
}

</style>
</HEAD>

HERE;



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

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

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


$PProjectCode 	= $_GET['PProjectCode'];
$fromdate 		= $_GET['fromdate'];
$todate			= $_GET['todate'];
$tvType			= $_GET['tvType'];
$tsSupTeamId 	= $_GET['tsSupTeamId'];
$tvClosed		= $_GET['tvClosed'];

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

$title = 'Ticket Visit Report';

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'>$title List</span></td></tr>";
echo "<tr height='30px'><td colspan='2'> </td></tr>";
echo "</table>";	

if ($tsSupTeamId != ''){
	$queryTeam 	 = "select username from user_support where user_id = $tsSupTeamId";
	$resTeam 	 = mysql_query($queryTeam, $connection);
	$rowTeam 	 = mysql_fetch_row($resTeam);
	$Team		 = $rowTeam[0];
	
}

if ($ticketVisit_type == 1)	$visitType = 'Maintenance';
else if ($ticketVisit_type == 2) $visitType = 'Ticket'; 
else $visitType = '';

if ($tvClosed == '1') $isClosed = 'Closed';
else if ($tvClosed == '2') $isClosed = 'Not Closed';
else $isClosed = '';

$SelectedOptions = "Filter by: <br>";
if ($fromdate != '')      		$SelectedOptions .= "-> Date From: ". $fromdate . "  To: ". $todate ."<br>";	
if ($PProjectCode != '')  		$SelectedOptions .= "-> Branch: ". $PProjectCode ."<br>";	
if ($ticketVisit_type != '')  	$SelectedOptions .= "-> Visit Type: ". $visitType."<br>";
if ($tsSupTeamId != '')   		$SelectedOptions .= "-> Support Team: ". $Team ."<br>";
if ($tvClosed != '')   	  		$SelectedOptions .= "-> Visit Status: ". $isClosed ."<br>";


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

$fromdate	   = GetSQLValueString($fromdate, 'date');
$todate        = GetSQLValueString($todate, 'date');   

  
  
print <<<HERE
<table width="100%" border="0" align="left" >
<tr><td>
<table class="report-master" width="100%" border="0" align="center" style='table-layout:fixed'>
<col width=100>
<tr><td>&nbsp;</td>
  <td  valign='top' width=100%><span class='header3'><b>$SelectedOptions</b></span></td>
</tr>
<tr height='5'><td><h5></td></tr>
</table></td></tr>
</table>
HERE;


 $wdt = "1180px";



echo "<table width='$wdt' cellspacing='4' cellpadding='0' border='0'>";

echo "<col width=150><col width=150><col width=150>
	  <col width=150><col width=100><col width=90>
	  <col width=90><col width=180><col width=200><col width=120>";

	
 echo"<tr height='25px'>
		 <td align='left' ><span class='header3'>Date/Time</td>
		 <td align='left' ><span class='header3'>Branch</td>
		 <td align='left' ><span class='header3'>Visit Type</td>
		 <td align='left' ><span class='header3'>Support Team</td>
		 <td align='left' ><span class='header3'>Status</td>
		 <td align='left' ><span class='header3'>Closed</td>
		 <td align='left' ><span class='header3'>Approved</td>
		 <td align='left' ><span class='header3'>Contact Person</td>
		 <td align='left' ><span class='header3'>Remark</td>
		 <td align='left' ><span class='header3'>User Account</td>
 </tr>"; 

 $querysearch = "select date_format(ticketVisit_date, '%d/%m/%Y %h:%i %p') as VisitDate, project_code, ticketVisit_type, username as TeamName, ticketVisit_status,
				if(ticketVisit_closed='1','&#10004;','&#10006') as ticketClosed, if(ticketVisit_approved='1','&#10004;','&#10006') as ticketApproved, ticketVisit_contactPerson,
				ticketVisit_remark, user_account
				from ticket_visit
				left join user_support on user_id = ticketVisit_userId
			where date_format(ticketVisit_date,'%Y-%m-%d') between $fromdate and $todate
				and if('$PProjectCode'='', 1=1, project_code = '$PProjectCode') 
				and if('$tvType' = '', 1=1, ticketVisit_type = '$tvType')	
				and if('$tsSupTeamId' = '', 1=1, ticketVisit_userId = '$tsSupTeamId')	
				and if('$tvClosed' = '2', ifnull(ticketVisit_closed,0) = 0, if('$tvClosed' = '1', ifnull(ticketVisit_closed,0) = 1, 1=1))	
			order by date_format(ticketVisit_date,'%Y-%m-%d %H:%i') desc "; 
$result = mysql_query($querysearch, $connection);

while($rows = mysql_fetch_array($result)){
	extract($rows);
	if (!($ct%2))
			$bgcolor = "bgcolor='#ABD4F1'";
		else 
			$bgcolor = "bgcolor='#EFF1F3'";
	
	if ($ticketVisit_type == 1)	$visitType = 'Maintenance';
	else if ($ticketVisit_type == 2) $visitType = 'Ticket'; 
	else $visitType = '';
	
	echo"<tr $bgcolor>
			<td align='left'><span class='detail1'>$VisitDate</span></td>
			<td align='left'><span class='detail1'>$project_code</span></td>	
			<td align='left'><span class='detail1'>$visitType</span></td>	
			<td align='left'><span class='detail1'>$TeamName</span></td>	
			<td align='left'><span class='detail1'>$ticketVisit_status</span></td>	
			<td align='center'><span class='detail1'>$ticketClosed</span></td>	
			<td align='center'><span class='detail1'>$ticketApproved</span></td>	
			<td align='left'><span class='detail1'>$ticketVisit_contactPerson</span></td>	
			<td align='left'><span class='detail1'>$ticketVisit_remark</span></td>	
			<td align='left'><span class='detail1'>$user_account</span></td>	
		</tr>";
	
	$ct++;
}
 
echo "</table>";



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




if ($printaccess==1)
print <<< HERE

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