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
$Insert RDS_EQUATES
$Insert WO_REACT_EQUATES

Declare function   Database_Services, SRP_JSON, Error_Services, obj_Prod_Spec, Wo_Mat_Qa_Services
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)
        RDSNo              = Database_Services('ReadDataColumn', 'WO_MAT', WOMatKey, WO_MAT_RDS_NO$)
        If Error_Services('NoError') then 
			If (RDSNo NE '') then
				// Check if it has been assigned to a reactor
				ReactNo = Database_Services('ReadDataColumn', 'RDS', RDSNo, RDS_REACTOR$)
				If Error_Services('HasError') then
					ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
				end
			end else
				// Either not signed into a reactor yet or this WO_MAT is EpiPro and therefore there is no
				// 1-to-1 RDS record. Because of this, reactor scheduled QA met is not possible with EpiPro.
				ReactNo = ''
			end
			If (ErrorMsg EQ '') then 
				WOStepKeys = Database_Services('ReadDataColumn', 'WO_LOG', WONo, WO_LOG_WO_STEP_KEY$)
				If Error_Services('NoError') then 
					StepCnt            = DCount(WOStepKeys,@VM)
					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$>
					
					FOR WOStepNo = 1 TO StepCnt
						PSNo = Database_Services('ReadDataColumn', 'WO_STEP', WOStepKeys<1,WOStepNo>, WO_STEP_PROD_SPEC_ID$)
						If Error_Services('NoError') then
							PSRec     = Database_Services('ReadDataRow', 'PROD_SPEC', PSNo) 
							If Error_Services('NoError') then 
								QAMetData = obj_Prod_Spec('GetQAMet',PSNo:@RM:PSRec)
								QAStages  = QAMetData<COL$QA_MET_STAGE>
								StageCnt  = DCount(QAStages,@VM)
								
								If (ReactNo NE '') then 
									WOReactKey = WONo:'*':WOStepNo:'*':ReactNo
								end else
									WOReactKey = ''
								end
								
								If (WOReactKey NE '') then
									WOReactRDSNos = Database_Services('ReadDataColumn', 'WO_REACT', WOReactKey, WO_REACT_RDS_NO$)
									If Error_Services('HasError') then 
										ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
									end
								end
								
								If (ErrorMsg EQ '') then 
									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   = False$
										
										If ( (Interval NE '') and (Start NE '') ) then
											// Add Work Order scheduled tests (i.e., not reactor scheduled tests)
											If Not(ReactSched) then 
												If Interval = Start then
													If ( Rem(CassNo, Interval) EQ 0 ) then TestFlag = True$
												end else
													If ( Abs( (Start + Int(CassNo / Interval)*Interval) - CassNo ) EQ 0) then TestFlag = True$
												end
											end else
												Locate RDSNo in WOReactRDSNos using @VM setting TestSeq then 
													If ( Rem( (TestSeq - Start), Interval) EQ 0 ) then TestFlag = True$
												end
											end
											If TestFlag then
												WOMatQARec<WO_MAT_QA_PROFILE$,MetLine>        = WOStepNo:MetTest
												WOMatQARec<WO_MAT_QA_PROP$,MetLine>           = QAMetData<COL$QA_MET_PROP,StageNo>				
												WOMatQARec<WO_MAT_QA_TOOL_CLASS$,MetLine>     = QAMetData<COL$QA_MET_TOOL_CLASS,StageNo>			
												WOMatQARec<WO_MAT_QA_STAGE$,MetLine>          = Stage
												WOMatQARec<WO_MAT_QA_MIN$,MetLine>            = QAMetData<COL$QA_MET_MIN,StageNo>					
												WOMatQARec<WO_MAT_QA_MAX$,MetLine>            = QAMetData<COL$QA_MET_MAX,StageNo>					
												WOMatQARec<WO_MAT_QA_SLOT$,MetLine>           = QAMetData<COL$QA_MET_SLOT,StageNo>				
												WOMatQARec<WO_MAT_QA_RECIPE$,MetLine>         = QAMetData<COL$QA_MET_RECIPE,StageNo>				
												WOMatQARec<WO_MAT_QA_RECIPE_PATTERN$,MetLine> = QAMetData<COL$QA_MET_RECIPE_PATTERN,StageNo>		
												WOMatQARec<WO_MAT_QA_WFR_QTY$,MetLine>        = QAMetData<COL$QA_MET_WFR_QTY,StageNo>				
												WOMatQARec<WO_MAT_QA_WFR_TYPE$,MetLine>       = QAMetData<COL$QA_MET_WFR_TYPE,StageNo>			
												WOMatQARec<WO_MAT_QA_REACT_SCHED$,MetLine>    = ReactSched										
												WOMatQARec<WO_MAT_QA_SHIP_DOC$,MetLine>       = QAMetData<COL$QA_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:MetTest
													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
								end
							end else
								ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
							end
						end else
							ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
						end
					Until (ErrorMsg NE '')
					Next WOStepNo
					If (ErrorMsg EQ '') then 
						Database_Services('WriteDataRow', 'WO_MAT_QA', WONo:'*':CassNo, WOMatQARec)
						If Error_Services('HasError') then ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
					end
				end else
					ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
				end
			end
		end else
			ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
		end
    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


Service GetQAMetComplete(WOMatKey)

	ErrorMsg = ''
	QAMetComplete = True$
	
    If WOMatKey NE '' then
        // Lack of WO_MAT_QA record may not be an error here. Gen5 Products do not have WO_MAT_QA Records
        If Rowexists('WO_MAT_QA', WOMatKey) then
            QAMetRec   = Database_Services('ReadDataRow', 'WO_MAT_QA', WOMatKey, '', True$, 0, False$)
            If Error_Services('NoError') then
                Stages     = QAMetRec<WO_MAT_QA_STAGE$>
                Results    = QAMetRec<WO_MAT_QA_RESULT$>
                StageCount = DCount(Stages, @VM)
                QAStagePresent = False$
                For I = 1 to StageCount
                    If Stages<1, I> EQ 'QA' then QAStagePresent = True$
                    If Results<1,I> EQ '' then
                        QAMetComplete = False$
                    end
                Until QAMetComplete EQ False$
                Next I
                If QAMetComplete EQ True$ AND QAStagePresent = True$ then
                    QAMUMetComplete = WO_Mat_QA_Services('GetMUWaferQAComplete', WOMatKey)
                    If Error_Services('NoError') then
                        If Not(QAMUMetComplete) then
                            QAMetComplete = False$
                        end
                    end else
                        ErrorMsg = Error_Services('GetMessage')
                    end
                end
            end else
                ErrorMsg = Error_Services('GetMessage')
            end
        end
    end else
        ErrorMsg = 'Missing WOMatKey parameter.'
    end
    
    If ErrorMsg EQ '' then
        Response = QAMetComplete
    end else
        Response = False$
        Error_Services('Add', ErrorMsg)
    end
    
end service


Service GetMUWaferQAComplete(WOMatKey)

    ErrorMsg = ''
    
	If WOMatKey NE '' then
        Response = True$
        WOMatRec = Database_Services('ReadDataRow', 'WO_MAT', WoMatKey, True$, 0, False$)
        If Error_Services('NoError') then
            MUBoxes = ''
            MUBoxesResults = ''
            MUWafers = WOMatRec<WO_MAT_SLOT_MOVED_FROM$>
            Counter = 0
            
            If Count(MUWafers, @VM) GT 0 then
                for each BoxNumber in MUWafers using @VM setting OrigIndex
                    if BoxNumber NE '' then
                        MUCassId = FIELD(BoxNumber,'.',1,2)
                        Locate MUCassId in MUBoxesResults using @VM setting BoxIndex then
                            MUThkResult = WOMatRec<WO_MAT_MU_WAFER_THK_RESULT$, OrigIndex>
                            If MUThkResult NE '' then
                                MUBoxesResults<BoxIndex, 2> = True$
                            end
                        end else
                            Counter += 1
                            MUBoxesResults<Counter> = MUCassID
                            MUThkResult = WOMatRec<WO_MAT_MU_WAFER_THK_RESULT$, OrigIndex>
                            If MUThkResult NE '' then
                                MUBoxesResults<Counter, 2> = True$
                            end
                        end
                    end
                Next BoxNumber
                
                for each Box in MUBoxesResults using @FM
                    if Box<1,2> EQ '' then
                        Response = False$
                    end
                Until Response = False$
                Next Box
            end
        end else
            ErrorMsg = 'Failed to open WO_MAT record.'
        end
	end else
	    ErrorMsg = 'Missing WOMatKey parameter.'
	end
	
	If ErrorMsg NE '' then
	    Error_Services('Add', ErrorMsg)
	end
	
end service

