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




$min_dt = substr($invdate,6,4).'-'.substr($invdate,3,2).'-'.substr($invdate,0,2);

$query = "select bcode,prodcode,po_no,po_date,qty,specifications,remarks,compcode,indent_record,received_qty,rejected_qty from matpo where record ='$p_record' ";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_row($result);
$bcode = $row[0];
$prodcode =$row[1];
$po_no = $row[2];
$po_date = $row[3];
$qty = $row[4];
$specifications = $row[5];
$remarks = $row[6];
$compcode = $row[7];
$indent_record = $row[8];
$recd_qty = $row[9];
$rej_qty = $row[10];

$matpo_received_qty = $recd_qty + $accepted_qty;
$matpo_rejected_qty = $rej_qty + $rejected_qty;
/*
if($qty > ($accepted_qty+$rejected_qty))
{
	echo("Entered Qty is more than PO 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($accepted_qty >0 )
{
	$inv_date = substr($invdate,6,4).'-'.substr($invdate,3,2).'-'.substr($invdate,0,2);
	$tc_date = substr($tcdate,6,4).'-'.substr($tcdate,3,2).'-'.substr($tcdate,0,2);
	$txresult = mysql_query("select received_qty from matindent where record='$indent_record' ") or die(mysql_error());
	$trow = mysql_fetch_row($txresult);
	$received_qty = $trow[0];
		
	$new_received_qty =$received_qty+$accepted_qty;

	$query= "update matindent set received_qty = $new_received_qty where record='$indent_record'";

	mysql_query($query) or die(mysql_error());

	$query= "update matpo set received_qty = $matpo_received_qty,rejected_qty = $matpo_rejected_qty,min_no ='$min_no',min_date='$min_dt'  where record ='$p_record'";
	mysql_query($query) or die(mysql_error());			

	$query="insert into mat_purreg values ('$prodcode','$po_no','$po_date','$bcode','$accepted_qty','','$accepted_qty','$rejected_qty','$invno','$inv_date','$compcode','$p_record','$account_year','$min_no','$min_dt','$tcno','$tc_date','$idno','$sample_size','$finish_spec','$finish_checking','$finish_observe','$burr_spec','$burr_checking','$burr_observe','$bend_spec','$bend_checking','$bend_observe','$thick_spec','$thick_checking','$thick_observe','$width_spec','$width_checking','$width_observe','$diameter_spec','$diameter_checking','$diameter_observe','$hard_spec','$hard_checking','$hard_observe','$carbon_spec','$carbon_checking','$carbon_observe','$inspector','$incharge_qc','$status','$accepted_qty_nos','','$accepted_qty_nos',NULL	)";
	mysql_query($query) or die (mysql_error());
			

	$resu = mysql_query("select sum(balance) from mat_purreg where  prodcode = '$prodcode' and bcode = '$bcode'  ") or die(mysql_error());
	$row= mysql_fetch_row($resu);
	$qtyonhand = 0;
	
	if(is_array($row))
	{
	   $qtyonhand = $row[0];
	}
	if ($qtyonhand > 0)
	{
	   $opbal = $qtyonhand - $accepted_qty;
	}
	else
	{
		$opbal = 0;
	}
	$clbal = $qtyonhand;

	if ($qtyonhand==0)
	{
	   $clbal=$accepted_qty;
	}	
	$resu = mysql_query("select max(record) from mat_purreg  ") or die(mysql_error());
	$row= mysql_fetch_row($resu);
	$mat_pur_record = $row[0];

	$query="insert into stktrans values ('$prodcode','$min_dt','$bcode','$opbal','$accepted_qty','0','$clbal','$invno','$rate','$account_year','$compcode','$mat_pur_record',NULL)";

	mysql_query($query) or die (mysql_error());

}
?>

<br>
<br>
<br>

<br><br><font size=1><center><a href= "con_select.php">Make another MIN</center></font>
<br><br><font size=1><center><a href= "/wilson/stores.html">Return to index page</center></font>
