Compile function Chase_Services(@Service, @Params)
#pragma precomp SRP_PreCompiler
$insert LOGICAL

$Insert RDS_EQUATES
$Insert REACTOR_EQUATES
$Insert PRS_STAGE_EQUATES
$Insert PROD_SPEC_EQUATES

equ WOCust$ to 2

Declare function Rds_Services, Database_Services, Extract_SI_Keys, SRP_Clean_Array
Declare subroutine Database_Services, Clean_Insp_Services, Extract_SI_Keys

GoToService

Return Response or ""

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

Service UpdateLoadedPsnWithPostStage()
	
	activeWOs = ''
    
    // Get all the WOs from the CONFIG table
    reactorStart = 20
    reactorEnd = 79
    For reactorNum = reactorStart to reactorEnd
    	ReactType = Xlate('REACTOR', reactorNum, REACTOR_REACT_TYPE$, 'X')
		If ReactType NE 'EPP' then
			ReactorConfigKey = 'WO_DAILY_SCHED':reactorNum
			WoNo = Field(Xlate('CONFIG', ReactorConfigKey, WOCust$, 'X'), ' ', 1)
			If WoNo NE '' then
				activeWos := WoNo
				If reactorNum < reactorEnd then activeWos := @FM
			end
		end
    Next reactorNum
    
	For WoIdx = 1 to Len(activeWOs)
		// Get the RDSs for current WO
		currentWO = activeWOs<WoIdx, 1>
		RDSColumns        = ''
		RDSColumns<0, 1>  = 'SEQ'
		RdsList = RDS_Services('GetRDSData', currentWO, RDSColumns, False$, '')
		// Loop through the RDSs
		For rdsIdx = 1 to Len(RdsList)
			// If RDS is not FQA, and is in LOAD or UNLOAD run stage, then update the PSN
			rdsNum = RdsList<rdsIdx, 1>
			FqaSigned = Rds_Services("GetFinalQAStatus", rdsNum)
			RunStatus = Xlate('REACT_RUN', rdsNum, 'RUN_STATUS', 'X')
			If (Not(FqaSigned)) and ((RunStatus _EQC 'load') or (RunStatus _EQC 'unload')) then
				PsnNo = Xlate('RDS', rdsNum, RDS_PROD_SPEC_ID$, 'X')
				specRec = Database_Services('ReadDataRow', 'PROD_SPEC', PsnNo)
				currentStages = specRec<PROD_SPEC_PRS_STAGE_KEY$>
				Swap @VM with @FM in currentStages
				// Determine if a post stage already exists
				postIdx = 0
				For stageIdx = 1 to Len(currentStages)
					currentStage = currentStages<stageIdx, 1>[-4, 4]
					If currentStage _EQC 'post' then
						postIdx = stageIdx
					end
				Until postIdx GT 0	
				Next stageIdx
				postStageRec = ''
				stageRecKey = PsnNo:'*':'POST'
				// If post stage already exists, get it
				If postIdx GT 0 then
					postStageRec = Database_Services("ReadDataRow", "PRS_STAGE", stageRecKey)
				end else
					// otherwise, add the post stage key to the spec
					currentStageKeys = specRec<PROD_SPEC_PRS_STAGE_KEY$>
					updatedStageKeys = currentStageKeys:@VM:stageRecKey
					specRec<PROD_SPEC_PRS_STAGE_KEY$> = updatedStageKeys
					Database_Services('WriteDataRow', 'PROD_SPEC', PsnNo, specRec)
				end
				// Do not override existing specs if post stage already exists
				currentSurfSigReq = postStageRec<PRS_STAGE_SURFSCAN_SIG_REQ$, 1>
				If currentSurfSigReq EQ '' then postStageRec<PRS_STAGE_SURFSCAN_SIG_REQ$, 1> = False$
				currentSurfRecipe = postStageRec<PRS_STAGE_SURFSCAN_RECIPE$, 1>
				If currentSurfRecipe EQ '' then
					// construct and apply the correct recipe
					reactorNum = Xlate('RDS', rdsNum, RDS_REACTOR$, 'X')
					waferSize = Xlate('CONFIG', 'WO_DAILY_SCHED':reactorNum, 4, 'X')[1, 1]
					surfScanRecipe = waferSize : 'INCLEAN'
					postStageRec<PRS_STAGE_SURFSCAN_RECIPE$, 1> = surfScanRecipe
				end
				// copy other surf scan specs from LWI stage
				lwiStageRecKey = PsnNo:'*':'LWI'
				lwiSurfHaze = Xlate('PRS_STAGE', lwiStageRecKey, PRS_STAGE_SURF_HAZE$, 'X')<1, 1>
				currentSurfHaze = postStageRec<PRS_STAGE_SURF_HAZE$>
				If currentSurfHaze EQ '' then postStageRec<PRS_STAGE_SURF_HAZE$> = lwiSurfHaze
				lwiSurfDefects = Xlate('PRS_STAGE', lwiStageRecKey, PRS_STAGE_SURF_DEFECTS$, 'X')<1, 1>
				currentSurfDefects = postStageRec<PRS_STAGE_SURF_DEFECTS$>
				If currentSurfDefects EQ '' then postStageRec<PRS_STAGE_SURF_DEFECTS$> = lwiSurfDefects
				lwiSampleQty = Xlate('PRS_STAGE', lwiStageRecKey, PRS_STAGE_SS_SAMP_QTY$, 'X')<1, 1>
				currentSampleQty = postStageRec<PRS_STAGE_SS_SAMP_QTY$>
				If currentSampleQty EQ '' then postStageRec<PRS_STAGE_SS_SAMP_QTY$> = lwiSampleQty
				Database_Services('WriteDataRow', 'PRS_STAGE', stageRecKey, postStageRec)
				
				// push out the spec changes
				runOrderNum = Xlate('RDS', rdsNum, 'RUN_ORDER_NUM', 'X')
				woMatKey = currentWO : '*' : runOrderNum
				Clean_Insp_Services('UpdateAllCleanInsp', woMatKey)
			end
		Next rdsIdx
	Next idx
    
End Service


Service AddAdHocPostCleanToPsnsByCustomerId(custId, cleansRecipe, TencorRecipe, sd, sh)
	ProdVerKeys = ''
    Extract_SI_Keys('PROD_VER', 'CUST_NO', custId, ProdVerKeys)
    If ProdVerKeys NE '' then
        PSNs = Xlate('PROD_VER', ProdVerKeys, 'PSN_ID', 'X')
        UniquePsns = SRP_Clean_Array(PSNs, @VM, 'UNIQUE')
        For each PsnNo in UniquePsns using @VM setting Idx
        	PsnStatus = Xlate('PROD_SPEC', PsnNo, PROD_SPEC_STATUS$, 'X')
        	If PsnStatus EQ 'A' then
        		postStageRecKey = PsnNo:'*':'POST'
				postStageRec = Database_Services("ReadDataRow", "PRS_STAGE", postStageRecKey)
				debug
*				postCleanTool = 'AKRION1'
*				currentPostCleanTool = postStageRec<PRS_STAGE_CLEAN_TOOL$>
*				If currentPostCleanTool EQ '' then postStageRec<PRS_STAGE_CLEAN_TOOL$> = postCleanTool
				
				postCleansRecipe = cleansRecipe
				currentPostCleanRecipe = postStageRec<PRS_STAGE_CLEAN_RECIPE$>
				If currentPostCleanRecipe EQ '' then postStageRec<PRS_STAGE_CLEAN_RECIPE$> = postCleansRecipe
				
				postSurfScanRecipe = TencorRecipe
				currentPostSurfScanRecipe = postStageRec<PRS_STAGE_SURFSCAN_RECIPE$>
				If currentPostSurfScanRecipe EQ '' then postStageRec<PRS_STAGE_SURFSCAN_RECIPE$> = postSurfScanRecipe
				
				surfHaze = sh
				currentPostSurfHaze = postStageRec<PRS_STAGE_SURF_HAZE$>
				If currentPostSurfHaze EQ '' then postStageRec<PRS_STAGE_SURF_HAZE$> = surfHaze
				
				surfDefects = sd
				currentPostSurfDefects = postStageRec<PRS_STAGE_SURF_DEFECTS$><1, 1>
				If currentPostSurfDefects EQ '' then postStageRec<PRS_STAGE_SURF_DEFECTS$> = surfDefects
				
				Database_Services('WriteDataRow', 'PRS_STAGE', postStageRecKey, postStageRec)
        	end
        Next PsnNo
    end
end service




