COMPILE ROUTINE FIX_NCR(Dummy)
ROWDEF(CHARSTR)

DECLARE SUBROUTINE obj_Schedule,Send_Dyn,Send_Info,RList, obj_WO_Mat, Activate_Save_Select
DECLARE FUNCTION Get_Status, Set_Printer

$INSERT NCR_EQU
$INSERT RDS_EQU
$INSERT WO_MAT_EQUATES

OPEN 'NCR' To FileIn ELSE
	DEBUG
	GOTO Bail
END

OPEN 'DICT.NCR' TO @DICT ELSE
	DEBUG
	GOTO Bail
END

OPEN 'WO_MAT' TO WOMatfile ELSE
	DEBUG
	GOTO Bail
END

DEBUG


SelectSent = 'SELECT NCR BY WO_NO'

RList(SelectSent,5,'','','')

DEBUG

Done = 0
FixCnt = 0
RecCnt = 0
LOOP
	READNEXT NCRNo ELSE Done = 1
UNTIL Done 
	RecCnt += 1
	
	READ NCRRec FROM FileIn,NCRNo THEN
		WONo = NCRrec<NCR_WO_NO$>
		
		WORec = XLATE('WO_LOG',WONo,'','X')
		
		IF WORec = '' THEN
			*DELETE FileIn,NCRNo THEN
				FixCnt += 1
				Send_Dyn(RecCnt:' ':FixCnt:' ':NCRNo:' ':WONo)
			*END
		END
		
	END
REPEAT






GOTO Bail

SelectSent = 'SELECT NCR WITH DEPT_RESP = "Supplier" AND WITH LOSS_BY = "M"'

RList(SelectSent,5,'','','')

DEBUG

Done = 0
RecCnt = 0
FixCnt = 0
LOOP
	READNEXT NCRKey ELSE Done = 1
UNTIL Done
	
	RecCnt += 1
	
	READ NCRRec FROM FileIn,NCRKey THEN
		LossBy = NCRRec<NCR_LOSS_BY$>
		IF LossBy = 'M' THEN
			NCRRec<NCR_LOSS_BY$> = 'C'
		
			WRITE NCRRec ON FileIn,NCRKey THEN
				FixCnt += 1
				Send_Dyn(RecCnt:' ':FixCnt:' ':NCRKey)
			END
		END
	END
REPEAT



			

GOTO Bail

SelectSent = 'SELECT NCR WITH WO_NO > "128679" AND  WITH REACTOR_TYPE NE "P" '

RList(SelectSent,5,'','','')

DEBUG

Done = 0
RecCnt = 0
FixCnt = 0
LOOP
	READNEXT NCRKey ELSE Done = 1
UNTIL Done
	IF NOT(NUM(NCRKey)) THEN DEBUG
	RecCnt += 1
	

	READ NCRRec FROM FileIn,NCRKey THEN
	
		WriteFlag = 0
		
		WONo = NCRRec<62>
		WOMatCassNo = NCRRec<64>
		IF WONo NE '' AND WOMatCassNo NE '' THEN
		     WOMatKey =  NCRRec<62>:'*': NCRRec<64>
		END ELSE
		     WOMatKey = ''
		END
		
		IF WOMatKEy NE '' AND WOMatKEy NE '*' THEN
			
			READ WOMatRec FROM WOMatFile,WOMatKey THEN
			
				IF NCRRec<NCR_REJ_WAFER_ID$> = '' THEN
					NCRSlotNos = ''
					FOR N = 1 TO COUNT(NCRRec<NCR_SLOT_NO$>,@VM) + (NCRRec<NCR_SLOT_NO$> NE '')
						IF NCRRec<NCR_SLOT_NO$,N> = 1 THEN NCRSlotNos<1,-1> = N
					NEXT N
				
					SlotNos = ''
					RejWfrIds = ''
					FOR I = 1 TO 25
						IF WOMatRec<WO_MAT_SLOT_NCR$,I> = NCRKey THEN
							SlotNos<1,-1> = WOMatRec<WO_MAT_SLOT_NO$,I>
							RejWfrIds<1,-1> = WONo:'.':WOMatCassNo:'.':WOMatRec<WO_MAT_SLOT_NO$,I>
						END
					NEXT I
					
					IF NCRSlotNos NE '' AND NCRSlotNos NE SlotNos THEN
						Send_Dyn(WOMatKey:' ':NCRKey)
						
					END ELSE
						NCRRec<NCR_SLOT_NO$> = SlotNos
						NCRRec<NCR_REJ_WAFER_ID$> = RejWfrIds
						
						FixCnt += 1
						WRITE NCRRec ON FileIn,NCRKey THEN
							*Send_Dyn( 'NCR: ':NCRKey)
							Send_Info(RecCnt)
						END
					END
				END
											
				
				
					*WRITE WOMatRec ON WOMatFile,WOMatKey THEN
						*FixCnt += 1
						*Send_Dyn( WOMatKey:' ':WOMatRec<WO_MAT_NCR_KEYS$>)
						*Send_Info(RecCnt)
					*END
				
			END
		END

		
	END

REPEAT




IF Get_Status(errCode) THEN DEBUG


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


