Compile function SPC_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 Infineon

    Name        :   SPC_Services

    Description :   Handler program for all module related services.

    Notes       :   The generic parameters should contain all the necessary information to process the services. Often
                    this will be information like the data Record and Key ID.

    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)
        10/24/18    djs     Original programmer. - [EPIOI-8]

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

$Insert APP_INSERTS
$Insert SERVICE_SETUP
$Insert SPC_QUEUE_EQUATES
$Insert RLIST_EQUATES
$Insert RDS_LAYER_EQUATES
$Insert DICT_EQUATES

Declare function Database_Services, Environment_Services, Status, Error_Services, Get_Status
Declare subroutine Database_Services, Set_Status, Error_Services, RList, Get_Status

GoToService

Return Response or ""

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

//----------------------------------------------------------------------------------------------------------------------
// ProcessQueue
//
// Checks the SPC queue to see if any documents need to be written to the SPC directory located within the EC domain. 
//----------------------------------------------------------------------------------------------------------------------
Service ProcessQueue()

    hSysLists   = Database_Services('GetTableHandle', 'SYSLISTS')
    Lock hSysLists, ServiceKeyID then
        SPCDir    = Environment_Services('GetSPCDataPath')
        hSPCQueue = Database_Services('GetTableHandle', 'SPC_QUEUE')
        If Error_Services('NoError') then
            Set_Status(0)
            SelectStatement = 'SELECT SPC_QUEUE'
            RList(SelectStatement, 5)
            StatusCode      = ''
            If Get_Status(StatusCode) Then
                Null
            end else
                EOF = False$
                Loop
                    Readnext SPCQueueID else EOF = True$
                Until EOF
                    Read QPCQueueRec from hSPCQueue, SPCQueueID then
                        Filename = QPCQueueRec<SPC_QUEUE.SPC_FILENAME$>
                        SPCDoc   = QPCQueueRec<SPC_QUEUE.SPC_DOC$>
                        FullDir  = SPCDir:'\':Filename
                        Set_Status(0)
                        OSWrite SPCDoc on FullDir
                        If Get_Status(StatusCode) then
                            ErrMsg  = 'Error writing ':Filename:' to ':SPCDir:'. Error code: ':StatusCode
                            Error_Services('Add', ErrMsg)
                        end else
                            Delete hSPCQueue, SPCQueueID else
                                ErrCode = Status()
                                ErrMsg  = 'Error deleting SPC Record ':SPCQueueID:'from queue. Error code: ':ErrCode
                                Error_Services('Add', ErrMsg)
                            end
                        end                                             
                    end
                Repeat                
            end           
        end
        ClearSelect 0

        Unlock hSysLists, ServiceKeyID else Null
    end

end service


//----------------------------------------------------------------------------------------------------------------------
// PostToQueue
//
// Posts an SPC document to the SPC Queue for processing on the EC server. 
//----------------------------------------------------------------------------------------------------------------------
Service PostToQueue(Tablename, KeyID)

    Username = @USER4
    
    Begin Case
        Case Tablename _EQC 'RDS_TEST'
            
            Filename = 'SPC_Data.txt'        
            SPCQueueKey = Date():'*':Time():'*':Username
            DupRequest  = RowExists('SPC_QUEUE', SPCQueueKey)
            If DupRequest then return
            SPCQueueRec = ''
            SPCQueueRec<SPC_QUEUE.SPC_FILENAME$> = Filename
            Database_Services('ActivateRecord', Tablename, KeyID)
            
            DataLine = ''
            PSNo			    = {PS_NO}
            RDSNo			    = {RDS_NO}
            LSId			    = {LS_ID}
            Reactor			    = {REACTOR}
            Zone			    = {ZONE}
            ThickAvg		    = OConv({THICK_AVG}, 'MD2')
            ThickStdv		    = OConv({THICK_STDV}, 'MD4')
            SheetRhoAvg		    = OConv({SHEETRHO_AVG}, 'MD3')
            SheetRhoStdv	    = OConv({SHEETRHO_STDV}, 'MD4')
            ResAvg			    = OConv({RES_AVG}, 'MD4')
            ResStdv			    = Oconv({RES_STDV}, 'MD4')
            HgcvAvg			    = OConv({HGCV1_RES_AVG}, 'MD3')
            HgCvStdv		    = OConv({HGCV1_RES_STDV}, 'MD3')
            GrowthRate		    = OConv({GROWTH_RATE}, 'MD2')
            ThickRange			= OConv({THICK_RANGE}, 'MD2')
            ThickRangePcnt		= OConv({THICK_RANGE_PCNT}, 'MD4')
            SheetRhoRange		= OConv({SHEETRHO_RANGE}, 'MD3')
            SheetRhoRangePcnt	= OConv({SHEETRHO_RANGE_PCNT}, 'MD4')
            ResRange			= OConv({RES_RANGE}, 'MD4')
            ResRangePcnt		= OConv({RES_RANGE_PCNT}, 'MD4')
            Hgcv1ResRange		= OConv({HGCV1_RES_RANGE}, 'MD3')
            Hgcv1ResRangePcnt	= OConv({HGCV1_RES_RANGE_PCNT}, 'MD4')

            CONVERT '%' TO '' IN ThickRangePcnt
            CONVERT '%' TO '' IN SheetRhoRangePcnt
            CONVERT '%' TO '' IN ResRangePcnt
            CONVERT '%' TO '' IN Hgcv1ResRangePcnt
            
            IF NUM(LSId) THEN
                DepositTime = ''
                DopantFlow = ''
                EpiH2Flow = ''
                FOffset = ''
                ROffset= ''
                SOffset = ''
                TcsFlow = ''
            END ELSE

                LayerRec = XLATE('RDS_LAYER',RDSNo:'*':LSId,'','X')
                DictInfo = XLATE('DICT.RDS_LAYER','%FIELDS%','','X')              

                LOCATE RDS_LAYER_EPI_TIME$ IN DictInfo<FIELDS_FIELD_NO$> SETTING Pos THEN
                    DepositTime	= OCONV(LayerRec<RDS_LAYER_EPI_TIME$>,DictInfo<FIELDS_CONV$,Pos>)
                END ELSE
                    DepositTime	= LayerRec<RDS_LAYER_EPI_TIME$>
                END

                LOCATE RDS_LAYER_DOPANT_FLOW$ IN DictInfo<FIELDS_FIELD_NO$> SETTING Pos THEN
                    DopantFlow	= OCONV(LayerRec<RDS_LAYER_DOPANT_FLOW$>,DictInfo<FIELDS_CONV$,Pos>)
                END ELSE
                    DopantFlow	= LayerRec<RDS_LAYER_DOPANT_FLOW$>
                END

                LOCATE RDS_LAYER_EPI_H2_FLOW$ IN DictInfo<FIELDS_FIELD_NO$> SETTING Pos THEN
                    EpiH2Flow	= OCONV(LayerRec<RDS_LAYER_EPI_H2_FLOW$>,DictInfo<FIELDS_CONV$,Pos>)
                END ELSE
                    EpiH2Flow	= LayerRec<RDS_LAYER_EPI_H2_FLOW$>
                END

                LOCATE RDS_LAYER_F_OFFSET$ IN DictInfo<FIELDS_FIELD_NO$> SETTING Pos THEN
                    FOffset		=  OCONV(LayerRec<RDS_LAYER_F_OFFSET$>,DictInfo<FIELDS_CONV$,Pos>)
                END ELSE
                    FOffset		= LayerRec<RDS_LAYER_F_OFFSET$>
                END

                LOCATE RDS_LAYER_R_OFFSET$ IN DictInfo<FIELDS_FIELD_NO$> SETTING Pos THEN
                    ROffset		=  OCONV(LayerRec<RDS_LAYER_R_OFFSET$>,DictInfo<FIELDS_CONV$,Pos>)
                END ELSE
                    ROffset		= LayerRec<RDS_LAYER_R_OFFSET$>
                END

                LOCATE RDS_LAYER_S_OFFSET$ IN DictInfo<FIELDS_FIELD_NO$> SETTING Pos THEN
                    SOffset		=  OCONV(LayerRec<RDS_LAYER_S_OFFSET$>,DictInfo<FIELDS_CONV$,Pos>)
                END ELSE
                    SOffset		= LayerRec<RDS_LAYER_S_OFFSET$>
                END

                LOCATE RDS_LAYER_TCS_FLOW$ IN DictInfo<FIELDS_FIELD_NO$> SETTING Pos THEN
                    TcsFlow		=  OCONV(LayerRec<RDS_LAYER_TCS_FLOW$>,DictInfo<FIELDS_CONV$,Pos>)
                END ELSE
                    TcsFlow		= LayerRec<RDS_LAYER_TCS_FLOW$>
                END
            END	;* End of check for Numeric Layer Set ID

            UserName = OCONV(Username,'[XLATE_CONV,LSL_USERS*FIRST_LAST]')

            DataLine  = QUOTE(PSNo):TAB$
            DataLine := QUOTE(RDSNo):TAB$
            DataLine := QUOTE(LSId):TAB$
            DataLine := QUOTE(Reactor):TAB$
            DataLine := QUOTE(Zone):TAB$
            DataLine := QUOTE(ThickAvg):TAB$
            DataLine := QUOTE(ThickStdv):TAB$
            DataLine := QUOTE(SheetRhoAvg):TAB$
            DataLine := QUOTE(SheetRhoStdv):TAB$
            DataLine := QUOTE(ResAvg):TAB$
            DataLine := QUOTE(ResStdv):TAB$
            DataLine := QUOTE(HgcvAvg):TAB$
            DataLine := QUOTE(HgcvStdv):TAB$
            DataLine := QUOTE(GrowthRate):TAB$
            DataLine := QUOTE(DepositTime):TAB$
            DataLine := QUOTE(DopantFlow):TAB$
            DataLine := QUOTE(EpiH2Flow):TAB$
            DataLine := QUOTE(FOffset):TAB$
            DataLine := QUOTE(ROffset):TAB$
            DataLine := QUOTE(SOffset):TAB$
            DataLine := QUOTE(TcsFlow):TAB$
            DataLine := QUOTE(UserName):TAB$
            DataLine := QUOTE(ThickRange):TAB$
            DataLine := QUOTE(ThickRangePcnt):TAB$
            DataLine := QUOTE(SheetRhoRange):TAB$
            DataLine := QUOTE(SheetRhoRangePcnt):TAB$
            DataLine := QUOTE(ResRange):TAB$
            DataLine := QUOTE(ResRangePcnt):TAB$
            DataLine := QUOTE(Hgcv1ResRange):TAB$
            DataLine := QUOTE(Hgcv1ResRangePcnt):TAB$
            
            SPCQueueRec<SPC_QUEUE.SPC_DOC$>      = DataLine:CRLF$

            Database_Services('WriteDataRow', 'SPC_QUEUE', SPCQueueKey, SPCQueueRec, True$, False$, True$)
            
        Case Tablename _EQC 'NCR'
            
            Filename = 'NCR_Data.txt'
            SPCQueueKey = Date():'*':Time():'*':@USER4
            DupRequest  = RowExists('SPC_QUEUE', SPCQueueKey)
            If DupRequest then return
            SPCQueueRec = ''
            SPCQueueRec<SPC_QUEUE.SPC_FILENAME$> = Filename
            Database_Services('ActivateRecord', TableName, KeyID)
            DataLine = ''
            
            NCRNo 		= KeyID
            CustName	= {CUST_NAME}
            PSNo		= {PROD_SPEC_NO}
            RDSIDs		= {RDS_ID}
            RDSReactors = {REACTOR}
            RejCnt      = {REJ_CNT}
            DeptResp	= {DEPT_RESP}
            LossCode	= {LOSS_CODE}
            LossDesc	= {LOSS_DESC}
            LossStage	= {LOSS_STAGE}
            LossBy		= {LOSS_BY}
            ACCode		= {AC_CODE}
            ACDesc		= {AC_DESC}
            ReactorType = {REACTOR_TYPE}

            RDSCnt = DCount(RDSIDs, @VM)

            FOR RDSIndex = 1 TO RDSCnt

                DataLine  = QUOTE(NCRNo):TAB$
                DataLine := QUOTE(RDSIDs<1, RDSIndex>):TAB$
                
                IF ReactorType = 'P' OR ReactorType = 'EPP' THEN
                    * EpiPRO reactors in SPC have "EP" on the front.
                    DataLine := QUOTE('EP':RDSReactors<1, RDSIndex>):TAB$	
                END ELSE
                    DataLine := QUOTE(RDSReactors<1, RDSIndex>):TAB$
                END
                
                DataLine := QUOTE(RejCnt<1, RDSIndex>):TAB$
                DataLine := QUOTE(PSNo):TAB$
                DataLine := QUOTE(DeptResp):TAB$
                DataLine := QUOTE(LossCode):TAB$
                DataLine := QUOTE(LossDesc):TAB$
                DataLine := QUOTE(LossStage):TAB$
                DataLine := QUOTE(LossBy):TAB$
                DataLine := QUOTE(ACCode):TAB$
                DataLine := QUOTE(ACDesc):TAB$
                
            Next RDSIndex
                     
            SPCQueueRec<SPC_QUEUE.SPC_DOC$> = DataLine:CRLF$
            Database_Services('WriteDataRow', 'SPC_QUEUE', SPCQueueKey, SPCQueueRec, True$, False$, True$)
            
    End Case

end service


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

