<?php
session_start();
$user = $_SESSION['staff'];
include("connect.php");
$billNo = $_POST['billNo'];

$j = $_POST['counter'];

if($_POST["Submit"] == "Submit")
{
if($billNo!=""){

$fpo= $_POST['poNo0'];

	$result = mysql_query("SELECT * FROM purchaseorder WHERE poNo = '$fpo'");
	
	
	$row = mysql_fetch_array($result);

$supID = $row["supID"];
$supName = $row["supName"];
$poDate = $row["poDate"];
$qtotal = $row["qTotal"];

	$result = mysql_query("INSERT INTO receivedinvoice(rinvNo, poNo0, supID, supName, rinvDate, qtotal, staffID)VALUES('$billNo', '$fpo', '$supID', '$supName', '$poDate', '$qtotal', '$user')");
	
	
$z = 0;	
for($i=0; $i<$j; $i++){
$poNo= $_POST['poNo'.$i];

	$result4 = mysql_query("SELECT qTotal FROM purchaseorder WHERE poNo = '$poNo'");
	
	$row4 = mysql_fetch_array($result4);
	$q = $row4["qTotal"];

	$z = $z + $q;

	$result = mysql_query("UPDATE receivedinvoice SET poNo".$i." = '$poNo', qtotal = '$z' WHERE rinvNo = '$billNo'");
	

	$result2 = mysql_query("INSERT INTO receivedinvitem (ISBN, price, poNo) SELECT poItem, poPrice, poNo FROM poitem WHERE poNo = '$poNo'");
	
	
	$result3 = mysql_query("UPDATE receivedinvitem SET invNo = '$billNo' WHERE invNo = ''");
	
}
	$db = mysql_close();
	//echo "<script language='javascript'>alert('Bill is Insert.')</script>";
	echo "<script language='javascript'>window.location='updatepobalance.php?rinvNo=$billNo'</script>";

}else{
echo "<script language='javascript'>alert('Please check your information.')</script>";
echo "<script language='javascript'>window.location='restock.php'</script>";
}
}
?>