<?
/*
Author		: Yaw
Version		: 1.01
Date		: 29th March 2002
Function	: Inventory Management

Revised		: Louis
Version		: Final
Date		: 9th June 2002
*/ 
session_start();
require("auth.php");
unset($msg);
unset($success);
######################################################
# Determine if Access Level is adequate to view page #
######################################################
if ($Transmarco["accesslevel"] < $public) {
	authfail();
} else {
	if ($action == "Create") {
		$todaydate = magiccode(date("Y-m-d H:i:s"));
		$createduser = magiccode($Transmarco["username"]);
		$stylestartingrange = magiccode($poolnonow);
		$selectedGroup = magiccode($selectedGroup);
		$selectedPool = magiccode($selectedPool);
		$start = intval($poolstart);
		$end = intval($poolend);
		// Check for positive number and NaN
		if (($inventorycreate > 0) && ereg("([0-9]{1,2})", $inventorycreate) && ($inventorycreate <= 50)){
			$x = 0;			
			$tmppool = intval($poolnonow);
			$sql = "select invnumber from inventoryprofile where invpoolid = $selectedPool and invnumber > $tmppool order by invnumber";
			$result = db_query($sql);
			if (db_numrows($result) >= 1) {
				for ($y = 0; $y < db_numrows($result); $y++) {
					$rs = db_fetch_array($result,$y);
					$pno[$y] = intval($rs["invnumber"]);
					if ($x < $inventorycreate || $tmppool <= $end) {
						if ($pno[$y] == $tmppool) {
							$tmppool++;
						}
						else if ($pno[$y] > $tmppool) {
							while ($pno[$y] > $tmppool && $x < $inventorycreate && $tmppool <= $end) {
								if (strlen($tmppool) == 1) {
									$tmppool = "000" . $tmppool;
								}
								if (strlen($tmppool) == 2) {
									$tmppool = "00" . $tmppool;
								}
								if (strlen($tmppool) == 3) {
									$tmppool = "0" . $tmppool;
								}
								$poolno = magiccode($tmppool);
								$newid = magiccode(find_new_id("invid","inventoryprofile"));
								$sql = "insert into inventoryprofile (invid, invcreatedtime, invgroupcode, invpoolid, invnumber, invcreatedbyuser, invlock, invedit)";
								$sql = $sql . " values ('$newid','$todaydate','$selectedGroup','$selectedPool','$poolno','$createduser','N','N')";
								db_query($sql);
								$createsuccess = $tmppool;
								$tmppool++;
								$success="Y";
								$x++;
							}
							$tmppool++;
						}
					}
					else {
						break;
					}
				}
			}

			while ($x < $inventorycreate) {
				if ($tmppool <= $end) {
					if (strlen($tmppool) == 1) {
						$tmppool = "000" . $tmppool;
					}
					if (strlen($tmppool) == 2) {
						$tmppool = "00" . $tmppool;
					}
					if (strlen($tmppool) == 3) {
						$tmppool = "0" . $tmppool;
					}
					$poolno = magiccode($tmppool);
					$newid = magiccode(find_new_id("invid","inventoryprofile"));
					$sql = "insert into inventoryprofile (invid, invcreatedtime, invgroupcode, invpoolid, invnumber, invcreatedbyuser, invlock, invedit)";
					$sql = $sql . " values ('$newid','$todaydate','$selectedGroup','$selectedPool','$poolno','$createduser','N','N')";						
					db_query($sql);
					$createsuccess = $tmppool;
					$tmppool++;
					$success="Y";
					$x++;
				}
				else {
					$msg = $msg . "<p><b>Remaining pool number is not enough to create " . $inventorycreate . " inventories profile.<br>";
					$msg = $msg . "Only " . $x . " inventory profiles are created.</b></p>";
					break;
				}
			}
			// Registering the query sql or message into the session so that the inventory profile query page will remember the settings
			$Transmarco["querysql"]="select * from inventoryprofile where invgroupcode='$selectedGroup' and invnumber>='$stylestartingrange' and invnumber<='$createsuccess'";
			$Transmarco["querymsg"]="Query showing last created inventory profiles in the same session";
		}
		else if ($manualcode > 0 && ereg("([0-9]{4})", $manualcode)) {			
			if (intval($manualcode) < $start || intval($manualcode) > $end) {
				$msg = $msg . "<br><b>The code is out of the number range.</b>";
			}
			else {
				$sql = "select * from inventoryprofile where invnumber = '$manualcode'";
				$result = db_query($sql);
				if (db_numrows($result) >= 1) {
					$msg = $msg . "<br><b>The code has already been used!</b>";
				}
				else {
					$manualcode = magiccode($manualcode);
					$stylestartingrange = $manualcode;
					$newid = magiccode(find_new_id("invid","inventoryprofile"));
					$sql = "insert into inventoryprofile (invid, invcreatedtime, invgroupcode, invpoolid, invnumber, invcreatedbyuser, invlock, invedit)";
					$sql = $sql . " values ('$newid','$todaydate','$selectedGroup','$selectedPool','$manualcode','$createduser','N','N')";
					db_query($sql);				
					$success = "Y";
					$Transmarco["querysql"]="select * from inventoryprofile where invgroupcode='$selectedGroup' and invnumber ='$manualcode'";
					$Transmarco["querymsg"]="<b>Query showing last created inventory profiles in the same session</b>";
				}
			}
		}
		else {
			// Failed inventorycreate check for positive number and NaN
			$msg = $msg . "<b>Invalid code of inventory profiles keyed-in for creation!</b><br>";
		}
	}
?>
<html>
<head>
<title>Inventory Management Form</title>
<? if ($success == "Y") { ?>
	<META HTTP-EQUIV="Refresh" CONTENT="8; URL=<?=$docroot?>/<?=$prodquery?>">
<? } ?>
<script language=javascript>
<!--
function OnChange(dropdown) {
	var myindex = dropdown.selectedIndex;
	var gcode = dropdown.options[myindex].value.substring(0,2);
	var pcode = dropdown.options[myindex].value.substring(2);
	var baseURL= "<?=$docroot?>/<?=$prodadd?>?selectedGroup=";
	//location.href = baseURL + dropdown.options[myindex].value;
	location.href = baseURL + gcode + "&selectedPool=" + pcode;
	return true;
}
//-->
</script>
<? include($style); ?>
</head>
<body bgcolor=#ffffff leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<p>&nbsp;</p>
<table width="680" border="0" cellspacing="0" cellpadding="8" align="center">
<tr valign="top"><td align="center"><font class="sub"><b>ADD INVENTORY PROFILE</b></font></td></tr>
<tr><td align="center">
<?
if ($success == "Y") { 
	if ($inventorycreate == 1 || trim($manualcode) != "") {
		$msg = $msg . "<b>" . $selectedGroup . $stylestartingrange . " Created!</b><br>";
	} else { 
		$msg = $msg . "<b>" . $selectedGroup . $stylestartingrange . " to " . $selectedGroup . $createsuccess . " Created!</b><br>";
	}
	$msg = $msg . "If your browser does not forward you to the update product page in 8 seconds, please click on this <a href=" . $docroot . "/" . $prodquery . ">link</a>";
}
echo $msg;
?></td></tr>
</table>
<br>
<table width="600" border="0" cellspacing="0" cellpadding="0" bgcolor="#EEEEEE" align="center">
<tr> 
	<td width="16" height="16" valign="top" align="left"><img src="<?=$images?>a.gif" width="16" height="18"></td>
	<td width="563"><img src="<?=$images?>shim.gif" width="16" height="16"></td>
	<td width="17" height="16" align="right" valign="top"><img src="<?=$images?>b.gif" width="16" height="16"></td>
</tr>
<tr> 
	<td width="23">&nbsp;</td>
	<td width="563">
		<form method="post" action="prodadd.php" id=form1 name=form1>
		<table width="563">
		<tr>
			<td width="25%">Group Code:</td>
			<td width="20%">
				<select name="selectedGroup" onchange='OnChange(this.form.selectedGroup);'>
				<option value="">Select One</option>
				<? 
				$sql = "select * from grouptable where groupactive='Y' order by groupcode";
				$result= db_query($sql);
				for ($x = 0;$x < db_numrows($result);$x++) {
					$row = db_fetch_array($result,$x);
					$groupid[$x] = $row["groupid"]; 
					$groupcode[$x] = $row["groupcode"];
					$poolid[$x] = $row["poolid"];
				?>
					<option value="<?=$groupcode[$x]?><?=$poolid[$x]?>" <?if(isset($selectedGroup) && $selectedGroup==$groupcode[$x]) echo "selected"; ?>><?=$groupcode[$x]?></option>
				<?
				} ?>
				</select></td>
			<td width="55%">
				<? 
				$sql = "select * from grouptable where groupactive='Y' and groupcode = '$selectedGroup' order by groupcode";
				$result= db_query($sql);
				$row = db_fetch_array($result,$x); ?>
				<? echo "Group description: <b>" . $row["groupdesc"] . "</b>";?></td>
		</tr>
		<noscript><INPUT type="submit" value="Go" class=formButton id=submit1 name=submit1></noscript>
		</table>
		</form>
		<hr>
		<?
		if ($selectedGroup != "") {
			$sql = "select poolstart, poolend from pool where poolid = $selectedPool";
			$result = db_query($sql);
			if (db_numrows($result) >= 1) {
				$rs = db_fetch_array($result,1);
				$poolstart = $rs["poolstart"];
				$nonow = intval($poolstart);
				$poolend = $rs["poolend"];
				$noend = intval($poolend);
			}
			
			$sql = "select invnumber from inventoryprofile where invpoolid = $selectedPool order by invnumber";
			$result= db_query($sql);
			if (db_numrows($result) >= 1) {
				for ($y = 0; $y < db_numrows($result); $y++) {
					$rs = db_fetch_array($result,$y);
					$no[$y] = intval($rs["invnumber"]);
					if ($no[$y] == $nonow) {
						$nonow++;
					}
					else if ($no[$y] > $nonow) {
						break;
					}
				}
			}

			if ($nonow <= $noend) {
				if (strlen($nonow) == 1) {
					$nonow = "000" . $nonow;
				}
				else if (strlen($nonow) == 2) {
					$nonow = "00" . $nonow;
				}
				else if (strlen($nonow) == 3) {
					$nonow = "0" . $nonow;
				}
				$flag = 1;
			}
			else {
				$flag = 0;
			}

			if ($flag == 1) { ?>		
			<form method="post" action="<?=$PHP_SELF?>" name="form">		
			<table width="563">
			<tr>
				<td width="35%" valign="bottom">First Available Inventory Code:</td>
				<td width="10%" valign="bottom"><b><?=$nonow?></b></td>
				<td width="55%" valign="bottom">
				<? 
				$sql = "select poolstart, poolend from pool where poolid = $selectedPool";
				$result = db_query($sql);
				$row = db_fetch_array($result,$x);
				echo "Block Range for Product " . $selectedGroup . ": <b>" . $poolstart . "</b> - <b>" . $poolend . "</b>";
				?>
				</td>
			</tr>
			<tr>
				<td valign="middle">Number of Profiles to Create:</td>
				<td><input type="text" name="inventorycreate" value="<?=$inventorycreated?>" maxlength="2" size="2"></td>
				<td valign="top">*This field must be +ve.<br>*Maximum of 50 inventory profiles may be created</td>
			</tr>
			<tr>
				<td width="35%" valign="middle">Manual Code Entry:</td>
				<td width="10%"><input type="text" name="manualcode" value="<?=$manualcoded?>" maxlength="4" size="2"></td>
				<td width="55%" valign="top">*Key in only the 4 digit numbers that fall in between the block range.</td>
			</tr>
			<tr>
				<td colspan="2">&nbsp;
					<input type="hidden" name="poolstart" value="<?=$poolstart?>">
					<input type="hidden" name="poolend" value="<?=$poolend?>">
					<input type="hidden" name="selectedGroup" value="<?=$selectedGroup?>">
					<input type="hidden" name="selectedPool" value="<?=$selectedPool?>">
					<input type="hidden" name="poolnonow" value="<?=$nonow?>">
					<input type="hidden" name="submitted" value="yes">
					<? if ($action == "Edit") { ?>
					<input type="hidden" name=id value="<?=$id?>">
					<? } ?></td>				
				<td><input type="submit" name="action" value="Create" class="input"></td>
			</tr>
			</table>
			</form>
			<? } else { ?>
			<table width="563">
			<tr><td align="center"><b>Warning! All of your code number has been used!<br>
			Please use other group codes which has free code number.</b></td></tr></table>
			<?
			}
		}?></td>
	<td width="23">&nbsp;</td>
</tr>
<tr> 
	<td width="23">&nbsp;</td>
	<td width="563"><center><a href="<?=$docroot?>/<?=$index?>" target="_top"><b><br>Back To Index Page</b></a></center></td>
	<td width="17">&nbsp;</td>
</tr>
<tr> 
	<td width="16" valign="bottom" align="left"><img src="<?=$images?>c.gif" width="16" height="18"></td>
	<td width="563">&nbsp;</td>
	<td width="17" align="right" valign="bottom"><img src="<?=$images?>d.gif" width="16" height="16"></td>
</tr>
</table>
</body>
</html>
<? } ?>
