<?php
session_start();


include("connect.php");

$invNo = $_POST['invNo'];
$j = $_POST['counter'];
$qtotal = $_POST['qtotal'];

if($_POST["Cancel"] == "Cancel")
{
$invNo = $_POST['invNo'];

	$query=mysql_query("delete from receivedinvitem where invNo='$invNo'");
	$query2=mysql_query("delete from receivedinvoice where rinvNo='$invNo'");
	
	echo "<script>alert('You cancel the process.')</script>";
	echo "<script language='javascript'>window.location='restock.php'</script>";
}

if($_POST["Submit"] == "Submit")
{
for ($k=0; $k <$j; $k++)
{
$ISBN= $_POST['ISBN'.$k];
$quantity = $_POST['quantity'.$k];

if($qtotal<=0){
echo "<script language='javascript'>alert('Please check your quantity.')</script>";
echo "<script language='javascript'>window.location='restockitem.php?rinvNo=$invNo'</script>";
}else{
	$result = mysql_query("SELECT itemQuantity FROM item WHERE ISBN = '$ISBN'");
	
	$row = mysql_fetch_array($result);
	
	$o = $row['itemQuantity'];
	$n = $o + $quantity;

	$result2 = mysql_query("UPDATE receivedinvitem SET quantity = '$quantity' WHERE ISBN = '$ISBN'&&invNo = '$invNo'");
	
	
	$result3 = mysql_query("UPDATE item SET itemQuantity = '$n' WHERE ISBN = '$ISBN'");
	
	
	$result4 = mysql_query("UPDATE receivedinvoice SET qtotal = '$qtotal' WHERE rinvNo = '$invNo'");
	
}
}
	$db = mysql_close();
	echo "<script language='javascript'>alert('Received Bill $invNo is created')</script>";
	echo "<script language='javascript'>window.location='restock.php'</script>";
}
?>