<?php


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

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

$ledger_number = $_POST["ledger_number"]; if ($ledger_number == ""){$ledger_number = $_GET["ledger_number"];}	
$todate = $_POST["todate"]; if ($todate == ""){$todate = $_GET["todate"];}	
$fromdate = $_POST["fromdate"]; if ($fromdate == ""){$fromdate = $_GET["fromdate"];}	
$users = $_POST["users"]; if ($users == ""){$users = $_GET["users"];}	
$groupby = $_POST["groupby"]; if ($groupby == ""){$groupby = $_GET["groupby"];}	

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

$query_login="select login_belongstogrp from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlogin=mysql_query($query_login,$connection) or die(mysql_error());

$row_login=mysql_fetch_row($resultlogin);
$belongstogroup=$row_login[0];

///    Base1 Base2    

$querysetup="select  base1, base2 ,DecimalBase1,DecimalBase2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}

$user=$HTTP_SESSION_VARS['useraccount'];

if($export ==1){
	$filename = "Exp_DailyVisit" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 

if($export ==1){
}
else  
print <<< HERE
<HTML>
<HEAD>
<TITLE>Daily Visits</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">
	
.data{
	
	 FONT-WEIGHT: normal;
	 FONT-SIZE: 8pt;	 
	
	 FONT-FAMILY: arial, sans-serif;
	}
	
.dataitalic{
	
	 FONT-STYLE: italic;
	 FONT-WEIGHT: normal;
	 FONT-SIZE: 8pt;	 
	
	 FONT-FAMILY: arial, sans-serif;
	}

.headeritalic{
	
	 FONT-STYLE: italic;
	 FONT-WEIGHT: bold;
	 FONT-SIZE: 9pt;	 
	
	 FONT-FAMILY: arial, sans-serif;
	}
	
.header{
	
	 FONT-WEIGHT: bold;
	 FONT-SIZE: 9pt;	 
	
	 FONT-FAMILY: arial, sans-serif;
	}
	
.footerbox{
				
							   
	border-left: 0 solid #000000; border-top: 1 solid #000000; border-bottom: 0 solid #000000;
	FONT-WEIGHT: bold;
	FONT-SIZE: 14px;	 
	color:#2D2D2D;
	font-family:Times-New-Roman;
	BACKGROUND-COLOR: #FFFFFF; 
	
	height: 20;
	 	}
		
</style>





</HEAD>

<BODY>
HERE;



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


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





echo "<table border=0 width='100%' cellspacing=0 cellpadding=0  style='table-layout:fixed;font-size:10px' >";
		
	
	echo "<tr height='80' ><td height='60'><img src='LogoFile.php' width='180px' height='100px' /></td><td  align='center'><b><font size=5><u>Daily Visits</u></font></td>
				<td align='right' ><font size='2'><b>Printed&nbsp;Date&nbsp;&amp;&nbsp;Time:</b>&nbsp;&nbsp;";
	echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
	echo "<br>".$dt[1]." ".$dt[2];
	echo "</font></td></tr>";
	
	
	$query = "select concat(user_code,'-',".$config['dbName_log'].".users.user_name),Client_code,Dailyvisit_latitude,Dailyvisit_longtitude,date_format(dailyvisit_Date,'%d/%m/%Y %h:%i'),if(dailyvisit_found=1,'Yes','No'),dailyvisit_remark,ledger_name 
				,dailyvisit_error from dailyvisits
				left join ledger on ledger.ledger_number = dailyvisits.Client_code 
				left join ".$config['dbName_log'].".users on users.user_account = dailyvisits.user_code where 1= 1";
if ($users != "") $query = $query. " and user_code = '".$users."'";	
if ($ledger_number != "") $query = $query. " and client_code = '".$ledger_number."'";	
if ($fromdate != "") $query = $query. " and dailyvisit_date >= '".explodeword2($fromdate)."'";	
if ($todate != "") $query = $query. " and dailyvisit_date <= '".explodeword2($todate)." 23:59:59' ";	


$query = $query. " and ifnull(dailyvisit_error,-1) = if('$printerror'='e',1,if('$printerror'='o',0,ifnull(dailyvisit_error,-1))) ";

if ($groupby == "users") 	$query = $query. " order by user_code,dailyvisit_Date";
if ($groupby == "clients") 	$query = $query. " order by client_code,dailyvisit_Date";

	
	
	$result = mysql_query($query) or die(mysql_error());

	echo "<tr height = '40'><td colspan =3 ></td></tr>";
	
	
	if ($groupby == "users") {
	
	echo "<tr><td colspan =3 >
	
	<table width= 1100 cellspacing = 0 style='table-layout:fixed;font-size:13px'  border=1>
	<tr>
	<th width=90 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;'>User Code</th>
	<th width=90 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;'>Date</th>
	<th width=90 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;'>Code</th>
	<th width=260 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;' colspan = 2>Name</th>

	<th width=90 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;'>Found</th>
	<th width=250 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;border-right: 1 solid #000000;'>Remark</th>";

	//if($printerror=='e' or $printerror=='b')
		echo"<th width=90 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;border-right: 1 solid #000000;'>Location</th>";
	
	echo"</tr>";
	$id = "";
	while($row=mysql_fetch_row($result)){
	if ($row[0] != $id) {
	echo "<tr>";
	echo "<td><b>$row[0]</b></td>";
	echo "<td>$row[4]</td>";
	echo "<td ></td>";
	echo "<td colspan = 2></td>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td></td></tr>";
	
		echo "<tr>";
	echo "<td></td>";
	echo "<td>$row[4]</td>";
	echo "<td>$row[1]</td>";
	echo "<td colspan = 2>$row[7]</td>";

	echo "<td align = 'center'>$row[5]</td>";
	echo "<td>$row[6]</td>";
	}
	else {
	echo "<tr>";
	echo "<td></td>";
	echo "<td>$row[4]</td>";
	echo "<td>$row[1]</td>";
	echo "<td colspan = 2>$row[7]</td>";

	echo "<td align = 'center'>$row[5]</td>";
	echo "<td>$row[6]</td>";
	
	
	
	
	}

	if($row[8]==1)
		echo "<td align=center>Right</td></tr>";
		else echo "<td align=center>Wrong</td></tr>";


	$id = $row[0];
	}
	
	echo"	</table></td></tr></table>";
	}
	
	if($groupby == "clients") {
	
	
		
	echo "<tr><td colspan =3 >
	
	<table width=1100 cellspacing=0  >
	<tr>
	<th width=90 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;'>Code</th>
	<th width=90 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;'>User Code</th>
	<th width=90 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;'>Date</th>
	<th width=360 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;' colspan = 2>Name</th>

	<th width=90 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;'>Found</th>
	<th width=460 style = 'border-left: 1 solid #000000;border-top: 1 solid #000000;border-bottom: 1 solid #000000;border-right: 1 solid #000000;'>Remark</th>

	
	
	</tr>";
	$id = "";
	while($row=mysql_fetch_row($result)){
	if ($row[1] != $id) {
	echo "<tr>";
	echo "<td><b>$row[1]</b></td>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td><b>$row[7]</b></td>";

	echo "<td></td>";
	echo "<td></td>";
	echo "<td></td></tr>";
	
		echo "<tr>";
	echo "<td></td>";
	echo "<td>$row[0]</td>";
	echo "<td>$row[4]</td>";
	echo "<td colspan = 2></td>";

	echo "<td align = 'center'>$row[5]</td>";
	echo "<td> $row[6]</td></tr>";
	}
	else {
	echo "<tr>";
	echo "<td></td>";
	echo "<td>$row[0]</td>";
	echo "<td>$row[4]</td>";
	echo "<td colspan = 2></td>";

	echo "<td align = 'center'>$row[5]</td>";
	echo "<td>$row[6]</td></tr>";
	
	
	
	
	}
	
	
	$id = $row[1];
	}
	
	
	
	
	
	
	
	
	
	echo"	</table></td></tr></table>";
	
	
	}

	
	

	
	function explodeword2($date)
	{

		$trim = explode('/',$date);
		$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";

		return $trim;
	}
print <<< HERE

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

	