<?php
$login=mysql_connect('localhost', 'wauhaha_karaoke', 'cross123','wauhaha_karaoke');
mysql_selectDB("wauhaha_karaoke",$login);

$PoID=$_GET['PoID'];
$TID = $_POST['TID'];
$time1 = $_POST['time'];
$c = $_POST['c'];
$tp1 = $_POST['tp1'];
$result = mysql_query("Select * from kporder where PoID='$PoID' ");
$row=mysql_fetch_array($result);

$ProName = $row['ProName'];
$SupID = $row['SupID'];
$resulta=mysql_query("select * from ksupplier where SupID='$SupID'");
$row3=mysql_fetch_array($resulta);
$SupName=$row3['SupName'];
	for($i=1;$i<$c;$i++){
	$do = $_POST['do'.$i];
	$inv = $_POST['inv'.$i];
	$p = $_POST['p'.$i];
	$pr =$_POST['pr'.$i];
	$q =$_POST['q'.$i];
	$po=$_POST['po'.$i];
	$tp = $q * $pr;
	$result=mysql_query("select * from kporder where PoNum='$po'");
	$row = mysql_fetch_array($result);
	$BQuan = $row['BQuan'];
	$ProRQuan = $row['ProRQuan'];
	$RQuan = $ProRQuan - $q;
	$q1 = $q +  $BQuan;
	$result4=mysql_query("select RQuan from kstock where ProName='$p'");
	$row4 = mysql_fetch_array($result4);
	$r = $row4['RQuan'];
	$r2 = $q + $r;
		$query=mysql_query("insert into kstocktran(TID,PoID,DO,Inv,SupID,SupName,ProName,ProAddQuan,ProPrice,ProTTPrice,Date)value('$TID','$PoID','$do','$inv','$SupID','$SupName','$p','$q','$pr','$tp','$time1')");
	$query1=mysql_query("update kporder Set BQuan ='$q1' ,ProRQuan = '$RQuan', price = '$pr' where PoNum='$po'");
	$query2=mysql_query("update kstock Set RQuan ='$r2' where ProName='$p' ");
	}
	if(($query)||($query1)){
	echo "<script language=javascript>alert('Add successful!!');window.location='http://www.wauhaha.com/karaoke/kitstock.php'</script>";
}
echo "<script language=javascript>alert('Add fail!!');window.location='http://www.wauhaha.com/karaoke/kitstock.php '</script>";
?>
	