CREATE OR REPLACE PROCEDURE GERA_CONTAPAGAR(
         P_CDFILIAL   IN FILIAL.CDFILIAL%TYPE,
         P_CDFORNECED IN FORNECEDOR.CDFORNECED%TYPE,
         P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE,
         P_IDTIPOLANC IN NOTAFISCAL.IDENTRSAIDOP%TYPE)
AS
   V_CDFORMPGTO    FORMPGTO.CDFORMPGTO%TYPE;
   V_TIPOFORMPGTO  FORNECEDOR.IDTPFORPGTFO%TYPE;
   V_PARCPGTOFO    FORNECEDOR.NRPARCPGTOFO%TYPE;
   V_VRNOTAFISC    NOTAFISCAL.VRNOTAFISC%TYPE;
   V_IDTIPODTPGTO  FORMPGTO.IDTIPODTPGTO%TYPE;

   V_VRIMPORETI    NUMBER;
   V_BRUTSEMVENC   NUMBER; 
   V_SEQUENCIAL    NUMBER;
   V_VRPARCVENC    NUMBER;
   V_VRPARCAUXI    NUMBER;
   V_NRPARCELAS    NUMBER;

   V_DTEMISSAO     NOTAFISCAL.DTEMISSAO%TYPE;
   V_DTENTRSAID    NOTAFISCAL.DTENTRSAID%TYPE;

   V_DATABASE      NOTAFISCAL.DTENTRSAID%TYPE;
   V_DTAUXILIAR    NOTAFISCAL.DTENTRSAID%TYPE;
   V_STAUXILIAR    VARCHAR2(10);

   V_NRDIAPGTOVAR  DIAPGTO.NRDIAPGTOVAR%TYPE;
   V_NRMESFORMPGT  DIAPGTO.NRMESFORMPGT%TYPE;
   V_NRNUMMESPGTO  DIAPGTO.NRNUMMESPGTO%TYPE;

   V_VERIFMES      NUMBER;
   V_INDIAS        NUMBER;
   V_NUMERODIAS    NUMBER DEFAULT 0;
   V_NUMERODIAAUX  NUMBER DEFAULT 0;
   V_STDIAS        VARCHAR2(02);

   V_IDTPPERIODO   FORMPGTO.IDTPPERIODO%TYPE;
   V_IDUTDIAPGTFO  FORNECEDOR.IDUTDIAPGTFO%TYPE;
   V_NRDIAPGFIXFO  FORNECEDOR.NRDIAPGFIXFO%TYPE;

   V_CDFILDEST     AUTOFORNE.CDFILDEST%TYPE;
   V_NRAUTOFORN    AUTOFORNE.NRAUTOFORN%TYPE;
   V_IDCMPVNDBONI  VARCHAR2(01);

   w_R             CHAR(1) := 'R';
   w_F             CHAR(1) := 'F';
BEGIN
   DELETE FROM CONTAPAGAR
   WHERE (CDFILIAL   = P_CDFILIAL)    AND
         (NRLANCTONF = P_NRLANCTONF);

   BEGIN
      SELECT VRNOTAFISC, TRUNC(NVL(DTEMISSAO, SYSDATE)), DTENTRSAID, IDCMPVNDBONI
      INTO V_VRNOTAFISC, V_DTEMISSAO, V_DTENTRSAID, V_IDCMPVNDBONI
      FROM NOTAFISCAL N, TIPOOPERAC T
      WHERE (N.CDFILIAL   = P_CDFILIAL)    AND
            (N.NRLANCTONF = P_NRLANCTONF)  AND
            (N.CDTIPOOPER = T.CDTIPOOPER)  AND
            (N.IDENTRSAIDOP = T.IDENTRSAIDOP);
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRNOTAFISC := 0;  V_DTEMISSAO := NULL;  V_DTENTRSAID := NULL; V_IDCMPVNDBONI := 'N';
   END;

   SELECT SUM(VRTOTIMPONF)
   INTO V_VRIMPORETI
   FROM IMPOSNFTOT INF, IMPOSTO I
   WHERE INF.CDFILIAL   = P_CDFILIAL   AND
         INF.NRLANCTONF = P_NRLANCTONF AND
         INF.CDIMPOSTO  = I.CDIMPOSTO  AND
         I.IDTRATIMPO   = w_R          AND
         I.IDDESCRFONTE = w_R;

   IF (V_VRIMPORETI IS NULL) THEN
      V_VRIMPORETI := 0;
   END IF;

   V_BRUTSEMVENC := 0;
   IF (V_IDCMPVNDBONI = 'S') THEN
      SELECT NVL(SUM(VRBRUTITEM), 0) AS VRBRUTITEM
        INTO V_BRUTSEMVENC
        FROM NOTAFISCAL N, TPOPERCFOP T, ITEMPRODUTO I
       WHERE T.IDENTRSAIDOP = N.IDENTRSAIDOP
         AND T.CDTIPOOPER   = N.CDTIPOOPER
         AND T.IDENTRSAIDOP = N.IDENTRSAIDOP
         AND T.IDCOMPOEVENC = 'N'
         AND T.CDFISCOPER   = I.CDFISOPEITEM
         AND N.CDFILIAL     = P_CDFILIAL
         AND N.NRLANCTONF   = P_NRLANCTONF
         AND N.CDFILIAL     = I.CDFILIAL
         AND N.NRLANCTONF   = I.NRLANCTONF;
   END IF;

   IF (V_BRUTSEMVENC IS NULL) THEN
      V_BRUTSEMVENC := 0;
   END IF;

   V_VRNOTAFISC := ROUND(V_VRNOTAFISC - V_VRIMPORETI - V_BRUTSEMVENC,2);


   IF P_IDTIPOLANC = w_F THEN
      BEGIN
      SELECT F.IDTPFORPGTFO,  F.NRPARCPGTOFO,
             FP.IDTIPODTPGTO, FP.IDTPPERIODO,
             F.IDUTDIAPGTFO,  F.NRDIAPGFIXFO,
             FP.CDFORMPGTO
      INTO V_TIPOFORMPGTO, V_PARCPGTOFO,
           V_IDTIPODTPGTO, V_IDTPPERIODO,
           V_IDUTDIAPGTFO, V_NRDIAPGFIXFO,
           V_CDFORMPGTO
      FROM FORNECEDOR F, FORMPGTO FP
      WHERE F.CDFORNECED = P_CDFORNECED      AND
            F.CDFORMPGTO = FP.CDFORMPGTO(+);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_TIPOFORMPGTO := NULL;  V_PARCPGTOFO   := NULL;  V_IDTIPODTPGTO := NULL;
            V_IDTPPERIODO  := NULL;  V_IDUTDIAPGTFO := NULL;  V_NRDIAPGFIXFO := NULL;
            V_CDFORMPGTO   := NULL;
      END;
   ELSE
      SELECT MAX(A.IDTPFORPGTAF),  MAX(A.NRPARCPGTOAF),
             MAX(FP.IDTIPODTPGTO), MAX(FP.IDTPPERIODO),
             MAX(A.IDUTDIAPGTAF),  MAX(A.NRDIAPGFIXAF),
             MAX(A.CDFILDEST),     MAX(A.NRAUTOFORN),
             MAX(FP.CDFORMPGTO)
      INTO V_TIPOFORMPGTO, V_PARCPGTOFO,
           V_IDTIPODTPGTO, V_IDTPPERIODO,
           V_IDUTDIAPGTFO, V_NRDIAPGFIXFO,
           V_CDFILDEST,    V_NRAUTOFORN,
           V_CDFORMPGTO
      FROM AUTOFORNE A, ITEMAUTF I, FORMPGTO FP
      WHERE I.CDFILLANC  = P_CDFILIAL   AND
            I.NRLANCTONF = P_NRLANCTONF AND
            I.CDFILDEST  = A.CDFILDEST  AND
            I.CDFILLANC  = A.CDFILLANC  AND
            I.NRAUTOFORN = A.NRAUTOFORN AND
            A.CDFORMPGTO = FP.CDFORMPGTO(+);
   END IF;

/* Rotina para calcular o numero de dias de prazo */

   IF NOT V_TIPOFORMPGTO IS NULL THEN
      IF V_TIPOFORMPGTO = 'V' THEN
         IF V_IDTIPODTPGTO = 'EMIN' THEN
            V_INDIAS   := 0;
            V_DATABASE := V_DTEMISSAO;
         ELSIF V_IDTIPODTPGTO = 'EMEX' THEN
            V_INDIAS   := 1;
            V_DATABASE := V_DTEMISSAO;
         ELSIF V_IDTIPODTPGTO = 'ENIN' THEN
            V_INDIAS   := 0;
            V_DATABASE := V_DTENTRSAID;
         ELSIF V_IDTIPODTPGTO = 'ENEX' THEN
            V_INDIAS   := 1;
            V_DATABASE := V_DTENTRSAID;
         ELSIF V_IDTIPODTPGTO = 'DSIN' THEN
            V_INDIAS := 0;
            V_DATABASE := V_DTENTRSAID;
         ELSIF V_IDTIPODTPGTO = 'DSEX' THEN
            V_INDIAS := 1;
            V_DATABASE := V_DTENTRSAID;
         END IF;

         V_DTAUXILIAR := V_DATABASE + V_INDIAS;

         IF V_IDTPPERIODO = 'M' THEN
            V_STAUXILIAR := TO_CHAR(V_DTAUXILIAR, 'DD');

            SELECT MAX(NRDIAPGTOVAR), MAX(NRMESFORMPGT), NVL(MAX(NRNUMMESPGTO), 1)
            INTO V_NRDIAPGTOVAR, V_NRMESFORMPGT, V_NRNUMMESPGTO
            FROM DIAPGTO
            WHERE CDFORMPGTO   =  V_CDFORMPGTO AND
                  NRDIAINIPGTO <= V_STAUXILIAR AND
                  NRDIAFINPGTO >= V_STAUXILIAR;

            IF V_NRMESFORMPGT = 0 THEN
            	V_VERIFMES := 0;
            ELSE
            	V_VERIFMES := V_NRNUMMESPGTO;
            END IF;

            IF NOT V_NRDIAPGTOVAR IS NULL THEN
               VERIFICA_DATA(V_NRDIAPGTOVAR, V_DTAUXILIAR, V_VERIFMES, V_STDIAS);

               V_STAUXILIAR := TO_CHAR(V_DTAUXILIAR, 'DD/MM/YYYY');
               V_STAUXILIAR := V_STDIAS || SUBSTR(V_STAUXILIAR, 3, 8);

               V_DTAUXILIAR := TO_DATE(V_STAUXILIAR, 'DD/MM/YYYY');

               IF V_NRMESFORMPGT <> 0 THEN
                  V_DTAUXILIAR := ADD_MONTHS(V_DTAUXILIAR, V_NRNUMMESPGTO);
               END IF;
            END IF;
         ELSE
            V_STDIAS := LPAD(TO_CHAR(V_DTAUXILIAR, 'D'),2,'0');
            V_INDIAS := TO_NUMBER(V_STDIAS);

            SELECT MAX(NRDIAPGTOVAR), MAX(NRMESFORMPGT)
            INTO V_NRDIAPGTOVAR, V_NRMESFORMPGT
            FROM DIAPGTO
            WHERE CDFORMPGTO   =  V_CDFORMPGTO AND
                  NRDIAINIPGTO <= V_STAUXILIAR AND
                  NRDIAFINPGTO >= V_STAUXILIAR;

            IF V_NRDIAPGTOVAR IS NOT NULL THEN
               IF V_NRMESFORMPGT = 0 THEN
                  IF V_INDIAS = TO_NUMBER(V_NRDIAPGTOVAR) THEN
                     V_DTAUXILIAR := V_DTAUXILIAR + 7;
                  ELSE
                     WHILE TO_NUMBER(TO_CHAR(V_DTAUXILIAR, 'D')) <> TO_NUMBER(V_NRDIAPGTOVAR) LOOP
                        V_DTAUXILIAR := V_DTAUXILIAR + 1;
                     END LOOP;
                  END IF;
               ELSE
                  IF V_INDIAS = TO_NUMBER(V_NRDIAPGTOVAR) THEN
                     V_DTAUXILIAR := V_DTAUXILIAR + 7;
                  ELSE
                     IF V_INDIAS < TO_NUMBER(V_NRDIAPGTOVAR) THEN
                        V_DTAUXILIAR := V_DTAUXILIAR + 7;
                     END IF;

                     WHILE TO_NUMBER(TO_CHAR(V_DTAUXILIAR, 'D')) <> TO_NUMBER(V_NRDIAPGTOVAR) LOOP
                        V_DTAUXILIAR := V_DTAUXILIAR + 1;
                     END LOOP;
                  END IF;
               END IF;
            END IF;
         END IF;
         V_DTAUXILIAR := V_DTAUXILIAR - 1;
      ELSE
         V_DATABASE   := V_DTEMISSAO;
         V_DTAUXILIAR := V_DATABASE;

         IF (V_IDUTDIAPGTFO = 'I') AND
            (V_NRDIAPGFIXFO >= TO_NUMBER(TO_CHAR(V_DTAUXILIAR, 'DD'))) THEN

            VERIFICA_DATA(LPAD(TO_CHAR(V_NRDIAPGFIXFO),2,'0'), V_DTAUXILIAR, 0, V_STDIAS);

            V_STAUXILIAR := V_STDIAS || TO_CHAR(V_DTAUXILIAR, '/MM/YYYY');
            V_DTAUXILIAR := TO_DATE(V_STAUXILIAR, 'DD/MM/YYYY');
         ELSE
            V_DTAUXILIAR := ADD_MONTHS(V_DTAUXILIAR, 1);

            VERIFICA_DATA(LPAD(TO_CHAR(V_NRDIAPGFIXFO),2,'0'), V_DTAUXILIAR, 0, V_STDIAS);

            V_STAUXILIAR := V_STDIAS || TO_CHAR(V_DTAUXILIAR, '/MM/YYYY');
            V_DTAUXILIAR := TO_DATE(V_STAUXILIAR, 'DD/MM/YYYY');
         END IF;
      END IF;

      V_NUMERODIAS := V_DTAUXILIAR - V_DATABASE;
      IF V_NUMERODIAS < 0 THEN
         V_NUMERODIAS := 0;
      END IF;
   END IF;

/* Inclusao na tabela de contas a receber de acordo com
   a forma de pagamento do fornecedor
*/

   IF (V_TIPOFORMPGTO IS NULL) THEN  /*Sem forma de pagamento definida*/   
      INSERT INTO CONTAPAGAR (CDFILIAL, NRLANCTONF, CDMOTIPROR,
                              CDMOTIPARC, CDTPCTPAGAR, NRSEQVENPAG,
                              NRPARCELAPAG, QTDIASVENPAG, DTORIGVENPAG,
                              VRORIGPAG, DTATUAVENPAG, VRATUAPAG,
                              IDSTATPAG, DSCONTAPAG, CDFILIBPAG, NRLANBOLPAG)
      VALUES (P_CDFILIAL, P_NRLANCTONF, NULL,
              NULL, NULL, '01',
              '00', 0, V_DTENTRSAID,
              V_VRNOTAFISC, V_DTENTRSAID, V_VRNOTAFISC,
              'A', NULL, NULL, NULL);
   ELSIF (V_TIPOFORMPGTO = w_F) THEN /*Forma de pagamento fixa*/
      IF (V_PARCPGTOFO = 1) THEN
      
         INSERT INTO CONTAPAGAR (CDFILIAL, NRLANCTONF, CDMOTIPROR,
                                 CDMOTIPARC, CDTPCTPAGAR, NRSEQVENPAG,
                                 NRPARCELAPAG, QTDIASVENPAG, DTORIGVENPAG,
                                 VRORIGPAG, DTATUAVENPAG, VRATUAPAG,
                                 IDSTATPAG, DSCONTAPAG, CDFILIBPAG, NRLANBOLPAG)
         VALUES (P_CDFILIAL, P_NRLANCTONF, NULL,
                 NULL, NULL, '01',
                 '00', V_NUMERODIAS, V_DTAUXILIAR,
                 V_VRNOTAFISC, V_DTENTRSAID, V_VRNOTAFISC,
                 'A', NULL, NULL, NULL);
      ELSE
         V_SEQUENCIAL := 1;
         V_VRPARCVENC := ROUND(V_VRNOTAFISC/V_PARCPGTOFO,2);

         V_DTAUXILIAR := V_DATABASE + V_NUMERODIAS;

         WHILE V_SEQUENCIAL <= V_PARCPGTOFO LOOP

            IF V_SEQUENCIAL = V_PARCPGTOFO THEN
               V_VRPARCVENC := V_VRPARCVENC + ROUND(V_VRNOTAFISC - (V_VRPARCVENC * V_PARCPGTOFO),2);
            END IF;

            INSERT INTO CONTAPAGAR (CDFILIAL, NRLANCTONF, CDMOTIPROR,
                                    CDMOTIPARC, CDTPCTPAGAR, NRSEQVENPAG,
                                    NRPARCELAPAG, QTDIASVENPAG, DTORIGVENPAG,
                                    VRORIGPAG, DTATUAVENPAG, VRATUAPAG,
                                    IDSTATPAG, DSCONTAPAG, CDFILIBPAG, NRLANBOLPAG)
            VALUES (P_CDFILIAL, P_NRLANCTONF, NULL,
                    NULL, NULL, LPAD(TO_CHAR(V_SEQUENCIAL),2,'0'),
                    '00', V_NUMERODIAS, V_DTAUXILIAR,
                    V_VRPARCVENC, V_DTAUXILIAR, V_VRPARCVENC,
                    'A', NULL, NULL, NULL);

            V_SEQUENCIAL := V_SEQUENCIAL + 1;
            V_DTAUXILIAR := ADD_MONTHS(V_DTAUXILIAR, 1);
            V_NUMERODIAS := V_DTAUXILIAR - V_DATABASE;
            IF V_NUMERODIAS < 0 THEN
               V_NUMERODIAS := 0;
            END IF;
         END LOOP;
      END IF;
   ELSE                            /*Forma de pagamento variavel*/
      IF P_IDTIPOLANC = w_F THEN
         SELECT COUNT(*)
         INTO V_NRPARCELAS
         FROM PRAZOPGTFORN
         WHERE CDFORNECED = P_CDFORNECED;
      ELSE
         SELECT COUNT(*)
         INTO V_NRPARCELAS
         FROM PRAZOPGTAF
         WHERE CDFILLANC  = P_CDFILIAL  AND
               CDFILDEST  = V_CDFILDEST AND
               NRAUTOFORN = V_NRAUTOFORN;
      END IF;

      V_DTAUXILIAR := V_DATABASE + V_NUMERODIAS;

      IF (V_NRPARCELAS = 1) THEN
         IF P_IDTIPOLANC = w_F THEN
            SELECT NVL(NRDIASPGTOFO, 0)
            INTO V_INDIAS
            FROM PRAZOPGTFORN
            WHERE CDFORNECED = P_CDFORNECED;

            V_NUMERODIAS := V_NUMERODIAS + V_INDIAS;
            V_DTAUXILIAR := V_DTAUXILIAR + V_INDIAS;
         ELSE
            SELECT NVL(NRDIASPGTOAF, 0)
            INTO V_INDIAS
            FROM PRAZOPGTAF
            WHERE CDFILLANC  = P_CDFILIAL  AND
                  CDFILDEST  = V_CDFILDEST AND
                  NRAUTOFORN = V_NRAUTOFORN;

            V_NUMERODIAS := V_NUMERODIAS + V_INDIAS;
            V_DTAUXILIAR := V_DTAUXILIAR + V_INDIAS;
         END IF;

         INSERT INTO CONTAPAGAR (CDFILIAL, NRLANCTONF, CDMOTIPROR,
                                 CDMOTIPARC, CDTPCTPAGAR, NRSEQVENPAG,
                                 NRPARCELAPAG, QTDIASVENPAG, DTORIGVENPAG,
                                 VRORIGPAG, DTATUAVENPAG, VRATUAPAG,
                                 IDSTATPAG, DSCONTAPAG, CDFILIBPAG, NRLANBOLPAG)
         VALUES (P_CDFILIAL, P_NRLANCTONF, NULL,
                 NULL, NULL, '01',
                 '00', V_NUMERODIAS, V_DTAUXILIAR,
                 V_VRNOTAFISC, V_DTAUXILIAR, V_VRNOTAFISC,
                 'A', NULL, NULL, NULL);
      ELSE
         V_SEQUENCIAL := 1;
         V_VRPARCAUXI := 0;

         IF P_IDTIPOLANC = w_F THEN
            FOR C_PARCELAS IN (SELECT VRPERCPGTOFO, NRDIASPGTOFO
                               FROM PRAZOPGTFORN
                               WHERE CDFORNECED = P_CDFORNECED) LOOP

               V_VRPARCVENC := ROUND((V_VRNOTAFISC * (C_PARCELAS.VRPERCPGTOFO/100)),2);
               V_VRPARCAUXI := V_VRPARCAUXI + V_VRPARCVENC;

               IF V_SEQUENCIAL = V_NRPARCELAS THEN
                  V_VRPARCVENC := V_VRPARCVENC + ROUND((V_VRNOTAFISC - V_VRPARCAUXI),2);
               END IF;

               IF C_PARCELAS.NRDIASPGTOFO = 0 THEN
                  V_NUMERODIAS := V_NUMERODIAS + 1;
               ELSE               
                  V_NUMERODIAS := V_NUMERODIAS + C_PARCELAS.NRDIASPGTOFO - V_NUMERODIAAUX;                  
               END IF;

               V_DTAUXILIAR := V_DATABASE + V_NUMERODIAS;
               INSERT INTO CONTAPAGAR (CDFILIAL, NRLANCTONF, CDMOTIPROR,
                                       CDMOTIPARC, CDTPCTPAGAR, NRSEQVENPAG,
                                       NRPARCELAPAG, QTDIASVENPAG, DTORIGVENPAG,
                                       VRORIGPAG, DTATUAVENPAG, VRATUAPAG,
                                       IDSTATPAG, DSCONTAPAG, CDFILIBPAG, NRLANBOLPAG)
               VALUES (P_CDFILIAL, P_NRLANCTONF, NULL,
                       NULL, NULL, LPAD(TO_CHAR(V_SEQUENCIAL),2,'0'),
                       '00', V_NUMERODIAS, V_DTAUXILIAR,
                       V_VRPARCVENC, V_DTAUXILIAR, V_VRPARCVENC,
                       'A', NULL, NULL, NULL);
                       
               V_SEQUENCIAL := V_SEQUENCIAL + 1;               
               V_NUMERODIAAUX := C_PARCELAS.NRDIASPGTOFO;
            END LOOP;
         ELSE
            V_NUMERODIAAUX := V_NUMERODIAS;
            FOR C_PARCELAS IN (SELECT NRDIASPGTOAF, VRPERCPGTOAF
                               FROM PRAZOPGTAF
                               WHERE CDFILLANC  = P_CDFILIAL  AND
                                     CDFILDEST  = V_CDFILDEST AND
                                     NRAUTOFORN = V_NRAUTOFORN) LOOP

               V_VRPARCVENC := ROUND((V_VRNOTAFISC * (C_PARCELAS.VRPERCPGTOAF/100)),2);
               V_VRPARCAUXI := V_VRPARCAUXI + V_VRPARCVENC;

               IF V_SEQUENCIAL = V_NRPARCELAS THEN
                  V_VRPARCVENC := V_VRPARCVENC + ROUND((V_VRNOTAFISC - V_VRPARCAUXI),2);
               END IF;

               IF C_PARCELAS.NRDIASPGTOAF = 0 THEN
                  V_NUMERODIAAUX := V_NUMERODIAS + 1;
               ELSE
                  V_NUMERODIAAUX := V_NUMERODIAS + C_PARCELAS.NRDIASPGTOAF;
               END IF;

               V_DTAUXILIAR := V_DATABASE + V_NUMERODIAAUX;

               INSERT INTO CONTAPAGAR (CDFILIAL, NRLANCTONF, CDMOTIPROR,
                                       CDMOTIPARC, CDTPCTPAGAR, NRSEQVENPAG,
                                       NRPARCELAPAG, QTDIASVENPAG, DTORIGVENPAG,
                                       VRORIGPAG, DTATUAVENPAG, VRATUAPAG,
                                       IDSTATPAG, DSCONTAPAG, CDFILIBPAG, NRLANBOLPAG)
               VALUES (P_CDFILIAL, P_NRLANCTONF, NULL,
                       NULL, NULL, LPAD(TO_CHAR(V_SEQUENCIAL),2,'0'),
                       '00', V_NUMERODIAS, V_DTAUXILIAR,
                       V_VRPARCVENC, V_DTAUXILIAR, V_VRPARCVENC,
                       'A', NULL, NULL, NULL);

               V_SEQUENCIAL := V_SEQUENCIAL + 1;
            END LOOP;
         END IF;
      END IF;
   END IF;
END;
/