COMPILE FUNCTION obj_Annual_Contracts(Method,Parms)
/*
	Methods for ANNUAL_CONTRACTS table
    
	12/17/2003 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    AddOrder(DataStruct)		;* Add Order information to Annual Contracts	(from WRITE event of Order window)
	RemOrder					;* Rem Order Reference from Annual Contracts	(new to fix problems with garbage)
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Dialog_Box, Popup
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables

$INSERT MSG_EQUATES
$INSERT POPUP_EQUATES
$INSERT LOGICAL
$INSERT ANNUAL_CONTRACTS_EQU

ErrTitle = 'Error in Stored Procedure "obj_Annual_Contracts"'
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 = 'AddOrder'	; GOSUB AddOrder
	CASE Method = 'RemOrder'	; GOSUB RemOrder
	CASE 1

END CASE

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

RETURN Result


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


AcKeys = Dialog_Box( 'ANNUAL_CONTRACTS_QUERY', @window, '' )
Result = AcKeys

RETURN



* * * * * * *
AddOrder:
* * * * * * *

OrderNo		= Parms[1,@RM]
ACID		= Parms[COL2()+1,@RM]
ACPO		= Parms[COL2()+1,@RM]
ACRelNo		= Parms[COL2()+1,@RM]
ACRelDt		= Parms[COL2()+1,@RM]
OrderWOs	= Parms[COL2()+1,@RM]
WOSetupAmts	= Parms[COL2()+1,@RM]
WOQtys		= Parms[COL2()+1,@RM]
WOPrices	= Parms[COL2()+1,@RM]


IF OrderNo = ''	THEN ErrorMsg = 'Null parameter OrderNo passed to routine (':Method:').'
IF ACID = ''	THEN ErrorMsg = 'Null parameter ACID passed to routine (':Method:').'
IF ACPO = ''	THEN ErrorMsg = 'Null parameter ACPO passed to routine (':Method:').'
*IF ACRelNo = ''	THEN ErrorMsg = 'Null parameter ACRelNo passed to routine (':Method:').'
IF ACRelDt = ''	THEN ErrorMsg = 'Null parameter ACRelDt passed to routine (':Method:').'

IF ErrorMsg NE '' THEN RETURN

thisACRelDt = ICONV(ACRelDt,'D')
IF thisACRelDt = '' THEN
	ErrorMsg = 'Invalid data ACRelDt ':QUOTE(ACRelDt):' passed to routine (':Method:').'
	RETURN
END

IF WOSetUpAmts NE '' THEN
	thisWOSetUpAmts = ICONV(WOSetUpAmts,'MD2')
	IF thisWOSetUpAmts = '' THEN
		SWAP @VM WITH ', ' IN WOSetUpAmts
		ErrorMsg = 'Invalid data WOSetUpAmts ':QUOTE(WOSetUpAmts):' passed to routine (':Method:').'
		RETURN
	END		
END ELSE
	thisWOSetUpAmts = ''
END
	
IF WOQtys NE '' THEN
	thisWOQtys = ICONV(WOQtys,'MD0')
	IF thisWOQtys = '' THEN
		SWAP @VM WITH ', ' IN WOQtys
		ErrorMsg = 'Invalid data WOQtys ':QUOTE(WOQtys):' passed to routine (':Method:').'
		RETURN
	END	
END ELSE
	thisWOQtys = ''
END

IF WOPrices NE '' THEN
	thisWOPrices = ICONV(WOPrices,'MD2')
	IF thisWOPrices = '' THEN
		SWAP @VM WITH ', ' IN WOPrices
		ErrorMsg = 'Invalid data WOPrices ':QUOTE(WOPrices):' passed to routine (':Method:').'
		RETURN
	END	
END ELSE
	thisWOPrices = ''
END

TableVar = ''
OtParms = 'ANNUAL_CONTRACTS':@RM:ACID:@RM:TableVar 
ACRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update
IF Get_Status(errCode) THEN Return

WOCnt = COUNT(OrderWOs,@VM) + (OrderWOs NE '')

FOR I = 1 TO WOCnt

	thisOrderWO		= OrderWOs<1,I>
	thisWOSetUpAmt	= thisWOSetUpAmts<1,I>
	thisWOQty		= thisWOQtys<1,I>
	thisWOPrice		= thisWOPrices<1,I>

	LOCATE thisOrderWO IN ACRec<annual_contracts_rel_wo$> BY 'DR' USING @VM SETTING Pos THEN
		ACRec<annual_contracts_rel_po$,Pos>				= ACPO
		ACRec<annual_contracts_rel_no$,Pos>				= ACRelNo
		ACRec<annual_contracts_rel_date$,Pos>			= thisACRelDt
		ACRec<annual_contracts_rel_setup_charge$,Pos>	= thisWOSetUpAmt
		ACRec<annual_contracts_rel_quantity$,Pos>		= thisWOQty
		ACRec<annual_contracts_rel_price$,Pos>			= thisWOPrice
	END ELSE
		* Put new orders into top of the list
		ACRec = insert(ACRec,annual_contracts_rel_no$,Pos,0,ACRelNo)
		ACRec = insert(ACRec,annual_contracts_rel_po$,Pos,0,ACPO)
		ACRec = insert(ACRec,annual_contracts_rel_date$,Pos,0,thisACRelDt)
		ACRec = insert(ACRec,annual_contracts_rel_wo$,Pos,0,thisOrderWO)
		ACRec = insert(ACRec,annual_contracts_rel_setup_charge$,Pos,0,thisWOSetUpAmt)
		ACRec = insert(ACRec,annual_contracts_rel_quantity$,Pos,0,thisWOQty)
		ACRec = insert(ACRec,annual_contracts_rel_price$,Pos,0,thisWOPrice)
	END

NEXT I

OtParms = FieldStore(OtParms,@RM,4,1,ACRec)

obj_Tables('WriteRec',OtParms)		;* Writes and unlocks the record

RETURN



* * * * * * *
RemOrder:
* * * * * * *

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

IF ACID	= ''	THEN ErrorMsg = 'Null parameter ACID passed to routine (':Method:').'
IF PONo = ''	THEN ErrorMsg = 'Null parameter PONo passed to routine (':Method:').'

TableVar = ''
OtParms = 'ANNUAL_CONTRACTS':@RM:ACID:@RM:TableVar 
ACRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update
IF Get_Status(errCode) THEN Return

Done = 0
Updated = 0
LOOP
	LOCATE PONo IN ACRec<annual_contracts_rel_po$> USING @VM SETTING Pos THEN

		* Update existing reference
		
		ACRec = Delete(ACRec,annual_contracts_rel_no$,Pos,0)
		ACRec = Delete(ACRec,annual_contracts_rel_po$,Pos,0)
		ACRec = Delete(ACRec,annual_contracts_rel_date$,Pos,0)
		ACRec = Delete(ACRec,annual_contracts_rel_wo$,Pos,0)
		ACRec = Delete(ACRec,annual_contracts_rel_setup_charge$,Pos,0)
		ACRec = Delete(ACRec,annual_contracts_rel_quantity$,Pos,0)
		ACRec = Delete(ACRec,annual_contracts_rel_price$,Pos,0)
		
		Updated = 1
		
	END ELSE
		Done = 1
	END
UNTIL Done
REPEAT

IF NOT(Updated) THEN
	obj_Tables('UnlockRec',OtParms)
	RETURN		;* Invoice not on this Schedule, nothing to do
END ELSE
	OtParms = FieldStore(OtParms,@RM,4,1,ACRec)
	obj_Tables('WriteRec',OtParms)		;* Writes and unlocks the record
END

RETURN

