
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		marek
-- Create date: 22/07/2009
-- Description:	Log dla tabeli Wplaty - ON UPDATE
-- =============================================
ALTER TRIGGER [tg_WplatyUpdate] 
   ON  [dbo].[Wplaty] 
   FOR UPDATE
AS
BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	

DECLARE @idFaktury INT
DECLARE @dataWplaty DATETIME
DECLARE @rodzajPotwierdzenia VARCHAR(5)
DECLARE @symbolPotwierdzenia VARCHAR(25)
DECLARE @brutto MONEY
DECLARE @waluta MONEY
DECLARE @symbolWaluty NVARCHAR(4)

SELECT 
	@idFaktury=idFaktury, 
	@dataWplaty=dataWplaty,
	@rodzajPotwierdzenia=rodzajPotwierdzenia,
	@symbolPotwierdzenia=symbolPotwierdzenia,
	@brutto=brutto,
	@waluta=waluta_brutto,
	@symbolWaluty=waluta_miano
FROM INSERTED

DECLARE @details NVARCHAR(1000)

SET @details = 'Faktura: ' +  CAST(@idFaktury AS NVARCHAR(100))
SET @details = @details + '; DataWplaty: ' +  CONVERT(NVARCHAR(100),COALESCE(@dataWplaty,''))
SET @details = @details + '; Potwierdzenie: ' +  CONVERT(NVARCHAR(100),COALESCE(@rodzajPotwierdzenia,''))
SET @details = @details + '; Symbol: ' +  CONVERT(NVARCHAR(100),COALESCE(@symbolPotwierdzenia,''))

IF @brutto IS NOT NULL
SET @details = @details + '; Brutto: ' +  CAST(@brutto AS VARCHAR)
ELSE
SET @details = @details + '; Brutto: NULL'
IF @waluta IS NOT NULL
SET @details = @details + '; Waluta: ' +  CAST(@waluta AS VARCHAR)
ELSE
SET @details = @details + '; Waluta: NULL'
SET @details = @details + '; SymbolWaluty: ' +  CONVERT(NVARCHAR(100),COALESCE(@symbolWaluty,''))

INSERT INTO dbo.[Log](Data, UserName, Operacja, Tabela, Opis, Szczegoly)
VALUES(GETDATE(), USER_NAME(), 'UPDATE', 'Wplaty', @idFaktury, @details)

END
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

