COMPILE FUNCTION obj_Order_Det(Method,Parms)
/*
	Methods for the Order Detail (ORDER_DET) table
    
	01/15/2005 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Create(OrdNo,ItemNo,QuoteNo,WONo,ItemDesc,ItemQty,UnitPrice)			;* Create new WO Step entry 	
   
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Dialog_Box,NextKey, Popup, obj_Quote
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, Send_Dyn, Btree.Extract, ErrMsg, Send_Dyn, RList

$INSERT MSG_EQUATES
$INSERT WO_LOG_EQU
$INSERT QUOTE_EQU
$INSERT ORDER_EQU
$INSERT ORDER_DET_EQU
$INSERT PROD_SPEC_EQU
$INSERT QUOTE_SPEC_EQU
$INSERT EPI_SUSCEPTOR_EQUATES

EQU TARGET_ACTIVELIST$ TO 5


ErrTitle = 'Error in Stored Procedure "obj_OrderDet"'
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 = 'CurrStatus'			; GOSUB CurrStatus
	CASE Method = 'AddWONo'				; GOSUB AddWONo
	CASE Method = 'AddVisionInvoice'	; GOSUB AddVisionInvoice
	CASE Method = 'InvoicedQty'			; GOSUB InvoicedQty
	CASE Method = 'Cancel'				; GOSUB Cancel
	CASE Method = 'EpiPROAdd'			; GOSUB EpiPROAdd
	CASE Method = 'ProdVerNo'			; GOSUB ProdVerNo
	CASE Method = 'PartNo'				; GOSUB ProdVerNo
	CASE Method = 'ActiveProdVerNo'		; GOSUB ActiveProdVerNo
	
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to routine.'

END CASE

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

RETURN Result



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

OrderNo				= Parms[1,@RM]
ItemNo				= Parms[COL2()+1,@RM]
QuoteNo				= Parms[COL2()+1,@RM]
EpiPN				= Parms[COL2()+1,@RM]
ItemDesc			= Parms[COL2()+1,@RM]
UnitPrice			= Parms[COL2()+1,@RM]
ItemQty				= Parms[COL2()+1,@RM]
WONo				= Parms[COL2()+1,@RM]
VisionItemNo		= Parms[COL2()+1,@RM]
VisionItemStatus	= Parms[COL2()+1,@RM]
VisionPartType		= Parms[COL2()+1,@RM]
CustPartNo			= Parms[COL2()+1,@RM]
VisionShipQty		= Parms[COL2()+1,@RM]
PromiseDt			= Parms[COL2()+1,@RM]
ReqShipDt			= Parms[COL2()+1,@RM]
ShipWinOpenDt		= Parms[COL2()+1,@RM]
CustReqDt			= Parms[COL2()+1,@RM]

IF OrderNo 		= '' THEN ErrorMsg = 'Null parameter "OrderNo" passed to routine. (':Method:')'
IF ItemNo		= '' THEN ErrorMsg = 'Null parameter "ItemNo" passed to routine. (':Method:')'
IF QuoteNo		= '' THEN ErrorMsg = 'Null parameter "QuoteNo" passed to routine. (':Method:')'
IF EpiPN		= '' THEN Null	;* Not used during conversion

OrderDetKey = OrderNo:'*':ItemNo

PSNo		= XLATE('QUOTE',QuoteNo,QUOTE_PROC_STEP_PSN$,'X')<1,1>		;* Field is multivalued - take the first one
SuppliedBy	= XLATE('PROD_SPEC',PSNo,'SUB_SUPPLIED_BY','X')

TableVar = ''
OtParms = 'ORDER_DET':@RM:OrderDetKey:@RM:TableVar 
OrderDetRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update So OK maybe it's not STRICTLY a create method...

OrderDetRec = ''
OrderDetRec<ORDER_DET_QUOTE_NO$>			= QuoteNo
OrderDetRec<ORDER_DET_EPI_PN$>				= EpiPN
OrderDetRec<ORDER_DET_ITEM_DESC$>			= ItemDesc
OrderDetRec<ORDER_DET_UNIT_PRICE$>			= ICONV(UnitPrice,'MD2')
OrderDetRec<ORDER_DET_ITEM_QTY$>			= ICONV(ItemQty,'MD0')
OrderDetRec<ORDER_DET_WO_NO$>				= WONo
OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$>	= VisionItemStatus
OrderDetRec<ORDER_DET_VISION_PART_TYPE$>	= VisionPartType
OrderDetRec<ORDER_DET_CUST_PN$>				= CustPartNo
OrderDetRec<ORDER_DET_VISION_SHIP_QTY$>		= ICONV(VisionShipQty,'MD0')
OrderDetRec<ORDER_DET_PROMISE_DT$>			= ICONV(PromiseDt,'D')
OrderDetRec<ORDER_DET_REQ_SHIP_DT$>			= ICONV(ReqShipDt,'D')
OrderDetRec<ORDER_DET_SHIP_WIN_DT$>			= ICONV(ShipWinOpenDt,'D')
OrderDetRec<ORDER_DET_CUST_REQ_DT$>			= ICONV(CustReqDt,'D')
OrderDetRec<ORDER_DET_VISION_LINE_NO$>		= VisionItemNo
OrderDetRec<ORDER_DET_SUPPLIED_BY$>			= SuppliedBy


OtParms = FieldStore(OtParms,@RM,4,1,OrderDetRec)
obj_Tables('WriteRec',OtParms)

RETURN


* * * * * * *
CurrStatus:
* * * * * * *

OrderNo		= Parms[1,@RM]
ItemNo		= Parms[COL2()+1,@RM]
OrderDetRec	= Parms[COL2()+1,@RM]

IF OrderNo = ''	THEN RETURN			;* This is used in the dictionary -> don't throw an error for a null parmeter
IF ItemNo = ''	THEN RETURN			;* This is used in the dictionary -> don't throw an error for a null parmeter

WONo				= OrderDetRec<ORDER_DET_WO_NO$>
VisionItemStatus	= OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$>
ItemQty				= OrderDetRec<ORDER_DET_ITEM_QTY$>
ShipQty				= SUM(XLATE('COC',OrderDetRec<ORDER_DET_SHIP_NO$>,'TOT_QTY','X'))

IF VisionItemStatus = 'C' THEN
	IF (ShipQty = ItemQty) AND ItemQty > 0 THEN
		Result = 'COMP'
	END ELSE
		Result = 'CANC'
	END
	RETURN
END


WOStatus = XLATE('WO_LOG',WONo,'CURR_STATUS','X')

IF WOStatus = 'NEW' THEN
	SuppliedBy = OrderDetRec<ORDER_DET_SUPPLIED_BY$>
	IF SuppliedBy = 'C' THEN
		Result = 'RTP'
	END ELSE
		Result = 'ASN'
	END
	RETURN
END ELSE
	Result = WOStatus
END

RETURN


* * * * * * *
AddWONo:
* * * * * * *

OrderNo		= Parms[1,@RM]
ItemNos		= Parms[COL2()+1,@RM]
WONo		= Parms[COL2()+1,@RM]

ErrorMsg = ''

IF OrderNo	= '' THEN ErrorMsg = 'Null parameter "OrderNo" passed to routine. (':Method:')'
IF ItemNos	= '' THEN ErrorMsg = 'Null parameter "ItemNos" passed to routine. (':Method:')'
IF WONo		= '' THEN ErrorMsg = 'Null parameter "WONo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

TableVar = ''

FOR I = 1 TO COUNT(ItemNos,@VM) + (ItemNos NE '')
	ItemNo = ItemNos<1,I>
	OrderDetKey = OrderNo:'*':ItemNo

	OtParms = 'ORDER_DET':@RM:OrderDetKey:@RM:TableVar 
	OrderDetRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update 
	
	OrderDetRec<ORDER_DET_WO_NO$> = WONo

	OtParms = FieldStore(OtParms,@RM,4,1,OrderDetRec)
	obj_Tables('WriteRec',OtParms)
NEXT I

RETURN


* * * * * * *
AddVisionInvoice:
* * * * * * * 

OrderNo				= Parms[1,@RM]
ItemNo				= Parms[COL2()+1,@RM]
VisionInvoiceNo		= Parms[COL2()+1,@RM]
VisionInvoiceItem	= Parms[COL2()+1,@RM]

ErrorMsg = ''

IF OrderNo				= '' THEN ErrorMsg = 'Null parameter "OrderNo" passed to routine. (':Method:')'
IF ItemNo				= '' THEN ErrorMsg = 'Null parameter "ItemNo" passed to routine. (':Method:')'
IF VisionInvoiceNo		= '' THEN ErrorMsg = 'Null parameter "VisionInvoiceNo" passed to routine. (':Method:')'
IF VisionInvoiceItem	= '' THEN ErrorMsg = 'Null parameter "VisionInvoiceNo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

OrderDetKey = OrderNo:'*':ItemNo

OtParms = 'ORDER_DET':@RM:OrderDetKey 
OrderDetRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update 

LOCATE VisionInvoiceNo IN OrderDetRec<ORDER_DET_VISION_INVOICE_NO$> USING @VM SETTING POS ELSE
	OrderDetRec = INSERT(OrderDetRec,ORDER_DET_VISION_INVOICE_NO$,POS,0,VisionInvoiceNo)
	OrderDetRec = INSERT(OrderDetRec,ORDER_DET_VISION_INVOICE_ITEM$,POS,0,VisionInvoiceItem)
END
OtParms = FieldStore(OtParms,@RM,4,1,OrderDetRec)
obj_Tables('WriteRec',OtParms)

RETURN


* * * * * * *
InvoicedQty:
* * * * * * *

OrderNo		= Parms[1,@RM]
ItemNo		= Parms[COL2()+1,@RM]
OrderDetRec	= Parms[COL2()+1,@RM]

IF OrderNo = ''	THEN RETURN			;* This is used in the dictionary -> don't throw an error for a null parmeter
IF ItemNo = ''	THEN RETURN			;* This is used in the dictionary -> don't throw an error for a null parmeter

IF OrderDetRec = '' THEN
	OrderDetRec = XLATE('ORDER_DET',OrderNo:'*':ItemNo,'','X')
	IF OrderDetRec = '' THEN RETURN
END

InvoiceNos = OrderDetRec<ORDER_DET_INVOICE_NO$>
InvoicedQtys = ''
FOR I = 1 TO COUNT(InvoiceNos,@VM) + (InvoiceNos NE '')
	InvoiceNo = InvoiceNos<1,I>
	InvODItemNos	= XLATE('INVOICE',InvoiceNo,48,'X')
	InvQtys			= XLATE('INVOICE',InvoiceNo,35,'X')
	
	FOR N = 1 TO COUNT(InvODItemNos,@VM) + (InvODItemNos NE '')
		IF InvODItemNos<1,N> = ItemNo THEN
			InvoicedQtys<1,I> = InvoicedQtys<1,I> + SUM(InvQtys<1,N>)
		END
	NEXT N
NEXT I

Result = InvoicedQtys

RETURN



* * * * * * *
Cancel:
* * * * * * *

OrderNo		= Parms[1,@RM]
ItemNo		= Parms[COL2()+1,@RM]

ErrorMsg = ''

IF OrderNo		= '' THEN ErrorMsg = 'Null parameter "OrderNo" passed to routine. (':Method:')'
IF ItemNo		= '' THEN ErrorMsg = 'Null parameter "ItemNo" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

OrderDetKey = OrderNo:'*':ItemNo

OtParms = 'ORDER_DET':@RM:OrderDetKey 
OrderDetRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update 

OrderDetRec<ORDER_DET_ITEM_QTY$>	= 0
OrderDetRec<ORDER_DET_UNIT_PRICE$>	= 0

OtParms = FieldStore(OtParms,@RM,4,1,OrderDetRec)
obj_Tables('WriteRec',OtParms)

RETURN


* * * * * * *
EpiPROAdd:
* * * * * * *

OrderDetKey	= Parms[1,@RM]
OrderDetRec	= Parms[COL2()+1,@RM]

IF OrderDetKey = '' THEN RETURN
IF OrderDetRec = '' THEN OrderDetRec = XLATE('ORDER_DET',OrderDetKey,'','X')


OrderNo = FIELD(OrderDetKey,'*',1,1)

CustNo = XLATE('ORDER',OrderNo,ORDER_CUST_NO$,'X')

QuoteNo			= OrderDetRec<ORDER_DET_QUOTE_NO$>
ProcStepPSNs	= XLATE('QUOTE',QuoteNo,'PROC_STEP_PSN','X')
PSNo			= ProcStepPSNs<1,1>			;* Wafers don't change size from one step to the other so use the first PSN

PSNWaferSize		= XLATE('PROD_SPEC',PSNo,'SUB_WAFER_SIZE','X')
PSNReactorType  	= XLATE('PROD_SPEC',PSNo,'REACTOR_TYPE','X')
PSNSubstrateSpec	= XLATE('PROD_SPEC',PSNo,PROD_SPEC_SPEC_SUBSTRATE$,'X')

PSType = PSNSubstrateSpec<1,QSSubSpecType$>

IF PSNReactorType NE 'P' AND PSNReactorType NE 'EPP' THEN RETURN	;* Not an EpiPRO specification

*IF PSType = 'Q' OR CustNo = '7034' OR CustNo = '7035' THEN		;* Removed Vishay 2/21/2008 JCH

IF PSType = 'Q'THEN
	Result = 0							;* This is an EpiPRO Qual Run or a Vishay production order -> no extra boxes required
	RETURN
END

BEGIN CASE
	CASE PSNWaferSize = '150 mm 6 in'
		EpiSusceptor = XLATE('EPI_SUSCEPTOR','6','','X')
		
	CASE PSNWaferSize = '125 mm 5 in'
		EpiSusceptor = XLATE('EPI_SUSCEPTOR','5','','X')
		
	CASE 1
		RETURN ;* No error - this is called from the data dictionary
		
END CASE

TestWfrsRun		= OCONV(EpiSusceptor<EPI_SUSCEPTOR_TEST_WFRS_RUN$>,'MD2')
ProveInWfrsWO	= EpiSusceptor<EPI_SUSCEPTOR_PROVEIN_WFRS_WO$>
Pockets			= EpiSusceptor<EPI_SUSCEPTOR_POCKET_NO$>

PocketCnt	= COUNT(Pockets,@VM) + (Pockets NE '')
OrderQty	= OrderDetRec<ORDER_DET_ITEM_QTY$>

FracRuns	= (OrderQty + ProveInWfrsWO)/(PocketCnt - TestWfrsRun)
ReqRuns		= INT(FracRuns)

IF MOD(FracRuns,ReqRuns) > 0 THEN ReqRuns += 1

EstWafersReq = ReqRuns * PocketCnt

BoxCnt = INT(EstWafersReq/25)
IF MOD(EstWafersReq,25) THEN BoxCnt += 1		;* Now have the total number of boxes to be pulled

Result = (BoxCnt*25) - OrderQty 				;* Total number of wafers beyond the order quantity in a multiple of 25

RETURN


* * * * * * *
ProdVerNo:
* * * * * * *


OrdDetKey = Parms[1,@RM]
OrdDetRec = Parms[COL2()+1,@RM]

IF OrdDetKey = '' THEN RETURN
IF OrdDetRec = '' THEN 
	OrdDetRec = XLATE('ORDER_DET',OrdDetKey,'','X')
	IF OrdDetRec = '' THEN RETURN
END

CustPN		= OrdDetRec<ORDER_DET_CUST_PN$>
SubPN		= OrdDetRec<ORDER_DET_SUB_PART_NO$>

Supplier = SubPN[-1,'B-']
IF Supplier[1,1] = 'I' AND LEN(Supplier) = 2 ELSE Supplier = ''

OrdNo	= OrdDetKey[1,'*']
OrdRec	= XLATE('ORDER',OrdNo,'','X')

CustNo	= OrdRec<ORDER_CUST_NO$>
OrderDt	= OrdRec<ORDER_ENTRY_DATE$>

IF OrderDt < ICONV('8/1/2005','D') THEN RETURN	;* No part numbers on older records

ProcStepPSNs = XLATE('ORDER_DET',OrdDetKey,'QUOTE_PROC_STEP_PSN','X')

PsnID = ProcStepPSNs
CONVERT @VM TO '.' IN PsnID

ShipPSN = ProcStepPSNs[-1,'B':@VM]

ReactType = XLATE('PROD_SPEC',ShipPSN,PROD_SPEC_REACTOR_TYPE$,'X')

/*	Dead 9/5/2014 JCH
BEGIN CASE
	CASE ShipReactType = 'A'	; ReactType = 'ASM+'
	CASE ShipReactType = 'P'	; ReactType = 'EPP'
	CASE ShipReactType = 'E'	; ReactType = 'EPS'
	CASE ShipReactType = 'H'	; ReactType = 'HTR'
	CASE ShipReactType = 'C'	; ReactType = 'SIG'
	CASE 1						; ReactType = ''
END CASE
*/


SubPN = TRIM(SubPN)
CONVERT @LOWER_CASE TO @UPPER_CASE IN SubPN

SortKey = SubPN:'*':ReactType:'*':PSNId		;* PART_WORK key * * * * * * * * *

CONVERT @LOWER_CASE TO @UPPER_CASE IN SortKey

Result = XLATE('PART_WORK',SortKey,1,'X') ;* Only field in PART_WORK is ProdVer (was Part) No.
RETURN


* * * * * * *
ActiveProdVerNo:
* * * * * * *

OrdDetKey = Parms[1,@RM]
OrdDetRec = Parms[COL2()+1,@RM]

IF OrdDetKey = '' THEN RETURN
IF OrdDetRec = '' THEN 
	OrdDetRec = XLATE('ORDER_DET',OrdDetKey,'','X')
	IF OrdDetRec = '' THEN RETURN
END

PsnID = XLATE('QUOTE',OrdDetRec<1>,'PROC_STEP_PSN','X')

CONVERT @VM TO '.' IN PsnID

OPEN 'DICT.PROD_VER' TO DictProdVer ELSE
	ErrMsg('Unable to Open DICT.PROD_VER table for BTREE.EXTRACT')
	RETURN
END

*Search				= 'PSN_ID':@VM:PsnID:@FM:'STATUS':@VM:'A':@FM
Search				= 'PSN_ID':@VM:PsnID:@FM
Table				= 'PROD_VER'
ActiveProdVerNos	= ''
Option				= ''
Flag				= ''

Btree.Extract(Search,Table,DictProdVer,ActiveProdVerNos,Option,Flag)

Result = ActiveProdVerNos

RETURN
