<?php
session_start();
error_reporting(0);
set_time_limit(0);
include "conn.php";
isloggedin(session_id());

$from = $_POST["from"];
$to = $_POST["to"];
$typeid = $_POST["typeid"];
$staffid = $_POST["staffid"];

$pagetitle = '';
$query = "select move_staff.staffid, move.moveid, move.startdate, move.stopdate, move_type.typeid, move.movetitle, move.starttime, move.stoptime from move, move_type, move_staff where move.moveid=move_type.moveid and move.moveid=move_staff.moveid";

if ($from)
{
	$query .= " and (move.startdate>='$from' or move.stopdate>='$from')";
}
if ($to)
{
	$query .= " and (move.startdate<='$to' or move.stopdate<='$to')";
}

if ($typeid > 0)
{
	$typename = getmtype($typeid);
	$pagetitle .= " - " . $typename;
	
	$query .= " and move_type.typeid='$typeid'";
}
if ($staffid > 0)
{
	$name = getname($staffid);
	$pagetitle .= " - " . $name;
	
	$query .= " and move_staff.staffid='$staffid'";
}

$query .= " order by move.startdate desc, move.stopdate desc, move.movetitle asc";
$result = mysql_query($query) or die (mysql_error());
?>
<html><head><title>Staff Movements Report<?php echo $pagetitle; ?></title>
<link rel="stylesheet" href="../sft.css">
</head>
<body>
<center>
<table width=90% cellspacing=1 cellpadding=5 border=0>
<tr><td colspan=5 align=left><table width=100% cellspacing=0 cellpadding=0 border=0><tr><td><b>APP Systems Services Pte Ltd.</b></td><td align=right width=1%><input type=button value="Print" onclick="window.print()"></td></tr></table></td></tr>
<tr><td colspan=5 align=left><b>Staff Movements Report</b></td></tr>
<tr><td colspan=5>
<?php
if ($from)
{
	$tfrom = fd($from);
	
	echo "From: $tfrom";
	if ($to)
	{
		$tto = fd($to);
		
		echo "<br>To: $tto";
	}
	
	echo "<br>";
}
if ($typename)
{
	echo "Type: $typename<br>";
}
else
{
	echo "Type: All<br>";
}
if ($name)
{
	echo "Staff: $name";
}
{
	echo "Staff: All";
}
?>
</td></tr>
<tr><td colspan=5>&nbsp;</td></tr>

<?php
if (mysql_num_rows($result))
{
	echo "
		<tr bgcolor=a0a0a0><th width=1% nowrap>Staff</th><th width=1% nowrap>Start Date</th><th width=1% nowrap>Stop Date</th><th width=1% nowrap>Type</th><th width=1% nowrap>Activity</th></tr>";
	
	$tri = true;
		
	while ($row = mysql_fetch_array($result))
	{
		$staffid = $row[0];
		$moveid = $row[1];
		$startdate = $row[2];
		$stopdate = $row[3];
		$typeid = $row[4];
		$act = $row[5];
		$starttime = substr($row[6], 0, 5);
		$stoptime = substr($row[7], 0, 5);
		
		if ($tri = ! $tri)
		echo "
				<tr bgcolor=c9c9c9>";
		else
		echo "
				<tr bgcolor=e9e9e9>";
	
		echo "<td width=1% nowrap>" . getname($staffid) . "</td><td width=1% nowrap>" . fd($startdate) . " $starttime</td><td width=1% nowrap>" . fd($stopdate) . " $stoptime</td><td width=1% nowrap>" . getmtype($typeid) . "</td><td width=100%>$act</td>";
	}
}
else
{
	echo "<tr><td colspan=5>No movement found, <a href='javascript:window.close()'>close window</a></td></tr>";
}
?>
</table>
</center>
<?php
function fd($date)
{
	$split = explode("-", $date);
	$date = date("M jS, Y", mktime(0,0,0,$split[1], $split[2], $split[0]));
	return $date;
}

function getname($staffid)
{
	$res = mysql_query("select firstname, lastname from staff where staffid='$staffid'");
	
	if ($row = mysql_fetch_array($res))
	{
		$name = $row[0] . ", " . $row[1];
	}
	
	return $name;
}

function getmtype($typeid)
{
	$res = mysql_query("select typename from movetype where typeid='$typeid'");
	
	if ($row = mysql_fetch_array($res))
	{
		$typename = $row[0];
	}
	
	return $typename;
}
function isloggedin($sessid)
{
	include_once "conn.php";
	
	$query = "select loginstaff.staffid from loginstaff, staff where loginstaff.sessid='$sessid' and loginstaff.staffid=staff.staffid";
	$result = mysql_query($query) or die (mysql_error());
	if ($row = mysql_fetch_array($result))
	{
		$staffid = $row[0];
		return $staffid;
	}
	
	header ("location: staffarea.php");
}
?>