/*****************************************************************************
*
* Filename         - $Workfile:   uld_trace_triggers.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: triggers for uld_trace table
*
* Language         - Oracle SQL*Plus script
*
* Classification   - Commercial In Confidence
*
*          (c) Ultra Electronics Limited, 1997
*
*****************************************************************************/
/****************************************************************************
* @(#)$Workfile:   uld_trace_triggers.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:   /pvcsarch/db_ora/db/source/live/triggers/uld_trace_triggers.svl  $
* 
*    Rev 1.4   21 Jun 2001 15:04:50   tagtrak
* OR:
* CR:
* Re-baseline version 4.0.0 to include patches.
* 
*    Rev 1.3   02 Apr 1998 19:46:34   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.2   26 Feb 1998 11:42:58   tagtrak
* OR:
* CR:
* REV:
* Initial core product version
* 
*    Rev 1.1   15 Jan 1998 13:39:18   tagtrak
* OR: OR11101, TN13, TN15
* CR:
* REV:
* Phase 2C to Phase 3 upgrade plus code review actions and resolving some technical issues.
* 
* 
*    Rev 1.0   22 Oct 1997 16:38:46   tagtrak
* OR:
* CR:
* REV:
* Initial check-in
*
****************************************************************************/



create or replace trigger ti_uld_trace
before insert on uld_trace
for each row

declare
	in_location number;
	in_flight   number;
  psiteid     site_info.site_id%type;
begin
  :new.airport_code := multi_site.get_user_loc(:new.user_id);

  select site_id
    into psiteid
    from agents a , tagtrak_users t
  where  a.agent_code = t.agent_code
    and  t.user_id    = :new.user_id;  
  

	/*
	 * Before an insert on the uld_trace table :
	 *  - Initialise variables
	 *  - If location_code is NOT NULL
	 *     count the number of locations for the given location_code
	 *     - If location code is 0, count for the flight_no
	 *   - if no locations or flights found, then error.
	 *  - Get the next record_id
	 */

	in_location := 0;
	in_flight   := 0;

	if ( :new.uld_location_code is not null ) then

		select
			count(*)
		into
			in_location
		from
			location
		where
			location_code = :new.uld_location_code
     and site_id    = psiteid;

		if ( in_location = 0 ) then

			select
				count(*)
			into
				in_flight
			from
				flight_outbound_v1
			where
				flight_no = :new.uld_location_code;

		end if;

		if ( in_location = 0 and in_flight = 0 ) then
			raise_application_error( -20032, 'ULD Trace location is non-valid.' );
		end if;

	end if;

 
	select
		uld_trace_id.nextval
	into
		:new.record_id
	from
		dual;
	
end;
/

create or replace trigger tu_uld_trace
before update on uld_trace
for each row

declare
	in_location number;
	in_flight number;
   psiteid     site_info.site_id%type;

begin

	/*
	 * Before an update on the uld_trace table :
	 *  - Initialise variables
	 *  - If location_codes are not the same then
	 *     count the number of locations for the given location_code
	 *     - If location code is 0, count for the flight_no
	 *   - if no locations or flights found, then error.
	 */

	in_location := 0;
	in_flight   := 0;

  select site_id
    into psiteid
    from agents a , tagtrak_users t
  where  a.agent_code = t.agent_code
    and  t.user_id    = :old.user_id;

	if ( :new.uld_location_code != :old.uld_location_code ) then

		select
			count(*)
		into
			in_location
		from
			location
		where
			location_code = :new.uld_location_code
     and site_id    = psiteid;

		if ( in_location = 0 ) then

			select
				count(*)
			into
				in_flight
			from
				flight_outbound_v1
			where
				flight_no = :new.uld_location_code;

		end if;

		if ( in_location = 0 and in_flight = 0 ) then
			raise_application_error( -20032, 'ULD Trace location is non-valid.' );
		end if;

	end if;

end;
/
