<?php include("../../include/dbconnect.php"); 


$result4 = mysql_query("select bcode,prodcode from orders where record= '$record' ") or die(mysql_error());
$row4 = mysql_fetch_row($result4);
$bcode =$row4[0];
$prodcode =$row4[1];


$result3 = mysql_query("select issue, balance from purreg where bcode = '$bcode' and prodcode ='$prodcode' and balance > 0 ") or die(mysql_error());
$row3 = mysql_fetch_row($result3);
$issue2=$row3[0];
$balance2=$row3[1];
$issue2=$issue2+$qty;
$balance2=$balance2-$qty;


$result = mysql_query("select issued_qty, balance_qty,inv_nos,schedule_qty from orders where record= '$record' ") or die(mysql_error());
$row = mysql_fetch_row($result);
$issue=$row[0];
$balance=$row[1];
$issue=$issue+$qty;
$balance=$balance-$qty;
$order_qty=$row[3];

if($balance2 < 0)
{
	echo("Stock qty is less than issueing qty.  Please correct stock register later<br>");
}

if($balance <= $order_qty)
{
	if(empty($row[2]))
	{
		$invnos=$inv_no.'&nbsp;/&nbsp;'.$inv_date;
	
	}
	elseif(!empty($row[2]))
	{
		$invnos=' '.$row[2].','.$inv_no.'/'.$inv_date;
	
	}
	$invdt=substr($inv_date,6,4).'-'.substr($inv_date,3,2).'-'.substr($inv_date,0,2);				
	
	if ( isset($submit) && $submit == "Submit")
	{
		
		$query="update orders set balance_qty='$balance',issued_qty='$issue',inv_nos='$invnos',inv_date='$invdt' where record = '$record' ";
		mysql_query($query) or die(mysql_error());
		
		$query="update purreg set balance='$balance2',issue='$issue2' where bcode ='$bcode' and prodcode ='$prodcode' ";
		mysql_query($query) or die(mysql_error());
		
	}
}
else
{
	echo("Invoice Qty is more than Pending Qty.  Please check and resubmit again<br>");
	exit;
}

?>
<table>
<tr><td colspan=4 align=middle>

<h4><a href="view_sele.php">View Order Details  </a>
<h4><a href="issue_input.php">Enter Another Invoice  </a>

<br><br><font size=1><center><a href= "../../index.html">Return to index page</center></font></table>
