IF EXISTS(SELECT * FROM sysobjects O, sysusers U
          WHERE
            o.name = 'ATUALIZA_ADIANTAMENTOPAG'  and
            type = 'TR'               and
            u.uid = o.uid             and
            u.name = USER)
   DROP TRIGGER ATUALIZA_ADIANTAMENTOPAG
GO

CREATE TRIGGER ATUALIZA_ADIANTAMENTOPAG
ON ADIANTAPAGAR
AFTER DELETE, INSERT
AS
BEGIN
   DECLARE @V_IDCOMANDO       VARCHAR(01);
   DECLARE @V_OLDCDFILIAL     VARCHAR(04);
   DECLARE @V_OLDNRLANCTONF   VARCHAR(10);
   DECLARE @V_OLDNRSEQVENPAG  VARCHAR(02);
   DECLARE @V_OLDNRPARCELAPAG VARCHAR(02);
   DECLARE @V_OLDVRADIANTAPAG NUMERIC(13,2);
   DECLARE @V_NEWCDFILIAL     VARCHAR(04);
   DECLARE @V_NEWNRLANCTONF   VARCHAR(10);
   DECLARE @V_NEWNRSEQVENPAG  VARCHAR(02);
   DECLARE @V_NEWNRPARCELAPAG VARCHAR(02);
   DECLARE @V_NEWVRADIANTAPAG NUMERIC(13,2);


   DECLARE C_DADOS CURSOR FOR
      SELECT 'D' IDCOMANDO,
             '',
             '',
             '',
             '',
             0,
             CDFILIAL,
             NRLANCTONF,
             NRSEQVENPAG,
             NRPARCELAPAG,
             VRADIANTAPAG
        FROM DELETED
       WHERE 0 = (SELECT COUNT(*) FROM INSERTED)

      UNION ALL

      SELECT 'I' IDCOMANDO,
             CDFILIAL,
             NRLANCTONF,
             NRSEQVENPAG,
             NRPARCELAPAG,
             VRADIANTAPAG,
             '',
             '',
             '',
             '',
             0
        FROM INSERTED
       WHERE 0 = (SELECT COUNT(*) FROM DELETED);

   OPEN C_DADOS;
   FETCH NEXT FROM C_DADOS INTO @V_IDCOMANDO,
                                @V_NEWCDFILIAL,
                                @V_NEWNRLANCTONF,
                                @V_NEWNRSEQVENPAG,
                                @V_NEWNRPARCELAPAG,
                                @V_NEWVRADIANTAPAG,
                                @V_OLDCDFILIAL,
                                @V_OLDNRLANCTONF,
                                @V_OLDNRSEQVENPAG,
                                @V_OLDNRPARCELAPAG,
                                @V_OLDVRADIANTAPAG
   WHILE (@@FETCH_STATUS = 0) BEGIN

      IF @V_IDCOMANDO = 'I' BEGIN /*INSERTING*/
         UPDATE CONTAPAGAR
            SET VRADIAPAGUTL = ISNULL(VRADIAPAGUTL, 0) + @V_NEWVRADIANTAPAG
          WHERE CDFILIAL = @V_NEWCDFILIAL
            AND NRLANCTONF = @V_NEWNRLANCTONF
            AND NRSEQVENPAG = @V_NEWNRSEQVENPAG
            AND NRPARCELAPAG = @V_NEWNRPARCELAPAG;
      END;
      ELSE BEGIN
         IF @V_IDCOMANDO = 'D' BEGIN /*DELETING*/
            UPDATE CONTAPAGAR
               SET VRADIAPAGUTL = ISNULL(VRADIAPAGUTL, 0) - @V_OLDVRADIANTAPAG
             WHERE CDFILIAL = @V_OLDCDFILIAL
               AND NRLANCTONF = @V_OLDNRLANCTONF
               AND NRSEQVENPAG = @V_OLDNRSEQVENPAG
               AND NRPARCELAPAG = @V_OLDNRPARCELAPAG;
         END;
      END;

      FETCH NEXT FROM C_DADOS INTO @V_IDCOMANDO,
                                   @V_NEWCDFILIAL,
                                   @V_NEWNRLANCTONF,
                                   @V_NEWNRSEQVENPAG,
                                   @V_NEWNRPARCELAPAG,
                                   @V_NEWVRADIANTAPAG,
                                   @V_OLDCDFILIAL,
                                   @V_OLDNRLANCTONF,
                                   @V_OLDNRSEQVENPAG,
                                   @V_OLDNRPARCELAPAG,
                                   @V_OLDVRADIANTAPAG
   END;
   CLOSE      C_DADOS;
   DEALLOCATE C_DADOS;
END;
GO

-- VERSAO --
EXEC ATUVEROBJ 'ATUALIZA_ADIANTAMENTOPAG','5.11.000';
GO
