COMPILE ROUTINE FIX_Order(Dummy)
ROWDEF(CHARSTR)

DECLARE SUBROUTINE obj_Schedule,Send_Dyn,Send_Infom, obj_Order, obj_WO_Log, RList, RList, ErrMsg
DECLARE SUBROUTIne obj_WO_Step, Set_Status, Send_Info
DECLARE FUNCTION Get_Status, Set_Printer

$INSERT ORDER_DET_EQU
$INSERT ORDER_EQU
$INSERT ORDER_CHANGE_EQU
$INSERT QUOTE_EQU
$INSERT COMPANY_EQU


EQU TARGET_ACTIVELIST$	TO 6


OPEN 'ORDER' TO OrderTable  ELSE
	DEBUG
	GOTO Bail
END

OPEN 'ORDER_DET' TO OrderDetTable ELSE
	DEBUG
	GOTO Bail
END

OPEN 'ORDER_CHANGE' TO OrderChangeTable ELSE
	DEBUG
	GOTO Bail
END

RList('SELECT ORDER WITH WO_KEYS <= "128597"',TARGET_ACTIVELIST$, "", "", "")

DEBUG

RecCnt = 0
Done = 0
LOOP
	READNEXT OrderNo ELSE Done = 1
UNTIL Done
	
	READ OrderRec FROM OrderTable,OrderNo THEN
		RecCnt += 1
		Send_Info(RecCnt)
		
		OrdDetKeys	= OrderRec<ORDER_ORDER_DET_KEY$>
		ChgNoKeys	= OrderRec<ORDER_ORDER_CHANGE_NO$>
		
		DetCnt = COUNT(OrdDetKeys,@VM) + (OrdDetKeys NE '')
		
		DelFlag = 1
		
		FOR I = 1 TO DetCnt
			OrdDetKey = OrdDetKeys<1,I>
			OrdDetRec = XLATE('ORDER_DET',OrdDetKey,'','X')
			IF OrdDetRec<ORDER_DET_SHIP_NO$> NE '' THEN
				DelFlag = 0
				Send_Dyn(OrdDetKey:' ShipNo: ':OrdDetRec<ORDER_DET_SHIP_NO$>)
			END ELSE
				IF OrdDetRec<ORDER_DET_INVOICE_NO$> NE '' THEN
					DelFlag = 0
					Send_Dyn(OrdDetKey:' InvNo: ':OrdDetRec<ORDER_DET_INVOICE_NO$>)
				END ELSE
					DELETE OrderDetTable,OrdDetKey THEN
						*Send_Dyn(OrdDetKey:' ':OrdDetKey:' removed')
					END
				END
			END
		NEXT I
		
		ChgCnt = COUNT(ChgNoKeys,@VM) + (ChgNoKeys NE '')
		FOR I = 1 TO ChgCnt
			ChgNoKey = ChgNoKeys<1,I>
			DELETE OrderChangeTable,ChgNoKey THEN
				*Send_Dyn('Change No: ':ChgNoKey:' removed')
			END
		
		NEXT I
			
		IF DelFlag = 1 THEN
		
			DELETE OrderTable,OrderNo THEN
				Send_Info(RecCnt:' Order: ':OrderNo)
			END
		END
		
	END
REPEAT

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

END
