Function Retain_Manager_Services(@Service, @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 SRP Computer Solutions, Inc.

    Name        :   Retain_Manager_Services

    Description :   Handler program for all Retain Wafer services.

    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:

    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)
        04/29/20    cn     Original programmer

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

$Insert APP_INSERTS
$Insert SERVICE_SETUP
$Insert SERVICES_EQUATES
$Insert MSG_EQUATES
$Insert NCR_EQUATES
$Insert WO_WFR_EQUATES
$Insert REACT_RUN_EQUATES
$Insert RETAINED_WAFERS_EQUATES

// 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

Declare Function   SRP_FastArray, SRP_Array, Database_Services, Retain_Manager_Services, Environment_Services
Declare Function   SRP_Run_Command
Declare Subroutine SRP_FastArray

GoToService else
    Error_Services('Add', Service : ' is not a valid service request within the ' : ServiceModule : ' module.')
end

Return Response OR ''


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Parameter Options
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Options BOOLEAN     = True$, False$
Options WAFER_SIZES = '150 mm 6 in,200 mm 8 in'

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Services
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//----------------------------------------------------------------------------------------------------------------------
// GetWaferCount
//
// Input:
//      SRPFastArrayHandle.   - [Required]
//      Filters.              - [Required]
//
// Output:
//      Response - Returns count of total wafers for the given dataset.
//
//----------------------------------------------------------------------------------------------------------------------
Service GetWaferCount(SRPFastArrayHandle, Filter)
    If Filter EQ '' then
        WaferCount = SRP_FastArray('Count', SRPFastArrayHandle, @FM)
    end else
        ReduceHandle    = SRP_FastArray('Reduce', SRPFastArrayHandle, Filter, 'MatchAll')
        WaferCount      = SRP_FastArray('Count', ReduceHandle, @FM)
    end
    
    Response = WaferCount    
end service


//----------------------------------------------------------------------------------------------------------------------
// GetWaferData
//
// Input:
//      WaferSize   - [Required]
//
// Output:
//      Response - Returns Wafer List for given wafer size.
//
//----------------------------------------------------------------------------------------------------------------------
Service GetWaferData(WaferSize)
    
    Begin Case
        Case WaferSize EQ '150 mm 6 in'
            FullWaferList   = Database_Services('ReadDataRow', 'APP_INFO', 'RETAINED_WAFERS_6IN')
            
        Case WaferSize EQ '200 mm 8 in'
            FullWaferList   = Database_Services('ReadDataRow', 'APP_INFO', 'RETAINED_WAFERS_8IN')
            
    End Case
    
    Response = FullWaferList
end service


//----------------------------------------------------------------------------------------------------------------------
// GetWaferDataWithFilter
//
// Input:
//      WaferSize       - [Required]
//      ShowDestroyed   - [Optional]
//
// Output:
//      Response - Returns Wafer List for given wafer size and filter.
//
//----------------------------------------------------------------------------------------------------------------------
Service GetWaferDataWithFilter(WaferSize, ShowDestroyed)
    
    Begin Case
        Case WaferSize EQ '150 mm 6 in' AND ShowDestroyed EQ False$
            WaferList       = Database_Services('ReadDataRow', 'APP_INFO', 'RETAINED_WAFERS_6IN')
            WaferListHandle = SRP_FastArray('Create', WaferList)
            Locationfilters = 'Warehouse' : @FM : 'Cleanroom'
            WaferList       = Retain_Manager_Services('UpdateRetainList', WaferListHandle, LocationFilters)
            SRP_FastArray('Release', WaferListHandle)
            
        Case WaferSize EQ '200 mm 8 in' AND ShowDestroyed EQ False$
            WaferList       = Database_Services('ReadDataRow', 'APP_INFO', 'RETAINED_WAFERS_8IN')
            WaferListHandle = SRP_FastArray('Create', WaferList)
            Locationfilters = 'Warehouse' : @FM : 'Cleanroom'
            WaferList       = Retain_Manager_Services('UpdateRetainList', WaferListHandle, LocationFilters)
            SRP_FastArray('Release', WaferListHandle)
            
        Case WaferSize EQ '150 mm 6 in' AND ShowDestroyed EQ True$
            WaferList   = Database_Services('ReadDataRow', 'APP_INFO', 'RETAINED_WAFERS_6IN')
            WaferList   = SRP_Array('Rotate', WaferList, @FM, @VM)
            
        Case WaferSize EQ '200 mm 8 in' AND ShowDestroyed EQ True$
            WaferList   = Database_Services('ReadDataRow', 'APP_INFO', 'RETAINED_WAFERS_8IN')
            WaferList   = SRP_Array('Rotate', WaferList, @FM, @VM)
    End Case
    
    Response = WaferList
end service


//----------------------------------------------------------------------------------------------------------------------
// UpdateRetainList
//
// SRPFastArrayHandle. Handle of SRP_FastArray data. 
// SelectedFilters. List of filters to be applied against SRP_FrastArrray data.
// 
// Applies a filter to a SRP_FastArray.
//----------------------------------------------------------------------------------------------------------------------
Service UpdateRetainList(SRPFastArrayHandle, SelectedFilters)
    FilteredArray = ''
    
    If SelectedFilters EQ '' then
        FilteredArray    = SRP_FastArray('GetVariable', SRPFastArrayHandle)
    end else
        For Each Filter in SelectedFilters using @FM
            ReduceHandle    = SRP_FastArray('Reduce', SRPFastArrayHandle, Filter, 'MatchAll')
            FilteredList    = SRP_FastArray('GetVariable', ReduceHandle)
            FilteredArray  := FilteredList : @FM
        Next FilterItem    
    end
    
    FilteredArray[-1, 1]   = ''
    FilteredArray          = SRP_Array('Rotate', FilteredArray, @FM, @VM)
    SRP_FastArray('Release', ReduceHandle)
    Response = FilteredArray
end service


//----------------------------------------------------------------------------------------------------------------------
// GetRetainServiceLastRunTime
// 
// Returns the date and time stamp from when the last time the Update Retain Wafers service ran.
//---------------------------------------------------------------------------------------------------------------------
Service GetRetainServiceLastRunTime()
    LastRunDTM  = Database_Services('ReadDataRow', 'SERVICES', 'Update Retained Wafers')
    LastRunDTM  = OConv(LastRunDTM, 'DT2/^HS')<SERVICES.LAST_RUN_DTM$>
    Response    = LastRunDTM
end service


//----------------------------------------------------------------------------------------------------------------------
// GetRetentionPeriod
// 
// Returns the recorded retention period.
//---------------------------------------------------------------------------------------------------------------------
Service GetRetentionPeriod()
    RetentionPeriod = Database_Services('ReadDataRow', 'APP_INFO', 'RETAINED_WAFERS_RETENTION_PERIOD')
    Response        = RetentionPeriod
end service


//----------------------------------------------------------------------------------------------------------------------
// SetRetentionPeriod
//
// Input:
//      RetentionPeriod       - [Required]
// 
// Records the retention period.
//---------------------------------------------------------------------------------------------------------------------
Service SetRetentionPeriod(RetentionPeriod)
    Status      = Database_Services('WriteDataRow', 'APP_INFO', 'RETAINED_WAFERS_RETENTION_PERIOD', RetentionPeriod)
    Response    = Status
end service


//----------------------------------------------------------------------------------------------------------------------
// GetToBeRetainedCount
//
// Input:
//      WaferSize       - [Required]
//
// Output:
//      Response - Returns Wafer List for given wafer size and filter.
//
//----------------------------------------------------------------------------------------------------------------------
Service GetToBeRetainedCount(WaferSize)
    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]    = ''   
    WaferCount                  = Count(RetainedWaferList, @FM)
    Response                    = WaferCount
    
end service


//----------------------------------------------------------------------------------------------------------------------
// GetToBeDestroyedCount
//
// Input:
//      WaferSize       - [Required]
//
// Output:
//      Response - Returns Wafer List for given wafer size and filter.
//
//----------------------------------------------------------------------------------------------------------------------
Service GetToBeDestroyedCount(WaferSize)
    
    RetainedPeriod      = Database_Services('ReadDataRow', 'APP_INFO', 'RETAINED_WAFERS_RETENTION_PERIOD')
    CurrentDate         = Date()
    DestroyedWaferList  = ''
    FullWaferList       = Retain_Manager_Services('GetWaferData', WaferSize)
    
    For Each Wafer in FullWaferList using @FM setting WaferPos then
        CheckDate   = Wafer<1, RETAIN_WAFER_RETAIN_DATE$> + RetainedPeriod
        Location    = Wafer<1, RETAIN_WAFER_RETAIN_LOC$>
        Status      = Wafer<1, RETAIN_WAFER_STATUS$>
        
        If Location _NEC 'Wafer Destroyed' AND Status _NEC 'Do Not Destroy' then
            If CheckDate LE CurrentDate then
                DestroyedWaferList := Wafer<1, RETAIN_WAFER_RUN_ID$> : @VM : Wafer<1, RETAIN_WAFER_POCKET$>     : @VM : Wafer<1, RETAIN_WAFER_SCRIBE$>      : @VM
                DestroyedWaferList := Wafer<1, RETAIN_WAFER_WO_NO$>  : @VM : Wafer<1, RETAIN_WAFER_RETAIN_BOX$> : @VM : Wafer<1, RETAIN_WAFER_RETAIN_SLOT$> : @VM
                DestroyedWaferList := False$ : @VM : Wafer<1, RETAIN_WAFER_WAFER_ID$> : @FM    
            end
        end
    
    Next Wafer
    
    DestroyedWaferList[-1, 1]   = ''
    WaferCount                  = Count(DestroyedWaferList, @FM)
    Response                    = WaferCount
    
end service


Service GetRetainReport()
    
    WaferData        = Retain_Manager_Services('GetWaferData', '150 mm 6 in')
    WaferDataPrime   = SRP_Array('Rotate', WaferData, @FM, @VM)
    WfrIDs           = WaferDataPrime<RETAIN_WAFER_WAFER_ID$>
    NumWfrs          = DCount(WfrIDs, @VM)
    ReportData       = ''
    Def              = ""
    Def<MCAPTION$>   = "Building Retain Wafer Report..."
    Def<MTYPE$>      = "GC"
    Def<MEXTENT$>    = NumWfrs
    Def<MTEXTWIDTH$> = 600
    MsgUp            = Msg(@WINDOW, Def) ;* Start gas guage message
    
    ReportData<1, -1> = 'GaNRunID'
    ReportData<1, -1> = 'Reactor'           
    ReportData<1, -1> = 'Recipe'            
    ReportData<1, -1> = 'Pocket'               
    ReportData<1, -1> = 'Scribe'               
    ReportData<1, -1> = 'Grade'                
    ReportData<1, -1> = 'PartNo'               
    ReportData<1, -1> = 'WONo'                 
    ReportData<1, -1> = 'RetainBox'            
    ReportData<1, -1> = 'RetainSlot'           
    ReportData<1, -1> = 'RetainLoc'            
    ReportData<1, -1> = 'RetainStatus'
    ReportData<1, -1> = 'NCRNo'
    ReportData<1, -1> = 'RejectType'           
    ReportData<1, -1> = 'CustomerName'         
    ReportData<1, -1> = 'LossDesc'             
    ReportData<1, -1> = 'LossComments'         
    ReportData<1, -1> = 'AssignCauseDesc'      
    ReportData<1, -1> = 'AssignCauseComments'  
    ReportData<1, -1> = 'Shift'                
    ReportData<1, -1> = 'AuthRejSigName'       
    ReportData<1, -1> = 'RDSNo'
    
    For each WfrID in WfrIDs using @VM setting vPos
        Running      = Msg(@WINDOW, MsgUp, vPos, MSGINSTUPDATE$) ;* Update message
        WOWfrRec     = Database_Services('ReadDataRow', 'WO_WFR', WfrID)
        RetainWfrRec = Database_Services('ReadDataRow', 'RETAINED_WAFERS', WfrID)
        RDSNo        = Xlate('WO_WFR', WfrID, 'RDS_NO', 'X')
        ReactRunRec  = Database_Services('ReadDataRow', 'REACT_RUN', RDSNo)
        GaNRunID     = RetainWfrRec<RETAINED_WAFERS.RUN_ID$>
        Reactor      = ReactRunRec<REACT_RUN_REACTOR$>
        Recipe       = RetainWfrRec<RETAINED_WAFERS.RECIPE$>
        Pocket       = RetainWfrRec<RETAINED_WAFERS.POCKET$>
        Scribe       = RetainWfrRec<RETAINED_WAFERS.SCRIBE$>
        Grade        = RetainWfrRec<RETAINED_WAFERS.GRADE$>
        PartNo       = RetainWfrRec<RETAINED_WAFERS.PART_NO$>
        WONo         = Field(WfrID, '*', 1, 1)
        RetainBox    = RetainWfrRec<RETAINED_WAFERS.RETAIN_BOX$>
        RetainSlot   = RetainWfrRec<RETAINED_WAFERS.RETAIN_SLOT$>
        RetainLoc    = RetainWfrRec<RETAINED_WAFERS.LOCATION$>
        RetainStatus = RetainWfrRec<RETAINED_WAFERS.STATUS$>
        NCRNo        = WOWfrRec<WO_WFR_RDS_NCR_NO$>
        If NCRNo NE '' then
            NCRRec              = Database_Services('ReadDataRow', 'NCR', NCRNo)
            RejectType          = Xlate('NCR', NCRNo, 'REJ_TYPE', 'X')
            CustomerName        = Xlate('NCR', NCRNo, 'CUST_NAME', 'X')
            LossDesc            = Xlate('NCR', NCRNo, 'LOSS_DESC', 'X')
            LossComments        = NCRRec<NCR_LOSS_COMMENTS$>
            AssignCauseDesc     = Xlate('NCR', NCRNo, 'AC_DESC', 'X')
            AssignCauseComments = NCRRec<NCR_AC_COMMENTS$>
            Shift               = NCRRec<NCR_SHIFT$>
            AuthRejSigName      = NCRRec<NCR_AUTH_REJ_SIG$>
        end else
            RejectType          = ''
            CustomerName        = ''
            LossDesc            = ''
            LossComments        = ''
            AssignCauseDesc     = ''
            AssignCauseComments = ''
            Shift               = ''
            AuthRejSigName      = ''
        end
        
        ReportData<vPos+1, -1> = GaNRunID
        ReportData<vPos+1, -1> = Reactor              
        ReportData<vPos+1, -1> = Recipe               
        ReportData<vPos+1, -1> = Pocket               
        ReportData<vPos+1, -1> = Scribe               
        ReportData<vPos+1, -1> = Grade                
        ReportData<vPos+1, -1> = PartNo               
        ReportData<vPos+1, -1> = WONo                 
        ReportData<vPos+1, -1> = RetainBox            
        ReportData<vPos+1, -1> = RetainSlot           
        ReportData<vPos+1, -1> = RetainLoc            
        ReportData<vPos+1, -1> = RetainStatus
        ReportData<vPos+1, -1> = NCRNo
        ReportData<vPos+1, -1> = RejectType           
        ReportData<vPos+1, -1> = CustomerName         
        ReportData<vPos+1, -1> = LossDesc             
        ReportData<vPos+1, -1> = LossComments         
        ReportData<vPos+1, -1> = AssignCauseDesc      
        ReportData<vPos+1, -1> = AssignCauseComments  
        ReportData<vPos+1, -1> = Shift                
        ReportData<vPos+1, -1> = AuthRejSigName       
        ReportData<vPos+1, -1> = RDSNo
        
    Next WfrID
    Swap @VM with ',' in ReportData
    Swap @FM with CRLF$ in ReportData
    Msg(@Window,MsgUp)      ;* Take message down
    
    DialogParams = True$ : @FM : '' : @FM : '' : @FM : 'Retained 6 in with NCR.csv'
    SaveLocation = Utility('CHOOSEFILE', @Window, DialogParams)
    If SaveLocation NE '' then
        OSWrite ReportData on SaveLocation
        RunCommand = 'explorer.exe /select,"' : SaveLocation : '"'
        Output     = SRP_Run_Command(RunCommand, 'DOS')
    end
    
end service

