COMPILE ROUTINE FIX_INVOICE(Dummy)
ROWDEF (CHARSTR)

DECLARE SUBROUTINE SEND_INFO, SEND_DYN, RList, obj_Schedule, Btree.Extract, Set_Status

$INSERT SCHEDULE_EQU
$INSERT INVOICE_EQU


OPEN 'INVOICE' TO InvoiceTable ELSE
	Send_Info('Puked on INVOICE table open.')
	GOTO Bail
END

debug
READ InvoiceRec FROM InvoiceTable,13576 THEN
	WRITE InvoiceRec ON InvoiceTable,135762 THEN
		DELETE InvoiceTable,13576 ELSE Null
	END
END


GOTO Bail





OPEN "SCHEDULE" TO SchedTable ELSE
	Send_Info('Puked on table open')
	GOTO Bail
END


OPEN 'DICT.INVOICE' TO DictVar ELSE
	Send_Info('Unable to open DICT.INVOICE')
	GOTO Bail
END



SearchLine = 'SHIP_DATE':@VM:'>=':ICONV('7/13','D'):@FM

Btree.Extract(SearchLine,'INVOICE',DictVar,InvKeys,'',Flag)

IF Get_Status(errCode) THEN debug



FOR I = 1 TO Count(InvKeys,@VM) + (InvKeys NE '')
	InvNo = InvKeys<1,I>
	READ InvRec FROM InvoiceTable,InvNo THEN
		SchedKeys = InvRec<INVOICE_SCHEDULE_IDS$>
		SchedKeyCnt = COUNT(SchedKeys,@VM) + (SchedKeys NE '')
		FOR N = 1 TO SchedKeyCnt
			SchedKey = SchedKeys<1,N>
			READ SchedRec FROM SchedTable,SchedKey THEN
				LOCATE InvNo IN SchedRec<SCHEDULE_INVOICE_IDS$> USING @VM Setting Pos ELSE
					Send_Dyn('Sched No: ':SchedKey:' - Inv No: ':InvNo)
					
					SetupCharge = OCONV(InvRec<INVOICE_SETUP_CHARGE$,N>,'MD2')
					SRPCharge	= OCONV(InvRec<INVOICE_SRP_CHARGE$,N>,'MD2')
					ShipQty		= InvRec<INVOICE_QUANTITY$,N>
					ShipPrice	= OCONV(InvRec<INVOICE_PRICE$,N>,'MD2')
					ShipVia		= InvRec<INVOICE_SHIP_VIA$>					;* Single
					Rejects		= InvRec<INVOICE_REJECTS$,N>
					Unprocessed	= InvRec<INVOICE_UNPROCESSED$,N>
					BoxCnt		= InvRec<INVOICE_BOX_CNT$>					;* Single
					ShipDt		= OCONV(InvRec<INVOICE_SHIP_DATE$>,'D4/')
					TrackNos	= InvRec<INVOICE_TRACKING_NO$>			
					
					
					Parms  = InvNo:@RM:SchedKey:@RM:SetupCharge:@RM:SRPCharge:@RM:ShipQty:@RM:ShipPrice:@RM
					Parms := ShipVia:@RM:Rejects:@RM:Unprocessed:@RM:BoxCnt:@RM:TrackNos:@RM:ShipDt
					
					Set_Status(0)
					obj_Schedule('AddInvoice',Parms)
					IF Get_Status(errCode) THEN
						debug
						Set_Status(0)
					END
				END
			END ;* End of SchedRec Read
		NEXT N
	END ;* End of Invoice Read
NEXT I


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

END
