UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.7.7' where NAME = 'Record-Organizer' 
GO 

ALTER TABLE TBPMO_STRUCTURE_NODES_CONFIGURATION ADD EXPAND BIT NOT NULL DEFAULT 0
GO 
ALTER VIEW [dbo].[VWPMO_STRUCTURE_NODES] AS 
select 
	T.*,
	T1.CONTROL_ID,
	T1.CREATE_RECORD,
	T1.ENTITY_ID,
	T1.NAME,
	T1.TYPE_NODE,
	T1.PARENT_NODE,
	T1.EXPAND AS EXPAND_NODE
FROM TBPMO_STRUCTURE_NODES T, TBPMO_STRUCTURE_NODES_CONFIGURATION T1
WHERE T.NODE_CONFIG_ID = T1.GUID
GO
CREATE TABLE dbo.TBPMO_CONTROL_VALUE_HISTORY(
	GUID				INT,
	CONTROL_ID			INT,
	RECORD_ID			INT,
	VALUE				VARCHAR(max) ,
	ADDED_WHO			VARCHAR(50),
	ADDED_WHEN			DATETIME,
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CHANGE_STEP			INT,
	REDO				BIT,
 CONSTRAINT PK_TBPMO_CONTROL_VALUE_HISTORY PRIMARY KEY (GUID)
 )

GO
CREATE NONCLUSTERED INDEX [IDX_TBPMO_CONTROL_VALUE_HISTORY]
ON [dbo].[TBPMO_CONTROL_VALUE_HISTORY] ([RECORD_ID])
INCLUDE ([VALUE])
GO
ALTER TRIGGER [dbo].[TBPMO_CONTROL_VALUE_AFT_DEL] ON [dbo].[TBPMO_CONTROL_VALUE]
FOR DELETE
AS
BEGIN TRY
	DECLARE 
		@GUID			INTEGER,
		@RECORD_ID		INTEGER,
		@CONTROL_ID		INTEGER,
		@ADDED_WHO		VARCHAR(50),
		@ADDED_WHEN		DATETIME,
		@CHANGED_WHO	VARCHAR(50),
		@CHANGED_WHEN	DATETIME,
		@VALUE_DEL		VARCHAR(MAX),
		@DOC_ID			INTEGER,
		@USER_ID		INTEGER,
		@USER_ID_ECM	INTEGER,
		@RIGHT			INTEGER,
		@ENTITY_ID		INTEGER,
		@COL_NAME		VARCHAR(50),
		@COUNTER_CONTROL_UPDATE_ID	INTEGER
	SELECT
		@GUID = DELETED.GUID,
  		@RECORD_ID = DELETED.RECORD_ID,
		@CONTROL_ID = DELETED.CONTROL_ID,
		@VALUE_DEL = DELETED.VALUE,
		@ENTITY_ID = T1.FORM_ID,
		@COL_NAME = T1.COL_NAME	,
		@ADDED_WHO = DELETED.ADDED_WHO,	
		@ADDED_WHEN  = DELETED.ADDED_WHEN,
		@CHANGED_WHO = DELETED.CHANGED_WHO,
		@CHANGED_WHEN = DELETED.CHANGED_WHEN
	FROM DELETED, TBPMO_RECORD T,TBPMO_CONTROL T1
	WHERE DELETED.RECORD_ID = T.GUID AND
	DELETED.CONTROL_ID = T1.GUID

	IF EXISTS(SELECT CONTROL_ID FROM TBPMO_APPOINTMENT_CONTROLS WHERE CONTROL_ID = @CONTROL_ID)
		DELETE FROM TBPMO_APPOINTMENTS WHERE [CustomField1] = @RECORD_ID AND [CustomField2] = @CONTROL_ID
	EXEC PRPMO_ENTITY_TABLE_UPDATE_COLUMN @ENTITY_ID, @RECORD_ID,@CONTROL_ID, @COL_NAME,''	

	INSERT INTO TBPMO_CONTROL_VALUE_HISTORY(GUID,CONTROL_ID,RECORD_ID,VALUE,CHANGE_STEP,REDO,ADDED_WHO,ADDED_WHEN,CHANGED_WHO,CHANGED_WHEN)
	SELECT GUID,CONTROL_ID,RECORD_ID,VALUE,CHANGE_STEP,REDO,ADDED_WHO,ADDED_WHEN,CHANGED_WHO,CHANGED_WHEN FROM DELETED


	/*###### KUNDEN-INDIVIDUELL - BEGIN ####################################################################*/
	IF @CONTROL_ID in (29,14,346)
				BEGIN
					SELECT @COUNTER_CONTROL_UPDATE_ID = COUNTER_CONTROL_UPDATE_ID + 1 FROM TBPMO_KONFIGURATION WHERE GUID = 1
					UPDATE TBPMO_KONFIGURATION SET COUNTER_CONTROL_UPDATE_ID = @COUNTER_CONTROL_UPDATE_ID WHERE GUID = 1
				END	
	IF @CONTROL_ID = 346 
		BEGIN
			DECLARE cursor_DOCS CURSOR FOR 
				select DocID from VWPMO_DOC_SEARCH T, TBPMO_DOC_RECORD_LINK TRL where T.DocID = TRL.DOC_ID AND 
				TRL.RECORD_ID = @RECORD_ID
				--SELECT [USER_ID] FROM TBDD_GROUPS_USER WHERE GROUP_ID = (SELECT GUID FROM TBDD_USER_GROUPS WHERE NAME = @VALUE_NEW)
			OPEN cursor_DOCS 
			FETCH NEXT FROM cursor_DOCS INTO @DOC_ID
			WHILE @@FETCH_STATUS = 0
			BEGIN
				BEGIN
					DECLARE cursor_User CURSOR FOR 
						SELECT [USER_ID] FROM TBDD_GROUPS_USER WHERE GROUP_ID = (SELECT GUID FROM TBDD_USER_GROUPS WHERE NAME = @VALUE_DEL)
					OPEN cursor_User 
					FETCH NEXT FROM cursor_User INTO @USER_ID
					WHILE @@FETCH_STATUS = 0
					BEGIN
						SELECT @USER_ID_ECM = USERID_FK_INT_ECM FROM TBDD_USER WHERE GUID = @USER_ID
						EXEC PRPMO_RIGHTS_CHECK_RIGHT @DOC_ID,@USER_ID,@USER_ID_ECM,1,'DELETE RIGHT','LESEBERECHTIGUNG TEAM','TBPMO_CONTROL_VALUE_AFT_DEL_346', @COUNTER_CONTROL_UPDATE_ID
						FETCH NEXT FROM cursor_User INTO @USER_ID
					END
					CLOSE cursor_User
					DEALLOCATE cursor_User
				END
				FETCH NEXT FROM cursor_DOCS INTO @DOC_ID
			END
			CLOSE cursor_DOCS
			DEALLOCATE cursor_DOCS
		END
		/*###### KUNDEN-INDIVIDUELL - ENDE ####################################################################*/
END TRY
BEGIN CATCH   
	PRINT 'FEHLER IN TRIGGER: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ZEILE: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - FEHLER-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH

GO
ALTER PROCEDURE [dbo].[PRPMO_DELETE_RECORD](@pRECORD_ID INT, @WHO VARCHAR(30))
AS
	DELETE FROM TBPMO_WORKFLOW_TASK_USER WHERE WF_TASK_ID IN (SELECT GUID FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @pRECORD_ID)
	DELETE FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_RECORD_CONNECT WHERE RECORD1_ID = @pRECORD_ID OR RECORD2_ID = @pRECORD_ID
	DELETE FROM TBPMO_WORKFLOW_TASK_HISTORY WHERE WF_TASK_ID IN (SELECT GUID FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @pRECORD_ID)
    DELETE FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @pRECORD_ID
    DELETE FROM TBPMO_CONTROL_VALUE WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_WORKFLOW_REMINDER WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_FOLLUPEMAIL_RECORD WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_RECORD_VARIANT WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_RECORD_GEODATA WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_DOC_VALUES WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_RECORD_LOG WHERE RECORD_ID = @pRECORD_ID
    
	DECLARE @DELETE_SQL		NVARCHAR(MAX),@ENTITY_ID INT
	SELECT @ENTITY_ID = FORM_ID FROM TBPMO_RECORD WHERE GUID = @pRECORD_ID   
	SET @DELETE_SQL = 'DELETE FROM TBPMO_ENTITY_TABLE@ENT_ID WHERE [Record-ID] = @RECORD_ID'  
	SET @DELETE_SQL = REPLACE(@DELETE_SQL,'@ENT_ID',@ENTITY_ID)
	SET @DELETE_SQL = REPLACE(@DELETE_SQL,'@RECORD_ID',@pRECORD_ID)
	EXEC sp_executesql @DELETE_SQL
	DELETE FROM TBPMO_RECORD WHERE GUID = @pRECORD_ID

	INSERT INTO TBPMO_LOG_ESSENTIALS (REFERENCE_KEY,REFERENCE_STRING,COMMENT,ADDED_WHO) VALUES (@pRECORD_ID,'RECORD-ID','RECORD DELETED BY USER',@WHO)
GO
ALTER TABLE TBPMO_CONTROL
ADD AUTO_SUGGEST BIT NOT NULL DEFAULT 0
GO
ALTER VIEW [dbo].[VWPMO_CONTROL_SCREEN] AS
SELECT TOP 100 PERCENT
	T.GUID			CONTROL_ID,
	T.FORM_ID		CONTROL_ENTITY_ID,
	T.NAME			CONTROL_NAME,
	T.REQUIRED		CONTROL_REQUIRED,
	T.DEFAULT_VALUE	CONTROL_DEF_VALUE,
	T.READ_ONLY		CONTROL_READ_ONLY,
	T.SELECT_ONLY	CONTROL_SELECT_ONLY,
	T.MULTILINE		CONTROL_MULTILINE,
	T.PARENT_CONTROL_ID	CONTROL_PARENT_ID,
	T.COL_NAME		CONTROL_COL_NAME,
	T.SHOW_COLUMN   CONTROL_SHOW_COLUMN,
	T.CONNECTION_ID_1	CONTROL_CONNID_1,
	T.SQL_COMMAND_1		CONTROL_SQLCOMMAND_1,
	T.SQL_COMMAND_2		CONTROL_SQLCOMMAND_2,
	T.VISIBLE		CONTROL_VISIBLE,
	T.TREE_VIEW		CONTROL_TREE_VIEW,
	T.STATIC_LIST	CONTROL_STATIC_LIST,
	T.AUTO_SUGGEST	CONTROL_AUTO_SUGGEST,
	T4.GUID			CTRLTYPE_ID,
	T4.TYPE_NAME	CTRLTYPE_NAME,
	T.FORMAT_TYPE	CONTROL_FORMAT_TYPE,
	T.FORM_ID,
	T3.NAME			FORM_NAME,
	T3.LEVEL		FORM_LEVEL,
	T1.SCREEN_ID	SCREEN_ID,
	T2.NAME			SCREEN_NAME,
	T1.GUID			CTRLSCR_ID,
	T1.CONTROL_TEXT	CTRLSCR_CAPTION,
	T1.X_LOC		CTRLSCR_X_LOC,
	T1.Y_LOC		CTRLSCR_Y_LOC,
	T1.HEIGHT		CTRLSCR_HEIGHT,
	T1.WIDTH		CTRLSCR_WIDTH,
	T1.FONT_FAMILY	CTRLSCR_FONT_FAMILY,
	T1.FONT_COLOR	CTRLSCR_FONT_COLOR,
	T1.FONT_SIZE	CTRLSCR_FONT_SIZE,
	T1.FONT_STYLE	CTRLSCR_FONT_STYLE,
	T1.TAB_INDEX	CTRLSCR_TAB_INDEX,
	T1.TAB_STOP		CTRLSCR_TAB_STOP,
	T1.BACK_COLOR   CTRLSCR_BACK_COLOR,
	T.MASTER_DATA_ID CTRLSCR_MASTER_DATA_ID
FROM
	TBPMO_CONTROL T,
	TBPMO_CONTROL_SCREEN T1,
	TBPMO_SCREEN T2,
	TBPMO_FORM T3,
	TBPMO_CONTROL_TYPE T4
WHERE
	T.FORM_ID = T3.GUID AND
	T.GUID = T1.CONTROL_ID AND
	T1.SCREEN_ID = T2.GUID AND
	T.CONTROL_TYPE_ID = T4.GUID
ORDER BY 
	T1.SCREEN_ID,
	T.FORM_ID,
	T1.TAB_STOP

GO


