COMPILE ROUTINE FIX_WO_LOG(Dummy)
ROWDEF(CHARSTR)

DECLARE SUBROUTINE obj_Schedule,Send_Dyn,Send_Info, obj_WO_Step, obj_Tables, obj_WO_Step, Send_Dyn
DECLARE SUBROUTINE Set_Status, RList, Reduce, FsMsg, Btree.Extract, obj_Order_Det
DECLARE FUNCTION Get_Status, Set_Printer, obj_Tables, obj_RDS, obj_Order_Det, obj_WO_Log

$INSERT WO_LOG_EQUATES
$INSERT WO_STEP_EQU
$INSERT PROD_SPEC_EQU
$INSERT WO_MAT_EQUATES
$INSERT REACT_RUN_EQUATES
$INSERT RDS_EQUATES
$INSERT ORDER_EQU
$INSERT ORDER_DET_EQU
$INSERT PROD_VER_EQUATES
$INSERT COMPANY_EQUATES
$INSERT REACTOR_EQUATES

$INSERT DICT_EQUATES

equ TARGET_ACTIVELIST$ to 5

OPEN 'WO_LOG' TO WOLogFile ELSE
	debug
	GOTO Bail
END

OPEN 'DICT.REACT_RUN' TO DictVar ELSE
	debug
	GOTO Bail
END

OPEN 'WO_MAT' TO WOMatFile ELSE
	debug
	GOTO Bail
END

OPEN 'DICT.WO_MAT' TO WOMatDict ELSE
	debug
	GOTO Bail
END


RList('SELECT WO_LOG WITH WO > "141530" AND WITH REACT_TYPE = ""',TARGET_ACTIVELIST$, "", "", "")

DEBUG

Done = 0
RecCnt = 0

LOOP
	READNEXT WONo ELSE Done = 0
UNTIL Done 
	RecCnt += 1
	
	Search = 'WO_NO':@VM:WONo:@FM
	
	Btree.Extract(Search,'REACT_RUN',DictVar,RDSNos,'','')
	
	IF Get_Status(errCode) THEN DEBUg
	
	rCnt = COUNT(RDSNos,@VM) + (RDSNos NE '')
	
	FOR N = 1 TO rCnt
		RDSNo = RDSNos<1,N>
		Reactor = XLATE('REACT_RUN',RDSNo,REACT_RUN_REACTOR$,'X')
		IF Reactor = '' THEN
			Reactor =  XLATE('RDS',RDSNo,RDS_REACTOR$,'X')
		END
	UNTIL Reactor NE ''
	NEXT N
	
	
	IF Reactor NE '' THEN
		ReactType = XLATE('REACTOR',Reactor,REACTOR_REACT_TYPE$,'X')
		
		READ WORec FROM WOLogFile,WONo THEN
			IF WORec<WO_LOG_REACT_TYPE$> = '' THEN
			
				WORec<WO_LOG_REACT_TYPE$> = ReactType
				
				WRITE WORec ON WOLogFile,WONo THEN
					Send_Info(RecCnt:' ':WONo)
				END
				
			END
		END
	END
	
	
	
	*Send_Dyn(RecCnt:' ':WONo)
	*
	

REPEAT


Send_Dyn(RecCnt:' ':WONo)

DEBUG



GOTO Bail



Done = 0
RecCnt = 0
LOOP
	READNEXT WONo ELSE Done = 1
UNTIL DONE

	READ WOLogRec FROM WOLogFile,WONo THEN
	
		RecCnt += 1
		
		WOLogRec = XLATE('WO_LOG',WONo,'','X')
		WOStepKeys = WOLogRec<WO_LOG_WO_STEP_KEY$>

		FOR I = 1 TO COUNT(WOStepKeys,@VM) + (WOStepKeys NE '')
		
			WOStepRec = XLATE('WO_STEP',WOStepKeys<1,I>,'','X')
				
			IF WOStepRec<WO_STEP_RDS_KEY$> NE '' THEN
				Send_Dyn(WONo:' Work Order Step currently has RDS records attached.')
			END

			IF WOStepRec<WO_STEP_WM_IN_KEYS$> NE '' THEN
				Send_Dyn(WONo:' Work Order Step currently has WM_IN records attached and may not be deleted.')
			END

			IF WOStepRec<WO_STEP_WM_OUT_KEYS$> NE '' THEN
				Send_Dyn(WONo:' Work Order Step currently has WM_OUT records attached and may not be deleted.')
			END
			
		NEXT I
		
		FOR I = 1 TO COUNT(WOStepKeys,@VM) + (WOStepKeys NE '')
			WOStepKey = WOStepKeys<1,I>
			CONVERT '*' TO @RM IN WOStepKey
			Set_Status(0)
			obj_WO_Step('Delete',WOStepKey)
			IF Get_Status(errCode) THEN
				GOTO Skip
			END
			
		NEXT I
		
		
		WOMatKeys = WOLogRec<WO_LOG_WO_MAT_KEY$>
		
		WOMatCnt = COUNT(WOMatKeys,@VM) + (WOMatKeys NE '')
		FOR I = 1 TO WOMatCnt
			WOMatKey = WOMatKeys<1,I>
			Send_Dyn(WOMatKey)
			
			WOMatRec = XLATE('WO_MAT',WOMatKey,'','X')
			IF WOMatRec<WO_MAT_RDS_NO$> NE '' THEN
				Send_Dyn(WOMatKey:' RDS No in record.')
			END
			
			IF WOMatRec<WO_MAT_WMI_KEY$> NE '' THEN
				Send_Dyn(WOMatKey:'WM_IN in record.')
			END
			
			IF WOmatRec<WO_MAT_WMO_KEY$> NE '' THEN
				Send_Dyn(WOMatKey:' WM_OUT in record.')
			END
			
			Set_Status(0)
			*obj_WO_Mat('Delete',WOMatKey)
			IF Get_Status(errCode) THEN
				GOTO Skip
			END
				
				

		NEXT I		

		TableVar = ''
		OtParms = 'WO_LOG':@RM:WONo:@RM:TableVar 
		WOLogRec = obj_Tables('DeleteRec',OtParms)			;* Locks and reads record for update

		*IF Get_Status(errCode) THEN ErrMsg(errCode)

		Send_Info(RecCnt:' ':WONo)
				
		
	END	;* End of WOLogRec read
	
Skip:

	
REPEAT


GOTO Bail

* * * * * * * * * *
RList('SELECT WO_LOG "152573" "152574" ',TARGET_ACTIVELIST$, "", "", "")

DEBUG

Done = 0
RecCnt = 0
LOOP
	READNEXT WONo ELSE Done = 1
UNTIL DONE

	READ WOLogRec FROM WOLogFile,WONo THEN
		
			SearchString = 'WO_NO':@VM:WONo:@FM
			Option = ''
			Flag = ''

			Btree.Extract(SearchString,'WO_MAT',WOMatDict, WOMatKeys, Option, Flag)
			
			IF Get_Status(errCode) THEN
				DEBUG
				GOTO Bail
			END
		
			CustPartNo = WOLogRec<WO_LOG_CUST_PART_NO$>
				
			WOMatCnt = COUNT(WOMatKeys,@VM) + (WOMatKeys NE '')
			
			WriteFlag = 0
			
			FOR I = 1 TO WOMatCnt
				WOMatKey = WOMatKeys<1,I>
				READ WOMatRec FROM WOMatFile,WOMatKey THEN
					WMCustPartNo = WOMatRec<WO_MAT_CUST_PART_NO$>
					
					IF 1 THEN			;*  WMCustPartNo NE CustPartNo THEN
					
						WriteFlag = 1
						
						RDSNos = WOMatRec<WO_MAT_RDS_NO$>
						
						IF RDSNos NE '' THEN
							*obj_RDS2('SetPartNo',RDSNos:@RM:CustPartNo)
							
							SWAP @VM WITH ', ' IN RDSNos
							Send_Dyn(RDSNos:' updated')
						END
						
						WOMatRec<WO_MAT_CUST_PART_NO$> = CustPartNo
						
						*WRITE WOMatRec ON WOMatFile,WOMatKey THEN
							Send_Dyn(WOMatKey:': ':WMCustPartNo:' -> ':CustPartNo)
						*END
					
					END	;* End of check for Cust Part No mismatch
				END	;* End of WOMatRec Read
							
			NEXT I
			
			IF WriteFlag = 1 THEN
				Send_Dyn('WO: ':WONo:' Completed')
				Send_DYN(' ')
			END
		END
	
REPEAT


GOTO Bail





RList('SELECT WO_LOG WITH PROD_ORD_NO NE "" AND WITH CUST_PART_NO = ""',TARGET_ACTIVELIST$, "", "", "")

Done = 0
RecCnt = 0
LOOP
	READNEXT WONo ELSE Done = 1
UNTIL DONE

	SearchString = 'WO_NO':@VM:WONo:@FM
	Option = ''
	Flag = ''

	Btree.Extract(SearchString,'WO_MAT',WOMatDict, WOMatKeys, Option, Flag)
	
	IF Get_Status(errCode) THEN
		DEBUG
		GOTO Bail
	END
	
	SortedWOMatKeys = ''
	
	WOMKeyCnt = COUNT(WOMatKeys,@VM) + (WOMatKeys NE '')
	
	FOR I = 1 TO WOMKeyCnt
		WOMatKey = WOMatKeys<1,I>
		LOCATE WOMatKey IN SortedWOMatKeys BY 'AR' USING @VM SETTING Pos ELSE
			SortedWoMatKeys = INSERT(SortedWOMatKeys,1,Pos,0,WOMatKey)
		END
	NEXT I
	
	READ WOLogRec FROM WOLogFile,WONo THEN
		WOLogRec<WO_LOG_WO_MAT_KEY$> = SortedWOMatKeys
		WRITE WOLogRec ON WOLogFile,WONo THEN
			Send_Dyn(WONo:' ':SortedWOMatKeys)
		END
	END

REPEAT



GOTO Bail


OrderNo		= 54439
OrderItems	= 2
UQuote		= 7203


WONo = obj_WO_Log('Create',OrderNo:@RM:OrderItems:@RM:UQuote)
		
IF Get_Status(errCode) THEN
	DEBUG
END ELSE
	obj_Order_Det('AddWONo',OrderNo:@RM:OrderItems:@RM:WONo)
	IF Get_Status(errCode) THEN
		DEBUG
	END
END		



GOTO Bail



RList('SELECT WO_LOG WITH ENTRY_ID = "SAP"',TARGET_ACTIVELIST$, "", "", "")

debug

Done = 0
RecCnt = 0
LOOP
	READNEXT WONo ELSE Done = 1
UNTIL DONE
	IF WONo NE 149927  AND WONo NE 149644 AND WONo NE 149954 THEN
		DELETE WOLogFile,WONo THEN
			Send_Dyn(WONo)
		END
	END

REPEAT


GOTO Bail

RList('SELECT WO_LOG WITH ENTRY_ID = "SAP"  ',TARGET_ACTIVELIST$, "", "", "")

debug

SelectCnt =  @RECCOUNT

Done = 0
RecCnt = 0
LOOP
	READNEXT WONo ELSE Done = 1
UNTIL Done 
	RecCnt += 1
	
	READ WORec FROM WOLogFile,WONo THEN
		OrderDetKey = WORec<WO_LOG_ORDER_NO$>:'*':WORec<WO_LOG_ORDER_ITEM$>
		
		DefPartNo = obj_Order_Det('PartNo',OrderDetKey:@RM)
		
		
		IF DefPartNo NE '' THEN
			
			IF WORec<WO_LOG_PART_NO$> NE DefPartNo THEN
			
				WOMatKeys = WORec<WO_LOG_WO_MAT_KEY$>
				
				IF WORec<WO_LOG_PART_NO$> NE DefPartNo THEN
				
					WORec<WO_LOG_PART_NO$> = DefPartNo
					WRITE WORec ON WOLogFile,WONo THEN
						Send_Info(WONo:' -> ':DefPartNo)
					END
				END
					
				WMCnt = COUNT(WOMatKeys,@VM) + (WOMatKeys NE '')
				
				FOR I = 1 TO WMCnt
					Set_Status(0)
					WOMatKey = WOMatKeys<1,I>
					READ WOMatRec FROM WOMatFile,WOMatKey THEN
						
						IF WOMatRec<WO_MAT_BOM_NO$> NE DefPartNo THEN
							
							WOMatRec<WO_MAT_BOM_NO$> = DefPartNo
							WRITE WOMatRec ON WOMatFile,WOMatKey THEN
								Send_Info(RecCnt:'/':SelectCnt:' ':WOMatKey:' -> ':DefPartNo)
							END
						END
					
					END
				NEXT I
				
				
					
			END
		END
	END

Send_Info(RecCnt)

REPEAT


*/


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


END
