<?php
	/*
	** File: DELETE_DEPARTMENT
	** Description: Removes a department and all items/SKUs
	** Version: $Revision: 1.7 $
	** Created: 06/21/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/09 23:23:20 $
	**
	** Log : $Log: DELETE_DEPARTMENT,v $
	** Log : Revision 1.7  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.6  1999/11/23 18:47:16  jj
	** Log : Fixed missing semi-colon.
	** Log :
	** Log : Revision 1.5  1999/11/09 22:21:14  leon
	** Log : More localization work.
	** Log :
	** Log : Revision 1.4  1999/10/16 01:21:46  jj
	** Log : Departments may no longer be deleted if they have items in them, or if they
	** Log : have sub-departments.
	** Log :
	** Log : Revision 1.3  1999/08/18 22:31:48  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.2  1999/08/03 00:28:05  jj
	** Log : Added stricter error checking and more friendly SCREEN redirection.
	** Log :
	** Log : Revision 1.1  1999/07/28 21:16:41  leon
	** Log : New actions based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	$ActionResults[] = "Boogy, Boogy! ";
	$continueScript = TRUE;


	/* 
	** Make sure the user has admin rights. 
	*/
	if( !$UserInfo["Permission"]["Administrate"] )
	{
		$ActionResults[] = L_DELETEDEPT_DENIED;
		$continueScript = FALSE;
	}
	
	
	
	/*
	** Check to make sure the department variable is set.
	*/
	if ( $continueScript && !isset($department) )
	{
		$ActionResults[] = L_DELETEDEPT_NEEDDEPT;
		$continueScript = FALSE;
	}
	else
	{
		/* Go ahead and cast $department. */
		$department = intval($department);
	}
	
	
	
	/*
	** In the event that we're successful, we'll want to return the 
	** user to this department's parent. Figure out who that is.
	*/
	if ($continueScript)
	{
		$parentQuery = "SELECT Parent FROM department ";
		$parentQuery .= "WHERE ID = $department";
		if ( !($parentRecords = mysql_query($parentQuery, $DatabaseLink)) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() .  
				"   Query was: $parentQuery";
			$continueScript = FALSE;
		}
		else
		{
			switch (mysql_num_rows($parentRecords))
			{
				case 0: 
					$ActionResults[] = L_DELETEDEPT_NOTFOUND;
					$continueScript = FALSE;
					break;
				case 1:
					list($departmentParent) = mysql_fetch_row($parentRecords);
					break;
				default:
					$ActionResults[] = 
						"There are multiple departments with the same " . 
						"department ID. How in the world did that happen?";
					$continueScript = FALSE;
					break;
			}
		}
	}



	/*
	** Check if there are any items in this department.
	*/
	if ($continueScript) 
	{
		$Query = "SELECT count(*) ";
		$Query .= "FROM department_item ";
		$Query .= "WHERE Department = $department";
		if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() .  
				"   Query was: $Query";
			$continueScript = FALSE;
		}
		else
		{
			list($ItemCount) = mysql_fetch_row($DatabaseResult);
			if($ItemCount > 0)
			{
				$ActionResults[] = L_DELETEDEPT_NOTEMPTY;
				$continueScript = FALSE;
			}
		}
	}
	
	
	
	/*
	** Make sure there are no sub-departments.
	*/
	if ($continueScript)
	{
		$Query = "SELECT count(*) FROM department ";
		$Query .= "WHERE Parent=$department";
		if( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() .  
				"   Query was: $Query";
			$continueScript = FALSE;
		}
		else
		{
			list($numChildren) = mysql_fetch_row($DatabaseResult);
			if ($numChildren)
			{
				$ActionResults[] = L_DELETEDEPT_HASCHILDREN;
				$continueScript = FALSE;
			}
		}
	}



	/*
	** If everything is okay at this point, we may go ahead and delete the 
	** department.
	*/
	if ($continueScript)
	{
		$Query = "DELETE FROM department WHERE ID=$department";
		if( !mysql_query($Query, $DatabaseLink) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() .  
				"   Query was: $Query";
			$continueScript = FALSE;
		}
		else
		{
			$ActionResults[] = L_DELETEDEPT_SUCCESS;
		}
	}
	
	
	
	/*
	** Send the user back to the appropriate screen.
	*/
	$SCREEN = "admin_departments";
	if ($continueScript)
	{
		$department = $departmentParent;
	}
	
?>
