COMPILE FUNCTION obj_Order(Method,Parms)
/*
	Methods for Order table
    
	08/15/2004 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Find()							;* Lookup Order number
    Ord_WOLog(OrderNo,OrderRecord)	;* Adds detail information to WO_LOG from old format Order window
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Dialog_Box, obj_WO_Log, NextKey
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, Send_Dyn, obj_WO_Step, obj_WO_Log, Btree.Extract, ErrMsg
DECLARE SUBROUTINE obj_Order_Det, obj_Order_Change, obj_Notes, Send_Info

$INSERT MSG_EQUATES
$INSERT ORDER_EQU
$INSERT ORDER_DET_EQU
$INSERT QUOTE_EQU
$INSERT PROD_SPEC_EQU
$INSERT COMPANY_EQU
$INSERT WO_LOG_EQU
$INSERT WO_STEP_EQU
$INSERT VISION_LOG_EQUATES		;* Auto generated from dictionary builder
$INSERT QUOTE_SIGS_EQU
$INSERT NOTIFICATION_EQU
$INSERT RDS_EQU

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 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

ErrTitle = 'Error in Stored Procedure "obj_Order"'
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 = 'Find'		; GOSUB Find
	CASE Method = 'CurrStatus'	; GOSUB CurrStatus
	CASE Method = 'WOQuotes'	; GOSUB WOQuotes
	CASE Method = 'PostVision'	; GOSUB PostVision
	CASE Method = 'POChange'	; GOSUB POChange
	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


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

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

IF OrderNo = '' THEN RETURN		;* No error just return

IF OrderRec = '' THEN OrderRec = XLATE('ORDER',OrderNo,'','X')
IF OrderRec = '' THEN RETURN	;* No error just return


BEGIN CASE



END CASE


Result = ''
RETURN


* * * * * * *
Find:
* * * * * * *

OrderKeys = Dialog_Box( 'DIALOG_ORDER_FIND', @WINDOW, '' )

Result = OrderKeys


RETURN

* * * * * * *
WOQuotes:
* * * * * * *

IF NOT(ASSIGNED(OrderNo)) THEN
	OrderNo	= Parms[1,@RM]
	OrderRec = Parms[COL2()+1,@RM]
END ELSE
	OrderRec = ''
END

IF OrderNo = '' THEN RETURN

IF OrderRec = '' THEN
	OrderRec = XLATE('ORDER',OrderNo,'','X')
END

IF OrderRec = '' THEN RETURN

OrderDetKeys = OrderRec<ORDER_ORDER_DET_KEY$>

ItemNos = ''
FOR I = 1 TO COUNT(OrderDetKeys,@VM) + (OrderDetKeys NE '')
	ItemNos<1,I> = FIELD(OrderDetKeys<1,I>,'*',2)
NEXT I

ItemQuoteNos	= XLATE('ORDER_DET',OrderDetKeys,ORDER_DET_QUOTE_NO$,'X')
ItemPromiseDts	= XLATE('ORDER_DET',OrderDetKeys,ORDER_DET_PROMISE_DT$,'X')
ItemWorkOrders	= XLATE('ORDER_DET',OrderDetKeys,ORDER_DET_WO_NO$,'X')
ItemQtys		= XLATE('ORDER_DET',OrderDetKeys,ORDER_DET_ITEM_QTY$,'X')

SortArray = ''
QuoteResults = ''
PromiseDtResults = ''
ItemNoResults = ''
WorkOrderResults = ''


FOR I = 1 TO COUNT(ItemQuoteNos,@VM) + (ItemQuoteNos NE '')
	IF ItemQtys<1,I> > 0 THEN
		Element = ItemQuoteNos<1,I>:'*':ItemPromiseDts<1,I>
		LOCATE Element IN SortArray BY 'AR' USING @VM SETTING Pos THEN
			ItemNoResults		= INSERT(ItemNoResults,1,Pos,-1,ItemNos<1,I>)
		END ELSE
			SortArray			= INSERT(SortArray,1,Pos,0,Element)
			QuoteResults		= INSERT(QuoteResults,1,Pos,0,ItemQuoteNos<1,I>)
			PromiseDtResults	= INSERT(PromiseDtResults,1,Pos,0,ItemPromiseDts<1,I>)
			ItemNoResults		= INSERT(ItemNoResults,1,Pos,0,ItemNos<1,I>)
			WorkOrderResults	= INSERT(WorkOrderResults,1,Pos,0,ItemWorkOrders<1,I>)
		END
	END	;* End of check for cancelled line item
NEXT I

SWAP @SVM WITH ', ' IN ItemNoResults

Result = QuoteResults:@FM:PromiseDtResults:@FM:ItemNoResults:@FM:WorkOrderResults:@FM:SortArray

RETURN



* * * * * * * *
PostVision:
* * * * * * * *

* Create or Update Order information from the AS400 - Vision system

VisionOrderNo	= Parms[1,@RM]				;* Order<48>
VisionItemNo	= Parms[COL2()+1,@RM]		;* Order_Det<18> - same as OI item number
LineItemStatus	= Parms[COL2()+1,@RM]		;* Order_Det<19>
BillToCustNo	= Parms[COL2()+1,@RM]		;* Order<46>			;* Vision Customer Number
ShipToCustNo	= Parms[COL2()+1,@RM]		;* Order<47>			;* Vision Customer Number
CustPONo		= Parms[COL2()+1,@RM]		;* Order<41>
EpiPartNo		= Parms[COL2()+1,@RM]		;* Order_Det<3>
PartType		= Parms[COL2()+1,@RM]		;* Order_Det<20>
CustPartNo		= Parms[COL2()+1,@RM]		;* Order_Det<8>
ItemQty			= Parms[COL2()+1,@RM]		;* Order_Det<6>
ItemShipQty		= Parms[COL2()+1,@RM]		;* Order_Det<21>
ItemNetQty		= Parms[COL2()+1,@RM]		;* Not Used
ItemUnitPrice	= Parms[COL2()+1,@RM]		;* Order_Det<5>
ItemExtPrice	= Parms[COL2()+1,@RM]		;* Not Used
ItemPromiseDt	= Parms[COL2()+1,@RM]		;* Order_Det<11>
ItemReqShipDt	= Parms[COL2()+1,@RM]		;* Order_Det<13>
ItemCalcSWODt	= Parms[COL2()+1,@RM]		;* Order_Det<14>
ItemCRD			= Parms[COL2()+1,@RM]		;* Order_Det<12>
ItemQuoteNo		= Parms[COL2()+1,@RM]		;* Order_Det<22>
CustName		= Parms[COL2()+1,@RM]		;* Order<49>

IF VisionOrderNo = '' THEN RETURN
IF ItemQuoteNo = '' THEN RETURN

OrderNo = XLATE('VISION_ORDER',VisionOrderNo,1,'X')

IF OrderNo = '' THEN
	ItemNo = ABS(VisionItemNo)
END ELSE
	OrderDetKeys	= XLATE('ORDER',OrderNo,ORDER_ORDER_DET_KEY$,'X')
	VisionItemNos	= XLATE('ORDER_DET',OrderDetKeys,ORDER_DET_VISION_LINE_NO$,'X')
	
	LOCATE VisionItemNo IN VisionItemNos USING @VM SETTING Pos THEN
		ItemNo = FIELD(OrderDetKeys<1,Pos>,'*',2)	;* Take the item number for the second part of the key
	END ELSE
		ItemNo = ABS(VisionItemNo)
	END
END

QuoteRec = XLATE('QUOTE',ItemQuoteNo,'','X')

***   Get Epi Customer Number   ***

OPEN 'DICT.COMPANY' TO DictCompany ELSE
	ErrMsg('Unable to open "DICT.COMPANY" for lookup in COMM_CUSTOMER_EPI - CompNoLF Method.')
	RETURN
END

SearchString = 'VISION_CUST_NO':@VM:ShipToCustNo:@FM
Option = ''
Flag = ''
Btree.Extract(SearchString,'COMPANY',DictCompany,EpiCustNo,Option,Flag)		;* Get Epi Cust No

*** Verify Customer Number is on record ***

SendMessage = 0

IF EpiCustNo = '' THEN

	LogNo = NextKey('VISION_LOG')
	LogRec = ''
	LogRec<VISION_LOG_TIMESTAMP$>		= ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')
	LogRec<VISION_LOG_LOG_DESC$>		= 'No EPI Cust No. for Vision Ship To Customer'
	LogRec<VISION_LOG_VISION_ORDER$>	= VisionOrderNo
	LogRec<VISION_LOG_VISION_LINE$>		= VisionItemNo
	LogRec<VISION_LOG_ORDER_NO$>		= OrderNo
	LogRec<VISION_LOG_ITEM_NO$>			= ItemNo
	LogRec<VISION_LOG_FIELD_NAME$,-1>	= 'SHIP_TO_CUST_NO'
	LogRec<VISION_LOG_VISION_VALUE$,-1>	= ShipToCustNo
	LogRec<VISION_LOG_OI_VALUE$,-1>		= ''		;* Log as an error
	
	OVParms = 'VISION_LOG':@RM:LogNo:@RM:@RM:LogRec
	obj_Tables('WriteRec',OVParms)
	
	Recipients		= XLATE('NOTIFICATION','ORDER_ENTRY',NOTIFICATION_USER_ID$,'X')
	SentFrom		= "Vision Posting Process"
	Subject			= 'Vision Error Logged ':LogNo
	Message			= 'Errors occured while attempting to post an order from Vision'
	AttachWindow	= 'VISION_LOG'
	AttachKey		= LogNo   
	SendToGroup		= ''   

	Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
	obj_Notes('Create',Parms)
	RETURN
	
END ELSE

	Captive = XLATE('COMPANY',EpiCustNo,COMPANY_CAPTIVE$,'X')
	
	IF Captive THEN
	
		LogNo = NextKey('VISION_LOG')
		LogRec = ''
		LogRec<VISION_LOG_TIMESTAMP$>		= ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')
		LogRec<VISION_LOG_LOG_DESC$>		= 'Order with Captive EPI Cust No. Received from Vision'
		LogRec<VISION_LOG_VISION_ORDER$>	= VisionOrderNo
		LogRec<VISION_LOG_VISION_LINE$>		= VisionItemNo
		LogRec<VISION_LOG_ORDER_NO$>		= OrderNo
		LogRec<VISION_LOG_ITEM_NO$>			= ItemNo
		LogRec<VISION_LOG_FIELD_NAME$,-1>	= 'CUST_NO'
		LogRec<VISION_LOG_VISION_VALUE$,-1>	= ShipToCustNo
		LogRec<VISION_LOG_OI_VALUE$,-1>		= ''		;* Log as an error
		
		OVParms = 'VISION_LOG':@RM:LogNo:@RM:@RM:LogRec
		obj_Tables('WriteRec',OVParms)
		
		Recipients		= XLATE('NOTIFICATION','ORDER_ENTRY',NOTIFICATION_USER_ID$,'X')			;* OrderNotif$ is customer service 
		SentFrom		= "Vision Posting Process"
		Subject			= 'Vision Error Logged ':LogNo
		Message			= 'Errors occured while attempting to post an order from Vision'
		AttachWindow	= 'VISION_LOG'
		AttachKey		= LogNo   
		SendToGroup		= ''   

		Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
		obj_Notes('Create',Parms)
		RETURN
		
	END ;* End of check for captive
END	;* End of check for Null EpiCustNo



Send_Info('Posting Vision order...')




*** Verify Customer Part Number matches that on the Quote -> PSN ***

IF PartType = '2' THEN
	StepPSNs = QuoteRec<QUOTE_PROC_STEP_PSN$>

	FirstStepPSN = StepPSNs<1,1>
	CustPartNos = XLATE('PROD_SPEC',FirstStepPSN,PROD_SPEC_CUST_PART_NO$,'X')
	CustPartDesc = XLATE('PROD_SPEC',FirstStepPSN,PROD_SPEC_PN_DESC$,'X')
	OldStylePartNos = ''
	FOR I = 1 TO COUNT(CustPartNos,@VM) + (CustPartNos NE '')
		OldStylePartNos<1,I> = CustPartNos<1,I>:CustPartDesc<1,I>
	NEXT I
	

	CONVERT @LOWER_CASE TO @UPPER_CASE IN CustPartNos
	
	LOCATE CustPartNo IN CustPartNos USING @VM SETTING Dummy ELSE
		LOCATE CustPartNo IN OldStylePartNos USING @VM SETTING Dummy ELSE
			SWAP @VM WITH ', ' IN CustPartNos
			LogNo = NextKey('VISION_LOG')
			LogRec = ''
			LogRec<VISION_LOG_TIMESTAMP$>		= ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')
			LogRec<VISION_LOG_LOG_DESC$>		= 'Cust Part No. ':QUOTE(CustPartNo):' Not Found IN PSN: ':FirstStepPSN
			LogRec<VISION_LOG_VISION_ORDER$>	= VisionOrderNo
			LogRec<VISION_LOG_VISION_LINE$>		= VisionItemNo
			LogRec<VISION_LOG_ORDER_NO$>		= OrderNo
			LogRec<VISION_LOG_ITEM_NO$>			= ItemNo
			LogRec<VISION_LOG_FIELD_NAME$,-1>	= 'CUST_PART_NO'
			LogRec<VISION_LOG_VISION_VALUE$,-1>	= CustPartNo
			LogRec<VISION_LOG_OI_VALUE$,-1>		= CustPartNos		;* Log as an error
			
			OVParms = 'VISION_LOG':@RM:LogNo:@RM:@RM:LogRec
			obj_Tables('WriteRec',OVParms)
			
			Recipients		= XLATE('NOTIFICATION','ORDER_ENTRY',NOTIFICATION_USER_ID$,'X')				;* OrderNotif$ is customer service 
			SentFrom		= "Vision Posting Process"
			Subject			= 'Vision Error Logged ':LogNo
			Message			= 'Errors occured while attempting to post an order from Vision'
			AttachWindow	= 'VISION_LOG'
			AttachKey		= LogNo   
			SendToGroup		= ''   

			Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
			obj_Notes('Create',Parms)
			RETURN
		END
	END
END	;* End of check for part type 2



*** Verify Customer Number on the Quote  ***

QuoteCustNo = QuoteRec<QUOTE_CUST_NO$>

* EpiCustNo is the EpiShipToCustNo on the order

SearchString = 'VISION_CUST_NO':@VM:BillToCustNo:@FM
Option = ''
Flag = ''
Btree.Extract(SearchString,'COMPANY',DictCompany,EpiBillCustNo,Option,Flag)		;* Get Epi BillToCustNo

LogMessage = ''
IF EpiCustNo NE QuoteCustNo THEN
	IF EpiBillCustNo NE QuoteCustNo THEN
		LogMessage = "Customer No on the Quote is NOT one of the Customer Numbers on the Order"
	END
END
IF LogMessage NE '' THEN

	* Customer Number mismatch

	LogNo = NextKey('VISION_LOG')
	LogRec = ''
	LogRec<VISION_LOG_TIMESTAMP$>		= ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')
	LogRec<VISION_LOG_LOG_DESC$>		= "Customer Number on Quote doesn't match either Customer Number on Order"
	LogRec<VISION_LOG_VISION_ORDER$>	= VisionOrderNo
	LogRec<VISION_LOG_VISION_LINE$>		= VisionItemNo
	LogRec<VISION_LOG_ORDER_NO$>		= OrderNo
	LogRec<VISION_LOG_ITEM_NO$>			= ItemNo
	LogRec<VISION_LOG_FIELD_NAME$,-1>	= 'QUOTE_NO'
	LogRec<VISION_LOG_VISION_VALUE$,-1>	= 'S':ShipToCustNo:'>':EpiCustNo:' - B':BillToCustNo:'>':EpiBillCustNo
	LogRec<VISION_LOG_OI_VALUE$,-1>		= QuoteCustNo		;* Log as an error
	
	OVParms = 'VISION_LOG':@RM:LogNo:@RM:@RM:LogRec
	obj_Tables('WriteRec',OVParms)
	
	Recipients		= XLATE('NOTIFICATION','ORDER_ENTRY',NOTIFICATION_USER_ID$,'X')				;* OrderNotif$ is customer service 
	SentFrom		= "Vision Posting Process"
	Subject			= 'Vision Error Logged ':LogNo
	Message			= 'Errors occured while attempting to post an order from Vision'
	AttachWindow	= 'VISION_LOG'
	AttachKey		= LogNo   
	SendToGroup		= ''   

	Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
	obj_Notes('Create',Parms)
	RETURN
END


DefaultItemDesc = QuoteRec<QUOTE_PROC_DESC$>
IF DefaultItemDesc = '' THEN
	ItemDesc = 'Order from Vision'
END ELSE
	ItemDesc = DefaultItemDesc
END

OCTableNames	= ''
OCRecordKeys	= ''
OCColumns		= ''
OCValues		= ''
OCOldValues		= ''
OCNewValues		= ''


IF OrderNo = '' THEN
	* New Order
	OrderNo = NextKey('ORDER')
	
	OrderRec = ''	
	OrderRec<ORDER_ENTRY_ID$>			= 'VISION'
	OrderRec<ORDER_ENTRY_DATE$>			= Date()
	OrderRec<ORDER_VISION_ORDER_NO$>	= VisionOrderNo
	OrderRec<ORDER_CUST_NO$>			= EpiCustNo
	OrderRec<ORDER_SHIP_TO_CUST_NO$>	= ShipToCustNo
	OrderRec<ORDER_BILL_TO_CUST_NO$>	= BillToCustNo
	OrderRec<ORDER_PO_NO$>				= CustPONo
	OrderRec<ORDER_VISION_CUST_NAME$>	= CustName
	
	* Added 08/25/2005  JCH - J.C. Henry & Co., Inc.
	
	CustRec		= XLATE('COMPANY',EpiCustNo,'','X')
	CustBillTo	= CustRec<COMPANY_BILL_TO_CUST_NO$>
	
	IF CustBillTo = '' OR (BillToCustNo = ShipToCustNo) THEN
		OrderRec<ORDER_BILL_TO_COMPANY$>	= CustRec<COMPANY_BILL_TO_CO$>
		OrderRec<ORDER_BILL_TO_ATTN$>		= CustRec<COMPANY_BILL_TO_ATTN$>
		OrderRec<ORDER_BILL_TO_ADDRESS$>	= CustRec<COMPANY_BILL_TO_ADDRESS$>
		OrderRec<ORDER_BILL_TO_CITY$>		= CustRec<COMPANY_BILL_TO_CITY$>
		OrderRec<ORDER_BILL_TO_STATE$>		= CustRec<COMPANY_BILL_TO_STATE$>
		OrderRec<ORDER_BILL_TO_ZIP$>		= CustRec<COMPANY_BILL_TO_ZIP$>
		OrderRec<ORDER_BILL_TO_COUNTRY$>	= CustRec<COMPANY_COUNTRY$>
	END ELSE
	
		BillCustRec = XLATE('COMPANY',CustBillTo,'','X')
		
		OrderRec<ORDER_BILL_TO_COMPANY$>	= BillCustRec<COMPANY_BILL_TO_CO$>
		OrderRec<ORDER_BILL_TO_ATTN$>		= BillCustRec<COMPANY_BILL_TO_ATTN$>
		OrderRec<ORDER_BILL_TO_ADDRESS$>	= BillCustRec<COMPANY_BILL_TO_ADDRESS$>
		OrderRec<ORDER_BILL_TO_CITY$>		= BillCustRec<COMPANY_BILL_TO_CITY$>
		OrderRec<ORDER_BILL_TO_STATE$>		= BillCustRec<COMPANY_BILL_TO_STATE$>
		OrderRec<ORDER_BILL_TO_ZIP$>		= BillCustRec<COMPANY_BILL_TO_ZIP$>
		OrderRec<ORDER_BILL_TO_COUNTRY$>	= BillCustRec<COMPANY_COUNTRY$>
	END
	
	OrderRec<ORDER_SHIP_TO_COMPANY$>	= CustRec<COMPANY_SHIP_TO_CO$>
	OrderRec<ORDER_SHIP_TO_ATTN$>		= CustRec<COMPANY_SHIP_TO_ATTN$>
	OrderRec<ORDER_SHIP_TO_ADDRESS$>	= CustRec<COMPANY_SHIP_TO_ADDRESS$>
	OrderRec<ORDER_SHIP_TO_CITY$>		= CustRec<COMPANY_SHIP_TO_CITY$>
	OrderRec<ORDER_SHIP_TO_STATE$>		= CustRec<COMPANY_SHIP_TO_STATE$>
	OrderRec<ORDER_SHIP_TO_ZIP$>		= CustRec<COMPANY_SHIP_TO_ZIP$>
	
	IF CustRec<COMPANY_COUNTRY$> = '' THEN
		OrderRec<ORDER_SHIP_TO_COUNTRY$>	= 'USA'
	END ELSE
		OrderRec<ORDER_SHIP_TO_COUNTRY$>	= CustRec<COMPANY_COUNTRY$>
	END
	
	OrderRec<ORDER_TERMS$>				= CustRec<COMPANY_TERMS$>
	OrderRec<ORDER_INVOICE_NOTES$>		= CustRec<COMPANY_INVOICE_NOTES$>
	OrderRec<ORDER_CAPTIVE$>			= CustRec<COMPANY_CAPTIVE$>
	OrderRec<ORDER_CONSIGNMENT$>		= CustRec<COMPANY_CONSIGNMENT$>
	OrderRec<ORDER_INCO_CODE$>			= CustRec<COMPANY_INCO_CODE$>
	
	* End of added code
	
	
	obj_Tables('WriteRec','ORDER':@RM:OrderNo:@RM:@RM:OrderRec)
	*obj_Tables('WriteRec','VISION_ORDER':@RM:VisionOrderNo:@RM:@RM:OrderNo)		;* Application maintained cross reference table
																					;* Changed to a Relational Index 8/26/2005 JCH
	
	ODParms  = OrderNo:@RM
	ODParms := ItemNo:@RM
	ODParms := ItemQuoteNo:@RM
	ODParms := EpiPartNo:@RM
	ODParms := ItemDesc:@RM				;* XLATE to EPI_PART table ?			
	ODParms := ItemUnitPrice:@RM
	ODParms := ItemQty:@RM
	ODParms := '':@RM					;* Pass WONo as null
	ODParms := VisionItemNo:@RM
	ODParms := LineItemStatus:@RM
	ODParms := PartType:@RM
	ODParms := CustPartNo:@RM
	ODParms := ItemShipQty:@RM
	ODParms := ItemPromiseDt:@RM
	ODParms := ItemReqShipDt:@RM
	ODParms := ItemCalcSWODt:@RM
	ODParms := ItemCRD
	
	obj_Order_Det('Create',ODParms)
	
	OCTableNames = 'ORDER_DET'
	OCRecordKeys = OrderNo:'*':ItemNo
	
	IF PartType = '2' OR PartType = '5' THEN
		
		WONo = obj_WO_Log('Create',OrderNo:@RM:ItemNo:@RM:ItemQuoteNo)
			
		IF Get_Status(errCode) THEN
			RETURN
			Send_Info('                                    ')
		END ELSE
			obj_Order_Det('AddWONo',OrderNo:@RM:ItemNo:@RM:WONo)
			
			OCTableNames<1,-1> = 'WO_LOG'
			OCRecordKeys<1,-1> = WONo
		END	
	END	
	
	Reason = 'New from Vision'
	obj_Order_Change('Create',OrderNo:@RM:OCTableNames:@RM:OCRecordKeys:@RM:OCColumns:@RM:OCValues:@RM:OCOldValues:@RM:OCNewValues:@RM:Reason:@RM:'VISION')
		
END ELSE

	* No changes to the order itself
	
	IF RowExists('ORDER_DET', OrderNo:'*':ItemNo) THEN
		
		* This is an update
		
		ODParms = 'ORDER_DET':@RM:OrderNo:'*':ItemNo
		OrderDetRec = obj_Tables('ReadRec',ODParms)
		
		IF Get_Status(errCode) THEN
			Send_Info('                                    ')
			RETURN
		END
		
		WONo = OrderDetRec<ORDER_DET_WO_NO$>		
		WORec = XLATE('WO_LOG',WONo,'','X')
		
		OCCnt			= 0						;* Order Change log parameters
		OCTableNames	= ''
		OCRecordKeys	= ''
		OCColumns		= ''
		OCValues		= ''
		OCNewValues		= ''
		OCOldValues		= ''
		
		IF WORec<WO_LOG_CASS_DTM$> NE '' THEN
		
			
			LogRec = ''
			
			
			* Material has been received
			
			Reason = 'Vision Change after Material Received'
			
			IF WORec<WO_LOG_CASS_REL_DTM$,1> = '' THEN
				
				IF OrderDetRec<ORDER_DET_ITEM_QTY$>	NE ICONV(ItemQty,'MD0') THEN
					OCCnt += 1
					OCTableNames<1,OCCnt>	= 'ORDER_DET'
					OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
					OCColumns<1,OCCnt>		= 'ITEM_QTY'
					OCValues<1,OCCnt>		= ''
					OCNewValues<1,OCCnt>	= ICONV(ItemQty,'MD0')
					OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_ITEM_QTY$>
				END
				
				OrderDetRec<ORDER_DET_ITEM_QTY$> = ICONV(ItemQty,'MD0')						;* Work Order not yet Released to Production
				
			END ELSE
				IF ICONV(ItemQty,'MD0') NE OrderDetRec<ORDER_DET_ITEM_QTY$> THEN
					LogRec<VISION_LOG_FIELD_NAME$,-1>	= 'ITEM_QTY'
					LogRec<VISION_LOG_VISION_VALUE$,-1>	= ICONV(ItemQty,'MD0')
					LogRec<VISION_LOG_OI_VALUE$,-1>	= OrderDetRec<ORDER_DET_ITEM_QTY$>		;* Log as an error
				END
			END
			
			* Error Log stuff
				
			IF ICONV(ItemPromiseDt,'D') NE OrderDetRec<ORDER_DET_PROMISE_DT$> THEN
				LogRec<VISION_LOG_FIELD_NAME$,-1>	= 'PROMISE_DT'
				LogRec<VISION_LOG_VISION_VALUE$,-1>	= ICONV(ItemPromiseDt,'D')
				LogRec<VISION_LOG_OI_VALUE$,-1>	= OrderDetRec<ORDER_DET_PROMISE_DT$>
			END
			
			IF 	ICONV(ItemCRD,'D') NE OrderDetRec<ORDER_DET_CUST_REQ_DT$> THEN
				LogRec<VISION_LOG_FIELD_NAME$,-1>	= 'CUST_REQ_DT'
				LogRec<VISION_LOG_VISION_VALUE$,-1>	= ICONV(ItemCRD,'D')
				LogRec<VISION_LOG_OI_VALUE$,-1>	= OrderDetRec<ORDER_DET_CUST_REQ_DT$>
			END
			
			IF LogRec NE '' THEN
			
				LogNo = NextKey('VISION_LOG')
				
				LogRec<VISION_LOG_TIMESTAMP$>		= ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')
				LogRec<VISION_LOG_LOG_DESC$>		= 'Order Item Update after Material Received'
				LogRec<VISION_LOG_VISION_ORDER$>	= VisionOrderNo
				LogRec<VISION_LOG_VISION_LINE$>		= VisionItemNo
				LogRec<VISION_LOG_ORDER_NO$>		= OrderNo
				LogRec<VISION_LOG_ITEM_NO$>			= ItemNo
				
				
				OVParms = 'VISION_LOG':@RM:LogNo:@RM:@RM:LogRec
				obj_Tables('WriteRec',OVParms)
			
				Recipients		= XLATE('NOTIFICATION','ORDER_ENTRY',NOTIFICATION_USER_ID$,'X')				;* OrderNotif$ is customer service 
				SentFrom		= "Vision Posting Process"
				Subject			= 'Vision Error Logged ':LogNo
				Message			= 'Errors occured while attempting to post an order from Vision'
				AttachWindow	= 'VISION_LOG'
				AttachKey		= LogNo   
				SendToGroup		= ''   

				Parms = Recipients:@RM:SentFrom:@RM:Subject:@RM:Message:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
				obj_Notes('Create',Parms)
			END
			
			IF OrderDetRec<ORDER_DET_UNIT_PRICE$> NE ICONV(ItemUnitPrice,'MD2') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'UNIT_PRICE'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemUnitPrice,'MD2')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_UNIT_PRICE$>
			END
			
			IF OrderDetRec<ORDER_DET_REQ_SHIP_DT$> NE ICONV(ItemReqShipDt,'D') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'REQ_SHIP_DT'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemReqShipDt,'D')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_REQ_SHIP_DT$>
			END
			
			IF OrderDetRec<ORDER_DET_SHIP_WIN_DT$> NE ICONV(ItemCalcSWODt,'D') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'SHIP_WIN_DT'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemCalcSWODt,'D')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_SHIP_WIN_DT$>
			END
			
			IF OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$> NE LineItemStatus THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'VISION_ITEM_STATUS'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= LineItemStatus
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$>
			END
			
			IF OrderDetRec<ORDER_DET_VISION_SHIP_QTY$> NE ICONV(ItemShipQty,'MD0') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'VISION_SHIP_QTY'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemShipQty,'MD0')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_VISION_SHIP_QTY$>
			END
			
			IF OCCnt > 0 THEN
				obj_Order_Change('Create',OrderNo:@RM:OCTableNames:@RM:OCRecordKeys:@RM:OCColumns:@RM:OCValues:@RM:OCOldValues:@RM:OCNewValues:@RM:Reason:@RM:'VISION')
			END
			
			OrderDetRec<ORDER_DET_UNIT_PRICE$>			= ICONV(ItemUnitPrice,'MD2')
			OrderDetRec<ORDER_DET_REQ_SHIP_DT$> 		= ICONV(ItemReqShipDt,'D')
			OrderDetRec<ORDER_DET_SHIP_WIN_DT$>			= ICONV(ItemCalcSWODt,'D')
			OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$>	= LineItemStatus
			OrderDetRec<ORDER_DET_VISION_SHIP_QTY$>		= ICONV(ItemShipQty,'MD0')
			
			ODParms = FieldStore(ODParms,@RM,4,1,OrderDetRec)
			obj_Tables('WriteRec',ODParms)
			
		END ELSE
		
			* CustWaferInfo entered
			* Material Received
			* Released
			
			OrderDetTotShipQty = XLATE('ORDER_DET',OrderNo:'*':ItemNo,'TOTAL_SHIP_QTY','X')		;*****************************
			OrderDetItemQty = OrderDetRec<ORDER_DET_ITEM_QTY$>
			
			IF LineItemStatus = 'C' THEN
				
				BEGIN CASE
				
					CASE OrderDetTotShipQty = 0 AND WORec<WO_LOG_CASS_REL_DTM$,1> = ''
					
						* Item is being cancelled zero out qtys and prices
						ItemUnitPrice	= 0
						ItemQty			= 0
						
						IF WONo NE '' THEN
							obj_WO_Log('RemOrderItem',WONo:@RM:OrderNo:@RM:ItemNo)
						END
						OrderDetRec<ORDER_DET_WO_NO$> = ''
						
						Reason = 'Vision Order Item Cancellation Received'
					
					CASE OrderDetTotShipQty = OrderDetItemQty
					
						Reason = 'Vision Order Item Closed from Vision'
						
					CASE 1
					
						Reason = 'Vision Order Change Received'
					
				END CASE
			END ELSE
				Reason = 'Vision Order Change Received'
			END
			
			
			IF OrderDetRec<ORDER_DET_UNIT_PRICE$> NE ICONV(ItemUnitPrice,'MD2') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'UNIT_PRICE'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemUnitPrice,'MD2')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_UNIT_PRICE$>
			END
			
			IF OrderDetRec<ORDER_DET_ITEM_QTY$>	NE ICONV(ItemQty,'MD0') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'ITEM_QTY'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemQty,'MD0')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_ITEM_QTY$>
			END
			
			IF OrderDetRec<ORDER_DET_PROMISE_DT$> NE ICONV(ItemPromiseDt,'D') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'PROMISE_DT'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemPromiseDt,'D')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_PROMISE_DT$>
			END
			
			IF OrderDetRec<ORDER_DET_CUST_REQ_DT$> NE ICONV(ItemCRD,'D') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'CUST_REQ_DT$'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemCRD,'D')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_CUST_REQ_DT$>
			END
			
			IF OrderDetRec<ORDER_DET_REQ_SHIP_DT$> NE ICONV(ItemReqShipDt,'D') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'REQ_SHIP_DT'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemReqShipDt,'D')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_REQ_SHIP_DT$>
			END
			
			IF OrderDetRec<ORDER_DET_SHIP_WIN_DT$> NE ICONV(ItemCalcSWODt,'D') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'SHIP_WIN_DT'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemCalcSWODt,'D')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_SHIP_WIN_DT$>
			END
			
			IF OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$> NE LineItemStatus THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'VISION_ITEM_STATUS'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= LineItemStatus
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$>
			END
			
			IF OrderDetRec<ORDER_DET_VISION_SHIP_QTY$> NE ICONV(ItemShipQty,'MD0') THEN
				OCCnt += 1
				OCTableNames<1,OCCnt>	= 'ORDER_DET'
				OCRecordKeys<1,OCCnt>	= OrderNo:'*':ItemNo
				OCColumns<1,OCCnt>		= 'VISION_SHIP_QTY'
				OCValues<1,OCCnt>		= ''
				OCNewValues<1,OCCnt>	= ICONV(ItemShipQty,'MD0')
				OCOldValues<1,OCCnt>	= OrderDetRec<ORDER_DET_VISION_SHIP_QTY$>
			END
			
			obj_Order_Change('Create',OrderNo:@RM:OCTableNames:@RM:OCRecordKeys:@RM:OCColumns:@RM:OCValues:@RM:OCOldValues:@RM:OCNewValues:@RM:Reason)
			
			OrderDetRec<ORDER_DET_UNIT_PRICE$>			= ICONV(ItemUnitPrice,'MD2')
			OrderDetRec<ORDER_DET_ITEM_QTY$>			= ICONV(ItemQty,'MD0')
			OrderDetRec<ORDER_DET_PROMISE_DT$>			= ICONV(ItemPromiseDt,'D')
			OrderDetRec<ORDER_DET_CUST_REQ_DT$> 		= ICONV(ItemCRD,'D')
			OrderDetRec<ORDER_DET_REQ_SHIP_DT$> 		= ICONV(ItemReqShipDt,'D')
			OrderDetRec<ORDER_DET_SHIP_WIN_DT$>			= ICONV(ItemCalcSWODt,'D')
			OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$>	= LineItemStatus
			OrderDetRec<ORDER_DET_VISION_SHIP_QTY$>		= ICONV(ItemShipQty,'MD0')
			
			ODParms = FieldStore(ODParms,@RM,4,1,OrderDetRec)
			obj_Tables('WriteRec',ODParms)
			
			
		END	;* End of check for material received
		
	END ELSE
	
		* This is a new detail record
		
		GOSUB WOQuotes
		
		WorkOrders			= Result<4>
		QuoteShipDtPairs 	= Result<5>
		Result = ''
		
		ODQuoteDate = ItemQuoteNo:'*':ICONV(ItemPromiseDt,'D')
		LOCATE ODQuoteDate IN QuoteShipDtPairs BY 'AR' USING @VM SETTING Pos THEN
			WONo = WorkOrders<1,Pos>
			IF WONo NE '' AND ( PartType = '2' OR PartType = '5') THEN
				
				obj_WO_Log('AddOrderItem',WONo:@RM:OrderNo:@RM:ItemNo)
			END
		END ELSE
			WONo = ''
		END
		

		ODParms  = OrderNo:@RM
		ODParms := ItemNo:@RM
		ODParms := ItemQuoteNo:@RM
		ODParms := EpiPartNo:@RM
		ODParms := ItemDesc:@RM				;* XLATE to EPI_PART table ?			
		ODParms := ItemUnitPrice:@RM
		ODParms := ItemQty:@RM
		ODParms := WONo:@RM					;* Pass as null
		ODParms := VisionItemNo:@RM
		ODParms := LineItemStatus:@RM
		ODParms := PartType:@RM
		ODParms := CustPartNo:@RM
		ODParms := ItemShipQty:@RM
		
		ODParms := ItemPromiseDt:@RM
		ODParms := ItemReqShipDt:@RM
		ODParms := ItemCalcSWODt:@RM
		ODParms := ItemCRD
		
		
		obj_Order_Det('Create',ODParms)
		
		
		IF WONo = '' AND ( PartType = '2' OR PartType = '5') THEN
		
			WONo = obj_WO_Log('Create',OrderNo:@RM:ItemNo:@RM:ItemQuoteNo)
		
			IF Get_Status(errCode) THEN
				Send_Info('                                    ')
				RETURN
			END ELSE
				obj_Order_Det('AddWONo',OrderNo:@RM:ItemNo:@RM:WONo)
				
			END		
		END
	END
END

Send_Info('                                    ')		;* Look at the whole line!!!!!!!!!!!!!!!!!!!

RETURN


* * * * * * *
POChange:
* * * * * * *

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

IF OrderNo = '' 	THEN ErrorMsg = 'Unassigned parameter "OrderNo" passed to routine (':Method:').'
IF OrgPONo = ''		THEN ErrorMsg = 'Unassigned parameter "OrgPONo" passed to routine (':Method:').'
IF PONo = ''		THEN ErrorMsg = 'Unassigned parameter "PONo" passed to routine (':Method:').'

IF ErrorMsg NE '' THEN RETURN


OrderRec= XLATE('ORDER',OrderNo,'','X')
WONos = OrderRec<ORDER_WO_KEYS$>								;* Get List of Work Orders on Order

WOStepKeys = XLATE('WO_LOG',WONos,WO_LOG_WO_STEP_KEY$,'X')		;* Get List of all WO Steps for All Work Orders on Order

CONVERT @VM TO @FM IN WOStepKeys

RdsNos = XLATE('WO_STEP',WOStepKeys,WO_STEP_RDS_KEY$,'X')		;* Get List of all RDS Keys for all WOSteps on all Work Orders

CONVERT @FM TO @VM IN RdsNos


RTParms			= 'RDS'
LockedRDSNos	= ''

FOR I = 1 TO COUNT(RdsNos,@VM) + (RdsNos NE '')
	RdsNo = RdsNos<1,I>
	IF RdsNo NE '' THEN
		RTParms = FieldStore(RTParms, @RM, 2, 1, RdsNo)

		obj_Tables('LockRec',RTParms)
		
		IF Get_Status(errCode) THEN
		
			FOR N = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
				RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNos<1,N>)
				obj_Tables('UnlockRec',RTParms)		;* Unlock everything locked up to here
			NEXT N
			
			
			otParms = 'ORDER':@RM:OrderNo
			OrderRec = obj_Tables('ReadRec',otParms)
			
			OrderRec<ORDER_PO_NO$> = OrgPONo
			
			otParms = FieldStore(otParms,@RM,4,1,OrderRec)
			obj_Tables('WriteRec',otParms)
			
			ErrorMsg = 'Unable to lock RDS ':QUOTE(RdsNo):' for delete.'
			
			RETURN
		END ELSE
			LockedRDSNos<1,-1> = RdsNo
		END
	END
NEXT I

RDSTableVar = FIELD(RTParms,@RM,3)

FOR I = 1 TO COUNT(LockedRDSNos,@VM) + (LockedRDSNos NE '')
	LockedRDSNo = LockedRDSNos<1,I>
	READ RDSRec FROM RDSTableVar,LockedRDSNo THEN
		RDSRec<RDS_PO$> = PONo
		RTParms = FieldStore(RTParms, @RM, 2, 1, LockedRDSNo)
		RTParms = FieldStore(RTParms, @RM, 4, 1, RDSRec)
		obj_Tables('WriteRec',RTParms)								;* Write and unlock RDS records
	END

	
NEXT I

Result = WONos

RETURN

