<?php
include ("../../include/session.php");
include ("../../include/db.php");
if(!$session->isRadstoneReceptionUser() && !$session->isAdmin()){
	header("Location: ../../storeMenu.php");
} else {

	$productId = mysql_real_escape_string($_POST['productId']);
	//		$empId = mysql_real_escape_string($_POST['empId']);
	$orderNo = mysql_real_escape_string($_POST['order']);
	$client = mysql_real_escape_string($_POST['client']);
	$quantity = mysql_real_escape_string($_POST['quantity']);
	$price = mysql_real_escape_string($_POST['price']);
	$saleId = mysql_real_escape_string($_POST['saleId']);
	$comments = mysql_real_escape_string($_POST['comments']);
	$delNote = mysql_real_escape_string($_POST['delNote']);
	$date = isset ($_REQUEST["date1"]) ? $_REQUEST["date1"] : "";
	$quantity = 0 - $quantity;
  
	$q2 = "SELECT quantity FROM products WHERE productId = '$productId'";
	$result2 = execsql($q2);
	$totalQuantity = mysql_result($result2, 0, "quantity") - $quantity;
	
	$qO = "UPDATE orders SET reserved = reserved - $quantity WHERE orderNo = '$orderNo' AND productId = '$productId'";
	execsql($qO);
	$qP = "UPDATE products SET quantity = quantity - $quantity, reserved = reserved - $quantity WHERE productId = '$productId'";
//	echo $qP;
	execsql($qP);
	
	$qSale = "UPDATE sales SET returned = '1' WHERE saleId = '$saleId'";
	$rSale = execsql($qSale);
//	echo $qSale;

	$q = "";
//	$q1 = "UPDATE products SET quantity = quantity - '$quantity', reserved = reserved + $quantity WHERE productId = '$productId'";
	$q3 = "";

	$q = "INSERT INTO sales (productId, client, orderNo, delNote, quantity, price, comments, date, value, returned) VALUES ('" . $productId . "', '$client','" . $orderNo . "', '" . $delNote . "' ,'" . $quantity . "','" . $price . "', '$comments', '" . $date . "', '$client', '1')";
	$q3 = "INSERT INTO itemhistory (productId, details, quantity, total, date) VALUES ('" . $productId . "', 'Returned (Del Note): <a href=\"../Store/displayDelNote.php?delNote=$delNote\">" . $delNote . "</a>" . "','" . $quantity . "','" . $totalQuantity . "','" . $date . "')";
	
	

	
	$return = mysql_real_escape_string($_POST['submitok']);
	
//	echo $q;

	$result = execsql($q);
//	$resultQ = execsql($q1);
	$resultH = execsql($q3);
	if ($result && $resultH) {
		if ($return == "Save and New") {
			$cat = mysql_real_escape_string($_POST['cat']);
			header("Location: ../orderByDates.php?orderNo=$orderNo");
		} else {
						header("Location: ../displayDelNote.php?delNote=$delNote");
		}
	}
}
?>
 
