COMPILE FUNCTION obj_Notes_Sent(Method,Parms)
/*
	Methods for Notes_Sent table
    
	08/02/2006 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
	Create(Recipient,NoteID,TxDTM)			;* Creates Records
	Delete(NoteSentKeys)					;* Deletes Records
	GetUserKeys(Recipient)					;* Return NOTES_SENT keys for passed in user ID
	
*/

DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables,  ErrMsg, Btree.Extract,ErrMsg, Update_Index

$INSERT MSG_EQUATES
$INSERT NOTES_EQU
$INSERT NOTES_SENT_EQUATES

$INSERT LOGICAL
$INSERT SRPMail_Inserts

EQU TARGET_ACTIVELIST$ TO 5


ErrTitle = 'Error in Stored Procedure "obj_Notes_Sent"'
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 = 'Create'			; GOSUB Create
	CASE Method = 'Delete'			; GOSUB Delete
	CASE Method = 'GetUserKeys'		; GOSUB GetUserKeys
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to object.'
		
END CASE

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

RETURN Result


* * * * * * *
Create:
* * * * * * *
	
	Recipient	= Parms[1,@RM]
	NoteID		= Parms[COL2()+1,@RM]
	TxDTM		= Parms[COL2()+1,@RM]		;* In External format
	
	IF Recipient = ''	THEN ErrorMsg = 'Unassigned Parameter "Recipient" passed to object. (':Method:')'
	IF NoteID = ''		THEN ErrorMsg = 'Unassigned Parameter "NoteID" passed to object. (':Method:')'
	IF TxDTM = ''		THEN ErrorMsg = 'Unassigned Parameter "TxDTM" passed to object. (':Method:')'
	
	IF ErrorMsg NE '' THEN RETURN
	
	thisTxDTM = ICONV(TxDTM,'DT')
	
	IF thisTxDTM = '' THEN
		ErrorMsg = 'Invalid parameter "TxDTM" = ':QUOTE(TxDTM):' passed to object. (':Method:')'
		RETURN
	END
	
	OtParms = 'NOTES_SENT':@RM:Recipient:'*':NoteID:@RM:@RM:TxDTM		;* First (and only) field in record is TxDTM
	obj_Tables('WriteRec',OtParms)										;* Writes new record
	
	Update_Index('NOTES_SENT','USER_ID','', True$)
	
	
RETURN


* * * * * * *
Delete:
* * * * * * *
	
	
	NotesSentKeys = Parms[1,@RM]
	
	IF NotesSentKeys = '' THEN RETURN
	
	OPEN 'NOTES_SENT' TO NotesSentFile THEN
		KeyCnt = COUNT(NotesSentKeys,@VM) + (NotesSentKeys NE '')
		FOR I = 1 TO KeyCnt
			DELETE NotesSentFile,NotesSentKeys<1,I> ELSE NULL
		NEXT I
	END
	
RETURN


* * * * * * *
GetUserKeys:
* * * * * * *
	
	Recipient = Parms[1,@RM]
	
	IF Recipient = '' THEN RETURN
	
	OPEN 'DICT.NOTES_SENT' TO DictVar THEN
		SearchString = 'USER_ID':@VM:Recipient:@FM
		Flag = ''
		Btree.Extract(SearchString,'NOTES_SENT',DictVar,NotesSentKeys,'',Flag)
		IF Get_Status(errCode) THEN
			ErrMsg(errCode)
			RETURN
		END
		
		Result = NotesSentKeys
	END
	
RETURN

