Compile function Calibration_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 Technologies.

    Name        :   Calibration_Services

    Description :   Handler program for all Scan services.

    Notes       :   

    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)
        12/03/2024    djs     Added SendReminders service

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

$Insert APP_INSERTS
$Insert SERVICE_SETUP
$Insert CALIB_LIST_EQUATES
$Insert POPUP_EQUATES
$Insert RLIST_EQUATES
$Insert NOTIFICATION_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

Declare subroutine obj_Notes
Declare function   SRP_Array, Database_Services

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


Service SendReminders()
	
	hSysLists = Database_Services('GetTableHandle', 'SYSLISTS')
	Lock hSysLists, ServiceKeyID then	
		ErrorMsg  = ''
		OPEN 'CALIB_LIST' TO FileIn then
			
			CalDueRecipients = Xlate('NOTIFICATION', 'CALIB_DUE_NOTIFY', NOTIFICATION_USER_ID$, 'X')
			
			CheckDt    = OConv(Date() + 15, 'D4/')
			SelectSent = 'SELECT CALIB_LIST WITH NEXT_CAL_DT < ':QUOTE(CheckDt):' AND WITH EQ_LOC NE "Out of Service" BY NEXT_CAL_DT'
			RList(SelectSent,TARGET_ACTIVELIST$,'','','')

			NoteText = ''
			Depts = ''

			Done = 0
			LOOP
				READNEXT CLNo ELSE Done = 1
			UNTIL Done
				READ CLRec FROM FileIn,CLNo THEN
				
					NextCalDt = OCONV(XLATE('CALIB_LIST',CLNo,'NEXT_CAL_DT','X'),'D4/')
					
					IF NextCalDt NE '' THEN
					
						NewLine = ''
						CLType	= CLRec<CALIB_LIST_CL_TYPE$>
						Dept	= CLRec<CALIB_LIST_DEPT$>
						IF Dept = '' THEN Dept = 'MET'
						
						IF CLType = 'E' THEN
							Desc	= CLRec<CALIB_LIST_EQ_DESC$>
							SN		= CLRec<CALIB_LIST_EQ_SN$>
							Loc		= CLRec<CALIB_LIST_EQ_LOC$>
							NewLine = Desc:' (S/N: ':SN:') located in the ':Loc:' is due for calibration on ':NextCalDt:CRLF$
						END 
						IF CLType = 'S' THEN
							Desc	= CLRec<CALIB_LIST_STD_DESC$>
							SN		= CLRec<CALIB_LIST_STD_SN$>
							Loc		= CLRec<CALIB_LIST_STD_LOC$>
							NewLine	= 'The NIST Standard ':Desc:' (S/N: ':SN:') located in the ':Loc:' is due for calibration on ':NextCalDt:CRLF$
						END
						
						LOCATE Dept IN Depts USING @FM SETTING Pos ELSE
							Depts = INSERT(Depts,Pos,0,0,Dept)
						END
						NoteText<Pos> = NoteText<Pos>:NewLine
						
					END	;* End of check for NextCalDt
				END	;* End of CLRec read
			REPEAT

			DeptUsers = ''
			DeptDescs =''
			 
			PopupLiteral = XLATE('SYSREPOSPOPUPS',@APPID<1>:'**DEPT',PDISPLAY$,'X')

			CONVERT @VM:@SVM TO @FM:@VM IN PopupLiteral

			LiteralCnt = COUNT(PopupLiteral,@FM) + (PopupLiteral NE '')

			FOR I = 1 TO LiteralCnt
				Dept = PopupLiteral<I,1>
				DeptDesc = PopupLiteral<I,2>
				UserNames = PopupLiteral<I,3>
				LOCATE Dept IN Depts USING @FM SETTING Pos THEN
					CONVERT ' ' TO '' IN UserNames
					CONVERT ',' TO @VM IN UserNames
					DeptUsers<Pos> = UserNames
					DeptDescs<Pos> = DeptDesc
				END
			NEXT I

			NoteSubject = "Equipment Calibration Reminder"

			DeptCnt = DCount(Depts,@FM)

			FOR I = 1 TO DeptCnt

				IF NoteText<I> NE '' THEN
					
					Recipients   = SRP_Array('Join', CalDueRecipients, DeptUsers<I>, 'AND', @VM)
					SentFrom     = 'System'
					Subject      = DeptDescs<I>:" Department - Equipment Calibration Reminder"
					Message      = NoteText<I>
					AttachWindow = 'MASTER_CALIB_LIST'
					AttachKeys   = ''
					SendToGroup  = ''
						
					Obj_Notes('Create',Recipients:@RM:'System':@RM:NoteSubject:@RM:NoteText<I>:@RM:'MASTER_CALIB_LIST')
					
				END

			NEXT I	
		end else
			ErrorMsg = 'Unable to open "CALIB_LIST" table.'
		end
		
		If ErrorMsg NE '' then
			Error_Services('Add', ErrorMsg)
		end
		Unlock hSysLists, ServiceKeyID else Null
	end
	
end service

