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

session_start();
require("auth.php");
unset($msg);

######################################################
# Determine if Access Level is adequate to view page #
######################################################
if ($Transmarco["accesslevel"] < $coadminlevel) {
	authfail();
} else 	{ 

#######################################################################
# Registering Sort Order into Session                                 #
# Will control the page so that the page appears in the same sorting  #
# order everytime the user from the same browser access the same page #
#######################################################################
// First time the user access the page, automatically sort by group starting range
if (($Transmarco["usersuppliersort"] == "") && ($sortorder == "")) {
	$Transmarco["usersuppliersort"] = "suppliername";
}
// Subsequently when users decide on a sorting preference, store their preference into a session
if (($Transmarco["usersuppliersort"] != "") && ($sortorder != "")) {
	$Transmarco["usersuppliersort"] = $sortorder;
}

###################################################
# Deactivate a Supplier                           #
# Updating database if user decides to Deactivate #
###################################################
#if ($action == 'Deactivate') {
#	$sql = "update supplier set supplierstatus='N' where supplierid=$id";
#	db_query($sql);
#	$msg = "<br><b>Supplier Deactivated</b><br>";
#}

#################################################
# Activating a Supplier                         #
# Updating database if user decides to Activate #
#################################################
#if ($action == 'Activate') {
#	$sql = "update supplier set supplierstatus='Y' where supplierid=$id";
#	db_query($sql);
#	$msg = "<br><b>Supplier Activated</b><br>";
#}

##################################################
# Deleting a Supplier                            #
# Delete selected supplier from database         #
##################################################
if ($action == 'Delete') {
	$sql = "delete from supplier where supplierid=$id";
	db_query($sql);
	$msg = "<br><b>Supplier Deleted</b><br>";
}

###################################################
# Editing a Supplier                              #
# Inserting into database if user decides to Edit #
###################################################
if (($action == 'Edit') && ($submitted == 'yes')) {
	// Initializing Msg Text
	$msg = "";

	// Checking if Group Description is blank when User is editing a group
	if (trim($supplierdesc) != "") {
		$supplierdesc = magiccode(ucwords($supplierdesc));
		$sql = "update supplier set supplierdesc='$supplierdesc' where supplierid=$id";
		$result = db_query($sql);
		$msg = "<br><b>Supplier Description Successfully Updated!";
	} else {
		$msg = "<br>Supplier Description Must Be Filled In";
	}
}

##################################################
# Add a Supplier                                 #
# Inserting into database if user decides to Add #
##################################################
if (($action == 'Add') && ($submitted == 'yes')) {
	// Initializing Msg Text
	$msg = "";

	// Checking if Supplier name is valid when User is adding a supplier, and if valid, force all characters to uppercase
	if (trim($suppliername) != "") {
		// Checking to see the the existing Group Code already exists in the database
		$suppliername = magiccode(strtoupper($suppliername));
		$sql = "select * from supplier where suppliername='$suppliername'";
		$result = db_query($sql);
		if (db_numrows($result) >= 1) {
			$msg = $msg . "<br>The Supplier name keyed in already exists in the database!";
		}
	} else {
		$msg = $msg . "<br>Invalid Supplier Name Entered";
	}

	// Checking if Supplier Description is valid when User is adding a supplier
	if (trim($supplierdesc) != "") {
		$supplierdesc = magiccode($supplierdesc);
	} else {
		$msg = $msg . "<br>Invalid Supplier Description Entered";
	}

	// If no messages are stored up until this point, all data entered is valid, Proceed with insertion
	if ($msg == "") {
		$newid = magiccode(find_new_id("supplierid","supplier"));
		$sql = "insert into supplier (`supplierid`,`suppliername`,`supplierdesc`,`supplierstatus`) values('$newid','$suppliername','$supplierdesc','Y')";
		db_query($sql);
		$msg = "<br><b>Supplier " . $suppliername . " Created<br>";
		unset($supplierid);
		unset($suppliername);
		unset($supplierdesc);
		unset($supplierstatus);
	} else {
		$msg = $msg . "<br><b>Creation of New Supplier Failed!</b><br>";
	}
}

###############################
# HTML FOR FIRST TABLE ON TOP #
###############################
?>
<HTML>
	<HEAD>
		<TITLE>Supplier Management Form</TITLE>
		<META content="text/html; charset=windows-1252" http-equiv=Content-Type>
		<META content=en-us http-equiv=Content-Language>
		<? include($style); ?>
	</HEAD>
	<BODY bgColor=#ffffff leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
	<center>
	<p>&nbsp;</p>
	<table width="680" border="0" cellspacing="0" cellpadding="8" align="center">
		<tr valign="top"> 
			
      <td align="center"><font class="sub"><b>EXISTING Supplier(s)</b></font><br><? echo $msg?></td>
		</tr>
	</table>
	<table class="bigbox" border="0" cellspacing="0" cellpadding="0" align="center">
		<tr> 
			<td width="16" height="16" valign="top" align="left"><img src="<? echo $images ?>a.gif" width="16" height="18"></td>
			<td width="363"><img src="<? echo $images ?>shim.gif" width="16" height="16"></td>
			<td width="17" height="16" align="right" valign="top"><img src="<? echo $images ?>b.gif" width="16" height="16"></td>
		</tr>
		<tr> 
			<td width="16">&nbsp;</td>
			<td valign="top" class="bgline"> 
				<table cellpadding="5" cellspacing="1" border="0" align="center">
					<tr class="highlights"> 
						<td><a href="<? echo $docroot ?>/<? echo $suppliermgt ?><? echo "?sortorder=suppliername"?>"><b>Supplier Name</b></a></td>
						<td><b>Supplier Description</b></td>
						<td><a href="<? echo $docroot ?>/<? echo $suppliermgt ?><? echo "?sortorder=supplierstatus"?>"><b>Supplier Status</b></a></td>
						<td><b>Action</b></td>
					</tr>
					<? 
					//  Generating Existing Supplier
					$sql = "select * from supplier order by $Transmarco[usersuppliersort]";
					$result = db_query($sql);
					for($x = 0; $x < db_numrows($result); $x++){
						$row = db_fetch_array($result,1);
						$currentsupplierid = $row["supplierid"];
						$currentsuppliername = $row["suppliername"];
						$currentsupplierdesc = $row["supplierdesc"];
						$currentsupplierstatus = $row["supplierstatus"];

					?>
					<tr class="tdfill"> 
						<td><? echo $currentsuppliername ?></td>
						<td><? echo $currentsupplierdesc ?></td>						
						<td align="center">
						<?
						if ($currentsupplierstatus == "Y") {
						?>
						Active
						<? } else { ?>
						Inactive
						<? } ?>
						</td>
						<td><a href="<? echo $docroot ?>/<? echo $suppliermgt ?><? echo "?action=Edit&id=" . $currentsupplierid ?>">Edit</a> 
						<? 
						// Generating the Activate or Deactivate action
						#if ($currentsupplierstatus == "Y") {		
						?>
						<!--<a href="<? echo $docroot ?>/<? echo $suppliermgt ?><? echo "?action=Deactivate&id=" . $currentsupplierid ?>">Deactivate</a>-->
						<? #} else { ?>
						<!--<a href="<? echo $docroot ?>/<? echo $suppliermgt ?><? echo "?action=Activate&id=" . $currentsupplierid ?>">Activate</a>-->
						<? #}
						if ($Transmarco["accesslevel"] == $adminlevel) { 
						echo "<a href=" . $docroot . "/" . $suppliermgt . "?action=Delete&id=" . $currentsupplierid . ">Delete</a>";
						}
						?>
						</td>
					</tr>
					<? } ?>
				</table>
			</td>
			<td width="17" align="right">&nbsp;</td>
		</tr>
		<tr> 
			<td width="16" valign="bottom" align="left"><img src="<? echo $images ?>c.gif" width="16" height="18"></td>
			<td width="363">&nbsp;</td>
			<td width="17" align="right" valign="bottom"><img src="<? echo $images ?>d.gif" width="16" height="16"></td>
		</tr>
	</table>
	<p align="center"><a href="<? echo $docroot ?>/<? echo $suppliermgt ?><? echo "?action=Add" ?>">Add New Supplier</a></p>
	</center>
	<br>
<? 	
##################################
# HTML FOR FIRST TABLE AT BOTTOM #
##################################

	if (($action == 'Edit') || ($action == 'Add')){ 
	// Prefilling the fields in the form with values from db
	if ($action=='Edit') {
		$sql = "select * from supplier where supplierid = $id";
		$result = db_query($sql);
		for($x=0; $x<db_numrows($result); $x++){
			$row = db_fetch_array($result,1);
			$supplierid = $row["supplierid"];
			$suppliername = $row["suppliername"];
			$supplierdesc = $row["supplierdesc"];
			$supplierstatus = $row["supplierstatus"];
		}
	}
?>
<form method="post" action="<? echo $PHP_SELF ?>" name="form">
	<table width="680" border="0" cellspacing="0" cellpadding="8" align="center">
	<tr valign="top"> 
	  <td align="center"><font class="sub"><b>
		<? echo strtoupper($action) ?> SUPPLIER</b></font></td>
	</tr>
	</table>
	<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="<? echo $images ?>a.gif" width="16" height="18"></td>
			<td width="563"><img src="<? echo $images ?>shim.gif" width="16" height="16"></td>
			<td width="17" height="16" align="right" valign="top"><img src="<? echo $images ?>b.gif" width="16" height="16"></td>
		</tr>
		<tr> 
			<td width="23">&nbsp;</td>
			<td width="563">
				<table>
					<tr>
						<td>Supplier Name</td>
						<? if ($action == 'Add') {?>
						<td><input type="text" name="suppliername" value="<? echo $suppliername ?>" maxlength="20" size="20"> * The System will input this in uppercase</td>
						<? } ?>
						<? if ($action == 'Edit') {?>
						<td><? echo $suppliername ?></td>
						<? } ?>
					</tr>
					<tr>
						<td>Supplier Description</td>
						<td><input type="text" name="supplierdesc" value="<? echo $supplierdesc ?>" maxlength="200" size="65"></td>
					</tr>
				</table>
			</td>
			<td width="17">&nbsp;</td>
		</tr>
		<tr valign="baseline"> 
			<td width="23">&nbsp;</td>
				<input type="hidden" name="submitted" value="yes">
			<? if ($action == "Edit") { ?>
				<input type="hidden" name=id value=<? echo $id ?>>
			<? } ?>
			<td width="563"><center><input type="submit" name="action" value="<? echo ucwords($action) ?>">&nbsp<input type="reset" name="Reset" value="Clear">&nbsp<input type="submit" name="action" value="Cancel"></center></td>
			<td width="17">&nbsp;</td>
		</tr>
		<tr> 
			<td width="16" valign="bottom" align="left"><img src="<? echo $images ?>c.gif" width="16" height="18"></td>
			<td width="563">&nbsp;</td>
			<td width="17" align="right" valign="bottom"><img src="<? echo $images ?>d.gif" width="16" height="16"></td>
		</tr>
	</table>
</form>
<? } ?>
	</BODY>
</HTML>

<? } ?>
