UPDATE TBDD_MODULES SET DB_VERSION = '2.2.1.1' where NAME = 'Process-Manager'
GO
-- =============================================
-- Author:		Digital Data MS
-- Created: 11.06.2021
-- Description:	Gets the next DocumentID
-- Changed:	05.05.2022	Insert only when <> 0
-- =============================================
ALTER PROCEDURE [dbo].[PRPM_GET_NEXT_DOC_INFO]
(
	@PROFIL_ID Integer,
	@DocID BigInt, 
	@UserID Integer
)
AS
BEGIN
	IF @DocID <> 0
		BEGIN
			INSERT INTO TBPM_DOCWALKOVER (DocID,UserID) VALUES (@DocID,@UserID);
		END

	SELECT GUID as DocGUID, DOC_ID as DocID
	,(SELECT COUNT(GUID) FROM TBPM_VALIDATION_PROFILE_GROUP_USER WHERE UserID = @UserID) - (SELECT COUNT(DocID) + 1 FROM TBPM_DOCWALKOVER WHERE UserID = @UserID)  REMAINING
	FROM TBPM_PROFILE_FILES WHERE PROFIL_ID = @PROFIL_ID AND DOC_ID = (
	SELECT MIN(DocID) FROM TBPM_VALIDATION_PROFILE_GROUP_USER WHERE UserID = @UserID AND DocID not in (SELECT DISTINCT DocID FROM TBPM_DOCWALKOVER WHERE UserID = @UserID))

	RETURN 
END
