COMPILE FUNCTION obj_Location(Method,Parms)
#pragma precomp SRP_PreCompiler

/*
	Methods for the LOCATION (Inventory Location) Tables
	
	4/22/2016 JCH - Initial coding
	
	
*/

DECLARE FUNCTION Get_Status, Msg, obj_Tables, Send_Dyn, ErrMsg, obj_Tables, Logging_Services, Environment_Services
DECLARE SUBROUTINE ErrMsg, Set_Status, FieldStore, obj_Tables, obj_WO_Wfr, RList, Msg, Send_Dyn, Btree.Extract, Logging_Services

$Insert LOCATION_EQUATES
$INSERT RLIST_EQUATES

ErrTitle = 'Error in Stored Procedure "obj_Location"'
ErrorMsg = ''
ErrCode = ''

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 = 'SetWfrID'		; Gosub SetWfrID
	Case Method = 'ClearWfrID'		; Gosub ClearWfrID
	CASE Method = 'StageLocations'	; GOSUB Locations
	CASE Method = 'KitLocations'	; GOSUB Locations
	CASE Method = 'PTILocations'	; GOSUB Locations
		
	CASE 1
		NULL

END CASE

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

RETURN Result


* * * * * * *
SetWfrID:
* * * * * * *

LocID	= Parms[1,@RM]
WfrIDs	= Parms[COL2()+1,@RM]

If LocID = ''		THEN ErrorMsg = 'Null Parameter "LocID" passed to routine. (':Method:')'
If WfrIDs = ''		THEN ErrorMsg = 'Null Parameter "WfrID" passed to routine. (':Method:')'

LogWfrIDs = WfrIDs
Convert @VM to ',' in LogWfrIDs
* Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: '          obj_Location/ SetWfrID' : @FM : '1. LocID : ' : LocID : ', WfrIDs : ' : LogWfrIDs, @RM, @FM)

If ErrorMsg NE '' Then Return 

otParms = 'LOCATION':@RM:LocID
LocRec = obj_Tables('ReadRec',otParms)

WriteFlag = 0

wCnt = COUNT(WfrIDs,@VM) + (WfrIDs NE '')

FOR I = 1 TO wCnt
	WfrID = WfrIDs<1,I>
	
	Locate WfrID In LocRec<LOCATION_WFR_ID$> USING @VM SETTING Pos ELSE	
		LocRec = Insert(LocRec,LOCATION_WFR_ID$ ,Pos,0, WfrID)
		WriteFlag = 1
	END

NEXT I

IF WriteFlag THEN
	otParms  = FieldStore(otParms,@RM,4,0,LocRec)
	obj_Tables('WriteRec',otParms)
* 	Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: '          obj_Location/ SetWfrID' : @FM : '2. LocID : ' : LocID : ', WfrIDs : ' : LogWfrIDs, @RM, @FM)
END ELSE
	obj_Tables('UnlockRec',otParms)
* 	Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: '          obj_Location/ SetWfrID' : @FM : '3. LocID : ' : LocID : ', WfrIDs : ' : LogWfrIDs, @RM, @FM)
END

Return



* * * * * * *
ClearWfrID:
* * * * * * *

LocID	= Parms[1,@RM]
WfrIDs	= Parms[COL2()+1,@RM]

If LocID = ''		THEN ErrorMsg = 'Null Parameter "LocID" passed to routine. (':Method:')'
If WfrIDs = ''		THEN ErrorMsg = 'Null Parameter "WfrIDs" passed to routine. (':Method:')'

LogWfrIDs = WfrIDs
Convert @VM to ',' in LogWfrIDs
* Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: '          obj_Location/ ClearWfrID' : @FM : '1. LocID : ' : LocID : ', WfrIDs : ' : LogWfrIDs, @RM, @FM)


If ErrorMsg NE '' Then Return 

otParms = 'LOCATION':@RM:LocID
LocRec = obj_Tables('ReadRec',otParms)

WriteFlag = 0

wCnt = COUNT(WfrIDs,@VM) + (WfrIDs NE '')

FOR I = 1 TO wCnt
	WfrID = WfrIDs<1,I>
	
	Locate WfrID In LocRec<LOCATION_WFR_ID$> USING @VM SETTING Pos Then
		LocRec = Delete(LocRec,LOCATION_WFR_ID$,Pos,0)
		WriteFlag = 1
	End
	
NEXT I

IF WriteFlag THEN
	otParms  = FieldStore(otParms,@RM,4,0,LocRec)
	obj_Tables('WriteRec',otParms)
*    Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: '          obj_Location/ ClearWfrID' : @FM : '2. LocID : ' : LocID : ', WfrIDs : ' : LogWfrIDs, @RM, @FM)
END ELSE 
	obj_Tables('UnlockRec',otParms)
* 	Logging_Services('AppendLog', objLog, LoggingDTM : @FM : @USER4 : @FM: '          obj_Location/ ClearWfrID' : @FM : '3. LocID : ' : LocID : ', WfrIDs : ' : LogWfrIDs, @RM, @FM)
END


Return 


* * * * * * *
Locations:
* * * * * * *

WONo = Parms[1,@RM]

IF WONo = '' THEN RETURN

BEGIN CASE
	CASE Method = 'StageLocations'	; LocVar = '1K*R]'
	CASE Method = 'KitLocations'	; LocVar = 'SR*KTR]'	    
 	CASE Method = 'PTILocations'
 	    LocVar = ''
 	    LocVar<0, -1> = 'SR*KTR]'
 	    LocVar<0, -1> = 'SR*RB'
 	    LocVar<0, -1> = '1K*PTI'
 	    LocVar<0, -1> = 'CR*GAN1'
 	    LocVar<0, -1> = 'CR*BE'
 	    LocVar<0, -1> = 'CR*BO'
 	    LocVar<0, -1> = 'CR*CLN1'
 	    LocVar<0, -1> = 'CR*CLN2'
 	    LocVar<0, -1> = 'CR*TUN'
 	    LocVar<0, -1> = 'CR*EPP1'
 	    LocVar<0, -1> = 'CR*EPP2'
 	    LocVar<0, -1> = 'CR*EPR'
 	    LocVar<0, -1> = 'CR*FE'
 	    LocVar<0, -1> = 'CR*FEH'
 	    LocVar<0, -1> = 'CR*FO'
 	    LocVar<0, -1> = 'CR*FOH'
 	    LocVar<0, -1> = 'CR*MU'
 	    
 	    OPEN 'DICT.WO_MAT' TO DictVar THEN

        SearchString  = 'WO_NO':@VM:WONo:@FM
        SearchString := 'CURR_LOCATION':@VM:LocVar:@FM
        Flag = ''
        WOMatKeys = ''

        Btree.Extract(SearchString, 'WO_MAT', DictVar, WOMatKeys, '', Flag)
        IF Get_Status(errCode) THEN
            Msg(@WINDOW,MsgUp)
            ErrMsg(errCode)
            RETURN
        END

        END ELSE
            RETURN
        END

        Locations = ''
        LocCnts	= ''
        LocCassNos = ''

        LOOP
            WOMatKey = WOMatKeys[1,@VM]
        UNTIL WOMatKey = '' OR WOMatKeys = ''

            CurrLoc	= XLATE('WO_MAT',WOMatKey,'CURR_LOCATION','X')
            CassNo	= FIELD(WOMatKey,'*',2,1)
            
            LOCATE CurrLoc IN Locations USING @VM SETTING Pos THEN
                LocCnts<1,Pos> = LocCnts<1,Pos> +1
                LocCassNos<1,Pos> = LocCassNos<1,Pos>:',':CassNo
            END ELSE
                Locations	= INSERT(Locations,1,Pos,0,CurrLoc)
                LocCnts		= INSERT(LocCnts,1,Pos,0,1)
                LocCassNos	= INSERT(LocCassNos,1,Pos,0,CassNo)
            END
            
            WOMatKeys[1,COL2()] = ''

        REPEAT

        Result = Locations:@FM:LocCnts:@FM:LocCassNos

        RETURN
	    
	CASE 1
		RETURN
END CASE

OPEN 'DICT.WO_MAT' TO DictVar THEN

	SearchString  = 'WO_NO':@VM:WONo:@FM
	SearchString := 'CURR_LOCATION':@VM:LocVar:@FM
	Flag = ''
	
	Btree.Extract(SearchString, 'WO_MAT', DictVar, WOMatKeys, '', Flag)
	IF Get_Status(errCode) THEN
		Msg(@WINDOW,MsgUp)
		ErrMsg(errCode)
		RETURN
	END

END ELSE
	RETURN
END

Locations = ''
LocCnts	= ''
LocCassNos = ''

LOOP
	WOMatKey = WOMatKeys[1,@VM]
UNTIL WOMatKey = '' OR WOMatKeys = ''

	CurrLoc	= XLATE('WO_MAT',WOMatKey,'CURR_LOCATION','X')
	CassNo	= FIELD(WOMatKey,'*',2,1)
	
	LOCATE CurrLoc IN Locations USING @VM SETTING Pos THEN
		LocCnts<1,Pos> = LocCnts<1,Pos> +1
		LocCassNos<1,Pos> = LocCassNos<1,Pos>:',':CassNo
	END ELSE
		Locations	= INSERT(Locations,1,Pos,0,CurrLoc)
		LocCnts		= INSERT(LocCnts,1,Pos,0,1)
		LocCassNos	= INSERT(LocCassNos,1,Pos,0,CassNo)
	END
	
	WOMatKeys[1,COL2()] = ''

REPEAT

Result = Locations:@FM:LocCnts:@FM:LocCassNos

RETURN


