<body background="">
<?php include("../../include/dbconnect.php"); 



$tddt = substr($from_date,6,4).'-'.substr($from_date,3,2).'-'.substr($from_date,0,2);



$result = mysql_query("select account_year,name from company where compcode like '%$compcode' ") or die(mysql_error());
$row = mysql_fetch_row($result);

$account_year = $row[1];
$compname = $row[2];
	
if($upd=='Yes')
{

	for($ind=0;$ind<=count($qty);$ind++)
	{	

		if($qty[$ind]>0)
		{
			$result = mysql_query("select issue, balance,issue_nos,balance_nos,prodcode from mat_purreg where record= '$record[$ind]' ") or die(mysql_error());
			$row = mysql_fetch_row($result);
			$issue=$row[0];
			$balance=$row[1];
			$issue_nos = $row[2];
			$balance_nos =$row[3];
			$prodcode=$row[4];
		

	
			$resu = mysql_query("select sum(balance),sum(balance_nos) from mat_purreg where prodcode='$prodcode'  ") or die(mysql_error());
			$row = mysql_fetch_row($resu);
			$qtyonhand = 0;
	
	

			
			if(is_array($row))
			{
			  	$qtyonhand = $row[0];
	
			}
			if ($qtyonhand > 0)
			{
			 	$opbal = $qtyonhand;
			}
			$clbal = $qtyonhand-$qty[$ind];
			echo("Total Available Qty : $qtyonhand<br>");
			echo("This Issue Qty: $qty[$ind]<br>");			
			echo("Closing Balance : $clbal<br>");
			if ($qtyonhand==0)
			{
			   $clbal=$qty[$ind];
			}	
			if($clbal < 0)
			{
				?> <h3>Issued Quantity is more than available qty, Please CORRECT THE ENTRIES IN PURCHASE REGISTER.  THERE IS -VE FIGURE IN BALANCE COLUMN. and resubmit again</h3><?
				exit;
			}
				
			$issue=$issue+$qty[$ind];
			$balance=$balance-$qty[$ind];
			$issue_nos=$issue_nos+$qty_nos[$ind];
			$balance_nos=$balance_nos-$qty[$ind];
			



			$query="insert into stktrans values ('$prodcode','$tddt','$bcode','$opbal','0','$qty[$ind]','$clbal','$invno','','','$compcode','$record[$ind]',NULL)";
			mysql_query($query) or die (mysql_error());
	
			$query="update mat_purreg set balance='$balance',issue='$issue',issue_nos='$issue_nos',balance_nos='$balance_nos' where record = '$record[$ind]' ";
			mysql_query($query) or die(mysql_error());

		}	
	}

}


?>
<br>
<h3>Stock is updated .</h3>
<br><br><font size=+1><center><a href= "issue_mat.php">Issue another Material</a></center></font>
<br><br><font size=1><center><a href= "/wilson/stores.html">Return to index page</a></center></font>
