/*****************************************************************************
*
* Filename         - $Workfile:   mail_bpm3.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:   mail_bpm3.sql  $  $Revision:   1.5  $
*
* 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/mail_bpm3.sqv  $
* 
*    Rev 1.5   02 Apr 1998 19:36:50   tagtrak
* OR:
* CR:
* REV:
* Initial Issue
* See label for concession
* 
*    Rev 1.4   26 Feb 1998 10:14:14   tagtrak
* OR:
* CR:
* REV:
* Initial core product version
* 
*    Rev 1.3   15 Jan 1998 12:13:26   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:15:46   tagtrak
* Added PVCS header information
*
****************************************************************************/


PROMPT create or replace procedure mail_bpm3 

create or replace procedure mail_bpm3 (
	fid    in out  number,
	bpmid  in out  number,
	uld    in out  char,
	btype  in out  char,
	retval in out  number
) 
as
	l_proc  system_cfg.process_name%TYPE; 
	l_unit  system_cfg.unit_name%TYPE; 
	l_dcsid system_cfg.dcsid%TYPE; 
	l_sub   system_cfg.subunit_name%TYPE; 
	l_addr  mail_indiv.msg_address%TYPE; 
	l_mesg  mail_indiv.msg_text%TYPE;
begin

	/*
	 * - Initialise Variable
	 * - Select columns from the flight table where the flight_id is equal to
	 *    the fid parameter.  If no data is found the assign -1015 to the return
	 *    value.
	 * - If the return value is 0 (ie. some data has been retrieved) then
	 *   Format the address and message.
	 *   Call the store_mail procedure.
	 */

	retval := 0;

	/* this is a bodge be careful */
	begin
		select
			rtrim(process_name),
			unit_name,
			dcsid,
			subunit_name 
		into
			l_proc,
			l_unit,
			l_dcsid,
			l_sub 
		from
			flight
		where
			flight_id = fid;
		exception when NO_DATA_FOUND then
			retval := -1015;
		end;
																														
		if (retval = 0) then 
			/* will only be run if the BPM status == "Y" */
			l_addr := RTRIM(l_proc)||RTRIM(l_unit);
			l_mesg := RTRIM(l_dcsid)||','||RTRIM(l_sub)||','||btype||','||to_char(fid)||','||
					to_char(bpmid)||','||uld;

			mail_pack.store_mail (
				l_addr,
				'BPM',
				l_mesg,
				1,
				'I',
				retval
			);
		end if;

end mail_bpm3;
/
