Function Reactor_Log_Actions(Action, CalcColName, FSList, Handle, Name, FMC, Record, Status, OrigRecord, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from Infineon.

    Name        :   Reactor_Log_Actions

    Description :   Handles calculated columns and MFS calls for the current table.

    Notes       :   This function uses @ID, @RECORD, and @DICT to make sure {ColumnName} references work correctly.
                    If called from outside of a calculated column these will need to be set and restored.

    Parameters  :
        Action          [in] -- Name of the action to be taken
        CalcColName     [in] -- Name of the calculated column that needs to be processed. Normally this should only be
                                populated when the CalcField action is being used.
        FSList          [in] -- The list of MFSs and the BFS name for the current file or volume. This is an @SVM
                                delimited array, with the current MFS name as the first value in the array, and the BFS
                                name as the last value. Normally set by a calling MFS.
        Handle          [in] -- The file handle of the file or media map being accessed. Note, this does contain the
                                entire handle structure that the Basic+ Open statement would provide. Normally set by a
                                calling MFS.
        Name            [in] -- The name (key) of the record or file being accessed. Normally set by a calling MFS.
        FMC             [in] -- Various functions. Normally set by a calling MFS.
        Record          [in] -- The entire record (for record-oriented functions) or a newly-created handle (for
                                "get handle" functions). Normally set by a calling MFS.
        Status      [in/out] -- Indicator of the success or failure of an action. Normally set by the calling MFS but
                                for some actions can be set by the action handler to indicate failure.
        OrigRecord      [in] -- Original content of the record being processed by the current action. This is
                                automatically being assigned by the WRITE_RECORD and DELETE_RECORD actions within
                                BASE_MFS.
        Param1-10   [in/out] -- Additional request parameter holders
        ActionFlow     [out] -- Used to control the action chain (see the ACTION_SETUP insert for more information.)
                                Can also be used to return a special value, such as the results of the CalcField
                                method.

    History     :   (Date, Initials, Notes)
        02/14/19    djs     Original programmer.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler

$Insert LOGICAL
$Insert FILE.SYSTEM.EQUATES
$Insert ACTION_SETUP
$Insert RDS_EQUATES
$Insert CLEAN_INSP_EQUATES
$Insert COMPANY_EQUATES
$Insert RDS_LAYER_EQUATES
$Insert TOOL_PARMS_EQUATES
$Insert RLIST_EQUATES
$Insert REACTOR_LOG_EQUATES
$Insert REACTOR_EQUATES
$Insert WO_MAT_EQUATES
$Insert WO_MAT_QA_EQUATES

equ WOCust$ to 2

Declare function    Error_Services, Database_Services, Logging_Services, Environment_Services
Declare subroutine  Error_Services, Database_Services, Logging_Services, Set_Status, Reactor_Log_Services

If KeyID then GoSub Initialize_System_Variables

Begin Case

    Case Action _EQC 'CalculateColumn'      ;   GoSub CalculateColumn
    Case Action _EQC 'READ_RECORD_PRE'      ;   GoSub READ_RECORD_PRE
    Case Action _EQC 'READ_RECORD'          ;   GoSub READ_RECORD
    Case Action _EQC 'READONLY_RECORD_PRE'  ;   GoSub READONLY_RECORD_PRE
    Case Action _EQC 'READONLY_RECORD'      ;   GoSub READONLY_RECORD
    Case Action _EQC 'WRITE_RECORD_PRE'     ;   GoSub WRITE_RECORD_PRE
    Case Action _EQC 'WRITE_RECORD'         ;   GoSub WRITE_RECORD
    Case Action _EQC 'DELETE_RECORD_PRE'    ;   GoSub DELETE_RECORD_PRE
    Case Action _EQC 'DELETE_RECORD'        ;   GoSub DELETE_RECORD
    Case Otherwise$                         ;   Status = 'Invalid Action'

End Case

If KeyID then GoSub Restore_System_Variables

If Assigned(ActionFlow) else ActionFlow = ACTION_CONTINUE$

Return ActionFlow


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculated Columns 
//
// The typical structure of a calculated column will look like this:
//
// Declare function Database_Services
//
// @ANS = Database_Services('CalculateColumn')
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CalculateColumn:

    // Make sure the ActionFlow return variable is cleared in case nothing is calculated.
    ActionFlow = ''

*    Begin Case
*        Case CalcColName EQ 'ROTR_ACTION'       ; GoSub ROTR_ACTION
*    End Case
    
return


// ----- MFS calls -----------------------------------------------------------------------------------------------------

READ_RECORD_PRE:
    // In order to stop a record from being read in this action these lines of code must be used:
    //
    //    OrigFileError   = 100 : @FM : KeyID
    //    Status          = 0
    //    Record          = ''
    //    ActionFlow      = ACTION_STOP$
return

READ_RECORD:
    // In order to stop a record from being read in this action these lines of code must be used:
    //
    //    OrigFileError   = 100 : @FM : KeyID
    //    Status          = 0
    //    Record          = ''
return

READONLY_RECORD_PRE:
    // In order to stop a record from being read in this action these lines of code must be used:
    //
    //    OrigFileError   = 100 : @FM : KeyID
    //    Status          = 0
    //    Record          = ''
    //    ActionFlow      = ACTION_STOP$
return

READONLY_RECORD:
    // In order to stop a record from being read in this action these lines of code must be used:
    //
    //    OrigFileError   = 100 : @FM : KeyID
    //    Status          = 0
    //    Record          = ''
return

WRITE_RECORD_PRE:
    
    OrigROTRFlag = OrigRecord<REACTOR_LOG_ROTR$>
    NewROTRFlag  = Record<REACTOR_LOG_ROTR$>
    
    ReactNo = Record<REACTOR_LOG_REACTOR$>
    StopRDS = Xlate('REACT_STATE', ReactNo, 'LAST_RDS_NO', 'X')
    
    OrigWafersRemoved = OrigRecord<REACTOR_LOG_WAFERS_REMOVED$>
    NewWafersRemoved = Record<REACTOR_LOG_WAFERS_REMOVED$>
    
    If (Not(OrigROTRFlag) and NewROTRFlag EQ True$) or (Not(OrigWafersRemoved) and NewWafersRemoved) then
        If ReactNo NE '' then
            ReactRec = Database_Services('ReadDataRow', 'REACTOR', ReactNo)
            ReactRec<REACTOR_ROTR_STOP_RDS$> = StopRDS
            Database_Services('WriteDataRow', 'REACTOR', ReactNo, ReactRec, True$, False$, True$)
        end
    end
    
    RdsKeys = Record<REACTOR_LOG_WAFERS_REMOVED_RDS$>
    Swap @VM with @FM in RdsKeys
    
    If (Not(OrigWafersRemoved)) and (NewWafersRemoved EQ True$) and (ReactNo NE '') and (RdsKeys NE '') then
		ReactorConfigKey = 'WO_DAILY_SCHED':ReactNo
		WoNo = Field(Xlate('CONFIG', ReactorConfigKey, WOCust$, 'X'), ' ', 1)
		
		CurrentRds = ''
		
		For idx = 1 to Len(RdsKeys)
			CurrentRds = RdsKeys<idx>
			// Set a flag on the CI record indicating the run was aborted
			CleanInspKey   = Xlate('RDS', CurrentRds, 'LWI_CI_NO', 'X')
			CIRec = Database_Services('ReadDataRow', 'CLEAN_INSP', CleanInspKey)
			CIRec<CLEAN_INSP_WAFERS_REMOVED$> = True$
			Database_Services('WriteDataRow', 'CLEAN_INSP', CleanInspKey, CIRec, True$, False$, False$)
			// Change the QA metrology requirements to all wafers
			CassNo = Xlate('RDS', CurrentRds, 'CASS_NO', 'X')
			WoMatQaKey = WoNo : '*' : CassNo
			WoMatQaRec = Xlate('WO_MAT_QA', WoMatQaKey, '', 'X')
			WoWaferQty = Xlate('WO_MAT', WoMatQaKey, WO_MAT_WAFER_QTY$, 'X')
			WoMatQaStages = WoMatQaRec<WO_MAT_QA_STAGE$>
			Swap @VM with @FM in WoMatQaStages
			WoMatQaProfiles = WoMatQaRec<WO_MAT_QA_PROFILE$>
			Swap @VM with @FM in WoMatQaProfiles
			WoMatQaSlots = WoMatQaRec<WO_MAT_QA_SLOT$>
			Swap @VM with @FM in WoMatQaSlots
			WoMatQaWfrQtys = WoMatQaRec<WO_MAT_QA_WFR_QTY$>
			Swap @VM with @FM in WoMatQaWfrQtys
			WoMatQaProps = WoMatQaRec<WO_MAT_QA_PROP$>
			Swap @VM with @FM in WoMatQaProps
			WoMatQaToolClasses = WoMatQaRec<WO_MAT_QA_TOOL_CLASS$>
			Swap @VM with @FM in WoMatQaToolClasses
			WoMatQaRecipes = WoMatQaRec<WO_MAT_QA_RECIPE$>
			Swap @VM with @FM in WoMatQaRecipes
			WoMatQaRecipePatterns = WoMatQaRec<WO_MAT_QA_RECIPE_PATTERN$>
			Swap @VM with @FM in WoMatQaRecipePatterns
			WoMatQaMin = WoMatQaRec<WO_MAT_QA_MIN$>
			Swap @VM with @FM in WoMatQaMin
			WoMatQaMax = WoMatQaRec<WO_MAT_QA_MAX$>
			Swap @VM with @FM in WoMatQaMax
			WoMatQaOOS = WoMatQaRec<WO_MAT_QA_OUT_OF_SPEC$>
			Swap @VM with @FM in WoMatQaOOS
			StageCount = DCount(WoMatQaStages, @FM)
			For stageIdx = 1 to StageCount
				If (WoMatQaStages<stageIdx> _EQC 'QA') and (WoMatQaProfiles<stageIdx> _EQC '1ADE') then
					Slot = WoMatQaSlots<stageIdx>
					If (Slot _NEC 'L') then
						WoMatQaSlots<stageIdx> = 'A'
						WoMatQaWfrQtys<stageIdx> = WoWaferQty
					end
					If (Slot _EQC 'L') then
						WoMatQaStages<stageIdx> = ''
						WoMatQaProfiles<stageIdx> = ''
						WoMatQaSlots<stageIdx> = ''
						WoMatQaWfrQtys<stageIdx> = ''
						WoMatQaProps<stageIdx> = ''
						WoMatQaToolClasses<stageIdx> = ''
						WoMatQaRecipes<stageIdx> = ''
						WoMatQaRecipePatterns<stageIdx> = ''
						WoMatQaMin<stageIdx> = ''
						WoMatQaMax<stageIdx> = ''
						WoMatQaOOS<stageIdx> = ''
					end
				end
			Next stageIdx
			Swap @FM with @VM in WoMatQaStages
			WoMatQaRec<WO_MAT_QA_STAGE$> = WoMatQaStages
			Swap @FM with @VM in WoMatQaProfiles
			WoMatQaRec<WO_MAT_QA_PROFILE$> = WoMatQaProfiles
			Swap @FM with @VM in WoMatQaSlots
			WoMatQaRec<WO_MAT_QA_SLOT$> = WoMatQaSlots
			Swap @FM with @VM in WoMatQaWfrQtys
			WoMatQaRec<WO_MAT_QA_WFR_QTY$> = WoMatQaWfrQtys
			Swap @FM with @VM in WoMatQaProps
			WoMatQaRec<WO_MAT_QA_PROP$> = WoMatQaProps
			Swap @FM with @VM in WoMatQaToolClasses
			WoMatQaRec<WO_MAT_QA_TOOL_CLASS$> = WoMatQaToolClasses
			Swap @FM with @VM in WoMatQaRecipes
			WoMatQaRec<WO_MAT_QA_RECIPE$> = WoMatQaRecipes
			Swap @FM with @VM in WoMatQaRecipePatterns
			WoMatQaRec<WO_MAT_QA_RECIPE_PATTERN$> = WoMatQaRecipePatterns
			Swap @FM with @VM in WoMatQaMin
			WoMatQaRec<WO_MAT_QA_MIN$> = WoMatQaMin
			Swap @FM with @VM in WoMatQaMax
			WoMatQaRec<WO_MAT_QA_MAX$> = WoMatQaMax
			Swap @FM with @VM in WoMatQaOOS
			WoMatQaRec<WO_MAT_QA_OUT_OF_SPEC$> = WoMatQaOOS
			Database_Services('WriteDataRow', 'WO_MAT_QA', WoMatQaKey, WoMatQaRec, True$, False$, False$)
		Next idx
    end
    
return

WRITE_RECORD:

	OrigServIds = OrigRecord<REACTOR_LOG_REACT_SERV_ID$>
	NewServIds  = Record<REACTOR_LOG_REACT_SERV_ID$>
	If OrigServIds NE NewServIds then
		// Check if particular service IDs were added or removed.
		// ID 240  - ASM Tube Change
		// ID 1280 - HTR Tube Change
		// ID 1275 - PM - Semi Annual
		// ID 1260 - PM - Annual
		ReactType = {REACT_TYPE}
		
		AsmTubeChangeAdded   = ( (Index(OrigServIds,  240, 1) EQ 0) and (Index(NewServIds,  240, 1) GT 0) )
		AsmTubeChangeRemoved = ( (Index(OrigServIds,  240, 1) GT 0) and (Index(NewServIds,  240, 1) EQ 0) )
		HtrTubeChangeAdded   = ( (Index(OrigServIds, 1280, 1) EQ 0) and (Index(NewServIds, 1280, 1) GT 0) )
		HtrTubeChangeRemoved = ( (Index(OrigServIds, 1280, 1) GT 0) and (Index(NewServIds, 1280, 1) EQ 0) ) 
		SemiannualAdded      = ( (Index(OrigServIds, 1275, 1) EQ 0) and (Index(NewServIds, 1275, 1) GT 0) )
		SemiannualRemoved    = ( (Index(OrigServIds, 1275, 1) GT 0) and (Index(NewServIds, 1275, 1) EQ 0) )
		AnnualAdded          = ( (Index(OrigServIds, 1260, 1) EQ 0) and (Index(NewServIds, 1260, 1) GT 0) )
		AnnualRemoved        = ( (Index(OrigServIds, 1260, 1) GT 0) and (Index(NewServIds, 1260, 1) EQ 0) )
		FiveYearAdded        = ( (Index(OrigServIds, 1264, 1) EQ 0) and (Index(NewServIds, 1264, 1) GT 0) )
		FiveYearRemoved      = ( (Index(OrigServIds, 1264, 1) GT 0) and (Index(NewServIds, 1264, 1) EQ 0) )
		TenYearAdded         = ( (Index(OrigServIds, 1265, 1) EQ 0) and (Index(NewServIds, 1265, 1) GT 0) )
		TenYearRemoved       = ( (Index(OrigServIds, 1265, 1) GT 0) and (Index(NewServIds, 1265, 1) EQ 0) )
		
		If (FiveYearAdded or TenYearAdded) then
			// Create NICA order
			If ( (ReactType EQ 'ASM') or (ReactType EQ 'ASM+') or (ReactType EQ 'HTR') ) then
				Reactor_Log_Services('CreateReactorLogPMOrder', Name, 'ASM_HTR_FIVE_AND_TEN_YEAR_PM')
			end			
		end
		
		If (FiveYearRemoved or TenYearRemoved) then
			// Cancel NICA order
			If ( (ReactType EQ 'ASM') or (ReactType EQ 'ASM+') or (ReactType EQ 'HTR') ) then
				ChecklistTypes    = Record<REACTOR_LOG_CHECKLIST_TYPE$>
				ChecklistOrderIds = Record<REACTOR_LOG_CHECKLIST_ORDER_ID$>
				Locate 'ASM_HTR_FIVE_AND_TEN_YEAR_PM' in ChecklistTypes using @VM setting vPos then
					OrderId = ChecklistOrderIds<0, vPos>
					Reactor_Log_Services('CancelReactorLogPMOrder', Name, OrderId)
				end
			end			
		end			
		
		If (AsmTubeChangeAdded or HtrTubeChangeAdded) then
			// Create NICA order
			If ( (ReactType EQ 'ASM') or (ReactType EQ 'ASM+') or (ReactType EQ 'HTR') ) then
				Reactor_Log_Services('CreateReactorLogPMOrder', Name, 'ASM_HTR_TUBE_CHANGE')
			end
		end
		
		If (AsmTubeChangeRemoved or HtrTubeChangeRemoved) then
			// Cancel NICA order
			If ( (ReactType EQ 'ASM') or (ReactType EQ 'ASM+') or (ReactType EQ 'HTR') ) then
				ChecklistTypes    = Record<REACTOR_LOG_CHECKLIST_TYPE$>
				ChecklistOrderIds = Record<REACTOR_LOG_CHECKLIST_ORDER_ID$>
				Locate 'ASM_HTR_TUBE_CHANGE' in ChecklistTypes using @VM setting vPos then
					OrderId = ChecklistOrderIds<0, vPos>
					Reactor_Log_Services('CancelReactorLogPMOrder', Name, OrderId)
				end
			end			
		end		

		If SemiannualAdded then
			// Create NICA order
			If ( (ReactType EQ 'ASM') or (ReactType EQ 'ASM+') or (ReactType EQ 'HTR') ) then
				Reactor_Log_Services('CreateReactorLogPMOrder', Name, 'ASM_HTR_SEMIANNUAL_PM')
			end
		end
		
		If SemiannualRemoved then
			// Cancel NICA order
			If ( (ReactType EQ 'ASM') or (ReactType EQ 'ASM+') or (ReactType EQ 'HTR') ) then
				ChecklistTypes    = Record<REACTOR_LOG_CHECKLIST_TYPE$>
				ChecklistOrderIds = Record<REACTOR_LOG_CHECKLIST_ORDER_ID$>
				Locate 'ASM_HTR_SEMIANNUAL_PM' in ChecklistTypes using @VM setting vPos then
					OrderId = ChecklistOrderIds<0, vPos>
					Reactor_Log_Services('CancelReactorLogPMOrder', Name, OrderId)
				end
			end
		end
		
		If AnnualAdded then
			// Create NICA order
			If ( (ReactType EQ 'ASM') or (ReactType EQ 'ASM+') or (ReactType EQ 'HTR') ) then
				Reactor_Log_Services('CreateReactorLogPMOrder', Name, 'ASM_HTR_ANNUAL_PM')
			end
		end
		
		If AnnualRemoved then
			// Cancel NICA order
			If ( (ReactType EQ 'ASM') or (ReactType EQ 'ASM+') or (ReactType EQ 'HTR') ) then
				ChecklistTypes    = Record<REACTOR_LOG_CHECKLIST_TYPE$>
				ChecklistOrderIds = Record<REACTOR_LOG_CHECKLIST_ORDER_ID$>
				Locate 'ASM_HTR_ANNUAL_PM' in ChecklistTypes using @VM setting vPos then
					OrderId = ChecklistOrderIds<0, vPos>
					Reactor_Log_Services('CancelReactorLogPMOrder', Name, OrderId)
				end
			end
		end
	end
	
return

DELETE_RECORD_PRE:
return

DELETE_RECORD:
return



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ClearCursors:
    For counter = 0 to 8
        ClearSelect counter
    Next counter
return


Initialize_System_Variables:

    // Save these for restoration later
    SaveDict        = @DICT
    SaveID          = @ID
    SaveRecord      = @RECORD
    OrigFileError   = @FILE.ERROR

    // Now make sure @DICT, ID, and @RECORD are populated
    CurrentDictName = ''
    If @DICT then
        DictHandle = @DICT<1, 2>
        Locate DictHandle in @TABLES(5) Using @FM Setting fPos then
            CurrentDictName = Field(@TABLES(0), @FM, fPos, 1)
        end
    end

    If CurrentDictName NE DictName then
         Open DictName to @DICT else Status = 'Unable to initialize @DICT'
    end    

    @ID = KeyID
    If Record else
        // Record might not have been passed in. Read the record from the database table just to make sure.
        @FILE.ERROR     = ''
        Open TableName to hTable then
            FullFSList  = hTable[1, 'F' : @VM]
            BFS         = FullFSList[-1, 'B' : @SVM]
            LastHandle  = hTable[-1, 'B' : \0D\]
            FileHandle  = \0D\ : LastHandle[1, @VM]

            Call @BFS(READO.RECORD, BFS, FileHandle, KeyID, FMC, Record, ReadOStatus)
        end
    end
    @RECORD = Record

return


Restore_System_Variables:

    Transfer SaveDict   to @DICT
    Transfer SaveID     to @ID
    Transfer SaveRecord to @RECORD
    @FILE.ERROR = OrigFileError

return












