COMPILE ROUTINE FIX_Order_Detail(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_EQU
$INSERT WO_LOG_EQU
$INSERT WO_STEP_EQU
$INSERT SCHEDULE_EQU
$INSERT RDS_EQU

$INSERT RLIST_EQUATES

EQU MT_ORG_QUOTE$	TO 1	;* Quote as found in Order Line Item
EQU MT_NEW_QUOTE$	TO 2	;* Quote as corrected for multi step processes
EQU MT_PSN$			TO 3	;* PSN(s) as found in corrected quote detail table -> position of PSN in this list gives step number
EQU MT_WO$			TO 4	;* WO(s) from Order Line Item - items multiple WO's will always use the second (or) only wo found here

OPEN 'ORDER' TO OrderTable  ELSE
	DEBUG
	GOTO Bail
END

SelectSent  =  'SELECT ORDER WITH ENTRY_DATE > =  "1 1 2004"'

RList(SelectSent,TARGET_ACTIVELIST$,'','','')

QuoteMappings = ''
QuoteMappings<1,1> = '4487'	; QuoteMappings<2,1> = '3957'
QuoteMappings<1,2> = '3615'	; QuoteMappings<2,2> = '2970'
QuoteMappings<1,3> = '3556'	; QuoteMappings<2,3> = '4075'

RecCnt = 0
Done = 0
LOOP
	READNEXT OrderNo ELSE Done = 1
UNTIL Done
	
	READ OrderRec FROM OrderTable,OrderNo THEN
		Send_Dyn(OrderNo)
		RecCnt += 1
		* Build the Map Table to allow mapping of old information into new format

		LineItemQuoteNos	= OrderRec<ORDER_QUOTE_IDS$>
		LineItemPSNs		= OrderRec<ORDER_PROD_SPEC_IDS$>
		LineItemWOs			= OrderRec<ORDER_WO$>
		LineItemScheds		= OrderRec<ORDER_SCHEDULE_IDS$>
		
		LineItemCnt	= COUNT(LineItemQuoteNos,@VM) + (LineItemQuoteNos NE '')
		
		MapTable = ''
		FOR I = 1 TO LineItemCnt
			LineItemQuoteNo = LineItemQuoteNos<1,I>
			LineItemWO		= LineItemWOs<1,I>
			LineItemPSN		= LineItemPSNs<1,I>
			
			LOCATE LineItemQuoteNo IN QuoteMappings<1> USING @VM SETTING Pos THEN
				NewQuoteNo = QuoteMappings<2,Pos>									;* Corrected Quote number for multilayer
				PSNs = XLATE('QUOTE',NewQuoteNo,33,'X')
			END ELSE
				NewQuoteNo = ''
				PSNs = XLATE('QUOTE',LineItemQuoteNo,33,'X')
			END
			
			IF INDEX(QuoteDetKeys,@VM,1) THEN
				* This needs to be done by hand - multiple Quote detail items. Probable SiGe
				Send_Dyn(OrderNo:' -> Multiple Quote Items')
				GOTO Skip
			END ELSE
				
				LOCATE LineItemPSN IN PSNs USING @VM SETTING Step ELSE
					Send_Dyn(OrderNo:' -> ':LineItemPSN:' not in ':PSNs)
					GOTO Skip
				END
				
				CONVERT @VM TO @SVM IN PSNs
			
				LOCATE LineItemQuoteNo IN MapTable<MT_ORG_QUOTE$> USING @VM SETTING LinePos THEN
					* Need to add the work orders to the right spot
					IF NewQuoteNo = '' THEN
						
						IF Step = 2 THEN
							MapTable<MT_WO$,LinePos> = LineItemWO
						END ELSE
							LOCATE LineItemWO IN MapTable<MT_WO$,LinePos> USING @SVM SETTING Dummy ELSE
								MapTable = INSERT(MapTable,MT_WO$,LinePos,Step,LineItemWO)
							END
						END
					END
					
				END ELSE
					MapTable = INSERT(MapTable,MT_ORG_QUOTE$,LinePos,0,LineItemQuoteNo)
					MapTable = INSERT(MapTable,MT_NEW_QUOTE$,LinePos,0,NewQuoteNo)
					MapTable = INSERT(MapTable,MT_WO$,LinePos,0,LineItemWO)
					IF NewQuoteNo = '' THEN
						MapTable = INSERT(MapTable,MT_PSN$,LinePos,0,PSNs)
					END ELSE
						MapTable = INSERT(MapTable,MT_PSN$,LinePos,0,'')
					END
				END		;* End of locate	
			END
		NEXT I
		
		* OK now we have a map table to work from so we can take another pass through the line items
		
		
		FOR I = 1 TO LineItemCnt
			
			LineItemQuoteNo = LineItemQuoteNos<1,I>
			LineItemWO		= LineItemWOs<1,I>
			LinePSN			= LineItemPSNs<1,I>
			LineItemSched	= LineItemScheds<1,I>
			
			LOCATE LineItemQuoteNo IN MapTable<MT_ORG_QUOTE$> USING @VM SETTING Pos THEN
				IF MapTable<MT_NEW_QUOTE$,Pos> NE '' THEN
					LineItemQuoteNo = MapTable<MT_NEW_QUOTE$,Pos>
					LOCATE LineItemQuoteNo IN MapTable<MT_ORG_QUOTE$> USING @VM SETTING Pos THEN
						PSNs = MapTable<MT_PSN$,Pos>
						WONo = MapTable<MT_WO$,Pos>
					END ELSE
						debug
						GOTO Skip
					END
				END ELSE
					PSNs = MapTable<MT_PSN$,Pos>
					WONo = MapTable<MT_WO$,Pos>
				END
				IF NOT(INDEX(PSNs,@SVM,1)) THEN
					* Not Multilayer - use the WONo from the line item
					 WONo = LineItemWO
				END
				
				LOCATE LinePSN IN PSNs USING @SVM SETTING Step ELSE Null		;* Find step from PSN numbers
				
				WOStepKey = WONo:'*':Step
				
			END ELSE
				debug
				GOTO Skip
			END	
			
			
			* WONo				= NEW system Work Order number and Step = Work Order Step in new system
			* Step				= NEW system QUOTE$ITEM_NO and WO STEP$PROC_STEP_NO
			* PSNs				= NEW system WO_STEP$PROD_SPEC_ID
			* LineItemQuoteNo	= NEW system QUOTE$QUOTE_NO
			* LineItemSched		= Existing system Schedule number on this item
			
			** Add OrderNo, QuoteNo, QuoteItemNo, Unit Price, Add_Chg_Desc and Add_Chg_Amt to WO_LOG record.
			** Read Sched Rec and add CassNo,CassLotNo, CassWaferQty,CassPartNo and CassSubPartNo to WO_LOG record.
			** Read/Create WOStep Record
			** Add ProdSpecIds, HotLots, MakeupBoxs and SchedDts to WOStep from Schedule record
			** Add WO_STEP to Schedule Record
			** Using RDS_IDS from Schedule record, add WO_STEP_KEY to RDS records
			** ONLY UPDATE IF NOT CORRECT -> Relational index attached
			
			* Extract Add_Chg_Desc and Add_Chg_Amt from line Item
			
			LotNo		= OrderRec<ORDER_LOT_NUM$,I>
			SetupChg	= OrderRec<ORDER_SETUP_CHARGE$,I>
			SRPChg		= OrderRec<ORDER_SRP_CHARGE$,I>
			PartNo		= OrderRec<ORDER_PART_NUM$,I>
			SubPartNo	= OrderRec<ORDER_SUB_PART_NUM$,I>
			UnitPrice	= OrderRec<ORDER_PRICE$,I>
			
			IF LotNo = '' OR (SetupChg NE '' OR SRPChg NE '') THEN
				AddChgDesc = PartNo
				IF SubPartNo NE '' THEN AddChgDesc := ' ':SubPartNo
				
				AddChgAmt = ''
				IF SetupChg NE ''	THEN AddChgAmt = SetupChg
				IF SRPChg NE ''		THEN AddChgAmt = SRPChg
				IF AddChgAmt = ''	THEN AddChgAmt = 0
			END ELSE
				AddChgDesc = ''
				AddChgAmt = ''
			END
			
			QuoteItemNo = 1		;* This code doesn't process any quotes with multiple Quote Det items.
			
			oWOParms = WONo:@RM:OrderNo:@RM:LineItemQuoteNo:@RM:QuoteItemNo:@RM:UnitPrice:@RM:LineItemSched:@RM:SubPartNo:@RM:AddChgDesc:@RM:AddChgAmt
			obj_WO_Log('AddOrderDetail',oWOParms)
			IF Get_Status(errCode) THEN
				DEBUG
				errMsg(errCode)
			END
			
			StepPSN = PSNs<1,1,Step>
			
			oWOStepParms = WONo:@RM:Step:@RM:StepPSN:@RM:OrderRec<ORDER_WO$,I>:@RM:OrderRec<ORDER_SCHEDULE_IDS$,I>
			obj_WO_Step('AddOrderDetail',oWOStepParms)
			
			IF Get_Status(errCode) THEN
				DEBUG
				errMsg(errCode)
			END
			
			oSchedParms = WONo:'*':Step:@RM:OrderRec<ORDER_SCHEDULE_IDS$,I>
			obj_Schedule('AddOrderDetail',oSchedParms)
			
			IF Get_Status(errCode) THEN
				DEBUG
				errMsg(errCode)
			END
			
		NEXT I
		
		Set_Status(0)
		obj_Order('OrderDet',OrderNo)
		Send_Info(OrderNo)
		IF Get_Status(errCode) THEN
			ErrMsg(errCode)
			debug
		END
Skip:

	END	;* End of order Record read

REPEAT


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

END

		
