<?php
include("connect.php");
	
	
	
$rinvNo = $_GET['rinvNo'];
	
$result = mysql_query("SELECT poNo0, poNo1, poNo2 FROM receivedinvoice WHERE rinvNo = '$rinvNo'");

$row = mysql_fetch_array($result);

$poNo0 = $row['poNo0'];
$poNo1 = $row['poNo1'];
$poNo2 = $row['poNo2'];

$result1 = mysql_query("SELECT poItem, poQuantity FROM poitem WHERE poNo = '$poNo0'");

$num_results1 = mysql_num_rows($result1);

for ($i=0; $i <$num_results1; $i++)
{
$row1 = mysql_fetch_array($result1);

$poItem = $row1['poItem'];
$poQuantity = $row1['poQuantity'];

$result4 = mysql_query("SELECT qtotal FROM receivedinvoice WHERE rinvNo = '$rinvNo'");

$row4 = mysql_fetch_array($result4);

$qtotal = $row4['qtotal'];

$result2 = mysql_query("SELECT quantity FROM receivedinvitem WHERE ISBN = '$poItem'&&poNo = '$poNo0'&&invNo != '$rinvNo'");

$num_results2 = mysql_num_rows($result2);
$total = 0;
for ($z=0; $z <$num_results2; $z++)
{

$row2 = mysql_fetch_array($result2);

$quantity = $row2['quantity'];
$total = $total + $quantity;
}

$fquantity = $poQuantity - $total;
$qtotal = $qtotal - $total;
$result3 = mysql_query("UPDATE receivedinvitem SET quantity = '$fquantity' WHERE ISBN = '$poItem'&&invNo = '$rinvNo'&&poNo = '$poNo0'");


$result5 = mysql_query("UPDATE receivedinvoice SET qtotal = '$qtotal' WHERE rinvNo = '$rinvNo'");

}

if($poNo1!=""){
$result6 = mysql_query("SELECT poItem, poQuantity FROM poitem WHERE poNo = '$poNo1'");

$num_results6 = $result6->num_rows;

for ($i=0; $i <$num_results6; $i++)
{
$row6 = mysql_fetch_array($result6);

$poItem = $row6['poItem'];
$poQuantity = $row6['poQuantity'];

$result4 = mysql_query("SELECT qtotal FROM receivedinvoice WHERE rinvNo = '$rinvNo'");

$row4 = mysql_fetch_array($result4);

$qtotal = $row4['qtotal'];

$result2 = mysql_query("SELECT quantity FROM receivedinvitem WHERE ISBN = '$poItem'&&poNo = '$poNo1'&&invNo != '$rinvNo'");

$num_results2 = mysql_num_rows($result2);
$total2 = 0;
for ($z=0; $z <$num_results2; $z++)
{

$row2 = mysql_fetch_array($result2);

$quantity = $row2['quantity'];
$total2 = $total2 + $quantity;
}

$fquantity = $poQuantity - $total2;
$qtotal = $qtotal - $total2;
$result3 = mysql_query("UPDATE receivedinvitem SET quantity = '$fquantity' WHERE ISBN = '$poItem'&&invNo = '$rinvNo'&&poNo = '$poNo1'");


$result5 = mysql_query("UPDATE receivedinvoice SET qtotal = '$qtotal' WHERE rinvNo = '$rinvNo'");

}
}

if($poNo2!=""){
$result7 = mysql_query("SELECT poItem, poQuantity FROM poitem WHERE poNo = '$poNo2'");

$num_results7 = $result7->num_rows;

for ($i=0; $i <$num_results7; $i++)
{
$row7 = mysql_fetch_array($result7);

$poItem = $row7['poItem'];
$poQuantity = $row7['poQuantity'];

$result4 = mysql_query("SELECT qtotal FROM receivedinvoice WHERE rinvNo = '$rinvNo'");

$row4 = mysql_fetch_array($result4);

$qtotal = $row4['qtotal'];

$result2 = mysql_query("SELECT quantity FROM receivedinvitem WHERE ISBN = '$poItem'&&poNo = '$poNo2'&&invNo != '$rinvNo'");

$num_results2 = mysql_num_rows($result2);
$total3 = 0;
for ($z=0; $z <$num_results2; $z++)
{

$row2 = mysql_fetch_array($result2);

$quantity = $row2['quantity'];
$total3 = $total3 + $quantity;
}

$fquantity = $poQuantity - $total3;
$qtotal = $qtotal - $total3;
$result3 = mysql_query("UPDATE receivedinvitem SET quantity = '$fquantity' WHERE ISBN = '$poItem'&&invNo = '$rinvNo'&&poNo = '$poNo2'");


$result5 = mysql_query("UPDATE receivedinvoice SET qtotal = '$qtotal' WHERE rinvNo = '$rinvNo'");

}
}

$result12 = mysql_query("SELECT * FROM receivedinvoice WHERE rinvNo = '$rinvNo'");
$row12 = mysql_fetch_array($result12);
$rqtotal = $row12['qtotal'];

if($rqtotal == 0){
$query8=mysql_query("delete from receivedinvitem where invNo='$rinvNo'");
$query13=mysql_query("delete from receivedinvoice where rinvNo='$rinvNo'");
$db = mysql_close();
echo "<script language='javascript'>alert('This PO is transfer already.')</script>";
echo "<script language='javascript'>window.location='restock.php'</script>";
}else{
$query8=mysql_query("delete from receivedinvitem where quantity='0'");
$db = mysql_close();
echo "<script language='javascript'>window.location='restockitem.php?rinvNo=$rinvNo'</script>";
}
?>