IF EXISTS(SELECT *
          FROM SYSOBJECTS O, SYSUSERS U
          WHERE O.NAME = 'LOG_FIN_AJCTPAGAR' AND
                TYPE   = 'TR'              AND
                U.UID  = O.UID             AND
                U.NAME = USER)

DROP TRIGGER LOG_FIN_AJCTPAGAR
GO
CREATE TRIGGER LOG_FIN_AJCTPAGAR
ON AJCTPAGAR
FOR UPDATE,INSERT, DELETE
AS
BEGIN
   DECLARE @V_USUARIO             VARCHAR(40);
   DECLARE @V_TIPO                VARCHAR(1);
   DECLARE @V_TPMOVI              VARCHAR(1);
   DECLARE @V_ITPESQ              VARCHAR(40);
   DECLARE @V_DESC                VARCHAR(500);
   DECLARE @V_CDFILLOGOPER        VARCHAR(4);
   DECLARE @V_DSREMOTEADDR        VARCHAR(15);
   DECLARE @V_DSREMOTEHOST        VARCHAR(15);
   DECLARE @V_DSSERVERRDM         VARCHAR(20);
   DECLARE @V_DSSERVMETHOD        VARCHAR(30);
   DECLARE @V_DSREMOTEOPER        VARCHAR(12);

   DECLARE @V_CDFILIAL        VARCHAR(04);
   DECLARE @V_NRLANCTONF      VARCHAR(10);
   DECLARE @V_NRSEQVENPAG     VARCHAR(2);
   DECLARE @V_NRPARCELAPAG    VARCHAR(2);
   DECLARE @V_DTAJUSTEPAG     DATETIME;
   DECLARE @V_CDAJUCTPAG      VARCHAR(4);
   DECLARE @V_DSAJUSTEPAG     VARCHAR(20);
   DECLARE @V_VRAJUSTEPAG     NUMERIC(15,5);
   
   SET @V_TIPO = '0';
   SET @V_DESC = '';
   SET @V_CDFILLOGOPER = '';
   SET @V_USUARIO = '-';
   SET @V_TPMOVI = '0';

   DECLARE C_CURSOR_AJ CURSOR FOR
      SELECT
         'D' IDCOMANDO,
          CDFILIAL, NRLANCTONF, NRSEQVENPAG, NRPARCELAPAG,
          DTAJUSTEPAG, CDAJUCTPAG, DSAJUSTEPAG, VRAJUSTEPAG
      FROM
         DELETED
      WHERE
         0 = (SELECT COUNT(*) FROM INSERTED)

      UNION ALL

      SELECT
         'I' IDCOMANDO,
         CDFILIAL, NRLANCTONF, NRSEQVENPAG, NRPARCELAPAG,
         DTAJUSTEPAG, CDAJUCTPAG, DSAJUSTEPAG, VRAJUSTEPAG
      FROM
         INSERTED
      WHERE
         0 = (SELECT COUNT(*) FROM DELETED)

   OPEN C_CURSOR_AJ;
   FETCH NEXT FROM C_CURSOR_AJ INTO @V_TIPO,
                                    @V_CDFILIAL, @V_NRLANCTONF, @V_NRSEQVENPAG, @V_NRPARCELAPAG,
                                    @V_DTAJUSTEPAG, @V_CDAJUCTPAG, @V_DSAJUSTEPAG, @V_VRAJUSTEPAG;

   WHILE (@@FETCH_STATUS = 0) BEGIN

      IF @V_TIPO = 'D' BEGIN
         SET @V_TPMOVI  = '3';
      END
      ELSE IF @V_TIPO = 'I' BEGIN
         SET @V_TPMOVI  = '1';
      END;

      SET @V_DESC = 'Filial '         + @V_CDFILIAL                       +
                    ' Lancamento '    + @V_NRLANCTONF                     +
                    ' Nr. Sequencial '+ @V_NRSEQVENPAG                    +
                    ' Nr. Parcela '   + @V_NRPARCELAPAG                   +
                    ' Dt. Ajuste '    + CONVERT(VARCHAR, @V_DTAJUSTEPAG)  +
                    ' Codigo '        + @V_CDAJUCTPAG                     +
                    ' Descricao '     + @V_DSAJUSTEPAG                    +
                    ' Valor '         + CONVERT(VARCHAR,@V_VRAJUSTEPAG);

      SET @V_ITPESQ  = @V_CDFILIAL+@V_NRLANCTONF+
                       @V_NRSEQVENPAG+@V_NRPARCELAPAG;

      SET @V_CDFILLOGOPER = @V_CDFILIAL;

      IF @V_TPMOVI <> '0' BEGIN
         EXEC NMUSUARIO  @V_USUARIO;
         EXEC WEBTUNNING @V_DSREMOTEADDR,
                         @V_DSREMOTEHOST,
                         @V_DSSERVERRDM,
                         @V_DSSERVMETHOD,
                         @V_DSREMOTEOPER;

         IF NOT @V_DESC IS NULL BEGIN
            IF NOT @V_USUARIO IS NULL BEGIN

               INSERT INTO LOGOPERA
                  (NMTABELA, DTOPERACAO, NMRESPOPER, IDTIPOOPER, CDPESQITEM,
                   DSOPERACAO, CDFILLOGOPER, DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER,
                   DSMETUSUOPER, CDORESPOPER)
               VALUES
                  ('AJCTPAGAR', GETDATE(), @V_USUARIO, @V_TPMOVI, @V_ITPESQ,
                   SUBSTRING(@V_DESC,1,300), @V_CDFILLOGOPER, @V_DSSERVERRDM, @V_DSREMOTEADDR,
                   @V_DSREMOTEHOST, @V_DSSERVMETHOD, @V_DSREMOTEOPER);
            END;
         END;
      END;

      FETCH NEXT FROM C_CURSOR_AJ INTO @V_TIPO,
                                       @V_CDFILIAL, @V_NRLANCTONF, @V_NRSEQVENPAG, @V_NRPARCELAPAG,
                                       @V_DTAJUSTEPAG, @V_CDAJUCTPAG, @V_DSAJUSTEPAG, @V_VRAJUSTEPAG;
   END;
   CLOSE C_CURSOR_AJ;
   DEALLOCATE C_CURSOR_AJ;
END;
GO


-- VERSAO --
EXEC ATUVEROBJ 'LOG_FIN_AJCTPAGAR','5.09.000';
GO
