CREATE OR REPLACE FUNCTION CONSISTE_FERI_BANC(P_CDFILIEMIRECE IN VARCHAR2,
                                P_DTVENCIMENTO IN DATE,
                                P_IDCONSCALVENNF IN VARCHAR2) RETURN DATE
   IS
      V_BOFERI BOOLEAN;
      V_NRDIASFERI NUMBER;
      V_DTANTERIOR DATE;
      V_DTVENC  DATE;
      /*Identificador de feriado bancário  */
      V_IDCOFERIBANC PARAMGERAL.IDCOFERIBANC%TYPE;
  
   BEGIN
   
            /*Considera Feriado Bancario*/
      SELECT MAX(IDCOFERIBANC)
        INTO V_IDCOFERIBANC
        FROM PARAMGERAL;

      V_BOFERI := TRUE;
      V_DTVENC := P_DTVENCIMENTO;
   IF NVL(V_IDCOFERIBANC,'N') = 'S' THEN
      WHILE V_BOFERI LOOP
         
            BEGIN
               IF P_IDCONSCALVENNF = 'F' THEN
                  SELECT COUNT(*)
                    INTO V_NRDIASFERI
                    FROM FERIFILI
                   WHERE CDFILIAL = P_CDFILIEMIRECE
                     AND DTFERIFILI BETWEEN P_DTVENCIMENTO AND V_DTVENC;
               ELSIF P_IDCONSCALVENNF = 'C' THEN
                  SELECT COUNT(*)
                    INTO V_NRDIASFERI
                    FROM FERICOMP
                   WHERE CDFILIAL = P_CDFILIEMIRECE
                     AND DTFERICOMP BETWEEN P_DTVENCIMENTO AND V_DTVENC;
               ELSE
                  SELECT COUNT(*)
                    INTO V_NRDIASFERI
                    FROM FERIBANC
                   WHERE CDFILIAL = P_CDFILIEMIRECE
                     AND DTFERIBANC BETWEEN P_DTVENCIMENTO AND V_DTVENC;
               END IF;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_NRDIASFERI := 0;
            END;
         IF NOT(V_NRDIASFERI = 0) THEN
            V_DTANTERIOR := V_DTVENC;
            V_DTVENC := V_DTVENC + V_NRDIASFERI;
            WHILE V_BOFERI LOOP
               V_DTANTERIOR := V_DTANTERIOR+1;
               BEGIN
                  IF P_IDCONSCALVENNF = 'F' THEN
                     SELECT COUNT(*)
                       INTO V_NRDIASFERI
                       FROM FERIFILI
                      WHERE CDFILIAL = P_CDFILIEMIRECE
                        AND DTFERIFILI BETWEEN V_DTANTERIOR AND V_DTVENC;
                  ELSIF P_IDCONSCALVENNF = 'C' THEN
                     SELECT COUNT(*)
                       INTO V_NRDIASFERI
                       FROM FERICOMP
                      WHERE CDFILIAL = P_CDFILIEMIRECE
                        AND DTFERICOMP BETWEEN V_DTANTERIOR AND V_DTVENC;
                  ELSE
                     SELECT COUNT(*)
                       INTO V_NRDIASFERI
                       FROM FERIBANC
                      WHERE CDFILIAL = P_CDFILIEMIRECE
                        AND DTFERIBANC BETWEEN V_DTANTERIOR AND V_DTVENC;
                  END IF;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_NRDIASFERI := 0;
               END;
               IF V_NRDIASFERI > 0 THEN
                  V_DTANTERIOR := V_DTVENC;
                  V_DTVENC := V_DTVENC + V_NRDIASFERI;
               ELSE
                  V_BOFERI := FALSE;
               END IF;
            END LOOP;
         ELSE
            V_BOFERI := FALSE;
         END IF;
      END LOOP;
   END IF;
      RETURN V_DTVENC;
   END;
 