IF EXISTS (SELECT * FROM dbo.SysObjects WHERE id = object_id(N'dbo.AUDIT_upd_TimeRegistration') AND OBJECTPROPERTY(id, N'IsTrigger') = 1) DROP TRIGGER dbo.AUDIT_upd_TimeRegistration 
GO
CREATE TRIGGER [dbo].[AUDIT_upd_TimeRegistration]
ON [dbo].[TimeRegistration] 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(
		'TimeRegistration',
		'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([SessionId])
	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('[SessionId]='+CONVERT(nvarchar(4000), IsNull(OLD.[SessionId], NEW.[SessionId]), 0), '[SessionId] Is Null')),
		    'SessionId',
			CONVERT(nvarchar(4000), OLD.[SessionId], 0),
			CONVERT(nvarchar(4000), NEW.[SessionId], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[SessionId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[SessionId], 0)))
			
		FROM deleted OLD Full Outer Join inserted NEW On
			(CONVERT(nvarchar(4000), NEW.[SessionId], 0)=CONVERT(nvarchar(4000), OLD.[SessionId], 0) or (NEW.[SessionId] Is Null and OLD.[SessionId] Is Null))
			WHERE (
			
			
				(
					NEW.[SessionId] <>
					OLD.[SessionId]
				) Or
			
				(
					NEW.[SessionId] Is Null And
					OLD.[SessionId] Is Not Null
				) Or
				(
					NEW.[SessionId] Is Not Null And
					OLD.[SessionId] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([UserIdNumber])
	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('[SessionId]='+CONVERT(nvarchar(4000), IsNull(OLD.[SessionId], NEW.[SessionId]), 0), '[SessionId] Is Null')),
		    'UserIdNumber',
			CONVERT(nvarchar(4000), OLD.[UserIdNumber], 0),
			CONVERT(nvarchar(4000), NEW.[UserIdNumber], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[SessionId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[SessionId], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[SessionId], 0)=CONVERT(nvarchar(4000), OLD.[SessionId], 0) or (NEW.[SessionId] Is Null and OLD.[SessionId] Is Null))
			where (
			
			
				(
					NEW.[UserIdNumber] <>
					OLD.[UserIdNumber]
				) Or
			
				(
					NEW.[UserIdNumber] Is Null And
					OLD.[UserIdNumber] Is Not Null
				) Or
				(
					NEW.[UserIdNumber] Is Not Null And
					OLD.[UserIdNumber] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([LoginTime])
	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('[SessionId]='+CONVERT(nvarchar(4000), IsNull(OLD.[SessionId], NEW.[SessionId]), 0), '[SessionId] Is Null')),
		    'LoginTime',
			CONVERT(nvarchar(4000), OLD.[LoginTime], 0),
			CONVERT(nvarchar(4000), NEW.[LoginTime], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[SessionId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[SessionId], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[SessionId], 0)=CONVERT(nvarchar(4000), OLD.[SessionId], 0) or (NEW.[SessionId] Is Null and OLD.[SessionId] Is Null))
			where (
			
			
				(
					NEW.[LoginTime] <>
					OLD.[LoginTime]
				) Or
			
				(
					NEW.[LoginTime] Is Null And
					OLD.[LoginTime] Is Not Null
				) Or
				(
					NEW.[LoginTime] Is Not Null And
					OLD.[LoginTime] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([LogoutTime])
	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('[SessionId]='+CONVERT(nvarchar(4000), IsNull(OLD.[SessionId], NEW.[SessionId]), 0), '[SessionId] Is Null')),
		    'LogoutTime',
			CONVERT(nvarchar(4000), OLD.[LogoutTime], 0),
			CONVERT(nvarchar(4000), NEW.[LogoutTime], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[SessionId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[SessionId], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[SessionId], 0)=CONVERT(nvarchar(4000), OLD.[SessionId], 0) or (NEW.[SessionId] Is Null and OLD.[SessionId] Is Null))
			where (
			
			
				(
					NEW.[LogoutTime] <>
					OLD.[LogoutTime]
				) Or
			
				(
					NEW.[LogoutTime] Is Null And
					OLD.[LogoutTime] Is Not Null
				) Or
				(
					NEW.[LogoutTime] Is Not Null And
					OLD.[LogoutTime] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([Module])
	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('[SessionId]='+CONVERT(nvarchar(4000), IsNull(OLD.[SessionId], NEW.[SessionId]), 0), '[SessionId] Is Null')),
		    'Module',
			CONVERT(nvarchar(4000), OLD.[Module], 0),
			CONVERT(nvarchar(4000), NEW.[Module], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[SessionId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[SessionId], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[SessionId], 0)=CONVERT(nvarchar(4000), OLD.[SessionId], 0) or (NEW.[SessionId] Is Null and OLD.[SessionId] Is Null))
			where (
			
			
				(
					NEW.[Module] <>
					OLD.[Module]
				) Or
			
				(
					NEW.[Module] Is Null And
					OLD.[Module] Is Not Null
				) Or
				(
					NEW.[Module] Is Not Null And
					OLD.[Module] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([Note])
	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('[SessionId]='+CONVERT(nvarchar(4000), IsNull(OLD.[SessionId], NEW.[SessionId]), 0), '[SessionId] Is Null')),
		    'Note',
			CONVERT(nvarchar(4000), OLD.[Note], 0),
			CONVERT(nvarchar(4000), NEW.[Note], 0),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[SessionId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[SessionId], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[SessionId], 0)=CONVERT(nvarchar(4000), OLD.[SessionId], 0) or (NEW.[SessionId] Is Null and OLD.[SessionId] Is Null))
			where (
			
			
				(
					NEW.[Note] <>
					OLD.[Note]
				) Or
			
				(
					NEW.[Note] Is Null And
					OLD.[Note] Is Not Null
				) Or
				(
					NEW.[Note] Is Not Null And
					OLD.[Note] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([LoginDate])
	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('[SessionId]='+CONVERT(nvarchar(4000), IsNull(OLD.[SessionId], NEW.[SessionId]), 0), '[SessionId] Is Null')),
		    'LoginDate',
			CONVERT(nvarchar(4000), OLD.[LoginDate], 121),
			CONVERT(nvarchar(4000), NEW.[LoginDate], 121),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[SessionId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[SessionId], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[SessionId], 0)=CONVERT(nvarchar(4000), OLD.[SessionId], 0) or (NEW.[SessionId] Is Null and OLD.[SessionId] Is Null))
			where (
			
			
				(
					NEW.[LoginDate] <>
					OLD.[LoginDate]
				) Or
			
				(
					NEW.[LoginDate] Is Null And
					OLD.[LoginDate] Is Not Null
				) Or
				(
					NEW.[LoginDate] Is Not Null And
					OLD.[LoginDate] Is Null
				)
				)
        
		SET @Inserted = CASE WHEN @@ROWCOUNT > 0 Then 1 Else @Inserted End
	END
	
	If UPDATE([LogoutDate])
	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('[SessionId]='+CONVERT(nvarchar(4000), IsNull(OLD.[SessionId], NEW.[SessionId]), 0), '[SessionId] Is Null')),
		    'LogoutDate',
			CONVERT(nvarchar(4000), OLD.[LogoutDate], 121),
			CONVERT(nvarchar(4000), NEW.[LogoutDate], 121),
			'A'
			, IsNULL( CONVERT(nvarchar(500), CONVERT(nvarchar(4000), OLD.[SessionId], 0)), CONVERT(nvarchar(500), CONVERT(nvarchar(4000), NEW.[SessionId], 0)))
			
		FROM deleted OLD Inner Join inserted NEW On 
			(CONVERT(nvarchar(4000), NEW.[SessionId], 0)=CONVERT(nvarchar(4000), OLD.[SessionId], 0) or (NEW.[SessionId] Is Null and OLD.[SessionId] Is Null))
			where (
			
			
				(
					NEW.[LogoutDate] <>
					OLD.[LogoutDate]
				) Or
			
				(
					NEW.[LogoutDate] Is Null And
					OLD.[LogoutDate] Is Not Null
				) Or
				(
					NEW.[LogoutDate] Is Not Null And
					OLD.[LogoutDate] 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
