<?php
	/*
	** File: UPDATE_SKU
	** Description: Updates a SKU.
	** Version: $Revision: 1.13 $
	** 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 02:00:33 $
	**
	** Log : $Log: UPDATE_SKU,v $
	** Log : Revision 1.13  2000/01/28 02:00:33  jj
	** Log : When SKUs are updated, the admin is now transferred to edit_sku rather than admin_item.
	** Log :
	** Log : Revision 1.12  1999/12/09 23:23:26  leon
	** Log : Changed ActionResults use so that it's an array instead of a
	** Log : continually-growing string.  Note the little function in standard_library
	** Log : for printing out all the action results.
	** Log :
	** Log : Revision 1.11  1999/11/24 19:47:55  jj
	** Log : Minor changes for adding SKUs--no longer requiring a name, may now have skus
	** Log : with same name as long as they aren't within the same item.
	** Log :
	** Log : Revision 1.10  1999/11/13 00:31:01  leon
	** Log : Undoing JJ's change...
	** Log :
	** Log : Revision 1.8  1999/10/14 23:34:28  jj
	** Log : Found a typo.
	** Log :
	** Log : Revision 1.7  1999/10/14 21:38:14  leon
	** Log : Started pulling text out of action modules
	** Log :
	** Log : Revision 1.6  1999/08/18 22:31:56  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.5  1999/08/03 22:51:55  jj
	** Log : Doing quality control on the department and item editors.
	** Log :
	** Log : Revision 1.4  1999/08/03 00:28:05  jj
	** Log : Added stricter error checking and more friendly SCREEN redirection.
	** Log :
	** Log : Revision 1.3  1999/07/28 19:27:16  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"] )
	{
		$ActionResults[] = L_UPDATESKU_ACCESS_DENIED;
		$continueScript = FALSE;
	}
	
	
	
	/*
	** Make sure we have some form of valid data. We want at least the 
	** following: $item, $sku, and $inputName.
	*/
	if ($continueScript)
	{
		if (!isset($item) || $item<=0)
		{
			$continueScript = FALSE;
			$ActionResults[] = L_UPDATESKU_MISSING_ITEM;
			$SCREEN = "admin_menu";
		}
		else if (!isset($sku) || $sku<=0)
		{
			$continueScript = FALSE;
			$ActionResults[] = L_UPDATESKU_MISSING_SKU;
			$SCREEN = "admin_menu";
		}
		/* Uncomment this to force admins to name SKUs.
			else if (!isset($inputName) || $inputName=="")
			{
				$continueScript = FALSE;
				$ActionResults[] = L_UPDATESKU_NEED_NAME;
				$SCREEN = "edit_sku";
			}
		*/
	}
	
	
	
	/*
	** Make sure that this is not a repeat entry--i.e. there had better be
	** no other SKUs within the same item that have the same name and external
	** SKU.
	*/
	if ($continueScript)
	{
		$repeatQuery = "SELECT ID FROM sku WHERE ";
		$repeatQuery .= "Item=" . intval($item) . " ";
		$repeatQuery .= "AND Name='" . addslashes($inputName) . "' ";
		$repeatQuery .= "AND ExternalSKU='" . addslashes($inputExternalSKU) . "'";
		if ( !($repeatRecords = mysql_query($repeatQuery, $DatabaseLink)) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was: $repeatQuery";
			$continueScript = FALSE;
		}
		else
		{
			while ( list($id) = mysql_fetch_row($repeatRecords) )
			{
				if ($id != $sku)
				{
					$ActionResults[] = L_UPDATESKU_NAME_USED . "<A HREF=\"" . 
						ScreenURL("edit_sku", FALSE, array("sku"=>$id)) . 
						"\">$inputName</A>.";
					$SCREEN = "edit_sku";
					$continueScript = FALSE;
				}
			}
		}
	}
	

	
	/*
	** Update the main information about this sku.
	*/
	if ($continueScript)
	{
		$Query = "UPDATE sku ";
		$Query .= "SET Item=" . intval($item) . ", ";
		$Query .= "ExternalSKU='" . addslashes($inputExternalSKU) . "', ";
		$Query .= "Name='" . addslashes($inputName) . "', ";
		$Query .= "ListPrice=" . doubleval($inputListPrice) . ", ";
		$Query .= "SalePrice=" . doubleval($inputSalePrice) . ", ";
		$Query .= "Shipping=" . doubleval($inputShipping) . ", ";
		$Query .= "DisplayPrecedence=" . intval($inputDisplayPrecedence) . ", ";
		$Query .= "Active='" . addslashes($inputActive) . "' ";
		$Query .= "WHERE ID=" . intval($sku);
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was: $repeatQuery";
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** Delete variations.
	*/
	if ($continueScript)
	{
		$Query = "DELETE FROM sku_variation WHERE SKU=" . intval($sku);
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was: $repeatQuery";
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** Insert new variations.
	*/
	if ($continueScript)
	{
		for($index = 0; $index < count($inputVariation); $index++)
		{
			$Query = "INSERT INTO sku_variation VALUES (";
			$Query .= intval($sku) . ", ";
			$Query .= intval($inputVariation[$index]);
			$Query .= ")";
			if(!mysql_query($Query, $DatabaseLink))
			{
				$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
					"   Query was: $repeatQuery";
				$continueScript = FALSE;
			}
		}
	}
	
	
	
	/*
	** Update inventory row.
	*/
	if ($continueScript)
	{
		$inputBackorder = $inputBackorder == "Y" ? "Y" : "N";
		$Query = "UPDATE inventory ";
		$Query .= "SET Available = " . intval($inputAvailable) . ", ";
		$Query .= "Reserve = " . intval($inputReserve) . ", ";
		$Query .= "Backorder = '$inputBackorder' ";
		$Query .= "WHERE SKU = " . intval($sku);
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was: $repeatQuery";
			$continueScript = FALSE;
		}
	}



	/*
	** Give the user a happy message and send him back to the appropriate
	** screen. If there was an error, all of this has already been taken
	** care of. 
	*/
	if ($continueScript)
	{
		$ActionResults[] = L_UPDATESKU_SUCESS;
		$SCREEN = "edit_sku";
	}
	
?>
