IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.AUDIT_upd_Products') AND OBJECTPROPERTY(id, N'IsTrigger') = 1) DROP TRIGGER dbo.AUDIT_upd_Products 
GO
CREATE TRIGGER [dbo].[AUDIT_upd_Products]
ON [dbo].[Products] 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(
		'Products',
		'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([MarkAsDeleted])
	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('[ProductNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ProductNumber], NEW.[ProductNumber]), 0), '[ProductNumber] Is Null')),
		    'MarkAsDeleted',
			CONVERT(nvarchar(4000), OLD.[MarkAsDeleted], 0),
			CONVERT(nvarchar(4000), NEW.[MarkAsDeleted], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ProductNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)=CONVERT(nvarchar(4000), OLD.[ProductNumber], 0) or (NEW.[ProductNumber] Is Null and OLD.[ProductNumber] Is Null))
			where (
			
			
				(
					NEW.[MarkAsDeleted] <>
					OLD.[MarkAsDeleted]
				) Or
			
				(
					NEW.[MarkAsDeleted] Is Null And
					OLD.[MarkAsDeleted] Is Not Null
				) Or
				(
					NEW.[MarkAsDeleted] Is Not Null And
					OLD.[MarkAsDeleted] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ProductName])
	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('[ProductNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ProductNumber], NEW.[ProductNumber]), 0), '[ProductNumber] Is Null')),
		    'ProductName',
			CONVERT(nvarchar(4000), OLD.[ProductName], 0),
			CONVERT(nvarchar(4000), NEW.[ProductName], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ProductNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)=CONVERT(nvarchar(4000), OLD.[ProductNumber], 0) or (NEW.[ProductNumber] Is Null and OLD.[ProductNumber] Is Null))
			where (
			
			
				(
					NEW.[ProductName] <>
					OLD.[ProductName]
				) Or
			
				(
					NEW.[ProductName] Is Null And
					OLD.[ProductName] Is Not Null
				) Or
				(
					NEW.[ProductName] Is Not Null And
					OLD.[ProductName] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ProductDate])
	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('[ProductNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ProductNumber], NEW.[ProductNumber]), 0), '[ProductNumber] Is Null')),
		    'ProductDate',
			CONVERT(nvarchar(4000), OLD.[ProductDate], 0),
			CONVERT(nvarchar(4000), NEW.[ProductDate], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ProductNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)=CONVERT(nvarchar(4000), OLD.[ProductNumber], 0) or (NEW.[ProductNumber] Is Null and OLD.[ProductNumber] Is Null))
			where (
			
			
				(
					NEW.[ProductDate] <>
					OLD.[ProductDate]
				) Or
			
				(
					NEW.[ProductDate] Is Null And
					OLD.[ProductDate] Is Not Null
				) Or
				(
					NEW.[ProductDate] Is Not Null And
					OLD.[ProductDate] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ProductEndDate])
	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('[ProductNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ProductNumber], NEW.[ProductNumber]), 0), '[ProductNumber] Is Null')),
		    'ProductEndDate',
			CONVERT(nvarchar(4000), OLD.[ProductEndDate], 0),
			CONVERT(nvarchar(4000), NEW.[ProductEndDate], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ProductNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)=CONVERT(nvarchar(4000), OLD.[ProductNumber], 0) or (NEW.[ProductNumber] Is Null and OLD.[ProductNumber] Is Null))
			where (
			
			
				(
					NEW.[ProductEndDate] <>
					OLD.[ProductEndDate]
				) Or
			
				(
					NEW.[ProductEndDate] Is Null And
					OLD.[ProductEndDate] Is Not Null
				) Or
				(
					NEW.[ProductEndDate] Is Not Null And
					OLD.[ProductEndDate] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([PriceOverWrite])
	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('[ProductNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ProductNumber], NEW.[ProductNumber]), 0), '[ProductNumber] Is Null')),
		    'PriceOverWrite',
			CONVERT(nvarchar(4000), OLD.[PriceOverWrite], 0),
			CONVERT(nvarchar(4000), NEW.[PriceOverWrite], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ProductNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)=CONVERT(nvarchar(4000), OLD.[ProductNumber], 0) or (NEW.[ProductNumber] Is Null and OLD.[ProductNumber] Is Null))
			where (
			
			
				(
					NEW.[PriceOverWrite] <>
					OLD.[PriceOverWrite]
				) Or
			
				(
					NEW.[PriceOverWrite] Is Null And
					OLD.[PriceOverWrite] Is Not Null
				) Or
				(
					NEW.[PriceOverWrite] Is Not Null And
					OLD.[PriceOverWrite] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([ExcludeVAT])
	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('[ProductNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[ProductNumber], NEW.[ProductNumber]), 0), '[ProductNumber] Is Null')),
		    'ExcludeVAT',
			CONVERT(nvarchar(4000), OLD.[ExcludeVAT], 0),
			CONVERT(nvarchar(4000), NEW.[ExcludeVAT], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[ProductNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[ProductNumber], 0)=CONVERT(nvarchar(4000), OLD.[ProductNumber], 0) or (NEW.[ProductNumber] Is Null and OLD.[ProductNumber] Is Null))
			where (
			
			
				(
					NEW.[ExcludeVAT] <>
					OLD.[ExcludeVAT]
				) Or
			
				(
					NEW.[ExcludeVAT] Is Null And
					OLD.[ExcludeVAT] Is Not Null
				) Or
				(
					NEW.[ExcludeVAT] Is Not Null And
					OLD.[ExcludeVAT] 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
