COMPILE FUNCTION obj_PRS_Layer(Method,Parms)
/*
	Methods for the PRS_Layer table
    
	06/14/2011 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Convert()						;* Creates and overwrites PRS_LAYER records based on PROD_SPEC record
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, NextKey, obj_Prod_Spec, obj_Popup
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, RList, ErrMsg, Btree.Extract

$INSERT MSG_EQUATES
$INSERT PRS_PROP_EQUATES
$INSERT PROD_SPEC_EQUATES
$INSERT QUOTE_SPEC_EQU
$INSERT PRS_LAYER_EQUATES


EQU PRS_LAYER_RECIPE$				TO 2

EQU PRS_LAYER_CONC_TARGET$			TO 30	;* These fields are used when returning data from obj_Prod_Spec
EQU PRS_LAYER_RES_TARGET$			TO 31
EQU PRS_LAYER_THICK_TARGET$			TO 32
EQU PRS_LAYER_STRESS_TARGET$		TO 33
EQU PRS_LAYER_CRES_TARGET$			TO 34



EQU PRS_MTOOL$			TO 1
EQU PRS_MTYPE$			TO 2
EQU PRS_MRECIPE$		TO 3
EQU PRS_MFREQ$			TO 4
EQU PRS_MPROVEIN$		TO 5
EQU PRS_MFIRST$			TO 6
EQU PRS_MLAST$			TO 7
EQU PRS_MSPC$			TO 8
EQU PRS_MOVERGROW$		TO 9
EQU PRS_MEXP_TW_QTY$	TO 10
EQU PRS_MPATTERN$		TO 11

ErrTitle = 'Error in Stored Procedure "obj_PRS_LAYER"'
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 = 'SortPropKeys'	; GOSUB SortPropKeys
	CASE Method = 'Convert'			; GOSUB Convert
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to object.'

END CASE

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

RETURN Result


* * * * * * *
SortPropKeys:
* * * * * * *

PRSLKey 	= Parms[1,@RM]
PRSLRec	= Parms[COL2()+1,@RM]

IF PRSLKey = '' THEN RETURN
IF PRSLRec = '' THEN
	PRSLRec = XLATE('PROD_SPEC',PRSLKey,'','X')
	IF PRSLRec = '' THEN
		RETURN
	END
END

LSKeys = PRSLRec<PRS_LAYER_PRS_PROP_KEY$>

SortedProps = obj_Popup('AllCodes','MET_PROPERTY':@RM:2)	;* All Met Prop codes in the MET_PROPERTY popup

lsCnt = COUNT(LSKeys,@VM) + (LSKeys NE '')

SortedKeys = ''

FOR I = 1 TO lsCnt
	LSKey = LSKeys<1,I>
	LSProp = FIELD(LSKey,'*',3)
	LOCATE LSProp IN SortedProps USING @VM SETTING Pos THEN
		SortedKeys = INSERT(SortedKeys,1,Pos,0,LSKey)
	END
NEXT I


CONVERT @VM TO ' ' IN SortedKeys
SortedKeys = TRIM(SortedKeys)
CONVERT ' ' TO @VM IN SortedKeys

Result = SortedKeys

RETURN


* * * * * * *
Convert:
* * * * * * *

PSNo 	= Parms[1,@RM]
PSRec	= Parms[COL2()+1,@RM]

DEBUG

IF PSNo = '' THEN RETURN
IF PSRec = '' THEN
	PSRec = XLATE('PROD_SPEC',PSNo,'','X')
	IF PSRec = '' THEN
		RETURN
	END
	
	
	LayerSpecs = obj_Prod_Spec('GetLayerProp',PSNo:@RM:@RM:1)	;* Returns specs for all layers in internal format


	* LayerSpecs is @RM between layers, @FM between fields, LayerSet ID is in the first Field and needs to peeled off
	* before the equates match up correctly

	* Prod_Spec table has layer specs all in one field
	* RDS has First layer stuff in individual fields and then has 2 and 3 shoved into Field 33 (Layer Info)

	LayerSpec 	= FIELD(LayerSpecs,@RM,1)				;* Take the first Layer
	LayerSet	= FIELD(LayerSpec,@FM,1)				;* Not used here but shown for clarity
	LayerSpec	= FIELD(LayerSpec,@FM,2,99)				;* LayerSpec without the LayerSet

	RecipeNo = LayerSpec<PRS_LAYER_RECIPE$>
	RecipeRec = XLATE('RECIPE',RecipeNo,'','X')			;* This used in 2nd and 3rd layer stuff (in error it appears)

	


	FOR I = 1 TO COUNT(LayerSpecs,@RM) + (LayerSpecs NE '')
		LayerSpec	= FIELD(LayerSpecs,@RM,I)		;* Take the Ith Layer
		LayerSet	= FIELD(LayerSpec,@FM,1)
		LayerSpec	= FIELD(LayerSpec,@FM,2,99)	;* LayerSpec without the LayerSet
		
		IF LayerSet = I THEN NoCombinedLayerFlag = 1 ELSE NoCombinedLayerFlag = 0
		
		
		
		
		
		*obj_RDS_Layer('Create',RDSNo:@RM:LayerSet:@RM:PS_No:@RM:NoCombinedLayerFlag)

		*RDSRec<RDS_RDS_LAYER_KEYS$,I> = RDSNo:'*':LayerSet							;* Added 4/17/2006 JCH

		*obj_RDS_Test('Create',RDSNo:@RM:LayerSet:@RM:PS_No)
		
NEXT I

	
	
	
	
END

/*
TestRec = PreRec
CONVERT @FM:@VM TO '':'' IN TestRec

IF TestRec NE '' THEN
	obj_Tables('WriteRec','PRS_STAGE':@RM:PreKey:@RM:@RM:PreRec)
END

*/


RETURN
