<?
require_once('Connections/localhost.php'); 
mysql_select_db($database_localhost,$localhost);

//$productID = 'P001.N00001';
//$exp = '2012-01-05';
//$quan = '50';
//$own = 'O002';
$productID = $_POST['proID'];
$quan = $_POST['quan'];

//$codeNum = $_POST['codeNum'];
$idSub = substr($productID, -11, 4);
$codeSub = substr($productID, -6, 6);

date_default_timezone_set('Etc/GMT-8');

$dateToday = date('Y-m-d', time());
$action = 'Out Stock';
//echo "<script>alert('".$productType."');</script>";

$query_search = "select * from tbl_introprod where product_ID = '$idSub' AND codeNum = '$codeSub'";
$query_search2 = "select * from tempid";
$query_exec = mysql_query($query_search) or die(mysql_error());
$query_exec2 = mysql_query($query_search2) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
$row = mysql_fetch_array($query_exec);
$row2 = mysql_fetch_array($query_exec2);

$intime = $row['timeToIn'];
$expire = $row['expireDay'];
$hs = $row2['user_ID'];
$owID = $row['OWNER'];
$allQuan = $row['product_QUANTITY'];
$receiver = $row['personForHander'];
$wareH = $row['wareHouse'];
$location = $row['location'];

$result = mysql_query("insert into tbl_history (product_ID,Intime,Outtime,expireD,handStaff,quantity,ownerID,codeNum,action,receiver) value ('$idSub','$intime','$dateToday','$expire','$hs','$quan','$owID','$codeSub','$action','$receiver')");
if($result --> 0){
if($quan < $allQuan){
$nowQuan = $allQuan - $quan;
$result1 = mysql_query("update tbl_introprod SET product_QUANTITY='$nowQuan' where product_ID = '$idSub' AND codeNum = '$codeSub'");
			if($result --> 0){
				$query_search3 = "select * from tbl_location where wareHouse = '$wareH' and location = '$location'";
				$query_exec3 = mysql_query($query_search3) or die(mysql_error());
				$row3 = mysql_fetch_array($query_exec3);
				$storeQ = $row3['numStore'];
				$totalSQ = $storeQ - $quan;
				$result2 = mysql_query("update tbl_location SET numStore='$totalSQ' where wareHouse = '$wareH' AND location = '$location'");
			}
			else{
				$result1 = mysql_query("delete from tbl_history where product_ID = '$idSub' AND codeNum = '$codeSub'");
			}
}
else{
$result1 = mysql_query("delete from tbl_introprod where product_ID = '$idSub' AND codeNum = '$codeSub'");
			if($result --> 0){
				$query_search3 = "select * from tbl_location where wareHouse = '$wareH' and location = '$location'";
				$query_exec3 = mysql_query($query_search3) or die(mysql_error());
				$row3 = mysql_fetch_array($query_exec3);
				$storeQ = $row3['numStore'];
				$totalSQ = $storeQ - $quan;
				$result2 = mysql_query("update tbl_location SET numStore='$totalSQ' where wareHouse = '$wareH' AND location = '$location'");
			}
			else{
				$result1 = mysql_query("delete from tbl_history where product_ID = '$idSub' AND codeNum = '$codeSub'");
			}
}
}
//$row = mysql_fetch_array($result);


?>