CREATE OR REPLACE PROCEDURE CONSISTE_FERI_FISCAL (P_CDFILIEMIRECE IN VARCHAR2,
                                P_DTVENCIMENTO IN DATE,
                                P_DTVENC OUT DATE,
                                P_IDCONSDIAUTIL IN VARCHAR2,
                                P_IDTPPERIODO IN VARCHAR2,
                                P_IDTPCALEND IN VARCHAR2,
                                P_IDVENCDIANAOUTIL IN VARCHAR2)
   AS
      V_BOFERI BOOLEAN;
      V_NRDIASFERI NUMBER;
      V_DTANTERIOR DATE;
   BEGIN
      IF (P_IDCONSDIAUTIL = 'S') AND (P_IDTPPERIODO <> 'S')  THEN
        V_BOFERI := TRUE;
      ELSE
        V_BOFERI := FALSE;
      END IF;
      P_DTVENC := P_DTVENCIMENTO;

      WHILE V_BOFERI LOOP  
         BEGIN
            IF P_IDTPCALEND = 'F' THEN
               SELECT COUNT(*)
                 INTO V_NRDIASFERI
                 FROM FERIFILI
                WHERE CDFILIAL = P_CDFILIEMIRECE
                  AND DTFERIFILI BETWEEN P_DTVENCIMENTO AND P_DTVENC;
            ELSIF P_IDTPCALEND = 'C' THEN
               SELECT COUNT(*)
                 INTO V_NRDIASFERI
                 FROM FERICOMP
                WHERE CDFILIAL = P_CDFILIEMIRECE
                  AND DTFERICOMP BETWEEN P_DTVENCIMENTO AND P_DTVENC;
            ELSE
               SELECT COUNT(*)
                 INTO V_NRDIASFERI
                 FROM FERIBANC
                WHERE CDFILIAL = P_CDFILIEMIRECE
                  AND DTFERIBANC BETWEEN P_DTVENCIMENTO AND P_DTVENC;
            END IF;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_NRDIASFERI := 0;
         END;

         IF NOT(V_NRDIASFERI = 0) THEN
            V_DTANTERIOR := P_DTVENC;
            /*VERIFICA SE PRORROGA OU ANTECIPA O VENCIMENTO CASO SEJA FERIADO*/
            IF P_IDVENCDIANAOUTIL = 'A' THEN
               P_DTVENC := P_DTVENC - V_NRDIASFERI;
            ELSE
               P_DTVENC := P_DTVENC + V_NRDIASFERI;
            END IF;

            WHILE V_BOFERI LOOP
               IF P_IDVENCDIANAOUTIL = 'A' THEN
                  V_DTANTERIOR := V_DTANTERIOR - 1;
               ELSE
                  V_DTANTERIOR := V_DTANTERIOR + 1;
               END IF;
               
               BEGIN
                  IF P_IDTPCALEND = 'F' THEN
                     SELECT COUNT(*)
                       INTO V_NRDIASFERI
                       FROM FERIFILI
                      WHERE CDFILIAL = P_CDFILIEMIRECE
                        AND DTFERIFILI BETWEEN V_DTANTERIOR AND P_DTVENC;
                  ELSIF P_IDTPCALEND = 'C' THEN
                     SELECT COUNT(*)
                       INTO V_NRDIASFERI
                       FROM FERICOMP
                      WHERE CDFILIAL = P_CDFILIEMIRECE
                        AND DTFERICOMP BETWEEN V_DTANTERIOR AND P_DTVENC;
                  ELSE
                     SELECT COUNT(*)
                       INTO V_NRDIASFERI
                       FROM FERIBANC
                      WHERE CDFILIAL = P_CDFILIEMIRECE
                        AND DTFERIBANC BETWEEN V_DTANTERIOR AND P_DTVENC;
                  END IF;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_NRDIASFERI := 0;
               END;
               IF V_NRDIASFERI > 0 THEN
                  V_DTANTERIOR := P_DTVENC;
                  IF P_IDVENCDIANAOUTIL = 'A' THEN
                     P_DTVENC := P_DTVENC - V_NRDIASFERI;
                  ELSE
                     P_DTVENC := P_DTVENC + V_NRDIASFERI;
                  END IF;
               ELSE
                  V_BOFERI := FALSE;
               END IF;
            END LOOP;
         ELSE
            V_BOFERI := FALSE;
         END IF;
      END LOOP;
      IF ((P_IDCONSDIAUTIL = 'S') AND (P_IDTPPERIODO = 'S')) THEN
         BEGIN
            IF P_IDTPCALEND = 'F' THEN
               SELECT COUNT(*)
                 INTO V_NRDIASFERI
                 FROM FERIFILI
                WHERE CDFILIAL   = P_CDFILIEMIRECE
                  AND DTFERIFILI = P_DTVENC;
            ELSIF P_IDTPCALEND = 'C' THEN
               SELECT COUNT(*)
                 INTO V_NRDIASFERI
                 FROM FERICOMP
                WHERE CDFILIAL   = P_CDFILIEMIRECE
                  AND DTFERICOMP = P_DTVENC;
            ELSE
               SELECT COUNT(*)
                 INTO V_NRDIASFERI
                 FROM FERIBANC
                WHERE CDFILIAL   = P_CDFILIEMIRECE
                  AND DTFERIBANC = P_DTVENC;
            END IF;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_NRDIASFERI := 0;
         END;
         P_DTVENC := P_DTVENC + NVL(V_NRDIASFERI,0);
      END IF; 
   END;  