<?php
session_start();


include("connect.php");

$poNo = $_POST['poNo'];
$j = $_POST['counter'];
$qtotal = $_POST['qtotal'];
$total = $_POST['total'];

if($_POST["Submit"] == "Submit")
{
	$result3 = mysql_query("UPDATE purchaseorder SET poTotal = '$total', qTotal = '$qtotal' WHERE poNo = '$poNo'");
	

for ($k=0; $k <$j; $k++)
{
$description= $_POST['description'.$k];
$quantity = $_POST['quantity'.$k];
$price = $_POST['price'.$k];

	$result = mysql_query("SELECT ISBN FROM item WHERE itemName = '$description'");
	
	$row = mysql_fetch_array($result);
$ISBN = $row["ISBN"];
if($ISBN!=""){
$result2 = mysql_query("INSERT INTO poitem(poItem, poQuantity, poPrice, poNo)
	VALUES
	('$ISBN', '$quantity', '$price', '$poNo')");

//if(($quantity<1)||($price<0)){
//echo "<script language='javascript'>alert('Please check your amount and unit price.')</script>";
//echo "<script language='javascript'>window.location='addstock.php?po=$poNo'</script>";
//}else{
	//$result2 = mysql_query("UPDATE poitem SET poQuantity = '$quantity', poPrice = '$price' WHERE poItem = '$ISBN'&&poNo = '$poNo'");
	//
//}
//}
}
}
if($total<=0){
	
	
	$poNo = $_POST['poNo'];
	$query=mysql_query("delete from purchaseorder where poNo='$poNo'");
	echo "<script>alert('Please check your PO information.')</script>";
	echo "<script language='javascript'>window.location='addpo.php'</script>"; 
	$db = mysql_close();
}
	$db = mysql_close();
	echo "<script language='javascript'>alert('Purchase order $poNo is created')</script>";
	echo "<script language='javascript'>window.location='addpo.php'</script>";

}

if($_POST["Cancel"] == "Cancel")
{
	
	
	$poNo = $_POST['poNo'];
	$query=mysql_query("delete from purchaseorder where poNo='$poNo'");
	echo "<script>alert('You determine the process.')</script>";
	echo "<script language='javascript'>window.location='addpo.php'</script>"; 
}
?>