COMPILE FUNCTION obj_Tool_Esc(Method,Parms)
/*
	Methods for TOOL_ESC table
    
	09/26/2007 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Find()							;* Lookup Tool Escalation key
   
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Dialog_Box
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, Btree.Extract, ErrMsg
DECLARE SUBROUTINE  obj_Notes, Send_Info


$INSERT TOOL_ESC_EQUATES
$INSERT MSG_EQUATES
$INSERT NOTIFICATION_EQUATES

EQU CRLF$	TO \0D0A\

ErrTitle = 'Error in Stored Procedure "obj_Tool_Esc"'
ErrorMsg = ''

IF NOT(ASSIGNED(Method))	THEN ErrorMsg = 'Unassigned parameter "Method" passed to subroutine'
IF NOT(ASSIGNED(Parms))		THEN Parms = ''

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

Result = ''

BEGIN CASE
	CASE Method = 'Find'		; GOSUB Find
	CASE Method = 'Create'		; GOSUB Create
	CASE Method = 'Close'		; GOSUB Close
	
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to routine.'

END CASE

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

RETURN Result



* * * * * * *
Find:
* * * * * * *



ToolEscKeys = Dialog_Box( 'DIALOG_React_ESC_QUERY', @WINDOW, 'TOOL' )

Result = ToolEscKeys


RETURN




* * * * * * *
Create:
* * * * * * *

ToolID			= Parms[1,@RM]
StartDTM		= Parms[COL2()+1,@RM]
UserID			= Parms[COL2()+1,@RM]
Reason			= Parms[COL2()+1,@RM]


IF ToolID	= ''	THEN ErrorMsg = 'Null parameter ToolID passed to routine (':Method:').'
IF StartDTM = ''	THEN ErrorMsg = 'Null parameter StartDTM passed to routine (':Method:').'
IF UserID	= ''	THEN ErrorMsg = 'Null parameter UserID passed to routine (':Method:').'

IF ErrorMsg NE '' THEN RETURN

thisStartDTM = ICONV(StartDTM,'DT')
IF thisStartDTM = '' THEN
	ErrorMsg = 'Invalid value ':QUOTE(StartDTM):' passed for parameter StartDTM (':Method:').'
	RETURN
END

ToolEscKey = ToolID:'*':thisStartDTM

ToolEscRec = ''
ToolEscRec<TOOL_ESC_START_USER$>		= UserID
ToolEscRec<TOOL_ESC_START_REASON$>		= Reason

TableVar = ''

OtParms = 'TOOL_ESC':@RM:ToolEscKey:@RM:TableVar:@RM:ToolEscRec

obj_Tables('WriteRec',OtParms)		;* Writes and unlocks the record

Recipients		= XLATE('NOTIFICATION','TOOL_ESCALATION',NOTIFICATION_USER_ID$,'X')
SentFrom		= UserID
Subject			= 'Tool Escalation Opened'
Message			= "Tool ID ":ToolID:" escalation opened.":CRLF$:Reason
AttachWindow	= 'TOOL_ESC'
AttachKey		= ToolID:'*':StartDTM
SendToGroup		= ''

Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
obj_Notes('Create',Parms)


RETURN


* * * * * * *
Close:
* * * * * * *

ToolID		= Parms[1,@RM]
StartDTM	= Parms[COL2()+1,@RM]
StopDTM		= Parms[COL2()+1,@RM]
UserID		= Parms[COL2()+1,@RM]
Reason		= Parms[COL2()+1,@RM]


IF ToolID	= ''	THEN ErrorMsg = 'Null parameter ToolID passed to routine (':Method:').'
IF StartDTM = ''	THEN ErrorMsg = 'Null parameter StartDTM passed to routine (':Method:').'
IF StopDTM	= ''	THEN ErrorMsg = 'Null parameter StopDTM passed to routine (':Method:').'
IF UserID	= ''	THEN ErrorMsg = 'Null parameter UserID passed to routine (':Method:').'

IF ErrorMsg NE '' THEN RETURN

thisStartDTM = ICONV(StartDTM,'DT')
IF thisStartDTM = '' THEN
	ErrorMsg = 'Invalid value ':QUOTE(StartDTM):' passed for parameter StartDTM (':Method:').'
	RETURN
END

thisStopDTM = ICONV(StopDTM,'DT')
IF thisStopDTM = '' THEN
	ErrorMsg = 'Invalid value ':QUOTE(StopDTM):' passed for parameter StopDTM (':Method:').'
	RETURN
END

ToolEscKey = ToolID:'*':thisStartDTM

OtParms = 'TOOL_ESC':@RM:ToolEscKey
ToolEscRec = obj_Tables('ReadRec',OtParms)

ToolEscRec<TOOL_ESC_STOP_DTM$>		= thisStopDTM
ToolEscRec<TOOL_ESC_STOP_USER$>		= UserID
ToolEscRec<TOOL_ESC_STOP_REASON$>	= Reason

OtParms = FieldStore(OtParms,@RM,4,1,ToolEscRec)

obj_Tables('WriteRec',OtParms)		;* Writes and unlocks the record

* * * * * * * 

Recipients		= XLATE('NOTIFICATION','TOOL_ESCALATION',NOTIFICATION_USER_ID$,'X')
SentFrom		= UserID
Subject			= 'Tool Escalation Closed'
Message			= "Tool ID ":ToolID:" escalation closed.":CRLF$:Reason
AttachWindow	= 'TOOL_ESC'
AttachKey		= ToolID:'*':StartDTM
SendToGroup		= ''

Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
obj_Notes('Create',Parms)


* * * * *  *

RETURN


