COMPILE FUNCTION obj_Order_Recon(Method,Parms)
/*
	Methods for Order_Recon table
    
	06/13/2005 JCH - Initial Coding
     
    Properties:
     
    Methods: ReconOrder(Parms)
    
	
*/


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

$INSERT MSG_EQUATES
$INSERT ORDER_EQU
$INSERT ORDER_DET_EQU
$INSERT ORDER_RECON_EQU
$INSERT COMPANY_EQU

ErrTitle = 'Error in Stored Procedure "obj_Order_Recon"'
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 = 'ReconOrder'		; GOSUB ReconOrder
	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:
* * * * * * *


RETURN


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


RETURN


* * * * * * * *
ReconOrder:
* * * * * * * *

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

TimeStamp		= Parms[1,@RM]
Parms[1,COL2()]	= ''						;* Strip off Timedate from front of the parameter list

VisionOrderNo		= Parms[1,@RM]				;* Order<48>
VisionItemNo		= Parms[COL2()+1,@RM]		;* Order_Det<18>
LineItemStatus		= Parms[COL2()+1,@RM]		;* Order_Det<19>
BillToCustNo		= Parms[COL2()+1,@RM]		;* Order<47>
ShipToCustNo		= Parms[COL2()+1,@RM]		;* Order<46>
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>
ItemCRDt			= Parms[COL2()+1,@RM]		;* Order_Det<12>
ItemQuoteNo			= Parms[COL2()+1,@RM]		;* Order_Det<22>
CustName			= Parms[COL2()+1,@RM]		;* Order<49>
VisionInvoiceNo		= Parms[COL2()+1,@RM]		;* OrderDet<24>
VisionInvoiceItem	= Parms[COL2()+1,@RM]		;* OrderDet<25>

IF VisionOrderNo = '' THEN RETURN

thisItemQty			= ICONV(ItemQty,'MD0')
thisItemUnitPrice	= ICONV(ItemUnitPrice,'MD2')
thisItemPromiseDt	= ICONV(ItemPromiseDt,'D')
thisItemReqShipDt	= ICONV(ItemReqShipDt,'D')
thisItemCalcSWODt	= ICONV(ItemCalcSWODt,'D')
thisItemCRDt		= ICONV(ItemCRDt,'D')

IF thisItemPromiseDt = ''	THEN ErrorMsg = 'Invalid ItemPromiseDt ':QUOTE(ItemPromiseDt):' passed to routine. (':Method:')'
IF thisItemReqShipDt = ''	THEN ErrorMsg = 'Invalid ItemReqShipDt ':QUOTE(ItemReqShipDt):' passed to routine. (':Method:')'
IF thisItemCalcSWODt = ''	THEN ErrorMsg = 'Invalid ItemCalcSWODt ':QUOTE(ItemCalcSWODt):' passed to routine. (':Method:')'
IF thisItemCRDt = '' 		THEN ErrorMsg = 'Invalid ItemCRDt ':QUOTE(ItemCRDt):' passed to routine. (':Method:')'

IF thisItemUnitPrice = ''	THEN thisItemUnitPrice = 0	;* This thing is failing the test below  JCH 3/14/2008
IF thisItemQty = ''			THEN NULL


IF ItemShipQty = '0' AND LineItemStatus = 'C' THEN
	* This is a canceled item
	thisItemQty = 0
	thisItemUnitPrice = 0
END


OPEN 'DICT.COMPANY' TO DictCompany ELSE
	ErrorMsg = 'Unable to open "DICT.COMPANY" for update.'
	RETURN
END


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

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

IF Captive OR Consignment THEN RETURN

IF ErrorMsg THEN RETURN


ItemDesc = 'Order from Vision'

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

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


IF VisionInvoiceNo NE '' AND OrderNo NE '' THEN
	obj_Order_Det('AddVisionInvoice',OrderNo:@RM:ItemNo:@RM:VisionInvoiceNo:@RM:VisionInvoiceItem)
END

OPEN 'ORDER_RECON' TO ORTable ELSE
	ErrorMsg = 'Unable to open "ORDER_RECON" for update.'
	RETURN
END

OPEN 'DICT.ORDER_RECON' TO DictORTable ELSE
	ErrorMsg = 'Unable to open "DICT.ORDER_RECON" for update.'
	RETURN
END

WRITE TimeStamp ON DictORTable,'%LAST_DTM%' ELSE
	ErrorMsg = 'Unable to WRITE TimeStamp on DICT.ORDER_RECON'
	RETURN
END


READ ORRec FROM ORTable,VisionOrderNo ELSE
	ORRec = ''		;* No locking - this is the only process with access to this table

	ORRec<ORDER_RECON_ORDER_NO$> = OrderNo
END

LoadedItems = ''

IF OrderNo = '' THEN
	obj_Order('PostVision',Parms)							;* Put into OpenInsight  ????????????????????
END

IF NOT(RowExists('ORDER_DET',OrderNo:'*':ItemNo)) THEN
	obj_Order('PostVision',Parms)							;* Put into OpenInsight  ????????????????????
END

OrderRec = XLATE('ORDER',OrderNo,'','X')
OrderDetRec = XLATE('ORDER_DET',OrderNo:'*':ItemNo,'','X')

IF  OrderRec<ORDER_BILL_TO_CUST_NO$> NE BillToCustNo THEN
	
	DetItemNo		= '-'
	FieldName	= 'BILL_TO_CUST_NO'
	VisionVal	= BillToCustNo
	OIVal		= OrderRec<ORDER_BILL_TO_CUST_NO$>
	
	GOSUB LoadItem
END

IF OrderRec<ORDER_SHIP_TO_CUST_NO$>	NE ShipToCustNo THEN
	
	DetItemNo	= '-'
	FieldName	= 'SHIP_TO_CUST_NO'
	VisionVal	= ShipToCustNo
	OIVal		= OrderRec<ORDER_SHIP_TO_CUST_NO$>
	
	GOSUB LoadItem
END	

OrdPONo		= OrderRec<ORDER_PO_NO$>
OrdRelNo	= OrderRec<ORDER_PO_REL$>
OrdLineNo	= OrderRec<ORDER_PO_LINE$>

BEGIN CASE
	CASE OrdPONo = CustPONo	
		Match = 1
		
	CASE OrdPONo:'-':OrdRelNo = CustPONo
		OrdPONo = OrdPONo:'-':OrdRelNo
		Match = 1
		
	CASE OrdPONo:'-':OrdLineNo = CustPONo
		OrdPONo = OrdPONo:'-':OrdLineNo
		Match = 1
		
		
	CASE OrdPONo:'-':OrdRelNo:'-':OrdLineNo = CustPONo
		OrdPONo = OrdPONo:'-':OrdRelNo:'-':OrdLineNo
		Match = 1
		
		
	CASE OrdPONo:'-':OrdLineNo:'-':OrdRelNo = CustPONo
		OrdPONo = OrdPONo:'-':OrdLineNo:'-':OrdRelNo
		Match = 1
		
	CASE 1
		Match = 0
	
END CASE



IF NOT(Match) THEN
	
	DetItemNo	= '-'
	FieldName	= 'PO_NO'
	VisionVal	= CustPONo
	OIVal		= OrdPONo
	
	GOSUB LoadItem
END	


IF OrderRec<ORDER_VISION_CUST_NAME$> NE CustName THEN

	DetItemNo	= '-'
	FieldName	= 'VISION_CUST_NAME'
	VisionVal	= CustName
	OIVal		= OrderRec<ORDER_VISION_CUST_NAME$>

	GOSUB LoadItem
END

IF OrderDetRec<ORDER_DET_QUOTE_NO$> NE ItemQuoteNo THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'QUOTE_NO'
	VisionVal	= ItemQuoteNo
	OIVal		= OrderDetRec<ORDER_DET_QUOTE_NO$>

	GOSUB LoadItem
END

IF OrderDetRec<ORDER_DET_EPI_PN$> NE EpiPartNo THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'EPI_PN'
	VisionVal	= EpiPartNo
	OIVal		= OrderDetRec<ORDER_DET_EPI_PN$>
	
	GOSUB LoadItem
END


IF OrderDetRec<ORDER_DET_UNIT_PRICE$> NE thisItemUnitPrice THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'UNIT_PRICE'
	VisionVal	= thisItemUnitPrice
	OIVal		= OrderDetRec<ORDER_DET_UNIT_PRICE$>
	
	GOSUB LoadItem
END

IF OrderDetRec<ORDER_DET_ITEM_QTY$> NE thisItemQty THEN

	DetItemNo	= ItemNo
	FieldName	= 'ITEM_QTY'
	VisionVal	= thisItemQty
	OIVal		= OrderDetRec<ORDER_DET_ITEM_QTY$>
	
	GOSUB LoadItem
END
	
IF OrderDetRec<ORDER_DET_VISION_LINE_NO$> NE VisionItemNo THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'VISION_LINE_NO'
	VisionVal	= VisionItemNo
	OIVal		= OrderDetRec<ORDER_DET_VISION_LINE_NO$>
	
	GOSUB LoadItem
END
	
IF OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$> NE LineItemStatus THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'VISION_ITEM_STATUS'
	VisionVal	= LineItemStatus
	OIVal		= OrderDetRec<ORDER_DET_VISION_ITEM_STATUS$>
	
	GOSUB LoadItem
END	

IF OrderDetRec<ORDER_DET_VISION_PART_TYPE$> NE PartType THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'VISION_PART_TYPE'
	VisionVal	= PartType
	OIVal		= OrderDetRec<ORDER_DET_VISION_PART_TYPE$>
	
	GOSUB LoadItem
END	

IF OrderDetRec<ORDER_DET_CUST_PN$> NE CustPartNo THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'CUST_PN'
	VisionVal	= CustPartNo
	OIVal		= OrderDetRec<ORDER_DET_CUST_PN$>
	
	GOSUB LoadItem
END	

IF XLATE('ORDER_DET',OrderNo:'*':ItemNo,'TOT_SHIP_WFR_QTY','X') NE ItemShipQty THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'VISION_SHIP_QTY'
	VisionVal	= ItemShipQty
	OIVal		= XLATE('ORDER_DET',OrderNo:'*':ItemNo,'TOT_SHIP_WFR_QTY','X')
	
	GOSUB LoadItem
END	
	
IF OrderDetRec<ORDER_DET_PROMISE_DT$> NE thisItemPromiseDt THEN

	DetItemNo	= ItemNo
	FieldName	= 'PROMISE_DT'
	VisionVal	= thisItemPromiseDt
	OIVal		= OrderDetRec<ORDER_DET_PROMISE_DT$>
	
	GOSUB LoadItem
END		
	
IF OrderDetRec<ORDER_DET_REQ_SHIP_DT$> NE thisItemReqShipDt THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'REQ_SHIP_DT'
	VisionVal	= thisItemReqShipDt
	OIVal		= OrderDetRec<ORDER_DET_REQ_SHIP_DT$>
	
	GOSUB LoadItem
END	
	
IF OrderDetRec<ORDER_DET_SHIP_WIN_DT$> NE thisItemCalcSWODt THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'SHIP_WIN_DT'
	VisionVal	= thisItemCalcSWODt
	OIVal		= OrderDetRec<ORDER_DET_SHIP_WIN_DT$>
	
	GOSUB LoadItem
END

IF OrderDetRec<ORDER_DET_CUST_REQ_DT$> NE thisItemCRDt THEN
	
	DetItemNo	= ItemNo
	FieldName	= 'CUST_REQ_DT'
	VisionVal	= thisItemCRDt
	OIVal		= OrderDetRec<ORDER_DET_CUST_REQ_DT$>
	
	GOSUB LoadItem
END

IF ORRec NE OrderNo THEN
	WRITE ORRec ON ORTable,VisionOrderNo ELSE NULL
END

RETURN


* * * * * * *
LoadItem:
* * * * * * * 


LoadedItem 	= DetItemNo:'*':FieldName

LOCATE LoadedItem IN LoadedItems USING @VM SETTING Pos ELSE
	LoadedItems = INSERT(LoadedItems,1,Pos,0,LoadedItem)
	ORRec = INSERT(ORRec,ORDER_RECON_ITEM_NO$,Pos,0,DetItemNo)
	ORRec = INSERT(ORRec,ORDER_RECON_FIELD_NAME$,Pos,0,FieldName)
	ORRec = INSERT(ORRec,ORDER_RECON_VISION_VALUE$,Pos,0,VisionVal)
	ORRec = INSERT(ORRec,ORDER_RECON_OI_VALUE$,Pos,0,OIVal)
END

RETURN







