COMPILE ROUTINE FIX_REACT_ITEM(Dummy)
ROWDEF(CHARSTR)

DECLARE SUBROUTINE Send_Dyn,Send_Info,RList, Btree.Extract
DECLARE FUNCTION Get_Status, Set_Printer, NextKey, obj_React_Item_Hist, obj_Reactor_Log, obj_React_Item

$INSERT REACT_ITEM_EQUATES
$INSERT REACT_ITEM_HIST_EQUATES

$INSERT REACT_ITEMS_EQU
$INSERT RLIST_EQUATES

$INSERT REACTOR_LOG_EQUATES



EQU REACT_SERVS_REACT_ITEM_TYPE$ TO 9


EQU TAB$ TO \09\

OPEN 'TUBE' TO TubeTable ELSE
	DEBUG
	GOTO Bail
END

OPEN 'REACT_ITEM' TO ReactItemTable ELSE
	DEBUG
	GOTO Bail
END

OPEN 'REACT_ITEM_HIST' TO ReactItemHistTable ELSE
	DEBUG
	GOTO Bail
END


OPEN 'REACT_GRAPHITE' TO ReactGraphiteTable ELSE
	DEBUG
	GOTO Bail
END

OPEN 'REACTOR_LOG' TO ReactLogTable ELSE
	DEBUG
	GOTO Bail
END

OPEN 'DICT.REACT_ITEM' TO DictReactItem ELSE
	DEBUG
	GOTO Bail
END


SELECT ReactItemTable

Done = 0
LOOP
	READNEXT RINo ELSE Done = 1
UNTIL Done
	READ ReactItemRec FROM ReactItemTable,RINo THEN
	
		IF ReactItemRec<REACT_ITEM_RI_TYPE$> = 'T' THEN
		
			SerialNo = ReactItemRec<REACT_ITEM_SERIAL_NO$>
			Serial = ReactItemRec<REACT_ITEM_SERIAL$>
			
			IF SerialNo = '' AND Serial NE '' THEN
				ReactItemRec<REACT_ITEM_SERIAL_NO$> = Serial
				
				*WRITE ReactItemRec ON ReactITemTable,RINo THEN
					Send_Dyn(RINo:' ':ReactItemRec<REACT_ITEM_SERIAL$>:' -> ':ReactItemRec<REACT_ITEM_SERIAL_NO$>)
				*END
			END
		
			
			
		END	;* End of check for Tube records
		
	END	;* End of item read
	
REPEAT




GOTO Bail

* * * * * * * * * * * * * * *


RITypes = 'A':@FM:'G':@FM:'R':@FM:'RU':@FM:'RL':@FM:'S':@FM:'T':@FM:'W'

DEBUG

FOR I = 1 TO 8	
	RIType = RITypes<I>
	FOR ReactNo = 20 TO 79
		
	HistKeys = obj_React_Item_Hist('GetHistory',ReactNo:@RM:@RM:RIType)
	HistCnt = COUNT(HistKeys,@VM) + (HistKeys NE '')
		
	IF HistCnt > 1 THEN


		OPEN 'REACT_ITEM_HIST' TO ReactItemHistTable ELSE
			ErrorMsg = 'Unable to OPEN "REACT_ITEM_HIST" for updates.'
			RETURN
		END
		
		FOR N = 2 TO HistCnt
			HistKey		= HistKeys<1,N>
			PrevHistKey = HistKeys<1,N-1>
			
			READ HistRec FROM ReactItemHistTable,HistKey THEN
				
				READ PrevHistRec FROM ReactItemHistTable,PrevHistKey THEN
					
					*IF HistRec<REACT_ITEM_HIST_REM_DTM$> = '' THEN
						HistRec<REACT_ITEM_HIST_REM_DTM$>			= FIELD(PrevHistKey,'*',3)
						HistRec<REACT_ITEM_HIST_REM_RL_ID$>			= PrevHistRec<REACT_ITEM_HIST_INST_RL_ID$>
						HistRec<REACT_ITEM_HIST_REM_REACT_WFR_CNT$>	= PrevHistRec<REACT_ITEM_HIST_INST_REACT_WFR_CNT$>
						HistRec<REACT_ITEM_HIST_REM_REACT_HRS$>		= PrevHistRec<REACT_ITEM_HIST_INST_REACT_HRS$>
						HistRec<REACT_ITEM_HIST_REM_REASON$>		= ''
					
						
						*WRITE HistRec ON ReactItemHistTable,HistKey THEN
							NULL
							*Send_Dyn('Closed: ':HistKey:' ':HistRec)
						*END
					*END
					
				END	;* End of Closing History Record
			END	;* End of History Record Read
		
		NEXT N	;* End of loop through History keys
	END	;* End of check for HistCnt > 1



	NEXT ReactNo
NEXT I





GOTO Bail


******* This data from the ReactorLog table  *****

SELECT ReactLogTable

Done = 0
RecCnt = 0
FixCnt = 0
SerialNos = ''
RINos = ''
NextRINo = 22090
CutOffDt = ICONV('12/31/2008','D')

DummyKey = 100

LOOP
	READNEXT ReactLogNo ELSE Done = 1
UNTIL Done 
	RecCnt += 1
	
	
	READ ReactLogRec FROM ReactLogTable,ReactLogNo THEN
		IF ReactLogRec<REACTOR_LOG_ENTRY_DATE$> > CutOffDt THEN
		
			ReactNo 		= ReactLogRec<REACTOR_LOG_REACTOR$>
			EndDt			= OCONV(ReactLogRec<REACTOR_LOG_END_DATE$>,'D4/')
			EndTm			= OCONV(ReactLogRec<REACTOR_LOG_END_TIME$>,'MTS')
			InstDtm			= ICONV(EndDt:' ':EndTm,'DT')
			
			
			
			ReactItemIDs	= ReactLogRec<REACTOR_LOG_REACT_ITEM_IDS$>
			
			testReactItemIDs = ReactItemIDs
			CONVERT @VM TO '' IN testReactItemIDs
			
			IF testReactItemIDs NE '' THEN
			
				siCnt = COUNT(ReactItemIDs,@VM) + (ReactItemIDs NE '')
				
				
				
				FOR I = 1 TO siCnt
					IF ReactItemIDs<1,I> NE '' THEN

						OrgSerialNo = ReactItemIDs<1,I>
						
						SerialNo = OrgSerialNo
						IF SerialNo[1,1] = '#' THEN
							
							SWAP '#' WITH 'No.' IN SerialNo
						END
						
						SearchString = 'SERIAL':@VM:SerialNo:@FM
						

						Option = ''
						Flag = ''

						Btree.Extract(SearchString,'REACT_ITEM',DictReactItem,RINo,Option,Flag)
						
						IF INDEX(RINo,@VM,1) THEN 
							*DEBUG
							Send_Dyn(SerialNo -> RINo)
						END
						
						IF RINo = '' THEN
								
							
							/*
							ReactItemsRec = XLATE('REACT_ITEMS',OrgSerialNo,'','X')
							
							IF ReactItemsRec = '' THEN
								Send_Dyn('RLNo: ':ReactLogNo:' Bad Serial No: ':SerialNo)		;* Junk data in REACTOR_LOG field
							END ELSE
								
								Send_Dyn('Serial: ':SerialNo:' -> ':ReactItemsRec)
							

								LOCATE SerialNo IN SerialNos BY 'AR' USING @VM SETTING SPos THEN
									* Add History
									RINo = RINos<1,SPos>
									Send_Dyn(SerialNo:' <-> ':RINo)
									
								END ELSE
									SerialNos	= INSERT(SerialNos,1,SPos,0,SerialNo)
									
									*RINo		 = NextRINo	;********************************************************
									*NextRINo 	+= 1		;********************************************************
									
									RINo		= NextKey('REACT_ITEM')
									RINos		= INSERT(RINos,1,SPos,0,RINo)
									
									ItemType = ReactItemsRec<REACT_ITEMS_GRAPHITE_OR_TUBE$>
								
									ReactItemRec = ''
									ReactItemRec<REACT_ITEM_SERIAL$>	= SerialNo
									ReactItemRec<REACT_ITEM_DESC$> 		= ReactItemsRec<REACT_ITEMS_DESCRIPTION$>
									ReactItemRec<REACT_ITEM_ENTER_BY$>	= ReactItemsRec<REACT_ITEMS_ENTRY_ID$>
									ReactItemRec<REACT_ITEM_ENTER_DTM$>	= ICONV(OCONV(ReactItemsRec<REACT_ITEMS_ENTRY_DATE$>,'D4/'):' 00:12:00','DT')
									ReactItemRec<REACT_ITEM_RI_TYPE$>	= ReactItemsRec<REACT_ITEMS_GRAPHITE_OR_TUBE$>
									
									RetireDt = ReactItemsRec<REACT_ITEMS_INACTIVE_DATE$>
									
									IF RetireDt NE '' THEN
										ReactItemRec<REACT_ITEM_RETIRE_BY$> = 'BRYCE_BARB'
									END
									
									ReactItemRec<REACT_ITEM_RETIRE_DT$>	= RetireDt
								
									WRITE ReactItemRec ON ReactItemTable,RINo THEN
										Send_Dyn('Serial: ':SerialNo:' -> ':RINo:' / ':ReactItemRec)
									END
								END	;* End of check for existing Serial No
								
								
								
							END
							*/
							
						END ELSE
						
							
							RIHKey = ReactNo:'*':RINo:'*':InstDTM 
							READ ReactItemHistRec FROM ReactItemHistTable,RIHKey ELSE
							
								ReactHistRec = ''
								ReactHistRec<REACT_ITEM_HIST_INST_RL_ID$>			= ReactLogNo
								ReactHistRec<REACT_ITEM_HIST_INST_REACT_WFR_CNT$>	= ReactLogRec<REACTOR_LOG_REACT_WAFER_CNT$>
								ReactHistRec<REACT_ITEM_HIST_INST_REACT_HRS$>		= ReactLogRec<REACTOR_LOG_REACTORHOURS$>
								
								FixCnt += 1
								WRITE ReactHistRec ON ReactItemHistTable,RIHKey THEN
									Send_Dyn(FixCnt:' ':RIHKey)
								END
							END
						END
					END
				NEXT I
					
			END	
		END	;* End of check for EntryDate	
	END	;* End of ReactLogRec Read
		
	Send_Info(RecCnt:" - ":FixCnt)
	
REPEAT	

GOTO Bail


GOTO Bail
* * * * 


******************************************************

FOR ReactNo = 20 TO 74


	RIType	= 'W'

	HistKeys = obj_React_Item_Hist('GetHistory',ReactNo:@RM:@RM:RIType)
	HistCnt = COUNT(HistKeys,@VM) + (HistKeys NE '')
		
	IF HistCnt > 1 THEN
		
		FOR N = 2 TO HistCnt
			HistKey		= HistKeys<1,N>
			PrevHistKey = HistKeys<1,N-1>
			
			READ HistRec FROM ReactItemHistTable,HistKey THEN
				
				READ PrevHistRec FROM ReactItemHistTable,PrevHistKey THEN
					
				
					HistRec<REACT_ITEM_HIST_REM_DTM$>			= FIELD(PrevHistKey,'*',3)
					HistRec<REACT_ITEM_HIST_REM_RL_ID$>			= PrevHistRec<REACT_ITEM_HIST_INST_RL_ID$>
					HistRec<REACT_ITEM_HIST_REM_REACT_WFR_CNT$>	= PrevHistRec<REACT_ITEM_HIST_INST_REACT_WFR_CNT$>
					HistRec<REACT_ITEM_HIST_REM_REACT_HRS$>		= PrevHistRec<REACT_ITEM_HIST_INST_REACT_HRS$>
					HistRec<REACT_ITEM_HIST_REM_REASON$>		= ''
				
					
					*WRITE HistRec ON ReactItemHistTable,HistKey THEN
						Send_Dyn(ReactNo:' Closed: ':HistKey:' ':HistRec)
					*END
					
					
				END	;* End of Closing History Record
			END	;* End of History Record Read
		
		NEXT N	;* End of loop through History keys
	END	;* End of check for HistCnt > 1
NEXT ReactNo

GOTO Bail

GOTO Bail

************************************

SELECT ReactGraphiteTable

Done = 0
RecCnt = 0

LOOP
	READNEXT RGKey ELSE Done = 1
UNTIL Done
	READ RGRec FROM ReactGraphiteTable,RGKey THEN
	
		ReactNo		= RGKey[1,'*']
		GRType		= RGKey[COL2()+1,'*']		;* Not used in REACT_HIST_EQUATES
		InstDTM		= RGKey[COL2()+1,'*']
		
		RINo = RGRec<REACT_GRAPHITE_GR_NO$>
		
		RIHKey = ReactNo:'*':RINo:'*':InstDTM
	
		RIHRec = ''
		RIHRec<REACT_ITEM_HIST_INST_RL_ID$>			= RGRec<REACT_GRAPHITE_INST_RL_ID$>
		RIHRec<REACT_ITEM_HIST_INST_REACT_WFR_CNT$> = RGRec<REACT_GRAPHITE_INST_REACT_WFR_CNT$>
		RIHRec<REACT_ITEM_HIST_INST_REACT_HRS$>		= RGRec<REACT_GRAPHITE_INST_REACT_HRS$>
		RIHRec<REACT_ITEM_HIST_REM_DTM$>			= RGRec<REACT_GRAPHITE_REM_DTM$>
		RIHRec<REACT_ITEM_HIST_REM_RL_ID$>			= RGRec<REACT_GRAPHITE_REM_RL_ID$>
		
		RIHRec<REACT_ITEM_HIST_REM_REACT_WFR_CNT$>	= RGRec<REACT_GRAPHITE_REM_REACT_WFR_CNT$>
		RIHRec<REACT_ITEM_HIST_REM_REACT_HRS$>		= RGRec<REACT_GRAPHITE_REM_REACT_HRS$>
		RIHRec<REACT_ITEM_HIST_REM_REASON$>			= ''
		
		*WRITE RIHRec ON ReactItemHistTable,RIHKey THEN
			RecCnt += 1
			Send_Info(RecCnt)
		*END
		
		
	
	END ;* End of RGRecord read

REPEAT











* * * * * * *
Bail:
* * * * * * *

END
