IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.AUDIT_upd_AllotMentLines') AND OBJECTPROPERTY(id, N'IsTrigger') = 1) DROP TRIGGER dbo.AUDIT_upd_AllotMentLines 
GO
CREATE TRIGGER [dbo].[AUDIT_upd_AllotMentLines]
ON [dbo].[AllotMentLines] FOR UPDATE NOT FOR REPLICATION AS
BEGIN
    

	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(
		'AllotMentLines',
		'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([InitialCount])
	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, KEY2, KEY3
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[AllotmentNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[AllotmentNumber], NEW.[AllotmentNumber]), 0), '[AllotmentNumber] Is Null')+' AND '+IsNull('[RoomTypeNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[RoomTypeNumber], NEW.[RoomTypeNumber]), 0), '[RoomTypeNumber] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 0),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'InitialCount',
			CONVERT(nvarchar(4000), OLD.[InitialCount], 0),
			CONVERT(nvarchar(4000), NEW.[InitialCount], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[AllotmentNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[AllotmentNumber], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[RoomTypeNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[RoomTypeNumber], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[AllotmentNumber], 0)=CONVERT(nvarchar(4000), OLD.[AllotmentNumber], 0) or (NEW.[AllotmentNumber] Is Null and OLD.[AllotmentNumber] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[RoomTypeNumber], 0)=CONVERT(nvarchar(4000), OLD.[RoomTypeNumber], 0) or (NEW.[RoomTypeNumber] Is Null and OLD.[RoomTypeNumber] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 0)=CONVERT(nvarchar(4000), OLD.[DateStamp], 0) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[InitialCount] <>
					OLD.[InitialCount]
				) Or
			
				(
					NEW.[InitialCount] Is Null And
					OLD.[InitialCount] Is Not Null
				) Or
				(
					NEW.[InitialCount] Is Not Null And
					OLD.[InitialCount] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([TotalChange])
	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, KEY2, KEY3
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[AllotmentNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[AllotmentNumber], NEW.[AllotmentNumber]), 0), '[AllotmentNumber] Is Null')+' AND '+IsNull('[RoomTypeNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[RoomTypeNumber], NEW.[RoomTypeNumber]), 0), '[RoomTypeNumber] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 0),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'TotalChange',
			CONVERT(nvarchar(4000), OLD.[TotalChange], 0),
			CONVERT(nvarchar(4000), NEW.[TotalChange], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[AllotmentNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[AllotmentNumber], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[RoomTypeNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[RoomTypeNumber], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[AllotmentNumber], 0)=CONVERT(nvarchar(4000), OLD.[AllotmentNumber], 0) or (NEW.[AllotmentNumber] Is Null and OLD.[AllotmentNumber] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[RoomTypeNumber], 0)=CONVERT(nvarchar(4000), OLD.[RoomTypeNumber], 0) or (NEW.[RoomTypeNumber] Is Null and OLD.[RoomTypeNumber] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 0)=CONVERT(nvarchar(4000), OLD.[DateStamp], 0) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[TotalChange] <>
					OLD.[TotalChange]
				) Or
			
				(
					NEW.[TotalChange] Is Null And
					OLD.[TotalChange] Is Not Null
				) Or
				(
					NEW.[TotalChange] Is Not Null And
					OLD.[TotalChange] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([Blocked])
	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, KEY2, KEY3
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[AllotmentNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[AllotmentNumber], NEW.[AllotmentNumber]), 0), '[AllotmentNumber] Is Null')+' AND '+IsNull('[RoomTypeNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[RoomTypeNumber], NEW.[RoomTypeNumber]), 0), '[RoomTypeNumber] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 0),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'Blocked',
			CONVERT(nvarchar(4000), OLD.[Blocked], 0),
			CONVERT(nvarchar(4000), NEW.[Blocked], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[AllotmentNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[AllotmentNumber], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[RoomTypeNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[RoomTypeNumber], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[AllotmentNumber], 0)=CONVERT(nvarchar(4000), OLD.[AllotmentNumber], 0) or (NEW.[AllotmentNumber] Is Null and OLD.[AllotmentNumber] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[RoomTypeNumber], 0)=CONVERT(nvarchar(4000), OLD.[RoomTypeNumber], 0) or (NEW.[RoomTypeNumber] Is Null and OLD.[RoomTypeNumber] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 0)=CONVERT(nvarchar(4000), OLD.[DateStamp], 0) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[Blocked] <>
					OLD.[Blocked]
				) Or
			
				(
					NEW.[Blocked] Is Null And
					OLD.[Blocked] Is Not Null
				) Or
				(
					NEW.[Blocked] Is Not Null And
					OLD.[Blocked] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([AvailBeforeRelease])
	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, KEY2, KEY3
		)
		SELECT
			@AUDIT_LOG_TRANSACTION_ID,
		    convert(nvarchar(1500), IsNull('[AllotmentNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[AllotmentNumber], NEW.[AllotmentNumber]), 0), '[AllotmentNumber] Is Null')+' AND '+IsNull('[RoomTypeNumber]='+CONVERT(nvarchar(4000), IsNull(OLD.[RoomTypeNumber], NEW.[RoomTypeNumber]), 0), '[RoomTypeNumber] Is Null')+' AND '+IsNull('[DateStamp]=N'''+replace(CONVERT(nvarchar(4000), IsNull(OLD.[DateStamp], NEW.[DateStamp]), 0),'''' ,'''''')+'''', '[DateStamp] Is Null')),
		    'AvailBeforeRelease',
			CONVERT(nvarchar(4000), OLD.[AvailBeforeRelease], 0),
			CONVERT(nvarchar(4000), NEW.[AvailBeforeRelease], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[AllotmentNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[AllotmentNumber], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[RoomTypeNumber], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[RoomTypeNumber], 0))), IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[DateStamp], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[DateStamp], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[AllotmentNumber], 0)=CONVERT(nvarchar(4000), OLD.[AllotmentNumber], 0) or (NEW.[AllotmentNumber] Is Null and OLD.[AllotmentNumber] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[RoomTypeNumber], 0)=CONVERT(nvarchar(4000), OLD.[RoomTypeNumber], 0) or (NEW.[RoomTypeNumber] Is Null and OLD.[RoomTypeNumber] Is Null)) AND (CONVERT(nvarchar(4000), NEW.[DateStamp], 0)=CONVERT(nvarchar(4000), OLD.[DateStamp], 0) or (NEW.[DateStamp] Is Null and OLD.[DateStamp] Is Null))
			where (
			
			
				(
					NEW.[AvailBeforeRelease] <>
					OLD.[AvailBeforeRelease]
				) Or
			
				(
					NEW.[AvailBeforeRelease] Is Null And
					OLD.[AvailBeforeRelease] Is Not Null
				) Or
				(
					NEW.[AvailBeforeRelease] Is Not Null And
					OLD.[AvailBeforeRelease] 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
