COMPILE FUNCTION WO_MAT_PURGE(WOMatKey)

* Removes Work Order and all information related to it from the system.

DECLARE SUBROUTINE ErrMsg, Send_Info, Send_Dyn, Send_Info
DECLARE FUNCTION obj_WM_In, obj_WM_Out


$INSERT WO_LOG_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT WO_STEP_EQUATES
$INSERT WM_IN_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT RDS_EQUATES
$INSERT SURFACE_SCAN_EQUATES
$INSERT RDS_LAYER_EQUATES
$INSERT RDS_TEST_EQUATES
$INSERT TW_USE_EQUATES
$INSERT NCR_EQUATES
$INSERT REACT_RUN_EQUATES
$INSERT CLEAN_INSP_EQUATES
$INSERT DICT_EQUATES

equ CRLF$       to char(13):char(10)

EQU COL$DEPEND_COL_NO		TO 1
EQU COL$DEPEND_COL_NAME		TO 2
EQU COL$DEPEND_TABLE		TO 3

Result = ''

IF NOT(ASSIGNED(WOMatKey)) THEN WOMatKey = ''

IF WOMatKey = '' THEN
	ErrMsg('Null WONO passed to routine.')
	RETURN Result
END

OPEN 'WO_LOG' TO WOLogTable ELSE
	DEBUG
	RETURN Result
END

OPEN 'WO_MAT' TO WOMatTable ELSE
	DEBUG
	RETURN Result
END

OPEN 'WM_IN' TO WMInTable ELSE
	DEBUG
	RETURN Result
END
OPEN 'WM_OUT' TO WMOutTable ELSE
	DEBUG
	RETURN Result
END
OPEN 'RDS' TO RDSTable ELSE
	DEBUG
	RETURN Result
END
OPEN 'SURFACE_SCAN' TO SurfScanTable ELSE
	DEBUG
	RETURN Result
END


OPEN 'NCR' TO NCRTable ELSE
	DEBUG
	RETURN Result
END


OPEN 'CLEAN_INSP' TO CleanInspTable ELSE
	DEBUG
	RETURN Result
END

/*

* Test code

OPEN 'DICT.WO_MAT' TO DictWOMat THEN
	
	SELECT DictWOMat

	DependData = ''
	
	Done = 0
	LineCnt = 0

	DEBUG

	LOOP
		READNEXT DictKey ELSE Done = 1
	UNTIL Done
		IF DictKey[1,1] NE '%' THEN
			READ DictRec FROM DictWOMat,DictKey THEN
				IF DictRec<DICT_RELATED$> NE '' THEN
					LineCnt += 1

					DependData<COL$DEPEND_COL_NO,LineCnt>	= DictRec<DICT_COLUMN_NO$>
					DependData<COL$DEPEND_COL_NAME,LineCnt>	= DictKey
					DependData<COL$DEPEND_TABLE,LineCnt>	= DictRec<DICT_RELATED$>[1,'*']
				END
			END
		END	
	REPEAT


END	; * End of DICT.WO_MAT open


RETURN
*/



Log = ''

WOMatFlag = 1

READ WOMatRec FROM WOMatTable,WOMatKey THEN

	*********
	
	NCRKeys = WOMatRec<WO_MAT_NCR_KEYS$>
	
	NCRCnt = COUNT(NCRKeys,@VM) + (NCRKeys NE '')
	
	FOR NCR = 1 TO NCRCnt
	
		NCRKey = NCRKeys<1,NCR>
		
		TableName	= 'NCR'
		TableVar	= NCRTable
		RecKey		= NCRKey
		
		GOSUB DeleteRec
		
		IF NOT(Gone) THEN RDSFlag = 0
	
	NEXT NCR


	*********
	
	CombCIKeys = ''
	
	IF WOMatRec<WO_MAT_CLEAN_INSP_KEY$> NE '' THEN
		CombCIKeys := WOMatRec<WO_MAT_CLEAN_INSP_KEY$>:@VM
	END
	
	IF WOMatRec<WO_MAT_EPI_CI_NO$> NE '' THEN
		CombCIKeys := WOMatRec<WO_MAT_EPI_CI_NO$>:@VM
	END
	
	IF WOMatRec<WO_MAT_EPO_CI_NO$> NE '' THEN
		CombCIKeys := WOMatRec<WO_MAT_EPO_CI_NO$>:@VM
	END
	
	IF CombCIKeys[-1,1] = @VM THEN CombCIKeys[-1,1] = ''
	
	CIFlag = 1
	
	IF CombCIKeys NE '' THEN
	
		CICnt = COUNT(CombCIKeys,@VM) + (CombCIKeys NE '')
		
		FOR CI = 1 TO CICnt
		
			CIKey = CombCIKeys<1,CI>
			
			TableName	= 'CLEAN_INSP'
			TableVar	= CleanInspTable
			RecKey		= CIKey
			
			GOSUB DeleteRec
		
			IF NOT(Gone) THEN CIFlag = 0
		
		NEXT CI
	
	END	;* End of check for CI Keys
	
	*********
	
	WMInKeys	= WOMatRec<WO_MAT_WMI_KEY$>
	
	WMICnt = COUNT(WMInKeys,@VM) + (WMInKeys NE '')
	
	FOR Wmi = 1 TO WMICnt
	
		WMInKey = WMInKeys<1,Wmi>
		
		READ WMInRec FROM WMInTable,WMInKey THEN
		
			WMInFlag = 1
			
			NCRNos = obj_WM_In('NCRNos',WMInKey:@RM:WMInRec)
			
			NCRCnt = COUNT(NCRNos,@VM) + (NCRNos NE '')
			
			FOR Ncr = 1 TO NCRCnt
			
				NCRNo = NCRNos<1,Ncr>
				
				TableName	= 'NCR'
				TableVar	= NCRTable
				RecKey		= NCRNo
				
				GOSUB DeleteRec
				
				IF NOT(Gone) THEN WMInFlag = 0
				
			NEXT Ncr
		
		END	;* End of WMInRec read
		
		IF WMInFlag THEN
		
			TableName	= 'WM_IN'
			TableVar	= WMInTable
			RecKey		= WMInKey
			
			GOSUB DeleteRec
			
			IF NOT(Gone) THEN WOMatFlag = 0
		END

	NEXT Wmi
	
	*********

	WMOutKeys	= WOMatRec<WO_MAT_WMO_KEY$>
	
	WMOCnt = COUNT(WMOutKeys,@VM) + (WMOutKeys NE '')
	
	FOR Wmo = 1 TO WMOCnt
	
		WMOutKey = WMOutKeys<1,Wmo>
		
		READ WMOutRec FROM WMOutTable,WMOutKey THEN
		
			WMOutFlag = 1
			
			NCRNos = obj_WM_Out('NCRNos',WMOutKey:@RM:WMOutRec)
			
			NCRCnt = COUNT(NCRNos,@VM) + (NCRNos NE '')
			
			FOR Ncr = 1 TO NCRCnt
			
				NCRNo =NCRNos<1,Ncr>
				
				TableName	= 'NCR'
				TableVar	= NCRTable
				RecKey		= NCRNo
				
				GOSUB DeleteRec
				
				IF NOT(Gone) THEN WMOutFlag = 0
				
			NEXT Ncr
						
		END	;* End of WMOutRec read				
		
		IF WMOutFlag THEN
			TableName	= 'WM_OUT'
			TableVar	= WMOutTable
			RecKey		= WMOutKey
			
			GOSUB DeleteRec
			
			IF NOT(Gone) THEN WOMatFlag = 0
		END

	NEXT Wmo
	
	*********
	
	RDSKeys	= WOMatRec<WO_MAT_RDS_NO$> 
	
	RDSCnt = COUNT(RDSKeys,@VM) + (RDSKeys NE '')
	
	FOR Rds = 1 TO RDSCnt
	
		RDSFlag = 1
		
		RDSKey = RDSKeys<1,Rds> 
		
		READ RDSRec FROM RDSTable,RDSKey THEN
			
			*********
			
			RDSLayerKeys = RDSRec<RDS_RDS_LAYER_KEYS$>
			
			LSCnt = COUNT(RDSLayerKeys,@VM) + (RDSLayerKeys NE '')
			
			FOR LayerSet = 1 TO LSCnt
				
				RDSLayerFlag = 1
				
				RDSLayerKey = RDSLayerKeys<1,LayerSet>
				
				READ RDSLayerRec FROM RDSLayerTable,RDSLayerKey THEN
					
					RDSTestKeys = RDSLayerRec<RDS_LAYER_RDS_TEST_KEYS$>
					
					RDSTestCnt = COUNT(RDSTestKeys,@VM) + (RDSTestKeys NE '')
					
					FOR RdsTest = 1 To RDSTestCnt
						RDSTestFlag = 1
						
						RDSTestKey = RDSTestKeys<1,RdsTest>
						
						READ RDSTestRec FROM RDSTestTable,RDSTestKey THEN
							TWUseKeys = RDSTestRec<RDS_TEST_TW_USE_ID$>
								
							TWUseCnt = COUNT(TWUseKeys,@VM) + (TWUseKeys NE '')
							
							FOR TWUse = 1 TO TWUseCnt
								TWUseKey = TWUseKeys<1,TWUse>
									
								TableName	= 'TW_USE'
								TableVar	= TWUseTable
								RecKey 		= TWUseKey
								
								GOSUB DeleteRec
								
								IF NOT(Gone) THEN RDSTestFlag = 0
							
							NEXT TWUse
							
						END	;* End OF RDSTestRec read
						
						IF RDSTestFlag THEN
							
							TableName	= 'RDS_TEST'
							TableVar	= RDSTestTable
							RecKey		= RDSTestKey
							
							GOSUB DeleteRec
							
							IF NOT(Gone) THEN RDSLayerFlag = 0
						END
					
					NEXT RdsTest
					
				END	ELSE
					RDSLayerFlag = 0
				END		;* End OF RDSLayer Record Read
				
				IF RDSLayerFlag THEN
				
					TableName	= 'RDS_LAYER'
					TableVar	= RDSLayerTable
					RecKey		= RDSLayerKey
					
					GOSUB DeleteRec
					
					IF NOT(Gone) THEN RDSFlag = 0
				END 
			
			NEXT LayerSet
			
			*********
			
			SurfScanKeys = RDSRec<RDS_SURFSCAN_KEYS$>
			
			SSCnt = COUNT(SurfScanKeys,@VM) + (SurfScanKeys NE '')
			
			FOR SurfScan = 1 TO SSCnt
			
				SurfScanKey = SurfScanKeys<1,SurfScan>
				
				TableName	= 'SURFACE_SCAN'
				TableVar	= SurfScanTable
				RecKey		= SurfScanKey
				
				GOSUB DeleteRec
				
				IF NOT(Gone) THEN RDSFlag = 0
			
			NEXT SurfScan
			
			*********
			
			NCRKeys = RDSRec<RDS_NCR_KEYS$>
			
			NCRCnt = COUNT(NCRKeys,@VM) + (NCRKeys NE '')
			
			FOR NCR = 1 TO NCRCnt
			
				NCRKey = NCRKeys<1,NCR>
				
				TableName	= 'NCR'
				TableVar	= NCRTable
				RecKey		= NCRKey
				
				GOSUB DeleteRec
				
				IF NOT(Gone) THEN RDSFlag = 0
			
			NEXT NCR
		
		END	;* End of RDSRec Read
		
		IF RDSFlag THEN
			TableName	= 'RDS'
			TableVar	= RDSTable
			RecKey		= RDSKey
			
			GOSUB DeleteRec
			
			IF NOT(Gone) THEN WOMatFlag = 0
		END ELSE
			WOMatFlag = 0
		END
		
		READ ReactRunRec FROM ReactRunTable,RDSKey THEN
		
			ReactRunFlag = 1
		
			IF ReactRunRec<REACT_RUN_PRE_CI_NO$> NE '' THEN
				
				TableName	= 'CLEAN_INSP'
				TableVar	= CleanInspTable
				RecKey		= ReactRunRec<REACT_RUN_PRE_CI_NO$>
				
				GOSUB DeleteRec
				
				IF NOT(Gone) THEN ReactRunFlag = 0
				
			END
			
			IF ReactRunRec<REACT_RUN_WFR_CI_NO$> NE '' THEN
			
				TableName	= 'CLEAN_INSP'
				TableVar	= CleanInspTable
				RecKey		= ReactRunRec<REACT_RUN_WFR_CI_NO$>
				
				GOSUB DeleteRec
				
				IF NOT(Gone) THEN ReactRunFlag = 0
				
			END
			
			IF ReactRunRec<REACT_RUN_POST_CI_NO$ > NE '' THEN
			
				TableName	= 'CLEAN_INSP'
				TableVar	= CleanInspTable
				RecKey		= ReactRunRec<REACT_RUN_POST_CI_NO$>
				
				GOSUB DeleteRec
				
				IF NOT(Gone) THEN ReactRunFlag = 0
			END
		
		END	ELSE
			ReactRunFlag = 0
		END	;* End of ReactRunRec READ
		
		IF ReactRunFlag THEN
			TableName	= 'REACT_RUN'
			TableVar	= ReactRunTable
			RecKey		= RDSKey
			
			GOSUB DeleteRec
		END
		
	NEXT Rds
		
	
END	;* End of WOMatRec Read

IF WOMatFlag THEN
	
	TableName	= 'WO_MAT'
	TableVar	= WOMatTable
	RecKey		= WOMatKey
	
	GOSUB DeleteRec
	
END

RETURN WOMatFlag

*CONVERT @FM TO @VM IN Log

*Result = Log

*RETURN Result



* * * * * * *
DeleteRec:
* * * * * * *

Send_Info('Deleting ':TableName:' ':RecKey)

DELETE TableVar,RecKey THEN
	Log<-1> = TableName:' ':RecKey:' purged.'
	Gone = 1
END ELSE
	IF @FILE_ERROR NE '' AND @FILE_ERROR<1> NE '100'  THEN
		Log<-1> = '*** Unable to purge ':TableName:' ':RecKey:'   ***'
		Gone = 0
	END ELSE
		Gone = 1
	END
END

RETURN
