COMPILE FUNCTION obj_Invoice(Method,Parms)

/*
	Methods for Invoice table
    
	02/28/2005 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Create(DataStruct)			;* Create new record
    CommInvoice(DataStruct)		;* Returns Commercial Invoice data structure
	
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, NextKey, obj_WM_Out, obj_Popup
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, obj_Notes, ErrMsg

$INSERT INVOICE_EQU
$INSERT ORDER_EQU
$INSERT ORDER_DET_EQU
$INSERT QUOTE_SPEC_EQU
$INSERT QUOTE_EQU
$INSERT PROD_SPEC_EQU
$INSERT RDS_EQU
$INSERT SCHEDULE_EQU
$INSERT WO_LOG_EQUATES
$INSERT WO_STEP_EQU
$INSERT WO_MAT_EQUATES
$INSERT COMPANY_EQU
$INSERT COC_EQU
$INSERT QUOTE_SIGS_EQU
$INSERT MSG_EQUATES


EQU COMM_INVOICE_SHIP_TO$			TO 1
EQU COMM_INVOICE_PHONE_TYPE$		TO 2
EQU COMM_INVOICE_PHONE_NO$			TO 3
EQU COMM_INVOICE_SHIP_DT$			TO 4
EQU COMM_INVOICE_PO$				TO 5
EQU COMM_INVOICE_SHIP_NO$			TO 6
EQU COMM_INVOICE_ORIGIN$			TO 7
EQU COMM_INVOICE_SHIP_VIA$			TO 8
EQU COMM_INVOICE_ITEM_ORD_NO$		TO 9
EQU COMM_INOVICE_ITEM_ORD_ITEM$		TO 10
EQU COMM_INVOICE_ITEM_QTY$			TO 11
EQU COMM_INVOICE_ITEM_DESC$			TO 12
EQU COMM_INVOICE_ITEM_PRICE$		TO 13
EQU COMM_INVOICE_ITEM_EXT_AMT$		TO 14
EQU COMM_INVOICE_SUB_SUPPLIED_BY$	TO 15
EQU COMM_INVOICE_INCO_CODE$			TO 16





ErrTitle = 'Error in Stored Procedure "obj_Invoice"'
ErrorMsg = ''

IF NOT(ASSIGNED(Method))	THEN ErrorMsg = 'Unassigned parameter "Method" passed to subroutine'
IF NOT(ASSIGNED(Parms))		THEN Parms = ''

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

Result = ''

BEGIN CASE
	CASE Method = 'Create'			; GOSUB Create
	CASE Method = 'CommInvoice'		; GOSUB CommInvoice
	CASE 1

END CASE

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

RETURN Result


* * * * * * *
Create:
* * * * * * *

ShipNo		= Parms[1,@RM]
ShipDt		= Parms[COL2()+1,@RM]
WONo		= Parms[COL2()+1,@RM]
WOSteps		= Parms[COL2()+1,@RM]
CassNos		= Parms[COL2()+1,@RM]
RDSNos		= Parms[COL2()+1,@RM]

IF ShipNo = ''		THEN ErrorMsg = 'Null Parameter "ShipNo" passed to routine. (':Method:')'
IF ShipDt = ''		THEN ErrorMsg = 'Null Parameter "ShipDt" passed to routine. (':Method:')'
IF WONo = ''		THEN ErrorMsg = 'Null Parameter "WONo" passed to routine. (':Method:')'
IF WOSteps = ''		THEN ErrorMsg = 'Null Parameter "WOSteps" passed to routine. (':Method:')' 
IF CassNos = ''		THEN ErrorMsg = 'Null Parameter "CassNos" passed to routine. (':Method:')'

IF NOT(ASSIGNED(RDSNos)) THEN RDSNos = ''

IF ErrorMsg NE '' THEN RETURN

thisShipDt = ICONV(ShipDt,'D')
IF thisShipDt = '' THEN
	ErrorMsg = 'Invalid Parameter "ShipDt" ':ShipDt:' passed to routine. (':Method:')'
	RETURN
END

WORec = XLATE('WO_LOG',WONo,'','X')

OrderNo		= WORec<WO_LOG_ORDER_NO$>
OrderRec	= XLATE('ORDER',OrderNo,'','X')

InvNo = NextKey('INVOICE')

InvRec = ''

InvRec<INVOICE_ORDER_NO$>	= OrderNo
InvRec<INVOICE_CUST_NO$> 	= OrderRec<ORDER_CUST_NO$>
InvRec<INVOICE_PO$>			= OrderRec<ORDER_PO_NO$>
InvRec<INVOICE_SHIP_NO$>	= ShipNo

InvRec<INVOICE_ENTRY_ID$>	= @USER4
InvRec<INVOICE_ENTRY_DATE$>	= Date()
InvRec<INVOICE_SHIP_DATE$>	= thisShipDt

InvRec<INVOICE_DUE_DATE$>	= ''
InvRec<INVOICE_STATUS$>		= ''

InvRec<INVOICE_BILL_TO_ATTN$>		= OrderRec<ORDER_BILL_TO_ATTN$>
InvRec<INVOICE_BILL_TO_ADDRESS$> 	= OrderRec<ORDER_BILL_TO_ADDRESS$>
InvRec<INVOICE_BILL_TO_CITY$>		= OrderRec<ORDER_BILL_TO_CITY$>
InvRec<INVOICE_BILL_TO_STATE$> 		= OrderRec<ORDER_BILL_TO_STATE$>
InvRec<INVOICE_BILL_TO_ZIP$> 		= OrderRec<ORDER_BILL_TO_ZIP$>
InvRec<INVOICE_SHIP_TO_ATTN$> 		= OrderRec<ORDER_SHIP_TO_ATTN$>
InvRec<INVOICE_SHIP_TO_ADDRESS$> 	= OrderRec<ORDER_SHIP_TO_ADDRESS$>
InvRec<INVOICE_SHIP_TO_CITY$> 		= OrderRec<ORDER_SHIP_TO_CITY$>
InvRec<INVOICE_SHIP_TO_STATE$> 		= OrderRec<ORDER_SHIP_TO_STATE$>
InvRec<INVOICE_SHIP_TO_ZIP$> 		= OrderRec<ORDER_SHIP_TO_ZIP$>
InvRec<INVOICE_TERMS$> 				= OrderRec<ORDER_TERMS$>
InvRec<INVOICE_SHIP_VIA$> 			= OrderRec<ORDER_SHIP_VIA$>
InvRec<INVOICE_ACCOUNT_NO$> 		= OrderRec<ORDER_ACCOUNT_NO$>
InvRec<INVOICE_BILL_TO_COMPANY$> 	= OrderRec<ORDER_BILL_TO_COMPANY$>
InvRec<INVOICE_SHIP_TO_COMPANY$> 	= OrderRec<ORDER_SHIP_TO_COMPANY$>
InvRec<INVOICE_INVOICE_NOTES$>		= OrderRec<ORDER_INVOICE_NOTES$>
InvRec<INVOICE_PLANT$>				= 'M'		;* Mesa

* Stuff to load from the RDS records that were shipped

InvRec<INVOICE_ORDER_LINE_ITEM$>	= ''
InvRec<INVOICE_QUOTE_IDS$>			= ''
InvRec<INVOICE_SCHEDULE_IDS$>		= ''
InvRec<INVOICE_WO$>					= ''
InvRec<INVOICE_PART_NUM$>			= ''
InvRec<INVOICE_LOT_NUM$>			= ''
InvRec<INVOICE_SETUP_CHARGE$>		= ''
InvRec<INVOICE_SRP_CHARGE$>			= ''
InvRec<INVOICE_QUANTITY$>			= ''
InvRec<INVOICE_PRICE$>				= ''
InvRec<INVOICE_GL_ACCT$>			= ''
InvRec<INVOICE_REJECTS$>			= ''
InvRec<INVOICE_UNPROCESSED$>		= ''



IF RDSNos NE '' THEN
	FOR I = 1 TO COUNT(RDSNos,@VM) + (RDSNos NE '')

		RDSNo = RDSNos<1,I>
		RDSRec = XLATE('RDS',RDSNo,'','X')
		CassNo = CassNos<1,I>
		
		WaferQty = XLATE('WO_MAT',WONo:'*':CassNo,'CURR_WFR_CNT','X')
		
		OrderItem	= RDSRec<RDS_ORDER_ITEM$>
		QuoteNo		= RDSRec<RDS_QUOTE_NO$>
		LotNo		= RDSRec<RDS_LOT_NUM$>
		PartNo		= RDSRec<RDS_PART_NUM$>
		*WaferQty	= XLATE('RDS',RDSNo,'WFRS_OUT','X')
		Rejects		= RDSRec<RDS_WAFERS_IN$> - WaferQty
		ProdSpecID	= RDSRec<RDS_PROD_SPEC_ID$>
		GLCode		= XLATE('PROD_SPEC',ProdSpecID,'SUB_PRODUCT','X')
		GLCode		= GLCode[1,'-']
		
		UnitPrice = XLATE('ORDER_DET',OrderNo:'*':OrderItem,ORDER_DET_UNIT_PRICE$,'X')
		
		LOCATE LotNo IN InvRec<INVOICE_LOT_NUM$> USING @VM SETTING Pos THEN
			InvRec<INVOICE_QUANTITY$,Pos> = InvRec<INVOICE_QUANTITY$,Pos> + WaferQty
		END ELSE
			InvRec = INSERT(InvRec,INVOICE_LOT_NUM$,Pos,0,LotNo)
			InvRec = INSERT(InvRec,INVOICE_QUOTE_IDS$,Pos,0,QuoteNo)
			InvRec = INSERT(InvRec,INVOICE_SCHEDULE_IDS$,Pos,0,'')
			InvRec = INSERT(InvRec,INVOICE_WO$,Pos,0,WoNo)
			InvRec = INSERT(InvRec,INVOICE_PART_NUM$,Pos,0,PartNo)
			InvRec = INSERT(InvRec,INVOICE_SETUP_CHARGE$,Pos,0,'')
			InvRec = INSERT(InvRec,INVOICE_SRP_CHARGE$,Pos,0,'')
			InvRec = INSERT(InvRec,INVOICE_QUANTITY$,Pos,0,WaferQty)
			InvRec = INSERT(InvRec,INVOICE_PRICE$,Pos,0,UnitPrice)
			InvRec = INSERT(InvRec,INVOICE_GL_ACCT$,Pos,0,GLCode)
			InvRec = INSERT(InvRec,INVOICE_REJECTS$,Pos,0,Rejects)
			InvRec = INSERT(InvRec,INVOICE_UNPROCESSED$,Pos,0,'')
			InvRec = INSERT(InvRec,INVOICE_OD_ITEM_NO$,Pos,0,OrderItem)
		END
	NEXT I
END ELSE
	
	QuoteNo = XLATE('WO_LOG',WONo,'QUOTE_NO','X')

	FOR I = 1 TO COUNT(CassNos,@VM) + (CassNos NE '')
		CassNo = CassNos<1,I>
		WOStep = WOSteps<1,I>
		
		InboundCassData = obj_WM_Out('InCassData',WONo:'*':WOStep:'*':CassNo:@RM )
		
		FOR N = 1 TO COUNT(InboundCassData<1>,@VM) + (InboundCassData<1> NE '')
			IF InboundCassData<1,N>[1,'*'] = WONo THEN
				InCassNo	= InboundCassData<1,N>[-1,'B*']
				InLotNo		= InboundCassData<2,N>
				InPartNo	= InboundCassData<3,N>
			END
		NEXT N
		
		IF NOT(ASSIGNED(InCassNo)) THEN
			InCassNo	= InboundCassData<1,1>[-1,'B*']
			InLotNo		= InboundCassData<2,1>
			InPartNo	= InboundCassData<3,1>
		END
		
		WOMatKey = WONo:'*':InCassNo
		
		WOMatRec		= XLATE('WO_MAT',WOMatKey,'','X')
		OrderItem		= WOMatRec<WO_MAT_ORDER_ITEM$>
		LotNo 			= WOMatRec<WO_MAT_LOT_NO$>
		PartNo			= WOMatRec<WO_MAT_CUST_PART_NO$>
		
		
		WaferQty	= XLATE('WM_OUT',WONo:'*':WOStep:'*':CassNo,'WFRS_OUT','X')
		Rejects		= XLATE('WM_OUT',WONo:'*':WOStep:'*':CassNo,'WFRS_SCHED','X') - WaferQty
		
		ProdSpecID	= XLATE('WO_STEP',WONo:'*':WOStep,WO_STEP_PROD_SPEC_ID$,'X')
		GLCode		= XLATE('PROD_SPEC',ProdSpecID,'SUB_PRODUCT','X')
		GLCode		= GLCode[1,'-']
		
		UnitPrice	= XLATE('ORDER_DET',OrderNo:'*':OrderItem,ORDER_DET_UNIT_PRICE$,'X')
		
		LOCATE LotNo IN InvRec<INVOICE_LOT_NUM$> USING @VM SETTING Pos THEN
			InvRec<INVOICE_QUANTITY$,Pos> = InvRec<INVOICE_QUANTITY$,Pos> + WaferQty
		END ELSE
			InvRec = INSERT(InvRec,INVOICE_LOT_NUM$,Pos,0,LotNo)
			InvRec = INSERT(InvRec,INVOICE_QUOTE_IDS$,Pos,0,QuoteNo)
			InvRec = INSERT(InvRec,INVOICE_SCHEDULE_IDS$,Pos,0,'')
			InvRec = INSERT(InvRec,INVOICE_WO$,Pos,0,WoNo)
			InvRec = INSERT(InvRec,INVOICE_PART_NUM$,Pos,0,PartNo)
			InvRec = INSERT(InvRec,INVOICE_SETUP_CHARGE$,Pos,0,'')
			InvRec = INSERT(InvRec,INVOICE_SRP_CHARGE$,Pos,0,'')
			InvRec = INSERT(InvRec,INVOICE_QUANTITY$,Pos,0,WaferQty)
			InvRec = INSERT(InvRec,INVOICE_PRICE$,Pos,0,UnitPrice)
			InvRec = INSERT(InvRec,INVOICE_GL_ACCT$,Pos,0,GLCode)
			InvRec = INSERT(InvRec,INVOICE_REJECTS$,Pos,0,Rejects)
			InvRec = INSERT(InvRec,INVOICE_UNPROCESSED$,Pos,0,'')
			InvRec = INSERT(InvRec,INVOICE_OD_ITEM_NO$,Pos,0,OrderItem)
		END

	NEXT I
END



FOR I = 1 TO COUNT(InvRec<INVOICE_LOT_NUM$>,@VM) + (InvRec<INVOICE_LOT_NUM$> NE '')
	InvRec<INVOICE_ORDER_LINE_ITEM$,I> = I
NEXT I

obj_Tables('WriteRec','INVOICE':@RM:InvNo:@RM:@RM:InvRec)


RETURN


* * * * * * * 
CommInvoice:
* * * * * * *

ShipNo	= Parms[1,@RM]

IF ShipNo	= '' THEN
	ErrorMsg = 'Null parameter "ShipNo" passed to routine. (':Method:')' 
	RETURN
END

ShipRec = XLATE('COC',ShipNo,'','X')

ShipVia				= ShipRec<COC_SHIP_VIA$>
TrackingNos			= ShipRec<COC_TRACKING_NOS$>
BoxCnts				= ShipRec<COC_TRACK_NO_BOX_CNT$>
ShipWeights			= ShipRec<COC_TRACK_NO_SHIP_WT$>

InvRec = ''

* Build line item details

WorkOrderNo	= ShipRec<COC_WO$>
WORec		= XLATE('WO_LOG',WorkOrderNo,'','X')

FirstStepPSN = XLATE('WO_STEP',WorkOrderNo:'*1',WO_STEP_PROD_SPEC_ID$,'X')
SpecSubstrate = XLATE('PROD_SPEC',FirstStepPSN,PROD_SPEC_SPEC_SUBSTRATE$,'X')

WaferDesc	= SpecSubstrate<1,QSSubWafersize$>:' silicon wafers'
SubSupplyBy = SpecSubstrate<1,QSSubWafersSupBy$>					;* C - Customer, L - EPI Services

SWAP ' mm ' WITH 'mm/' IN WaferDesc
SWAP ' in' WITH 'in' IN WaferDesc

WOCustNo		= WORec<WO_LOG_CUST_NO$>


ShipToInfo		= XLATE('COMPANY',WOCustNo,'SHIP_TO_INFO','X') 		;*XLATE('ORDER',OrderNo,'SHIP_TO_INFO','X')
INCOCode		= WORec<WO_LOG_INCO_CODE$>							;*OrderRec<ORDER_INCO_CODE$>

INCOCodeDesc = obj_Popup('CodeDesc','INCO_CODES':@RM:INCOCode)

InvRec = ''
InvRec<COMM_INVOICE_SHIP_TO$> = ShipToInfo
InvRec<COMM_INVOICE_INCO_CODE$> = INCOCode

CustNo = WORec<WO_LOG_CUST_NO$>
CustRec = XLATE('COMPANY',CustNo,'','X')

CompPhones			= CustRec<COMPANY_PHONES$>
CompPhoneTypes		= CustRec<COMPANY_PHONE_TYPES$>
CompIntPhones		= CustRec<COMPANY_INTL_PHONES$>
CompIntPhoneTypes	= CustRec<COMPANY_INTL_PHONE_TYPES$>

Phones = ''
PhoneTypes = ''
IF CompPhones NE '' THEN
	Phones<1,-1> = CompPhones
	PhoneTypes<1,-1> = CompPhoneTypes
END
IF CompIntPhones NE '' THEN
	Phones<1,-1> = CompIntPhones
	PhoneTypes<1,-1> = CompIntPhoneTypes
END

InvRec<COMM_INVOICE_PHONE_TYPE$>		= PhoneTypes
InvRec<COMM_INVOICE_PHONE_NO$>			= Phones
InvRec<COMM_INVOICE_SHIP_DT$>			= ShipRec<COC_SHIP_DT$>
InvRec<COMM_INVOICE_PO$>				= WORec<WO_LOG_CUST_PO_NO$>
InvRec<COMM_INVOICE_SHIP_NO$>			= ShipNo
InvRec<COMM_INVOICE_ORIGIN$>			= XLATE('COMPANY',WOCustNo,COMPANY_COUNTRY$,'X')
InvRec<COMM_INVOICE_SHIP_VIA$>			= ShipRec<COC_SHIP_VIA$>
InvRec<COMM_INVOICE_SUB_SUPPLIED_BY$>	= SubSupplyBy


Captive		= XLATE('COMPANY',WOCustNo,COMPANY_CAPTIVE$,'X')
Consignment = XLATE('COMPANY',WOCustNo,COMPANY_CONSIGNMENT$,'X')

BEGIN CASE
	CASE Captive		; Ordertype = 'CAPTIVE'
	CASE Consignment	; OrderType = 'CAPTIVE'
	CASE 1				; OrderType = 'MERCHANT'
END CASE


* Build the Order Line Item Detail from RDS records using local methods

OrderShippedQtys = ''
OrderRejQtys = ''
OrderRDSNos = ''

RDSNos = ShipRec<COC_RDS_NO$>

WOCassNos = ShipRec<COC_CASS_NO$>

IF RDSNos = '' THEN
	WOStepNos = ShipRec<COC_WO_STEP$>
	
	FOR I = 1 TO COUNT(WOCassNos,@VM) + (WOCassNos NE '')
		WOStepNo = WOStepNos<1,I>
		WOCassNo = WOCassNos<1,I>

		WfrsOut = XLATE('WM_OUT',WorkOrderNo:'*':WOStepNo:'*':WOCassNo,'WAFER_CNT','X')
		Rejects = 0
		
		OrderShippedQtys	= OrderShippedQtys> + WfrsOut
		OrderRejQtys		= OrderRejQtys + Rejects	
		OrderRDSNos<1,-1> 	= WorkOrderNo:'.':WOStepNo:'.':WOCassNo			;* Substitute CassID for RDS
	NEXT I
	
END ELSE
	
	CassCnt =  COUNT(WOCassNos,@VM) + (WOCassNos NE '')
	
	FOR I = 1 TO CassCnt
		WOMatKey = WorkOrderNo:'*':WOCassNos<1,I>
		
		CassRejQty 		= XLATE('WO_MAT',WOMatKey,'CURR_WFR_CNT_REJ','X')
		CassShipQty		= XLATE('WO_MAT',WOMatKey,'CURR_WFR_CNT','X')
		CassPTQty		= XLATE('WO_MAT',WOMatKey,'CURR_WFR_CNT_PT','X')
		
		IF Captive OR Consignment THEN
			CassRejQty = 0
		END ELSE
			CassRejQty += CassPTQty
		END
		
		OrderShippedQtys	= OrderShippedQtys + CassShipQty
		OrderRejQtys		= OrderRejQtys + CassRejQty
		
	NEXT I
	
END


CustPONo = WORec<WO_LOG_CUST_PO_NO$>

TotalWafersOrdered = WORec<WO_LOG_WO_QTY$>

TotalShipWfrQty		= XLATE('COC',ShipNo,'TOT_QTY','X')									;* Total Good Wafers on Shipment
TotalShipRejQty		= XLATE('COC',ShipNo,'TOT_REJ','X')									;* Total Reject Wafers in Shipment
TotalShipProdQty	= XLATE('COC',ShipNo,'TOT_PROD','X')

TotalShipmentWafers = TotalShipWfrQty + TotalShipRejQty + TotalShipProdQty				;* Total wafers in this shipment

PrevShipNos	= XLATE('WO_LOG',WorkOrderNo,'SHIP_NO','X')

Done = 0
LOOP
UNTIL Done
	LOCATE ShipNo IN PrevShipNos USING @VM SETTING Pos THEN
		PrevShipNos = DELETE(PrevShipNos,1,Pos,0)											;* Remove current shipment from list
	END ELSE
		Done = 1
	END
REPEAT

PrevSRP_MiscQtys = XLATE('COC',PrevShipNos,'ITEM_MISC_QTY','X')
CONVERT @VM TO '' IN PrevSRP_MiscQtys

IF PrevSRP_MiscQtys NE '' THEN SurchargesSent = 1 ELSE SurchargesSent = 0

TotPrevShipQtys 	= SUM(XLATE('COC',PrevShipNos,'TOT_WAFERS_SHIPPED','X'))				;* All wafers shipped on previous shipments against this Work Order
TotPrevShipWfrQty 	= SUM(XLATE('COC',PrevShipNos,'TOT_QTY','X'))
TotPrevShipRejQty 	= SUM(XLATE('COC',PrevShipNos,'TOT_REJ','X'))
TotPrevShipProdQty 	= SUM(XLATE('COC',PrevShipNos,'TOT_PROD','X'))


EpiPN = WORec<WO_LOG_EPI_PART_NO$>

QuoteNo		= WORec<WO_LOG_QUOTE_NO$>
QuoteRec	= XLATE('QUOTE',QuoteNo,'','X')

CommInvUnitPrice			= QuoteRec<QUOTE_COMM_INVOICE_UNIT_PRICE$>				;* Commercial Invoice Override pricing from Quote
CommInvRejUnitPrice			= QuoteRec<QUOTE_COMM_INVOICE_REJ_UNIT_PRICE$>
CommInvAddItemQtys			= QuoteRec<QUOTE_COMM_INVOICE_ADD_ITEM_QTY$>			;* Additional line items added to bottom of invoice
CommInvAddItemDescs			= QuoteRec<QUOTE_COMM_INVOICE_ADD_ITEM_DESC$>
CommInvAddItemUnitPrices	= QuoteRec<QUOTE_COMM_INVOICE_ADD_ITEM_UNIT_PRICE$>
CommInvTestWfrQtys			= QuoteRec<QUOTE_COMM_INVOICE_TEST_WFR_QTY$>			;* Test wafer charges override information
CommInvTestWfrDescs			= QuoteRec<QUOTE_COMM_INVOICE_TEST_WFR_DESC$>
CommInvTestWfrUnitPrices	= QuoteRec<QUOTE_COMM_INVOICE_TEST_WFR_UNIT_PRICE$>

LineCnt = 0

IF OrderShippedQtys > 0  THEN
				
	LineCnt += 1
	
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_ORD_NO$,LineCnt,0,WorkOrderNo)
	InvRec = INSERT(InvRec,COMM_INOVICE_ITEM_ORD_ITEM$,LineCnt,0,FIELD(OrderItemKey,'*',2))
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_QTY$,LineCnt,0,ItemQty)
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_DESC$,LineCnt,0,WaferDesc)
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_PRICE$,LineCnt,0,UnitPrice)
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_EXT_AMT$,LineCnt,0,OCONV(ICONV(UnitPrice*ItemQty,'MD32'),'MD32,$'))
	
END	;* End of check for Shipped Qtys

	
IF CommInvTestWfrQtys NE ''   THEN
	
	LineCnt += 1
	OrderItemKey = LineCnt:'R'						;* Add an 'R' for Rejects and change the pricing
	
	ItemQty		= CommInvTestWfrQtys
	ItemDesc	= CommInvTestWfrDescs
	UnitPrice	= OCONV(CommInvTestWfrUnitPrices,'MD2,')
	
	InvoicedOrderDetKeys = INSERT(InvoicedOrderDetKeys,1,LineCnt,0,OrderItemKey)
		
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_ORD_NO$,LineCnt,0,OrderNo)
	InvRec = INSERT(InvRec,COMM_INOVICE_ITEM_ORD_ITEM$,LineCnt,0,FIELD(OrderItemKey,'*',2))
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_QTY$,LineCnt,0,ItemQty)
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_DESC$,LineCnt,0,ItemDesc)
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_PRICE$,LineCnt,0,UnitPrice)
	InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_EXT_AMT$,LineCnt,0,OCONV(ICONV(UnitPrice*ItemQty,'MD32'),'MD32,$'))
		
	
END

IF OrderItemRejQtys > 0 THEN
	IF CommInvRejUnitPrice NE '' THEN
		UnitPrice = OCONV(CommInvRejUnitPrice,'MD32')
	END ELSE
		UnitPrice = OCONV(100,'MD32')
	END
	
	ItemQty = OrderItemRejQtys<1,I>
	OrderItemKey := 'R'						;* Add an 'R' for Rejects and change the pricing
	
	LOCATE OrderItemKey IN InvoicedOrderDetKeys BY 'AR' USING @VM SETTING Pos THEN
		InvRec<COMM_INVOICE_ITEM_QTY$,Pos> = InvRec<COMM_INVOICE_ITEM_QTY$,Pos> + ItemQty
		ExtItemQty = InvRec<COMM_INVOICE_ITEM_QTY$,Pos>
		InvRec<COMM_INVOICE_ITEM_EXT_AMT$,Pos> = OCONV(ICONV(UnitPrice*ExtItemQty,'MD32'),'MD32,$')
	
	END ELSE
			
		InvoicedOrderDetKeys = INSERT(InvoicedOrderDetKeys,1,Pos,0,OrderItemKey)
		
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_ORD_NO$,Pos,0,OrderNo)
		InvRec = INSERT(InvRec,COMM_INOVICE_ITEM_ORD_ITEM$,Pos,0,FIELD(OrderItemKey,'*',2))
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_QTY$,Pos,0,ItemQty)
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_DESC$,Pos,0,WaferDesc:'(Rejects/Test)')
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_PRICE$,Pos,0,UnitPrice)
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_EXT_AMT$,Pos,0,OCONV(ICONV(UnitPrice*ItemQty,'MD32'),'MD32,$'))
	END
END




IF CommInvAddItemQtys NE '' THEN
	LineCnt = COUNT(CommInvAddItemQtys,@VM) + (CommInvAddItemQtys NE '') + 1
	FOR I = 1 TO COUNT(CommInvAddItemQtys,@VM) + (CommInvAddItemQtys NE '')
		OrderItemKey = OrderNo:'**'
		
		ItemQty		= CommInvAddItemQtys<1,I>
		ItemDesc	= CommInvAddItemDescs<1,I>
		UnitPrice	= OCONV(CommInvAddItemUnitPrices<1,I>,'MD2,')
	
		InvoicedOrderDetKeys = INSERT(InvoicedOrderDetKeys,1,LineCnt + I,0,OrderItemKey)
				
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_ORD_NO$,LineCnt + I,0,OrderNo)
		InvRec = INSERT(InvRec,COMM_INOVICE_ITEM_ORD_ITEM$,LineCnt + I,0,'-')
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_QTY$,LineCnt + I,0,ItemQty)
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_DESC$,LineCnt + I,0,ItemDesc)
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_PRICE$,LineCnt + I,0,UnitPrice)
		InvRec = INSERT(InvRec,COMM_INVOICE_ITEM_EXT_AMT$,LineCnt + I,0,OCONV(ICONV(UnitPrice*ItemQty,'MD32'),'MD32,$'))
	
	NEXT I
END

Result = InvRec

RETURN




