Compile function NDW_RDS_PROC_STAT_NEPP_EVENTS(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

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

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert                 

    History     :   (Date, Initials, Notes)
        09/29/21    djs     Created initial commuter module.                                           

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_RDS_PROC_STAT_NEPP

$Insert LOGICAL
$Insert EVENT_SETUP
$Insert ACTION_SETUP
$Insert APPCOLORS
$Insert MSG_EQUATES
$Insert WO_MAT_QA_EQUATES

Equ COL.MET_TEST$       To 1
Equ COL.TEST_DESC$      To 2
Equ COL.SLOT$           To 3
Equ COL.SLOT_DESC$      To 4
Equ COL.WFR_QTY$        To 5
Equ COL.MET_MIN$        To 6
Equ COL.MET_MAX$        To 7
Equ COL.MET_RESULT$     To 8
Equ COL.STD_DEV_MAX$    To 9
Equ COL.STD_DEV_RESULT$ To 10
Equ COL.SIG$            To 11
Equ COL.SIG_DTM$        To 12

Equ CRLF$		to \0D0A\

Equ SS_STAGE$ To 1
Equ SS_PASS$  To 2
Equ SS_SIG$   To 3
Equ SS_DATA$  To 4
Equ SS_NA$    To 5

Declare subroutine  SRP_Show_Window, Set_Property, Send_Message, ErrMsg, Msg, Start_Window, QA_Services, Post_Event
Declare subroutine  obj_Appwindow, Popup, Signature_Services
Declare function    Database_Services, Form_Services, Signature_Services, Msg, SRP_Array, QA_Services, obj_Popup
Declare function    Error_Services, Popup_Services, Dialog_Box, MemberOf

SubclassInfo    = Form_Services('FindSubclassControl')
Subclass        = SubclassInfo<1>

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
    Transfer Event to OIEvent
    Transfer Param1 to Event
    Transfer Param2 to Param1
    Transfer Param3 to Param2
    Transfer Param4 to Param3
    Transfer Param5 to Param4
    Transfer Param6 to Param5
    Transfer Param7 to Param6
    Transfer Param8 to Param7
end

GoToEvent Event for CtrlEntID

Return EventFlow else EVENT_CONTINUE$

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------

Event WINDOW.CREATE(CreateParam)
    
    GoSub SetupOLEControls
    If RowExists('RDS', CreateParam) then
        Set_Property(@Window:'.RDS_NO', 'TEXT', CreateParam)
        GoSub RefreshForm
    end
    GoSub EnableRefreshButton
    FormSize = '' 
    SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
    
End Event


Event OLE_BTN_REFRESH.OnClick(Point, Button, Shift, Ctrl)

    GoSub RefreshForm
    
end event


Event EDL_RDS_NO.LOSTFOCUS(Flag, FocusID)

    GoSub EnableRefreshButton
    GoSub RefreshForm
    
end event


Event OLE_EDT_STAGES.OnButtonClick(Cell, Point, Button, Shift, Ctrl)

    // Open related form so the user can sign the stage/metrology test/NCR/etc...
    RDSNo    = Get_Property(@Window:'.EDL_RDS_NO', 'TEXT')
    ErrorMsg = ''
    If RDSNo NE '' then
        StageIndex = Field(Cell, ';', 2)
        StageIDs   = Get_Property(@Window, '@STAGE_ID_LIST')
        StageID    = StageIDs<StageIndex>
        Begin Case
            Case StageID EQ 'VER'
                Start_Window('RDS_PRE_EPI', @Window, RDSNo)
            Case StageID EQ 'LOAD'
                Start_Window('RDS', @Window, RDSNo)
            Case StageID EQ 'UNLOAD'
                Start_Window('RDS_UNLOAD', @Window, RDSNo)
            Case StageID EQ 'POST'
                Start_Window('RDS_POST_EPI', @Window, RDSNo)
            Case StageID EQ 'QA'
                Start_Window('RDS_POST_EPI', @Window, RDSNo)
            Case StageID EQ 'RDS_TEST'
                MetKeys = Xlate('RDS', RDSNo, 'MET_KEYS', 'X')
                NumKeys = DCount(MetKeys, @VM)
                Begin Case
                    Case NumKeys EQ 1
                        Start_Window('RDS_TEST', @Window, MetKeys)
                    Case NumKeys GT 1
                        // Just display the Post-Epi RDS form so that users can select which RDS_TEST record
                        // they would like to view.
                        Start_Window('RDS_POST_EPI', @Window, RDSNo)
                    Case NumKeys EQ 0
                        ErrorMsg = 'RDS_TEST record for RDS ':RDSNo:' does not exist!'
                End Case
            Case ( (StageID EQ 'PREI') or (StageID EQ 'PREC') or (StageID EQ 'PRES') )
                CINo = Xlate('RDS', RDSNo, 'PRE_CI_NO', 'X')
                If RowExists('CLEAN_INSP', CINo) then
                    GoSub OpenCleanInsp
                end else
                    ErrorMsg = 'PRE CLEAN_INSP record for RDS ':RDSNo:' does not exist!'
                end
            Case ( (StageID EQ 'FWII') or (StageID EQ 'FWIC') or (StageID EQ 'FWIS') )
                CINo = Xlate('RDS', RDSNo, 'FWI_CI_NO', 'X')
                If RowExists('CLEAN_INSP', CINo) then
                    GoSub OpenCleanInsp
                end else
                    ErrorMsg = 'FWI CLEAN_INSP record for RDS ':RDSNo:' does not exist!'
                end
            Case StageID EQ 'LOAD_QA_MET'
                WOMatQAKey = Xlate('RDS', RDSNo, 'WO_MAT_KEY', 'X')
                If RowExists('WO_MAT_QA', WOMatQAKey) then
                    WOMatQAStages = Xlate('WO_MAT_QA', WOMatQAKey, 'STAGE', 'X')
                    Locate 'FWI' in WOMatQAStages using @VM setting vPos then                    
                        WONo       = Field(WOMatQAKey, '*', 1)
                        WOStep     = 1
                        CassNo     = Field(WOMatQAKey, '*', 2)
                        RunStep    = 'FWI'
                        CassID     = WONo:'*':WOStep:'*':CassNo:'*':RDSNo:'*':RunStep
                        Start_Window('QA_MET_RESULT', @Window, CassID)
                    end else
                        ErrorMsg = 'LOAD/FWI QA metrology is not prescribed for RDS ':RDSNo:'!'
                    end
                end else
                    ErrorMsg = 'WO_MAT_QA record for RDS ':RDSNo:' does not exist!'
                end
            Case ( (StageID EQ 'LWII') or (StageID EQ 'LWIC') or (StageID EQ 'LWIS') or (StageID EQ 'ROTR') )
                CINo = Xlate('RDS', RDSNo, 'LWI_CI_NO', 'X')
                If RowExists('CLEAN_INSP', CINo) then
                    GoSub OpenCleanInsp    
                end else
                    ErrorMsg = 'LWI CLEAN_INSP record for RDS ':RDSNo:' does not exist!'
                end
            Case StageID EQ 'UNLOAD_QA_MET'
                WOMatQAKey = Xlate('RDS', RDSNo, 'WO_MAT_KEY', 'X')
                If RowExists('WO_MAT_QA', WOMatQAKey) then
                    WONo       = Field(WOMatQAKey, '*', 1)
                    WOStep     = 1
                    CassNo     = Field(WOMatQAKey, '*', 2)
                    RunStep    = 'UNLOAD'
                    CassID     = WONo:'*':WOStep:'*':CassNo:'*':RDSNo:'*':RunStep
                    Start_Window('NDW_QA_MET_RESULT', @Window, CassID)
                end else
                    ErrorMsg = 'WO_MAT_QA record for RDS ':RDSNo:' does not exist!'
                end
            Case ( (StageID EQ 'PSTI') or (StageID EQ 'PSTC') or (StageID EQ 'PSTS') )
                CINo = Xlate('RDS', RDSNo, 'POST_CI_NO', 'X')
                If RowExists('CLEAN_INSP', CINo) then
                    GoSub OpenCleanInsp
                end else
                    ErrorMsg = 'POST CLEAN_INSP record for RDS ':RDSNo:' does not exist!'
                end
            Case StageID EQ 'POST_QA_MET'
                WOMatQAKey = Xlate('RDS', RDSNo, 'WO_MAT_KEY', 'X')
                If RowExists('WO_MAT_QA', WOMatQAKey) then
                    MetSlot    = ''
                    WONo       = Field(WOMatQAKey, '*', 1)
                    WOStep     = 1
                    CassNo     = Field(WOMatQAKey, '*', 2)
                    RunStep    = 'QA'
                    CassID     = WONo:'*':WOStep:'*':CassNo:'*':RDSNo:'*':RunStep
                    TestStages = Xlate('WO_MAT_QA', WOMatQAKey, 'STAGE', 'X')
                    NumTests   = Count(TestStages, 'QA')
                    If NumTests GT 1 then
                        AllSlots = Xlate('WO_MAT_QA', WOMatQAKey, 'SLOT', 'X')
                        QASlots  = ''
                        For each Stage in TestStages using @VM setting vPos
                            If Stage EQ 'QA' then QASlots<0, -1> = AllSlots<0, vPos>
                        Next Stage
                        MetSlot  = Popup_Services('DisplayPopupLiteral', 'Select QA Met Slot', 'Slot', QASlots, 1)
                    end else
                        For each Stage in TestStages using @VM setting vPos
                            If Stage EQ 'QA' then MetSlot = AllSlots<0, vPos>
                        Next Stage
                    end
                    If MetSlot NE '' then
                        WOMatQARec = Database_Services('ReadDataRow', 'WO_MAT_QA', WOMatQAKey)
                        Done       = False$
                        For each Stage in TestStages using @VM setting vPos
                            ThisSlot = AllSlots<0, vPos>
                            If ( (Stage EQ 'QA') and (ThisSlot EQ MetSlot) ) then Done = True$
                        Until Done EQ True$
                        Next Stage
                        MetTest                          = WOMatQARec<WO_MAT_QA_PROFILE$, vPos>
                        TestLine                         = ''
                        TestLine<0, COL.MET_TEST$>       = MetTest
                        TestLine<0, COL.TEST_DESC$>      = obj_Popup('CodeDesc', 'QA_MET_TEST':@RM:MetTest)
                        TestLine<0, COL.SLOT$>           = MetSlot
                        TestLine<0, COL.SLOT_DESC$>      = obj_Popup('CodeDesc','TEST_SLOTS':@RM:MetSlot)
                        TestLine<0, COL.WFR_QTY$>        = WOMatQARec<WO_MAT_QA_WFR_QTY$, vPos>
                        TestLine<0, COL.MET_MIN$>        = WOMatQARec<WO_MAT_QA_MIN$, vPos>
                        TestLine<0, COL.MET_MAX$>        = WOMatQARec<WO_MAT_QA_MAX$, vPos>
                        TestLine<0, COL.MET_RESULT$>     = WOMatQARec<WO_MAT_QA_RESULT$, vPos>
                        TestLine<0, COL.STD_DEV_MAX$>    = WOMatQARec<WO_MAT_QA_STD_MAX$, vPos>
                        TestLine<0, COL.STD_DEV_RESULT$> = WOMatQARec<WO_MAT_QA_STD_RESULT$, vPos>
                        TestLine<0, COL.SIG$>            = WOMatQARec<WO_MAT_QA_SIG$, vPos>
                        TestLine<0, COL.SIG_DTM$>        = WOMatQARec<WO_MAT_QA_SIG_DTM$, vPos>
                        Start_Window('QA_MET_RESULT', @Window, CassID:@FM:TestLine)
                    end
                end else
                    ErrorMsg = 'WO_MAT_QA record for RDS ':RDSNo:' does not exist!'
                end
            Case StageID EQ 'LBLCHK'
                Start_Window('DIALOG_QA_LABEL_CHECK')
            Case StageID EQ 'NCR'
                WOMatKey = Xlate('RDS', RDSNo, 'WO_MAT_KEY', 'X')
                NCRKeys  = Xlate('WO_MAT', WOMatKey, 'NCR_KEYS', 'X')
                If NCRKeys NE '' then
                    NumKeys = DCount(NCRKeys, @VM)
                    If NumKeys GT 1 then
                        NCRKey = Popup_Services('DisplayPopupLiteral', 'Select NCR', 'NCR No', NCRKeys, 1)
                    end else
                        NCRKey = NCRKeys
                    end
                    If NCRKey NE '' then Start_Window('NCR', @Window, NCRKey)
                end else
                    ErrorMsg = 'No NCR records on file for RDS ':RDSNo:'!'
                end
            Case StageID EQ 'SAP_BATCH_ID'
                WOMatKey = Xlate('RDS', RDSNo, 'WO_MAT_KEY', 'X')
                WONo     = Field(WOMatKey, '*', 1)
                CassNo   = Field(WOMatKey, '*', 2)
                Start_Window('WO_MAT', @Window, WOMatKey)
                Set_Property('WO_MAT.WO_NO', 'TEXT', WONo)
                Set_Property('WO_MAT.CASS_NO', 'TEXT', CassNo)
                Post_Event('WO_MAT', 'READ')
        End Case
    end
    
    If ErrorMsg NE '' then ErrMsg(ErrorMsg)
    
end event


Event OLE_EDT_STAGES.OnCheckChanged(Cell, OldValue, NewValue)

    GoSub EnableAutoSignButton
    
end event


Event PUB_AUTO_SIGN.CLICK()
    
    If (Not(MemberOf(@User4, 'LEAD')) or Not(MemberOf(@User4, 'SUPERVISOR') ) ) then
        Response = Dialog_Box('NDW_VERIFY_USER', @WINDOW, @USER4:@FM:'LEAD':@VM:'SUPERVISOR')
        Valid    = Response<1>
    end else
        Valid = True$
    end
    If Valid then
        
        ErrorMsgs = ''
        RDSNo     = Get_Property(@Window:'.EDL_RDS_NO', 'TEXT')
        WOMatKey  = Xlate('RDS', RDSNo, 'WO_MAT_KEY', 'X')
        // Check all locks first
        RDSLock   = Database_Services('IsKeyIDLocked', 'RDS', RDSNo)
        If RDSLock then
            ErrorMsgs<-1> = 'RDS record ':RDSNo:' is locked!'
        end
        WOMatLock   = Database_Services('IsKeyIDLocked', 'WO_MAT', WOMatKey)
        If WOMatLock then
            ErrorMsgs<-1> = 'WO_MAT record ':WOMatKey:' is locked!'
        end
        WOMatQALock = Database_Services('IsKeyIDLocked', 'WO_MAT_QA', WOMatKey)
        If WOMatQALock then
            ErrorMsgs<-1> = 'WO_MAT_QA record ':WOMatKey:' is locked!'
        end
        NCRLock     = False$
        NCRKeys     = Xlate('WO_MAT', WOMatKey, 'NCR_KEYS', 'X')
        If NCRKeys NE '' then
            For each NCRKey in NCRKeys using @VM setting vPos
                Locked = Database_Services('IsKeyIDLocked', 'NCR', NCRKey)
                If Locked then NCRLock = True$
            Until Locked
            Next NCRKey
            If NCRLock then
                ErrorMsgs<-1> = 'NCR record ':NCRKey:' is locked!'
            end
        end
        PreCILock = Database_Services('IsKeyIDLocked', 'CLEAN_INSP', Xlate('RDS', RDSNo, 'PRE_CI_NO', 'X') )
        If PreCILock then
            ErrorMsgs<-1> = 'PRE CLEAN_INSP record is locked!'
        end
        FWICILock = Database_Services('IsKeyIDLocked', 'CLEAN_INSP', Xlate('RDS', RDSNo, 'FWI_CI_NO', 'X') )
        If FWICILock then
            ErrorMsgs<-1> = 'FWI CLEAN_INSP record is locked!'
        end
        LWICILock = Database_Services('IsKeyIDLocked', 'CLEAN_INSP', Xlate('RDS', RDSNo, 'LWI_CI_NO', 'X') )
        If LWICILock then
            ErrorMsgs<-1> = 'LWI CLEAN_INSP record is locked!'
        end
        QACILock  = Database_Services('IsKeyIDLocked', 'CLEAN_INSP', Xlate('RDS', RDSNo, 'POST_CI_NO', 'X') )
        If QACILock then
            ErrorMsgs<-1> = 'POST CLEAN_INSP record is locked!'
        end
        
        If ErrorMsgs EQ '' then 
            Def         = ""
            Def<MTEXT$> = "Auto-signing stages. Please wait..."
            Def<MTYPE$> = "U"
            MsgUp       = Msg(@Window, Def)   ;* display the processing message
            
            // Procedurally sign all stages that have a checkmark
            ChkBoxList = Get_Property(@Window:'.OLE_EDT_STAGES', "OLE.CellCheck[4; All]")
            Swap @RM with @FM in ChkBoxList
            StageIDs   = Get_Property(@Window, '@STAGE_ID_LIST')
            NumRows    = DCount(ChkBoxList, @FM)
            For RowIndex = 1 to NumRows
                ChkVal = ChkBoxList<RowIndex>
                If ChkVal EQ True$ then
                    StageID = StageIDs<RowIndex>
                    Begin Case
                        Case StageID EQ 'VER'
                            WaferQty = Xlate('RDS', RDSNo, 'VERIFY_QTY', 'X')
                            Reactor  = Xlate('RDS', RDSNo, 'REACTOR', 'X')
                            SigReady = QA_Services('PreEpiSignatureReady', RDSNo, @User4, WaferQty, Reactor)
                            If SigReady then
                                QA_Services('SignPreEpiStage', RDSNo, @User4, WaferQty, Reactor, False$)
                                If Error_Services('NoError') then
                                    ChkBoxList<RowIndex> = False$
                                end else
                                    ErrorMsgs<-1> = Error_Services('GetMessage')
                                end
                            end else
                                ErrorMsgs<-1> = Error_Services('GetMessage')
                            end
                        Case StageID EQ 'LOAD'
                            WafersIn = Xlate('RDS', RDSNo, 'WAFERS_IN', 'X')
                            Reactor  = Xlate('RDS', RDSNo, 'REACTOR', 'X')
                            LLSide   = Xlate('RDS', RDSNo, 'LOAD_LOCK_SIDE', 'X')
                            SigReady = QA_Services('LoadSignatureReady', RDSNo, @User4, WafersIn, LLSide, False$)
                            If SigReady then
                                QA_Services('SignLoadStage', RDSNo, @User4, WaferQty, LLSide, False$)
                                If Error_Services('NoError') then
                                    ChkBoxList<RowIndex> = False$
                                end else
                                    ErrorMsgs<-1> = Error_Services('GetMessage')
                                end
                            end else
                                ErrorMsgs<-1> = Error_Services('GetMessage')
                            end
                        Case StageID EQ 'UNLOAD'
                            SigReady = QA_Services('UnloadSignatureReady', RDSNo, @User4)
                            If SigReady then
                                QA_Services('SignUnloadStage', RDSNo, @User4, False$)
                                If Error_Services('NoError') then
                                    ChkBoxList<RowIndex> = False$
                                end else
                                    ErrorMsgs<-1> = Error_Services('GetMessage')
                                end
                            end else
                                ErrorMsgs<-1> = Error_Services('GetMessage')
                            end
                        Case StageID EQ 'POST'
                            PSNo                 = Xlate('RDS', RDSNo, 'PROD_SPEC_ID', 'X')
                            VerInst              = XLATE('PRS_STAGE', PSNo:'*POST', 'INST', 'X')
                            InstructionsVerified = True$
                            If VerInst NE '' then InstructionsVerified = Dialog_Box('RDS_VER', @Window, VerInst)
                            If InstructionsVerified then
                                SigReady = Signature_Services('PostEpiSigReady', RDSNo)
                                If SigReady then
                                    Signature_Services('SignPostEpiStage', RDSNo, @User4)
                                    If Error_Services('NoError') then
                                        ChkBoxList<RowIndex> = False$
                                    end else
                                        ErrorMsgs<-1> = Error_Services('GetMessage')
                                    end
                                end else
                                    ErrorMsgs<-1> = Error_Services('GetMessage')
                                end
                            end else
                                ErrorMsgs<-1> = 'You must acknowledge RDS_VER instructions in order to sign POST-EPI stage.'
                            end
                        Case StageID EQ 'QA'
                            SigReady = Signature_Services('QASigReady', RDSNo)
                            If SigReady then
                                Signature_Services('SignQAStage', RDSNo, @User4)
                                If Error_Services('NoError') then
                                    ChkBoxList<RowIndex> = False$
                                end else
                                    ErrorMsgs<-1> = Error_Services('GetMessage')
                                end
                            end else
                                ErrorMsgs<-1> = Error_Services('GetMessage')
                            end
                        Case StageID EQ 'RDS_TEST'
                            // No signature for RDS_TEST records
                            Null
                        Case ( (StageID EQ 'PREI') or (StageID EQ 'PREC') or (StageID EQ 'PRES') or (StageID EQ 'FWII') |
                            or (StageID EQ 'FWIC') or (StageID EQ 'FWIS') or (StageID EQ 'LWII') or (StageID EQ 'LWIC') |
                            or (StageID EQ 'LWIS') or (StageID EQ 'PSTI') or (StageID EQ 'PSTC') or (StageID EQ 'PSTS') )
                            SigReady = Signature_Services('CleanInspSigReady', RDSNo, StageID)
                            If SigReady then
                                Signature_Services('SignCleanInsp', RDSNo, StageID, @User4)
                                If Error_Services('NoError') then
                                    ChkBoxList<RowIndex> = False$
                                end else
                                    ErrorMsgs<-1> = Error_Services('GetMessage')
                                end
                            end else
                                ErrorMsgs<-1> = Error_Services('GetMessage')
                            end
                        Case ( (StageID EQ 'LOAD_QA_MET') or (StageID EQ 'UNLOAD_QA_MET') or (StageID EQ 'POST_QA_MET') )
                            Begin Case
                                Case StageID EQ 'LOAD_QA_MET'
                                    QAStage = 'LOAD'
                                Case StageID EQ 'UNLOAD_QA_MET'
                                    QAStage = 'UNLOAD'
                                Case StageID EQ 'POST_QA_MET'
                                    QAStage = 'QA'
                            End Case
                            WOMatQARec = Database_Services('ReadDataRow', 'WO_MAT_QA', WOMatKey)
                            TestStages = WOMatQARec<WO_MAT_QA_STAGE$>
                            TestSlots  = WOMatQARec<WO_MAT_QA_SLOT$>
                            For each TestStage in TestStages using @VM setting vPos
                                If TestStage EQ QAStage then
                                    TestSlot = TestSlots<0, vPos>
                                    SigReady   = Signature_Services('QAMetSigReady', RDSNo, QAStage, TestSlot)
                                    If SigReady then
                                        Signature_Services('SignQAMet', RDSNo, QAStage, @User4, TestSlot)
                                        If Error_Services('HasError') then
                                            ErrorMsgs<-1> = Error_Services('GetMessage')
                                        end
                                    end else
                                        ErrorMsgs<-1> = Error_Services('GetMessage')
                                    end
                                end
                            Next TestStage
                            If ErrorMsgs EQ '' then ChkBoxList<RowIndex> = False$
                        Case StageID EQ 'ROTR'
                            SigReady = Signature_Services('ROTRSigReady', RDSNo)
                            If SigReady then
                                // Collect ROTR reason from user
                                MsgParms = 'ROTR Acceptance' : @FM : 'ROTR Acceptance Reason' : @FM : ''
                                Reason   = Msg(@Window, '', 'MESSAGE_INPUT', '', MsgParms)
                                If Reason NE '' then
                                    Signature_Services('SignROTR', RDSNo, @User4, Reason)
                                    If Error_Services('NoError') then
                                        ChkBoxList<RowIndex> = False$
                                    end else
                                        ErrorMsgs<-1> = Error_Services('GetMessage')
                                    end
                                end else
                                    ErrorMsgs<-1> = 'An ROTR acceptance reason must be provided in order to sign ROTR'
                                end
                            end else
                                ErrorMsgs<-1> = Error_Services('GetMessage')
                            end
                        Case StageID EQ 'LBLCHK'
                            // No signature for LBLCHK. Users must use the UI and barcode scanner.
                            Null
                        Case StageID EQ 'NCR'
                            SigReady = Signature_Services('NCRSigReady', RDSNo)
                            If SigReady then
                                Signature_Services('SignNCRs', RDSNo, @User4)
                                If Error_Services('NoError') then
                                    ChkBoxList<RowIndex> = False$
                                end else
                                    ErrorMsgs<-1> = Error_Services('GetMessage')
                                end
                            end else
                                ErrorMsgs<-1> = Error_Services('GetMessage')
                            end
                        Case Otherwise$
                            
                    End Case
                end
            Next RowIndex
            
            Msg(@Window, MsgUp)      ;* Take message down
            Set_Property(@Window, '@CHK_BOX_LIST', ChkBoxList)
            GoSub RefreshForm
        end
    end else
        ErrorMsgs<-1> = 'Only a LEAD or SUPERVISOR may auto-sign.'
    end
    
    If ErrorMsgs NE '' then
        Swap @FM with CRLF$ in ErrorMsgs
        ErrMsg(ErrorMsgs)
    end
    
end event


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

SetupOLEControls:
    
    NumCols          = 4
    NumRows          = 25
    StageIDs         = Database_Services('ReadDataRow', 'APP_INFO', 'SILICON_STAGE_LIST')
    CtrlList         = Xlate('STAGE', StageIDs, 'DESC', 'X')
    StageCtrl        = @Window:'.OLE_EDT_STAGES'
    HeaderFontArray  = 'Segoe UI':@SVM:8:@SVM:700
    HeaderColArray   = 40:@FM:False$:@FM:False$:@FM:False$
    HeaderAlignArray = 'Center':@FM:'Center':@FM:'Center'
    DimArray         = NumCols:@FM:NumRows
    HeaderTitles     = 'Stage':@VM:'Data':@VM:'Signature':@VM:'Sign'
    DataColArray     = 120:@FM:True$:@FM:True$:@FM:True$:@FM:False$
    
    Set_Property(StageCtrl, "OLE.CellFont[All; All]", 'Segoe UI':@SVM:8)
    Set_Property(StageCtrl, "OLE.Dimension", DimArray)
    Set_Property(StageCtrl, "OLE.CellFont[1;All]", HeaderFontArray)
    Set_Property(StageCtrl, "OLE.LIST", CtrlList)
    Set_Property(StageCtrl, "OLE.HeaderFont[All; 1]", HeaderFontArray)
    Set_Property(StageCtrl, "OLE.HeaderFont[1; All]", HeaderFontArray)
    Set_Property(StageCtrl, "OLE.TitleList", HeaderTitles)
    Set_Property(StageCtrl, "OLE.AllowDeletions", False$)
    Set_Property(StageCtrl, "OLE.AllowInserts", False$)
    Set_Property(StageCtrl, "OLE.HeaderColumn[1]", HeaderColArray)
    Set_Property(StageCtrl, "OLE.HeaderAlignment[All; 1]", HeaderAlignArray)
    Set_Property(StageCtrl, "OLE.DataColumn[1]", DataColArray)
    DataColArray<1> = 60
    DataColArray<4> = False$
    Set_Property(StageCtrl, "OLE.DataColumn[2-4]", DataColArray)
    CellAlignArray  = 'Center':@FM:'Center':@FM:'Center'
    Set_Property(StageCtrl, "OLE.CellAlignment[4; All]", CellAlignArray)
    
    // Qualify OLE events that we want to intercept
    Qualifier = ''
    Qualifier<1> = 1
    Qualifier<4> = 0 ; * process synchronously (i.e. immediately)
    Send_Message(StageCtrl,  'QUALIFY_EVENT', 'OLE.OnButtonClick', Qualifier)
    Send_Message(StageCtrl,  'QUALIFY_EVENT', 'OLE.OnCheckChanged', Qualifier)
    Send_Message(@Window:'.OLE_BTN_REFRESH', 'QUALIFY_EVENT', 'OLE.OnClick', Qualifier)
    
    // Stage cell button style setup
    ButtonTypeArray       = ''
    ButtonTypeArray<1>    = 'Push Button'
    ButtonTypeArray<2>    = ''
    ButtonTypeArray<2, 1> = False$
    ButtonTypeArray<2, 2> = True$
    ButtonTypeArray<2, 3> = True$
    ButtonTypeArray<3>    = 'None'
    ButtonTypeArray<4>    = "Vertical(Gradient(":OI_HOT_BLUE$:", ":OI_HOT_BLUE$:"), Border(":OI_BLUE$:"))"
    ButtonTypeArray<5>    = "Vertical(Gradient(":OI_CLICK_BLUE$:", ":OI_CLICK_BLUE$:"), Border(":OI_BLUE$:"))"
    ButtonTypeArray<6>    = 'OD'
    ButtonTypeArray<7>    = 'None'
    Set_Property(StageCtrl, "OLE.CellType[1; All]", ButtonTypeArray)
    
return


EnableRefreshButton:
    
    RDSNo   = Get_Property(@Window:'.EDL_RDS_NO', 'TEXT')
    If RDSNo[1, 2] EQ '1T' then
        RDSNo = RDSNo[3, 999]
        Set_Property(@Window:'.EDL_RDS_NO', 'TEXT', RDSNo)
    end
    Enabled = RowExists('RDS', RDSNo)
    Set_Property(@Window:'.OLE_BTN_REFRESH', 'ENABLED', Enabled)
    
return


EnableAutoSignButton:
    
    ChkVals    = Get_Property(@Window:'.OLE_EDT_STAGES', "OLE.CellCheck[4; All]")
    Enabled    = Sum(ChkVals)
    Set_Property(@Window:'.PUB_AUTO_SIGN', 'ENABLED', Enabled)
    
return


RefreshForm:
    
    RDSNo = Get_Property(@Window:'.EDL_RDS_NO', 'TEXT')
    If RDSNo NE '' then
        If RDSNo[1,2] EQ '1T' then RDSNo[1,2] = ''
        Set_Property(@Window:'.EDL_RDS_NO', 'TEXT', RDSNo)
        ReactorType = Xlate('RDS', RDSNo, 'REACTOR_TYPE', 'X')
        Begin Case
            Case ReactorType EQ 'EPP'
                ErrMsg('EpiPro runs are not supported by this form at this time!')
                Set_Property(@Window:'.EDL_RDS_NO', 'FOCUS', True$)
                Set_Property(@Window:'.EDL_RDS_NO', 'SELECTION', 1:@FM:999)
            Case ReactorType EQ 'GAN'
                ErrMsg('GaN runs are not supported by this form!')
                Set_Property(@Window:'.EDL_RDS_NO', 'FOCUS', True$)
                Set_Property(@Window:'.EDL_RDS_NO', 'SELECTION', 1:@FM:999)
            Case RowExists('RDS', RDSNo)
                Def              = ""
                Def<MTEXT$>      = "Loading stage data. Please wait..."
                Def<MTYPE$>      = "U"
                MsgUp            = Msg(@Window, Def)   ;* display the processing message
                WOMatKey         = Xlate('RDS', RDSNo, 'WO_MAT_KEY', 'X')
                CassSummary      = Signature_Services('GetCassSummary', WOMatKey)
                CassSummaryPrime = SRP_Array('Rotate', CassSummary, @FM, @VM)
                ArrayData        = Get_Property(@Window:'.OLE_EDT_STAGES', 'OLE.ARRAY')
                StageIDs         = Database_Services('ReadDataRow', 'APP_INFO', 'SILICON_STAGE_LIST')
                StageDescs       = Xlate('STAGE', StageIDs, 'DESC', 'X')
                ColorArray       = ''
                DataList         = ''
                ChkBoxList       = ''
                
                Set_Property(@Window, '@STAGE_ID_LIST', StageIDs)
                Set_Property(@Window, '@STAGE_DESC_LIST', StageDescs)
                
                For each Row in CassSummary using @FM setting fPos
                    StageID       = Row<0, SS_STAGE$>
                    StageDesc     = StageDescs<0, fPos>
                    StagePass     = Row<0, SS_PASS$>
                    StageSigComp  = Row<0, SS_SIG$>
                    StageDataComp = Row<0, SS_DATA$>
                    StageNA       = Row<0, SS_NA$>
                    
                    ChkBoxList<fPos> = False$
                    Begin Case
                        Case StagePass
                            ColorArray<fPos, 1> = GREEN$
                            ColorArray<fPos, 2> = BGREEN$
                        Case StageDataComp and Not(StageSigComp)
                            ColorArray<fPos, 1> = YELLOW$
                            ColorArray<fPos, 2> = BYELLOW$
                            ChkBoxList<fPos>    = True$
                        Case Not(StagePass) and Not(StageNA)
                            ColorArray<fPos, 1> = RED$
                            ColorArray<fPos, 2> = BRED$
                        Case Otherwise$
                            ColorArray<fPos, 1> = GREY$
                            ColorArray<fPos, 2> = LTGREY$
                    End Case
                    Begin Case
                        Case StageDataComp
                            DataList<fPos, 2>   = 'Complete'
                            ColorArray<fPos, 3> = GREEN$
                        Case Not(StageDataComp) and Not(StageNA)
                            DataList<fPos, 2>   = 'Missing'
                            ColorArray<fPos, 3> = YELLOW$
                        Case Otherwise$
                            DataList<fPos, 2>   = 'N/A'
                            ColorArray<fPos, 3> = 'None'
                    End Case
                    Begin Case
                        Case StageSigComp
                            DataList<fPos, 3>   = 'Complete'
                            ColorArray<fPos, 4> = GREEN$
                        Case Not(StageSigComp) and Not(StageNA)
                            DataList<fPos, 3>   = 'Missing'
                            ColorArray<fPos, 4> = YELLOW$
                        Case Otherwise$
                            DataList<fPos, 3>   = 'N/A'
                            ColorArray<fPos, 4> = 'None'
                    End Case

                    
                Next Row
                Set_Property(@Window:'.OLE_EDT_STAGES', "OLE.CellCheck[4; All]", False$)
                Set_Property(@Window, '@CHK_BOX_LIST', ChkBoxList)
                Set_Property(@Window, '@DATA_LIST', DataList)
                Set_Property(@Window, '@COLOR_ARRAY', ColorArray)
                GoSub PopulateCells
                GoSub ColorCells
                GoSub EnableChkBoxes
                GoSub EnableAutoSignButton
                GoSub UpdateHoldLabel
                Msg(@Window, MsgUp)      ;* Take message down
            Case Otherwise$
                ErrMsg('RDS ':RDSNo:' does not exist!.')
                Set_Property(@Window:'.EDL_RDS_NO', 'FOCUS', True$)
                Set_Property(@Window:'.EDL_RDS_NO', 'SELECTION', 1:@FM:999)
        End Case
    end
    
return


ColorCells:
    
    ColorArray = Get_Property(@Window, '@COLOR_ARRAY')
    If ColorArray NE '' then
        StageCtrl = @Window:'.OLE_EDT_STAGES'
        For each Row in ColorArray using @FM setting fPos
            // Column 1 - Stage Name
            BackColor        = Row<0, 1>
            HotBackColor     = Row<0, 2>
            CellTypeArray    = Get_Property(StageCtrl, 'OLE.CellType[1;':fPos:']')
            CellTypeArray<3> = BackColor
            CellTypeArray<4> = HotBackColor
            Set_Property(StageCtrl, 'OLE.CellType[1;':fPos:']', CellTypeArray)
            For Col = 2 to 3
                BackColor         = Row<0, Col + 1>
                CellColorArray    = Get_Property(StageCtrl, 'OLE.CellColors[':Col:';':fPos:']')
                CellColorArray<2> = BackColor
                Set_Property(StageCtrl, 'OLE.CellColors[':Col:';':fPos:']', CellColorArray)
            Next Col
            
        Next Row
    end
    
return


PopulateCells:
    
    DataList = Get_Property(@Window, '@DATA_LIST')
    If DataList NE '' then
        StageCtrl = @Window:'.OLE_EDT_STAGES'
        For each Row in DataList using @FM setting fPos
            For Col = 2 to 3
                CellText = DataList<fPos, Col>
                Set_Property(StageCtrl, 'OLE.CellText[':Col:';':fPos:']', CellText)
            Next Col
        Next Row
    end
    
return


EnableChkBoxes:
    
    ChkBoxList = Get_Property(@Window, '@CHK_BOX_LIST')
    If ChkBoxList NE '' then
        StageCtrl = @Window:'.OLE_EDT_STAGES'
        For each Row in ChkBoxList using @FM setting fPos
            ChkBoxEnabled = Row<0, 1>
            If ChkBoxEnabled then
                CellColorArray    = Get_Property(StageCtrl, 'OLE.CellColors[4;':fPos:']')
                CellColorArray<2> = 'None'
                Set_Property(StageCtrl, 'OLE.CellColors[4;':fPos:']', CellColorArray)
                CellTypeArray     = 'CHB':@FM:True$
                Set_Property(StageCtrl, "OLE.CellType[4;":fPos:"]", CellTypeArray)
                Set_Property(StageCtrl, "OLE.CellProtection[4;":fPos:"]", 'None')
            end else
                CellColorArray    = Get_Property(StageCtrl, 'OLE.CellColors[4;':fPos:']')
                CellColorArray<2> = GREY$
                Set_Property(StageCtrl, 'OLE.CellColors[4;':fPos:']', CellColorArray)
                Set_Property(StageCtrl, "OLE.CellType[4;":fPos:"]", 'TEXT')
                Set_Property(StageCtrl, "OLE.CellProtection[4;":fPos:"]", 'Full')
            end
        Next Row
    end
    
return


UpdateHoldLabel:
    
    Ctrl   = @Window:'.LBL_HOLD'
    RDSNo  = Get_Property(@Window:'.EDL_RDS_NO', 'TEXT')
    OnHold = Xlate('RDS', RDSNo, 'HOLD', 'X')
    If OnHold then
        // Set label to "On Hold" and set text color to red
        Set_Property(Ctrl, 'TEXT', 'On Hold')
        Set_Property(Ctrl, 'FORECOLOR', BRED$)
    end else
        // Set label to "Off Hold" and set text color to green
        Set_Property(Ctrl, 'TEXT', 'Off Hold')
        Set_Property(Ctrl, 'FORECOLOR', BGREEN$)
    end
    
return


OpenCleanInsp:
    
    DetWindow  = 'CLEAN_INSP'
    DetKeys    = CINo
    DefaultRec = ''
    RetKey     = RDSNo
    RetWin     = CtrlEntID
    RetPage    = 1
    RetCtrl    = 'OLE_EDT_STAGES'
    RetPos     = ''
    obj_Appwindow('ViewNewDetail',DetWindow:@RM:DetKeys:@RM:DefaultRec:@RM:RetKey:@RM:RetPage:@RM:RetCtrl:@RM:RetPos)
    Set_Property(DetWindow, '@SKIP_RETURN', True$)
    
return


OpenWOMatWfr:
    
    DetWindow  = 'WO_MAT_WFR'
    DetKeys    = WOMatKey
    DefaultRec = ''
    RetKey     = RDSNo
    RetWin     = ''
    RetPage    = 1
    RetCtrl    = ''
    RetPos     = ''
    obj_Appwindow('ViewNewDetail',DetWindow:@RM:DetKeys:@RM:DefaultRec:@RM:RetKey:@RM:RetPage:@RM:RetCtrl:@RM:RetPos)
    Set_Property(DetWindow, '@SKIP_RETURN', True$)
    Set_Property(DetWindow, 'VISIBLE', True$)
    
return


