COMPILE FUNCTION obj_PM(Method,Parms)

/*
	Methods for PM table
    
	09/29/2016 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
	GetHistory(PMSNo,)											;* Returns sorted list of PMNos by descending Comp Dt
	LastPM(PM
																	
	
*/


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

$INSERT PM_EQUATES

EQU CRLF$	TO \0D0A\
EQU TAB$	TO CHAR(9)

ErrTitle = 'Error in Stored Procedure "obj_PM"'
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 = 'GetHistory'		; GOSUB GetHistory
	
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to routine.'

END CASE

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

RETURN Result




* * * * * * *
GetHistory:
* * * * * * *

* This has been moved to obj_PM_Spec -> use that one and not this one. JCH

PMSId	= Parms[1,@RM]

IF PMSId = '' THEN RETURN

OPEN 'DICT.PM' TO DictVar ELSE
	ErrorMsg = 'Unable to open DICT.PM for index lookup'
	RETURN
END

Search = ''

IF PMSID NE '' 	THEN Search := 'PMS_ID':@VM:PMSId:@FM

Option = ''
Flag = ''
PMKeys = ''

Btree.Extract(Search, 'PM', DictVar, PMKeys, Option, Flag)	;* Get unsorted keys based on Search parameters

* Put sort data in first column (Install DTM)

WorkList = ''
pmCnt = COUNT(PMKeys,@VM) + (PMKeys NE '')
FOR I = 1 TO pmCnt
	PMKey = PMKeys<1,I>
	WorkList<I> = XLATE('PM',PMKey,PM_COMP_DTM$,'X'):@VM:pmKey
NEXT I

WorkList := @RM	

CONVERT @FM:@VM TO @RM:@FM IN WorkList				;* push delimiters higher for V119

CALL V119('S','','D','R',WorkList,'')				;* Descending Left justified sort

CONVERT @FM:@RM TO @VM:@FM IN WorkList				;* pull delimiters lower

WorkList[-1,1] = ''	;* Trim trailing delimiter

FOR I = 1 TO pmCnt
	Result<1,I> = WorkList<I,2>						;* Strip Sort column(s)
NEXT I

RETURN
