<?php
include_once("classes/globals.php");
include_once("classes/mysql.php");

$link = new mySQL($__host, $__user, $__password, $__database);


if(isset($_GET["update"]))
{
	$client = $_GET["client_id"];
	$spill = $_GET["remaining"];
	$link->query("UPDATE spilltimes SET remaining=\"$spill\" WHERE client_id=\"$client\"");
}
?>
<html>
	<head>
		<title>Spill Times calculator</title>
		<style type="text/css">
		.header
			{
				font-weight:bold;
				background-color:#666666;
				color:#ffffff;
			}	
		.spilltable
		{
			border:solid 1px #dcdcdc;
			border-collapse:collapse;
			width:90%;
		}
		.spilltable td
		{
			border:solid 1px #dcdcdc;
			text-align:center;
		}
		.spilltable tr:hover
		{
			background-color:#FFFF00;
		}
		.updatetable
		{
			width:50%;
		}
		.updatetable tr:hover
		{
			background-color:#FF00FF;
		}
		</style>
	</head>
	
	<body>
		
		<h1>Spill Time calculator</h1>
<?php
$clientlink = new mySQL($__host, $__user, $__password, $__database);

$link->query("SELECT * FROM nr_clients WHERE support_hours <> 0000 ORDER BY ID ASC");

$now = date("m");
$thismonth = (strlen($now-1)>1) ? ($now-1):("0".($now-1));
$prevmonth = (strlen($thismonth-1)>1) ? ($thismonth-1):("0".($thismonth-1));

?>

<table class="spilltable">
<tr>
	<td class="header">Client</td>
	<td class="header">Spill last month</td>
	<td class="header">Hours this month</td>
	<td class="header">Allocated hours</td>
	<td class="header">Hours to bill</td>
	<td class="header">New Spill hours</td>
</tr>

<?php
$count = 0;
while($row = $link->getRow())
{
	++$count;
	$col = ($count%2==0)? "#F0F0F0":"#FFFFFF";
	$client = $row["ID"]; $allocated = $row["support_hours"]/100;
	?><tr bgcolor="<?php echo $col;?>">
		<td><?php echo $row["name"];?></td>
	<?php

	$query = "SELECT remaining FROM spillhours WHERE client_id=\"$client\"";
	$clientlink->query($query);
	$detail = $clientlink->getRow();
	$spill = $detail["remaining"];
	?> 	<td><?php echo $spill; ?></td> <?php

	$query = "SELECT details, date, time_out, time_in FROM nr_timesheet WHERE client_id=\"$client\" AND date BETWEEN 2007".$thismonth."00 AND 2007".$thismonth."31 ORDER BY date";
	$clientlink->query($query);
	$total = 0;
	
	while($detail = $clientlink->getRow())
	{
		$hours = substr($detail["time_out"],0,2)-substr($detail["time_in"],0,2);
		$mins = (substr($detail["time_out"],2,2)-substr($detail["time_in"],2,2))/60;
		$total += $hours+$mins;
	}
	?> 	<td><?php echo $total; ?></td> 
			<td><?php echo $allocated; ?></td>
			<td><?php 
					$tobill = (($allocated+$spill-$total)<0)? abs($allocated+$spill-$total) : 0;
				echo $tobill;?>
			</td>
			<td><?php
				$newspill = (($allocated+$spill-$total)<0)?  0 : abs($allocated+$spill-$total);
				echo $newspill;
				?>
			</td>
		</tr>	<?php
}

?>
</table>

<h1>Update spill times</h1>

<?php

$link->query("SELECT nr_clients.ID, nr_clients.name, spillhours.remaining FROM spillhours JOIN nr_clients ON spillhours.client_id=nr_clients.ID ORDER BY nr_clients.name ASC");
while($row = $link->getRow())
{?>
<form method="GET" action="<?php echo $_SERVER["PHP_SELF"];?>">
<table class="updatetable">
		<tr>
		<td width="50%"><?php	echo $row["name"];?></td>
		<td width="30%"><input name="remaining" type="text" size="5" value="<?php echo $row["remaining"];?>" /></td>
		<td width="20%">
			<input type="hidden" value="<?php echo $row["ID"];?>" name="client_id" />
			<input type="submit" value="Update" name="update" />
		</td>
	</tr>
</table>
</form>
<?php } ?>

</body>
</html>

syntax highlighted by Code2HTML, v. 0.9.1