IF not EXISTS(SELECT GUID FROM TBDD_MODULES WHERE NAME = 'ToolCollection' )
	INSERT INTO TBDD_MODULES (NAME, SHORT_NAME, LICENSE,ACTIVE) VALUES ('ToolCollection','TC','kc6j9HSv/UJVAzFjMZv2A3wHZFy+2nNLKP9Qs8g9EVY=',1)
UPDATE TBDD_MODULES SET DB_VERSION = '1.1.0.0' where UPPER(NAME) = UPPER('ToolCollection')
GO
CREATE FUNCTION [dbo].[FNCUST_GET_TC_WD_RIGHTS] (
	@windreamDocID		BIGINT
)
RETURNS TABLE
AS
RETURN

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

	SELECT		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