<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.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(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$query1="select grid_color1,grid_color2, company_name, Company_Logo, DecimalBase from setup";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
extract($rows);

}

$top = "border-top: 1px solid black";
$bottom = "border-bottom: 1px solid black";
$right = "border-right: 1px solid black";
$left = "border-left: 1px solid black";

$datefrom 	= $_GET['datefrom'];
$dateto 	= $_GET['dateto'];
$stafffrom 	= $_GET['stafffrom'];
$staffto 	= $_GET['staffto'];

$dispdatefrom = $datefrom;
$dispdateto   = $dateto;

if ($datefrom != ''){
	$ndate = explode('-',$datefrom);
	$datefrom = $ndate[2]."-".$ndate[1]."-".$ndate[0];
}
if ($dateto != ''){
	$ndate = explode('-',$dateto);
	$dateto = $ndate[2]."-".$ndate[1]."-".$ndate[0];
}
echo "<html>";
echo "<head>";
echo "<title>Staff List</title>";
echo "<style>";
echo " 
	.styleh1 {
	font-size: 12px;
    font-weight: bold;
    font-family: arial,sans-serif;
	} ";
echo ".styleh2 {
	font-size: 12px;
    font-family: arial,sans-serif;
	} ";
echo "</style>";
echo "</head>";

echo "<body>";
echo "<table width='100%' cellspacing=0 cellpadding=0>
<col width='20%'>
<col width='80%'>";

echo "<tr>";
if ($Company_Logo != '')
echo "<td  rowspan=2><img src='LogoFile.php' ></td>";
else echo "<td  >&nbsp;</td>";
echo "<td align='center'><span style='font-size:24px;font-weight:bold;'>$company_name</span></td></tr>";
echo "<tr height='30px'><td colspan='2'></td></tr>";
echo "<tr height='30px'><td colspan='2' align='right'><span style='font-size:12px;'><b>Printed Date:</b> ";
echo date("d/m/Y")."<br>";
echo " <b>Time:</b> ".date("h:i:sa");
echo "</span></td></tr>";
if ($datefrom != '') 
echo "<tr height='20px'><td colspan='2'><span class='styleh1'>Date: $dispdatefrom - $dispdateto</span></td></tr>";

echo "<tr height='20px'><td colspan='2'></td></tr>";
echo "</table>";

echo "<table width='100%' cellspacing=0 cellpadding=1>
	<col width='5%'>
	<col width='10%'>
	<col width='25%'>
	<col width='25%'>
	<col width='10%'>
	<col width='10%'>
	<col width='15%'>";
	
echo "<tr bgcolor='#d3d3d3' height='25px'>
		<td align='center' style='$top;$bottom;$right;$left;'><span class='styleh1'>S/N</span></td>
		<td align='center' style='$top;$bottom;$right;'><span class='styleh1'>Date</span></td>
		<td align='center' style='$top;$bottom;$right;'><span class='styleh1'>Occupant</span></td>
		<td align='center' style='$top;$bottom;$right;'><span class='styleh1'>Location</span></td>
		<td align='center' style='$top;$bottom;$right;'><span class='styleh1'>Previous Meter Reading</span></td>
		<td align='center' style='$top;$bottom;$right;'><span class='styleh1'>Present Meter Reading</span></td>
		<td align='center' style='$top;$bottom;$right;'><span class='styleh1'>Amount</span></td>
	</tr>";

	
	
$query = "select date_format(Electricity_Date,'%d-%m-%Y'), House_name,house_address, Electricity_PrevMeterReading, Electricity_CurMeterReading,Electricity_Amount
		from House_Electricity
		inner join staff_house on staff_house.house_Code = House_Electricity.house_Code
		where if ('$datefrom'='', 1=1, date_format(Electricity_Date,'%Y-%m-%d') between '$datefrom' and '$dateto')
		order by date_format(Electricity_Date,'%Y-%m-%d') ";

$i = 1;
$totalAmount = 0;
$res = mysql_query($query, $connection);
while ($rw = mysql_fetch_array($res)){
	
	if($i%2==0)
		$color = "style='background-color:#ABD4F1'";
	else
		$color = "style='background-color:#EFF1F3'";
	
	echo "<tr $color height='25px'>
			<td align='center' ><span class='styleh2'>$i</span></td>
			<td align='center' ><span class='styleh2'>$rw[0] </span></td>
			<td align='left' ><span class='styleh2'>$rw[1] ]</span></td>
			<td align='left' ><span class='styleh2'>$rw[2]</span></td>
			<td align='right' ><span class='styleh2'>$rw[3] </span></td>
			<td align='right' ><span class='styleh2'>$rw[4] </span></td>
			<td align='right' ><span class='styleh2'>".number_format($rw[5],$DecimalBase)." </span></td>
		</tr>";
	$totalAmount += $rw[5];	
	$i++;
	
} 

echo "<tr><td colspan='6' align='right' style='$top;'><span class='styleh1'>Total</span></td>
	<td align='right' style='$top;'><span class='styleh1'>".number_format($totalAmount,$DecimalBase)."</span></td></tr>";
echo "</table>";

print <<< HERE

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

echo "</body>";	
echo "</html>"


?>