CREATE OR REPLACE PROCEDURE CANCELA_DADOS_FINANCEIRO(P_CDFILIAL IN NOTAFISCAL.CDFILIAL%TYPE,
                                                     P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE,
                                                     P_IDCANFIN IN VARCHAR2 DEFAULT 'S',
                                                     P_RETORNO OUT NUMBER)
AS
   V_COUNT NUMBER;
   V_RESULTADO NUMBER;
   V_FATOR NUMBER;
   V_IDENTRSAID VARCHAR2(1);
   V_CDTIPOOPER    NOTAFISCAL.CDTIPOOPER%TYPE;

   /*DADOS DO TIPO DE OPERACAO*/
   CURSOR C_TIPOOPER (P_CDTIPOOPER VARCHAR2,
                      P_IDENTRSAIDOP VARCHAR2)
   IS
      SELECT CDTPCTPAGAR, CDTPCTRECEB, IDGERAVENC, IDUTILVENC, CDHISTPADR, IDGERAVENCNFAP
        FROM TIPOOPERAC
       WHERE CDTIPOOPER = P_CDTIPOOPER
         AND IDUTILOPER <> 'N'
         AND IDENTRSAIDOP = P_IDENTRSAIDOP;
   RECORD_TIPOOPER C_TIPOOPER%ROWTYPE;

BEGIN
   /*BUSCA AS INFORMACOES DA NOTA FISCAL*/
   SELECT IDENTRSAIDOP, CDTIPOOPER
     INTO V_IDENTRSAID, V_CDTIPOOPER
     FROM NOTAFISCAL
    WHERE CDFILIAL = P_CDFILIAL
      AND NRLANCTONF = P_NRLANCTONF;
   /*INFORMACOES SOBRE O TIPO DE OPERACAO*/
   OPEN C_TIPOOPER(V_CDTIPOOPER, V_IDENTRSAID);
   FETCH C_TIPOOPER INTO RECORD_TIPOOPER;

   /*NOTA FISCAL DE ENTRADA*/
   IF V_IDENTRSAID = 'E' THEN
       /*VERIFICA SE EXISTE GUIAPAGDOC OU ITEMNOTAGPS REFERENTE A GUIA DO IMPOSTO*/
       FOR C_NFDARF IN (SELECT GP.CDFILIAL, GP.NRLANCTONFDARF, CP.CDFILIBPAG, CP.NRLANBOLPAG
                          FROM CONTAPAGAR CP, GUIAPAGDOC GP
                         WHERE GP.CDFILIAL       = CP.CDFILIAL
                           AND GP.NRLANCTONFDARF = CP.NRLANCTONF
                           AND GP.CDFILIAL       = P_CDFILIAL
                           AND GP.NRLANCTONF     = P_NRLANCTONF
                         UNION ALL
                        SELECT IT.CDFILIALGPS, IT.NRLANCTONFGPS, CP.CDFILIBPAG, CP.NRLANBOLPAG
                          FROM CONTAPAGAR CP, ITEMNOTAGPS IT
                         WHERE IT.CDFILIALGPS    = CP.CDFILIAL
                           AND IT.NRLANCTONFGPS  = CP.NRLANCTONF
                           AND IT.CDFILIAL       = P_CDFILIAL
                           AND IT.NRLANCTONF     = P_NRLANCTONF)LOOP

          /*VERIFICA EXISTENCIA DE CONTA A PAGAR BAIXADA*/
         SELECT COUNT(BO.CDFILIAL)
           INTO V_COUNT
           FROM CONTAPAGAR CP,
                BOLETPAGAR BO
          WHERE (BO.CDFILIAL    = C_NFDARF.CDFILIAL)
            AND (BO.NRLANBOLPAG = C_NFDARF.NRLANCTONFDARF)
            AND (BO.CDFILIAL    = CP.CDFILIBPAG)
            AND (BO.NRLANBOLPAG = CP.NRLANBOLPAG)
            AND (BO.NRBAIXAPAG IS NOT NULL);
          IF V_COUNT > 0 THEN
            P_RETORNO := 9;
          ELSE
             /*VERIFICA EXISTENCIA DE CONTAS PARCELADAS REFERENTE A GUIA DO IMPOSTO*/
            SELECT COUNT(*)
              INTO V_COUNT
              FROM CONTAPAGAR CP
             WHERE (CP.CDFILIAL = C_NFDARF.CDFILIAL)
               AND (CP.NRLANCTONF = C_NFDARF.NRLANCTONFDARF)
               AND (SELECT COUNT(*)
                      FROM CONTAPAGAR
                     WHERE CDFILIAL = C_NFDARF.CDFILIAL
                       AND NRLANCTONF = C_NFDARF.NRLANCTONFDARF
                       AND NRSEQVENPAG = CP.NRSEQVENPAG) > 1;
            IF V_COUNT > 0 THEN
               P_RETORNO := 10;
            ELSE
               /*VERIFICA EXISTENCIA DE BOLETO REAL REFERENTE A GUIA DO IMPOSTO*/
               SELECT COUNT(BO.CDFILIAL)
                 INTO V_COUNT
                 FROM CONTAPAGAR CP,
                      BOLETPAGAR BO
                WHERE (CP.CDFILIAL = C_NFDARF.CDFILIAL)
                  AND (CP.NRLANCTONF = C_NFDARF.NRLANCTONFDARF)
                  AND (BO.CDFILIAL = CP.CDFILIBPAG)
                  AND (BO.NRLANBOLPAG = CP.NRLANBOLPAG)
                  AND (BO.IDBPAGREAL = 'S');
               IF V_COUNT > 0 THEN
                  P_RETORNO := 12;
               ELSE
                  /*VERIFICA EXISTENCIA DE BOLETO LIGADO A REMESSA REFERENTE A GUIA DO IMPOSTO*/
                  SELECT COUNT(BO.CDFILIAL)
                    INTO V_COUNT
                    FROM CONTAPAGAR CP,
                         BOLETPAGAR BO
                   WHERE (CP.CDFILIAL = C_NFDARF.CDFILIAL)
                     AND (CP.NRLANCTONF = C_NFDARF.NRLANCTONFDARF)
                     AND (BO.CDFILIAL = CP.CDFILIBPAG)
                     AND (BO.NRLANBOLPAG = CP.NRLANBOLPAG)
                     AND (IDSITUCNABP <> '00');
                  IF V_COUNT > 0 THEN
                     P_RETORNO := 13;
                  ELSE
                     /*VERIFICA EXISTENCIA DE BOLETO ASSOCIADO A NOTA FISCAL DIFERENTE REFERENTE A GUIA DO IMPOSTO*/
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM (SELECT DISTINCT C.CDFILIAL, C.NRLANCTONF
                               FROM CONTAPAGAR C,
                                    (SELECT DISTINCT CDFILIBPAG as CDFILIAL, NRLANBOLPAG
                                       FROM CONTAPAGAR CP
                                      WHERE (CP.CDFILIAL   = C_NFDARF.CDFILIAL)
                                        AND (CP.NRLANCTONF = C_NFDARF.NRLANCTONFDARF)) BO
                      WHERE BO.CDFILIAL    = C.CDFILIBPAG
                        AND BO.NRLANBOLPAG = C.NRLANBOLPAG) A;

                     IF V_COUNT > 1 THEN
                          P_RETORNO := 14;
                     ELSE
                        /*VERIFICA SE A NOTA FISCAL DE DEVOLUCAO TEM VALOR UTILIZADO REFERENTE A GUIA DO IMPOSTO*/
                        SELECT NVL(VRDEVOLNFUTL,0) VRDEVOLNFUTL
                          INTO V_COUNT
                          FROM NOTAFISCAL
                         WHERE CDFILIAL   = C_NFDARF.CDFILIAL
                           AND NRLANCTONF = C_NFDARF.NRLANCTONFDARF;
                        IF V_COUNT > 0 THEN
                           P_RETORNO := 15;
                        ELSE
                           
                           SELECT COUNT(B.CDFILIAL)
                             INTO V_COUNT
                             FROM CONTAPAGAR C, BOLETPAGAR B
                            WHERE C.CDFILIAL = C_NFDARF.CDFILIAL
                              AND C.NRLANCTONF = C_NFDARF.NRLANCTONFDARF
                              AND C.CDFILIBPAG = B.CDFILIAL
                              AND C.NRLANBOLPAG = B.NRLANBOLPAG
                              AND B.NRLANCANTECIPA IS NOT NULL;
                           IF V_COUNT > 0 THEN
                              P_RETORNO := 12;
                           ELSE
                              
                              SELECT COUNT(AJ.CDFILIAL)
                                INTO V_COUNT
                                FROM CONTAPAGAR CP,
                                     AJBOLPAG   AJ
                               WHERE (CP.CDFILIAL = C_NFDARF.CDFILIAL)
                                 AND (CP.NRLANCTONF = C_NFDARF.NRLANCTONFDARF)
                                 AND (AJ.CDFILIAL = CP.CDFILIBPAG)
                                 AND (AJ.NRLANBOLPAG = CP.NRLANBOLPAG);
                              IF V_COUNT > 0 THEN
                                 P_RETORNO := 11;
                              ELSE
                                 P_RETORNO := 0;
                                 IF (NVL(P_IDCANFIN, 'S') = 'S') THEN
                                   /*APAGA AS CONTAS A PAGAR REFERENTES A GUIA*/
                                   FOR C_CNTPAGAR IN (SELECT CDFILIAL, NRLANCTONF, NRSEQVENPAG, NRPARCELAPAG,
                                                             DTATUAVENPAG, VRORIGPAG, IDMOVFLUXO, DTMOVFLUXO,
                                                             CDCLASSFINA, CDFILIBPAG, NRLANBOLPAG, CDEMPRESA, CDFIMOVFLPAG
                                                        FROM CONTAPAGAR
                                                       WHERE CDFILIAL = C_NFDARF.CDFILIAL
                                                         AND NRLANCTONF = C_NFDARF.NRLANCTONFDARF) LOOP
                                      /*ATUALIZA ORCAMENTO FINANCEIRO REFERENTES A GUIA*/
                                      ORCAMENTO_OK(C_CNTPAGAR.CDEMPRESA,
                                                   C_CNTPAGAR.CDFIMOVFLPAG,
                                                   C_CNTPAGAR.CDCLASSFINA,
                                                   C_CNTPAGAR.DTATUAVENPAG,
                                                   C_CNTPAGAR.VRORIGPAG * -1,
                                                   V_RESULTADO);
                                      /*APAGA O RATEIO DA CONTA A PAGAR REFERENTES A GUIA*/
                                      DELETE FROM RATCTPAGAR
                                       WHERE CDFILIAL     = C_CNTPAGAR.CDFILIAL
                                         AND NRLANCTONF   = C_CNTPAGAR.NRLANCTONF
                                         AND NRSEQVENPAG  = C_CNTPAGAR.NRSEQVENPAG
                                         AND NRPARCELAPAG = C_CNTPAGAR.NRPARCELAPAG;
                                      /*APAGA OS AJUSTES DA CONTA A PAGAR*/
                                      FOR C_AJPAGAR IN (SELECT AJ.CDFILIAL, AJ.NRLANCTONF, AJ.NRSEQVENPAG, AJ.NRPARCELAPAG,
                                                               AJ.DTAJUSTEPAG, AJ.CDAJUCTPAG, T.IDAJUCTPAG, AJ.CDEMMFAJCNTP,
                                                               AJ.CDFIMFAJCNTP, AJ.CDCLASSFINA, AJ.IDMOVFLUXO, AJ.VRAJUSTEPAG,
                                                               AJ.DTMOVFLUXO
                                                          FROM AJCTPAGAR AJ, TIPOAJPAGAR T
                                                         WHERE AJ.CDFILIAL = C_CNTPAGAR.CDFILIAL
                                                           AND AJ.NRLANCTONF = C_CNTPAGAR.NRLANCTONF
                                                           AND AJ.NRSEQVENPAG = C_CNTPAGAR.NRSEQVENPAG
                                                           AND AJ.NRPARCELAPAG = C_CNTPAGAR.NRPARCELAPAG
                                                           AND AJ.CDAJUCTPAG = T.CDAJUCTPAG) LOOP
                                         IF C_AJPAGAR.IDAJUCTPAG = 'D' THEN
                                            V_FATOR := 1;
                                         ELSE
                                            V_FATOR := -1;
                                         END IF;
                                         IF NOT (C_AJPAGAR.CDEMMFAJCNTP IS NULL)
                                            AND NOT (C_AJPAGAR.CDFIMFAJCNTP IS NULL)
                                            AND NOT (C_AJPAGAR.DTMOVFLUXO IS NULL)
                                            AND NOT (C_AJPAGAR.CDCLASSFINA IS NULL) THEN
                                            /*ATUALIZACAO DO FLUXO DE CAIXA DO AJUSTE REFERENTES A GUIA*/
                                            ATUALIZA_FLUXO_DE_CAIXA(C_AJPAGAR.CDEMMFAJCNTP,
                                                                    C_AJPAGAR.CDFIMFAJCNTP,
                                                                    C_AJPAGAR.DTMOVFLUXO,
                                                                    C_AJPAGAR.IDMOVFLUXO,
                                                                    C_AJPAGAR.CDCLASSFINA,
                                                                    0,
                                                                    C_AJPAGAR.VRAJUSTEPAG * V_FATOR);
                                         END IF;
                                         /*APAGA OS AJUSTES DA CONTA A PAGAR REFERENTES A GUIA*/
                                         DELETE FROM AJCTPAGAR
                                          WHERE (CDFILIAL     = C_AJPAGAR.CDFILIAL)
                                            AND (NRLANCTONF   = C_AJPAGAR.NRLANCTONF)
                                            AND (NRSEQVENPAG  = C_AJPAGAR.NRSEQVENPAG)
                                            AND (NRPARCELAPAG = C_AJPAGAR.NRPARCELAPAG)
                                            AND (DTAJUSTEPAG  = C_AJPAGAR.DTAJUSTEPAG)
                                            AND (CDAJUCTPAG   = C_AJPAGAR.CDAJUCTPAG);
                                      END LOOP;
                                      
                                      IF NOT (C_CNTPAGAR.CDEMPRESA IS NULL)
                                         AND NOT (C_CNTPAGAR.CDFIMOVFLPAG IS NULL)
                                         AND NOT (C_CNTPAGAR.DTMOVFLUXO IS NULL)
                                         AND NOT (C_CNTPAGAR.CDCLASSFINA IS NULL) THEN
                                         ATUALIZA_FLUXO_DE_CAIXA(C_CNTPAGAR.CDEMPRESA,
                                                                 C_CNTPAGAR.CDFIMOVFLPAG,
                                                                 C_CNTPAGAR.DTMOVFLUXO,
                                                                 C_CNTPAGAR.IDMOVFLUXO,
                                                                 C_CNTPAGAR.CDCLASSFINA,
                                                                 0,
                                                                 C_CNTPAGAR.VRORIGPAG * -1);
                                      END IF;
                                      /*ATUALIZA AS INFORMACOES DA CONTA A PAGAR REFERENTES A GUIA, RETIRANDO
                                      AS INFORMACOES ATUALIZADAS NA APROVACAO*/
                                      UPDATE CONTAPAGAR
                                         SET
                                            CDTPCTPAGAR = NULL,
                                            CDFILIBPAG = NULL,
                                            NRLANBOLPAG = NULL,
                                            CDCLASSFINA = NULL,
                                            CDEMPRESA = NULL,
                                            CDFIMOVFLPAG = NULL,
                                            DTMOVFLUXO = NULL,
                                            IDMOVFLUXO = NULL,
                                            DSCONTAPAG = DECODE(IDDSCONTANF,'N',NULL,DSCONTAPAG),
                                            VRATUAPAG =  VRORIGPAG
                                         WHERE CDFILIAL = C_CNTPAGAR.CDFILIAL
                                           AND NRLANCTONF = C_CNTPAGAR.NRLANCTONF
                                           AND NRSEQVENPAG = C_CNTPAGAR.NRSEQVENPAG
                                           AND NRPARCELAPAG = C_CNTPAGAR.NRPARCELAPAG;

                                      SELECT COUNT(*)
                                        INTO V_COUNT
                                        FROM CONTAPAGAR
                                       WHERE CDFILIBPAG = C_CNTPAGAR.CDFILIBPAG
                                         AND NRLANBOLPAG = C_CNTPAGAR.NRLANBOLPAG;

                                      /*APAGA O BOLETO RELACIONADO A CONTA A PAGAR REFERENTE A GUIA*/
                                      IF V_COUNT = 0 THEN
                                         DELETE FROM BOLETPAGAR
                                          WHERE CDFILIAL = C_CNTPAGAR.CDFILIBPAG
                                            AND NRLANBOLPAG = C_CNTPAGAR.NRLANBOLPAG;
                                         /*APAGA A CONTA A PAGAR REFERENTE A GUIA*/
                                         DELETE FROM CONTAPAGAR
                                          WHERE CDFILIAL = C_CNTPAGAR.CDFILIAL
                                            AND NRLANCTONF = C_CNTPAGAR.NRLANCTONF;

                                         DELETE FROM NOTAFISCAL
                                          WHERE CDFILIAL = C_CNTPAGAR.CDFILIAL
                                            AND NRLANCTONF = C_CNTPAGAR.NRLANCTONF;
                                      END IF;
                                   END LOOP;

                                   /*ATUALIZACAO DAS CONTAS A PAGAR RELACIONADAS A AUTORIZACAO DE FORNECIMENTO*/
                                   FOR C_NOTA IN (SELECT CDFILIALIT, NRLANCTONFIT
                                                    FROM ITEMNOTAFISC
                                                   WHERE CDFILIAL = P_CDFILIAL
                                                     AND NRLANCTONF = P_NRLANCTONF) LOOP

                                      AJUSTE_CONTAPAGAR_NF(C_NOTA.CDFILIALIT,
                                                           C_NOTA.NRLANCTONFIT,
                                                          'P');
                                   END LOOP;

                                   AJUSTE_CONTAPAGAR_NF(P_CDFILIAL,
                                                        P_NRLANCTONF,
                                                        'P');
                                 END IF;
                              END IF;
                           END IF;
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END IF;
          END IF;
       END LOOP;
      IF ((P_RETORNO IS NULL) OR (P_RETORNO = 0)) THEN
         /*VERIFICA EXISTENCIA DE CONTA A PAGAR BAIXADA*/
         SELECT COUNT(BO.CDFILIAL)
           INTO V_COUNT
           FROM CONTAPAGAR CP,
                BOLETPAGAR BO
          WHERE (CP.CDFILIAL    = P_CDFILIAL)
            AND (CP.NRLANCTONF  = P_NRLANCTONF)
            AND (BO.CDFILIAL    = CP.CDFILIBPAG)
            AND (BO.NRLANBOLPAG = CP.NRLANBOLPAG)
            AND (BO.NRBAIXAPAG IS NOT NULL);
         IF V_COUNT > 0 THEN
            P_RETORNO := 1;
         ELSE
            /*VERIFICA EXISTENCIA DE CONTAS PARCELADAS*/
            SELECT COUNT(*)
              INTO V_COUNT
              FROM CONTAPAGAR CP
             WHERE (CP.CDFILIAL = P_CDFILIAL)
               AND (CP.NRLANCTONF = P_NRLANCTONF)
               AND (SELECT COUNT(*)
                      FROM CONTAPAGAR
                     WHERE CDFILIAL = CP.CDFILIAL
                       AND NRLANCTONF = CP.NRLANCTONF
                       AND NRSEQVENPAG = CP.NRSEQVENPAG) > 1;
            IF V_COUNT > 0 THEN
               P_RETORNO := 2;
            ELSE

               /*VERIFICA EXISTENCIA DE BOLETO REAL*/
               SELECT COUNT(BO.CDFILIAL)
                 INTO V_COUNT
                 FROM CONTAPAGAR CP,
                      BOLETPAGAR BO
                WHERE (CP.CDFILIAL = P_CDFILIAL)
                  AND (CP.NRLANCTONF = P_NRLANCTONF)
                  AND (BO.CDFILIAL = CP.CDFILIBPAG)
                  AND (BO.NRLANBOLPAG = CP.NRLANBOLPAG)
                  AND (BO.IDBPAGREAL = 'S');
               IF V_COUNT > 0 THEN
                  P_RETORNO := 5;
               ELSE
                  /*VERIFICA EXISTENCIA DE BOLETO LIGADO A REMESSA*/
                  SELECT COUNT(BO.CDFILIAL)
                    INTO V_COUNT
                    FROM CONTAPAGAR CP,
                         BOLETPAGAR BO
                   WHERE (CP.CDFILIAL = P_CDFILIAL)
                     AND (CP.NRLANCTONF = P_NRLANCTONF)
                     AND (BO.CDFILIAL = CP.CDFILIBPAG)
                     AND (BO.NRLANBOLPAG = CP.NRLANBOLPAG)
                     AND (IDSITUCNABP <> '00');
                  IF V_COUNT > 0 THEN
                     P_RETORNO := 6;
                  ELSE
                     /*VERIFICA EXISTENCIA DE BOLETO ASSOCIADO A NOTA FISCAL DIFERENTE*/
                     SELECT COUNT(*)
                       INTO V_COUNT
                       FROM (SELECT DISTINCT C.CDFILIAL, C.NRLANCTONF
                               FROM CONTAPAGAR C,
                                    (SELECT DISTINCT CDFILIBPAG as CDFILIAL, NRLANBOLPAG
                                       FROM CONTAPAGAR CP
                                      WHERE (CP.CDFILIAL   = P_CDFILIAL)
                                        AND (CP.NRLANCTONF = P_NRLANCTONF)) BO
                      WHERE BO.CDFILIAL    = C.CDFILIBPAG
                        AND BO.NRLANBOLPAG = C.NRLANBOLPAG) A;

                     IF V_COUNT > 1 THEN
                          P_RETORNO := 7;
                     ELSE
                        /*VERIFICA SE A NOTA FISCAL DE DEVOLUCAO TEM VALOR UTILIZADO*/
                        SELECT NVL(VRDEVOLNFUTL,0) VRDEVOLNFUTL
                          INTO V_COUNT
                          FROM NOTAFISCAL
                         WHERE CDFILIAL   = P_CDFILIAL
                           AND NRLANCTONF = P_NRLANCTONF;
                        IF V_COUNT > 0 THEN
                           P_RETORNO := 8;
                        ELSE
                           
                           SELECT COUNT(B.CDFILIAL)
                             INTO V_COUNT
                             FROM CONTAPAGAR C, BOLETPAGAR B
                            WHERE C.CDFILIAL = P_CDFILIAL
                              AND C.NRLANCTONF = P_NRLANCTONF
                              AND C.CDFILIBPAG = B.CDFILIAL
                              AND C.NRLANBOLPAG = B.NRLANBOLPAG
                              AND B.NRLANCANTECIPA IS NOT NULL;
                           IF V_COUNT > 0 THEN
                              P_RETORNO := 5;
                           ELSE
                              
                              SELECT COUNT(AJ.CDFILIAL)
                                INTO V_COUNT
                                FROM CONTAPAGAR CP,
                                     AJBOLPAG   AJ
                               WHERE (CP.CDFILIAL = P_CDFILIAL)
                                 AND (CP.NRLANCTONF = P_NRLANCTONF)
                                 AND (AJ.CDFILIAL = CP.CDFILIBPAG)
                                 AND (AJ.NRLANBOLPAG = CP.NRLANBOLPAG);
                              IF V_COUNT > 0 THEN
                                 P_RETORNO := 4;
                              ELSE
                                 P_RETORNO := 0;
                                 IF (NVL(P_IDCANFIN, 'S') = 'S') THEN
                                    /*APAGA AS CONTAS A PAGAR*/
                                    FOR C_CNTPAGAR IN (SELECT CDFILIAL, NRLANCTONF, NRSEQVENPAG, NRPARCELAPAG,
                                                              DTATUAVENPAG, VRORIGPAG, IDMOVFLUXO, DTMOVFLUXO,
                                                              CDCLASSFINA, CDFILIBPAG, NRLANBOLPAG, CDEMPRESA, CDFIMOVFLPAG
                                                         FROM CONTAPAGAR
                                                        WHERE CDFILIAL = P_CDFILIAL
                                                          AND NRLANCTONF = P_NRLANCTONF) LOOP
                                       /*ATUALIZA ORCAMENTO FINANCEIRO*/
                                       ORCAMENTO_OK(C_CNTPAGAR.CDEMPRESA,
                                                    C_CNTPAGAR.CDFIMOVFLPAG,
                                                    C_CNTPAGAR.CDCLASSFINA,
                                                    C_CNTPAGAR.DTATUAVENPAG,
                                                    C_CNTPAGAR.VRORIGPAG * -1,
                                                    V_RESULTADO);
                                       /*APAGA O RATEIO DA CONTA A PAGAR*/
                                       DELETE FROM RATCTPAGAR
                                        WHERE CDFILIAL     = C_CNTPAGAR.CDFILIAL
                                          AND NRLANCTONF   = C_CNTPAGAR.NRLANCTONF
                                          AND NRSEQVENPAG  = C_CNTPAGAR.NRSEQVENPAG
                                          AND NRPARCELAPAG = C_CNTPAGAR.NRPARCELAPAG;
                                       /*APAGA OS AJUSTES DA CONTA A PAGAR*/
                                       FOR C_AJPAGAR IN (SELECT AJ.CDFILIAL, AJ.NRLANCTONF, AJ.NRSEQVENPAG, AJ.NRPARCELAPAG,
                                                                AJ.DTAJUSTEPAG, AJ.CDAJUCTPAG, T.IDAJUCTPAG, AJ.CDEMMFAJCNTP,
                                                                AJ.CDFIMFAJCNTP, AJ.CDCLASSFINA, AJ.IDMOVFLUXO, AJ.VRAJUSTEPAG,
                                                                AJ.DTMOVFLUXO
                                                           FROM AJCTPAGAR AJ, TIPOAJPAGAR T
                                                          WHERE AJ.CDFILIAL = C_CNTPAGAR.CDFILIAL
                                                            AND AJ.NRLANCTONF = C_CNTPAGAR.NRLANCTONF
                                                            AND AJ.NRSEQVENPAG = C_CNTPAGAR.NRSEQVENPAG
                                                            AND AJ.NRPARCELAPAG = C_CNTPAGAR.NRPARCELAPAG
                                                            AND AJ.CDAJUCTPAG = T.CDAJUCTPAG) LOOP
                                          IF C_AJPAGAR.IDAJUCTPAG = 'D' THEN
                                             V_FATOR := 1;
                                          ELSE
                                             V_FATOR := -1;
                                          END IF;
                                          IF NOT (C_AJPAGAR.CDEMMFAJCNTP IS NULL)
                                             AND NOT (C_AJPAGAR.CDFIMFAJCNTP IS NULL)
                                             AND NOT (C_AJPAGAR.DTMOVFLUXO IS NULL)
                                             AND NOT (C_AJPAGAR.CDCLASSFINA IS NULL) THEN
                                             /*ATUALIZACAO DO FLUXO DE CAIXA DO AJUSTE*/
                                             ATUALIZA_FLUXO_DE_CAIXA(C_AJPAGAR.CDEMMFAJCNTP,
                                                                     C_AJPAGAR.CDFIMFAJCNTP,
                                                                     C_AJPAGAR.DTMOVFLUXO,
                                                                     C_AJPAGAR.IDMOVFLUXO,
                                                                     C_AJPAGAR.CDCLASSFINA,
                                                                     0,
                                                                     C_AJPAGAR.VRAJUSTEPAG * V_FATOR);
                                          END IF;
                                          /*APAGA OS AJUSTES DA CONTA A PAGAR*/
                                          DELETE FROM AJCTPAGAR
                                           WHERE (CDFILIAL     = C_AJPAGAR.CDFILIAL)
                                             AND (NRLANCTONF   = C_AJPAGAR.NRLANCTONF)
                                             AND (NRSEQVENPAG  = C_AJPAGAR.NRSEQVENPAG)
                                             AND (NRPARCELAPAG = C_AJPAGAR.NRPARCELAPAG)
                                             AND (DTAJUSTEPAG  = C_AJPAGAR.DTAJUSTEPAG)
                                             AND (CDAJUCTPAG   = C_AJPAGAR.CDAJUCTPAG);
                                       END LOOP;
                                       
                                       IF NOT (C_CNTPAGAR.CDEMPRESA IS NULL)
                                          AND NOT (C_CNTPAGAR.CDFIMOVFLPAG IS NULL)
                                          AND NOT (C_CNTPAGAR.DTMOVFLUXO IS NULL)
                                          AND NOT (C_CNTPAGAR.CDCLASSFINA IS NULL) THEN
                                          ATUALIZA_FLUXO_DE_CAIXA(C_CNTPAGAR.CDEMPRESA,
                                                                  C_CNTPAGAR.CDFIMOVFLPAG,
                                                                  C_CNTPAGAR.DTMOVFLUXO,
                                                                  C_CNTPAGAR.IDMOVFLUXO,
                                                                  C_CNTPAGAR.CDCLASSFINA,
                                                                  0,
                                                                  C_CNTPAGAR.VRORIGPAG * -1);
                                       END IF;
                                       
                                       UPDATE CONTAPAGAR
                                          SET
                                             CDTPCTPAGAR = NULL,
                                             CDFILIBPAG = NULL,
                                             NRLANBOLPAG = NULL,
                                             CDCLASSFINA = NULL,
                                             CDEMPRESA = NULL,
                                             CDFIMOVFLPAG = NULL,
                                             DTMOVFLUXO = NULL,
                                             IDMOVFLUXO = NULL,
                                             DSCONTAPAG = DECODE(IDDSCONTANF,'N',NULL,DSCONTAPAG),
                                             VRATUAPAG =  VRORIGPAG,
                                             IDBLOQNFFINCP = DECODE(RECORD_TIPOOPER.IDGERAVENCNFAP,'S','B',NULL)
                                          WHERE CDFILIAL = C_CNTPAGAR.CDFILIAL
                                            AND NRLANCTONF = C_CNTPAGAR.NRLANCTONF
                                            AND NRSEQVENPAG = C_CNTPAGAR.NRSEQVENPAG
                                            AND NRPARCELAPAG = C_CNTPAGAR.NRPARCELAPAG;

                                       SELECT COUNT(*)
                                         INTO V_COUNT
                                         FROM CONTAPAGAR
                                        WHERE CDFILIBPAG = C_CNTPAGAR.CDFILIBPAG
                                          AND NRLANBOLPAG = C_CNTPAGAR.NRLANBOLPAG;

                                       /*APAGA O BOLETO RELACIONADO A CONTA A PAGAR*/
                                       IF V_COUNT = 0 THEN
                                          DELETE FROM BOLETPAGAR
                                           WHERE CDFILIAL = C_CNTPAGAR.CDFILIBPAG
                                             AND NRLANBOLPAG = C_CNTPAGAR.NRLANBOLPAG;
                                       END IF;
                                    END LOOP;

                                    /*ATUALIZACAO DAS CONTAS A PAGAR RELACIONADAS A AUTORIZACAO DE FORNECIMENTO*/
                                    FOR C_NOTA IN (SELECT CDFILIALIT, NRLANCTONFIT
                                                     FROM ITEMNOTAFISC
                                                    WHERE CDFILIAL = P_CDFILIAL
                                                      AND NRLANCTONF = P_NRLANCTONF) LOOP

                                       AJUSTE_CONTAPAGAR_NF(C_NOTA.CDFILIALIT,
                                                            C_NOTA.NRLANCTONFIT,
                                                           'P');
                                    END LOOP;

                                    AJUSTE_CONTAPAGAR_NF(P_CDFILIAL,
                                                         P_NRLANCTONF,
                                                         'P');
                                 END IF;
                              END IF;
                           END IF;
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END IF;
         END IF;
      END IF;
   /*NOTA FISCAL DE SAIDA*/
   ELSIF V_IDENTRSAID = 'S' THEN


      /*VERIFICA EXISTENCIA DE CONTA A RECEBER BAIXADA*/
      SELECT COUNT(BO.CDFILIAL)
        INTO V_COUNT
        FROM CONTARECEBER CR,
             BOLETRECEBER BO
       WHERE (CR.CDFILIAL    = P_CDFILIAL)
         AND (CR.NRLANCTONF  = P_NRLANCTONF)
         AND (BO.CDFILIAL    = CR.CDFILIBOLREC)
         AND (BO.NRLANBOLREC = CR.NRLANBOLREC)
         AND (BO.NRBAIXAREC IS NOT NULL);
      IF V_COUNT > 0 THEN
         P_RETORNO := 1;
      ELSE
         /*VERIFICA EXISTENCIA DE CONTAS PARCELADAS*/
         SELECT COUNT(*)
           INTO V_COUNT
           FROM CONTARECEBER CR
          WHERE (CR.CDFILIAL = P_CDFILIAL)
            AND (CR.NRLANCTONF = P_NRLANCTONF)
            AND (NVL(CR.IDPARCAUTOFOS,'N') = 'N')
            AND (SELECT COUNT(*)
                   FROM CONTARECEBER
                  WHERE CDFILIAL = CR.CDFILIAL
                    AND NRLANCTONF = CR.NRLANCTONF
                    AND NRSEQVENREC = CR.NRSEQVENREC) > 1;
         IF V_COUNT > 0 THEN
            P_RETORNO := 2;
         ELSE
            /*VERIFICA EXISTENCIA DE BOLETO EMITIDO*/
            SELECT COUNT(BO.CDFILIAL)
              INTO V_COUNT
              FROM CONTARECEBER CR,
                   BOLETRECEBER BO,
                   CLIENTE      CL
             WHERE (CR.CDFILIAL = P_CDFILIAL)
               AND (CR.NRLANCTONF = P_NRLANCTONF)
               AND (BO.CDFILIAL = CR.CDFILIBOLREC)
               AND (BO.NRLANBOLREC = CR.NRLANBOLREC)
               AND (BO.IDSTATBREC = 'E')
               AND (CL.CDCLIENTE = BO.CDCLIENTE)
               AND (NVL(CL.IDENVBOLANTNOTA,'N') <> 'S');
            IF V_COUNT > 0 THEN
               P_RETORNO := 5;
            ELSE
               /*VERIFICA EXISTENCIA DE BOLETO LIGADO A REMESSA*/
               SELECT COUNT(BO.CDFILIAL)
                 INTO V_COUNT
                 FROM CONTARECEBER CR,
                      BOLETRECEBER BO
                WHERE (CR.CDFILIAL = P_CDFILIAL)
                  AND (CR.NRLANCTONF = P_NRLANCTONF)
                  AND (BO.CDFILIAL = CR.CDFILIBOLREC)
                  AND (BO.NRLANBOLREC = CR.NRLANBOLREC)
                  AND (IDSITUCNABR <> '00');
               IF V_COUNT > 0 THEN
                  P_RETORNO := 6;
               ELSE
                  /*VERIFICA EXISTENCIA DE BOLETO ASSOCIADO A NOTA FISCAL DIFERENTE*/
                  SELECT COUNT(BO.CDFILIAL)
                    INTO V_COUNT
                    FROM CONTARECEBER CR,
                         BOLETRECEBER BO
                   WHERE (CR.CDFILIAL = P_CDFILIAL)
                     AND (CR.NRLANCTONF = P_NRLANCTONF)
                     AND (BO.CDFILIAL = CR.CDFILIBOLREC)
                     AND (BO.NRLANBOLREC = CR.NRLANBOLREC)
                     AND (SELECT COUNT(*)
                            FROM CONTARECEBER
                           WHERE CDFILIBOLREC = BO.CDFILIAL
                             AND NRLANBOLREC = BO.NRLANBOLREC) > 1;
                  IF V_COUNT > 0 THEN
                     P_RETORNO := 7;
                  ELSE
                     /*VERIFICA SE A NOTA FISCAL DE DEVOLUCAO TEM VALOR UTILIZADO*/
                     SELECT NVL(VRDEVOLNFUTL,0) VRDEVOLNFUTL
                       INTO V_COUNT
                       FROM NOTAFISCAL
                      WHERE CDFILIAL = P_CDFILIAL
                        AND NRLANCTONF = P_NRLANCTONF;
                     IF V_COUNT > 0 THEN
                        P_RETORNO := 8;
                     ELSE
                        /*PESQUISA A EXISTENCIA DE AJUSTES NOS BOLETOS DAS CONTAS*/
                        SELECT COUNT(AJ.CDFILIAL)
                          INTO V_COUNT
                          FROM CONTARECEBER CR,
                               AJBOLREC   AJ
                         WHERE (CR.CDFILIAL = P_CDFILIAL)
                           AND (CR.NRLANCTONF = P_NRLANCTONF)
                           AND (AJ.CDFILIAL = CR.CDFILIBOLREC)
                           AND (AJ.NRLANBOLREC = CR.NRLANBOLREC);
                        IF V_COUNT > 0 THEN
                           P_RETORNO := 4;
                        ELSE
                           P_RETORNO := 0;

                           IF (NVL(P_IDCANFIN, 'S') = 'S') THEN
                              /*APAGA AS CONTAS A RECEBER*/
                              FOR C_CNTRECEBER IN (SELECT CDFILIAL, NRLANCTONF, NRSEQVENREC, NRPARCELAREC,
                                                          CDFILIBOLREC, NRLANBOLREC, DTATUAVENREC, VRORIGREC,
                                                          CDEMPRESA, CDFIMOVFLREC, CDCLASSFINA, DTMOVFLUXO, IDMOVFLUXO
                                                     FROM CONTARECEBER
                                                    WHERE CDFILIAL = P_CDFILIAL
                                                      AND NRLANCTONF = P_NRLANCTONF) LOOP
                                  /*ATUALIZA ORCAMENTO FINANCEIRO FINANCEIRO*/
                                 ORCAMENTO_OK(C_CNTRECEBER.CDEMPRESA,
                                              C_CNTRECEBER.CDFIMOVFLREC,
                                              C_CNTRECEBER.CDCLASSFINA,
                                              C_CNTRECEBER.DTATUAVENREC,
                                              C_CNTRECEBER.VRORIGREC * -1,
                                              V_RESULTADO);
                                 /*APAGA O RATEIO DA CONTA A PAGAR*/
                                 DELETE FROM RATCTRECEBER
                                  WHERE CDFILIAL     = C_CNTRECEBER.CDFILIAL
                                    AND NRLANCTONF   = C_CNTRECEBER.NRLANCTONF
                                    AND NRSEQVENREC  = C_CNTRECEBER.NRSEQVENREC
                                    AND NRPARCELAREC = C_CNTRECEBER.NRPARCELAREC;
                                 /*APAGA OS AJUSTES DA CONTA A PAGAR*/
                                 FOR C_AJRECEBER IN (SELECT AJ.CDFILIAL, AJ.NRLANCTONF, AJ.NRSEQVENREC, AJ.NRPARCELAREC,
                                                          AJ.DTAJUSTEREC, AJ.CDAJUCTREC, T.IDAJUCTREC, AJ.CDEMMFAJCNTR,
                                                          AJ.CDFIMFAJCNTR, AJ.CDCLASSFINA, AJ.IDMOVFLUXO, AJ.VRAJUSTEREC,
                                                          AJ.DTMOVFLUXO
                                                     FROM AJCTRECEBER AJ, TIPOAJRECEBER T
                                                    WHERE AJ.CDFILIAL = C_CNTRECEBER.CDFILIAL
                                                      AND AJ.NRLANCTONF = C_CNTRECEBER.NRLANCTONF
                                                      AND AJ.NRSEQVENREC = C_CNTRECEBER.NRSEQVENREC
                                                      AND AJ.NRPARCELAREC = C_CNTRECEBER.NRPARCELAREC
                                                      AND AJ.CDAJUCTREC = T.CDAJUCTREC) LOOP
                                    IF C_AJRECEBER.IDAJUCTREC = 'D' THEN
                                       V_FATOR := 1;
                                    ELSE
                                       V_FATOR := -1;
                                    END IF;
                                    IF NOT (C_AJRECEBER.CDEMMFAJCNTR IS NULL)
                                       AND NOT (C_AJRECEBER.CDFIMFAJCNTR IS NULL)
                                       AND NOT (C_AJRECEBER.DTMOVFLUXO IS NULL)
                                       AND NOT (C_AJRECEBER.CDCLASSFINA IS NULL) THEN
                                       /*ATUALIZACAO DO FLUXO DE CAIXA DO AJUSTE*/
                                       ATUALIZA_FLUXO_DE_CAIXA(C_AJRECEBER.CDEMMFAJCNTR,
                                                               C_AJRECEBER.CDFIMFAJCNTR,
                                                               C_AJRECEBER.DTMOVFLUXO,
                                                               C_AJRECEBER.IDMOVFLUXO,
                                                               C_AJRECEBER.CDCLASSFINA,
                                                               C_AJRECEBER.VRAJUSTEREC * V_FATOR,
                                                               0);
                                    END IF;
                                    /*APAGA OS AJUSTES DA CONTA A RECEBER*/
                                    DELETE FROM AJCTRECEBER
                                     WHERE (CDFILIAL     = C_AJRECEBER.CDFILIAL)
                                       AND (NRLANCTONF   = C_AJRECEBER.NRLANCTONF)
                                       AND (NRSEQVENREC  = C_AJRECEBER.NRSEQVENREC)
                                       AND (NRPARCELAREC = C_AJRECEBER.NRPARCELAREC)
                                       AND (DTAJUSTEREC  = C_AJRECEBER.DTAJUSTEREC)
                                       AND (CDAJUCTREC   = C_AJRECEBER.CDAJUCTREC);
                                 END LOOP;
                                 
                                 IF NOT (C_CNTRECEBER.CDEMPRESA IS NULL)
                                    AND NOT (C_CNTRECEBER.CDFIMOVFLREC IS NULL)
                                    AND NOT (C_CNTRECEBER.DTMOVFLUXO IS NULL)
                                    AND NOT (C_CNTRECEBER.CDCLASSFINA IS NULL) THEN
                                    ATUALIZA_FLUXO_DE_CAIXA(C_CNTRECEBER.CDEMPRESA,
                                                            C_CNTRECEBER.CDFIMOVFLREC,
                                                            C_CNTRECEBER.DTMOVFLUXO,
                                                            C_CNTRECEBER.IDMOVFLUXO,
                                                            C_CNTRECEBER.CDCLASSFINA,
                                                            C_CNTRECEBER.VRORIGREC * -1,
                                                            0);
                                 END IF;
                                 
                                 UPDATE CONTARECEBER
                                    SET
                                       CDTPCTRECEB = NULL,
                                       CDFILIBOLREC = NULL,
                                       NRLANBOLREC = NULL,
                                       CDCLASSFINA = NULL,
                                       CDEMPRESA = NULL,
                                       CDFIMOVFLREC = NULL,
                                       DTMOVFLUXO = NULL,
                                       IDMOVFLUXO = NULL,
                                       DSCONTAREC = NULL,
                                       VRATUAREC =  VRORIGREC
                                    WHERE CDFILIAL = C_CNTRECEBER.CDFILIAL
                                      AND NRLANCTONF = C_CNTRECEBER.NRLANCTONF
                                      AND NRSEQVENREC = C_CNTRECEBER.NRSEQVENREC
                                      AND NRPARCELAREC = C_CNTRECEBER.NRPARCELAREC;
                                 
                                 DELETE FROM BOLETRECEBER
                                  WHERE CDFILIAL = C_CNTRECEBER.CDFILIBOLREC
                                    AND NRLANBOLREC = C_CNTRECEBER.NRLANBOLREC;
                              END LOOP;
                           END IF;
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END IF;
         END IF;
      END IF;
   END IF;
   CLOSE C_TIPOOPER;
END;
/