<?php

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

}

print <<< HERE
<HTML>
<HEAD>
<TITLE> Vehicle Movement Report </TITLE>


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


</HEAD>
HERE;


include("config.inc.php");

//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($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

//maroun 30.03.2011
$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>';



  
print <<< HERE
<table width="100%" border="0" align="left" >
<tr><td>
<table width="100%" border="0" align="center">
<tr>
  <td width='30%'><b>$CompanyName</b></td>
  <td width='55%' align='center'><font size=4><b>Vehicle Movement</b></font></td>
  <td width='5%'><font size=1><b> Printed Date: </b> <br><b>Time: </b>
  <td width='10%'><font size=1>
HERE;

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

echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
echo "<br>".$dt[1]." ".$dt[2];
  
print <<<HERE
</font></td>
</tr>
<tr>
   <td colspan='4' width='100%'> <hr width='100%' color='black' size='1'> </td>
</tr>
</table>




<table class="params" width="100%" border="0" align="center" >
<tr>
  <td width="100%" valign='top'><b>$SelectedOptions</b></td>
</tr>

</table>

HERE;


	echo "    <table  border='0' align='left' class='master' style='table-layout:fixed;width:900px' cellpadding='0' cellspacing='0'>
				<col width=95>
				<col width=140>
				<col width=140>
				<col width=150>
				<col width=150>
				<col width=100>
				<col width=100>
				<col width=150>
				<col width=200>";

echo "<th>Reference</th>
		<th align='left'>Start Date/Time</th>
		<th align='left'>End Date/Time</th>
		<th align='left'>From Place</th>
		<th align='left'>To Place</th>
		<th align='left'>Millage</th>
		<th align='left'>Plate#</th>
		<th align='left'>Vehicle</th>
		<th align='left'>Employee</th>		
		";				
if (!empty($fromdate))
   $fromdate = explodeword($fromdate);

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

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

$fromreference      = GetSQLValueString($fromreference, 'text');
$toreference      = GetSQLValueString($toreference, 'text');
$vehicleid      = GetSQLValueString($vehicleid, 'int');
$placeid      = GetSQLValueString($placeid, 'int');

$q = "select employee_id from employee where concat(employee.employee_firstname,' ',employee_lastname) = '$fromemployee' ";
$result=mysql_query($q,$connection) or die(mysql_error());
$row=mysql_fetch_row($result);
$fromemployee = $row[0];
$fromemployee      = GetSQLValueString($fromemployee, 'text');

$query = "select vehicleMovement_id, date_format(vehicleMovement_start,'%d/%m/%Y %r'), date_format(vehicleMovement_end,'%d/%m/%Y %r'), fromplace_name,toplace_name, vehicle_make, concat(employee.employee_firstname,' ',employee_lastname)
			, vehiclemovement_millage, vehiclemovement_millagetype, vehiclemovement_reference,vehicle.vehicle_platenumber
			from vehiclemovement left join place on place.place_id = vehiclemovement.place_id
			left join vehicle on vehicle.vehicle_id = vehiclemovement.vehicle_id
			left join employee on employee.employee_id = vehiclemovement.employee_id
			where ( if('$vdate'='date',
					date_format(vehicleMovement_start,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(vehicleMovement_start,'%Y-%m-%d')) 
							and ifNull(". $todate.",date_format(vehicleMovement_start,'%Y-%m-%d')) 
					And
					date_format(ifnull(vehicleMovement_end,now()),'%Y-%m-%d') between ifNull(". $fromdate.",date_format(ifnull(vehicleMovement_end,now()),'%Y-%m-%d')) 
							and ifNull(". $todate.",date_format(ifnull(vehicleMovement_end,now()),'%Y-%m-%d')) 
					,1=1)		
					)
			and (if('$vdate'='reference',ifnull(vehiclemovement_reference,0) between ifnull($fromreference,ifnull(vehiclemovement_reference,0)) and ifnull($toreference,ifnull(vehiclemovement_reference,0)),1=1))		
			and ifnull(employee.employee_id,1) = ifNull(".$fromemployee.",ifnull(employee.employee_id,1))
			and ifnull(vehiclemovement.vehicle_id,1) = ifnull($vehicleid,ifnull(vehiclemovement.vehicle_id,1))
			and ifnull(vehiclemovement.place_id,1) = ifnull($placeid,ifnull(vehiclemovement.place_id,1))
			order by vehiclemovement_reference";
//echo $query ;
$result=mysql_query($query,$connection) or die(mysql_error());

		echo "<tr>";
		echo "   <td colspan='10'><hr width='100%' color='black' size='1'></td>";
		echo "</tr>";	
while($row=mysql_fetch_row($result)){

	echo "<tr>";
		echo "   <td >$row[9]</td>";
		echo "   <td >$row[1]</td>";
		echo "   <td >$row[2]</td>";
		echo "   <td >$row[3]</td>";
		echo "   <td >$row[4]</td>";
		echo "   <td >$row[7] $row[8]</td>";
		echo "   <td >$row[10]</td>";
		echo "   <td >$row[5]</td>";
		echo "   <td >$row[6]</td>";
		echo "</tr>";


		echo "<tr>";
		echo "   <td colspan='10'><hr width='100%' color='black' size='1'></td>";
		echo "</tr>";



}




























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

function format_currency($amount, $DecimalBase) { 

    if (Round(abs($amount), $DecimalBase) == 0)
		return number_format(abs($amount), $DecimalBase); 
	else if($amount < 0) 
        return "(".number_format(abs($amount),$DecimalBase).")"; 
    else 
		return number_format($amount, $DecimalBase); 
} 

function GetSQLValueString($theValue, $theType) 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "Null";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Null";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;
  }
  return $theValue;
}
?>

				 
				 
				 