UPDATE TBDD_MODULES SET DB_VERSION = '2.0.0.5' where NAME = 'Process-Manager'
GO
CREATE TABLE [dbo].[TBPM_DOCWALKOVER](
	[DocID] [bigint] NOT NULL,
	[UserID] [int] NOT NULL,
	[ADDED_WHEN] [datetime] NOT NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[TBPM_DOCWALKOVER] ADD  CONSTRAINT [DF_TBPM_CUST_DocWalkOver_ADDED_WHEN]  DEFAULT (getdate()) FOR [ADDED_WHEN]
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,1)

	SELECT 
		GUID as DocGUID 
		,DOC_ID as DocID,
		(SELECT COUNT(GUID) FROM TBPM_PROFILE_FILES T WHERE EDIT = 0 and ACTIVE = 1 AND PROFIL_ID = @PROFIL_ID AND 
		DOC_ID NOT IN (SELECT DocID FROM TBPM_DOCWALKOVER WHERE UserID = @UserID)) COUNT_DocsLeft 
	FROM 
		TBPM_PROFILE_FILES WHERE GUID = (
		SELECT MIN(GUID) FROM TBPM_PROFILE_FILES WHERE PROFIL_ID = @PROFIL_ID and DOC_ID not in 
			(SELECT DISTINCT DocID FROM TBPM_DOCWALKOVER WHERE UserID = @UserID))
END
