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


$staffID=$_GET['staffID'];

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

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

$doNo = $_POST["doNo"];
$custName = $_POST["custName"];
$doAddress = $_POST["doAddress"];
$doEmail = $_POST["doEmail"];
$doCode = $_POST["doCode"];
$doCity = $_POST["doCity"];
$doState = $_POST["doState"];
$doTel = $_POST["doTel"];

$result7 = mysql_query("UPDATE deliveryorder SET custName = '$custName', doAddress = '$doAddress', doEmail = '$doEmail', doCode = '$doCode', doCity = '$doCity', doState = '$doState', doTel = '$doTel' WHERE doNo = '$doNo'");


echo "<script>alert('Delivery Order $doNo is edited.')</script>";
echo "<script language='javascript'>window.location='deliveryorderlist.php'</script>";
}

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


$doNo=$_POST['doNo'];
	$query=mysql_query("delete from deliveryorder where doNo='$doNo'");
	$query2=mysql_query("delete from doitem where doNo='$doNo'");
	echo "<script>alert('Sales Order $doNo is deleted.')</script>";
	echo "<script language='javascript'>window.location='deliveryorderlist.php'</script>"; 
}

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

if($_POST["Submit"] == "Invoice")
{
include("connect.php");
include("invID.php");
session_start();

$doNo = $_POST["doNo"];

$result = mysql_query("SELECT * FROM invoice WHERE doNo = '$doNo'");

$num_results = mysql_num_rows($result);

if($num_results>=1){
echo "<script>alert('An Invoice for Delivery Order $doNo is exist.')</script>";
echo "<script language='javascript'>window.location='deliveryorderlist.php'</script>";
}else{
$invNo = $index;
$invDate = date("Y-m-d");

$result1 = mysql_query("INSERT INTO invoice (doNo, soNo, custName, invAddress, invDate, invEmail, invCode, invCity, invState, invTel, invTotal, invShip, invPay) SELECT doNo, soNo, custName, doAddress, doDate, doEmail, doCode, doCity, doState, doTel, doTotal, doShip, doPay FROM deliveryorder WHERE doNo = '$doNo'");


$result2 = mysql_query("UPDATE invoice SET invNo = '$invNo', invDate= '$invDate' WHERE doNo = '$doNo'");


$result3 = mysql_query("INSERT INTO invitem (invItem, invQuantity, invPrice, doNo) SELECT doItem, doQuantity, doPrice, doNo FROM doitem WHERE doNo = '$doNo'");


$result4 = mysql_query("UPDATE invitem SET invNo = '$invNo' WHERE doNo = '$doNo'");


echo "<script>alert('Invoice for $doNo is created.')</script>";
echo "<script language='javascript'>window.location='deliveryorderlist.php'</script>";
}
}
if($_POST["Edit"] == "Edit Order")
{
include("connect.php");
$doNo = $_POST["doNo"];
?>
	
<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");
?>
</div>
</fieldset>
</td>
</tr>
</table>

</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="onlineorderlist.php"><img src="Picture/onlineorder.gif" width="30" height="30" alt="manage online order"><a href="onlineorderlist.php">Online Order</a><br/>
<br/>
<a href="salesorder.php"><img src="Picture/salesorder.gif" width="30" height="30" alt="manage sales order"><a href="salesorder.php">Sales Order</a><br/>
<br/>
<a href="deliveryorderlist.php"><img src="Picture/do.gif" width="30" height="30" alt="manage delivery order"><a href="deliveryorderlist.php">Delivery Order</a><br/>
<br/>
<a href="invlist.php"><img src="Picture/invoice.gif" width="30" height="30" alt="manage invoice"><a href="invlist.php">invoice</a>
</td>
</tr>
</table>
</fieldset>
</td>
<td width="75%">

<?php
include("connect.php");



$doNo=$_POST['doNo'];

$result = mysql_query("SELECT * FROM deliveryorder WHERE doNo = '$doNo'");

$row = mysql_fetch_array($result);

$result2 = mysql_query("SELECT * FROM doitem WHERE doNo = '$doNo'");

$num_results = $result2->num_rows;

echo "<div id='bill'>";
echo "<h3>Delivery Order View - $doNo</h3>";
echo "<form name='deliveryorderform' method='post' action='deliveryorderedit.php'>";

echo "<fieldset style='width:90%'>";
echo "<legend>Delivery Order - $doNo</legend>";
echo "<br/>";
echo "<table width='100%' border='0'>";
echo "<tr>";
echo "<td width='15%'><b>Buyer Name: </b></td>";
echo "<td colspan='5'><input type='text' name='custName' value='$row[custName]'></td>";

echo "<td width='15%'><b>Delivery Order ID: </b></td>";
echo "<td width='25%'><input type='hidden' name='doNo' value='$row[doNo]'>$row[doNo]</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Address: </b></td>";
echo "<td colspan='5'><input type='text' name='doAddress' value='$row[doAddress]'>,<br/><input type='text' name='doCode' value='$row[doCode]'>, <input type='text' name='doCity' value='$row[doCity]'>, <input type='text' name='doState' value='$row[doState]'></td>";

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

echo "<tr>";
echo "<td><b>Contact No: </b></td>";
echo "<td colspan='2'><input type='text' name='doTel' value='$row[doTel]'></td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Email: </b></td>";
echo "<td colspan='2'><input type='text' name='doEmail' value='$row[doEmail]'></td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Paying Method: </b></td>";
echo "<td colspan='6'>".$row["doPay"]."</td>";
echo "</tr>";

echo "</table>";

echo "<br/><br/>";


echo "<table border='1' cellspacing='0' cellpadding='5' bordercolor='white' width='100%'>";

echo "<div id='main2'>";
echo "<tr>";
echo "<th colspan='6'><center>Delivery Order Item List</center></th>";
echo "</tr>";

echo "<tr>";
echo "<th>No</th>";
echo "<th>ISBN</th>";
echo "<th>Item Description</th>";
echo "<th>Quantity</th>";
echo "<th>Price</th>";
echo "<th>Subtotal</th>";
echo "</tr>";

for($i=0; $i <$num_results; $i++)
{
	$row2 = mysql_fetch_array($result2);
	$doQ = $row2["doQuantity"]; 
	$doP = $row2["doPrice"];
	$subtotal = $doQ * $doP;
	$total = $total + $subtotal;
	
	$doquantity = $doquantity + $doQ;
	
	echo "<tr>";
	echo "<td>".($i + 1)."</td>";
	echo "<td>".$row2["doItem"]."</td>";
	echo "<td>";
	
	$result3 = mysql_query("SELECT itemName FROM item WHERE ISBN = $row2[doItem]");
	
	$row3 = mysql_fetch_array($result3);
	echo "$row3[itemName]";
	echo "</td>";
	echo "<td align='center'>".$row2["doQuantity"]."</td>";
	echo "<td align='center'>RM ";
	printf("%10.2f",$row2["doPrice"]);
	echo "</td>";
	echo "<td>RM";
	printf("%10.2f",$subtotal);
	echo "</td>";
	echo "</tr>";
}
echo "<tr bgcolor='gray'>";
	echo "<th colspan='3'>Total</th>";
	echo "<th>";
	echo "<center>";
	printf($doquantity);
	echo "</center>";
	echo "</th>";
	echo "<td></td>";
	echo "<th>RM";
	printf("%10.2f",$total);
	echo "</th>";
	echo "</tr>";

echo "<tr bgcolor='gray'>";
echo "<th colspan='3'>Shipping Method</th>";
echo "<th>";
echo "<center>";
echo $row["doShip"];
echo "</center>";
echo "</th>";

$shipType =  $row["doShip"];

$result3 = mysql_query("SELECT * FROM ship WHERE shipType = '$shipType'");


$row3 = mysql_fetch_array($result3);

echo "<td>";
echo "</td>";
echo "<th>RM ";
printf("%10.2f",$row3["shipCost"]);
echo "</th>";
echo "</tr>";

echo "<tr bgcolor='gray'>";
echo "<th colspan='3'>Grandtotal</th>";
echo "<th>";
echo "<center>";
printf($doquantity);
echo "</center>";
echo "</th>";
echo "<td>";
echo "</td>";
echo "<th>RM ";
$ship = $row3["shipCost"];
$grandtotal = $total + $ship;
	
printf("%10.2f",$grandtotal);
echo "</th>";
echo "</tr>";
	
echo "<tr>";
	echo "<input type='hidden' name='doNo' value='".$row["doNo"]."'/>";
	echo "<td colspan='6' align='center'>	
	<input type='submit' name='Submit' value='Submit'>
	<input type='submit' name='Back' value='Back'>";
	echo "</tr>";
echo "</table>";
echo "</fieldset>";
echo "</form>";
?>

</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>