/*****************************************************************************
*
* Filename         - $Workfile:   mba_new_flights.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:   mba_new_flights.sql  $  $Revision:   1.4  $
*
* 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/mba_new_flights.sqv  $
* 
*    Rev 1.4   02 Apr 1998 19:37:02   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.3   15 Jan 1998 12:13:34   tagtrak
* OR: OR11101, TN13, TN15
* CR:
* REV:
* Phase 2C to Phase 3 upgrade plus code review actions and resolving some technical isses.
* 
*    Rev 1.2   24 Oct 1997 11:16:44   tagtrak
* Added PVCS header information
*
****************************************************************************/


PROMPT create or replace procedure mba_new_flights

create or replace procedure mba_new_flights (ldtime in varchar2) 
as
  
begin

	/*
	 * - Insert flight_id, first_time, delete_marker into the mba_flights table
	 *    from the flight table where the departure time is less than the
	 *    ldtime parameter, the archive_flag is set to 0 and the flight_id is
	 *    not already in the mba_flights table.
	 * - Now delete from the mba_flights table where the flight_id is in the
	 *    flight table and the departure_time is greater than or equal to
	 *    the ldtime parameter and the archive_flag is 0.
	 */

	/* flights now in the time window,
	ie. time is now 3 hrs window period (default 3 hrs) before STD */

	insert into
		mba_flights (
			flight_id,
			first_time,
			delete_marker
		)

	select
		flight_id,
		1,
		archive_flag
	from
		flight_outbound_v1
	where
		departure_time < ldtime and
		archive_flag   = 0      and
		flight_id not in (
			select
				flight_id
			from
				mba_flights
			);

	/* flights now out of the time window,
	maybe because delayed (STD now later) */
														
	delete from
		mba_flights
	where
		flight_id in (
		select
			flight_id
		from
			flight_outbound_v1
		where
			departure_time >= ldtime and
			archive_flag    = 0
		);

	end mba_new_flights;
/
