Compile function Calibration_Services(@Service, @Params)
#pragma precomp SRP_PreCompiler

$Insert LOGICAL
$Insert SERVICE_SETUP
$Insert CALIB_LIST_EQUATES

EQU COL$CL_NO			TO 1
EQU COL$DEPT			TO 2
EQU COL$EQ_INSTRUMENT	TO 3
EQU COL$EQ_SN			TO 4
EQU COL$EQ_LOC			TO 5
EQU COL$EQ_CAL_INT		TO 6
EQU COL$EQ_INT_EXT		TO 7
EQU COL$EQ_LAST_CAL		TO 8
EQU COL$EQ_NEXT_CAL		TO 9

EQU COL$NIST_STANDARD	TO 3
EQU COL$NIST_TSN		TO 4
EQU COL$NIST_LOC		TO 5
EQU COL$NIST_CAL_INT	TO 6
EQU COL$NIST_INT_EXT	TO 7
EQU COL$NIST_LAST_CAL	TO 8
EQU COL$NIST_NEXT_CAL	TO 9

GoToService

Return Response or ""

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Parameter Options
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Options BOOLEAN     = True$, False$
Options CALIB_TYPES = 'Equipment', 'NIST'

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SERVICES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Service GetCalibListData(CalibType=CALIB_TYPES, SelectActive=BOOLEAN)
    
    If SelectActive EQ '' then SelectActive = False$
    CalibListData = ''
    ErrorMsg      = ''
    Begin Case
        Case CalibType EQ 'Equipment'
            
            OPEN 'CALIB_LIST' TO TableIn then
                 
                If SelectActive EQ False$ then
                    SELECT TableIn
*                end else
*                    Select 'CALIB_LIST' By "" Using Cursor else
*                        ErrorMsg = 'Error in ':Service:' service. Error selecting CALIB_LIST records.'
*                    end
                end
                
                If ErrorMsg EQ '' then 

                    EquipList  = ''
                    EquipDescs = ''
                    Done       = False$
                    Today      = Date() 

                    LOOP 

                    READNEXT CLNo ELSE Done = True$

                    UNTIL Done
                        READ CalRec FROM TableIn,CLNo THEN
                        
                            NextCalDt = XLATE('CALIB_LIST',CLNo,'NEXT_CAL_DT','X')
                            
                            IF CalRec<CALIB_LIST_CL_TYPE$> = 'E' THEN
                        
                                EquipDesc	= CalRec<CALIB_LIST_EQ_DESC$>
                                Dept		= CalRec<CALIB_LIST_DEPT$>
                                
                                IF EquipDesc NE '' THEN
                                
                                    SortKey = Dept:'*':EquipDesc:'*':CLNo
                                    
                                    LOCATE SortKey IN EquipDescs BY 'AL' SETTING Pos ELSE
                                    
                                        EquipDescs = INSERT(EquipDescs,1,Pos,0,SortKey)
                                        
                                        EquipLine = CLNo
                                        EquipLine<1,COL$DEPT>			= Dept
                                        EquipLine<1,COL$EQ_INSTRUMENT>	= CalRec<CALIB_LIST_EQ_DESC$>
                                        EquipLine<1,COL$EQ_SN>			= CalRec<CALIB_LIST_EQ_SN$>
                                        EquipLine<1,COL$EQ_LOC>			= CalRec<CALIB_LIST_EQ_LOC$>
                                        EquipLine<1,COL$EQ_CAL_INT>		= CalRec<CALIB_LIST_CAL_INTERVAL$> 
                                        EquipLine<1,COL$EQ_INT_EXT>		= CalRec<CALIB_LIST_CAL_LOC$> 
                                        EquipLine<1,COL$EQ_LAST_CAL>	= OCONV(XLATE('CALIB_LIST',CLNo,'LAST_CAL_DT','X'),'D4/')
                                        EquipLine<1,COL$EQ_NEXT_CAL>	= OCONV(NextCalDt,'D4/')
                                        
                                        EquipList	= INSERT(EquipList,Pos,0,0,EquipLine)
                                        
                                    END	;* End of LOCATE SortKey
                                    
                                END	;* End of check for Null EquipDesc
                                
                            END;* End of check for CL_TYPE = 'E'
                            
                        END	;* End of CalRec read
                    REPEAT
                    CalibListData = EquipList
                end
            end else
                ErrorMsg = 'Error in ':Service:' service. Unable to open "CALIB_LIST" table.'
            end
                
        Case CalibType EQ 'NIST'
            
            OPEN 'CALIB_LIST' TO TableIn then
                 
                If SelectActive EQ False$ then 
                    SELECT TableIn
*                end else
*                    Select 'CALIB_LIST' By "" Using Cursor else
*                        ErrorMsg = 'Error in ':Service:' service. Error selecting CALIB_LIST records.'
*                    end
                end
                
                If ErrorMsg EQ '' then 

                    NISTList = ''
                    StdDescs = ''
                    Done     = False$
                    Today    = Date() 

                    LOOP 

                    READNEXT CLNo ELSE Done = True$

                    UNTIL Done
                        READ CalRec FROM TableIn,CLNo THEN
                        
                            NextCalDt = XLATE('CALIB_LIST',CLNo,'NEXT_CAL_DT','X')
                                               
                            IF CalRec<CALIB_LIST_CL_TYPE$> = 'S' THEN
                                StdDesc = CalRec<CALIB_LIST_STD_DESC$>
                                Dept	= CalRec<CALIB_LIST_DEPT$>
                                
                                IF StdDesc NE '' THEN
                            
                                    SortKey = Dept:'*':StdDesc:'*':CLNo
                                    
                                    LOCATE SortKey IN StdDescs BY 'AL' SETTING Pos ELSE
                                    
                                        StdDescs = INSERT(StdDescs,1,Pos,0,SortKey)
                                        
                                        NISTLine = CLNo
                                        NISTLine<1,COL$DEPT>			= CalRec<CALIB_LIST_DEPT$>
                                        NISTLine<1,COL$NIST_STANDARD>	= CalRec<CALIB_LIST_STD_DESC$>
                                        NISTLine<1,COL$NIST_TSN>		= CalRec<CALIB_LIST_STD_SN$>
                                        NISTLine<1,COL$NIST_LOC>		= CalRec<CALIB_LIST_STD_LOC$>
                                        NISTLine<1,COL$NIST_CAL_INT>	= CalRec<CALIB_LIST_CAL_INTERVAL$>
                                        NISTLine<1,COL$NIST_INT_EXT>	= CalRec<CALIB_LIST_CAL_LOC$>
                                        NISTLine<1,COL$NIST_LAST_CAL>	= OCONV(XLATE('CALIB_LIST',CLNo,'LAST_CAL_DT','X'),'D4/') 
                                        NISTLine<1,COL$NIST_NEXT_CAL>	= OCONV(NextCalDt,'D4/') 
                                        
                                        NISTList = INSERT(NISTList,Pos,0,0,NISTLine)
                                        
                                    END	;* End of LOCATE SortKey
                                    
                                END	;* End of check for Null StdDesc
                            
                            END	;* End of check for CL_TYPE = 'S'
                        END	;* End of CalRec read
                    REPEAT
                    CalibListData = NistList
                end 
            end else
                ErrorMsg = 'Error in ':Service:' service. Unable to open "CALIB_LIST" table.'
            end

        Case Otherwise$
            ErrorMsg = 'Error in ':Service:' service. Null CalibType passed into service'
    End Case
    
    If ErrorMsg NE '' then Error_Services('Add', ErrorMsg)
    Response = CalibListData
    
End Service


