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


$staffID=$_GET['staffID'];

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

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


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

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

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

$soNo = $_POST["soNo"];

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

$num_results = mysql_num_rows($result);

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

$result1 = mysql_query("INSERT INTO deliveryorder (soNo, custName, doAddress, doDate, doEmail, doCode, doCity, doState, doTel, doTotal, doShip, doPay) SELECT soNo, custName, soAddress, soDate, soEmail, soCode, soCity, soState, soTel, soTotal, soShip, soPay FROM salesorder WHERE soNo = '$soNo'");


$result2 = mysql_query("UPDATE deliveryorder SET doNo = '$doNo', doDate= '$doDate' WHERE soNo = '$soNo'");


$result3 = mysql_query("INSERT INTO doitem (doItem, doQuantity, doPrice, soNo) SELECT soItem, soQuantity, soPrice, soNo FROM soitem WHERE soNo = '$soNo'");


$result4 = mysql_query("UPDATE doitem SET doNo = '$doNo' WHERE soNo = '$soNo'");


echo "<script>alert('Delivery Order for $soNo is created.')</script>";
echo "<script language='javascript'>window.location='salesorder.php'</script>";
}
}
if($_POST["Edit"] == "Edit Order")
{
?>
	
<html>
<head>
<title>Bookstore - Sales 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="salesorder.php"><h4>Sales Order List</h4></a>
</center></td>
</tr>
</table>

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



$soNo=$_POST['soNo'];

$result = mysql_query("SELECT * FROM salesorder WHERE soNo = '$soNo'");

$row = mysql_fetch_array($result);

$result2 = mysql_query("SELECT * FROM soitem WHERE soNo = '$soNo'");

$num_results = $result2->num_rows;

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

echo "<fieldset style='width:70%'>";
echo "<legend>Sales Order - $soNo</legend>";
echo "<br/>";
echo "<table width='100%' border='0'>";
echo "<tr>";
echo "<td width='15%'>Order No: </td>";
echo "<td>".$row["soNo"]."<input type='hidden' name='soNo' value='".$row["soNo"]."'/></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='soAddress' value='".$row["soAddress"]."' maxlength='100' size='100'/></td>";
echo "</tr>";

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

echo "<tr>";
echo "<td>Contact No: </td>";
echo "<td><input type='text' name='soTel' value='".$row["soTel"]."' 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);
	$soQ = $row2["soQuantity"]; 
	$soP = $row2["soPrice"];
	$subtotal = $soQ * $soP;
	$total = $total + $subtotal;
	
	$soquantity = $soquantity + $soQ;
	
	echo "<tr>";
	echo "<td>".($i + 1)."</td>";
	echo "<td>".$row2["soItem"]."</td>";
	echo "<td>";
	
	$result3 = mysql_query("SELECT itemName FROM item WHERE ISBN = $row2[soItem]");
	
	$row3 = mysql_fetch_array($result3);
	echo "$row3[itemName]";
	echo "</td>";
	
	echo "<td align='center'>";
	echo "<form name='orderqform' method='post' action='editsalesorder.php'>";
	echo "<input type='hidden' name='so2Item' value='".$row2["soItem"]."'>";
	echo "<input type='hidden' name='so2No' value='".$row2["soNo"]."'/>";
	echo "<name='so2Quantity' value='".$row2["soQuantity"]."'>";
	echo "<select name='so2Quantity' onchange='this.form.submit()' value='Save' name='Save''>";
	$soQuantityArray = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
	for($j = 0; $j < 9 ; $j++)
	{if($row2["soQuantity"] == $soQuantityArray[$j])
	{echo "<option selected>".$soQuantityArray[$j];
	}else
	{echo "<option>".$soQuantityArray[$j];
	}
	}
	echo "</select>";
	echo "</form></td>";
	echo "<td align='center'>RM ";
	printf("%10.2f",$row2["soPrice"]);
	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($soquantity);
	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>";
}
?>