UPDATE TBDD_MODULES SET DB_VERSION = '2.5.1.0' where NAME = 'Record-Organizer' 
GO 
ALTER TABLE TBPMO_FORM_CONSTRUCTOR 
ADD NODE_NAVIGATION BIT	NOT NULL DEFAULT 0;
GO
ALTER VIEW [dbo].[VWPMO_CONSTRUCTOR_FORMS] AS
SELECT TOP 100 PERCENT
	T2.GUID,
	T1.GUID FORM_VIEW_ID,
	T1.SCREEN_ID,
	T2.CONSTRUCT_ID,
	T.PARENT_ID,
	T2.SEQUENCE,
	T2.LEVEL1_SELECT,
	T.GUID			AS FORM_ID, 
	T1.FORM_TITLE, 
	T2.SQL_SELECT_EBENE1,
	T2.SQL_SELECT_EBENE2,
	T2.SQL_QUICK_VIEW,
	T2.COLUMN_NAME1,
	T2.COLUMN_NAME2,
	T2.ADD_RECORDS,
	T.FORM_TYPE_ID,
	T2.LOAD_DIRECT,
	T2.CONTROL_DOCTYPE_MATCH,
	T2.SQL_RIGHT_READ_AND_VIEW_ONLY,
	T2.SQL_RIGHT_WINDREAM_VIEW,
	T2.DOC_SEARCH_TAB1,
	T2.[ACTIVATE_DOC_SEARCH_ON_EDIT],
	T1.DOCUMENT_VIEW,
	T.DATE_FORMAT,
	T.LOCK_CONTROLS_BG_LOAD,
	(SELECT COUNT(GUID) FROm TBPMO_CONTROL WHERE FORM_ID = T1.FORM_ID AND TREE_VIEW = 1) AS COUNT_TREEVIEW,
	T3.NODE_NAVIGATION
FROM 
	tbpmo_form T, 
	TBPMO_FORM_VIEW T1,
	TBPMO_FORM_CONSTRUCTOR_DETAIL T2,
	TBPMO_FORM_CONSTRUCTOR T3
where 
	T2.FORM_ID = T.GUID AND
	T.GUID = T1.FORM_ID AND
	T2.CONSTRUCT_ID = T3.GUID
ORDER BY 
	T2.CONSTRUCT_ID,T.PARENT_ID,T2.SEQUENCE

GO
CREATE PROCEDURE PRPMO_IN_WORK_RESET_DATEDIFF 
AS
DECLARE @DocID BIGINT
DECLARE cursInWork_Reset CURSOR FOR 
	select A.DocID from TBPMO_DOCRESULT_LIST A LEFT JOIN 
	TBPMO_DOC_IN_WORK_HISTORY B ON A.DocID = B.DOCID 
	WHERE A.IN_WORK = 1
	AND DATEDIFF(DAY,B.IN_WORK_WHEN,GETDATE()) > 20
OPEN cursInWork_Reset 
FETCH NEXT FROM cursInWork_Reset INTO @DocID
WHILE @@FETCH_STATUS = 0
BEGIN
	UPDATE TBPMO_DOCRESULT_LIST SET IN_WORK = 0 WHERE DocID = @DocID
	FETCH NEXT FROM cursInWork_Reset INTO @DocID
END
CLOSE cursInWork_Reset
DEALLOCATE cursInWork_Reset
GO

ALTER TRIGGER [dbo].[TBPMO_DOCRESULT_LIST_AFT_UPD] ON [dbo].[TBPMO_DOCRESULT_LIST]
FOR UPDATE
AS
	IF UPDATE (IN_WORK)
		BEGIN
			DECLARE @IN_WORK BIT, 
			@IN_WORK_USER VARCHAR(50),
			@IN_WORK_COMMENT VARCHAR(500),
			@DocID		BIGINT

			SELECT 
				@DocID = DocID,
				@IN_WORK = IN_WORK,
				@IN_WORK_USER = IN_WORK_USER,
				@IN_WORK_COMMENT = IN_WORK_COMMENT
			FROM INSERTED
			IF @IN_WORK = 1
				INSERT INTO TBPMO_DOC_IN_WORK_HISTORY (DOCID,COMMENT,IN_WORK_WHO) VALUES (@DocID,@IN_WORK_COMMENT,@IN_WORK_USER)
			ELSE
				BEGIN
					UPDATE TBPMO_DOC_IN_WORK_HISTORY SET FREE_WHO = @IN_WORK_USER, FREE_WHEN = GETDATE() WHERE DOCID = @DocID AND FREE_WHEN IS NULL
					UPDATE TBPMO_DOCRESULT_LIST SET IN_WORK_USER = '', IN_WORK_COMMENT = NULL FROM INSERTED WHERE TBPMO_DOCRESULT_LIST.[DocID] = INSERTED.[DocID]
				END

		END
	UPDATE TBPMO_DOCRESULT_LIST SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_DOCRESULT_LIST.[DocID] = INSERTED.[DocID]







