<?php
session_start();
$username = $_SESSION['staff'];


$staffID=$_GET['staffID'];

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

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

if($_POST["Delete"] == "Delete")
{
include("connect.php");


$poNo=$_POST['poNo'];
	$query=mysql_query("delete from purchaseorder where poNo='$poNo'");
	$query2=mysql_query("delete from poitem where poNo='$poNo'");
	echo "<script>alert('Purchase Order $poNo is deleted.')</script>";
	echo "<script language='javascript'>window.location='purchaseorderlist.php'</script>"; 
}
if($_SESSION['staff']!= null)
{
?>
	
<html>
<head>
<title>Daily Bookstore - Management System</title>
<!--include css file-->
<link href="bookstaff.css" rel="stylesheet" type="text/css"/>
</head>

<body>

<?php
include ("stafffunctionbar.php");
?>
</center>
<center>
<div id="mainpage">
<table width="90%">
<tr>
<td width="15%" valign="top">
<fieldset style="margin:0 5 0 0">
<legend><img src="Picture/bill.gif" width="30" height="30">Manage Bill</legend>
<table>
<tr>
<td valign="top">
<a href="purchaseorderlist.php"><img src="Picture/po.gif" width="30" height="30" alt="PO List"><a href="purchaseorderlist.php">PO List</a><br/>
<br/>
<a href="addpo.php"><img src="Picture/add.gif" width="30" height="30" alt="add purchase order"><a href="addpo.php">Create PO</a><br/>
<br/>
</td>
</tr>
</table>
</fieldset>
</td>
<td width="75%">
<?php
include("connect.php");
$poNo=$_GET['poNo'];

$result = mysql_query("SELECT * FROM purchaseorder WHERE poNo = '$poNo'");

$row = mysql_fetch_array($result);

$result2 = mysql_query("SELECT * FROM poitem WHERE poNo = '$poNo'");

$num_results = mysql_num_rows($result2);

$sup = $row['supID'];

$result3 = mysql_query("SELECT * FROM supplier WHERE supID = '$sup'");

$row3 = mysql_fetch_array($result3);
echo "<div id='bill'>";
echo "<form name='poviewform' method='post'>";

echo "<fieldset style='width:90%'>";
echo "<legend><img src='Picture/po.gif' width='30' height='30'>Purchase Order - $poNo</legend>";
echo "<br/>";
echo "<table width='100%' border='0'>";
echo "<tr>";
echo "<td width='15%'><b>Supplier: </b></td>";
echo "<td colspan='5'>$row[supName]</td>";

echo "<td width='15%'><b>PO ID: </b></td>";
echo "<td width='25%'><input type='hidden' name='poNo' value='$poNo'>$poNo</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Address: </b></td>";
echo "<td colspan='5'>$row3[supAddress]</td>";

echo "<td><b>Date: </b></td>";
echo "<td>".$row["poDate"]."</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Contact No: </b></td>";
echo "<td colspan='5'>".$row3["supPhone"]."</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Email: </b></td>";
echo "<td colspan='5'>".$row3["supEmail"]."</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='15%'>Unit<br/>Price</td>";
echo "<th width='10%'>Quantity</td>";
echo "<th width='15%'>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='ISBN".$i."' value='$row2[poItem]'>".$row2['poItem']."</td>";
	
	$ISBN = $row2['poItem'];
	$p = $row2['poPrice'];
	$q = $row2['poQuantity'];
	$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[poPrice]'>RM ";
	printf("%10.2f",$p);
	echo "</td>";
	echo "<td><input type='hidden' name='quantity".$i."' size='10' value='$row2[poQuantity]'>".$row2['poQuantity']."</td>";
	echo "<td>RM ";
	printf("%10.2f",$subtotal);
	echo "</td>";
	echo "</tr>";
}

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

echo "<tr>";
	echo "<td colspan='6' align='center'>
	<input type='submit' name='Print' value='Print'>
	<input type='submit' name='Delete' value='Delete'>
	<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>