USE [DD_ECM]
GO
UPDATE TBDD_MODULES SET DB_VERSION = '2.1.0.0' where NAME = 'Process-Manager'
GO
CREATE TABLE [dbo].[TBPM_DOCWALKOVER](
	[DocID]			BIGINT		NOT NULL,
	[UserID]		INT			NOT NULL,
	[ADDED_WHEN]	DATETIME	NOT NULL	DEFAULT GETDATE()
) ON [PRIMARY]
GO
-- =============================================
-- Author:		Digital Data MS
-- Create date: 11.06.2021
-- Description:	Gets the next DocumentID
-- =============================================
ALTER PROCEDURE [dbo].[PRPM_GET_NEXT_DOC_INFO]
(
	@PROFIL_ID Integer,
	@DocID BigInt, 
	@UserID Integer
)
AS
BEGIN
	INSERT INTO TBPM_DOCWALKOVER (DocID,UserID) VALUES (@DocID,@UserID)

	SELECT Top 1 GUID, DOC_ID 
	FROM 
		TBPM_PROFILE_FILES 
	WHERE
		PROFIL_ID = @PROFIL_ID AND EDIT = 0 AND IN_WORK = 0 AND 
        DOC_ID <> @DocID AND DOC_ID NOT IN (select DocID from TBPM_DOCWALKOVER WHERE UserID = @DocID AND UserID = @UserID)
		AND GUID NOT IN	(
						SELECT A.[DocGUID]
						FROM   [TBPM_FILES_USER_NOT_INDEXED] A INNER JOIN TBDD_USER B ON A.USR_NAME = B.USERNAME
						WHERE A.PROFIL_ID = @PROFIL_ID AND B.GUID = @UserID
						)

	RETURN 
END
GO