<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

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

print <<< HERE
<HTML>
<HEAD>
<TITLE> Visit Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

</HEAD>

<style>
.header1{
	font-size:14px;
	font-weight:bold;
}
.body1{
	font-size:14px;
}
.body2{
	font-size:13px;
}
.header2{
	font-size:13px;
	font-weight:bold;
}
</style>
HERE;


//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 Company_Name as CompanyName, ifNull(Company_Logo,0) as Company_Logo,grid_color1, grid_color2 ".
			" From Setup ";
					
	$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
	
	while($rowsetup=mysql_fetch_array($resultsetup)){
		extract($rowsetup);
	}
	


$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['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>';

$btop = 'border-top:black solid 1px;';
$bbottom = 'border-bottom:black solid 1px;';
$bleft = 'border-left:black solid 1px;';
$bright = 'border-right:black solid 1px;';

$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$date = split("-",$dt[0]);
$printdate = $date[2]."/".$date[1]."/".$date[0];
$printtime = $dt[1]." ".$dt[2]. " ".$dt[3];

print <<< HERE
<table width="1080" border="0" align="center" cellspacing='0' cellpadding='0'>
<col width='215'><col width='325'><col width='215'><col width='325'>
HERE;

echo "<tr>";
if($Company_Logo != '')
	echo"	<td align='center' ><img src='LogoFile.php' style='width:120px;height:90px;'; /></td>";
else 	echo"<td align='center' ></td>";

echo"	<td align='left'  colspan='2'><span style='font-size:14px; font-weight:bold; font-style:italic;'>".$CompanyName."</span></td>";

echo"<td align='right'  ><span style='font-size:11px; font-weight:bold;font-family:arial;'>

Printed Date: $printdate<br> Printed Time: $printtime </span></td>";

echo "</tr>";
echo "<tr><td colspan='4' align='center'><span style='font-size:20px; font-weight:bold;'>Customer Report</span></td></tr>";
echo "<tr><td colspan='4'><hr></td></tr>";

$btop = 'border-top:black solid 1px;';
$bbottom = 'border-bottom:black solid 1px;';
$bleft = 'border-left:black solid 1px;';
$bright = 'border-right:black solid 1px;';

$query = "select Customer_Code, Customer_Name,  Customer_Address,Customer_Telephone1, Customer_Telephone2, 
		Customer_Email, customer_description, customer_group
		from customer
		where Customer_Code='$customer_code';";
$result=mysql_query($query,$connection) or die(mysql_error());		
$rows=mysql_fetch_array($result);
	extract($rows);

echo "<tr  height='20px'><td colspan='4' ></td></tr> ";	
echo "<tr  height='20px'><td colspan='4' style='$btop;$bleft;$bright;padding-left:5px;'></td></tr> ";	
echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Customer Code: </span></td>
	<td ><span class='body1'>$Customer_Code</span></td>
	<td ><span class='header1'>Customer Name: </span></td><td style='$bright;'><span class='body1'>$Customer_Name</span></td></tr>";	

echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Phone Number: </span></td><td><span class='body1'>$Customer_Telephone1 ";
if($Customer_Telephone2 != '' )echo " - $Customer_Telephone2 ";
echo "</span></td>
	<td><span class='header1'>Address: </span></td><td style='$bright;'><span class='body1'>$Customer_Address</span></td>
	</tr>";	

echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Email: </span></td>
	<td ><span class='body1'>$Customer_Email</span></td>
	<td ><span class='header1'>Group Name: </span></td><td style='$bright;'><span class='body1'>$customer_group</span></td></tr>";	

echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Description: </span></td>
	<td colspan='3' style='$bright;'><span class='body1'>$customer_description</span></td>
	</tr>";	
	
echo "<tr  height='20px'><td colspan='4' style='$bbottom;$bleft;$bright;padding-left:5px;'></td></tr> ";	
echo "<tr  height='20px'><td colspan='4' ></td></tr> ";

//Main office table
echo "<tr  height='30px'><td colspan='4' ><span class='header1'>Main Offices</span></td></tr> ";


echo "<tr  height='30px'><td colspan='4' ><table border=0 width='1080px'>";
echo "<col width='150px'><col width='120px'><col width='150px'><col width='120px'>
	<col width='100px'><col width='100px'><col width='100px'><col width='110px'><col width='110px'><col width='110px'>";
echo  "<tr>
           <td><span class='header2'>Address</span></td>
           <td><span class='header2'>Contact</span></td>
		   <td><span class='header2'>Contact Name</span></td>
	       <td><span class='header2'>Position</span></td>
		   <td><span class='header2'>Phone 1</span></td>
		   <td><span class='header2'>Phone 2</span></td>
		   <td><span class='header2'>Phone 3</span></td>
		   <td><span class='header2'>Email 1</span></td>
		   <td><span class='header2'>Email 2</span></td>";
	
echo "<tr><td colspan='10'><hr></td></tr>";	
$querymain = "select customer_officeId,contact_firstname,contact_lastname,office_address,
			contact_position, contact_phone1, contact_phone2, contact_phone3, contact_email1, 
			contact_email2
			from Customer_Offices
			left join CustomerContactInfo on customeroffice_id = customer_officeId
			where mainoffice_id is null and Customer_Code=$Customer_Code";
		
$resmain = mysql_query($querymain,$connection);
$officeid='';
$ct = 0;
while($rowmain = mysql_fetch_array($resmain)){
	extract($rowmain);
	
	$ct+=1;
		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
		
	echo  "<tr $Color>";
	if($customer_officeId != $officeid)
	echo "<td><span class='body2'>$office_address</span></td>";
	//else echo "<td></td><td></td>";	
	else echo "<td></td><td></td><td></td>";
	echo"<td><span class='body2'>$contact_firstname</span></td>".
	   "<td><span class='body2'>$contact_lastname</span></td>".
		"<td><span class='body2'>$contact_position</span></td>".
		"<td><span class='body2'>$contact_phone1</span></td>".
		"<td><span class='body2'>$contact_phone2</span></td>".
		"<td><span class='body2'>$contact_phone3</span></td>".
		"<td><span class='body2'>$contact_email1</span></td>".
		"<td><span class='body2'>$contact_email2</span></td>";
		
 //$officeid = $customer_officeId;

}	
echo "</table></td></tr> ";
//end of main office table

//branch table
echo "<tr  height='20px'><td colspan='4' ></td></tr> ";
echo "<tr  height='30px'><td colspan='4' ><span class='header1'>Branch Offices</span></td></tr> ";

echo "<tr  height='30px'><td colspan='4' ><table border=0 width='1080px'>";
echo "<col width='120px'><col width='110px'><col width='120px'><col width='130px'><col width='110px'>
	<col width='100px'><col width='100px'><col width='100px'><col width='110px'><col width='110px'><col width='110px'>";
echo  "<tr><td><span class='header2'>Address</span></td><td><span class='header2'>Contact</span></td>
	<td><span class='header2'>Main Office</span></td><td><span class='header2'>Contact Name</span></td>
	<td><span class='header2'>Position</span></td><td><span class='header2'>Phone 1</span></td><td><span class='header2'>Phone 2</span></td>
	<td><span class='header2'>Phone 3</span></td><td><span class='header2'>Email 1</span></td><td><span class='header2'>Email 2</span></td>
	";
echo "<tr><td colspan='11'><hr></td></tr>";	
$querybranch = "select c.customer_officeId as customer_officeId,c.office_address as office_address, 
			c.office_contact as office_contact,
			concat(contact_firstname,' ',contact_lastname) as contactname,
			contact_position, contact_phone1, contact_phone2, contact_phone3, contact_email1, 
			contact_email2, contact_website, c2.office_address as mainaddress
			from Customer_Offices as c
			left join CustomerContactInfo on customeroffice_id = c.customer_officeId
			left join Customer_Offices c2 on c2.customer_officeId = c.mainoffice_id
			where c.mainoffice_id is not null and c.Customer_Code=$Customer_Code"; 
$resbranch = mysql_query($querybranch,$connection);
$officeid='';
$ct = 0;
while($rowbranch = mysql_fetch_array($resbranch)){
	extract($rowbranch);
	
	$ct+=1;
		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
		
	echo  "<tr $Color>";
	if($customer_officeId != $officeid)
	echo "<td><span class='body2'>$office_address</span></td>".
		"<td><span class='body2'>$office_contact</span></td>".
		"<td><span class='body2'>$mainaddress</span></td>";
	else echo "<td></td><td></td><td></td>";	
	
	echo"<td><span class='body2'>$contactname</span></td>".
		"<td><span class='body2'>$contact_position</span></td>".
		"<td><span class='body2'>$contact_phone1</span></td>".
		"<td><span class='body2'>$contact_phone2</span></td>".
		"<td><span class='body2'>$contact_phone3</span></td>".
		"<td><span class='body2'>$contact_email1</span></td>".
		"<td><span class='body2'>$contact_email2</span></td>";
 $officeid = $customer_officeId;

}	
echo "</table></td></tr> ";
// end branch table

//project talble
echo "<tr  height='20px'><td colspan='4' ></td></tr> ";
echo "<tr  height='30px'><td colspan='4' ><span class='header1'>Projects</span></td></tr> ";

echo "<tr  height='30px'><td colspan='4' ><table border=0 width='1080px'>";
echo "<col width='120px'><col width='110px'><col width='100px'><col width='90px'>
	<col width='120px'><col width='130px'><col width='150px'><col width='80px'>
	<col width='110px'><col width='100px'>";
echo  "<tr><td><span class='header2'>Description</span></td><td><span class='header2'>Address</span></td>
	<td><span class='header2'>Contact</span></td><td><span class='header2'>Phone #</span></td>
	<td><span class='header2'>Consultant Name</span></td><td><span class='header2'>Details</span></td>
	<td><span class='header2'>Situation of the Construction</span></td><td><span class='header2'>Iron Used</span></td>
	<td><span class='header2'>Competitor Name</span></td><td><span class='header2'>Related Project</span></td></tr>";
echo "<tr><td colspan='10'><hr></td></tr>";	
$queryproj = "select p1.customerproject_id,p1.customerproject_desc as description, p1.customerproject_address as address,
			p1.customerproject_contact as contact,p1.customerproject_phone as phonenbr,
			p1.customerproject_consultantname as consultantname,p1.customerproject_consultantdetail as consultantdetail,
			p1.customerproject_situation as situation,p1.customerproject_ironname as ironname,
			p1.customerproject_competitorname as competitorname,p2.customerproject_desc as relatedproject_desc
			from customer_project p1
			left join customer_project p2 on p2.customerproject_id = p1.customerproject_parent
			where p1.customer_code = $Customer_Code"; 
$resproj = mysql_query($queryproj,$connection);
$ct = 0;
while($rowproj = mysql_fetch_array($resproj)){
	extract($rowproj);
	
	$ct+=1;
		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
		
	echo  "<tr $Color>";
	
	echo "<td><span class='body2'>$description</span></td>".
		"<td><span class='body2'>$address</span></td>";
	
	echo"<td><span class='body2'>$contact</span></td>".
		"<td><span class='body2'>$phonenbr</span></td>".
		"<td><span class='body2'>$consultantname</span></td>".
		"<td><span class='body2'>$consultantdetail</span></td>".
		"<td><span class='body2'>$situation</span></td>".
		"<td><span class='body2'>$ironname</span></td>".
		"<td><span class='body2'>$competitorname</span></td>".
		"<td><span class='body2'>$relatedproject_desc</span></td></tr>";

}	
echo "</table></td></tr> ";
//end project table	
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;

?>