<?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["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");

$query1= "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'");


$query3= "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")
{
?>
	
<html>
<head>
<title>Bookstore - Delivery Order Edit</title>
</head>
<body style="background:lightyellow">

<table width="100%">
<tr>
<td rowspan="2" align="center" valign="center" alt="天天书局" title="天天书局" ><img src="Logo.gif" width="110" height="110"></td>

<td width="25%"></td>
<td width="25%"></td>

<td>
<form method=GET action="http://www.google.com/search">
<input type=hidden name=ie value=UTF-8 >
<input type=hidden name=oe value=UTF-8 target="_blank">
<table bgcolor="#FFFFFF" target="_blank">
<tr>
<td>
<a href="http://www.google.com">
<img src="GoogleLogo.jpg" border="0" alt="Google" align="absmiddle" title="Google" >
</a>
<input type=text name=q size=25 maxlength=255 value="">
<input type=submit name=btnG value="Google Search">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>

<?php
session_start();
$username = $_SESSION['staff'];
if($_SESSION['staff']!= null)
{
echo "<span style='color:black;font-weight:bold'>".$username."</span>";
echo "<a href='logout.php'>(Logout)</a>";
}else{
echo "<span style='color:black;font-weight:bold'>You haven't login.</span>";
echo "<a href='index.php'>(Login)</a>";
}
?>

<hr/>

<table align="right">
<tr>
<td><center>
<a href="index.php"><h4>Home Page</h4></a>
<a href="addbook.php"><h4>Add Book</h4></a>
<a href="addstaff.php"><h4>Add Staff</h4></a>
<a href="stafflist.php"><h4>Staff List</h4></a>
<a href="onlineorderlist.php"><h4>Online Order List</h4></a>
<a href="saledorder.php"><h4>Sales Order List</h4></a>
</center></td>
</tr>
</table>

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



$doNo=$_POST['doNo'];

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

$row = mysql_fetch_array($result);

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

$num_results = $result2->num_rows;

echo "<form name='saledorderform' method='post'>";

echo "<fieldset style='width:70%'>";
echo "<legend>Sales Order - $doNo</legend>";
echo "<br/>";
echo "<table width='100%' border='0'>";
echo "<tr>";
echo "<td width='15%'>Order No: </td>";
echo "<td>".$row["doNo"]."<input type='hidden' name='doNo' value='".$row["doNo"]."'/></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Buyer Name: </td>";
echo "<td><input type='text' name='cartName' value='".$row["custName"]."' maxlength='50' size='100'/></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Address: </td>";
echo "<td><input type='text' name='doAddress' value='".$row["doAddress"]."' maxlength='100' size='100'/></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Submit Date: </td>";
echo "<td><input type='text' name='doDate' value='".$row["doDate"]."' maxlength='15' size='15'/></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Contact No: </td>";
echo "<td><input type='text' name='doTel' value='".$row["doTel"]."' maxlength='15' size='15'/></td>";
echo "</tr>";
echo "</table>";

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

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

echo "<tr>";
echo "<td colspan='6' bgcolor='gray'>Order Item List</td>";
echo "</tr>";

echo "<tr bgcolor='gray' style='color:black'>";
echo "<td>No</td>";
echo "<td>ISBN</td>";
echo "<td>Item Description</td>";
echo "<td>Quantity</td>";
echo "<td>Price</td>";
echo "<td>Subtotal</td>";
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'>";
	echo "<form name='orderqform' method='post' action='editsaledorder.php'>";
	echo "<input type='hidden' name='do2Item' value='".$row2["doItem"]."'>";
	echo "<input type='hidden' name='do2No' value='".$row2["doNo"]."'/>";
	echo "<name='do2Quantity' value='".$row2["doQuantity"]."'>";
	echo "<select name='do2Quantity' onchange='this.form.submit()' value='Save' name='Save''>";
	$doQuantityArray = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
	for($j = 0; $j < 9 ; $j++)
	{if($row2["doQuantity"] == $doQuantityArray[$j])
	{echo "<option selected>".$doQuantityArray[$j];
	}else
	{echo "<option>".$doQuantityArray[$j];
	}
	}
	echo "</select>";
	echo "</form></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' style='color:black'>";
	echo "<td colspan='3'>Total</td>";
	echo "<td align='center'>";
	printf($doquantity);
	echo "</td>";
	echo "<td></td>";
	echo "<td>RM";
	printf("%10.2f",$total);
	echo "</td>";
	echo "</tr>";

echo "<tr>";
	echo "<td colspan='6' align='center'>	
	<input type='submit' name='Submit' value='Invoice'>
	<input type='submit' name='Edit' value='Edit Order'>
	<input type='submit' name='Delete' value='Delete'>
	<input type='submit' name='Back' value='Back'>";
	echo "</tr>";
echo "</table>";
echo "</fieldset>";
echo "</form>";
?>

</body>
</html>
<?php
}
}else{
	echo "<script language='javascript'>alert ('Please login.')</script>";
	echo "<script language='javascript'>window.location='index.php'</script>";
}
?>