/*****************************************************************************
*
* Filename         - $Workfile:   contracted_agents_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         - Triggers for contracted_agents table.
*
* Language         - Oracle SQL*Plus script
*
* Classification   - Commercial In Confidence
*
*          (c) Ultra Electronics Limited, 1998
*
*****************************************************************************/
/****************************************************************************
* @(#)$Workfile:   contracted_agents_triggers.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/triggers/contracted_agents_triggers.svl  $
* 
*    Rev 1.2   06 Jun 2000 09:56:30   tagtrak
*  
* 
*    Rev 1.1   02 Apr 1998 19:46:00   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.0   26 Feb 1998 11:43:32   tagtrak
* OR:
* CR:
* REV:
* Initial core product version
*
****************************************************************************/

create or replace trigger tbi_contracted_agents
before insert on contracted_agents
for each row
declare
numrows integer;
begin

	if ((:new.min_flight_no is not NULL and :new.min_flight_no <> 0) or
	    (:new.max_flight_no is not NULL and :new.max_flight_no <> 9999) ) then
			select count(*) into numrows
			from carrier_subgroup 
			where 
			:new.AIRLINE_ALPHA_CODE = alpha_code and
			:new.min_flight_no = min_flight_no and
			:new.max_flight_no = max_flight_no;
			if (numrows <> 1) then
			raise_application_error( -20039, 'flight number range invalid or overlap');
			end if;
	end if;

end;
/


/*
 * After insert trigger on contracted_agents
 * Log an audit record.
 */
create or replace trigger ti_contracted_agents
after insert on contracted_agents
for each row
begin

	/*
	 * Maintain audit trail.
	 */
	add_log_rec( 'CONTRACTED_AGENTS',
		'insert: Agent Code: '||rtrim(:new.agent_code)||
		'. Airline: '||rtrim(:new.airline_alpha_code)||
		'. Archive Flag: '||:new.archive_flag||'. Internal User: '||USER,
		:new.last_user );

end;
/

/*
 * After update trigger on contracted_agents
 * Log an audit record.
 */
create or replace trigger tu_contracted_agents
after update on contracted_agents
for each row
begin

	/*
	 * Maintain audit trail.
	 */
	add_log_rec( 'CONTRACTED_AGENTS',
		'update: (old values): Agent Code: '||rtrim(:old.agent_code)||
		'. Airline: '||rtrim(:old.airline_alpha_code)||
		'. Archive Flag: '||:old.archive_flag||'. Last user: '||:old.last_user||
		'. (new values): Agent Code: '||rtrim(:new.agent_code)||
		'. Airline: '||rtrim(:new.airline_alpha_code)||
		'. Archive Flag: '||:new.archive_flag||'. Internal User: '||USER,
		:new.last_user );

end;
/

/*
 * After delete trigger on contracted_agents
 * Log an audit record.
 */
create or replace trigger td_contracted_agents
after delete on contracted_agents
for each row
begin

	/*
	 * Maintain audit trail. The user id is unknown in this context.
	 */
	add_log_rec( 'CONTRACTED_AGENTS',
		'delete: Agent Code: '||rtrim(:old.agent_code)||
		'. Airline: '||rtrim(:old.airline_alpha_code)||
		'. Archive Flag: '||:old.archive_flag||'. Last user: '||
		:old.last_user||'. Internal User: '||USER,
		NULL );

end;
/
