<html>
<head>
<title>EZ Shopper - Managing customer shipping orders</title></head>
<body bgcolor = "#FFFFFF">
<?

 /***************************************************************************
 *			chg_ship.php	                                    *
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~	                    *
 *   File Info:		  Change/Edit Shipping                              *
 *   Copyright:       Copyright 2003 http://www.128concepts.com             *
 *						                            *
 ***************************************************************************/

include("./config.php");
include("./tests.php");
include("./your-headers.inc");
echo "<br>";
if ($_SESSION['admin'] == NULL)
{
echo("<center><b>You have not log in. If you are an existing member please click the <a href =./shop_viewpages.php>Login</a> link!</b><br>If you are a new user click the <a href=./newpass.php>Register Me</a> link.</center><br><hr>");
exit();
}
$orderid = getenv("QUERY_STRING");
if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
        $orderid= addslashes($_POST["orderid"]);
        $status = addslashes($_POST["status"]);
        $act = addslashes($_POST["act"]);
        $shipinfo = addslashes($_POST["shipinfo"]);
        $datetime = addslashes($_POST["datetime"]);
        $shiptype = addslashes($_POST["shiptype"]);
        $shiploct = addslashes($_POST["shiploct"]);
        $orderstat = addslashes($_POST["orderstat"]);
}
$table = "shipping";
$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");
$rs = @mysql_select_db($database,$conn) 
	or die("Unable to select the database...Please press 'Create Database' to create new database.");

$sql = "SELECT * from $table where orderid = '$orderid'";
$rst = @mysql_query($sql,$conn);
$number = @mysql_num_rows($rst);

$done = "FALSE";

### edit a shipping order ###
if ($act == "edit" && $shiploct == NULL)
{
	echo "<br>Opps...Shipping location cannot be null empty field...please go back and reinput details...";
echo "<br><br><img src=images/line.gif border=0 width=100% height=1><center><b>Click Previous Button to go back to previous page.<b><a href=javascript:history.back()><img src=images/bullet.gif border=0></a></center><img src=images/line.gif border=0 width=100% height=1><br><br>";
include_once("./footer.inc");
exit();
}
if ($act == "edit" && $shiploct != NULL)
{
echo "<center><img src=images/online-order.gif border=0><font face=verdana size=3 color=darkred> Update shipping order for order '$orderid'</font></center><img src=images/line.gif border=0 width=100% height=1>";
	$table = "shipping";
	$date_added = date("H:i dS F");
	echo "<table cellborder = 1 cellpadding = 2>
	         <tr><td><b>Order ID: </b></td><td>$orderid</td></tr>
	         <tr><td><b>Status: </b></td><td>$status</td></tr>
	        <tr><td><b>Shipping Info: </b></td><td>$shipinfo</td></tr>
                      <tr><td><b>Date-Time: </b></td><td>$date_added</td></tr>
	        <tr><td><b>Shipping Type: </b></td><td>$shiptype</td></tr>
	       <tr><td><b>Location: </b></td><td>$shiploct</td></tr>
                     <tr><td><b>ORDER STATUS: </b></td><td>$orderstat</td></tr></table><br>";
	$sql1 = "UPDATE $table set status = '$status' where orderid = '$orderid'";
	$upd1 = mysql_query($sql1,$conn)
		or die("SQL command failed - unable to update status for shipping order...");
	echo "<br><li><b>Updating status for shipping order '$orderid'....";
	$sql2 = "UPDATE $table set shipinfo = '$shipinfo' where orderid = '$orderid'";
	$upd2 = mysql_query($sql2,$conn)
		or die("SQL command failed - unable to update shipping info. for shipping order...");
	echo "<br><li><b>Updating shipping info. for shipping order '$orderid'....";
	$sql3 = "UPDATE $table set datetime = '$date_added' where orderid = '$orderid'";
	$upd3 = mysql_query($sql3,$conn)
		or die("SQL command failed - unable to update date-time for shipping order...");
	echo "<br><li><b>Updating date-time for shipping order '$orderid'....";
	$sql4 = "UPDATE $table set shiptype = '$shiptype' where orderid = '$orderid'";
	$upd4 = mysql_query($sql4,$conn)
		or die("SQL command failed - unable to update shipping type for shipping order...");
	echo "<br><li><b>Updating shipping type for shipping order '$orderid'....";
	$sql5 = "UPDATE $table set shiploct = '$shiploct' where orderid = '$orderid'";
	$upd5 = mysql_query($sql5,$conn)
		or die("SQL command failed - unable to update location for shipping order...");
	echo "<br><li><b>Updating location for shipping order '$orderid'....";
	$sql6 = "UPDATE $table set orderstat = '$orderstat' where orderid = '$orderid'";
	$upd6 = mysql_query($sql6,$conn)
		or die("SQL command failed - unable to update order status for shipping order...");
	echo "<br><li><b>Updating order status for shipping order '$orderid'....";
	echo "<br><br><center><font face = Verdana color = $highlight>Shipping Order for Order '$orderid' successfully updated....</font></center>";
	
	################ return orders #########################
	if ($act == "edit" and $status == "Dispatch Order" and $orderstat == "Terminated")
	{
		$table = "custdet";
		$sql = " SELECT * from $table where orderid = '$orderid'";

		$rs = mysql_query($sql,$conn)
			or die("Could not execute SQL query!!! Possibly SQL query already been executed.");
		
		$item = array();
		$qty = array();
		$stock = array();
		$count = 0;
		while ($row = mysql_fetch_array($rs))
		{
			$item[$count] = $row["product"];
			$qty[$count] = $row["qty"];
			$count += 1;
		}	
		$table = "stocks";
		for ($i = 0; $i < count($item); $i++)
		{
			$getsql = "SELECT * from $table where sitem = '$item[$i]'";
			$getrest = @mysql_query($getsql,$conn)
				or die("SQL command failed - unable to get product and quantity from stocks table...");
			$number = mysql_num_rows($getrest);
			while ($rows = mysql_fetch_array($getrest))
			{
				$stocks = $rows["stock"];
			}
			if ($number > 0)
			{
				$newstock = $stocks + $qty[$i];
				$updsql = "UPDATE $table set stock = $newstock where sitem = '$item[$i]'";
				$updates = @mysql_query($updsql,$conn)
					or die("SQL command failed - unable to update stocks for product item...");
				
			}
		}
		echo "<br><center><font face = Verdana color = $color>Stocks for Order '$orderid' successfully updated....i.e order terminated...<br>N.B The shipping order record will be deleted from the sql database.</font></center>";
		
		# remove the shipping order from the database if terminated
		$table = "shipping";
		$ss = "DELETE from $table where orderid = '$orderid'";
		$kill = mysql_query($ss,$conn)
			or die("Unable to remove terminated shipping record from database....");
		
	}
	echo("<br><center><a href = javascript:window.close('acePopup')>Close Window</a></center>");
	exit();
}

### create a shipping order ###

if ($act == "create" and $shiploct == NULL)
{
echo "<br>Opps...Shipping location cannot be null empty field...please go back and reinput details...";
echo "<br><br><img src=images/line.gif border=0 width=100% height=1><center><b>Click Previous Button to go back to previous page.<b><a href=javascript:history.back()><img src=images/bullet.gif border=0></a></center><img src=images/line.gif border=0 width=100% height=1><br><br>";
include_once("./footer.inc");
exit();	
}
if ($act == "create")
{
echo "<center><img src=images/online-order.gif border=0><font face=verdana size=3 color=darkred> Update shipping order for order '$orderid'</font></center><img src=images/line.gif border=0 width=100% height=1>";
	$table = "shipping";
	$date_added = date("H:i dS F");
	echo "<table cellborder = 1 cellpadding = 2>
	         <tr><td><b>Order ID: </b></td><td>$orderid</td></tr>
	         <tr><td><b>Status: </b></td><td>$status</td></tr>
	        <tr><td><b>Shipping Info: </b></td><td>$shipinfo</td></tr>
                      <tr><td><b>Date-Time: </b></td><td>$date_added</td></tr>
	        <tr><td><b>Shipping Type: </b></td><td>$shiptype</td></tr>
	       <tr><td><b>Location: </b></td><td>$shiploct</td></tr>
                     <tr><td><b>ORDER STATUS: </b></td><td>$orderstat</td></tr></table>";
             $sql = "INSERT INTO $table (orderid,status,shipinfo,datetime,shiptype,shiploct,orderstat) values('$orderid','$status','$shipinfo','$date_added','$shiptype','$shiploct','$orderstat')";
             $result = @mysql_query($sql,$conn)
		or die("SQL command failed - unable to create shipping order.....");
	echo "<br><center><font face = Verdana color = $highlight>Shipping Order for Order '$orderid' successfully created....</font></center>";
	### update stocks ###

		$table = "custdet";
		$sql = " SELECT * from $table where orderid = '$orderid'";

		$rs = mysql_query($sql,$conn)
			or die("Could not execute SQL query!!! Possibly SQL query already been executed.");
		
		$item = array();
		$qty = array();
		$stock = array();
		$count = 0;
		while ($row = mysql_fetch_array($rs))
		{
			$item[$count] = $row["product"];
			$qty[$count] = $row["qty"];
			$count += 1;
		}	
		$table = "stocks";
		for ($i = 0; $i < count($item); $i++)
		{
			$getsql = "SELECT * from $table where sitem = '$item[$i]'";
			$getrest = @mysql_query($getsql,$conn)
				or die("SQL command failed - unable to get product and quantity from stocks table...");
			$number = mysql_num_rows($getrest);
			while ($rows = mysql_fetch_array($getrest))
			{
				$stocks = $rows["stock"];
			}
			if ($number > 0)
			{
				$newstock = $stocks - $qty[$i];
				$updsql = "UPDATE $table set stock = $newstock where sitem = '$item[$i]'";
				$updates = @mysql_query($updsql,$conn)
					or die("SQL command failed - unable to update stocks for product item...");
				
			}
		}
		echo "<br><center><font face = Verdana color = $color>Stocks for Order '$orderid' successfully updated....</font></center>";
		echo("<br><center><a href = javascript:window.close('acePopup')>Close Window</a></center>");
		exit();


}
# permit user to add shipping order #

if ($number == 0)
{

echo "<center><img src=images/online-order.gif border=0><font face=verdana size=3 color=darkred> Update shipping order for order '$orderid'</font></center><img src=images/line.gif border=0 width=100% height=1>";

$form = "<FORM action=chg_ship.php method=POST>";
$form.= "<table cellspacing=5 cellpadding=5><tr>";
$form.="<td valign=top><strong>Order ID:</strong></td><td valign=top>";
$form.="<INPUT type=text name=orderid value = $orderid readonly size=16 maxlength=16></td></tr>";
$form.="<tr><td valign=top><strong>Status:</strong></td><td valign=top>";
$form.="<select name = status><option value = 'Await Shipping'>-- Await Shipping -- </option>";
$form.="<option value = 'Dispatch Order'>-- Dispatch Order --</option>";
$form.="<option value = 'Order Delayed'>-- Order Delayed --</option></select></td></tr>";
$form.="<tr><td valign=top><strong>Shipping Info:</strong></td><td valign=top>";
$form.="<select name = shipinfo><option value = '24 Hours'>-- 24 Hours -- </option>";
$form.="<option value = '48 Hours'>-- 48 Hours --</option>";
$form.="<option value = '72 Hours'>-- 72 Hours --</option>";
$form.="<option value = '3-5 Days'>-- 3-5 Days --</option>";
$form.="<option value = '1 week'>-- 1 week --</option>";
$form.="<option value = '2 weeks'>-- 2 weeks --</option></select></td></tr>";
$form.="<tr><td valign=top><strong>Ship By:</strong></td><td valign=top>";
$form.="<select name = shiptype><option value = 'Post Service'>-- Post Service --</option>";
$form.="<option value = 'Courier'>-- Courier --</option>";
$form.="<option value = 'International Post'>-- International Post --</option>";
$form.="<option value = 'International Courier'>-- International Courier --</option></select></td></tr>";
$form.="<tr><td valign=top><strong>Location:</strong></td><td valign=top>";
$form.="<INPUT type=text name=shiploct size=50 maxlength=50></td></tr>";
$form.="<tr><td valign=top><strong>ORDER STATUS:</strong></td><td valign=top>";
$form.="<select name = orderstat><option value = 'Completed'>-- Completed -- </option>";
$form.="<option value = 'Terminated'>-- Terminated --</option><option value = 'In Progress'>-- In Progress --</option></select></td></tr>";
$form.="<input type = hidden name = act value = 'create'><input type =submit value = 'Create Shipping Order'><input type = reset value = 'Clear Contents'>";
$form.="</form></table>";

echo $form;
echo "<br><center><b>Please input all data into all fields before pressing 'Create Shipping Order'</b><br>Location refers to shipping destination i.e country and city e.g UK, London<br>NOTE: Stocks will deducted from the database once shipping order record is created....</center><img src=images/line.gif border=0 width=100% height=1>";
}

########## permit user to edit existing shipping order ############

if ($number > 0)
{
$datsql = "SELECT * from $table where orderid = '$orderid'";
$drst = @mysql_query($datsql,$conn);
while ($nrow = mysql_fetch_array($drst))
{
	$status = $nrow["status"];
	$shipinfo = $nrow["shipinfo"];
	$date_added = $nrow["date_added"];
	$shiploct = $nrow["shiploct"];
	$shiptype = $nrow["shiptype"];
	$orderstat = $nrow["orderstat"];
}

echo "<center><img src=images/online-order.gif border=0><font face=verdana size=3 color=darkred> Create/Update shipping order for order '$orderid'</font></center><img src=images/line.gif border=0 width=100% height=1>";
$form = "<FORM action=chg_ship.php method=POST>";
$form.= "<table cellspacing=5 cellpadding=5><tr>";
$form.="<td valign=top><strong>Order ID:</strong></td><td valign=top>";
$form.="<INPUT type=text name=orderid value = $orderid readonly size=16 maxlength=16></td></tr>";
$form.="<tr><td valign=top><strong>Status:</strong></td><td valign=top>";
$form.="<select name = status>";
if ($status == "Await Shipping")
{
	$form.="<option value = 'Await Shipping' selected>-- Await Shipping --</option>";
	$form.="<option value = 'Dispatch Order'>-- Dispatch Order --</option>";
	$form.="<option value = 'Order Delayed'>-- Order Delayed --</option>";
}
if ($status == "Dispatch Order")
{
	$form.="<option value = 'Await Shipping'>-- Await Shipping --</option>";
	$form.="<option value = 'Dispatch Order' selected>-- Dispatch Order --</option>";
	$form.="<option value = 'Order Delayed'>-- Order Delayed --</option>";
}
if ($status == "Order Delayed")
{
	$form.="<option value = 'Await Shipping'>-- Await Shipping --</option>";
	$form.="<option value = 'Dispatch Order'>-- Dispatch Order --</option>";
	$form.="<option value = 'Order Delayed' selected>-- Order Delayed --</option>";
}
$form.="</select></td></tr>";
$form.="<tr><td valign=top><strong>Shipping Info:</strong></td><td valign=top>";
$form.="<select name = shipinfo>";
if ($shipinfo == "24 Hours")	
{
	$form.="<option value = '24 Hours' selected>-- 24 Hours --</option>";
	$form.="<option value = '48 Hours'>-- 48 Hours --</option>";
	$form.="<option value = '72 Hours'>-- 72 Hours --</option>";
	$form.="<option value = '3-5 Days'>-- 3-5 Days --</option>";
	$form.="<option value = '1 week'>-- 1 week --</option>";
	$form.="<option value = '2 weeks'>-- 2 weeks --</option>";
}
if ($shipinfo == "48 Hours")	
{
	$form.="<option value = '24 Hours'>-- 24 Hours --</option>";
	$form.="<option value = '48 Hours' selected>-- 48 Hours --</option>";
	$form.="<option value = '72 Hours'>-- 72 Hours --</option>";
	$form.="<option value = '3-5 Days'>-- 3-5 Days --</option>";
	$form.="<option value = '1 week'>-- 1 week --</option>";
	$form.="<option value = '2 weeks'>-- 2 weeks --</option>";
}
if ($shipinfo == "72 Hours")	
{
	$form.="<option value = '24 Hours'>-- 24 Hours --</option>";
	$form.="<option value = '48 Hours'>-- 48 Hours --</option>";
	$form.="<option value = '72 Hours' selected>-- 72 Hours --</option>";
	$form.="<option value = '3-5 Days'>-- 3-5 Days --</option>";
	$form.="<option value = '1 week'>-- 1 week --</option>";
	$form.="<option value = '2 weeks'>-- 2 weeks --</option>";
}
if ($shipinfo == "3-5 Days")	
{
	$form.="<option value = '24 Hours'>-- 24 Hours --</option>";
	$form.="<option value = '48 Hours'>-- 48 Hours --</option>";
	$form.="<option value = '72 Hours'>-- 72 Hours --</option>";
	$form.="<option value = '3-5 Days' selected>-- 3-5 Days --</option>";
	$form.="<option value = '1 week'>-- 1 week --</option>";
	$form.="<option value = '2 weeks'>-- 2 weeks --</option>";
}
if ($shipinfo == "1 week")	
{
	$form.="<option value = '24 Hours'>-- 24 Hours --</option>";
	$form.="<option value = '48 Hours'>-- 48 Hours --</option>";
	$form.="<option value = '72 Hours'>-- 72 Hours --</option>";
	$form.="<option value = '3-5 Days'>-- 3-5 Days --</option>";
	$form.="<option value = '1 week' selected>-- 1 week --</option>";
	$form.="<option value = '2 weeks'>-- 2 weeks --</option>";
}
if ($shipinfo == "2 weeks")	
{
	$form.="<option value = '24 Hours'>-- 24 Hours --</option>";
	$form.="<option value = '48 Hours'>-- 48 Hours --</option>";
	$form.="<option value = '72 Hours'>-- 72 Hours --</option>";
	$form.="<option value = '3-5 Days'>-- 3-5 Days --</option>";
	$form.="<option value = '1 week'>-- 1 week --</option>";
	$form.="<option value = '2 weeks' selected>-- 2 weeks --</option>";
}
$form.="</select></td></tr>";
$form.="<tr><td valign=top><strong>Ship By:</strong></td><td valign=top>";
$form.="<select name = shiptype>";
if ($shiptype == "Post Service")
{
	$form.="<option value = 'Post Service' selected>-- Post Service --</option>";
	$form.="<option value = 'Courier'>-- Courier --</option>";
	$form.="<option value = 'International Post'>-- International Post --</option>";
	$form.="<option value = 'International Courier'>-- International Courier --</option>";
}
if ($shiptype == "Courier")
{
	$form.="<option value = 'Post Service'>-- Post Service --</option>";
	$form.="<option value = 'Courier' selected>-- Courier --</option>";
	$form.="<option value = 'International Post'>-- International Post --</option>";
	$form.="<option value = 'International Courier'>-- International Courier --</option>";
}
if ($shiptype == "International Post")
{
	$form.="<option value = 'Post Service'>-- Post Service --</option>";
	$form.="<option value = 'Courier'>-- Courier --</option>";
	$form.="<option value = 'International Post' selected>-- International Post --</option>";
	$form.="<option value = 'International Courier'>-- International Courier --</option>";
}
if ($shiptype == "International Courier")
{
	$form.="<option value = 'Post Service'>-- Post Service --</option>";
	$form.="<option value = 'Courier'>-- Courier --</option>";
	$form.="<option value = 'International Post'>-- International Post --</option>";
	$form.="<option value = 'International Courier' selected>-- International Courier --</option>";
}
$form.="<select></td></tr>";
$form.="<tr><td valign=top><strong>Location:</strong></td><td valign=top>";
$form.="<INPUT type=text name=shiploct value = '$shiploct' size=50 maxlength=50></td></tr>";
$form.="<tr><td valign=top><strong>ORDER STATUS:</strong></td><td valign=top>";
$form.="<select name = orderstat>";
if ($orderstat == "Completed")
{
	$form.="<option value = 'Completed' selected>-- Completed --</option>";
	$form.="<option value = 'Terminated'>-- Terminated --</option>";
	$form.="<option value = 'In Progress'>-- In Progress --</option>";
}
if ($orderstat == "Terminated")
{
	$form.="<option value = 'Completed'>-- Completed --</option>";
	$form.="<option value = 'Terminated' selected>-- Terminated --</option>";
	$form.="<option value = 'In Progress'>-- In Progress --</option>";
}
if ($orderstat == "In Progress")
{
	$form.="<option value = 'Completed'>-- Completed --</option>";
	$form.="<option value = 'Terminated'>-- Terminated --</option>";
	$form.="<option value = 'In Progress' selected>-- In Progress --</option>";
}
$form.="</select></td></tr>";
$form.="<input type = hidden name = act value = 'edit'><input type =submit value = 'Update Shipping Order'><input type = reset value = 'Clear Contents'>";
$form.="</form></table>";
echo $form;
echo "<br><center><b>Please input all data into all fields before pressing 'Create Shipping Order'</b><br>Location refers to shipping destination i.e country and city e.g UK, London</center>";
echo "<br><center><b>Selecting Order Status = 'Terminated' will terminate an order. When an order is</b><br><b>terminated, stocks will be added back.</b></center>";

}
echo("<br><center><a href = javascript:window.close('acePopup')>Close Window</a></center>");


?>
  
</body>
</html>