<?php
header("Content-type: text/html; charset=windows-1256");

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


include("config.inc.php");
include("Functions_Purchase.php");


$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 ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
*/

//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

$querylog="SELECT print as printaccess, ifNull(FI_Grn,0) As FI_Grn, ifNull(FI_Purchases,0) As FI_Purchases, ExporttoExcel 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);
}


$querydflwh="select default_warehouse_code as defaultwarehouse from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_warehouse_code != '' ";
$resultdflwh=mysql_query($querydflwh,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflwh)){
  extract($rows);
}

//Projects List
$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());
$projectrows   = mysql_num_rows($projectresult);
if ($projectrows == 0)
    $queryproject=" select distinct project_code, project_name from project order by project_code ";
			  
//$resultfromproject=mysql_query($queryproject,$connection) or die(mysql_error());

//Costcenter List
$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(loginstk.costcent_code,'%') ".
 			   " where user_account='".$_SESSION['useraccount']."' ".
 			   " order by costcent.costcent_code";
			  
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentrows   = mysql_num_rows($costcentresult);
if ($costcentrows == 0)
    $querycostcent=" select distinct costcent_code, costcent_name from costcent order by costcent_code ";			  
//$resultcostcent=mysql_query($querycostcent,$connection) or die(mysql_error());



//Warehouse List
$querywarehouse=" select distinct warehouse.warehouse_code, warehouse_name ".
				" from warehouse where warehouse_code in (select warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') ".
				" order by warehouse.warehouse_code";
$resultwh = mysql_query($querywarehouse,$connection) or die(mysql_error());
$whexist   = mysql_num_rows($resultwh);
if ($whexist == 0)
	$querywarehouse="select warehouse_code,warehouse_name from warehouse order by warehouse_code";
	
$resultwarehouse=mysql_query($querywarehouse,$connection) or die(mysql_error());


$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());


$strt = $_SESSION['StartFinancial_Date'];
$end = $_SESSION['EndFinancial_Date'];

if (empty($fromdate)) $fromdate = displayToday();
if (empty($todate))   $todate   = displayToday();


$message = ereg_replace("'"," ",$message);

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">

<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>


<link type="text/css" href="js/calendar/jquery.datepick.css" rel="stylesheet">
<script type="text/javascript" src="js/calendar/jquery.datepick.js"></script>

<link type="text/css" href="js/grid/scripts/jquery-ui.css" rel="stylesheet">
<script type="text/javascript" src="js/grid/scripts/jquery-ui.min.js"></script>

<script type="text/javascript" language="javascript">
function showlistSalesman(str){
  var vdate, vcode;
  vdate=document.form1.fromdate.value;
  if(str==1)
  vcode=encodeURIComponent(document.form1.fromsalesmancode.value);
  else vcode=encodeURIComponent(document.form1.tosalesmancode.value);
  
  window.open('List_SorSalesman.php?frm=8&item='+str+'&PLedger_number='+vcode+'&TransDate='+vdate,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function PrintPrev(module,printaccess){

var fromdate = document.getElementById("fromdate").value;
var todate = document.getElementById("todate").value;

var fromsalesmancode = document.getElementById("fromsalesmancode").value;	
var tosalesmancode = document.getElementById("tosalesmancode").value;

var fromcustomercode = document.getElementById("fromcustomercode").value;
var tocustomercode = document.getElementById("tocustomercode").value;

if(document.getElementById("chkdetail").checked)
var vChkdetails = 1;
else var vChkdetails = 0;

if(document.getElementById("masterby1").checked)
var vmasterby = 1;
else if(document.getElementById("masterby2").checked)
var vmasterby = 2;


	
str = 'Rep_Visit.php?fromdate='+fromdate+'&todate='+todate+'&fromsalesmancode='+fromsalesmancode+'&tosalesmancode='+tosalesmancode+
	  '&fromcustomercode='+fromcustomercode+'&tocustomercode='+tocustomercode+'&chkdetails='+vChkdetails+'&masterby='+vmasterby+'&export=0';
	window.open(str,"_blank");

}


function ExportExcel(module){

var fromdate = document.getElementById("fromdate").value;
var todate = document.getElementById("todate").value;

var fromsalesmancode = document.getElementById("fromsalesmancode").value;	
var tosalesmancode = document.getElementById("tosalesmancode").value;

if(document.getElementById("chkdetail").checked)
var vChkdetails = 1;
else var vChkdetails = 0;

var fromcustomercode = document.getElementById("fromcustomercode").value;
var tocustomercode = document.getElementById("tocustomercode").value;
if(fromsalesmancode=='' && tosalesmancode=='' && fromcustomercode=='' && tocustomercode=='')
{
	alert('No Data was selected!');
	return 0;
}
else {	
str = 'Rep_Visit.php?fromdate='+fromdate+'&todate='+todate+'&fromsalesmancode='+fromsalesmancode+'&tosalesmancode='+tosalesmancode+
	  '&fromcustomercode='+fromcustomercode+'&tocustomercode='+tocustomercode+'&chkdetails='+vChkdetails+'&export=1';
	window.open(str,"_blank");
}
}

function OpenCustomer(str){ 
	 
	
	Win_Employee = window.open('List_Customers.php?frm=5&item='+str+'&PLedgerNo=&fromcallerid=1&isfound=','List_Customers', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=yes,width=900,height=750,left=250,top=200');		
	Win_Employee.creator = self;
	Win_Employee.focus();
	
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Visit Reports</title>



<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
.style1 {
	color: #FF0000;
	font-family: "Times New Roman", Times, serif;
	font-size: 20px;
}
.style8 {font-size: 11px; color: #999999; font-weight: bold; }
.style11 {font-size: 11px}
body {
	margin-top: 0px;
}
-->
</style>
</head>
<body >
<form name='form1' method='post'  enctype='multipart/form-data'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='message' value='$message' size='200'>

HERE;

//$message='';




echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Visit Reports</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='800px' align='left'>";

echo" <button type='reset' id='btnClear' name='btnClear'  title='Clear Page'><span><span>Clear</span></span></button>";
     

	 echo" <button type='button' id='preview' name='preview' onClick=\"PrintPrev(".$HTTP_SESSION_VARS['module'].",'".$printaccess."')\"; title='Preview'><span><span>Preview</span></span></button>";
	

if ($ExporttoExcel==1){
	 echo" <button type='button' id='btnExcel' name='btnExcel' onClick=\"ExportExcel(".$HTTP_SESSION_VARS['module'].")\"; title='Export Excel'><span><span>Export Excel</span></span></button>";

}	


echo"</td>";



echo"	</td></tr></table>";
echo"</td></tr>";				

		
echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
   <tr><td valign='top' align='left' colspan='10'>";
   
   	

print <<<HERE

		<table border="0" style='table-layout:fixed'>
		  <col width=80>
		  <col width=120>
		  <col width=300>
		  <col width=120>
		  <col width=40>
		  <col width=70>
		  <col width=50>
		  <col width=70>
		  <col width=100>
		  <col width=150>
		

HERE;

echo" <tr  height='30'>";	  
echo"   <td colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'>Date</span></td><td></td>";
echo"	<td align='center'><span class='style8'> Master By </span></td>";
echo "</tr>";

echo" <tr>";	  
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style8'>From</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
echo"      <input type='text' name='fromdate'  id='fromdate' value='$fromdate' maxlength='10' size='8'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.form1.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
echo"	<td align='center'><input type='checkbox' name='chkdetail' id='chkdetail' ><span class='style8'> Details</span></td>";
echo"	<td align='left'><input type='radio' name='masterby' id='masterby1' value='1' checked><span class='style8'> Client</span></td>";
echo "</tr>";

echo" <tr>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style8'>To</span></td>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style8'>";
echo"      <input type='text' name='todate'  id='todate' value='$todate'  maxlength='10' size='8'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.form1.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
echo"	<td align='center'></td>";
echo"	<td align='left'><input type='radio' name='masterby' id='masterby2' value='2' ><span class='style8'> Salesman</span></td>";
echo" </tr>";

//salesman
echo" <tr  height='20'><td></td></tr>";
echo" <tr  height='30'>";	  
echo"   <td colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'>Salesmen</span></td> ";
echo "</tr>";

echo" <tr>";	  
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style8'>From</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='2'><span class='style1'>";
echo"      <input type='text' name='fromsalesmancode'  id='fromsalesmancode' value='$fromsalesmancode' maxlength='10' style='width:95px;' >";
echo"      <input type='text' name='fromsalesmanname'  id='fromsalesmanname' value='$fromsalesmanname' style='width:285px;' ></span> ";
echo "<label><span class='style1'><img src='img/btnopen.jpg' title=' Select Salesman ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistSalesman(1); ' /> </span></label>";
echo "</td></tr>";

echo" <tr>";	  
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style8'>To</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='2'><span class='style8'>";
echo"      <input type='text' name='tosalesmancode'  id='tosalesmancode' value='$tosalesmancode' maxlength='10' style='width:95px;' >";
echo"      <input type='text' name='tosalesmanname'  id='tosalesmanname' value='$tosalesmanname' style='width:285px;' ></span> ";
echo "<label><span class='style1'><img src='img/btnopen.jpg' title=' Select Salesman ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistSalesman(2); ' /> </span></label>";
echo "</td></tr>";

//customer
echo" <tr  height='20'><td></td></tr>";
echo" <tr  height='30'>";	  
echo"   <td colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'>Customer</span></td> ";
echo "</tr>";

echo" <tr>";	  
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style8'>From</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='2'><span class='style1'>";
echo"      <input type='text' name='fromcustomercode'  id='fromcustomercode' value='$fromcustomercode' maxlength='10' style='width:95px;' >";
echo"      <input type='text' name='fromcustomername'  id='fromcustomername' value='$fromcustomername' style='width:285px;' ></span> ";
echo "<label><span class='style1'><img src='img/btnopen.jpg' title=' Select Salesman ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='OpenCustomer(1); ' /> </span></label>";
echo "</td></tr>";

echo" <tr>";	  
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style8'>To</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='2'><span class='style8'>";
echo"      <input type='text' name='tocustomercode'  id='tocustomercode' value='$tocustomercode' maxlength='10' style='width:95px;' >";
echo"      <input type='text' name='tocustomername'  id='tocustomername' value='$tocustomername' style='width:285px;' ></span> ";
echo "<label><span class='style1'><img src='img/btnopen.jpg' title=' Select Salesman ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='OpenCustomer(2); ' /> </span></label>";
echo "</td></tr>";

echo"   </table></td></tr>"; 
 echo"   </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
echo"   </table></td></tr>"; 

echo" </table>  ";//end of last table menu
	
print <<<HERE
</form>
</body>

HERE;

?>