<?php
session_start();
$username = $_SESSION['staff'];
$login=mysql_connect("localhost","root","");
mysql_select_db("bookstore",$login);
$staffID=$_GET['staffID'];

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

if($_POST["Delete"] == "Delete")
{
include("connect.php");
$login=mysql_connect("localhost","root","");
mysql_select_db("bookstore",$login);
$cartNo=$_GET['cartNo'];
	$query=mysql_query("delete from cart where cartNo='$cartNo'");
	$query2=mysql_query("delete from cartitem where cartNo='$cartNo'");
	echo "<script>alert('Sales Order $cartNo is deleted.')</script>";
	echo "<script language='javascript'>window.location='onlineorderlist.php'</script>"; 
}
	
if($_POST["Submit"] == "Sales Order")
{
include("connect.php");
include("salesorderID.php");
session_start();

$cartNo = $_POST["cartNo"];
$soNo = $index;
$soDate = date("Y-m-d");

$query1= "INSERT INTO salesorder (custName, soAddress, soTel, soEmail, soCode, soCity, soState, soTotal, soShip, soPay, cartNo) SELECT cartName, cartAddress, cartTel, cartEmail, cartCode, cartCity, cartState, cartTotal, cartShip, cartPay, cartNo FROM cart WHERE cartNo = '$cartNo'";
$result1 = $db->query($query1);

$query2 = "UPDATE salesorder SET soNo = '$soNo', soDate= '$soDate' WHERE cartNo = '$cartNo'";
$result2 = $db->query($query2);

$query3= "INSERT INTO soitem (soItem, soQuantity, soPrice, cartNo) SELECT cartItem, cartQuantity, cartPrice, cartNo FROM cartitem WHERE cartNo = '$cartNo'";
$result3 = $db->query($query3);

$query4 = "UPDATE soitem SET soNo = '$soNo' WHERE cartNo = '$cartNo'";
$result4 = $db->query($query4);

$query5=mysql_query("DELETE from cart WHERE cartNo='$cartNo'");
$query6=mysql_query("DELETE from cartitem WHERE cartNo='$cartNo'");

echo "<script>alert('Sales order for $cartNo is created.')</script>";
echo "<script language='javascript'>window.location='onlineorderlist.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"/>

<!--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 - Management 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 ("stafffunction.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="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="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="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="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");

$login=mysql_connect("localhost","root","");
mysql_select_db("bookstore",$login);
$cartNo=$_GET['cartNo'];

$query = "SELECT * FROM cart WHERE cartNo = '$cartNo'";
$result = $db->query($query);
$row = $result->fetch_array();

$query2 = "SELECT * FROM cartitem WHERE cartNo = '$cartNo'";
$result2 = $db->query($query2);
$num_results = $result2->num_rows;
echo "<div id='bill'>";
echo "<form name='orderviewform' method='post'>";

echo "<fieldset style='width:90%'>";
echo "<legend><img src='Picture/onlineorder.gif' width='30' height='30'>Online Order - $cartNo</legend>";
echo "<br/>";
echo "<table width='100%' border='0'>";
echo "<tr>";
echo "<td width='15%'><b>Buyer Name: </b></td>";
echo "<td colspan='5'>".$row["cartName"]."</td>";

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

echo "<tr>";
echo "<td><b>Address: </b></td>";
echo "<td colspan='5'>$row[cartAddress],<br/>$row[cartCode], $row[cartCity], $row[cartState]</td>";

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

echo "<tr>";
echo "<td><b>Contact No: </b></td>";
echo "<td colspan='2'>".$row["cartTel"]."</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Email: </b></td>";
echo "<td colspan='2'>".$row["cartEmail"]."</td>";
echo "</tr>";

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

echo "</table>";

echo "<br/>";

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

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

echo "<div id='main2'>";
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 = $result2->fetch_array();
	$cartQ = $row2["cartQuantity"]; 
	$cartP = $row2["cartPrice"];
	$subtotal = $cartQ * $cartP;
	$total = $total + $subtotal;
	
	$cartquantity = $cartquantity + $cartQ;
	
	echo "<tr>";
	echo "<td>".($i + 1)."</td>";
	echo "<td>".$row2["cartItem"]."</td>";
	echo "<td>";
	
	$query3 = "SELECT itemName FROM item WHERE ISBN = $row2[cartItem]";
	$result3 = $db->query($query3);
	$row3 = $result3->fetch_array();
	echo "$row3[itemName]";
	echo "</td>";
	echo "<td align='center'>".$row2["cartQuantity"]."</td>";
	echo "<td>RM ";
	printf("%10.2f",$row2["cartPrice"]);
	echo "</td>";
	echo "<td>RM";
	printf("%10.2f",$subtotal);
	echo "</td>";
	echo "</tr>";
}
echo "<tr>";
	echo "<th colspan='3'>Total</th>";
	echo "<th>";
	echo "<center>";
	printf($cartquantity);
	echo "</center>";
	echo "</th>";
	echo "<th></th>";
	echo "<th>RM";
	printf("%10.2f",$total);
	echo "</th>";
	echo "</tr>";

echo "<tr>";
echo "<th colspan='3'>Shipping Method</th>";
echo "<th>";
echo "<input type='hidden' value='shipType'>";
echo "<center>";
echo $row["cartShip"];
echo "</center>";
echo "</th>";

$shipType =  $row["cartShip"];

$query3 = "SELECT * FROM ship WHERE shipType = '$shipType'";
$result3 = $db->query($query3);

$row3 = $result3->fetch_array();

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

echo "<tr bgcolor='gray' style='color:black'>";
echo "<th colspan='3'>Grandtotal</th>";
echo "<td align='center'>";
echo "<font color='white'>";
printf($cartquantity);
echo "</font>";
echo "</td>";
echo "<th>";
echo "</td>";
echo "<th>RM ";
$ship = $row3["shipCost"];
$grandtotal = $total + $ship;
	
printf("%10.2f",$grandtotal);
echo "</th>";
echo "</tr>";

echo "<tr>";
	echo "<td colspan='6' align='center'>	
	<input type='submit' name='Submit' value='Sales Order'>
	<input type='submit' name='Delete' value='Delete'>
	<input type='submit' name='Back' value='Back'>";
	echo "</tr>";
echo "</table>";
echo "</fieldset>";
echo "</form>";
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>