<?php
	/*
	** File: ADD_SKU
	** Description: Adds a SKU to an item
	** Version: $Revision: 1.11 $
	** Created: 04/13/1999
	** Author: Leon Atkinson, Shannon -jj Behrens
	** Email: leon@clearink.com, jj@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/09 23:23:17 $
	**
	** Log : $Log: ADD_SKU,v $
	** Log : Revision 1.11  1999/12/09 23:23:17  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.10  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.9  1999/11/13 00:31:01  leon
	** Log : Undoing JJ's change...
	** Log :
	** Log : Revision 1.7  1999/11/09 21:47:11  leon
	** Log : More localization.
	** Log :
	** Log : Revision 1.6  1999/08/18 22:31:45  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:11  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_ADDSKU_DENIED;
		$continueScript = FALSE;
	}
	
	
	
	/*
	** Make sure we have some form of valid data.
	*/
	if ($continueScript)
	{
		/* Uncomment this to force admins to name SKUs.
			if (!isset($inputName) || $inputName=="")
			{
				$ActionResults[] = L_ADDSKU_NEEDNAME;
				$continueScript = FALSE;
			}
			else 
		*/ 
		if (!isset($item) || $item<=0)
		{
			$ActionResults[] = L_ADDSKU_BADITEM;
			$continueScript = FALSE;
		}
		/* Everything seems okay, lets do some casts. */
		else
		{
			$inputListPrice = abs((float)$inputListPrice);
			$inputSalePrice = abs((float)$inputSalePrice);
			$inputShipping = abs((float)$inputShipping);		
		}
	}



	/*
	** Make sure that this is not a repeat entry. Anything with the same name
	** external sku, and item is deemed to be a repeat entry.
	*/
	if ($continueScript)
	{
		$repeatEntryQuery = "SELECT ID FROM sku WHERE ";
		$repeatEntryQuery .= "Name = '" . addslashes($inputName) . "' ";
		$repeatEntryQuery .= "AND Item = " . intval($item) . " ";
		$repeatEntryQuery .= "AND ExternalSKU = '" . 
			addslashes($inputExternalSKU) . "'";
		if ( !($repeatEntryRecords = mysql_query($repeatEntryQuery, 
			$DatabaseLink)) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"  Query was $repeatEntryQuery";
			$continueScript = FALSE;
		}
		else
		{
			if (list($ID) = mysql_fetch_row($repeatEntryRecords))
			{
				$ActionResults[] = L_ADDSKU_DUPLICATE . 
					"<A HREF=\"" . ScreenURL("edit_sku") . 
					"&sku=$ID\">" . prepareText($inputName) . "</A>.";
				$continueScript = FALSE;
			}
		}
	}
	
	
	
	/*
	** Insert a new row into sku. 
	*/
	if ($continueScript)
	{
		$Query = "INSERT INTO sku (Item, ExternalSKU, Name, ListPrice, " . 
			"SalePrice, Shipping, DisplayPrecedence, Active) ";
		$Query .= "VALUES ( ";
		$Query .= intval($item) . ", ";
		$Query .= "'" . addslashes($inputExternalSKU) . "', ";
		$Query .= "'" . addslashes($inputName) . "', ";
		$Query .= "$inputListPrice, ";
		$Query .= "$inputSalePrice, ";
		$Query .= "$inputShipping, ";
		$Query .= intval($inputDisplayPrecedence) . ", ";
		$Query .= "'" . ($inputActive=="Y" ? "Y" : "N") . "'";
		$Query .= ")";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"  Query was $Query";
			$continueScript = FALSE;
		}
		else
		{
			$newID = mysql_insert_id($DatabaseLink);
		}
	}
	
	
	
	/*
	** Insert new variations.
	*/
	if ($continueScript)
	{
		for($index = 0; $index < count($inputVariation); $index++)
		{
			$Query = "INSERT INTO sku_variation VALUES (";
			$Query .= intval($newID) . ", ";
			$Query .= intval($inputVariation[$index]) . ")";
			if(!mysql_query($Query, $DatabaseLink))
			{
				$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
					"  Query was $Query";
				$continueScript = FALSE;
			}
		}
	}
	
	
	
	/*
	** Insert inventory row.
	*/
	if ($continueScript)
	{
		$Query = "INSERT INTO inventory VALUES (";
		$Query .= "$newID, "; 
		$Query .= intval($inputAvailable) . ", ";
		$Query .= intval($inputReserve) . ", ";
		$Query .= "'" . ($inputBackorder == "Y" ? "Y" : "N") . "' ) ";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"  Query was $Query";
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** Send the user back to the appropriate place. 
	*/
	if ($continueScript) 
	{
		$SCREEN = "admin_item";
		/* $item should be set */
	}
	else
	{
		if ( isset($item) )
		{
			$SCREEN = "add_sku";
		}
		else
		{
			/* Make them start over. */
			$SCREEN = "admin_menu";
		}
	}

?>
