UPDATE TBDD_MODULES SET DB_VERSION = '1.9.4.9' where NAME = 'Process-Manager'
GO

ALTER FUNCTION [dbo].[FNPM_GET_WM_FILE_PATH] (@pDOCGUID BIGINT,@STANDARD As INT = 0)
RETURNS VARCHAR(MAX)
AS 
BEGIN
	DECLARE @RELPATH VARCHAR(100),@FILEPATH VARCHAR(500),@NAVPATH as VARCHAR(MAX)
	IF @STANDARD = 1
		SET @RELPATH = '\\windream\objects'
	ELSE
		SELECT  @RELPATH = WM_REL_PATH FROM TBPM_KONFIGURATION WHERE GUID = 1
	SELECT  @FILEPATH = REPLACE(FILE_PATH,'W:\','') FROM TBPM_PROFILE_FILES WHERE (GUID = @pDOCGUID)
	SET @NAVPATH = @RELPATH + '\' + @FILEPATH
	IF @NAVPATH	IS NULL
		SET @NAVPATH = ''
	RETURN @NAVPATH
END
GO
ALTER TABLE TBPM_FILES_USER_NOT_INDEXED ADD DocGUID BigInt NOT NULL DEFAULT 0
GO
-- =============================================
-- Author:		Digital Data, MS
-- Create date: 05.07.2019
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE PRPM_FILES_NOT_INDEXED 
	-- Add the parameters for the stored procedure here
	@USER_NAME VARCHAR(50),
	@PROFIL_ID INTEGER,
	@FILEPATH VARCHAR(500),
	@DocGUID BIGINT
AS
BEGIN
	IF NOT EXISTS(SELECT * FROM TBPM_FILES_USER_NOT_INDEXED WHERE 
															UPPER(USR_NAME) = UPPER(@USER_NAME) 
															AND PROFIL_ID = @PROFIL_ID AND DocGUID = @DocGUID)
		INSERT INTO TBPM_FILES_USER_NOT_INDEXED (USR_NAME,PROFIL_ID,FILE_PATH,DocGUID) VALUES (@USER_NAME, @PROFIL_ID, @FILEPATH,@DocGUID)
END
GO

-- =============================================
-- Author:		Digital Data MS
-- Create date: 05.07.2019
-- Description:	Gets the next DocumentID
-- =============================================
CREATE FUNCTION [dbo].[FNPM_GET_NEXT_DOC_GUID]
(
	-- Add the parameters for the function here
	@PROFIL_ID Integer,
	@OrderByNewest Bit, @DocGUID BigInt, @USERNAME VARCHAR(50)
)
RETURNS BIGINT
AS
BEGIN
	DECLARE @NEXT_DOC_GUID BIGINT
	IF @OrderByNewest = 0
		SELECT Top 1 @NEXT_DOC_GUID = GUID FROM TBPM_PROFILE_FILES WHERE
            PROFIL_ID = @PROFIL_ID AND EDIT = 0 AND IN_WORK = 0 AND 
           GUID <> @DocGUID AND GUID NOT IN (
                SELECT DocGUID
                FROM TBPM_FILES_USER_NOT_INDEXED
                WHERE (PROFIL_ID = @PROFIL_ID) AND (UPPER(USR_NAME) = UPPER(@USERNAME))
            )
	ELSE
		SELECT Top 1 @NEXT_DOC_GUID = GUID FROM TBPM_PROFILE_FILES WHERE
            PROFIL_ID = @PROFIL_ID AND EDIT = 0 AND IN_WORK = 0 AND 
           GUID <> @DocGUID AND GUID NOT IN (
                SELECT DocGUID
                FROM TBPM_FILES_USER_NOT_INDEXED
                WHERE (PROFIL_ID = @PROFIL_ID) AND (UPPER(USR_NAME) = UPPER(@USERNAME)))
		ORDER BY DMS_ERSTELLT_DATE DESC,GUID DESC
	IF @NEXT_DOC_GUID IS NULL
		SET @NEXT_DOC_GUID = 0
	RETURN @NEXT_DOC_GUID
END
GO
ALTER FUNCTION [dbo].[FNPM_LAST_EDITED_DOC](@pPROFIL_ID INTEGER,@pDOC_ID INTEGER)
RETURNS DATETIME
AS
BEGIN
	DECLARE @RESULT DATETIME
	IF EXISTS(SELECT GUID FROM TBPM_FILES_WORK_HISTORY WHERE PROFIL_ID = @pPROFIL_ID AND DOC_ID = @pDOC_ID)
		SELECT @RESULT = WORKED_WHEN FROM TBPM_FILES_WORK_HISTORY WHERE GUID = 
			(SELECT MAX(GUID) FROM TBPM_FILES_WORK_HISTORY WHERE PROFIL_ID = @pPROFIL_ID AND DOC_ID = @pDOC_ID) 
	ELSE
		SELECT @RESULT = convert(date,T.DMS_ERSTELLT_DATE)  FROM TBPM_PROFILE_FILES T 
		WHERE T.PROFIL_ID = @pPROFIL_ID AND T.DOC_ID = @pDOC_ID
	RETURN @RESULT
END
GO


