<?php
	/*
	** File: DELETE_ITEM
	** Description: Removes an item and all SKUs
	** Version: $Revision: 1.9 $
	** Created: 04/21/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:20 $
	**
	** Log : $Log: DELETE_ITEM,v $
	** Log : Revision 1.9  1999/12/09 23:23:20  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.8  1999/11/09 22:21:14  leon
	** Log : More localization work.
	** Log :
	** Log : Revision 1.7  1999/10/14 23:35:45  jj
	** Log : Fixed a bug in my code: stuff was not being deleted from sku_variation when
	** Log : items were deleted.
	** Log :
	** Log : Revision 1.6  1999/08/18 22:31:48  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.5  1999/08/04 01:05:54  jj
	** Log : More item and department admin stuff: mostly adding stuff to DELETE scripts.
	** 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:12  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_DELETEITEM_DENIED;
		$continueScript = FALSE;
	}
	
	
	
	/*
	** Check for valid input. 
	*/
	if ( $continueScript )
	{
		if ( !isset($item) || $item<=0 )
		{
			$ActionResults[] = L_DELETEITEM_NEEDITEM;
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** In order to delete rows from the inventory table, we need to have
	** the list of skus that are children of this item.
	*/
	if ($continueScript) 
	{
		$listQuery = "SELECT ID FROM sku WHERE Item=" . intval($item);
		if ( !($listRecords = mysql_query($listQuery, $DatabaseLink)) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was $listQuery";
			$continueScript = FALSE;
		}
		else
		{
			/* Generate comma separated list. */
			if ( list($id) = mysql_fetch_row($listRecords) )
			{
				$skuList = $id;
				while ( list($id) = mysql_fetch_row($listRecords) )
				{
					$skuList .= ", $id";
				}
			}
		}
	}
	
	
	
	/*
	** For each sku that is going to be deleted, delete the corresponding 
	** row from the inventory table.
	*/
	if ($continueScript && isset($skuList))
	{
		$inventoryQuery = "DELETE FROM inventory WHERE SKU IN (" . 
			$skuList . ")";
		if ( !mysql_query($inventoryQuery, $DatabaseLink) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was $inventoryQuery";
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** Delete SKUs.
	*/
	if ($continueScript && isset($skuList))
	{
		$Query = "DELETE FROM sku WHERE Item=" . intval($item);
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query";
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** Delete from sku_variation for each of the skus.
	*/
	if ($continueScript && isset($skuList))
	{
		$Query = "DELETE FROM sku_variation ";
		$Query .= "WHERE SKU IN (" . $skuList . ")";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query";
			$continueScript = FALSE;
		}
	}
	


	/*
	** Delete item to department associations.
	*/
	if ($continueScript)
	{
		$Query = "DELETE FROM department_item WHERE Item=" . intval($item);
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query";
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** Delete item.
	*/
	if ($continueScript)
	{
		$Query = "DELETE FROM item WHERE ID=" . intval($item);
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query";
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** If everything has gone well, give the user a happy message, and send 
	** him to an appropriate screen.
	*/
	if ($continueScript)
	{
		$ActionResults[] = L_DELETEITEM_SUCCESS;
		$SCREEN = "admin_departments";
		/* $department will hopefully have been forwarded. */
	}
	
?>
