Function Reactor_Performance_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 SRP Computer Solutions, Inc.

    Name        :   Table_Actions (<Table> should be replaced with the actual Table.)

    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)
        10/31/17    dmb     Original programmer.

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

#pragma precomp SRP_PreCompiler

$insert LOGICAL
$insert FILE.SYSTEM.EQUATES
$insert ACTION_SETUP
$insert REACTOR_EQUATES

Declare function    SRP_Math, Database_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

Return ActionFlow OR ACTION_CONTINUE$


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 'DESCRIPTION'                   ; GoSub DESCRIPTION
        Case CalcColName EQ 'PARENT_KEY_ID'                 ; GoSub PARENT_KEY_ID
        Case CalcColName EQ 'ALL_DETAIL_CHILD_KEY_IDS'      ; GoSub ALL_DETAIL_CHILD_KEY_IDS
        Case CalcColName EQ 'TOTAL_WAFERS_EXPECTED'         ; GoSub TOTAL_WAFERS_EXPECTED
        Case CalcColName EQ 'TOTAL_WAFERS_EXPECTED_ROUNDED' ; GoSub TOTAL_WAFERS_EXPECTED_ROUNDED
        Case CalcColName EQ 'TOTAL_WAFERS_PROCESSED'        ; GoSub TOTAL_WAFERS_PROCESSED
        Case CalcColName EQ 'TOTAL_WAFERS_DELTA'            ; GoSub TOTAL_WAFERS_DELTA
        Case CalcColName EQ 'TOTAL_WAFERS_DELTA_ROUNDED'    ; GoSub TOTAL_WAFERS_DELTA_ROUNDED
        Case CalcColName EQ 'REACTOR_NAME'                  ; GoSub REACTOR_NAME
    End Case

return


DESCRIPTION:

    ActionFlow  = ''    ; // Default description.
    // Full KeyID = {REACTOR_NO} * {EPI_PART_NO} * {WEEK_YEAR} * {WEEK_NO} * {SCHEDULE_DATE}
    Begin Case
        Case {REACTOR_NO} NE ''
            // Reactor No is populated.
            Begin Case
                Case {SCHEDULE_DATE} NE '' AND {EPI_PART_NO} EQ ''
                    // This is the Schedule Date aggregate Key ID for one Reactor No.
                    ActionFlow  = 'Aggregate for the Reactor/Date'
                Case {SCHEDULE_DATE} NE ''
                    // This is the Schedule Date aggregate Key ID. Aggregate to the Week No (same Reactor No, Epi Part No,
                    // Week Year, and Week No) with and without a Reactor No.
                    ActionFlow  = 'Aggregate for the Reactor/Part/Year/Week/Date'
                Case {WEEK_NO} NE ''
                    // This is the Week No aggregate Key ID. Aggregate to the Week Year (same Reactor No and Epi Part No).
                    ActionFlow  = 'Aggregate for the Reactor/Part/Year/Week'
                Case {WEEK_YEAR} NE ''
                    // This is the Week Year aggregate Key ID. Aggregate to the Epi Part No (same Reactor No).
                    ActionFlow  = 'Aggregate for the Reactor/Part/Year'
                Case {EPI_PART_NO} NE ''
                    // This is the Epi Part No on the same Reactor No aggregate Key ID. Aggregate to the Epi Part No and the Reactor No (two different Key IDs).
                    ActionFlow  = 'Aggregate for the Reactor/Part'
            End Case

        Case Otherwise$
            // Reactor No is empty.
            Begin Case
                Case {WEEK_NO} NE ''
                    // This is the Week No aggregate Key ID. Aggregate to the Week Year (same Epi Part No).
                    ActionFlow  = 'Aggregate for the Part/Year/Week'
                Case {WEEK_YEAR} NE ''
                    // This is the Week Year aggregate Key ID. Aggregate to the Epi Part No (same Reactor No).
                    ActionFlow  = 'Aggregate for the Part/Year'
                Case {EPI_PART_NO} NE ''
                    // This is the Epi Part No on the same Reactor No aggregate Key ID. Aggregate to the Epi Part No and the Reactor No (two different Key IDs).
                    ActionFlow  = 'Aggregate for the Part'
            End Case

    End Case

return


PARENT_KEY_ID:

    ActionFlow  = ''    ; // Default response if the current Key ID should not be aggregated.
    // Full KeyID = {REACTOR_NO} * {EPI_PART_NO} * {WEEK_YEAR} * {WEEK_NO} * {SCHEDULE_DATE}
    Begin Case
        Case {REACTOR_NO} NE ''
            // Reactor No is populated.
            Begin Case
                Case {EPI_PART_NO} NE '' AND {SCHEDULE_DATE} NE ''
                    // This is the Epi Part No and Schedule Date aggregate Key ID. Aggregate to the Week No (same
                    // Reactor No, Epi Part No, Week Yea`r, and Week No) with and without a Reactor No. Also create an
                    // aggregate to the same Reactor No and Schedule Date for the performance reports.
                    ActionFlow  = {REACTOR_NO} : '*' : {EPI_PART_NO} : '*' : {WEEK_YEAR} : '*' : {WEEK_NO} : '*' : @VM
                    ActionFlow := {REACTOR_NO} : '**' : {WEEK_YEAR} : '*' : {WEEK_NO} : '*' : @VM
                    ActionFlow := '*' : {EPI_PART_NO} : '*' : {WEEK_YEAR} : '*' : {WEEK_NO} : '*' : @VM
                    ActionFlow := {REACTOR_NO} : '****' : {SCHEDULE_DATE}
                Case {EPI_PART_NO} NE '' AND {WEEK_NO} NE ''
                    // This is the Week No aggregate Key ID. Aggregate to the Week Year (same Reactor No and Epi Part No).
                    ActionFlow  = {REACTOR_NO} : '*' : {EPI_PART_NO} : '*' : {WEEK_YEAR} : '**'
                Case {EPI_PART_NO} NE '' AND {WEEK_YEAR} NE ''
                    // This is the Week Year aggregate Key ID. Aggregate to the Epi Part No (same Reactor No).
                    ActionFlow  = {REACTOR_NO} : '*' : {EPI_PART_NO} : '***'
                Case {EPI_PART_NO} NE ''
                    // This is the Epi Part No on the same Reactor No aggregate Key ID. Aggregate to the Epi Part No and the Reactor No (two different Key IDs).
                    ActionFlow  = {REACTOR_NO} : '****' : @VM
                    ActionFlow := '*' : {EPI_PART_NO} : '***'
            End Case

        Case Otherwise$
            // Reactor No is empty.
            Begin Case
                Case {WEEK_NO} NE ''
                    // This is the Week No aggregate Key ID. Aggregate to the Week Year (same Epi Part No).
                    ActionFlow  = '*' : {EPI_PART_NO} : '*' : {WEEK_YEAR} : '**'
            End Case

    End Case

return


ALL_DETAIL_CHILD_KEY_IDS:

    If {CHILD_KEY_IDS} NE '' then
        // Check the first Child Key ID to see if it is a full Key ID. If so, then return itself. Otherwise,
        // recursively check further.
        ChildKeyID1 = {CHILD_KEY_IDS}<0, 1>
        If (Field(ChildKeyID1, '*', 2, 1) NE '') AND (Field(ChildKeyID1, '*', 5, 1) NE '') then
            ActionFlow  = {CHILD_KEY_IDS}
        end else
            ActionFlow  = Xlate('REACTOR_PERFORMANCE', {CHILD_KEY_IDS}, 'ALL_DETAIL_CHILD_KEY_IDS', 'X')
        end
    end else
        ActionFlow  = @ID
    end

return

TOTAL_WAFERS_EXPECTED:

    If {CHILD_KEY_IDS} EQ '' then
        ActionFlow  = {WAFERS_EXPECTED}
    end else
        ActionFlow  = SUM(Xlate('REACTOR_PERFORMANCE', {CHILD_KEY_IDS}, 'TOTAL_WAFERS_EXPECTED', 'X'))
    end

return


TOTAL_WAFERS_EXPECTED_ROUNDED:

    // This is a modulus rounding to 25 since that is the number of wafers that is processed at one time due to the
    // cassette capacity.
    Round           = SRP_Math('ROUND', (Oconv({TOTAL_WAFERS_EXPECTED}, 'MD2') / 25), '', 0)
    ActionFlow      = Iconv(Round * 25, 'MD2')

return


TOTAL_WAFERS_PROCESSED:

    If {CHILD_KEY_IDS} EQ '' then
        ActionFlow  = {WAFERS_PROCESSED}
    end else
        ActionFlow  = SUM(Xlate('REACTOR_PERFORMANCE', {CHILD_KEY_IDS}, 'TOTAL_WAFERS_PROCESSED', 'X'))
    end

return


TOTAL_WAFERS_DELTA:

    ActionFlow  = {TOTAL_WAFERS_PROCESSED} - {TOTAL_WAFERS_EXPECTED}

return


TOTAL_WAFERS_DELTA_ROUNDED:

    ActionFlow  = {TOTAL_WAFERS_PROCESSED} - {TOTAL_WAFERS_EXPECTED_ROUNDED}

return


REACTOR_NAME:

    ActionFlow      = {REACTOR_NO}
    ReactorRow      = Database_Services('ReadDataRow', 'REACTOR', {REACTOR_NO})
    SecondChamber   = ReactorRow<REACTOR_SECOND_CHAMBER$>
    If SecondChamber NE '' then ActionFlow := '/' : SecondChamber

return


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MFS Actions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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:
return


WRITE_RECORD:
return


DELETE_RECORD_PRE:
return


DELETE_RECORD:
return


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

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
