Function Scans_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        :   Scans_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)
        08/03/18    dmb     Original programmer.

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

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

Declare function    UCase, Utility_DotNet, Database_Services, Error_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('CalculatedColumn')
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CalculateColumn:

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

    Begin Case
        Case CalcColName EQ 'LAST_MODIFIED'                     ; GoSub LAST_MODIFIED
        Case CalcColName EQ 'EMPLOYEE_NAME'                     ; GoSub EMPLOYEE_NAME
        Case CalcColName EQ 'EMPLOYEE_AUTHORIZED'               ; GoSub EMPLOYEE_AUTHORIZED
        Case CalcColName EQ 'EMPLOYEE_NOT_AUTHORIZED_REASON'    ; GoSub EMPLOYEE_NOT_AUTHORIZED_REASON
        Case CalcColName EQ 'LOCATION_NAME'                     ; GoSub LOCATION_NAME
        Case CalcColName EQ 'TOOL_NAME'                         ; GoSub TOOL_NAME
        Case CalcColName EQ 'SCHEDULER_WAFER_COUNT'             ; GoSub SCHEDULER_WAFER_COUNT
*        Case CalcColName EQ 'SCAN_ACCEPTABLE'                   ; GoSub SCAN_ACCEPTABLE
*        Case CalcColName EQ 'SCAN_NOT_ACCEPTABLE_REASON'        ; GoSub SCAN_NOT_ACCEPTABLE_REASON
        Case CalcColName EQ 'TRANSFER_TOOL_NAME'                ; GoSub TRANSFER_TOOL_NAME
    End Case

return


LAST_MODIFIED:

    // Create an HTTP-date formatted Date/Time stamp for this resource.
    LastDate    = {SCANNED_DATES}[-1, 'B' : @VM]
    LastTime    = {SCANNED_TIMES}[-1, 'B' : @VM]
    If LastDate EQ '' then
        // No scanning has occured so use the Create Date/Time.
        LastDate    = {CREATED_DATE}
        LastTime    = {CREATED_TIME}
    end

    lastDateTime    = Iconv(Oconv(LastDate, 'D4/') : ' ' : Oconv(LastTime, 'MTS'), 'DTS')
    lastDateTime    = Utility_DotNet('TIMEZONE', lastDateTime, -1)

    ActionFlow      = lastDateTime

return


EMPLOYEE_NAME:

    EmployeeID  = {EMPLOYEE_ID}
    EmployeeRow = Database_Services('ReadDataRow', 'LSL_USERS', EmployeeID)
    If Error_Services('NoError') then
        EmployeeName    = Trim(EmployeeRow<1> : ' ' : EmployeeRow<2>)
    end else
        EmployeeName    = ''
    end

    ActionFlow  = EmployeeName

return


EMPLOYEE_AUTHORIZED:

    Authorized      = True$
    /* To be implemented at a later date. - DJS 8/16/19
    // Logic to determine if the current employee is authorized to perform the scan action.
    Authorized      = False$    ; // Assume False$ until proven otherise.

    EmployeeID      = {EMPLOYEE_ID}
    ToolID          = {TOOL_ID}
    LocationID      = {LOCATION_ID}

    ValidEmployee   = False$    ; // Assume invalid employee for now.
    If EmployeeID NE '' then
        EmployeeRow = Database_Services('ReadDataRow', 'LSL_USERS', EmployeeID)
        If Error_Services('NoError') then ValidEmployee = True$
    end

    Begin Case
        Case (LocationID NE '') AND (ValidEmployee EQ True$)
            // Location scans can be performed by any valid employee.
            Authorized  = True$

        Case (ToolID NE '') AND (ValidEmployee EQ True$)
            // Location scans can be performed by any valid employee.
            Locate ToolID in EmployeeRow<3> using @VM setting vPos then
                // This employee is authorized to use this tool.
                Authorized  = True$
            end

        Case (ToolID EQ '') AND (LocationID EQ '') AND ((EmployeeID EQ '') OR (ValidEmployee EQ True$))
            // Nothing is happening yet, so nothing to authorize.
            Authorized  = True$

    End Case
    */
    ActionFlow  = Authorized

return


EMPLOYEE_NOT_AUTHORIZED_REASON:

    // If EMPLOYEE_AUTHORIZED is false, this returns the reason. Note that this uses much of the same logic that exists
    // in EMPLOYEE_AUTHORIZED.
    Reason  = ''

    If {EMPLOYEE_AUTHORIZED} NE True$ then
        EmployeeID  = {EMPLOYEE_ID}
        ToolID      = {TOOL_ID}
        If EmployeeID NE '' then
            EmployeeRow = Database_Services('ReadDataRow', 'LSL_USERS', EmployeeID)
            If Error_Services('NoError') then
                If ToolID NE '' then
                    Locate ToolID in EmployeeRow<3> using @VM setting vPos else
                        // This employee is authorized to use this tool.
                        ToolName        = Xlate('TOOL', ToolID, 'TOOL_DESC', 'X')
                        EmployeeName    = Xlate('LSL_USERS', EmployeeID, 'EMPLOYEE_NAME', 'X')
                        Reason          = EmployeeName : ' is not authorized to use the ' : ToolName : ' tool.'
                    end
                end
            end else
                Reason  = EmployeeID : ' is not a valid employee ID.'
            end
        end else
            Reason  = 'No employee ID has been scanned.'
        end
    end

    ActionFlow  = Reason

return


LOCATION_NAME:

    LocationID  = {LOCATION_ID}
    Convert '.' to '*' in LocationID
    LocationRow = Database_Services('ReadDataRow', 'LOCATION', LocationID)
    Convert '*' to '.' in LocationID
    If Error_Services('NoError') then
        LocationName    = Trim(LocationRow<1>)
    end else
        LocationName    = ''
    end

    ActionFlow  = LocationName

return


TOOL_NAME:

    ToolID  = {TOOL_ID}
    ToolRow = Database_Services('ReadDataRow', 'TOOL', ToolID)
    If Error_Services('NoError') then
        ToolName    = Trim(ToolRow<1>)
    end else
        ToolName    = ''
    end

    ActionFlow  = ToolName

return


SCHEDULER_WAFER_COUNT:

    // Wafer count as indicated by the scheduler. This is test code until moved onto the Infineon servers.
    If {CASSETTE_IDS} NE '' then

        // Make sure only the first Cassette ID is used. The API should have aleady pruned the list to one
        // Cassette ID, so this is a precaution.
        CassetteID = {CASSETTE_IDS}<0, 1>
        If DCount(CassetteID, '.') GT 1 then
            // WM_IN or WM_OUT Cassette ID. Need to get the associated RDS number.
            Convert '.' to '*' in CassetteID
            RDSNo   = Xlate('WM_IN', CassetteID, 'RDS_NO', 'X')<0, 1>
        end else
            // RDS Cassette ID
            RDSNo   = CassetteID
        end
        Count   = Xlate('RDS', RDSNo, 'CASS_WAFER_QTY', 'X')
    end else
        Count   = 0
    end

    ActionFlow  = Count

return


SCAN_ACCEPTABLE:

    // Logic to determine if the current scan is acceptable for processing.
    Acceptable  = False$    ; // Assume False$ until proven otherwise.

    ScanType    = {SCAN_TYPE}
    Begin Case
        Case (ScanType EQ 'Lot') AND ({EMPLOYEE_AUTHORIZED} EQ True$)
            If ({CASSETTE_IDS} NE '') AND (({TOOL_ID} NE '') OR ({LOCATION_ID} NE '')) then
                Acceptable = True$
            end
    End Case

    ActionFlow  = Acceptable

return


SCAN_NOT_ACCEPTABLE_REASON:

    // If SCAN_ACCEPTABLE is false, this returns the reason. Note that this uses much of the same logic that exists
    // in SCAN_ACCEPTABLE.
    Reason      = ''

    If {SCAN_ACCEPTABLE} NE True$ then
        ScanType    = {SCAN_TYPE}
        Begin Case
            Case ScanType EQ ''
                Reason  = 'Insufficient information to determine the scan type.'
            Case ScanType EQ 'Lot'
                Begin Case
                    Case {EMPLOYEE_AUTHORIZED} NE True$
                        Reason  = {EMPLOYEE_NOT_AUTHORIZED_REASON}
                    Case {CASSETTE_IDS} EQ ''
                        Reason  = 'No cassette(s) have been scanned.'
                    Case ({TOOL_ID} EQ '') AND ({LOCATION_ID} EQ '')
                        Reason  = 'No tool or location has been scanned'
                End Case
            Case Otherwise$
                Reason  = ScanType : ' is not a recognized scan type.'
        End Case
    end

    ActionFlow  = Reason

return


TRANSFER_TOOL_NAME:

    ToolID  = {TRANSFER_TOOL_ID}
    ToolRow = Database_Services('ReadDataRow', 'TOOL', ToolID)
    If Error_Services('NoError') then
        ToolName    = Trim(ToolRow<1>)
    end else
        ToolName    = ''
    end

    ActionFlow  = ToolName

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
