USE [DD_ECM]
GO

/****** Object:  UserDefinedFunction [dbo].[FNCUST_GET_TC_WD_RIGHTS]    Script Date: 21.03.2024 17:21:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- T-SQL Tabellenfunktion: Ermittle Gruppenrechte anhand des gesetzten Benutzernamens (WINDREAM STRING 22)

-- Stand: MK // 18.11.2021
-- 18.11.2021 Initial

CREATE FUNCTION [dbo].[FNCUST_GET_TC_WD_RIGHTS] (
	@windreamDocID		BIGINT
)
RETURNS TABLE
AS
RETURN

	----------------------------------------------------------------------------------------------------------------------------------

	SELECT		DISTINCT concat([wdGI].[szNTDomain],'\',[wdGI].[szGroupName]) as 'RightGroups'
	FROM		[windream60].[dbo].[BaseAttributes]		as [wdBA]	(NOLOCK)

	INNER JOIN	[windream60].[dbo].[UserInfo]			as [wdUI]	(NOLOCK)	ON UPPER([wdBA].[szText22])		= UPPER([wdUI].[szUserName])		-- Username for UserID
	INNER JOIN	[windream60].[dbo].[UserGroup]			as [wdU2G]	(NOLOCK)	ON [wdUI].[dwUserID]			= [wdU2G].[dwUserOrGroupID]			-- UserID for GroupsID
	INNER JOIN	[windream60].[dbo].[GroupInfo]			as [wdGI]	(NOLOCK)	ON [wdU2G].[dwGroupID]			= [wdGI].[dwGroupID]				-- GroupIDs for Groupnames

	INNER JOIN	[DD_ECM].[dbo].[TBDD_USER]				as [ddUI]	(NOLOCK)	ON [wdUI].[dwUserID]			= [ddUI].[USERID_FK_INT_ECM]		-- windream UserID to DD UserID
	INNER JOIN	[DD_ECM].[dbo].[TBDD_GROUPS_USER]		as [ddU2G]	(NOLOCK)	ON [ddUI].[GUID]				= [ddU2G].[USER_ID]					-- UserID for GroupsID
	INNER JOIN	[DD_ECM].[dbo].[TBDD_GROUPS]			as [ddGI]	(NOLOCK)	ON [ddU2G].[GROUP_ID]			= [ddGI].[GUID]						-- GroupIDs for Groupnames

	WHERE		UPPER([wdGI].[szGroupName])	= UPPER([ddGI].[NAME])
	  AND		[wdBA].[dwDocID]			= @windreamDocID
	  AND		[wdGI].[szGroupName]		not in ('Eingangsrechnungsworkflow-FinaleFreigeber')
	  AND		[wdGI].[szGroupName]		not like '%Scan-Erfasser%'

	----------------------------------------------------------------------------------------------------------------------------------

	-- TEST:
	-- select * from [FNCUST_GET_TC_WD_RIGHTS](730231)
	-- select * from [FNCUST_GET_TC_WD_RIGHTS](741102)
GO


