<!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;
}
</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="700" border="0" align="center" cellspacing='0' cellpadding='0'>
<col width='120'><col width='230'><col width='120'><col width='230'>
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>";
$query = "select visit_reference, Customer_Name, visit.salesmen_code as salesmen_code,Ledger_name, date_format(visit_date,'%d-%m-%Y') as visit_date,
		date_format(visit_starttime,'%H:%i') as visit_starttime,
		date_format(visit_endtime,'%H:%i') as visit_endtime,
		visit_note, visit_request,
		if(Visit_type=1,'Visit',if(Visit_type=2,'Call',if(Visit_type=3,'Mail',''))) as Visit_type,
		visit_followup, date_format(Visit_followupdate,'%d-%m-%Y')  as Visit_followupdate,
		visit_priceoffered, visit_paymentterms, visit_creditlimit, visit_gift, visit_result,
		visit_commission,
		c1.office_address as main_address, c2.office_address as branch_address, customerproject_desc,
		c1.office_contact as office_contact,Customer_Address, date_format(visit_followTime,'%H:%i') as visit_followTime
		from visit
		left join customer c on c.Customer_Code = visit.customer_code
		left join salesmen s on s.Ledger_number = visit.salesmen_code
		left join Customer_Offices c1 on c1.customer_officeId = visit.visit_mainofficeId
		left join Customer_Offices c2 on c2.customer_officeId = visit.visit_branchofficeId
		left join customer_project p on p.customerproject_id = visit.visit_projectId
		where visit_reference=$visitId;";
$result=mysql_query($query,$connection) or die(mysql_error());		
$rows=mysql_fetch_array($result);
	extract($rows);
echo "<tr><td colspan='4' align='center'><span style='font-size:20px; font-weight:bold;'>Visit Report</span><br><span style='font-size:16px; font-weight:bold;'>Ref. $visit_reference</span></td></tr>";

echo "<tr><td colspan='4'><hr></td></tr>";


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'>Salesman: </span></td>
	<td colspan='3' style='$bright;'><span class='body1'>$salesmen_code - $Ledger_name</span></td></tr>";	
echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Date: </span></td><td><span class='body1'>$visit_date</span></td>";
if($visit_starttime != '00:00' || $visit_endtime != '00:00')
echo "<td><span class='header1'>Visit Time: </span></td><td style='$bright;'>$visit_starttime - $visit_endtime</td>";
else echo "<td></td><td style='$bright;'></td>";
echo "</tr>";	
echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Visit Type: </span></td><td><span class='body1'>$Visit_type</span></td>
	<td></td><td style='$bright;'></td>
	</tr>";
if($visit_followup == 1){	
	echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Follow up Date: </span></td><td><span class='body1'>$Visit_followupdate</span></td>";
	if($visit_followTime != '00:00') echo "<td><span class='header1'>Follow up Time: </span></td><td style='$bright;'><span class='body1'>$visit_followTime</span></td>";
	else echo "<td></td><td style='$bright;'></td>";
	echo "</tr>";
}
	
echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Client: </span></td><td colspan='3' style='$bright;'><span class='body1'>$Customer_Name</span></td>
	</tr>";	
echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Contact: </span></td><td colspan='3' style='$bright;'><span class='body1'>$office_contact</span></td>
	</tr>";	
echo "<tr  height='30px'><td style='$bleft;padding-left:5px;'><span class='header1'>Address: </span></td><td colspan='3' style='$bright;'><span class='body1'>$Customer_Address</span></td>
	</tr>";		

echo "<tr  height='20px'><td colspan='4' style='$bbottom;$bleft;$bright;padding-left:5px;'></td></tr> ";

echo "<tr  height='40px'><td colspan='4'></td></tr> ";
echo "<tr  height='30px'><td><span class='header1'>Main Office: </span></td><td colspan='3'><span class='body1'>$main_address</span></td>
	</tr>";		
	
echo "<tr  height='30px'><td><span class='header1'>Branch: </span></td><td colspan='3'><span class='body1'>$branch_address</span></td>
	</tr>";		

echo "<tr  height='35px'><td><span class='header1'>Project: </span></td><td colspan='3'><span class='body1'>$customerproject_desc</span></td>
	</tr>";	

echo "<tr  height='35px'><td><span class='header1'>Price Offered: </span></td><td colspan='3'><span class='body1'>$visit_priceoffered</span></td>
	</tr>";		

echo "<tr  height='35px'><td><span class='header1'>Credit Limit: </span></td><td><span class='body1'>$visit_creditlimit</span></td>
	<td><span class='header1'>Gift: </span></td><td><span class='body1'>$visit_gift</span></td></tr>";	
	
echo "<tr  height='35px'><td><span class='header1'>Payment Terms: </span></td><td colspan='3'><span class='body1'>$visit_paymentterms</span></td>
	</tr>";		

echo "<tr  height='35px'><td><span class='header1'>Commission Offered: </span></td><td colspan='3'><span class='body1'>$visit_commission</span></td></tr>";			

echo "<tr  height='30px'><td><span class='header1'>Result: </span></td><td colspan='3'><span class='body1'>$visit_result</span></td>
	</tr>";		

echo "<tr  height='35px'><td><span class='header1'>Remarks: </span></td><td colspan='3'><span class='body1'>$visit_note</span></td>
	</tr>";		


echo "<tr  height='35px'><td><span class='header1'>Terms & Conditions: </span></td><td colspan='3'><span class='body1'>$visit_request</span></td>
	</tr>";		
	
	
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;

?>