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

    Name        :   NDW_GAN_RETAIN_MANAGER_TASK_RETAIN_EVENTS

    Description :   Commuter module for the NDW_GAN_RETAIN_MANAGER_TASK_RETAIN_EVENTS form.

    Notes       :   Application errors should be logged using the Error Services module. There are a few methodological
                    assumptions built into way errors are managed which are important to understand in order to properly
                    work with Error Services:

                    - The term 'top' refers to the originating procedure of a call stack and the term 'bottom' refers to
                      the last routine (or the current routine) within a call stack. Within the OpenInsight Debugger
                      this will appear backwards since the originating procedure always appears at the bottom of the
                      list and the current routine appears at the top of the list. We are using this orientation because
                      it is common to refer to the process of calling other procedures as 'drilling down'.

                    - The reason for defining the orientation of the call stack is because Error_Services allows for
                      multiple error conditions to be appended to an original error. In most cases this will happen when
                      a procedure at the bottom of the stack generates an error condition and then returns to its
                      calling procedure. This higher level procedure can optionally add more information relevant to
                      itself. This continues as the call stack 'bubbles' its way back to the top to where the
                      originating procedure is waiting.

                    - Native OpenInsight commands that handle errors (e.g., Set_Status, Set_FSError, Set_EventStatus)
                      preserve their error state until explicitly cleared. This can hinder the normal execution of code
                      since subsequent procedures (usually SSPs) will fail if a pre-existing error condition exists.
                      Our philosophy is that error conditions should automatically be cleared before a new procedure
                      is executed to avoid this problem. However, the nature of Basic+ does not make this easy to
                      automate for any given stored procedure. Therefore, if a stored procedure wants to conform to our
                      philosophy then it should include a call into the 'Clear' service request at the top of the
                      program. Alternatively this can be done through a common insert (see SERVICE_SETUP for example.)

                    - Service modules will use the SERVICE_SETUP insert and therefore automatically clear out any
                      error conditions that were set before.

    Parameters  :
        Service         [in] -- Name of the service being requested
        Param1-10   [in/out] -- Additional request parameter holders
        Response       [out] -- Response to be sent back to the Controller (MCP) or requesting procedure

    Metadata    :

    History     :   (Date, Initials, Notes)
        05/14/20    cn      Original programmer.
        06/22/20    cn      Split Retain and Destroy forms into separate forms

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_GAN_RETAIN_MANAGER_TASK_RETAIN

$Insert LOGICAL
$Insert APPCOLORS
$Insert RETAINED_WAFERS_EQUATES
$Insert MSG_EQUATES

// Control Name equates
Equ RetainedTable$    To @Window : '.OLE_EDT_RETAINED'
Equ StatusBar$        To @Window : '.OLE_STATUSBAR'  

// Reduce modes (for Select statement)
Equ NEW_EXIST$ To 0 
Equ NEXT_CUR$  To 1
Equ ADD_EXIST$ To 2

    
// OLE Edit Table Columns
Equ RUN_ID$         To  1
Equ POCKET$         To  2
Equ SCRIBE$         To  3
Equ WO_NO$          To  4
Equ RETAIN_BOX$     To  5
Equ RETAIN_SLOT$    To  6
Equ Selection$      To  7
Equ WAFER_ID$       To  8

Declare Function   Form_Services, Get_Property, Send_Message, SRP_Array, SRP_Sort_Array
Declare Function   Gan_Services, Retain_Manager_Services, MSG, MemberOf
Declare Subroutine SRP_Show_Window, Set_Property, Send_Message, SRP_Set_MinMaxInfo
Declare Subroutine SRP_Edittable_Manager, Form_Services, Gan_Services, Retain_Manager_Services

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

If Assigned(Param8) else Param8 = ''

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

Type = Get_Property(CtrlEntId, 'TYPE')
If Type EQ 'RADIOBUTTON' then
    Convert ' ' to '_' in CtrlEntID
end

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

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

Event WINDOW.CREATE(CreateParam) 
    GoSub SetupOLEControls
    Set_Property(@Window, 'TIMER', 30000 : @FM : 30000)
    SRP_Set_MinMaxInfo(@Window, 'DEF', 'DEF', 'DEF')
    SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, '', 1)
End Event


Event WINDOW.TIMER()
    GoSub UpdateServiceLastRunTime
end event


Event RAG_WAFER_TYPE.150_MM_6_IN.CLICK()
Event RAG_WAFER_TYPE.200_MM_8_IN.CLICK()
    GoSub UpdateServiceLastRunTime
    Form_Services('DisplayWaitScreen', '', 'Refreshing Data')
    GoSub LoadRetainedWaferData
    Form_Services('CloseWaitScreen')    
end event


Event PUB_RETAIN.CLICK()
    GoSub SignRetainedWafers
    GoSub UpdateServiceLastRunTime
    Set_Property(@Window, '@RETAINEDCHECKCOUNTER', '')
    Set_Property(@Window : '.PUB_RETAIN', 'ENABLED', False$)
end event


Event OLE_EDT_RETAINED.OnCheckChanged(Cell, OldValue, NewValue)
    RetainedCheckCounter = Get_Property(@Window, '@RETAINEDCHECKCOUNTER')
    If OldValue EQ True$ then
        RetainedCheckCounter   -= 1
    end else
        RetainedCheckCounter   += 1
    end
    
    Set_Property(@Window, '@RETAINEDCHECKCOUNTER', RetainedCheckCounter)
    
    If RetainedCheckCounter GT 0 then
        Set_Property(@Window : '.PUB_RETAIN', 'ENABLED', True$)
    end else
        Set_Property(@Window : '.PUB_RETAIN', 'ENABLED', False$)
    end
end event


Event WINDOW.CLOSE(CancelFlag)
end event

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


SetupOLEControls:
    ArrayDimension      = 8 : @FM : 50
    TitleList           = 'Run ID' : @VM : 'Pocket' : @VM : 'Scribe' : @VM : 'WO' : @VM : 'Retain Box' : @VM : 'Slot' : @VM : 'Select'  : @VM : 'Wafer ID'  : @VM
    ColumnWidths        = 'A'      : @FM : '45'     : @FM : '70'     : @FM : '65' : @FM : '75'         : @FM : '40'   : @FM : '40'      : @FM : '45'        : @FM
    ColumnAlignments    = 'L'      : @FM : 'C'      : @FM : 'C'      : @FM : 'C'  : @FM : 'C'          : @FM : 'C'    : @FM : 'C'       : @FM : 'L'         : @FM
    
    VirtualPos          = ''
    RowsAsColumns       = ''
    CellsAsFields       = ''
    ClearFill           = ''
    OIEditTable         = ''
    SRP_EditTable_Manager('Setup1', RetainedTable$, ArrayDimension, TitleList, ColumnWidths, ColumnAlignments, VirtualPos, RowsAsColumns, CellsAsFields, ClearFill, OIEditTable)
    Set_Property(RetainedTable$, 'OLE.SelectionStyle', 'Black' : @VM : OI_HOT_BLUE$ : ' L=70' : @FM : 'Black' : @VM : OI_HOT_BLUE$)     ; // Automatically highlight the current row with one color and highlight the current row with another color.
    Set_Property(RetainedTable$, 'OLE.HeaderFont[All; All]', 'Segoe UI' : @SVM : 8)
    Set_Property(RetainedTable$, 'OLE.CellFont[All; All]', 'Segoe UI' : @SVM : 8)
    Set_Property(RetainedTable$, 'OLE.HeaderColumn[1]', '' : @FM : False$ : @FM : False$ : @FM)
    Set_Property(RetainedTable$, 'OLE.CellProtection[ALL; All]', 'SEL')
 	Set_Property(RetainedTable$, 'OLE.NewRowCount', 0)
    Set_Property(RetainedTable$, 'OLE.HeaderRow[ALL]', '' : @FM : '' : @FM : False$ : @FM)
    Set_Property(RetainedTable$, 'OLE.DataColumn[ALL]', '' : @FM : '' : @FM : False$ : @FM)
    Set_Property(RetainedTable$, 'OLE.DataRow[ALL]', '' : @FM : '' : @FM : False$ : @FM)
    Set_Property(RetainedTable$, 'OLE.HeaderColors[All; All]', 'Auto' : @FM : 'None' : @FM : 'None' : @FM : OI_HOT_BLUE$ : @FM : False$)
    Set_Property(RetainedTable$, 'OLE.ResetSelPos', False$)
	Set_Property(RetainedTable$, 'OLE.AllowDeletions', False$)
    Set_Property(RetainedTable$, 'OLE.AllowInserts', False$)
    Set_Property(RetainedTable$, 'OLE.CellType[' : Selection$ : ';ALL]', 'Check Box' : @FM : 0 : @VM : 1)
    // Begin with hidden destroyed sig and date
    Set_Property(RetainedTable$, 'OLE.DataColumn[' : WAFER_ID$ : ']', '' : @FM : False$)
    
    // Qualify OLE events that we want to intercept
    Qualifier       = ''
    Qualifier<1>    = 1
    Qualifier<4>    = 0 ; * process synchronously (i.e. immediately)
    Send_Message(RetainedTable$,  'QUALIFY_EVENT', 'OLE.AfterUpdate',         Qualifier)
    Send_Message(RetainedTable$,  'QUALIFY_EVENT', 'OLE.OnClick',             Qualifier)
    Send_Message(RetainedTable$,  'QUALIFY_EVENT', 'OLE.OnButtonClick',       Qualifier)
    Send_Message(RetainedTable$,  'QUALIFY_EVENT', 'OLE.OnContextMenuClick',  Qualifier)
    Send_Message(RetainedTable$,  'QUALIFY_EVENT', 'OLE.PosChanged',          Qualifier)
    Send_Message(RetainedTable$,  'QUALIFY_EVENT', 'OLE.OnComboClicked',      Qualifier)
    Send_Message(RetainedTable$,  'QUALIFY_EVENT', 'OLE.OnHeaderClick',       Qualifier)
    Send_Message(RetainedTable$,  'QUALIFY_EVENT', 'OLE.OnCheckChanged',      Qualifier)
    Send_Message(RetainedTable$,  'QUALIFY_EVENT', 'OLE.OnDblClick',          Qualifier)
    
    // APP_INFO RETAINED_WAFER Fields
    Equ RETAIN_WAFER_RUN_ID$         To  1
    Equ RETAIN_WAFER_RECIPE$         To  2
    Equ RETAIN_WAFER_POCKET$         To  3
    Equ RETAIN_WAFER_SCRIBE$         To  4
    Equ RETAIN_WAFER_GRADE$          To  5
    Equ RETAIN_WAFER_PART$           To  6
    Equ RETAIN_WAFER_WO_NO$          To  7
    Equ RETAIN_WAFER_RETAIN_BOX$     To  8
    Equ RETAIN_WAFER_RETAIN_SLOT$    To  9
    Equ RETAIN_WAFER_RETAIN_LOC$     To 10
    Equ RETAIN_WAFER_STATUS$         To 11
    Equ RETAIN_WAFER_COMMENT$        To 12
    Equ RETAIN_WAFER_OPERATOR$       To 13
    Equ RETAIN_WAFER_RETAIN_DATE$    To 14
    Equ RETAIN_WAFER_DESTROY_SIG$    To 15
    Equ RETAIN_WAFER_DESTROY_DATE$   To 16 
    Equ RETAIN_WAFER_WAFER_ID$       To 17    
    
    GoSub LoadRetainedWaferData
    
    Set_Property(StatusBar$, 'OLE.PaneCount', 1)
    Set_Property(StatusBar$, 'OLE.Resizable', False$)
	Set_Property(StatusBar$, 'OLE.PaneType[1]', 'NOR')
    GoSub UpdateServiceLastRunTime
return


SignRetainedWafers:
    Set_Property(RetainedTable$, 'OLE.Redraw', False$)
    WaferList       = Get_Property(RetainedTable$, 'OLE.ARRAY')
    WaferList       = SRP_Array('Rotate', WaferList, @FM, @VM)
    RemoveWaferList = ''
    
    For Each Wafer in WaferList using @FM setting WaferPos then
        SelectedWafer = Wafer<1, Selection$>
        If SelectedWafer EQ True$ then
            RemoveWaferList := WaferPos : @FM
            InboundWfrID = Wafer<1, WAFER_ID$>
            Gan_Services('SignRetainedWafer', InboundWfrID)
        end
    Next Wafer
    
    RemoveWaferList[-1, 1]  = ''
    RemoveWaferList         = SRP_Sort_Array(RemoveWaferList, 'DN1', 1)
    
    For Each RemoveWaferPos in RemoveWaferList using @FM setting dummypos then
        Send_Message(RetainedTable$, 'OLE.DeleteRecords', RemoveWaferPos, 1)
    Next RemoveWaferPos
    Set_Property(RetainedTable$, 'OLE.Redraw', True$)
return


LoadRetainedWaferData:
    Set_Property(RetainedTable$, 'OLE.Redraw', False$)
    WaferSize           = Get_Property(@Window : '.RAG_WAFER_TYPE', 'VALUE')
    RetainedWaferList   = ''
    FullWaferList       = Retain_Manager_Services('GetWaferData', WaferSize)
    
    For Each Wafer in FullWaferList using @FM setting WaferPos then
        Convert @VM to @FM in Wafer
        Location    = Wafer<RETAIN_WAFER_RETAIN_LOC$>
        Status      = Wafer<RETAIN_WAFER_STATUS$>
        RetainSig   = Wafer<RETAIN_WAFER_OPERATOR$>
        
        If Location _NEC 'Wafer Destroyed' AND Status _NEC 'do not destroy' then
            RetainBox       = Wafer<RETAIN_WAFER_RETAIN_BOX$>
            RetainedSlot    = Wafer<RETAIN_WAFER_RETAIN_SLOT$>
            RetainedDate    = Wafer<RETAIN_WAFER_RETAIN_DATE$>
            If RetainBox NE '' AND RetainedSlot NE '' AND RetainedDate EQ '' then
                RetainedWaferList := Wafer<RETAIN_WAFER_RUN_ID$> : @VM : Wafer<RETAIN_WAFER_POCKET$> : @VM : Wafer<RETAIN_WAFER_SCRIBE$> : @VM
                RetainedWaferList := Wafer<RETAIN_WAFER_WO_NO$> : @VM : Wafer<RETAIN_WAFER_RETAIN_BOX$> : @VM : Wafer<RETAIN_WAFER_RETAIN_SLOT$> : @VM
                RetainedWaferList := False$ : @VM : Wafer<RETAIN_WAFER_WAFER_ID$> : @FM
            end
        end
    Next Wafer
    
    RetainedWaferList[-1, 1]    = ''   
    RetainedWaferList           = SRP_Array('Rotate', RetainedWaferList, @FM, @VM)
    RetainedWaferList           = SRP_Sort_Array(RetainedWaferList, 'AL1' : @FM : 'AN6', 0)
    
    Set_Property(RetainedTable$, 'OLE.ARRAY', RetainedWaferList)
    Set_Property(RetainedTable$, 'OLE.Redraw', True$)
return


UpdateServiceLastRunTime:
    LastRunDTM = Retain_Manager_Services('GetRetainServiceLastRunTime')
    Set_Property(StatusBar$, 'OLE.PaneCaption[1]', 'Retain Wafer Service last ran at: ' : LastRunDTM)
return

