IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.AUDIT_upd_Templates') AND OBJECTPROPERTY(id, N'IsTrigger') = 1) DROP TRIGGER dbo.AUDIT_upd_Templates 
GO
CREATE TRIGGER [dbo].[AUDIT_upd_Templates]
ON [dbo].[Templates] FOR UPDATE NOT FOR REPLICATION AS
BEGIN
	
	DECLARE @FiscalSweden INT
	SELECT @FiscalSweden = COUNT(*) 
	FROM dbo.HotelSetup h WITH (NOLOCK) 
	INNER JOIN HotelSetupValueXTab vx WITH (NOLOCK) ON vx.HotelSetupNumber = h.HotelSetupNumber AND UPPER(h.SearchKey) = UPPER('FiscalPrinterSweden')
	INNER JOIN HotelSetupValue v WITH (NOLOCK) ON vx.fk_ValueNumber = v.Pk_ValueNumber
	WHERE v.KeyValue = '1'
	IF ISNULL(@FiscalSweden, 0) = 0
		RETURN
	

	DECLARE 
		@IDENTITY_SAVE			varchar(50),
		@AUDIT_LOG_TRANSACTION_ID	Int,
		@PRIM_KEY			nvarchar(4000),
		@Inserted	    		bit,
 		@ROWS_COUNT				int,
		@FK_UserIdNumber INT,
		@FK_CashBox INT,
		@ProgramPart SMALLINT,
		@TerminalName VARCHAR(50),
		@SystemDateStr VARCHAR(8),
		@SystemDateTime DATETIME

	SET NOCOUNT ON

	Select @ROWS_COUNT=count(*) from inserted
	SET @IDENTITY_SAVE = CAST(IsNull(@@IDENTITY,1) AS varchar(50))

    SELECT TOP 1 @FK_UserIdNumber = FK_UserIdNumber,
	@FK_CashBox = FK_CashBox,
	@ProgramPart = ProgramPart,
	@TerminalName = TerminalName
	FROM PersonnelLogInfo WITH (NOLOCK)
	WHERE spid = @@spid 
	ORDER BY CreateDate DESC

	IF ISNULL(@FK_UserIdNumber, 0) = 0
	  SET @FK_UserIdNumber = 1
	IF ISNULL(@FK_CashBox, -1) <= 0
	  SET @FK_CashBox = -1
	IF ISNULL(@ProgramPart, -1) <= 0
	  SET @ProgramPart = 14 -- ppUnknown from SystemAll
	IF ISNULL(@TerminalName, '') = ''
	  SET @TerminalName = 'Unknown'

    SELECT @SystemDateStr = SystemDate FROM System WITH (NOLOCK) WHERE Number = 1
    SET @SystemDateTime = CONVERT(DATETIME, @SystemDateStr + ' ' + CONVERT(CHAR(8), CURRENT_TIMESTAMP, 108))

	INSERT INTO dbo.AUDIT_LOG_TRANSACTIONS
	(
		TABLE_NAME,
		TABLE_SCHEMA,
		AUDIT_ACTION_ID,
		HOST_NAME,
		APP_NAME,
		MODIFIED_BY,
		MODIFIED_DATE,
		AFFECTED_ROWS,
		FK_UserIdNumber,
		FK_CashBox,
		ProgramPart,
		TerminalName,
		RealDateTime
	)
	values(
		'Templates',
		'dbo',
		1,	--	ACTION ID For UPDATE
		CASE 
		  WHEN LEN(HOST_NAME()) < 1 THEN ' '
		  ELSE HOST_NAME()
		END,
		CASE 
		  WHEN LEN(APP_NAME()) < 1 THEN ' '
		  ELSE APP_NAME()
		END,
		SUSER_SNAME(),
		@SystemDateTime,
		@ROWS_COUNT,
		@FK_UserIdNumber,
		@FK_CashBox,
		@ProgramPart,
		@TerminalName,
		GETDATE()
	)	
	
	Set @AUDIT_LOG_TRANSACTION_ID = SCOPE_IDENTITY()
	
	SET @Inserted = 0
	
	If UPDATE([TemplateNumber])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'TemplateNumber',
			CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0),
			CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Full Outer Join inserted NEW On
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			WHERE (
			
			
				(
					NEW.[TemplateNumber] <>
					OLD.[TemplateNumber]
				) Or
			
				(
					NEW.[TemplateNumber] Is Null And
					OLD.[TemplateNumber] Is Not Null
				) Or
				(
					NEW.[TemplateNumber] Is Not Null And
					OLD.[TemplateNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([TemplateGroup])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'TemplateGroup',
			CONVERT(nvarchar(4000), OLD.[TemplateGroup], 0),
			CONVERT(nvarchar(4000), NEW.[TemplateGroup], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			where (
			
			
				(
					NEW.[TemplateGroup] <>
					OLD.[TemplateGroup]
				) Or
			
				(
					NEW.[TemplateGroup] Is Null And
					OLD.[TemplateGroup] Is Not Null
				) Or
				(
					NEW.[TemplateGroup] Is Not Null And
					OLD.[TemplateGroup] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([Name])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'Name',
			CONVERT(nvarchar(4000), OLD.[Name], 0),
			CONVERT(nvarchar(4000), NEW.[Name], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			where (
			
			
				(
					NEW.[Name] <>
					OLD.[Name]
				) Or
			
				(
					NEW.[Name] Is Null And
					OLD.[Name] Is Not Null
				) Or
				(
					NEW.[Name] Is Not Null And
					OLD.[Name] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([UserCreated])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'UserCreated',
			CONVERT(nvarchar(4000), OLD.[UserCreated], 0),
			CONVERT(nvarchar(4000), NEW.[UserCreated], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			where (
			
			
				(
					NEW.[UserCreated] <>
					OLD.[UserCreated]
				) Or
			
				(
					NEW.[UserCreated] Is Null And
					OLD.[UserCreated] Is Not Null
				) Or
				(
					NEW.[UserCreated] Is Not Null And
					OLD.[UserCreated] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([CountryNumber])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'CountryNumber',
			CONVERT(nvarchar(4000), OLD.[CountryNumber], 0),
			CONVERT(nvarchar(4000), NEW.[CountryNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			where (
			
			
				(
					NEW.[CountryNumber] <>
					OLD.[CountryNumber]
				) Or
			
				(
					NEW.[CountryNumber] Is Null And
					OLD.[CountryNumber] Is Not Null
				) Or
				(
					NEW.[CountryNumber] Is Not Null And
					OLD.[CountryNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([TemplateType])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'TemplateType',
			CONVERT(nvarchar(4000), OLD.[TemplateType], 0),
			CONVERT(nvarchar(4000), NEW.[TemplateType], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			where (
			
			
				(
					NEW.[TemplateType] <>
					OLD.[TemplateType]
				) Or
			
				(
					NEW.[TemplateType] Is Null And
					OLD.[TemplateType] Is Not Null
				) Or
				(
					NEW.[TemplateType] Is Not Null And
					OLD.[TemplateType] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([HotelNumber])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'HotelNumber',
			CONVERT(nvarchar(4000), OLD.[HotelNumber], 0),
			CONVERT(nvarchar(4000), NEW.[HotelNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			where (
			
			
				(
					NEW.[HotelNumber] <>
					OLD.[HotelNumber]
				) Or
			
				(
					NEW.[HotelNumber] Is Null And
					OLD.[HotelNumber] Is Not Null
				) Or
				(
					NEW.[HotelNumber] Is Not Null And
					OLD.[HotelNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([FK_SubTemplateNumber])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'FK_SubTemplateNumber',
			CONVERT(nvarchar(4000), OLD.[FK_SubTemplateNumber], 0),
			CONVERT(nvarchar(4000), NEW.[FK_SubTemplateNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			where (
			
			
				(
					NEW.[FK_SubTemplateNumber] <>
					OLD.[FK_SubTemplateNumber]
				) Or
			
				(
					NEW.[FK_SubTemplateNumber] Is Null And
					OLD.[FK_SubTemplateNumber] Is Not Null
				) Or
				(
					NEW.[FK_SubTemplateNumber] Is Not Null And
					OLD.[FK_SubTemplateNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([FkTemplateMaster])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'FkTemplateMaster',
			CONVERT(nvarchar(4000), OLD.[FkTemplateMaster], 0),
			CONVERT(nvarchar(4000), NEW.[FkTemplateMaster], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			where (
			
			
				(
					NEW.[FkTemplateMaster] <>
					OLD.[FkTemplateMaster]
				) Or
			
				(
					NEW.[FkTemplateMaster] Is Null And
					OLD.[FkTemplateMaster] Is Not Null
				) Or
				(
					NEW.[FkTemplateMaster] Is Not Null And
					OLD.[FkTemplateMaster] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([SubjectLine])
	BEGIN
    
		INSERT
		INTO dbo.AUDIT_LOG_DATA
		(
			AUDIT_LOG_TRANSACTION_ID,
			PRIMARY_KEY_DATA,
			COL_NAME,
			OLD_VALUE_LONG,
			NEW_VALUE_LONG,
			DATA_TYPE
			, KEY1
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[TemplateNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[TemplateNumber], NEW.[TemplateNumber]), 0), '[TemplateNumber] Is Null')),
		    'SubjectLine',
			CONVERT(nvarchar(4000), OLD.[SubjectLine], 0),
			CONVERT(nvarchar(4000), NEW.[SubjectLine], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[TemplateNumber], 0)=CONVERT(nvarchar(4000), OLD.[TemplateNumber], 0) or (NEW.[TemplateNumber] Is Null and OLD.[TemplateNumber] Is Null))
			where (
			
			
				(
					NEW.[SubjectLine] <>
					OLD.[SubjectLine]
				) Or
			
				(
					NEW.[SubjectLine] Is Null And
					OLD.[SubjectLine] Is Not Null
				) Or
				(
					NEW.[SubjectLine] Is Not Null And
					OLD.[SubjectLine] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	-- Watch
	
	-- Lookup
	
	IF @Inserted = 0
	BEGIN
		DELETE FROM dbo.AUDIT_LOG_TRANSACTIONS WHERE AUDIT_LOG_TRANSACTION_ID = @AUDIT_LOG_TRANSACTION_ID
	END
	-- Restore @@IDENTITY Value  
    DECLARE @maxprec AS varchar(2)
    SET @maxprec=CAST(@@MAX_PRECISION as varchar(2))
    EXEC('SELECT IDENTITY(decimal('+@maxprec+',0),'+@IDENTITY_SAVE+',1) id INTO #tmp')
End
