/*****************************************************************************
*
* Filename         - $Workfile:   location_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 location table
*
* Language         - Oracle SQL*Plus script
*
* Classification   - Commercial In Confidence
*
*          (c) Ultra Electronics Limited, 1997
*
*****************************************************************************/
/****************************************************************************
* @(#)$Workfile:   location_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/location_triggers.svl  $
* 
*    Rev 1.4   21 Jun 2001 15:04:28   tagtrak
* OR:
* CR:
* Re-baseline version 4.0.0 to include patches.
* 
*    Rev 1.3   02 Apr 1998 19:46:18   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.2   26 Feb 1998 11:42:32   tagtrak
* OR:
* CR:
* REV:
* Initial core product version
* 
*    Rev 1.1   15 Jan 1998 13:39:04   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:34   tagtrak
* OR:
* CR:
* REV:
* Initial check-in
*
****************************************************************************/



create or replace trigger ti_location
before insert on location
for each row
declare
	numrows integer;
begin

	/*
	 * - Check if we have a Location code and a location description
	 * - Insert details into the log table
	*/

	if (:new.location_code is null or :new.location_description is null) then
		raise_application_error( -20025, 'Location code and description must be supplied');
	end if;
	
	
	insert into log_table
		( user_id, reason, log_time )
	values
		( :new.last_user,
		'Location' || :new.location_code || ', '
		|| :new.location_description || ' added',
		SYSDATE );
end;
/

create or replace trigger tu_location
before update on location
for each row
begin

	/*
	 * - Check for location_code, location_description and last_user errors.
	 * - Set the archive_flag if it is NULL and the old flag is set.
	*/

	if (:old.location_code != :new.location_code) then
		raise_application_error( -20018, 'non modifiable column cannot be modified.');
	end if;
	
--	if (:old.location_description != :new.location_description) then
--		raise_application_error( -20018, 'non modifiable column cannot be modified.');
--	end if;
	
	
--	if (:old.last_user != :new.last_user) then
--		 raise_application_error( -20019, 'cannot modify parent code in child table.');
--	end if;
	
	
	if (:new.archive_flag is null and :old.archive_flag = 1) then
		:new.archive_flag := 2;
	end if;
end;
/

create or replace trigger td_location
before delete on location
for each row
begin

	/*
	 * - Before the user tries to delete a location, raise an error.
	*/

	raise_application_error( -20012, 'delete of location not allowed');
    
end;
/

