Compile function WO_Mat_QA_Services(@Service, @Params)
/***********************************************************************************************************************

    Name        :   WO_Mat_QA_Services

    Description :   Handler program for all WO_Mat_QA services.

    Notes       :   Application errors should be logged using the Error Services module. There are a few methodological
                    assumptions built into way errors are managed which are important to understand in order to properly
                    work with Error Services:

                    - The term 'top' refers to the originating procedure of a call stack and the term 'bottom' refers to
                      the last routine (or the current routine) within a call stack. Within the OpenInsight Debugger
                      this will appear backwards since the originating procedure always appears at the bottom of the
                      list and the current routine appears at the top of the list. We are using this orientation because
                      it is common to refer to the process of calling other procedures as 'drilling down'.

                    - The reason for defining the orientation of the call stack is because Error_Services allows for
                      multiple error conditions to be appended to an original error. In most cases this will happen when
                      a procedure at the bottom of the stack generates an error condition and then returns to its
                      calling procedure. This higher level procedure can optionally add more information relevant to
                      itself. This continues as the call stack 'bubbles' its way back to the top to where the
                      originating procedure is waiting.

                    - Native OpenInsight commands that handle errors (e.g., Set_Status, Set_FSError, Set_EventStatus)
                      preserve their error state until explicitly cleared. This can hinder the normal execution of code
                      since subsequent procedures (usually SSPs) will fail if a pre-existing error condition exists.
                      Our philosophy is that error conditions should automatically be cleared before a new procedure
                      is executed to avoid this problem. However, the nature of Basic+ does not make this easy to
                      automate for any given stored procedure. Therefore, if a stored procedure wants to conform to our
                      philosophy then it should include a call into the 'Clear' service request at the top of the
                      program. Alternatively this can be done through a common insert (see SERVICE_SETUP for example.)

                    - Service modules will use the SERVICE_SETUP insert and therefore automatically clear out any
                      error conditions that were set before.

    Parameters  :
        Service         [in] -- Name of the service being requested
        Param1-10   [in/out] -- Additional request parameter holders
        Response       [out] -- Response to be sent back to the Controller (MCP) or requesting procedure

    Metadata    :

    History     :   (Date, Initials, Notes)
        11/16/22    djs     Original programmer.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler

$Insert LOGICAL
$Insert SERVICE_SETUP
$Insert WO_LOG_EQUATES
$Insert WO_MAT_EQUATES
$Insert WO_STEP_EQUATES
$Insert WO_MAT_QA_EQUATES
$Insert PRS_LAYER_EQUATES
$Insert PRS_STAGE_EQUATES
$Insert PROD_VER_EQUATES
$Insert QA_MET_EQUATES			;* Used in GetQAMet data structure return variable

Declare function   Database_Services, SRP_JSON, Error_Services, obj_Prod_Spec
Declare subroutine Database_Services, SRP_JSON, Error_Services

GoToService

Return Response or ""

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service ConvertRecordToJSON(KeyID, Record, ItemURL)
    
    jsonRecord    = ''

    If KeyID NE '' then

        If Record EQ '' then Record = Database_Services('ReadDataRow', 'WO_MAT_QA', KeyID)
        If Error_Services('NoError') then
            @DICT   = Database_Services('GetTableHandle', 'DICT.WO_MAT_QA')
            @ID     = KeyID
            @RECORD = Record
            objJSON = ''
            If SRP_JSON(objJSON, 'New', 'Object') then
                objWOMatQA = ''
                If SRP_JSON(objWOMatQA, 'New', 'Object') then 
                    SRP_JSON(objWOMatQA, 'SetValue', 'keyId', @ID)
                    Stages         = {STAGE}
                    Profiles       = {PROFILE}
                    Props          = {PROP}
                    ToolClasses    = {TOOL_CLASS}
                    Recipes        = {RECIPE}
                    RecipePatterns = {RECIPE_PATTERN}
                    SpecMins       = {MIN}
                    SpecMaxes      = {MAX}
                    SpecSlots      = {SLOT}
                    TestSlots      = {SLOT_TEST}
                    TestResults    = {RESULT}
                    TestSigs       = {SIG}
                    TestSigDtms    = {SIG_DTM}
                    SpecStdDevs    = {STD_MAX}
                    TestStdDevs    = {STD_RESULT}
                    SpecWfrQty     = {WFR_QTY}
                    TestData       = {DATA_POINTS}
                    TestMins       = {MIN_RESULT}
                    TestMaxes      = {MAX_RESULT}
                    OutOfSpec      = {OUT_OF_SPEC}
                    PhaseMins      = {PHASE_MIN}
                    FailReasons    = {FAIL_REASON}
                    If Stages NE '' then
                        objTestArray = ''
                        If SRP_JSON(objTestArray, 'New', 'Array') then
                            For each Stage in Stages using @VM setting vPos
                                objTest = ''
                                If SRP_JSON(objTest, 'New', 'Object') then
                                    SRP_JSON(objTest, 'SetValue', 'stage', Stage)
                                    SRP_JSON(objTest, 'SetValue', 'profile', Profiles<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'prop', Props<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'toolClass', ToolClasses<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'recipe', Recipes<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'recipePattern', RecipePatterns<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'specMin', SpecMins<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'specMax', SpecMaxes<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'specSlot', SpecSlots<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'testSlot', TestSlots<0, vPos>)
                                    SRP_JSON(objTest, 'SetValueArray', 'testResult', TestResults<0, vPos>, @SVM)
                                    SRP_JSON(objTest, 'SetValue', 'testSig', TestSigs<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'testSigDtm', OConv(TestSigDtms<0, vPos>, 'DT2/^H'))
                                    SRP_JSON(objTest, 'SetValue', 'specStdDev', SpecStdDevs<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'testStdDev', TestStdDevs<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'specWfrQty', SpecWfrQty<0, vPos>)
                                    objDataArray = ''
                                    If SRP_JSON(objDataArray, 'New', 'Array') then
                                       DataPoints = TestData<0, vPos>
                                       For each Row in DataPoints using @SVM
                                           SRP_JSON(objDataArray, 'AddValueArray', Row, @TM)
                                       Next Row
                                       SRP_JSON(objTest, 'Set', 'dataPoints', objDataArray)
                                       SRP_JSON(objDataArray, 'Release')
                                    end
                                    SRP_JSON(objTest, 'SetValueArray', 'testResultMin', TestMins<0, vPos>, @SVM)
                                    SRP_JSON(objTest, 'SetValueArray', 'testResultMax', TestMaxes<0, vPos>, @SVM)
                                    SRP_JSON(objTest, 'SetValue', 'testOutOfSpec', OutOfSpec<0, vPos>, 'Boolean')
                                    SRP_JSON(objTest, 'SetValue', 'phaseMin', PhaseMins<0, vPos>)
                                    SRP_JSON(objTest, 'SetValue', 'failReason', FailReasons<0, vPos>)
                                end
                                SRP_JSON(objTestArray, 'Add', objTest)
                                SRP_JSON(objTest, 'Release')
                            Next Stage
                        end
                        SRP_JSON(objWOMatQA, 'Set', 'tests', objTestArray)
                        SRP_JSON(objTestArray, 'Release')
                    end
                    SRP_JSON(objJSON, 'Set', 'woMatQa', objWOMatQA)
                    SRP_JSON(objWOMatQA, 'Release')
                end
                
                If itemURL NE '' then
                    // The itemURL was passed in so add HAL+JSON properties.

                    // Create the _links property and then all link objects needed for this resource.
                    objLinks = ''
                    If SRP_JSON(objLinks, 'New', 'Object') then
                        // Create a self link.
                        objLink = ''
                        If SRP_JSON(objLink, 'New', 'Object') then
                            SRP_JSON(objLink, 'SetValue', 'href', ItemURL, 'String')
                            SRP_JSON(objLink, 'SetValue', 'title', 'Self', 'String')
                            SRP_JSON(objLinks, 'Set', 'self', objLink)
                            SRP_JSON(objLink, 'Release')
                        end
                        SRP_JSON(objJSON, 'Set', '_links', objLinks)
                        SRP_JSON(objLinks, 'Release')
                    end

                    // Create the _class property for this resource.
                    SRP_JSON(objJSON, 'SetValue', '_class', 'resource')
                end
                jsonRecord = SRP_JSON(objJSON, 'Stringify', 'Styled')
                SRP_JSON(objJSON, 'Release')
            end else
                Error_Services('Add', 'Unable to create JSON representation in the ' : Service : ' service.')
            end
        end
    end else
        Error_Services('Add', 'KeyID argument was missing in the ' : Service : ' service.')
    end

    Response    = jsonRecord
    
End Service


Service UpdateQAMet(WOMatKey)
    
    WOMatQARec = ''
    ErrorMsg   = ''
    If (WOMatKey NE '') then
        
        WONo   = Field(WOMatKey, '*', 1)
        CassNo = Field(WOMatKey, '*', 2)

        * * * * * * * *   Build QA_MET profile - Work Order scheduled only   * * * * * * * *
        WOStepKeys = XLATE('WO_LOG', WONo, WO_LOG_WO_STEP_KEY$, 'X')

        StepCnt = DCount(WOStepKeys,@VM)

        EpiProPSNo = ''
        EpiProPSRec = ''

        MetLine = 1

        // Get original WO_MAT_QA record to copy any test results
        OrigWOMatQARec     = Database_Services('ReadDataRow', 'WO_MAT_QA', WOMatKey)
        OrigQAStages       = OrigWOMatQARec<WO_MAT_QA_STAGE$>
        OrigQAProfiles     = OrigWOMatQARec<WO_MAT_QA_PROFILE$>
        OrigQASlots        = OrigWOMatQARec<WO_MAT_QA_SLOT$>
        OrigQASlotTests    = OrigWOMatQARec<WO_MAT_QA_SLOT_TEST$>
        OrigQAResults      = OrigWOMatQARec<WO_MAT_QA_RESULT$>
        OrigQASigs         = OrigWOMatQARec<WO_MAT_QA_SIG$>
        OrigQASigDtms      = OrigWOMatQARec<WO_MAT_QA_SIG_DTM$>
        OrigQAStdResults   = OrigWOMatQARec<WO_MAT_QA_STD_RESULT$>
        OrigQADataPoints   = OrigWOMatQARec<WO_MAT_QA_DATA_POINTS$>
        OrigQAMinResults   = OrigWOMatQARec<WO_MAT_QA_MIN_RESULT$>
        OrigQAMaxResults   = OrigWOMatQARec<WO_MAT_QA_MAX_RESULT$>
        OrigQARangeResults = OrigWOMatQARec<WO_MAT_QA_RANGE_PCT_RESULT$>
        OrigQAEdgeResults  = OrigWOMatQARec<WO_MAT_QA_EDGE_MEAN_RESULT$>
        OrigQA5mmResults   = OrigWOMatQARec<WO_MAT_QA_5MM_PCT_RESULT$>
        OrigQAOutOfSpecs   = OrigWOMatQARec<WO_MAT_QA_OUT_OF_SPEC$>
        OrigQAFailReasons  = OrigWOMatQARec<WO_MAT_QA_FAIL_REASON$>
        
        * WO_MAT record is used to check for pre-existing signatures jch 6/7/2015
        WOMatRec   = Database_Services('ReadDataRow', 'WO_MAT', WOMatKey)

        FOR WOStepNo = 1 TO StepCnt
            PSNo      = XLATE('WO_STEP', WOStepKeys<1,WOStepNo>, WO_STEP_PROD_SPEC_ID$, 'X')
            PSRec     = Database_Services('ReadDataRow', 'PROD_SPEC', PSNo) 
            QAMetData = obj_Prod_Spec('GetQAMet',PSNo:@RM:PSRec)
            QAStages  = QAMetData<COL$QA_MET_STAGE>
            StageCnt  = COUNT(QAStages,@VM) + (QAStages NE '')
            
            FOR StageNo = 1 TO StageCnt
                Stage = QAMetData<COL$QA_MET_STAGE,StageNo>
                
                MetTest = QAMetData<COL$QA_MET_TEST,StageNo>				;* StageRec<PRS_STAGE_MET_TEST$>
            
                    Interval   = QAMetData<COL$QA_MET_INTERVAL,StageNo>		;* StageRec<PRS_STAGE_MET_INTERVAL$,StageNo>
                    Start      = QAMetData<COL$QA_MET_START,StageNo>		;* StageRec<PRS_STAGE_MET_START$,StageNo>
                    ReactSched = QAMetData<COL$QA_MET_REACT_SCHED,StageNo>	;* StageRec<PRS_STAGE_MET_REACT_SCHED$,StageNo>
                    TestFlag   = 0
                    
                    IF Interval NE '' AND Start NE '' THEN
                        IF Interval = Start THEN
                            IF REM(CassNo,Interval) = 0 THEN TestFlag = 1
                        END ELSE
                            IF ABS((Start + INT(CassNo/Interval)*Interval) - CassNo) = 0 THEN TestFlag = 1
                        END
                        IF TestFlag AND NOT(ReactSched) THEN
                            WOMatQARec<WO_MAT_QA_PROFILE$,MetLine>			= WOStepNo:QAMetData<COL$QA_MET_TEST,StageNo>		;* StageRec<PRS_STAGE_MET_TEST$,N>
                            WOMatQARec<WO_MAT_QA_PROP$,MetLine>				= QAMetData<COL$QA_MET_PROP,StageNo>				;* StageRec<PRS_STAGE_MET_PROP$,N>
                            WOMatQARec<WO_MAT_QA_TOOL_CLASS$,MetLine>		= QAMetData<COL$QA_MET_TOOL_CLASS,StageNo>			;* StageRec<PRS_STAGE_MET_TOOL_CLASS$,N>
                            WOMatQARec<WO_MAT_QA_STAGE$,MetLine>			= QAMetData<COL$QA_MET_STAGE,StageNo>
                            WOMatQARec<WO_MAT_QA_MIN$,MetLine>				= QAMetData<COL$QA_MET_MIN,StageNo>					;* StageRec<PRS_STAGE_MET_MIN$,N>
                            WOMatQARec<WO_MAT_QA_MAX$,MetLine>				= QAMetData<COL$QA_MET_MAX,StageNo>					;* StageRec<PRS_STAGE_MET_MAX$,N>
                            WOMatQARec<WO_MAT_QA_SLOT$,MetLine>				= QAMetData<COL$QA_MET_SLOT,StageNo>				;* StageRec<PRS_STAGE_MET_SLOT$,N>
                            WOMatQARec<WO_MAT_QA_RECIPE$,MetLine>			= QAMetData<COL$QA_MET_RECIPE,StageNo>				;* StageRec<PRS_STAGE_MET_RECIPE$,N>
                            WOMatQARec<WO_MAT_QA_RECIPE_PATTERN$,MetLine>	= QAMetData<COL$QA_MET_RECIPE_PATTERN,StageNo>		;* StageRec<PRS_STAGE_MET_RECIPE_PATTERN$,StageNo>
                            WOMatQARec<WO_MAT_QA_WFR_QTY$,MetLine>			= QAMetData<COL$QA_MET_WFR_QTY,StageNo>				;* StageRec<PRS_STAGE_MET_WFR_QTY$,StageNo>
                            WOMatQARec<WO_MAT_QA_WFR_TYPE$,MetLine>			= QAMetData<COL$QA_MET_WFR_TYPE,StageNo>			;* StageRec<PRS_STAGE_MET_WFR_TYPE$,StageNo>
                            WOMatQARec<WO_MAT_QA_REACT_SCHED$,MetLine>		= ReactSched										;* StageRec<PRS_STAGE_MET_REACT_SCHED$,StageNo>
                            WOMatQARec<WO_MAT_QA_SHIP_DOC$,MetLine>			= QAMetData<COL$QA_MET_SHIP_DOC,StageNo>			;* StageRec<PRS_STAGE_MET_SHIP_DOC$,StageNo>
                            WOMatQARec<WO_MAT_QA_PHASE_MIN$,MetLine>		= QAMetData<COL$QA_MET_PHASE_MIN,StageNo>
                            
                            If OrigWOMatQARec NE '' then
                                CurrStage   = QAMetData<COL$QA_MET_STAGE,StageNo>
                                CurrProfile = WOStepNo:QAMetData<COL$QA_MET_TEST,StageNo>
                                CurrSlot    = QAMetData<COL$QA_MET_SLOT,StageNo>	
                                // Copy any existing results from the original WO_MAT_QA record.
                                If OrigQAStages NE '' then
                                    Found = False$
                                    For each OrigQAStage in OrigQAStages using @VM setting sPos
                                        OrigQAProfile = OrigQAProfiles<0, sPos>
                                        OrigQASlot    = OrigQASlots<0, sPos>
                                        If ( (CurrStage EQ OrigQAStage) and (CurrProfile EQ OrigQAProfile) and (CurrSlot EQ OrigQASlot) ) then
                                            Found                                           = True$
                                            WOMatQARec<WO_MAT_QA_SLOT_TEST$,MetLine>        = OrigQASlotTests<0, sPos>
                                            WOMatQARec<WO_MAT_QA_RESULT$,MetLine>           = OrigQAResults<0, sPos>
                                            WOMatQARec<WO_MAT_QA_SIG$,MetLine>              = OrigQASigs<0, sPos>
                                            WOMatQARec<WO_MAT_QA_SIG_DTM$,MetLine>          = OrigQASigDtms<0, sPos>
                                            WOMatQARec<WO_MAT_QA_STD_RESULT$,MetLine>       = OrigQAStdResults<0, sPos>
                                            WOMatQARec<WO_MAT_QA_DATA_POINTS$,MetLine>      = OrigQADataPoints<0, sPos>
                                            WOMatQARec<WO_MAT_QA_MIN_RESULT$,MetLine>       = OrigQAMinResults<0, sPos>
                                            WOMatQARec<WO_MAT_QA_MAX_RESULT$,MetLine>       = OrigQAMaxResults<0, sPos>
                                            WOMatQARec<WO_MAT_QA_RANGE_PCT_RESULT$,MetLine> = OrigQARangeResults<0, sPos>
                                            WOMatQARec<WO_MAT_QA_EDGE_MEAN_RESULT$,MetLine> = OrigQAEdgeResults<0, sPos>
                                            WOMatQARec<WO_MAT_QA_5MM_PCT_RESULT$,MetLine>   = OrigQA5mmResults<0, sPos>
                                            WOMatQARec<WO_MAT_QA_OUT_OF_SPEC$,MetLine>      = OrigQAOutOfSpecs<0, sPos>
                                            WOMatQARec<WO_MAT_QA_FAIL_REASON$,MetLine>      = OrigQAFailReasons<0, sPos>
                                        end
                                    Until Found
                                    Next OrigQAStage
                                end
                            end
                            MetLine += 1
                        END
                    END
                
            NEXT StageNo

        NEXT WOStepNo

        Database_Services('WriteDataRow', 'WO_MAT_QA', WONo:'*':CassNo, WOMatQARec)
        
    end else
        ErrorMsg = 'Error in ':Service:' service. Null WOMatKey passed in.'
    end
    
    If ErrorMsg NE '' then
        Error_Services('Add', ErrorMsg)
    end else
        Response = WOMatQARec
    end

end service


Service ClearSignatureByStage(WOMatQaKey, StageToClear)
    
    If ( (WOMatQAKey NE '') and (StageToClear NE '') ) then
        WOMatQaRec = Database_Services('ReadDataRow', 'WO_MAT_QA', WOMatQaKey)
        If Error_Services('NoError') then
            Stages = WOMatQaRec<WO_MAT_QA_STAGE$>
            If Stages NE '' then 
                For each Stage in Stages using @VM setting sPos
                    If Stage EQ StageToClear then
                        WOMatQaRec<WO_MAT_QA_SIG$,sPos>     = ''
                        WOMatQaRec<WO_MAT_QA_SIG_DTM$,sPos> = ''
                    end
                Next Stage
                Database_Services('WriteDataRow', 'WO_MAT_QA', WOMatQaKey, WOMatQaRec, True$, False$, True$)
                If Error_Services('HasError') then
                    Error_Services('Add', 'Error in ' :Service : ' service. Error writing WO_MAT_QA record. ' : WOMatQaKey)
                end
            end else
                Error_Services('Add', 'Error in ' :Service : ' service. WO_MAT_QA_STAGE column is null!')
            end
        end
    end else
        Error_Services('Add', 'Missing parameter in ' : service : ' service. WOOMatQaKey = ' WOMatQaKey : ', StageToClear = ' : StageToClear)
    end
    
end service


Service ClearResultsByStage(WOMatQaKey, StageToClear)
    
    If ( (WOMatQAKey NE '') and (StageToClear NE '') ) then
        WOMatQaRec = Database_Services('ReadDataRow', 'WO_MAT_QA', WOMatQaKey)
        If Error_Services('NoError') then
            Stages = WOMatQaRec<WO_MAT_QA_STAGE$>
            If Stages NE '' then 
                For each Stage in Stages using @VM setting sPos
                    If Stage EQ StageToClear then
                        WOMatQaRec<WO_MAT_QA_RESULT$, sPos>  = ''
                        WOMatQaRec<WO_MAT_QA_SIG$, sPos>     = ''
                        WOMatQaRec<WO_MAT_QA_SIG_DTM$, sPos> = ''
                    end
                Next Stage
                Database_Services('WriteDataRow', 'WO_MAT_QA', WOMatQaKey, WOMatQaRec, True$, False$, True$)
                If Error_Services('HasError') then
                    Error_Services('Add', 'Error in ' :Service : ' service. Error writing WO_MAT_QA record. ' : WOMatQaKey)
                end
            end else
                Error_Services('Add', 'Error in ' :Service : ' service. WO_MAT_QA_STAGE column is null!')
            end
        end
    end else
        Error_Services('Add', 'Missing parameter in ' : service : ' service. WOOMatQaKey = ' WOMatQaKey : ', StageToClear = ' : StageToClear)
    end
    
end service


Service AllWafersWereTested(WoMatQAKey, StageToInspect)

	if WOMatQAKey NE '' and StageToInspect NE '' then
		WOMatQaRec = Database_Services('ReadDataRow', 'WO_MAT_QA', WOMatQaKey)
		WoMatRec   = Database_Services('ReadDataRow', 'WO_MAT', WOMatQaKey)
		MUFlag     = Xlate('WO_MAT', WOMatQAKey, 'MU_BOX', 'X')
		for stageIdx = 1 to len(WOMatQaRec<WO_MAT_QA_STAGE$>)
			Stage = WoMatQaRec<WO_MAT_QA_STAGE$, stageIdx>
			if Stage _EQC StageToInspect then
			    If Not(MUFlag) then
			        //Not a MU box. Use what is in the WAFER_QTY field to ensure 25 wafers
			        WoWaferQty = WoMatRec<WO_MAT_WAFER_QTY$>
			    end else
			        //This is a MU box and probably has less than the WoWaferQty. For some reason that field is a static value =(
			        WoWaferQty = 0
			        Slots      = XLATE('WO_MAT', WOMatQAKey, 'SLOT_WAFER_ID', 'X')
			        Slot       = ''
			        for each Slot in Slots using @VM setting sPos
			           If Slot NE '' then
			               WoWaferQty = WoWaferQty + 1
			           end
			        Next Slot
			    end
				WafersWithValues = 0
				for WaferIdx = 1 to 25
					ThicknessValue = WoMatRec<WO_MAT_MU_WAFER_THK_RESULT$, WaferIdx>
					If ThicknessValue NE '' then WafersWithValues += 1
				Next WaferIdx
				if WafersWithValues GE WoWaferQty then Response = True$
			end
		Until Response EQ True$
		Next stageIdx
	end
	if Response NE True$ then Response = False$
	
end service


