<?php
session_start();
$username = $_SESSION['staff'];
include ("connect.php");
$staffID=$_GET['staffID'];

if($_POST["Print"] == "Print"){
$billNo = $_POST['billNo'];
echo "<script language='javascript'>window.location='receiptprint.php?billNo=".$billNo."'</script>";
}

if($_POST["Back"] == "Back")
{
	echo "<script language='javascript'>window.location='salesrecord.php'</script>"; 
}

if($_SESSION['staff']!= null)
{
?>
	
<html>
<head>
<title>Daily Bookstore - Cashier System</title>
<!--include css file-->
<link href="bookstaff.css" rel="stylesheet" type="text/css"/>

<!--clock function-->
<script type="text/javascript">
function init ( )
{
  timeDisplay = document.createTextNode ( "" );
  document.getElementById("clock").appendChild ( timeDisplay );
}

function updateClock ( )
{
  var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Compose the string for display
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}

// -->
</script>
</head>

<body onload="updateClock(); setInterval('updateClock()', 1000 )">

<table border="0" width="100%">

<tr>
<td><img src="Picture/Logo.gif"></td>
</tr>

<tr>
<td>&nbsp;</td>
</tr>

<tr>
<td colspan="2"><h2>Daily Bookstore - Cashier System</h2></td>
<td align="right"><h3>Current Time:<span id="clock">&nbsp;</span></h3></td>
</tr>

<tr>
<td colspan="2"></td>
<td align="right" style="color:darkblue"><h3>You log in as:<span>
<?php
echo "<span style='color:black;font-weight:bold'>".$username."</span>";
?>
<a href="logout.php">(Logout)</a>
</h3>
</span>
</td>
</tr>

</table>

<table align="center" width="100%" border="0">

<tr>
<td>
<fieldset>
<legend><img src="Picture/gear.gif" width="40" height="40">Function List</legend>
<div class="toplink">

<!--function list-->
<?php
include ("cashierfunction.php");
?>

</center>
<center>
<div id="mainpage">
</fieldset>
</td>
<td width="75%">
<?php
include("connect.php");
$receiptNo=$_GET['receiptNo'];

$result = mysql_query("SELECT * FROM receipt WHERE receiptNo = '$receiptNo'");
$row = mysql_fetch_array($result);

$result2 = mysql_query("SELECT * FROM receiptitem WHERE receiptNo = '$receiptNo'");
$num_results = mysql_num_rows($result2);

echo "<div id='bill'>";
echo "<form name='billform' method='post'>";

echo "<fieldset style='width:90%'>";
echo "<legend><input type='hidden' name='billNo' value='$receiptNo'><img src='Picture/po.gif' width='30' height='30'>Bill - $receiptNo</legend>";
echo "<br/>";
echo "<table width='100%' border='0'>";
echo "<tr>";
echo "<td width='15%'><b>Date: </b></td>";
echo "<td colspan='5'>".$row["receiptDate"]."</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Cashier Name: </b></td>";
echo "<td colspan='5'>".$row["cashierName"]."</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Member Name: </b></td>";
echo "<td colspan='5'>".$row["memberLname"]."</td>";
echo "</tr>";
echo "</table>";

echo "<br/>";

echo "<table border='1' cellspacing='0' cellpadding='5' bordercolor='white' width='100%' align=''>";
echo "<tr>";
echo "<th width='5%'>No</td>";
echo "<th width='15%'>ISBN</td>";
echo "<th width='40%'>Description</td>";
echo "<th width='10%'>Unit<br/>Price</td>";
echo "<th width='10%'>Discount</td>";
echo "<th width='10%'>Quantity</td>";
echo "<th width='10%'>Total</td>";
echo "</tr>";

for ($i=0; $i <$num_results; $i++)
{
	$row2 = mysql_fetch_array($result2);
	echo "<tr>";
	echo "<td>".($i+1)."</td>";
	echo "<td><input type='hidden' name='receiptItem".$i."' value='$row2[receiptItem]'>".$row2['receiptItem']."</td>";
	
	$ISBN = $row2['receiptItem'];
	$p = $row2['receiptPrice'];
	$q = $row2['receiptQuantity'];
	$subtotal = $p*$q;
	$total = $total + $subtotal;
	$totalq = $totalq+$q;
	
	$result5 = mysql_query("SELECT itemName FROM item WHERE ISBN = '$ISBN'");
	$row5 = mysql_fetch_array($result5);
	echo "<td>".$row5['itemName']."</td>";
	echo "<td><input type='hidden' name='price".$i."' size='10' value='$row2[receiptPrice]'>RM ";
	printf("%10.2f",$p);
	echo "</td>";
	echo "<td><input type='hidden' name='discount".$i."' size='10' value='$row2[receiptDiscount]'>".$row2['receiptDiscount']."</td>";
	echo "<td><input type='hidden' name='quantity".$i."' size='10' value='$row2[receiptQuantity]'>".$row2['receiptQuantity']."</td>";
	echo "<td>RM ";
	printf("%10.2f",$subtotal);
	echo "</td>";
	echo "</tr>";
}

echo "<tr>";
	echo "<th colspan='5'>Total</td>";
	echo "<th>$totalq</td>";
	echo "<th>RM ";
	printf("%10.2f",$total);
	echo "</td>";
echo "</tr>";

echo "<tr>";
	echo "<th colspan='6' align='left'>Payment:</td>";
	echo "<th align='right'>RM ";
	printf("%10.2f",$row['receiptCash']);
	echo "</td>";
echo "</tr>";

echo "<tr>";
	echo "<th colspan='6' align='left'>Change Due:</td>";
	echo "<th align='right'>RM ";
	printf("%10.2f",($row['receiptCash'] - $total));
	echo "</td>";
echo "</tr>";


echo "<tr>";
	echo "<td colspan='7' align='center'>
	<input type='submit' name='Print' value='Print'>
	<input type='submit' name='Back' value='Back'>";
	echo "</td>";
echo "</tr>";
echo "</table>";
echo "</fieldset>";
echo "</div>";
?>
</td>
</tr>
</table>
</div>
</center>
<?php
}else{
	echo "<script language='javascript'>alert ('Please login.')</script>";
	echo "<script language='javascript'>window.location='index.php'</script>";
}
?>
<hr/>

<center>
<div id="bottom">
<br/>
Copyright &#169; with Tian Tian Bookstore 2011. All Rights Reserved.
<br/>
Website:<a href="../bookstore" target="_blank">www.dailybook.com.my
</div>
</center>

</body>
</html>