UPDATE TBDD_MODULES SET DB_VERSION = '3.2' where NAME = 'Process-Manager';
GO



ALTER TABLE dbo.TBPM_MONITOR_KONFIG
ADD COCKPIT_ID BIGINT NOT NULL DEFAULT ((0));
GO



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- [FNTF_GET_COCKPIT_DEFINITION]
-- =================================================================
-- Returns a table with user allocated cockpits
-- 
-- Returns: TABLE
-- =================================================================
-- Copyright (c) 2026 by Digital Data GmbH
--
-- Digital Data GmbH • Ludwig-Rinn-Strasse 16 • D-35452 Heuchelheim
-- Tel.: 0641/202360 • E-Mail: info-flow@digitaldata.works
-- =================================================================
-- Creation Date / Author:    23.04.2026 / MK
-- Version Date / Editor:     02.06.2026 / MK
-- Version Number:            1.1.0.0
-- =================================================================
-- History:
-- 23.04.2026 / MK - First Version
-- 02.06.2026 / MK - Inaktive TF Profile werden gefiltert

CREATE OR ALTER FUNCTION [dbo].[FNTF_GET_COCKPIT_DEFINITION](
	@pUSER_ID		BIGINT,		                -- GUID from [TBDD_USER]
    @pLANGUAGE		NVARCHAR(10)	= NULL		-- OPTIONAL: Set Language if you want to Override setting in Profile table. Failsafe is getting data from [TBDD_USERS]
)
RETURNS @RESULT TABLE ( [PK_RESULT_ID]                          [bigint]  Identity(1,1)     NOT NULL,
                        [PK_CFG_COCKPIT_DEFINITION_ID]          [bigint]                    NOT NULL,
	                    [FK_CFG_COCKPIT_ID]                     [bigint]                    NOT NULL,
                        [PARENT_ID]                             [bigint]                    NOT NULL,
                        [MAP_SEQUENCE]                          [smallint]                  NULL,
	                    [ACTIVE]                                [bit]                       NOT NULL,
	                    [TYPE_ID]                               [tinyint]                   NOT NULL,
	                    [ICON_INDEX]                            [smallint]                  NOT NULL,
	                    [NAME]                                  [nvarchar](150)             NULL,
                        [FK_PROFILE_ID]                         [bigint]                    NULL,
	                    [FK_CHART_ID]                           [bigint]                    NULL,
	                    [FK_MONITOR_ID]                         [bigint]                    NULL,
	                    [SQL_QUERY]                             [nvarchar](max)             NOT NULL,
	                    [SQL_QUERY_COUNT]                       [nvarchar](max)             NULL,
	                    [COMMENT]                               [nvarchar](500)             NULL,
	                    [ADDED_WHO]                             [nvarchar](50)              NOT NULL,
	                    [ADDED_WHEN]                            [datetime] NOT              NULL,
	                    [CHANGED_WHO]                           [nvarchar](50)              NULL,
	                    [CHANGED_WHEN]                          [datetime]                  NULL)
AS
BEGIN

	--=========================================-- Parameter copies (avoid sniffing) --========================================--
	DECLARE @USER_ID	BIGINT	        = @pUSER_ID,
            @LANGUAGE	NVARCHAR(10)	= @pLANGUAGE;
	----------------------------------------------------------------------------------------------------------------------------


    --===============================================-- declare runtime vars --===============================================--
    DECLARE @MY_FUNCTION_NAME				NVARCHAR(128)	= OBJECT_NAME(@@PROCID);
	DECLARE	@MODULE_SHORT_NAME              NVARCHAR(10)    = 'PM';
    ----------------------------------------------------------------------------------------------------------------------------


	--===================================================-- Failsafe settings  --====================================================--
	SET @LANGUAGE =	
		CASE
			WHEN ((LEN(@LANGUAGE) = 0) AND (SELECT count([LANGUAGE]) FROM [TBDD_USER] WITH (NOLOCK) WHERE [GUID] = @USER_ID) = 1)
			THEN (SELECT [LANGUAGE] FROM [TBDD_USER] WITH (NOLOCK) WHERE [GUID] = @USER_ID)
			ELSE @LANGUAGE
		END;
	-----------------------------------------------------------------------------------------------------------------------------------


    --===============================================-- get user allocated cockpits --===============================================--
    INSERT INTO @RESULT([PK_CFG_COCKPIT_DEFINITION_ID],
                        [FK_CFG_COCKPIT_ID],
                        [PARENT_ID],
                        [MAP_SEQUENCE],
                        [ACTIVE],
                        [TYPE_ID],
                        [ICON_INDEX],
                        [NAME],
                        [FK_PROFILE_ID],
	                    [FK_CHART_ID],
	                    [FK_MONITOR_ID],
                        [SQL_QUERY],
                        [SQL_QUERY_COUNT],
                        [COMMENT],
                        [ADDED_WHO],
                        [ADDED_WHEN],
                        [CHANGED_WHO],
                        [CHANGED_WHEN])               

    SELECT DISTINCT TOP 100 PERCENT
                        [CCP].[PK_CFG_COCKPIT_DEFINITION_ID],
                        [CCP].[FK_CFG_COCKPIT_ID],
                        [CCP].[PARENT_ID],
                        [MC].[SEQUENCE],
                        [CCP].[ACTIVE],
                        [CCP].[TYPE_ID],
                        [CCP].[ICON_ID] ,
                        CASE
                            WHEN ((SELECT COUNT(1) FROM [dbo].[TBDD_GUI_LANGUAGE_PHRASE] WITH (NOLOCK) WHERE [MODULE] IN (@MODULE_SHORT_NAME) AND [LANGUAGE] = @LANGUAGE AND [TITLE] = [CCP].[NAME]) = 1)
                            THEN (SELECT [STRING1] FROM [dbo].[TBDD_GUI_LANGUAGE_PHRASE] WITH (NOLOCK) WHERE [MODULE] IN (@MODULE_SHORT_NAME) AND [LANGUAGE] = @LANGUAGE AND [TITLE] = [CCP].[NAME])

                            ELSE [CCP].[NAME]
                        END as [NAME],
                        [CCP].[FK_PROFILE_ID],
	                    [CCP].[FK_CHART_ID],
	                    [CCP].[FK_MONITOR_ID],
                        [CCP].[SQL_QUERY],
                        [CCP].[SQL_QUERY_COUNT],
                        [CCP].[COMMENT],
                        [CCP].[ADDED_WHO],
                        [CCP].[ADDED_WHEN],
                        [CCP].[CHANGED_WHO],
                        [CCP].[CHANGED_WHEN]

      FROM              [dbo].[TBTF_CFG_COCKPIT_DEFINITION] as [CCP] WITH (NOLOCK) 
      INNER JOIN        [dbo].[TBTF_CFG_COCKPIT]            as [CC]  WITH (NOLOCK) ON  [CCP].[FK_CFG_COCKPIT_ID] = [CC].[PK_CFG_COCKPIT_ID]
      LEFT  JOIN        [dbo].[TBTF_MAP_COCKPIT]            as [MC]  WITH (NOLOCK) ON  [CCP].[FK_CFG_COCKPIT_ID] = [MC].[FK_CFG_COCKPIT_ID] 
                                                                                   AND [MC].[FK_CFG_USER_ID]     = @USER_ID
      LEFT  JOIN        [dbo].[TBPM_PROFILE]                as [TFP] WITH (NOLOCK) ON  [CCP].[FK_PROFILE_ID]     = [TFP].[GUID]

      WHERE           (([CC].[ACTIVE] IN (0,1) AND [TYPE_ID] = 0)                          -- Overview
      OR               ([CC].[ACTIVE] IN (0,1) AND [TYPE_ID] = 1 AND [TFP].[ACTIVE] = 1)   -- Profile
      OR               ([CC].[ACTIVE] IN (0,1) AND [TYPE_ID] = 2)                          -- Monitor 
      OR               ([CC].[ACTIVE] IN (0,1) AND [TYPE_ID] = 3))                         -- Chart
      
      AND               [CCP].[FK_CFG_COCKPIT_ID] NOT IN (SELECT [FK_CFG_COCKPIT_ID] FROM @RESULT) -- Allow no duplicates
                        
      ORDER BY          [MC].[SEQUENCE] ASC, [CCP].[PARENT_ID] ASC, [CCP].[TYPE_ID] ASC;
    -----------------------------------------------------------------------------------------------------------------------------------


    --===============================================-- get group allocated cockpits --==============================================--
    INSERT INTO @RESULT([PK_CFG_COCKPIT_DEFINITION_ID],
                        [FK_CFG_COCKPIT_ID],
                        [PARENT_ID],
                        [MAP_SEQUENCE],
                        [ACTIVE],
                        [TYPE_ID],
                        [ICON_INDEX],
                        [NAME],
                        [FK_PROFILE_ID],
	                    [FK_CHART_ID],
	                    [FK_MONITOR_ID],
                        [SQL_QUERY],
                        [SQL_QUERY_COUNT],
                        [COMMENT],
                        [ADDED_WHO],
                        [ADDED_WHEN],
                        [CHANGED_WHO],
                        [CHANGED_WHEN])               

    SELECT DISTINCT TOP 100 PERCENT
                        [CCP].[PK_CFG_COCKPIT_DEFINITION_ID],
                        [CCP].[FK_CFG_COCKPIT_ID],
                        [CCP].[PARENT_ID],
                        [MC].[SEQUENCE],
                        [CCP].[ACTIVE],
                        [CCP].[TYPE_ID],
                        [CCP].[ICON_ID] ,
                        CASE
                            WHEN ((SELECT COUNT(1) FROM [dbo].[TBDD_GUI_LANGUAGE_PHRASE] WITH (NOLOCK) WHERE [MODULE] IN (@MODULE_SHORT_NAME) AND [LANGUAGE] = @LANGUAGE AND [TITLE] = [CCP].[NAME]) = 1)
                            THEN (SELECT [STRING1] FROM [dbo].[TBDD_GUI_LANGUAGE_PHRASE] WITH (NOLOCK) WHERE [MODULE] IN (@MODULE_SHORT_NAME) AND [LANGUAGE] = @LANGUAGE AND [TITLE] = [CCP].[NAME])

                            ELSE [CCP].[NAME]
                        END as [NAME],
                        [CCP].[FK_PROFILE_ID],
	                    [CCP].[FK_CHART_ID],
	                    [CCP].[FK_MONITOR_ID],
                        [CCP].[SQL_QUERY],
                        [CCP].[SQL_QUERY_COUNT],
                        [CCP].[COMMENT],
                        [CCP].[ADDED_WHO],
                        [CCP].[ADDED_WHEN],
                        [CCP].[CHANGED_WHO],
                        [CCP].[CHANGED_WHEN]

      FROM              [dbo].[TBTF_CFG_COCKPIT_DEFINITION] as [CCP] WITH (NOLOCK) 
      INNER JOIN        [dbo].[TBTF_CFG_COCKPIT]            as [CC]  WITH (NOLOCK) ON  [CCP].[FK_CFG_COCKPIT_ID] = [CC].PK_CFG_COCKPIT_ID
      LEFT  JOIN        [dbo].[TBTF_MAP_COCKPIT]            as [MC]  WITH (NOLOCK) ON  [CCP].[FK_CFG_COCKPIT_ID] = [MC].[FK_CFG_COCKPIT_ID] 
                                                                                   AND [MC].[FK_CFG_GROUP_ID] IN (SELECT DISTINCT [GROUP_ID]
                                                                                                                  FROM  [dbo].[TBDD_GROUPS_USER] WITH (NOLOCK)
                                                                                                                  WHERE [USER_ID] = @USER_ID)
      LEFT  JOIN        [dbo].[TBPM_PROFILE]                as [TFP] WITH (NOLOCK) ON [CCP].[FK_PROFILE_ID]     = [TFP].[GUID]

      WHERE           (([CC].[ACTIVE] IN (0,1) AND [TYPE_ID] = 0)                          -- Overview
      OR               ([CC].[ACTIVE] IN (0,1) AND [TYPE_ID] = 1 AND [TFP].[ACTIVE] = 1)   -- Profile
      OR               ([CC].[ACTIVE] IN (0,1) AND [TYPE_ID] = 2)                          -- Monitor 
      OR               ([CC].[ACTIVE] IN (0,1) AND [TYPE_ID] = 3))                         -- Chart
                        
      AND               [CCP].[FK_CFG_COCKPIT_ID] NOT IN (SELECT [FK_CFG_COCKPIT_ID] FROM @RESULT) -- Allow no duplicates

      ORDER BY          [MC].[SEQUENCE] ASC, [CCP].[PARENT_ID] ASC, [CCP].[TYPE_ID] ASC;
    ----------------------------------------------------------------------------------------------------------------------------


    RETURN;

END;

/***
-- TEST:
    
    SELECT * FROM [FNTF_GET_COCKPIT_DEFINITION](1,NULL)
    SELECT * FROM [FNTF_GET_COCKPIT_DEFINITION](2,'en-us')
    SELECT * FROM [FNTF_GET_COCKPIT_DEFINITION](5,'en-us')

***/
GO