Function Daily_Performance_Report_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        :   Daily_Performance_Report_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)
        01/04/23    dmb     Original programmer.

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

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

Declare function    Epi_Part_Services, Reactor_Services, SRP_Datetime, SRP_Math, Datetime	
Declare subroutine  Btree.Extract

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('CalculatedColumn')
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CalculateColumn:

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

    Begin Case
        Case CalcColName EQ 'MIN_PER_WAFER'                     ; GoSub MIN_PER_WAFER
        Case CalcColName EQ 'OEE'                               ; GoSub OEE
        Case CalcColName EQ 'PERCENT_ON_PART'                   ; GoSub PERCENT_ON_PART
        Case CalcColName EQ 'PROJECTED_OUT'                     ; GoSub PROJECTED_OUT
        Case CalcColName EQ 'REACTOR_TYPE'                      ; GoSub REACTOR_TYPE
        Case CalcColName EQ 'TIME_ADJ_PROJECTED_OUT'            ; GoSub TIME_ADJ_PROJECTED_OUT
        Case CalcColName EQ 'PROJECTED_OUT_ALT'                 ; GoSub PROJECTED_OUT_ALT
        Case CalcColName EQ 'TOTAL_DELTA'                       ; GoSub TOTAL_DELTA
        Case CalcColName EQ 'TOTAL_WAFERS_OUT'                  ; GoSub TOTAL_WAFERS_OUT
        Case CalcColName EQ 'WAFERS_PER_DAY'                    ; GoSub WAFERS_PER_DAY
        Case CalcColName EQ 'TOTAL_UPTIME_PERCENT'              ; GoSub TOTAL_UPTIME_PERCENT
        Case CalcColName EQ 'OEE_CALCULATION'                   ; GoSub OEE_CALCULATION
        Case CalcColName EQ 'TOTAL_DELTA_ALT'                   ; GoSub TOTAL_DELTA_ALT
            
    End Case

return


MIN_PER_WAFER:

    ReactorType = {REACTOR_TYPE}
    If ReactorType EQ 'EPP' then ReactorType = 'EPIPRO'
    ActionFlow  = Epi_Part_Services('GetMinutesPerWaferScheduler', {PART_NO}, ReactorType)

return


OEE:
    
    ReactType = {REACTOR_TYPE}
    If ReactType NE '' then 
        ActionFlow = Xlate('APP_INFO', 'OEE*':ReactType, 1, 'X')
    end else
        ActionFlow = ''
    end
    
return


PERCENT_ON_PART:
    
    ActionFlow = OConv(IConv({HOURS_ON_PART} / 24, 'MD2'), 'MD2')
    
return


PROJECTED_OUT:
    
    ActionFlow = OConv(IConv({WAFERS_PER_DAY} * {OEE}, 'MD2'), 'MD2')
    
return

PROJECTED_OUT_ALT:
    
    MinutesPerWafer    = {MIN_PER_WAFER}
    StartDTM           = XLATE('DAILY_PERFORMANCE_REPORT', @ID, 'START_DTM', 'X')
    StopDTM            = XLATE('DAILY_PERFORMANCE_REPORT', @ID, 'STOP_DTM', 'X')
    TotalMinutesOnPart = SRP_Datetime('MinuteSpan', StartDTM, StopDTM)
    TotalMinutesUptime = {TOTAL_PROD_MIN}
    IF MinutesPerWafer GT 0 then
        ExpectedWafers     = SRP_MATH('ROUND', TotalMinutesUptime / MinutesPerWafer, 2)
    end else
        ExpectedWafers = 0
    end
    ActionFlow         = ExpectedWafers
return


REACTOR_TYPE:
    
    ActionFlow = Xlate('REACTOR', {REACTOR}, 'REACT_TYPE', 'X')
    
return


TIME_ADJ_PROJECTED_OUT:
    
    ActionFlow = OConv(IConv({PROJECTED_OUT} * {PERCENT_ON_PART}, 'MD2'), 'MD2')
    
return


TOTAL_DELTA:
    
    TempDict      = @DICT
    TempID        = @ID
    TempRecord    = @RECORD
    TempFileError = @FILE.ERROR
    
    SearchString = 'REACTOR':@VM:{REACTOR}:@FM:'DATE_OUT':@VM:OConv({DATE_OUT}, 'D4/')
    Table        = 'DAILY_PERFORMANCE_REPORT'
    Keys         = ''
    Option       = 'S' ; // Suppress informational messages
    Flag         = ''
    Dict         = @Dict
    Btree.Extract(SearchString, Table, Dict, Keys, Option, Flag)
    If Flag EQ 0 then
        TotalTimeAdjProjOut = Sum(Xlate(Table, Keys, 'TIME_ADJ_PROJECTED_OUT', 'X'))
        TotalProjOut   = Sum(Xlate(Table, Keys, 'PROJECTED_OUT_ALT', 'X'))
        TotalWafersOut = Sum(Xlate(Table, Keys, 'ACTUAL_OUT', 'X'))
        ActionFlow     = TotalWafersOut - TotalTimeAdjProjOut
    end else
        ActionFlow   = ''
    end
    
    @DICT       = TempDict
    @ID         = TempID
    @RECORD     = TempRecord
    @FILE.ERROR = TempFileError
    
return

TOTAL_DELTA_ALT:

    TempDict      = @DICT
    TempID        = @ID
    TempRecord    = @RECORD
    TempFileError = @FILE.ERROR
    
    SearchString = 'REACTOR':@VM:{REACTOR}:@FM:'DATE_OUT':@VM:OConv({DATE_OUT}, 'D4/')
    Table        = 'DAILY_PERFORMANCE_REPORT'
    Keys         = ''
    Option       = 'S' ; // Suppress informational messages
    Flag         = ''
    Dict         = @Dict
    Btree.Extract(SearchString, Table, Dict, Keys, Option, Flag)
    If Flag EQ 0 then
        *TotalTimeAdjProjOut = Sum(Xlate(Table, Keys, 'TIME_ADJ_PROJECTED_OUT', 'X'))
        TotalProjOut   = Sum(Xlate(Table, Keys, 'PROJECTED_OUT_ALT', 'X'))
        TotalWafersOut = Sum(Xlate(Table, Keys, 'ACTUAL_OUT', 'X'))
        ActionFlow     = TotalWafersOut - TotalProjOut
    end else
        ActionFlow   = ''
    end
    
    @DICT       = TempDict
    @ID         = TempID
    @RECORD     = TempRecord
    @FILE.ERROR = TempFileError
    
return

TOTAL_WAFERS_OUT:
    
    TempDict      = @DICT
    TempID        = @ID
    TempRecord    = @RECORD
    TempFileError = @FILE.ERROR
    
    SearchString = 'REACTOR':@VM:{REACTOR}:@FM:'DATE_OUT':@VM:OConv({DATE_OUT}, 'D4/')
    Table        = 'DAILY_PERFORMANCE_REPORT'
    Keys         = ''
    Option       = 'S' ; // Suppress informational messages
    Flag         = ''
    Dict         = @Dict
    Btree.Extract(SearchString, Table, Dict, Keys, Option, Flag)
    If Flag EQ 0 then
        ActionFlow = Sum(Xlate(Table, Keys, 'ACTUAL_OUT', 'X'))
    end else
        ActionFlow = ''
    end
    
    @DICT       = TempDict
    @ID         = TempID
    @RECORD     = TempRecord
    @FILE.ERROR = TempFileError
    
return

TOTAL_UPTIME_PERCENT:
    ToolModeData = Reactor_Services('GetReactorUptimeMetricsByTimeSpan', {REACTOR}, {START_DTM}, {STOP_DTM})
    ActionFlow   = ToolModeData<1,1>
return

WAFERS_PER_DAY:
    
    ReactorType = {REACTOR_TYPE}
    If ReactorType EQ 'EPP' then ReactorType = 'EPIPRO'
    ActionFlow  = Epi_Part_Services('GetIdealWafersPerDayScheduler', {PART_NO}, ReactorType, {MIN_PER_WAFER})
    
return

OEE_CALCULATION:
    TotalUptimePercent      = XLATE('DAILY_PERFORMANCE_REPORT', @ID, 'TOTAL_PROD_PERCENT', 'X')
    TotalUptimeMin          = XLATE('DAILY_PERFORMANCE_REPORT', @ID, 'TOTAL_PROD_MIN', 'X')
    TotalUnschedDownPercent = XLATE('DAILY_PERFORMANCE_REPORT', @ID, 'TOTAL_UNSCHED_PERCENT', 'X')
    TotalUnschedDownMin     = XLATE('DAILY_PERFORMANCE_REPORT', @ID, 'TOTAL_UNSCHED_MIN', 'X')
    TotalSchedDownPercent   = XLATE('DAILY_PERFORMANCE_REPORT', @ID, 'TOTAL_SCHED_PERCENT', 'X')
    TotalSchedDownMin       = XLATE('DAILY_PERFORMANCE_REPORT', @ID, 'TOTAL_SCHED_MIN', 'X')
    
    IF {DATE_OUT} EQ Date()then
    	IF SRP_Datetime('AddMinutes', {START_DTM}, {HOURS_ON_PART} * 60) LT Datetime() then
    		MinutesOnPart = {HOURS_ON_PART} * 60
    	end else
    		MinutesOnPart = SRP_Datetime('MinuteSpan', {START_DTM}, Datetime())
    	end
    	
    end else
		MinutesOnPart           = SRP_Datetime('MinuteSpan', {START_DTM}, {STOP_DTM})	
    end
    //Begin Builidng Availability
    PlannedProdTime         = MinutesOnPart - TotalSchedDownMin
    OperatingTime           = PlannedProdTime - TotalUnschedDownMin
    IF SRP_MATH('EXPONENT',OperatingTime) LT 0 then OperatingTime = 0
    IF PlannedProdTime GT 0 then
        Availability = OperatingTime / PlannedProdTime  
    end else
        Availability = 0
    end
    
    //Begin Building Performance
    IF {MIN_PER_WAFER} GT 0 then
        Capacity    = (1 / {MIN_PER_WAFER}) * OperatingTime
        IF Capacity GT 0 then
            Performance = {ACTUAL_OUT} / Capacity
        end else
            Performance = 1
        end
        
    end else
        Performance = 0
    end
    //Quality ***TODO
    //(Total Parts Produced - Total Scrap) / Total Parts Produced = Quality %
    Quality      = 1
    
    OEE          = SRP_MATH('ROUND',Availability * Performance * Quality, 2)
    ActionFlow   = OEE
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



