/*****************************************************************************
*
* Filename         - $Workfile:   grant_by_flight.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         - grant_by_flight stored procedure
*
* Language         - Oracle SQL*Plus script
*
* Classification   - Commercial In Confidence
*
*          (c) Ultra Electronics Limited, 1998
*
*****************************************************************************/
/****************************************************************************
* @(#)$Workfile:   grant_by_flight.sql  $  $Revision:   1.2  $
*
* 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:   /pvcsarch/db_ora/db/source/live/procedures/grant_by_flight.svl  $
* 
*    Rev 1.2   20 Sep 1999 14:41:22   tagtrak
*  
* 
*    Rev 1.1   02 Apr 1998 19:36:30   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.0   26 Feb 1998 10:21:06   tagtrak
* OR:
* CR:
* REV:
* Initial core product version
*
****************************************************************************/

/*
 * grant_by_flight - grant access for a specified flight.
 * This procedure will extract all flight_privilege records which
 * are relevant to the flight, and add flight_access and stats_access records
 * accordingly. Note the implication of this is that there could
 * be more than one such record with the same flight_id and
 * agent_code, but with different flight_priv_id's.
 */
PROMPT create or replace procedure grant_by_flight 

create or replace procedure grant_by_flight (
	f_fltid   in  flight.flight_id%type,
	f_fltno   in  flight.flight_no%type,
	f_fltdate in  flight.flight_date%type,
  psiteid   in  flight.site_id%type,
	f_alpha   in  flight.carrier_alpha_code%type
)
as

	counter number;

	cursor relevant_priv_records is
		select
			id,
			f.agent_code,
			originating_agent
		from
			flight_privileges f, agents a
		where
      f.agent_code  = a.agent_code and
      a.site_id           = psiteid      and
			rtrim(airline_alpha_code) = rtrim(f_alpha) and
			(
				( flight_no = f_fltno and flight_date = f_fltdate ) or
				( flight_no = f_fltno and flight_date is NULL ) or
				( flight_no is NULL and flight_date = f_fltdate ) or
				( flight_no is NULL and flight_date is NULL )
			);

begin

	/*
	 * For this flight, fetch every flight_privilege record
	 * which is relevant, and add corresponding flight_access
	 * and stats_access records.
	 */
	for priv in relevant_priv_records loop

		add_flt_access( f_fltid, priv.agent_code, priv.id );

		add_stats_access( priv.originating_agent, priv.agent_code,
			f_fltno, f_fltdate );

	end loop;

end grant_by_flight;
/
