<html>
<head>
<title>Add Stock</title>
</head>
<body style="background-color: #ffffff;background-image:url(rail.jpg);background-repeat: repeat-y">
<center>
<table>
	<tr>	
		<td><a href="http://www.wauhaha.com/karaoke/adminaccount.php"><img src=admin.gif width=140 height=140 border=0/><br/></a></td>
		<td><a href="http://www.wauhaha.com/karaoke/adminviewmem.php"><img src=member.gif width=140 height=140 border=0/><br/></a></td>
		<td><a href="http://www.wauhaha.com/karaoke/adminviewstaff.php"><img src=staff.gif width=140 height=140 border=0/><br/></a></td>
		<td><a href="http://www.wauhaha.com/karaoke/dutytime.php"><img src=duty.gif width=140 height=140 border=0/><br/></a></td>
		<td><a href="http://www.wauhaha.com/karaoke/salary.php"><img src=salary.gif width=140 height=140 border=0/><br/></a></td>
		<td><a href=adminhistory.php><img src=leave.gif width=140 height=140 border=0/><br/></a></td>
		<td><a href=adminhistory.php><img src=job.gif width=140 height=140 border=0/><br/></a></td>
		<td><a href=adminhistory.php><img src=advertisement.gif width=140 height=140 border=0/><br/></a></td>
		<td><a href="http://www.wauhaha.com/karaoke/room.php"><img src=room.gif width=140 height=140 border=0/><br/></a></td>
		
	</tr>
</table>
</center>
<center><h1><u>Admin In Stock</u></h1>
<a href="http://www.wauhaha.com/karaoke/kinstock.php">Back</a></center>


<?php


$login=mysql_connect('localhost', 'wauhaha_karaoke', 'cross123','wauhaha_karaoke');
	
mysql_selectDB("wauhaha_karaoke",$login);
$time_offset ="240"; 
$time_a = ($time_offset * 120);
$time = date("Y-m-d");

$inv=$_POST['inv'];
$result = mysql_query("Select * from kporder where PoID='$po' ");
$result2 = mysql_query("Select * from kporder where PoID='$po' ");
$row2=mysql_fetch_array($result2);
$PoNum = $row2['PoNum'];
$SupName = $row2['SupName'];
$ProRQuan = $row2['ProRQuan'];
$result9 = mysql_query("Select * from ksupplier where SupName='$SupName' ");
$r = mysql_fetch_array($result9);
$SupID = $r['SupID'];
$g=$_POST['g'];
$total = $_POST['total'];

for($i=0;$i<$g;$i++){
$po = $_POST['po'.$i];
$q = $_POST['q'.$i];
$pr = $_POST['pr'.$i];
$t = $_POST['t'.$i];
$p = $_POST['p'.$i];
$result = mysql_query("Select * from kporder where PoID='$po' ");
$result2 = mysql_query("Select * from kporder where PoID='$po' ");
$row2=mysql_fetch_array($result2);
$PoNum = $row2['PoNum'];
$SupName = $row2['SupName'];
$ProRQuan = $row2['ProRQuan'];

$RQuan1 = $ProRQuan - $q;

if ($RQuan1 == 0){
$query7 = mysql_query("delete from kporder where PoID='$po' and ProName='$p'");

}
if ($RQuan1 < 0){
	echo "<script language=javascript>alert('Quantity Not enough!!');window.location='http://www.wauhaha.com/karaoke/kinstock.php'</script>";die;
}
	$result4=mysql_query("select RQuan from kstock where ProName='$p'");
	$row4 = mysql_fetch_array($result4);
	$r = $row4['RQuan'];
	$r2 = $r + $q;
$query=mysql_query("insert into kstocktran(PoID,Inv,SupID,SupName,ProName,ProAddQuan,ProPrice,ProTTPrice,Date)value('$po','$inv','$SupID','$SupName','$p','$q','$pr','$t','$time')");
	$query1=mysql_query("update kporder Set BQuan ='$q' ,ProRQuan = '$RQuan1', price = '$pr' where PoID='$po' and ProName='$p' ");
$query2=mysql_query("update kstock Set RQuan ='$r2' where ProName='$p' ");

}
if(($query)&&($query1)&&($query2)){
	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>";

?>
