/*****************************************************************************
*
* Filename         - $Workfile:   offload_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:   offload_bag.sql  $  $Revision:   1.6  $
*
* 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/aisarch/db_ora/db/source/live/procedures/offload_bag.sqv  $
* 
*    Rev 1.6   01 Aug 2002 14:01:52   yomi
* DESC: Timezone change
* CR:   15508
* 
*    Rev 1.5   02 Apr 1998 19:37:22   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.4   15 Jan 1998 12:13:50   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:18:32   tagtrak
* Added PVCS header information
*
****************************************************************************/

PROMPT create or replace procedure offload_bag

create or replace procedure offload_bag (
	tag2a           in out  char,
	tag2b           in out  char,
	tag3            in out  number,
	pid             in out  number,
	uldno           in out  char,
	p_location_code in out  char,
	p_scanner_id    in      char,
	fltid           in out  number ,
	retval          in out  number
) 
as
	nbags number(6);
  v_pid    pax.pax_id%type;
  depair      pax.departure_airport_code%type := null; 
begin

  depair := get_offset.find_airport_code(fltid,pid);

	/*
	 * - Initialise Variables
	 * - Call the remove_bag_from_uld procedure
	 * - IF the number of bags is greater than or equal to zero then
	 *   IF we have a bar code representation then
	 *     Update the bag table by setting the uld_no, uld_flight_id and
	 *      bag_sequence_no to NULL, loaded_status to 'U', time_loaded to
	 *      the system date, location_code equal to the p_location_code
	 *      parameter and the scanner_id equal to the p_scanner_id
	 *      parameter, where the tags and the pax's are the same.
	 *   ELSE
	 *     Update the bag table by setting the uld_no, uld_flight_id and
	 *      bag_sequence_no to NULL, loaded_status to 'U', time_loaded to
	 *      the system date, location_code equal to the p_location_code
	 *      parameter and the scanner_id equal to the p_scanner_id
	 *      parameter, where the tags and the pax's are the same.
	 * - ELSE
	 *   Assign -1013 to the return value
	 */

	nbags  := 0;
	retval := 0;
        

		if (tag2b != '-') then 
			/* use bar code representation */
			update
				bag
			set 
				uld_no          = NULL,
				uld_flight_id   = NULL,
				loaded_status   = 'U',
				bag_sequence_no = NULL,
				time_loaded     = SYSDATE + get_offset.airport_code(depair),
				location_code   = p_location_code,
				scanner_id      = p_scanner_id,
				hbmd_flag       = 'Y'
			where
				bag_tag3  = tag3  and
				bag_tag2b = tag2b and
				pax_id    =  pid  AND
				uld_no    = uldno;
		else 
			 /* use alpha */
			update
				bag
			set 
				uld_no          = NULL,
				uld_flight_id   = NULL,
				loaded_status   = 'U',
				bag_sequence_no = NULL,
				time_loaded     = SYSDATE + get_offset.airport_code(depair),
				location_code   = p_location_code,
				scanner_id      = p_scanner_id,
				hbmd_flag       = 'Y'
			where
				bag_tag3  = tag3  and
				bag_tag2a = tag2a and
				pax_id    =  pid  AND
				uld_no    = uldno;
		end if;

	remove_bag_from_uld ( fltid, uldno, nbags);
	if (nbags < 0) then
		/* uld error */ 
		retval := -1013;
	end if;

end offload_bag;
/
