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

$sdate = $today;


$tddt = substr($sdate,6,4).'-'.substr($sdate,3,2).'-'.substr($sdate,0,2);
	for($ind=0;$ind<=4;$ind++)
	{
			$txresult = mysql_query("select prodcode,approved_qty,po_issued_qty  from matindent where record='$record[$ind]' ") or die(mysql_error());
			$trow = mysql_fetch_row($txresult);
			$prodcode = $trow[0];
			$approved_qty=$trow[1];
			$po_issued_qty=$trow[2];
			$po_issued_qty=$po_issued_qty+$qty[$ind];

			if($po_issued_qty > $approved_qty)
			{
				echo("PO Qty is more than Approved Pending Qty, Please go back and re enter the qty<br>");
				exit;
			}			
		
	}

			$txresult = mysql_query("select account_year from company where compcode ='$compcode' ") or die(mysql_error());
			$trow = mysql_fetch_row($txresult);
			$account_year = $trow[0];


// UPDATE STOCK 
if($upd=='Yes')
{  
	for($ind=0;$ind<=5;$ind++)
	{
		if($qty[$ind] >0 )
		{
			$refdate = substr($date[$ind],6,4).'-'.substr($date[$ind],3,2).'-'.substr($date[$ind],0,2);

			$txresult = mysql_query("select prodcode,approved_qty,po_issued_qty,remarks from matindent where record='$record[$ind]' ") or die(mysql_error());
			$trow = mysql_fetch_row($txresult);
			$prodcode = $trow[0];
			$approved_qty=$trow[1];
			$po_issued_qty=$trow[2];
			$specification = $trow[3];			
			$po_issued_qty=$po_issued_qty+$qty[$ind];

	
			$query="insert into matpo values ('$compcode','$bcode','$prodcode','$po_no','$tddt','$qty[$ind]','$rate[$ind]','$refdate','$sales_tax','$excise_duty','$documents','$payment_terms','$mode_despatch','$despatch_from','','','','$record[$ind]','No','$remarks[$ind]','$specification','$account_year','$reference','$unit[$ind]','','',NULL)";
			mysql_query($query) or die (mysql_error());
			
			$query= "update matindent set po_prepared= 'Yes', po_issued_qty = '$po_issued_qty' where record='$record[$ind]'";
			mysql_query($query) or die(mysql_error());
				
					
		}
	}
}
?>

<br>
<br>
<br>

<meta http-equiv="refresh" content="0 url=index.html">
