COMPILE FUNCTION obj_TW_Use(Method,Parms)

/*
	Methods for TW_USE table
    
	07/21/2010 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Load(DataStruct)			;* Loads TW_USE table from RDS_TES
	
	
*/


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

$INSERT RDS_TEST_EQUATES
$INSERT TW_USE_EQUATES
$INSERT RLIST_EQUATES


EQU COL$TW_QTY				TO 1		;* Used for conversion process from RDS_TEST
EQU COL$TW_CODE				TO 2
EQU COL$TW_USAGE_DESC		TO 3
EQU COL$TW_TOOL				TO 4
EQU COL$TW_TYPE				TO 5
EQU COL$TW_SIG				TO 6
EQU COL$TW_SIG_DTM			TO 7
EQU COL$TW_HRS_SINCE_PROD	TO 8
EQU COL$TW_HRS_ESC			TO 9
EQU COL$TW_WAFER_ID			TO 10
EQU COL$TW_USE_ID			TO 11


ErrTitle = 'Error in Stored Procedure "obj_TW_Use"'
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 = 'Copy'			; GOSUB Copy
	CASE Method = 'Load'			; GOSUB Load
	CASE Method = 'Convert'			; GOSUB Convert
	
	CASE 1

END CASE

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

RETURN Result


* * * * * * *
Copy:
* * * * * * * 

MetNo		= Parms[1,@RM]
NewTWUseNo	= Parms[COL2()+1,@RM]
OrgTWUseNo	= Parms[COL2()+1,@RM]

IF MetNo = ''		THEN ErrorMsg = 'Null Parameter "MetNo" passed to routine. (':Method:')'
IF NewTWUseNo = ''	THEN ErrorMsg = 'Null Parameter "NewTWUseNo" passed to routine. (':Method:')'
IF OrgTWUseNo = ''	THEN ErrorMsg = 'Null Parameter "OrgTWUseNo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

OrgRec = XLATE('TW_USE',MetNo:'*':OrgTWUseNo,'','X')

IF OrgRec ='' THEN
	ErrorMsg = 'Original TW_USE Record not found ':QUOTE(MetNo:'*':OrgTWUseNo):'.'
	RETURN
END


OrgRec<TW_USE_SIGNATURE$> = ''
OrgRec<TW_USE_SIG_DTM$> = ''

TWUseKey = MetNo:'*':NewTWUseNo
obj_Tables('WriteRec','TW_USE':@RM:TWUseKey:@RM:@RM:OrgRec)

RETURN



* * * * * * *
Load:
* * * * * * *


OPEN 'RDS_TEST' TO MetTable ELSE
	DEBUG
	RETURN
END

OPEN 'TW_USE' TO TestWfrTable ELSE
	DEBUG
	RETURN
END


*SelectSent = 'SELECT RDS_TEST 512775'
SelectSent = 'SELECT RDS_TEST WITH TW_SIG_DT FROM "3/28/2011" TO "4/2/2011"'


RList(SelectSent,TARGET_ACTIVELIST$,'','','')

debug

IF Get_Status(ErrCode) THEN DEBUG

TotRecCnt = @RECCOUNT
RecCnt = 0
Done = 0
LOOP
	READNEXT MetNo ELSE Done = 1
UNTIL Done
	READ MetRec FROM MetTable,MetNo THEN
		RecCnt += 1
		TWCnt = COUNT(MetRec<RDS_TEST_TW_CNT$>,@VM) + (MetRec<RDS_TEST_TW_CNT$> NE '')
			
		TWList = ''
		
		FOR Line = 1 TO TWCnt
			TWList<Line,COL$TW_QTY>				= MetRec<RDS_TEST_TW_CNT$,Line>
			TWList<Line,COL$TW_CODE>			= MetRec<RDS_TEST_TW_CODE$,Line>
			TWList<Line,COL$TW_USAGE_DESC>		= ''
			TWList<Line,COL$TW_TOOL>			= MetRec<RDS_TEST_TW_TOOL$,Line>
			TWList<Line,COL$TW_TYPE>			= MetRec<RDS_TEST_TW_TYPE$,Line>
			TWList<Line,COL$TW_SIG>				= MetRec<RDS_TEST_TW_SIG$,Line>
			TWList<Line,COL$TW_SIG_DTM>			= OCONV(MetRec<RDS_TEST_TW_SIG_DTM$,Line>,'DT/^S')
			TWList<Line,COL$TW_HRS_SINCE_PROD>	= MetRec<RDS_TEST_TW_HRS_SINCE_PROD$,Line>
			TWList<Line,COL$TW_HRS_ESC>			= MetRec<RDS_TEST_TW_HRS_ESC$,Line>
			TWList<Line,COL$TW_WAFER_ID>		= MetRec<RDS_TEST_TW_WAFER_ID$,Line>
			TWList<Line,COL$TW_USE_ID>			= MetRec<RDS_TEST_TW_USE_ID$,Line>
		NEXT Line
	
		GOSUB Convert
	
		MetRec<RDS_TEST_TW_USE_ID$> = Result
		
		WRITE MetRec ON MetTable,MetNo THEN
			Send_Info(RecCnt:'/':TotRecCnt:'  ':MetNo:' - Written')
		END
		
	END	;* End of MetRec read
REPEAT

RETURN




* * * * * * *
Convert:
* * * * * * *


IF NOT(Assigned(MetNo)) THEN
	MetNo = Parms[1,@RM]
END

IF NOT(Assigned(TWList)) THEN
	TWList = Parms[COL2()+1,@RM]
END

ListCnt = COUNT(TWList,@FM) + (TWList NE '')
 
RowCnt = ListCnt
LOOP
	TWLine = TWList<RowCnt>
	CONVERT @VM TO '' IN TWLine
UNTIL TWLine NE '' OR TWList = ''
	TWList = DELETE(TWList,RowCnt,0,0)		;* Strip empty lines from display List
	RowCnt -= 1
REPEAT

TWArray = ''
FOR I = 1 TO RowCnt
	FOR N = COL$TW_QTY TO COL$TW_USE_ID
		TWArray<N,I> = TWList<I,N>			;* Build an 'ARRAY' from the 'LIST'
	NEXT N
NEXT I

UseIDs	= TWArray<COL$TW_USE_ID>
TWCodes	= TWArray<COL$TW_CODE>

LastSeq = 0
Pos		= 1
Flag	= ""
LOOP
    REMOVE UseID FROM UseIds AT Pos SETTING Flag
WHILE Flag
	SeqNo = UseID[-1,'B*']
	IF  SeqNo > LastSeq THEN LastSeq = SeqNo
REPEAT

NextSeq = LastSeq + 1		;* Highest number used in keys (Position in the array doesn't count)

OPEN 'DICT.TW_USE' TO DictTWUse ELSE
	ErrorMsg = 'Unable to open DICT.TW_USE in obj_TW_Use Convert method.'
	RETURN
END

OPEN 'TW_USE' TO TWUseFile ELSE
	ErrorMsg = 'Unable to open TW_USE in obj_TW_Use Convert method.'
	RETURN
END

SearchString = 'MET_NO':@VM:MetNo:@FM
Option = ''
Flag = ''

Set_Status(0)

Btree.Extract(SearchString,'TW_USE',DictTWUse,CurrUseIDs,Option,Flag)

IF Get_Status(errCode) THEN
	ErrMsg(errCode)
	RETURN
END

BuildKeys = ''
DeleteKeys = ''

FOR I = 1 TO RowCnt
	UseID = UseIDs<1,I>
	IF CurrUseIDs = '' THEN
		BuildKeys<1,I> = MetNo:'*':NextSeq
		NextSeq += 1
	END ELSE
		LOCATE UseID IN CurrUseIDs USING @VM SETTING Pos THEN
			BuildKeys<1,I> = ''
			
			READ TWUseRec FROM TWUseFile,UseID THEN
				OrgTWUseRec = TWUseRec
				TWUseRec<TW_USE_TW_QTY$>			= TWArray<COL$TW_QTY,I>
				TWUseRec<TW_USE_TW_USE_CODE$>		= TWArray<COL$TW_CODE,I>
				TWUseRec<TW_USE_TOOL$>				= TWArray<COL$TW_TOOL,I> 
				TWUseRec<TW_USE_TW_TYPE$>			= TWArray<COL$TW_TYPE,I>
				TWUseRec<TW_USE_SIGNATURE$> 		= TWArray<COL$TW_SIG,I>
				TWUseRec<TW_USE_SIG_DTM$>			= ICONV(TWArray<COL$TW_SIG_DTM,I>,'DT')
				TWUseRec<TW_USE_HRS_SINCE_PROD$>	= TWArray<COL$TW_HRS_SINCE_PROD,I>
				TWUseRec<TW_USE_HRS_ESC$> 			= TWArray<COL$TW_HRS_ESC,I>
				
				ProdWafers = TWArray<COL$TW_WAFER_ID,I>
				
				CONVERT ',' TO @VM IN ProdWafers
				CONVERT ' ' TO '' IN ProdWafers
				
				TWUseRec<TW_USE_WAFER_ID$> 			= ProdWafers
				
				IF TWUseRec NE OrgTWUseRec THEN
					WRITE TWUseRec ON TWUseFile,UseID THEN NULL
				END
			END
			
		END ELSE	
			BuildKeys<1,I> = MetNo:'*':NextSeq
			NextSeq += 1
		END
	END
NEXT I



DelCnt = 0
FOR I = 1 TO RowCnt
	CurrUseID = CurrUseIDs<1,I>
	IF CurrUseID NE '' THEN
		LOCATE CurrUseID IN UseIDs USING @VM SETTING Pos ELSE
			DeleteKeys<1,-1> = CurrUseID
			DelCnt += 1
		END
	END
NEXT I

IF DelCnt > 0 THEN
	
	FOR I = 1 TO DelCnt 
		DeleteKey = DeleteKeys<1,I>
		Set_Status(0)
		obj_Tables('DeleteRec','TW_USE':@RM:DeleteKey:@RM)	
		
		IF Get_Status(errCode) THEN	
			ErrMsg(errCode)
		END
	NEXT I
	
END

LineCnt = COUNT(TWArray<COL$TW_QTY>,@VM) + (TWArray<COL$TW_QTY> NE '')


FOR I = 1 TO LineCnt
	IF BuildKeys<1,I> NE '' THEN
	
		NewKey = BuildKeys<1,I>
	
		IF TWArray<COL$TW_TYPE,I> NE '' THEN

			TWUseRec = ''
			TWUseRec<TW_USE_TW_QTY$>			= TWArray<COL$TW_QTY,I>
			TWUseRec<TW_USE_TW_USE_CODE$>		= TWArray<COL$TW_CODE,I>
			TWUseRec<TW_USE_TOOL$>				= TWArray<COL$TW_TOOL,I> 
			TWUseRec<TW_USE_TW_TYPE$>			= TWArray<COL$TW_TYPE,I>
			TWUseRec<TW_USE_SIGNATURE$> 		= TWArray<COL$TW_SIG,I>
			TWUseRec<TW_USE_SIG_DTM$>			= ICONV(TWArray<COL$TW_SIG_DTM,I>,'DT')
			TWUseRec<TW_USE_HRS_SINCE_PROD$>	= TWArray<COL$TW_HRS_SINCE_PROD,I>
			TWUseRec<TW_USE_HRS_ESC$> 			= TWArray<COL$TW_HRS_ESC,I>
			TWUseRec<TW_USE_WAFER_ID$> 			= TWArray<COL$TW_WAFER_ID,I>
			
			obj_Tables('WriteRec','TW_USE':@RM:NewKey:@RM:@RM:TWUseRec)
			
			NextSeq += 1
		
			Result<1,I> = NewKey
			
		END
			
	END ELSE
		Result<1,I> = TWArray<COL$TW_USE_ID,I>
	END
NEXT I

IF LineCnt < ListCnt THEN
	Result := STR(@VM,ListCnt - LineCnt)
END

RETURN Result























