DECLARE @C_NOTA_CDFILIAL       VARCHAR(4);
DECLARE @C_NOTA_NRLANCTONF     VARCHAR(10);

DECLARE @C_CONTA_CDFILIAL      VARCHAR(4);
DECLARE @C_CONTA_NRLANCTONF    VARCHAR(10);
DECLARE @C_CONTA_NRSEQVENPAG   VARCHAR(2);
DECLARE @C_CONTA_NRPARCELAPAG  VARCHAR(2);
DECLARE @C_CONTA_CDFILIBPAG    VARCHAR(4);
DECLARE @C_CONTA_NRLANBOLPAG   VARCHAR(10);
DECLARE @C_CONTA_CDCLASSFINA   VARCHAR(6);
DECLARE @C_CONTA_CDEMPRESA     VARCHAR(2);
DECLARE @C_CONTA_CDFIMOVFLPAG  VARCHAR(4);
DECLARE @C_CONTA_DTMOVFLUXO    DATETIME;
DECLARE @C_CONTA_IDMOVFLUXO    VARCHAR(1);
DECLARE @C_CONTA_VRORIGPAG     DOUBLE PRECISION;
DECLARE @V_AUX                 DOUBLE PRECISION;
DECLARE @C_CONTA_IDSTATPAG     VARCHAR(1);

DECLARE @C_AJUSTE_CDCLASSFINA  VARCHAR(6);
DECLARE @C_AJUSTE_CDEMMFAJCNTP VARCHAR(2);
DECLARE @C_AJUSTE_CDFIMFAJCNTP VARCHAR(4);
DECLARE @C_AJUSTE_DTMOVFLUXO   DATETIME;
DECLARE @C_AJUSTE_IDMOVFLUXO   VARCHAR(1);
DECLARE @C_AJUSTE_VRAJUSTEPAG  DOUBLE PRECISION;

BEGIN
   DECLARE C_NOTA CURSOR FOR
     (SELECT CDFILIAL, NRLANCTONF
      FROM NOTAFISCAL N
      WHERE
         N.IDSISTEMA  = 'CMP' AND
         N.IDSTATUS   = 'V'   AND
         NOT EXISTS (SELECT 1
                     FROM ITEMAUTF I
                     WHERE I.CDFILLANC  = N.CDFILIAL
                       AND I.NRLANCTONF = N.NRLANCTONF))
   OPEN C_NOTA;
   FETCH NEXT FROM C_NOTA INTO @C_NOTA_CDFILIAL,
                               @C_NOTA_NRLANCTONF;
   WHILE (@@FETCH_STATUS = 0)
   BEGIN
      DECLARE C_CONTA CURSOR FOR
        (SELECT
            C.CDFILIAL,     C.NRLANCTONF,  C.NRSEQVENPAG, C.NRPARCELAPAG,
            C.CDFILIBPAG,   C.NRLANBOLPAG, C.CDCLASSFINA, C.CDEMPRESA,
            C.CDFIMOVFLPAG, C.DTMOVFLUXO,  C.IDMOVFLUXO,  C.VRORIGPAG,
            C.IDSTATPAG
         FROM
            CONTAPAGAR C
         WHERE
            C.CDFILIAL   = @C_NOTA_CDFILIAL AND
            C.NRLANCTONF = @C_NOTA_NRLANCTONF)
      OPEN C_CONTA;
      FETCH NEXT FROM C_CONTA INTO @C_CONTA_CDFILIAL, @C_CONTA_NRLANCTONF, @C_CONTA_NRSEQVENPAG,
                                   @C_CONTA_NRPARCELAPAG, @C_CONTA_CDFILIBPAG, @C_CONTA_NRLANBOLPAG,
                                   @C_CONTA_CDCLASSFINA, @C_CONTA_CDEMPRESA, @C_CONTA_CDFIMOVFLPAG,
                                   @C_CONTA_DTMOVFLUXO, @C_CONTA_IDMOVFLUXO, @C_CONTA_VRORIGPAG,
                                   @C_CONTA_IDSTATPAG;
      WHILE (@@FETCH_STATUS = 0)
      BEGIN
         IF (@C_CONTA_IDSTATPAG = 'A') BEGIN
            DECLARE C_AJUSTE CURSOR FOR
               (SELECT
                  CDCLASSFINA, CDEMMFAJCNTP, CDFIMFAJCNTP,
                  DTMOVFLUXO, IDMOVFLUXO, VRAJUSTEPAG
               FROM
                  AJCTPAGAR
               WHERE
                  CDFILIAL     = @C_CONTA_CDFILIAL     AND
                  NRLANCTONF   = @C_CONTA_NRLANCTONF   AND
                  NRSEQVENPAG  = @C_CONTA_NRSEQVENPAG  AND
                  NRPARCELAPAG = @C_CONTA_NRPARCELAPAG)
            OPEN C_AJUSTE;
            FETCH NEXT FROM C_AJUSTE INTO @C_AJUSTE_CDCLASSFINA, @C_AJUSTE_CDEMMFAJCNTP,
                                          @C_AJUSTE_CDFIMFAJCNTP, @C_AJUSTE_DTMOVFLUXO,
                                          @C_AJUSTE_IDMOVFLUXO, @C_AJUSTE_VRAJUSTEPAG;
            WHILE (@@FETCH_STATUS = 0)
            BEGIN
               IF (@C_AJUSTE_CDEMMFAJCNTP IS NOT NULL) AND (@C_AJUSTE_CDFIMFAJCNTP IS NOT NULL) AND
                  (@C_AJUSTE_DTMOVFLUXO   IS NOT NULL) AND (@C_AJUSTE_IDMOVFLUXO   IS NOT NULL) AND
                  (@C_AJUSTE_CDCLASSFINA  IS NOT NULL) BEGIN
                  SET @V_AUX = NULL;
                  SET @V_AUX = @C_AJUSTE_VRAJUSTEPAG * (-1);
                  EXEC ATUALIZA_FLUXO_DE_CAIXA @C_AJUSTE_CDEMMFAJCNTP, @C_AJUSTE_CDFIMFAJCNTP,
                                               @C_AJUSTE_DTMOVFLUXO, @C_AJUSTE_IDMOVFLUXO,
                                               @C_AJUSTE_CDCLASSFINA, 0, @V_AUX;
               END;
               FETCH NEXT FROM C_AJUSTE INTO @C_AJUSTE_CDCLASSFINA, @C_AJUSTE_CDEMMFAJCNTP,
                                             @C_AJUSTE_CDFIMFAJCNTP, @C_AJUSTE_DTMOVFLUXO,
                                             @C_AJUSTE_IDMOVFLUXO, @C_AJUSTE_VRAJUSTEPAG;

            END;
            CLOSE C_AJUSTE;
            DEALLOCATE C_AJUSTE;

            DELETE FROM AJCTPAGAR
            WHERE
               CDFILIAL   = @C_CONTA_CDFILIAL AND
               NRLANCTONF = @C_CONTA_NRLANCTONF;

            IF (@C_CONTA_DTMOVFLUXO  IS NOT NULL) AND (@C_CONTA_CDFIMOVFLPAG IS NOT NULL)  AND
               (@C_CONTA_CDCLASSFINA IS NOT NULL) AND (@C_CONTA_IDMOVFLUXO   IS NOT NULL) AND
               (@C_CONTA_CDEMPRESA   IS NOT NULL) BEGIN
               SET @V_AUX = NULL;
               SET @V_AUX = @C_CONTA_VRORIGPAG * (-1);
               EXEC ATUALIZA_FLUXO_DE_CAIXA @C_CONTA_CDEMPRESA, @C_CONTA_CDFIMOVFLPAG,
                                            @C_CONTA_DTMOVFLUXO, @C_CONTA_IDMOVFLUXO,
                                            @C_CONTA_CDCLASSFINA, 0, @V_AUX;
            END;

            DELETE FROM CONTAPAGAR
            WHERE
               CDFILIAL     = @C_CONTA_CDFILIAL     AND
               NRLANCTONF   = @C_CONTA_NRLANCTONF   AND
               NRSEQVENPAG  = @C_CONTA_NRSEQVENPAG  AND
               NRPARCELAPAG = @C_CONTA_NRPARCELAPAG;

            IF (@C_CONTA_CDFILIBPAG IS NOT NULL) AND (@C_CONTA_NRLANBOLPAG IS NOT NULL) BEGIN
               DELETE FROM AJBOLPAG
               WHERE
                  CDFILIAL    = @C_CONTA_CDFILIBPAG AND
                  NRLANBOLPAG = @C_CONTA_NRLANBOLPAG;

               DELETE FROM BOLETPAGAR
               WHERE
                  CDFILIAL    = @C_CONTA_CDFILIBPAG AND
                  NRLANBOLPAG = @C_CONTA_NRLANBOLPAG;
            END;
         END;
         FETCH NEXT FROM C_CONTA INTO @C_CONTA_CDFILIAL, @C_CONTA_NRLANCTONF, @C_CONTA_NRSEQVENPAG,
                                      @C_CONTA_NRPARCELAPAG, @C_CONTA_CDFILIBPAG, @C_CONTA_NRLANBOLPAG,
                                      @C_CONTA_CDCLASSFINA, @C_CONTA_CDEMPRESA, @C_CONTA_CDFIMOVFLPAG,
                                      @C_CONTA_DTMOVFLUXO, @C_CONTA_IDMOVFLUXO, @C_CONTA_VRORIGPAG,
                                      @C_CONTA_IDSTATPAG;
      END;
      CLOSE C_CONTA;
      DEALLOCATE C_CONTA;
      DELETE FROM NOTAFISCAL
      WHERE
         CDFILIAL     = @C_NOTA_CDFILIAL    AND
         NRLANCTONF   = @C_NOTA_NRLANCTONF;
      FETCH NEXT FROM C_NOTA INTO @C_NOTA_CDFILIAL,
                                  @C_NOTA_NRLANCTONF;
   END;
   CLOSE C_NOTA;
   DEALLOCATE C_NOTA;
END;
