/*****************************************************************************
*
* Filename         - $Workfile:   delete_bag.sql  $
*
*                     Name              Date      Version     CR Number
* Author           - See revision history below.
* Last Update      - See revision history below.
*
* Project Name     - TAGTRAK(Oracle Database)
*
* Source Code
* Identifier       - 0/3/300/8010/112
*
* Design Spec
* Section          - 
*
* Contents         - Live Schema Stored Procedure
*
* Language         - Oracle SQL*Plus script
*
* Classification   - Commercial In Confidence
*
*          (c) Ultra Electronics Limited, 1997
*
*****************************************************************************/
/****************************************************************************
* @(#)$Workfile:   delete_bag.sql  $  $Revision:   1.5  $
*
* Revision History
*
* Note that user tagtrak is many users. For these users the user
* name is entered as the last line in the change description.
*
* $Log:   /shared/pvcs/tagtarch/db_ora/db/source/live/procedures/delete_bag.sqv  $
* 
*    Rev 1.5   02 Apr 1998 19:32:46   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.4   15 Jan 1998 12:11:40   tagtrak
* OR: OR11101, TN13, TN15
* CR:
* REV:
* Phase 2C to Phase 3 upgrade plus code review actions and resolving some technical isses.
* 
*    Rev 1.3   24 Oct 1997 11:03:34   tagtrak
* Added PVCS header information
*
****************************************************************************/


PROMPT create or replace procedure delete_bag

create or replace procedure delete_bag (
	paxid      in      number,
	tag3       in      number,
	tag2a      in      char,
	tag2b      in      char,
	nobags        out  number
)
as 

	tsts char(1); 
	i    number(10);

begin

	/*
	 * - Initialise variables
	 * - Select the loaded status from the bag table where the pax_id is
	 *   equal to the pax_id passed in and the bag_tag3 is equal to tag3
	 *   tag2b is not = '-' and bag_tag2b = tag2b, or tag2a is not = '-' and
	 *   bag_tag2a = tag2a and delete marker != 1.  If no records have been
	 *   found then tsts = '-'.
	 * - If tsts != '-' then
	 *   nobags = 0
	 *   update the bag table and set the delete marker to 1
	 *   Update the pax table and decrement the no_of_bags by 1 where
	 *    the pax_ids are the same.
	 *   IF tsts = 'Y' then
	 *     nobags = 1
	 */

	tsts   := '-';
	nobags := -1;  /* found no bags */
 
	begin

		select
			loaded_status 
		into
			tsts 
		from
			bag
		where
			pax_id =  paxid  and
			bag_tag3 =  tag3 and (
				(nvl(tag2b,'-') != '-' and bag_tag2b = tag2b) or
				(nvl(tag2a,'-') != '-' and bag_tag2a = tag2a)
			) and
			delete_marker != 1; 

		exception
			when NO_DATA_FOUND then
				tsts := '-';
	end;

	if (tsts != '-') then 
		nobags := 0; /* found a bag */
 
		/* modify record to set delete flag */
		update
			bag
		set 
			delete_marker = 1
		where
			pax_id  =  paxid and
			bag_tag3 =  tag3 and (
				(nvl(tag2b,'-') != '-' and bag_tag2b = tag2b) or
				(nvl(tag2a,'-') != '-' and bag_tag2a = tag2a)
			);


		/* update the pax record bag count */
		update
			pax
		set 
			no_of_bags = no_of_bags-1
		where
			pax_id =  paxid; 

		if (tsts = 'Y') then 
			nobags := 1; /* it was loaded */
			/*
			 * In the case of deleting a loaded bag, the unload
			 * alert is raised by the bag update trigger.
			 */
		end if;

	end if;
 
end delete_bag;
/
