<?php
	/*
	** File: edit_sku
	** Description: Form for updating a sku.
	** Version: $Revision: 1.15 $
	** Created: 04/13/1999
	** Author: Leon Atkinson, Shannon -jj Behrens
	** Email: leon@clearink.com, jj@clearink.com
	** CVS Author: $Author: jj $
	** Last Revised: $Date: 2000/01/28 01:58:45 $
	** Log : $Log: edit_sku,v $
	** Log : Revision 1.15  2000/01/28 01:58:45  jj
	** Log : Fixed the itemPrice = itemPrice%1000 bug.
	** Log :
	** Log : Revision 1.14  1999/12/07 21:45:09  leon
	** Log : All forms should be using StartForm now...
	** Log :
	** Log : Revision 1.13  1999/11/09 00:40:04  jj
	** Log : Small layout change (because it didn't look quite right).
	** Log :
	** Log : Revision 1.12  1999/11/08 22:32:25  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.11  1999/10/19 18:24:02  jj
	** Log : The drop down for "sku active" was not setting the correct default because I
	** Log : misspelled the variable.
	** Log :
	** Log : Revision 1.10  1999/10/13 22:19:02  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.9  1999/10/13 00:42:28  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.8  1999/08/18 22:32:16  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.7  1999/08/03 22:50:57  jj
	** Log : Doing quality control on department and items editors.
	** Log :
	** Log : Revision 1.6  1999/08/03 00:26:40  jj
	** Log : A large amount of programming changing the whole department and item admin process.
	** Log :
	** Log : Revision 1.5  1999/07/28 19:27:32  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	$continueScript = TRUE;

	/* 
	** Make sure the user has admin rights. 
	*/
	if( !$UserInfo["Permission"]["Administrate"] )
	{
		print(L_EDITSKU_DENIED . "<BR>\n");
		$continueScript = FALSE;
	}
	
	
	
	/*
	** Make sure we know which sku we are trying to view.
	*/
	if ($continueScript)
	{
		if (!isset($sku))
		{
			print(L_EDITSKU_NOSKU);
			$continueScript = FALSE;
		}
	}
			
	
	
	/*
	** Get all the information about this sku from the database. 
	*/
	if ($continueScript)
	{
		$Query = "SELECT s.Item, s.ExternalSKU, s.Name, s.ListPrice, " . 
			"s.SalePrice, s.Shipping, s.DisplayPrecedence, s.Active, " . 
			"i.Available, i.Reserve, i.Backorder ";
		$Query .= "FROM sku s, inventory i ";
		$Query .= "WHERE s.ID = " . intval($sku) . " ";
		$Query .= "AND s.ID = i.SKU ";
		if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query -->\n");
			$continueScript = FALSE;
		}
		else
		{
			if ( !($DatabaseRow = mysql_fetch_row($DatabaseResult)) )
			{
				print(L_EDITSKU_NOSKU2);
				$continueScript = FALSE;
			}
			else
			{
				$item = $DatabaseRow[0];
				$skuExternalSKU = $DatabaseRow[1];
				$skuName = $DatabaseRow[2];
				$skuListPrice = $DatabaseRow[3];
				$skuSalePrice = $DatabaseRow[4];
				$skuShipping = $DatabaseRow[5];
				$skuDisplayPrecedence = $DatabaseRow[6];
				$skuActive = $DatabaseRow[7];
				$skuAvailable = $DatabaseRow[8];
				$skuReserve = $DatabaseRow[9];
				$skuBackorder = $DatabaseRow[10];
			}
		}
	}



	/*
	** Get variations. 
	*/
	if ($continueScript)
	{
		$Query = "SELECT Variation FROM sku_variation where SKU=" . 
			intval($sku);
		if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query -->\n");
			$continueScript = FALSE;
		}
		else
		{
			while($DatabaseRow = mysql_fetch_row($DatabaseResult))
			{
				$sku_variation[($DatabaseRow[0])] = TRUE;
			}
		}
	}
	
	
	
	/*
	** Do the form.
	*/
	if ($continueScript)
	{
		/* Put a nice border around everything. */
		print("<TABLE BORDER=\"1\" WIDTH=\"100%\"><TR><TD>\n");
		
		/* Start the form, the heading, and the organizational table. */
		print(StartForm("edit_item", 'post', 'UPDATE_SKU', FALSE, 
			array("item"=>$item, "sku"=>$sku)));

		print("<B>" . L_EDITSKU_EDITINGSKU . "</B> " . prepareText($skuName) . "<BR>\n");
		print("<TABLE>\n");
		
		/* Output all of the form fields. */
		print("<TR><TD>" . L_EDITSKU_EXTERNALSKU . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputExternalSKU\" SIZE=\"32\" ");
		print("VALUE=\"" . prepareText($skuExternalSKU) . "\"></TD></TR>\n");

		print("<TR><TD>" . L_EDITSKU_NAME . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputName\" SIZE=\"32\" ");
		print("VALUE=\"" . prepareText($skuName) . "\"></TD></TR>\n");

		print("<TR><TD>" . L_EDITSKU_LISTPRICE . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputListPrice\" SIZE=\"32\" ");
		print("VALUE=\"" . $skuListPrice . "\"></TD></TR>\n");

		print("<TR><TD>" . L_EDITSKU_SALEPRICE . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputSalePrice\" SIZE=\"32\" ");
		print("VALUE=\"" . $skuSalePrice . "\"></TD></TR>\n");
		
		print("<TR><TD>" . L_EDITSKU_SHIPPING . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputShipping\" SIZE=\"32\" ");
		print("VALUE=\"" . $skuShipping . "\"></TD></TR>\n");

		print("<TR><TD>" . L_EDITSKU_DISPLAYPRECEDENCE . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputDisplayPrecedence\" ");
		print("SIZE=\"2\" VALUE=\"" . $skuDisplayPrecedence . "\">" . 
			"</TD></TR>\n");

		print("<TR><TD>" . L_EDITSKU_INVENTORYAVAILABLE . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputAvailable\" ");
		print("SIZE=\"10\" VALUE=\"" . $skuAvailable . 
			"\"></TD></TR>\n");

		print("<TR><TD>" . L_EDITSKU_INVENTORYRESERVE . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputReserve\" ");
		print("SIZE=\"10\" VALUE=\"" . $skuReserve . "\"></TD></TR>\n");

		print("<TR><TD>" . L_EDITSKU_INVENTORYISZERO . "</TD>\n");
		print("<TD><INPUT TYPE=\"checkbox\" NAME=\"inputBackorder\" " . 
			"VALUE=\"Y\"");
		print($skuBackorder == 'Y' ? " CHECKED" : "");
		print("></TD></TR>\n");

		print("<TR><TD>" . L_EDITSKU_ACTIVE . "</TD>\n");
		print("<TD><SELECT NAME=\"inputActive\">\n");
		if ($skuActive=="N")
		{
			print("<OPTION VALUE=\"Y\">Yes\n");
			print("<OPTION VALUE=\"N\" SELECTED>No\n");	
		}
		else
		{
			print("<OPTION VALUE=\"Y\" SELECTED>Yes\n");
			print("<OPTION VALUE=\"N\">No\n");	
		}
		print("</SELECT>\n");
		
		/* Output all the variations. */
		$Query = "SELECT a.Name, v.ID, v.Name ";
		$Query .= "FROM attribute a, variation v ";
		$Query .= "WHERE v.Attribute = a.ID ";
		$Query .= "ORDER BY a.DisplayPrecedence, a.Name, " . 
			"v.DisplayPrecedence, v.Name";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		$last_attribute = "";
		while($DatabaseRow = mysql_fetch_row($DatabaseResult))
		{
			$attribute_Name = $DatabaseRow[0];
			$variation_ID = $DatabaseRow[1];
			$variation_Name = $DatabaseRow[2];
			if($last_attribute != $attribute_Name)
			{
				if($last_attribute != "")
				{
					print("</TD></TR>\n");
				}
				print("<TR><TD VALIGN=\"top\">" . prepareText($attribute_Name) . 
					"</TD>\n<TD>");
				$last_attribute = $attribute_Name;
			}
			print("<INPUT TYPE=\"checkbox\" NAME=\"inputVariation[]\" ");
			print("VALUE=\"$variation_ID\"");
			if($sku_variation[$variation_ID])
			{
				print(" CHECKED");
			}
			print(">");
			print(prepareText($variation_Name) . " &nbsp; ");
		}

		/* Finish up the form and the "organizational" table. */
		print("<TR><TD>\n");
		print("<INPUT TYPE=\"submit\" VALUE=\"" . L_EDITSKU_UPDATESKU . "\">\n");
		print("<INPUT TYPE=\"reset\" VALUE=\"" . L_EDITSKU_RESET . "\">\n");
		print("</TD>\n");
		
		print("<TD ALIGN=\"right\">\n");
		print("<A HREF=\"" . ScreenURL("edit_item") . 
			"&amp;item=" . intval($item) . "&amp;ACTION=DELETE_SKU&amp;sku=" . 
			intval($sku) . "\">" . L_EDITSKU_DELETESKU . "<A>");
		print(", &nbsp; ");
		
		print("<A HREF=\"" . ScreenURL("admin_item") . 
			"&amp;item=" . intval($item) . "\">" . L_EDITSKU_CANCEL . "</A>\n");
		print("</TD></TR>\n");
		
		print("</TABLE>\n");	
		print("</FORM>\n");
		
		/* Finish up the outter table. */
		print("</TABLE>\n");
	}
	
?>
