CREATE OR REPLACE PROCEDURE CONFERE_ERROS_NF
   (P_CDFILIAL     IN NOTAFISCAL.CDFILIAL%TYPE,
    P_NRLANCTONF   IN NOTAFISCAL.NRLANCTONF%TYPE,
    P_VRLANCAMENTO IN NOTAFISCAL.VRNOTAFISC%TYPE)
AS BEGIN
DECLARE
   V_CDFILIORIG   VARCHAR2(04);
   V_CDEMPRESA    VARCHAR2(02);
   V_CDTIPOOPER   VARCHAR2(04);
   V_IDENTRSAID   VARCHAR2(01);
   V_IDORIGDEST   VARCHAR2(01);
   V_CDFORNECED   VARCHAR2(21);
   V_IDCONSPARFIS VARCHAR2(01);
   V_CDFILDEST    VARCHAR2(04);
   V_CDFILLANC    VARCHAR2(04);
   V_NRAUTOFORN   VARCHAR2(10);
   V_DTPROGENTAF  DATE;

   V_VRIMPOSTOT   NUMBER;
   V_FAIXATOLER   NUMBER;
   V_IDCONVRUNIAF VARCHAR2(01);
   V_IDCONQTDEAF  VARCHAR2(01);
   V_IDCONQTDEREC VARCHAR2(01);
   V_IDERROAVISO  VARCHAR2(01);
   V_IDERROAVISOFIL  VARCHAR2(01);
   V_IDPRODNFAF     VARCHAR2(01);

   V_NRDECTOTANF  NUMBER;
   V_VRPETOLEVRAF NUMBER;
   V_VRPETOLEQTAF NUMBER;
   V_VRNFRELACAF  NUMBER;
   V_QTNFRELACAF  NUMBER;
   V_QTITEMPROD   NUMBER;
   V_QTENTRMERC   NUMBER;
   V_VRPTOLQTITAF NUMBER;
   V_VRPTOLVRITAF NUMBER;
   V_PERCDIF      NUMBER;
   V_VALRMIN      NUMBER;
   V_VALRMAX      NUMBER;
   V_VRNOTAFISC   NUMBER;
   V_VRFATOCONVAF NUMBER;
   V_NRLANCTONF      VARCHAR2(10);
   V_DIFPROD         VARCHAR2(1);
   V_NRORIGVENPAG    NUMBER;
   V_DEVOLORIG       VARCHAR2(10);
   V_IDOPERDEVONF    VARCHAR2(01);
   V_IDCONSVLRMAF    VARCHAR2(01);
   V_IDCONSVLRMENAF  VARCHAR2(01);
   V_IDUTRELACFORNAF VARCHAR2(01);
   V_IDTIPODTPGTO    VARCHAR2(4);
   V_IDNFGERADAVIAXML VARCHAR2(1);
   V_IDGERNFIGUALXML VARCHAR2(1);

   V_IDUTILDTPROC    VARCHAR2(01);

   w_F            CHAR(1) := 'F';
   w_N            CHAR(1) := 'N';
   w_S            CHAR(1) := 'S';
   w_C            CHAR(1) := 'C';
   w_P            CHAR(1) := 'P';
   w_R            CHAR(1) := 'R';
   w_1            CHAR(1) := '1';
   w_01           CHAR(2) := '01';
   w_03           CHAR(2) := '03';
   w_AV01         VARCHAR2(4) := 'AV01';
   w_AV02         VARCHAR2(4) := 'AV02';
   w_AV03         VARCHAR2(4) := 'AV03';
   w_AV04         VARCHAR2(4) := 'AV04';
   w_AV05         VARCHAR2(4) := 'AV05';
   w_AV06         VARCHAR2(4) := 'AV06';
   w_AV07         VARCHAR2(4) := 'AV07';
   w_AV08         VARCHAR2(4) := 'AV08';
   w_AV09         VARCHAR2(4) := 'AV09';
   w_AV10         VARCHAR2(4) := 'AV10';
   w_AV11         VARCHAR2(4) := 'AV11';
   w_AV12         VARCHAR2(4) := 'AV12';
   w_AV13         VARCHAR2(4) := 'AV13';
   w_AV14         VARCHAR2(4) := 'AV14';
   w_AV15         VARCHAR2(4) := 'AV15';
   w_AV16         VARCHAR2(4) := 'AV16';
   w_AV17         VARCHAR2(4) := 'AV17';
   w_ER01         VARCHAR2(4) := 'ER01';
   w_ER02         VARCHAR2(4) := 'ER02';
   w_ER03         VARCHAR2(4) := 'ER03';
   w_ER05         VARCHAR2(4) := 'ER05';

   V_VRSOMAENCA     NUMBER;
   V_VRMENOENCA     NUMBER;     

   V_VRSOMAIMPO     NUMBER;
   V_VRMENOIMPO     NUMBER;
   V_SOMAVRADICFECP NUMBER;
   V_VRSUBTRAIVENC  NUMBER;
   V_COUNT          NUMBER;
   V_IGUAL          VARCHAR2(01);
   V_IDREPLICADA   VARCHAR2(20);

   CURSOR C_NOTAFISCAL IS
      SELECT DECODE(IDENTRSAIDOP, 'E', CDFILIRECE, CDFILIEMIT), CDEMPRESA, CDTIPOOPER, IDENTRSAIDOP,
             CDFORNECED, NVL(IDORIGDESTNF,w_F), IDNFGERADAVIAXML, NVL(IDDOCREPLIC, DSNOTAFISORIGOL) AS IDREPLICADA
      FROM NOTAFISCAL
      WHERE CDFILIAL   = P_CDFILIAL AND
            NRLANCTONF = P_NRLANCTONF;

   CURSOR C_ITEMAUTF IS
      SELECT DISTINCT CDFILDEST, CDFILLANC, NRAUTOFORN,
                      DTPROGENTAF, NRLANCTONF
      FROM NFRELACAF
      WHERE CDFILIAL   = P_CDFILIAL AND
            NRLANCTONF = P_NRLANCTONF;
   
   CURSOR C_PRODAUTF IS
      SELECT DISTINCT NF.NRAUTOFORN, NF.NRLANCTONF, NF.CDPRODUTO, P.CDPRODESTO
        FROM NFRELACAF NF, PRODUTO P
       WHERE NF.CDFILIAL   = P_CDFILIAL
         AND NF.NRLANCTONF = P_NRLANCTONF
         AND NF.CDPRODUTO  = P.CDPRODUTO;

BEGIN

/* CONFERENCIA DE ERROS E AVISOS DA NOTA FISCAL */

   V_VRNFRELACAF := 0.00;
   V_QTNFRELACAF := 0.000;

   V_IDUTILDTPROC := 'N';

   /* PREENCHE AS VARIAVEIS P/ CONTROLE DE CONSISTENCIA DE AF X NF */
   SELECT NVL(VRPETOLEVRAF, 0.00),
          NVL(VRPETOLEQTAF, 0.00),
          NVL(NRDECITOTANF, 2),
          DECODE(VRFAIXTOLENF,0,0.009,NVL(VRFAIXTOLENF, 0.02)),
          IDCONSVLRMAF, IDCONSVLRMENAF, IDUTRELACFORNAF,
          NVL(IDUTILDTPROC,'N'), IDGERNFIGUALXML
   INTO V_VRPETOLEVRAF, V_VRPETOLEQTAF, V_NRDECTOTANF, V_FAIXATOLER,
        V_IDCONSVLRMAF, V_IDCONSVLRMENAF, V_IDUTRELACFORNAF, V_IDUTILDTPROC, V_IDGERNFIGUALXML
   FROM PARAMGERAL;

   OPEN C_NOTAFISCAL;
   LOOP
      FETCH C_NOTAFISCAL INTO
         V_CDFILIORIG, V_CDEMPRESA, V_CDTIPOOPER, V_IDENTRSAID, V_CDFORNECED, V_IDORIGDEST, V_IDNFGERADAVIAXML, V_IDREPLICADA;
      EXIT WHEN C_NOTAFISCAL%NOTFOUND;
   END LOOP;
   CLOSE C_NOTAFISCAL;

   OPEN C_ITEMAUTF;
   LOOP
      FETCH C_ITEMAUTF INTO
         V_CDFILDEST, V_CDFILLANC, V_NRAUTOFORN, V_DTPROGENTAF, V_NRLANCTONF;
      EXIT WHEN C_ITEMAUTF%NOTFOUND;
   END LOOP;
   CLOSE C_ITEMAUTF;

   BEGIN
      SELECT NVL(IDORIGDESTOP,w_F), NVL(IDOPERDEVONF, w_N), NVL(IDCONSPARFIS, w_S)
      INTO V_IDORIGDEST, V_IDOPERDEVONF, V_IDCONSPARFIS
      FROM TIPOOPERAC
      WHERE IDENTRSAIDOP = V_IDENTRSAID AND
            CDTIPOOPER   = V_CDTIPOOPER;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDORIGDEST := NULL;  V_IDOPERDEVONF := w_N; V_IDCONSPARFIS := w_S;
   END;

   UPDATE ITEMPRODUTO
   SET IDAVISOSITEM = '000000'
   WHERE (CDFILIAL = P_CDFILIAL)   AND
         (NRLANCTONF = P_NRLANCTONF);

   UPDATE TRABITPROIMPOS
   SET IDAVISIMPOIT = '00000'
   WHERE (CDFILIMPOIT  = P_CDFILIAL)   AND
         (NRLANCIMPOIT = P_NRLANCTONF);

   IF (NOT(V_CDTIPOOPER  IN ('09','10'))) AND (NOT ((V_IDNFGERADAVIAXML = w_S) AND (V_IDGERNFIGUALXML = w_S))) THEN /*VALE DE TRASFERENCIA E ENTRADA SEM NOTA*/

      /* (AVISO) ITEM COM CODIGO DA SITUACAO TRIBUTARIA INVALIDO */
      BEGIN
         SELECT IDERROAVISOFIL
         INTO V_IDERROAVISO
         FROM ERROAVISONFFIL
         WHERE CDERROAVISOFIL = w_AV09
           AND CDFILIAL = V_CDFILIORIG;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDERROAVISO := NULL;
      END;

      IF V_IDERROAVISO IS NULL THEN
         BEGIN
            SELECT IDERROAVISO
            INTO V_IDERROAVISO
            FROM ERROAVISONF
            WHERE CDERROAVISO = w_AV09;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;
      END IF;

      UPDATE ITEMPRODUTO
      SET IDAVISOSITEM = w_1 || SUBSTR(IDAVISOSITEM,2,5)
      WHERE (CDFILIAL   = P_CDFILIAL)   AND
            (NRLANCTONF = P_NRLANCTONF) AND
            ((CDSITUTRIBPR IS NULL) OR (LENGTH(TRIM(CDSITUTRIBPR)) < 3)) AND
            (V_IDERROAVISO <> w_N) AND
            (V_IDCONSPARFIS <> w_N);
-------------------------------------------------------------------------------------------------------
      /* (AVISO) ITEM COM CODIGO FISCAL DE OPERACAO INVALIDO */
      BEGIN
         SELECT IDERROAVISOFIL
         INTO V_IDERROAVISO
         FROM ERROAVISONFFIL
         WHERE CDERROAVISOFIL = w_AV10
           AND CDFILIAL = V_CDFILIORIG;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDERROAVISO := NULL;
      END;

      IF V_IDERROAVISO IS NULL THEN
         BEGIN
            SELECT IDERROAVISO
            INTO V_IDERROAVISO
            FROM ERROAVISONF
            WHERE CDERROAVISO = w_AV10;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;
      END IF;

      UPDATE ITEMPRODUTO
      SET IDAVISOSITEM = SUBSTR(IDAVISOSITEM,1,1) || w_1 || SUBSTR(IDAVISOSITEM,3,4)
      WHERE (CDFILIAL   = P_CDFILIAL)   AND
            (NRLANCTONF = P_NRLANCTONF) AND
            ((CDFISOPEITEM IS NULL) OR (LENGTH(CDFISOPEITEM) < 3)) AND
            (V_IDERROAVISO <> w_N) AND
            (V_IDCONSPARFIS <> w_N);

      IF NOT(V_CDTIPOOPER = '07') THEN /*NF DE MOVIMENTACAO*/
         /* (AVISO) IMPOSTO DO ITEM DA ENTRADA COM CODIGO FISCAL INVALIDO */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV04
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV04;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         UPDATE TRABITPROIMPOS
         SET IDAVISIMPOIT = SUBSTR(IDAVISIMPOIT,1,1) || w_1 || SUBSTR(IDAVISIMPOIT,3,3)
         WHERE (CDFILIMPOIT  = P_CDFILIAL)   AND
               (NRLANCIMPOIT = P_NRLANCTONF) AND
               ((CDFISCIMPOIT IS NULL) OR LENGTH(CDFISCIMPOIT) <> 1) AND
               (V_IDERROAVISO <> w_N);
-------------------------------------------------------------------------------------------------------
         /* (AVISO) IMPOSTO DO ITEM COM SITUACAO TRIBUTARIA(CST) INVALIDA */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV05
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV05;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         UPDATE TRABITPROIMPOS
         SET IDAVISIMPOIT = SUBSTR(IDAVISIMPOIT,1,2) || w_1 || SUBSTR(IDAVISIMPOIT,4,2)
         WHERE (CDFILIMPOIT  = P_CDFILIAL)   AND
               (NRLANCIMPOIT = P_NRLANCTONF) AND
               ((CDSTRIBIMPOIT IS NULL) OR (LENGTH(CDSTRIBIMPOIT) < 2)) AND
               (V_IDERROAVISO <> w_N) AND
               (V_IDCONSPARFIS <> w_N) AND
               (CDIMPOSTOIT IN(SELECT CDIMPOSTO
                                FROM IMPOSTO
                               WHERE IDCONSPARFISIP = 'S'));
-------------------------------------------------------------------------------------------------------
         /* (AVISO) IMPOSTO DO ITEM COM COD. FISCAL OPERACAO - CFOP INVALIDO */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV06
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV06;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         UPDATE TRABITPROIMPOS
         SET IDAVISIMPOIT = SUBSTR(IDAVISIMPOIT,1,3) || w_1 || SUBSTR(IDAVISIMPOIT,5,1)
         WHERE (CDFILIMPOIT  = P_CDFILIAL)   AND
               (NRLANCIMPOIT = P_NRLANCTONF) AND
               ((CDFOPERIMPOIT IS NULL) OR (LENGTH(CDFOPERIMPOIT) < 3)) AND
               (V_IDERROAVISO <> w_N) AND
               (V_IDCONSPARFIS <> w_N) AND
               (CDIMPOSTOIT IN(SELECT CDIMPOSTO
                                FROM IMPOSTO
                               WHERE IDCONSPARFISIP = 'S'));
-------------------------------------------------------------------------------------------------------
      END IF;
   END IF;

   IF V_IDENTRSAID = 'E' THEN
        IF (V_CDTIPOOPER NOT IN ('05', '07', '09', '10')) AND (NOT ((V_IDNFGERADAVIAXML = w_S) AND (V_IDGERNFIGUALXML = w_S))) THEN /*NF COBRANCA, NF MOVIMENTACAO, VALE TRANSFERENCIA, ENTRADA S/ NOTA*/
         UPDATE TRABIMPOSNFTOT
         SET IDAVISOSTOT = '00',
             IDERROSTOT  = '0'
         WHERE CDFILIMPTOT  = P_CDFILIAL    AND
               NRLANCIMPTOT = P_NRLANCTONF;

         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV02
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV02;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         IF V_IDOPERDEVONF = w_S THEN
            BEGIN
               SELECT NRLANCTONF
               INTO V_DEVOLORIG
               FROM NOTAFISCAL
               WHERE CDFILIAL   = P_CDFILIAL    AND
                     NRLANCTONF = P_NRLANCTONF;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_DEVOLORIG := NULL;
            END;
         END IF;

         /* (AVISO) PERCENTUAL DE ICMS DA TABELA DIFERE DO PERCENTUAL INFORMADO */

         UPDATE TRABITPROIMPOS
         SET IDAVISIMPOIT   = w_1 || SUBSTR(IDAVISIMPOIT,2,4)
         WHERE CDFILIMPOIT  = P_CDFILIAL    AND
               NRLANCIMPOIT = P_NRLANCTONF  AND
               NRSEQITPRO   = w_1           AND
               V_IDERROAVISO <> w_N         AND
               V_DEVOLORIG IS NULL          AND
               IDVALORINFO <> w_S           AND
               CDSTRIBITNFE <> '51'         AND
               IDCONSDIFALIQ <> 'S'         AND
               (V_CDTIPOOPER <> w_03)       AND
               CDIMPOSTOIT IN (SELECT CDIMPOSTO
                               FROM IMPOSTO
                               WHERE IDCONSISTENT = w_S    AND
                                     IDUTVLRQTDEIMPO <> w_S
                               MINUS
                               SELECT CDIMPOSTO
                               FROM IMPOSEMPRE
                               WHERE CDEMPRESA = V_CDEMPRESA AND
                                     IDCONIMPENT = w_N
                               MINUS
                               SELECT CDIMPOSTO
                               FROM IMPOSFORN
                               WHERE (CDFORNECED   = V_CDFORNECED) AND
                                     ((IDIMPOSOPCI = w_S) AND (TRABITPROIMPOS.VRALIQIMPOIT = 0))) AND
               V_FAIXATOLER < (SELECT ABS(NVL(SUM(VRIMPOSTOIT),0) - TRABITPROIMPOS.VRIMPOSIT) AS VRDIFERENCA
                               FROM ITEMPROIMPOS
                               WHERE CDFILIAL     = P_CDFILIAL    AND
                                     NRLANCTONF   = P_NRLANCTONF  AND
                                     NRSEQITPROD  = TRABITPROIMPOS.NRSEQIMPOIT AND
                                     CDIMPOSTO    = TRABITPROIMPOS.CDIMPOSTOIT AND
                                     NRITPROIMPOS = w_1);
-------------------------------------------------------------------------------------------------------
         /*01/ (AVISO) PERCENTUAL ICMS DO ITEM DIFERE DO VALOR ICMS DO ITEM*/
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV03
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV03;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         UPDATE TRABITPROIMPOS
         SET IDAVISIMPOIT = SUBSTR(IDAVISIMPOIT,1,4) || w_1
         WHERE (CDFILIMPOIT  = P_CDFILIAL)   AND
               (NRLANCIMPOIT = P_NRLANCTONF) AND
               (NRSEQITPRO   = w_1)          AND
               (CDSTRIBITNFE <> '51')        AND
               (V_IDERROAVISO <> w_N)        AND
               (IDCONSDIFALIQ <> 'S')        AND
               (V_CDTIPOOPER <> w_03) AND
               (CDIMPOSTOIT IN (SELECT CDIMPOSTO
                                FROM IMPOSTO I
                                WHERE I.IDCONSISTENT = w_S          AND
                                      NVL(I.IDUTVRMENSBC,w_N) = w_N AND
                                      IDUTVLRQTDEIMPO <> w_S        AND
                                      NOT EXISTS(SELECT CDIMPOSTO
                                                 FROM IMPOSTO IMP
                                                 WHERE IMP.CDIMPOSTRIB  = I.CDIMPOSTO AND
                                                       IMP.IDVRIMPSTRIB = w_S)
                                MINUS
                                SELECT CDIMPOSTO
                                FROM IMPOSEMPRE
                                WHERE CDEMPRESA = V_CDEMPRESA AND
                                      IDCONIMPENT = w_N)) AND
               (V_FAIXATOLER < ABS(ROUND((VRBCALCIMPOIT * (VRALIQIMPOIT/100)),2) - (VRIMPOSIT)));
------------------------------------------------------------------------------------------------------
         /* (ERRO) ICMS DA NF INVALIDO */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_ER01
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_ER01;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
            END;
         END IF;

         UPDATE TRABIMPOSNFTOT
         SET IDERROSTOT = w_1
         WHERE CDFILIMPTOT  = P_CDFILIAL    AND
               NRLANCIMPTOT = P_NRLANCTONF  AND
               V_IDERROAVISO <> w_N         AND
               CDIMPOSTOT   IN (SELECT CDIMPOSTO
                                FROM IMPOSTO
                                WHERE IDPRODNOTA   = w_P            AND
                                      NVL(IDUTVRMENSBC,w_N) = w_N   AND
                                      NVL(IDPREIMPENTR,w_N) = w_N   AND
                                      IDUTVLRQTDEIMPO <> w_S)       AND
               V_FAIXATOLER <  (SELECT ABS(SUM(ROUND(VRIMPOSIT,V_NRDECTOTANF)) - TRABIMPOSNFTOT.VRIMPOSTOT) AS VRIMPOSIT
                                FROM TRABITPROIMPOS
                                WHERE CDFILIMPOIT  = P_CDFILIAL                AND
                                      NRLANCIMPOIT = P_NRLANCTONF              AND
                                      CDIMPOSTOIT  = TRABIMPOSNFTOT.CDIMPOSTOT AND
                                      NRSEQITPRO   = w_1);
-------------------------------------------------------------------------------------------------------
         /* (AVISO) VALOR DE INSS INFORMADO DIFERENTE DA TABELA */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV07
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV07;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         UPDATE TRABIMPOSNFTOT
         SET IDAVISOSTOT = '10'
         WHERE CDFILIMPTOT  = P_CDFILIAL    AND
               NRLANCIMPTOT = P_NRLANCTONF  AND
               V_IDERROAVISO <> w_N         AND
               --((V_CDTIPOOPER = w_03 AND VRIMPOSTOT <> 0) OR
               (V_CDTIPOOPER <> w_03) AND
               CDIMPOSTOT IN  (SELECT CDIMPOSTO
                               FROM IMPOSTO
                               WHERE IDCONSISTENT = w_S AND
                                     IDPRODNOTA   = w_C
                               MINUS
                               SELECT CDIMPOSTO
                               FROM IMPOSEMPRE
                               WHERE CDEMPRESA = V_CDEMPRESA AND
                                     IDCONIMPENT = w_N
                               MINUS
                               SELECT CDIMPOSTO
                               FROM IMPOSFORN
                               WHERE (CDFORNECED   = V_CDFORNECED)) AND
               V_FAIXATOLER <  (SELECT ABS(ROUND(NVL(SUM(VRTOTIMPONF),0),V_NRDECTOTANF) - TRABIMPOSNFTOT.VRIMPOSTOT) AS VRTOTIMPONF
                                FROM IMPOSNFTOT
                                WHERE CDFILIAL   = P_CDFILIAL    AND
                                      NRLANCTONF = P_NRLANCTONF  AND
                                      CDIMPOSTO  = TRABIMPOSNFTOT.CDIMPOSTOT);
-------------------------------------------------------------------------------------------------------
         /* (AVISO) INSS DA NF DIFERE DA ALIQUOTA DO FORNECEDOR. */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV08
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV08;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         UPDATE TRABIMPOSNFTOT
         SET IDAVISOSTOT = w_01
         WHERE CDFILIMPTOT  = P_CDFILIAL    AND
               NRLANCIMPTOT = P_NRLANCTONF  AND
               V_IDERROAVISO <> w_N         AND
               --((V_CDTIPOOPER = w_03 AND VRIMPOSTOT <> 0) OR
               (V_CDTIPOOPER <> w_03) AND
               CDIMPOSTOT IN  (SELECT CDIMPOSTO
                               FROM IMPOSTO
                               WHERE IDCONSISTENT = w_S AND
                                     IDPRODNOTA   = w_C
                               INTERSECT
                               SELECT CDIMPOSTO
                               FROM IMPOSFORN
                               WHERE (CDFORNECED   = V_CDFORNECED) AND
                                     ((IDIMPOSOPCI =  w_S AND TRABIMPOSNFTOT.VRIMPOSTOT = 0) OR
                                       IDIMPOSOPCI <> w_S)
                               MINUS
                               SELECT CDIMPOSTO
                               FROM IMPOSEMPRE
                               WHERE CDEMPRESA = V_CDEMPRESA AND
                                     IDCONIMPENT = w_N) AND
               V_FAIXATOLER <  (SELECT ABS(NVL(SUM(VRTOTIMPONF),0) - TRABIMPOSNFTOT.VRIMPOSTOT) AS VRTOTIMPONF
                                FROM IMPOSNFTOT
                                WHERE CDFILIAL   = P_CDFILIAL    AND
                                      NRLANCTONF = P_NRLANCTONF  AND
                                      CDIMPOSTO  = TRABIMPOSNFTOT.CDIMPOSTOT);
      END IF;
-------------------------------------------------------------------------------------------------------
      UPDATE NOTAFISCAL
      SET IDERROS  = '0000',
          IDAVISOS = '00000'
      WHERE CDFILIAL   =  P_CDFILIAL AND
            NRLANCTONF =  P_NRLANCTONF;

      IF (V_CDTIPOOPER = '05') AND (NOT ((V_IDNFGERADAVIAXML = w_S) AND (V_IDGERNFIGUALXML = w_S))) THEN
         /* (ERRO) TOTAL NOTA FISCAL DIFERE SOMA DOS ITEMS */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_ER02
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_ER02;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         SELECT ROUND(SUM(N.VRNOTAFISC),2)
         INTO V_VRNOTAFISC
         FROM ITEMNOTAFISC I, NOTAFISCAL N
         WHERE I.CDFILIAL   = P_CDFILIAL    AND
               I.NRLANCTONF = P_NRLANCTONF  AND
               N.CDFILIAL   = I.CDFILIALIT  AND
               N.NRLANCTONF = I.NRLANCTONFIT;


         V_PERCDIF := P_VRLANCAMENTO - V_VRNOTAFISC;

         IF (V_FAIXATOLER = 0 OR V_FAIXATOLER IS NULL) AND (V_PERCDIF <> 0) THEN
            UPDATE NOTAFISCAL
            SET IDERROS = '1010'
            WHERE CDFILIAL   =  P_CDFILIAL    AND
                  NRLANCTONF =  P_NRLANCTONF  AND
                  V_IDERROAVISO <> w_N;
         ELSE
            V_VALRMAX := (V_VRNOTAFISC + V_FAIXATOLER);
            V_VALRMIN := (V_VRNOTAFISC - V_FAIXATOLER);

            IF (V_VALRMIN > P_VRLANCAMENTO) OR (V_VALRMAX < P_VRLANCAMENTO) THEN
               UPDATE NOTAFISCAL
               SET IDERROS = '1010'
               WHERE CDFILIAL   =  P_CDFILIAL    AND
                     NRLANCTONF =  P_NRLANCTONF  AND
                     V_IDERROAVISO <> w_N;
            END IF;
         END IF;
------------------------------------------------------------------------------------------------------
      ELSE
         IF NOT ((V_IDNFGERADAVIAXML = w_S) AND (V_IDGERNFIGUALXML = w_S)) THEN
            IF V_CDTIPOOPER = '11' THEN
               /* (ERRO) SOMA DAS ENTRADAS SEM NOTA FISCAL DIFERE DO VALOR TOTAL DA NOTA */
               BEGIN
                  SELECT IDERROAVISOFIL
                  INTO V_IDERROAVISO
                  FROM ERROAVISONFFIL
                  WHERE CDERROAVISOFIL = w_ER05
                    AND CDFILIAL = V_CDFILIORIG;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_IDERROAVISO := NULL;
               END;

               IF V_IDERROAVISO IS NULL THEN
                  BEGIN
                     SELECT IDERROAVISO
                     INTO V_IDERROAVISO
                     FROM ERROAVISONF
                     WHERE CDERROAVISO = w_ER05;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_IDERROAVISO := NULL;
                  END;
               END IF;

               SELECT NVL(SUM(VRITEMENCA),0) AS VRSOMAENCA
                 INTO V_VRSOMAENCA
                 FROM ITEMENCARGO I, ENCARGO E
                WHERE I.CDFILIAL   = P_CDFILIAL
                  AND I.NRLANCTONF = P_NRLANCTONF
                  AND I.CDENCARGO  = E.CDENCARGO
                  AND E.IDENCARGO = 'A';

               SELECT NVL(SUM(VRITEMENCA),0) AS VRMENOENCA
                 INTO V_VRMENOENCA
                 FROM ITEMENCARGO I, ENCARGO E
                WHERE I.CDFILIAL   = P_CDFILIAL
                  AND I.NRLANCTONF = P_NRLANCTONF
                  AND I.CDENCARGO  = E.CDENCARGO
                  AND E.IDENCARGO IN ('D', 'E');

               SELECT NVL(SUM(VRIMPOSTOIT),0) AS VRSOMAIMPO
                 INTO V_VRSOMAIMPO
                 FROM ITEMPROIMPOS NF, IMPOSTO IP
                WHERE NF.CDFILIAL   = P_CDFILIAL
                  AND NF.NRLANCTONF = P_NRLANCTONF
                  AND NF.CDIMPOSTO = IP.CDIMPOSTO
                  AND IP.IDTRATIMPO = 'A';

               SELECT NVL(SUM(VRADICFECP), 0)
                 INTO V_SOMAVRADICFECP
                 FROM TRABITPROIMPOS T, IMPOSTO I
                WHERE T.CDFILIMPOIT  = P_CDFILIAL
                  AND T.NRLANCIMPOIT = P_NRLANCTONF
                  AND I.CDIMPOSTO  = T.CDIMPOSTOIT
                  AND I.IDTRATIMPO   = 'A';

               V_VRSOMAIMPO := V_VRSOMAIMPO + V_SOMAVRADICFECP;    

               SELECT NVL(SUM(VRIMPOSTOIT),0) AS VRMENOIMPO
                 INTO V_VRMENOIMPO
                 FROM ITEMPROIMPOS NF, IMPOSTO IP
                WHERE NF.CDFILIAL   = P_CDFILIAL
                  AND NF.NRLANCTONF = P_NRLANCTONF
                  AND NF.CDIMPOSTO = IP.CDIMPOSTO
                  AND IP.IDTRATIMPO = 'D';

               UPDATE NOTAFISCAL
               SET IDERROS = '1100'
               WHERE CDFILIAL   =  P_CDFILIAL    AND
                     NRLANCTONF =  P_NRLANCTONF  AND
                     V_IDERROAVISO <> w_N        AND
                     V_FAIXATOLER < (SELECT ABS((NOTAFISCAL.VRNOTAFISC - (SUM(N.VRPRODUTOS) + V_VRSOMAENCA - V_VRMENOENCA + V_VRSOMAIMPO - V_VRMENOIMPO)))
                                       FROM (SELECT N.CDFILIAL, N.NRLANCTONF, ROUND(SUM(N.VRPRODUTOS),V_NRDECTOTANF) AS VRPRODUTOS
                                               FROM ITEMNOTAFISC I, NOTAFISCAL N
                                              WHERE I.CDFILIAL   = P_CDFILIAL    AND
                                                    I.NRLANCTONF = P_NRLANCTONF  AND
                                                    N.CDFILIAL   = I.CDFILIALIT  AND
                                                    N.NRLANCTONF = I.NRLANCTONFIT
                                              GROUP BY N.CDFILIAL, N.NRLANCTONF) N);

   ---------   ----------------------------------------------------------------------------------------------
            END IF;
         END IF;

         IF NOT ((V_IDNFGERADAVIAXML = w_S) AND (V_IDGERNFIGUALXML = w_S)) THEN
            /* (ERRO) TOTAL NOTA FISCAL DIFERE SOMA DOS ITEMS */
            BEGIN
               SELECT IDERROAVISOFIL
               INTO V_IDERROAVISO
               FROM ERROAVISONFFIL
               WHERE CDERROAVISOFIL = w_ER02
                 AND CDFILIAL = V_CDFILIORIG;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;

            IF V_IDERROAVISO IS NULL THEN
               BEGIN
                  SELECT IDERROAVISO
                  INTO V_IDERROAVISO
                  FROM ERROAVISONF
                  WHERE CDERROAVISO = w_ER02;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_IDERROAVISO := NULL;
               END;
            END IF;

            IF (V_IDOPERDEVONF <> w_S) OR ((V_IDOPERDEVONF = w_S) AND (V_DEVOLORIG IS NULL)) THEN

               UPDATE NOTAFISCAL
                  SET IDERROS = w_1||SUBSTR(IDERROS,2,1)||w_1||SUBSTR(IDERROS,4,1)
                WHERE CDFILIAL   =  P_CDFILIAL    AND
                      NRLANCTONF =  P_NRLANCTONF  AND
                      V_IDERROAVISO <> w_N        AND
                      V_FAIXATOLER < ABS(ROUND(VRNOTAFISC,V_NRDECTOTANF) - ROUND(P_VRLANCAMENTO,V_NRDECTOTANF)) AND
                      V_IDREPLICADA IS NULL;
            ELSE
               UPDATE NOTAFISCAL
               SET VRNOTAFISC = P_VRLANCAMENTO
               WHERE CDFILIAL   =  P_CDFILIAL    AND
                     NRLANCTONF =  P_NRLANCTONF;
            END IF;
         END IF;
      END IF;
-------------------------------------------------------------------------------------------------------

      IF (V_CDTIPOOPER NOT IN ('02','04','06','07','09')) AND (V_IDORIGDEST = w_F) AND (NOT ((V_IDNFGERADAVIAXML = w_S) AND (V_IDGERNFIGUALXML = w_S))) THEN
         /* (ERRO) TOTAL DE VENCIMENTOS DIFERE VALOR TOTAL NOTA FISCAL */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_ER03
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_ER03;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         SELECT ROUND(NVL(SUM(T.VRIMPOSTOT),0),V_NRDECTOTANF) INTO V_VRIMPOSTOT
         FROM TRABIMPOSNFTOT T, IMPOSTO I
         WHERE T.CDFILIMPTOT  = P_CDFILIAL    AND
               T.NRLANCIMPTOT = P_NRLANCTONF  AND
               V_IDERROAVISO  <> w_N          AND
               T.CDIMPOSTOT   = I.CDIMPOSTO   AND
               I.IDTRATIMPO   = w_R           AND
               I.IDDESCRFONTE = w_R;

         /* Verifica o valor bruto dos itens que nao irao compor o vencimento da nota fiscal */
         SELECT NVL(SUM(VRBRUTITEM),0) AS VRSUBTRAIVENC
           INTO V_VRSUBTRAIVENC
           FROM ITEMPRODUTO IT, TPOPERCFOP T
          WHERE IT.CDFILIAL   = P_CDFILIAL
            AND IT.NRLANCTONF = P_NRLANCTONF
            AND T.IDENTRSAIDOP = V_IDENTRSAID
            AND T.CDTIPOOPER   = V_CDTIPOOPER
            AND T.IDCOMPOEVENC = 'N'
            AND T.CDFISCOPER   = IT.CDFISOPEITEM;

         UPDATE NOTAFISCAL
         SET IDERROS = w_1 || SUBSTR(IDERROS,2,2) || w_1
         WHERE CDFILIAL   =  P_CDFILIAL    AND
               NRLANCTONF =  P_NRLANCTONF  AND
               V_FAIXATOLER < (SELECT ABS(NVL(SUM(VRORIGPAG),(NOTAFISCAL.VRNOTAFISC - V_VRIMPOSTOT - V_VRSUBTRAIVENC)) - (NOTAFISCAL.VRNOTAFISC - V_VRIMPOSTOT - V_VRSUBTRAIVENC))
                               FROM CONTAPAGAR
                               WHERE CDFILIAL   =  P_CDFILIAL    AND
                                     NRLANCTONF =  P_NRLANCTONF);
-------------------------------------------------------------------------------------------------------
      /* (AVISO) Data de Vencimento informada anterior a data de processamento.*/
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV17
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV17;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         UPDATE NOTAFISCAL
            SET IDAVISOS       =  SUBSTR(IDAVISOS,1,4) || W_1
          WHERE CDFILIAL       =  P_CDFILIAL    AND
                NRLANCTONF     =  P_NRLANCTONF  AND
                V_IDERROAVISO  <> w_N           AND
                (SELECT DECODE(V_IDUTILDTPROC,'S',DECODE(DTPROCESSA, NULL, TO_DATE(SYSDATE), DTPROCESSA),TO_DATE(SYSDATE))
                   FROM PARAMFILIAL
                  WHERE CDFILIAL = V_CDFILIORIG) > (SELECT MIN(DTORIGVENPAG)
                                                      FROM CONTAPAGAR
                                                     WHERE CDFILIAL   =  P_CDFILIAL    AND
                                                           NRLANCTONF =  P_NRLANCTONF);
      END IF;
-------------------------------------------------------------------------------------------------------
      IF NOT V_NRAUTOFORN IS NULL THEN

         /* (AVISO) DATA DE VENCIMENTO DA NOTA FISCAL DIFERE DA DATA DE PROGRAMADA NA AF */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV15
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV15;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         SELECT MAX(QTDIASVENPAG)
         INTO V_NRORIGVENPAG
         FROM CONTAPAGAR
         WHERE CDFILIAL    =  P_CDFILIAL    AND
               NRLANCTONF  =  P_NRLANCTONF  AND
               NRSEQVENPAG = w_01;

         BEGIN
            SELECT FORMPGTO.IDTIPODTPGTO
            INTO V_IDTIPODTPGTO
            FROM AUTOFORNE, FORMPGTO
            WHERE AUTOFORNE.CDFILDEST  = V_CDFILDEST   AND
                  AUTOFORNE.CDFILLANC  = V_CDFILLANC   AND
                  AUTOFORNE.NRAUTOFORN = V_NRAUTOFORN  AND
                  AUTOFORNE.CDFORMPGTO = FORMPGTO.CDFORMPGTO;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDTIPODTPGTO := NULL;
         END;

         IF NOT(V_IDTIPODTPGTO IN ('EMIN','ENIN','UEMI')) THEN

            UPDATE NOTAFISCAL
            SET IDAVISOS = w_1 || SUBSTR(IDAVISOS,2,2)|| w_1 || SUBSTR(IDAVISOS,5,1)
            WHERE CDFILIAL       =  P_CDFILIAL     AND
                  NRLANCTONF     =  P_NRLANCTONF   AND
                  V_NRORIGVENPAG <> (SELECT MIN(C.NRDIASPGTOAF)
                                     FROM PRAZOPGTAF C, ITEMAUTF I
                                     WHERE I.CDFILLANC   = C.CDFILLANC   AND
                                           I.CDFILDEST   = C.CDFILDEST   AND
                                           I.NRAUTOFORN  = C.NRAUTOFORN  AND
                                           I.CDFILLANC   = V_CDFILLANC   AND
                                           I.CDFILDEST   = V_CDFILDEST   AND
                                           I.NRAUTOFORN  = V_NRAUTOFORN) AND
                  V_IDERROAVISO <> w_N;
         END IF;

         IF (V_IDTIPODTPGTO IN ('EMIN','ENIN','UEMI')) THEN

            UPDATE NOTAFISCAL
            SET IDAVISOS = w_1 || SUBSTR(IDAVISOS,2,2)|| w_1 || SUBSTR(IDAVISOS,5,1)
            WHERE CDFILIAL       =  P_CDFILIAL     AND
                  NRLANCTONF     =  P_NRLANCTONF   AND
                  V_NRORIGVENPAG <> (SELECT MIN(C.NRDIASPGTOAF)-1
                                     FROM PRAZOPGTAF C, ITEMAUTF I
                                     WHERE I.CDFILLANC   = C.CDFILLANC   AND
                                           I.CDFILDEST   = C.CDFILDEST   AND
                                           I.NRAUTOFORN  = C.NRAUTOFORN  AND
                                           I.CDFILLANC   = V_CDFILLANC   AND
                                           I.CDFILDEST   = V_CDFILDEST   AND
                                           I.NRAUTOFORN  = V_NRAUTOFORN) AND
                  V_IDERROAVISO <> w_N;
         END IF;

         /* (AVISO) DATA DE ENTRADA DA NOTA FISCAL DIFERE DA DATA DE PROGRAMADA NA AF */
         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDERROAVISO
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV12
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDERROAVISO := NULL;
         END;

         IF V_IDERROAVISO IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDERROAVISO
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV12;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;
         END IF;

         UPDATE NOTAFISCAL
         SET IDAVISOS = '11' || SUBSTR(IDAVISOS,3,3)
         WHERE CDFILIAL   =  P_CDFILIAL    AND
               NRLANCTONF =  P_NRLANCTONF  AND
               DTENTRSAID <> V_DTPROGENTAF AND
               V_IDERROAVISO <> w_N;

         /* Se nao estiver sendo utilizado fornecedor relacionado para a AF  */
         IF V_IDUTRELACFORNAF <> 'S' THEN
            /* (AVISO) FORNECEDOR DA NOTA FISCAL DIFERE DO FORNECEDOR DA AF */
            BEGIN
               SELECT IDERROAVISOFIL
               INTO V_IDERROAVISO
               FROM ERROAVISONFFIL
               WHERE CDERROAVISOFIL = w_AV01
                 AND CDFILIAL = V_CDFILIORIG;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;

            IF V_IDERROAVISO IS NULL THEN
               BEGIN
                  SELECT IDERROAVISO
                  INTO V_IDERROAVISO
                  FROM ERROAVISONF
                  WHERE CDERROAVISO = w_AV01;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_IDERROAVISO := NULL;
               END;
            END IF;

            UPDATE NOTAFISCAL
            SET IDAVISOS = w_1 || SUBSTR(IDAVISOS,2,1) || w_1|| SUBSTR(IDAVISOS,4,2)
            WHERE CDFILIAL   =  P_CDFILIAL    AND
                  NRLANCTONF =  P_NRLANCTONF  AND
                  V_IDERROAVISO <> w_N        AND
                  CDFORNECED <> (SELECT MAX(CDFORNECED)
                                 FROM AUTOFORNE AF
                                 WHERE (AF.CDFILDEST     = V_CDFILDEST)  AND
                                       (AF.CDFILLANC     = V_CDFILLANC)  AND
                                       (AF.NRAUTOFORN    = V_NRAUTOFORN));
         ELSE
            /* (AVISO) FORNECEDOR DA NOTA FISCAL DIFERE DO FORNECEDOR DA AF */
            BEGIN
               SELECT IDERROAVISOFIL
               INTO V_IDERROAVISO
               FROM ERROAVISONFFIL
               WHERE CDERROAVISOFIL = w_AV01
                 AND CDFILIAL = V_CDFILIORIG;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDERROAVISO := NULL;
            END;

            IF  V_IDERROAVISO IS NULL THEN
               BEGIN
                  SELECT IDERROAVISO
                  INTO V_IDERROAVISO
                  FROM ERROAVISONF
                  WHERE CDERROAVISO = w_AV01;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_IDERROAVISO := NULL;
               END;
            END IF;

            UPDATE NOTAFISCAL
            SET IDAVISOS = w_1 || SUBSTR(IDAVISOS,2,1) || w_1|| SUBSTR(IDAVISOS,4,2)
            WHERE CDFILIAL   =  P_CDFILIAL    AND
                  NRLANCTONF =  P_NRLANCTONF  AND
                  V_IDERROAVISO <> w_N        AND
                  CDFORNECED NOT IN (SELECT CDFORNECED
                                     FROM AUTOFORNE AF
                                     WHERE (AF.CDFILDEST  = V_CDFILDEST)  AND
                                           (AF.CDFILLANC  = V_CDFILLANC)  AND
                                           (AF.NRAUTOFORN = V_NRAUTOFORN)
                                     UNION
                                     SELECT N.CDFORNECED
                                     FROM
                                        NOTAFISCAL N, FORNECEDOR F, AUTOFORNE AF
                                     WHERE
                                        (N.CDFILIAL    = P_CDFILIAL)      AND
                                        (N.NRLANCTONF  = P_NRLANCTONF)    AND
                                        (N.CDFORNECED  = F.CDFORNECED)    AND
                                        (AF.CDFILDEST  = V_CDFILDEST)     AND
                                        (AF.CDFILLANC  = V_CDFILLANC)     AND
                                        (AF.NRAUTOFORN = V_NRAUTOFORN)    AND
                                        (AF.CDFORNECED = F.CDFORNERELAC)
                                     UNION
                                     SELECT N.CDFORNECED
                                     FROM
                                        NOTAFISCAL N, FORNECEDOR F, AUTOFORNE AF, FORNECEDOR FR
                                     WHERE
                                        (N.CDFILIAL     = P_CDFILIAL)      AND
                                        (N.NRLANCTONF   = P_NRLANCTONF)    AND
                                        (N.CDFORNECED   = FR.CDFORNECED)   AND
                                        (AF.CDFILDEST   = V_CDFILDEST)     AND
                                        (AF.CDFILLANC   = V_CDFILLANC)     AND
                                        (AF.NRAUTOFORN  = V_NRAUTOFORN)    AND
                                        (AF.CDFORNECED  = F.CDFORNECED)    AND
                                        (NOT F.CDFORNERELAC IS NULL)       AND
                                        (F.CDFORNERELAC = FR.CDFORNERELAC)
                                     UNION
                                     SELECT F.CDFORNERELAC AS CDFORNECED
                                     FROM
                                        AUTOFORNE AF, FORNECEDOR F
                                     WHERE (AF.CDFILDEST  = V_CDFILDEST)   AND
                                           (AF.CDFILLANC  = V_CDFILLANC)   AND
                                           (AF.NRAUTOFORN = V_NRAUTOFORN)  AND
                                           (AF.CDFORNECED = F.CDFORNECED)  AND
                                           (NOT F.CDFORNERELAC IS NULL));
         END IF;

         BEGIN
            SELECT IDERROAVISOFIL
            INTO  V_IDCONQTDEAF
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV11
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDCONQTDEAF := NULL;
         END;

         IF V_IDCONQTDEAF IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO  V_IDCONQTDEAF
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV11;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDCONQTDEAF := NULL;
            END;
         END IF;

         BEGIN
            SELECT IDERROAVISOFIL
            INTO  V_IDCONVRUNIAF
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV13
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDCONVRUNIAF := NULL;
         END;

         IF V_IDCONVRUNIAF IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO  V_IDCONVRUNIAF
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV13;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDCONVRUNIAF := NULL;
            END;
         END IF;

         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDCONQTDEREC
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV14
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDCONQTDEREC := NULL;
         END;

         IF V_IDCONQTDEREC IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDCONQTDEREC
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV14;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDCONQTDEREC := NULL;
            END;
         END IF;

         BEGIN
            SELECT IDERROAVISOFIL
            INTO V_IDPRODNFAF
            FROM ERROAVISONFFIL
            WHERE CDERROAVISOFIL = w_AV16
              AND CDFILIAL = V_CDFILIORIG;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDPRODNFAF := NULL;
         END;

         IF V_IDPRODNFAF IS NULL THEN
            BEGIN
               SELECT IDERROAVISO
               INTO V_IDPRODNFAF
               FROM ERROAVISONF
               WHERE CDERROAVISO = w_AV16;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDPRODNFAF := NULL;
            END;
         END IF;

         FOR C_ITEMPRODUTO IN (SELECT DECODE(N.IDENTRSAIDOP, 'E', N.CDFILIRECE, N.CDFILIEMIT) AS CDFILIORIG,
                                      I.NRSEQITPROD, I.CDFILDEST, I.CDFILIAL, I.NRAUTOFORN, I.CDPRODUTO, I.CDMARCA,
                                      I.DTPROGENTAF, P.CDPRODESTO, P.CDARVPROD, I.VRUNITITEM
                                 FROM ITEMPRODUTO I, NOTAFISCAL N, PRODUTO P
                                WHERE (N.CDFILIAL   = P_CDFILIAL)   AND
                                      (N.NRLANCTONF = P_NRLANCTONF) AND
                                      (N.CDFILIAL = I.CDFILIAL)     AND
                                      (N.NRLANCTONF = I.NRLANCTONF) AND
                                      (I.CDPRODUTO  = P.CDPRODUTO)) LOOP

            BEGIN
               SELECT VRPTOLQTITAF, VRPTOLVRITAF
                 INTO V_VRPTOLQTITAF, V_VRPTOLVRITAF
                 FROM PRODFILI
                WHERE CDFILIAL  = C_ITEMPRODUTO.CDFILIORIG  AND
                   CDPRODUTO = C_ITEMPRODUTO.CDPRODUTO;
            EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_VRPTOLQTITAF := 0;  V_VRPTOLVRITAF := 0;
            END;

            IF V_VRPTOLQTITAF IS NULL THEN
               V_VRPTOLQTITAF := NVL(V_VRPETOLEQTAF,0.00);
            END IF;

            IF V_VRPTOLVRITAF IS NULL THEN
               V_VRPTOLVRITAF := NVL(V_VRPETOLEVRAF,0.00);
            END IF;

            /* (AVISO) QUANTIDADE DE PRODUTO INFORMADA DIFERE DA QUANTIDADE PREVISTA NA AF */

            V_QTNFRELACAF := NULL;
            FOR C_QTDRELACAF IN(
               SELECT (QTNFRELACAF) * (P.VRFATOCONV) AS QTNFRELACAF
                 --INTO V_QTNFRELACAF, V_VRFATOCONVAF
                 FROM NFRELACAF N, ITEMAUTF I, PRODUTO P, PRODUTO P1
                WHERE (N.CDFILIAL    = P_CDFILIAL)               AND
                      (N.NRLANCTONF  = P_NRLANCTONF)             AND
                      (N.CDFILDEST   = I.CDFILDEST)              AND
                      (N.CDFILLANC   = I.CDFILLANC)              AND
                      (N.NRAUTOFORN  = I.NRAUTOFORN)             AND
                      (N.CDMARCA     = I.CDMARCA)                AND
                      (N.CDPRODUTO   = I.CDPRODUTO)              AND
                      (N.DTPROGENTAF = I.DTPROGENTAF)            AND
                      (P1.CDPRODUTO  = C_ITEMPRODUTO.CDPRODUTO)  AND
                      (P1.CDPRODESTO = P.CDPRODESTO)             AND
                      (N.CDMARCA     = C_ITEMPRODUTO.CDMARCA)    AND
                      (N.CDPRODUTO   = P.CDPRODUTO)              AND
                      (I.CDPRODUTO   = P.CDPRODUTO)
            )LOOP
               IF V_QTNFRELACAF IS NULL THEN
                  V_QTNFRELACAF := C_QTDRELACAF.QTNFRELACAF;
               ELSE
                  V_QTNFRELACAF := V_QTNFRELACAF + C_QTDRELACAF.QTNFRELACAF;
               END IF;
            END LOOP;

            V_QTITEMPROD := NULL;
            FOR C_QTITEMPROD  IN(
               SELECT ((I.QTITEMPROD) * (P.VRFATOCONV)) AS QTITEMPROD
                 --INTO V_QTITEMPROD
                 FROM ITEMPRODUTO I, PRODUTO P, PRODUTO P1
                WHERE (I.CDFILIAL   = P_CDFILIAL)              AND
                      (I.NRLANCTONF = P_NRLANCTONF)            AND
                      (I.CDPRODUTO  = P.CDPRODUTO)             AND
                      (I.CDMARCA    = C_ITEMPRODUTO.CDMARCA)   AND
                      (I.CDPRODUTO  = P.CDPRODUTO)             AND
                      (P1.CDPRODUTO = C_ITEMPRODUTO.CDPRODUTO) AND
                      (P.CDPRODESTO = P1.CDPRODESTO)
            )LOOP
               IF V_QTITEMPROD IS NULL THEN
                  V_QTITEMPROD := C_QTITEMPROD.QTITEMPROD;
               ELSE
                  V_QTITEMPROD := V_QTITEMPROD + C_QTITEMPROD.QTITEMPROD;
               END IF;
            END LOOP;

            UPDATE ITEMPRODUTO
            SET IDAVISOSITEM = SUBSTR(IDAVISOSITEM,1,2) || w_1 || SUBSTR(IDAVISOSITEM,4,3)
            WHERE (CDFILIAL    = P_CDFILIAL)                AND
                  (NRLANCTONF  = P_NRLANCTONF)              AND
                  (NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD) AND
                  ((V_QTITEMPROD  > (V_QTNFRELACAF + ((ROUND(V_QTNFRELACAF,3) * V_VRPTOLQTITAF)/100))) OR
                   ((V_QTITEMPROD < (V_QTNFRELACAF - ((ROUND(V_QTNFRELACAF,3) * V_VRPTOLQTITAF)/100))) AND
                    (V_IDCONSVLRMAF) = w_S))                AND
                  (V_IDCONQTDEAF <> w_N);

            /* (AVISO) VALOR UNITARIO DO PRODUTO DIFERE DO VALOR UNITARIO PREVISTO NA AF */
            V_IGUAL := 'N';
            V_VRNFRELACAF := 0.00;
            IF (C_ITEMPRODUTO.NRAUTOFORN) IS NOT NULL THEN --VALIDAR PARA OS ITENS QUE FORAM IMPORTADOS DA AF
               FOR C_VRITEMAF IN(
                  SELECT ROUND((((VRNFRELACAF) / P.VRFATOCONV) * P1.VRFATOCONV), 5) - (NVL(IT.VRDESCITMAF, 0) / QTPROGENTAF) AS VRITEMAF
                    FROM NFRELACAF N, PRODUTO P, PRODUTO P1, ITEMAUTF IT
                   WHERE (N.CDFILIAL    = P_CDFILIAL)                AND
                         (N.NRLANCTONF  = P_NRLANCTONF)              AND
                         (N.NRAUTOFORN  = C_ITEMPRODUTO.NRAUTOFORN)  AND
                         (N.CDPRODUTO   = P.CDPRODUTO)               AND
                         (P.CDPRODESTO  = P1.CDPRODESTO)             AND
                         (P1.CDPRODUTO  = C_ITEMPRODUTO.CDPRODUTO)   AND
                         (N.CDMARCA     = C_ITEMPRODUTO.CDMARCA)     AND
                         (N.DTPROGENTAF = C_ITEMPRODUTO.DTPROGENTAF) AND
                         (IT.CDFILDEST   = N.CDFILDEST)   AND
                         (IT.CDFILLANC   = N.CDFILLANC)   AND
                         (IT.NRAUTOFORN  = N.NRAUTOFORN)  AND
                         (IT.CDMARCA     = N.CDMARCA)     AND
                         (IT.CDPRODUTO   = N.CDPRODUTO)   AND
                         (IT.DTPROGENTAF = N.DTPROGENTAF)
                  )LOOP

                  IF (V_IGUAL = 'N') THEN
                     IF (ROUND(C_ITEMPRODUTO.VRUNITITEM, 5) = ROUND(C_VRITEMAF.VRITEMAF, 5)) THEN
                        V_VRNFRELACAF := C_VRITEMAF.VRITEMAF;
                        V_IGUAL := 'S';
                     ELSE
                        IF (ABS(C_VRITEMAF.VRITEMAF - C_ITEMPRODUTO.VRUNITITEM)) < (ABS(C_VRITEMAF.VRITEMAF - V_VRNFRELACAF)) THEN
                           V_VRNFRELACAF := C_VRITEMAF.VRITEMAF;
                        ELSE
                           V_VRNFRELACAF := C_VRITEMAF.VRITEMAF;
                        END IF;
                     END IF;
                  END IF;
               END LOOP;

               UPDATE ITEMPRODUTO
                  SET IDAVISOSITEM = SUBSTR(IDAVISOSITEM,1,3)|| w_1 || SUBSTR(IDAVISOSITEM,5,2)
                WHERE (CDFILIAL = P_CDFILIAL)                   AND
                      (NRLANCTONF  = P_NRLANCTONF)              AND
                      (NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD) AND
                      ((ROUND((VRUNITITEM - (VRDESCITEM/QTITEMPROD)),2) > (ROUND(V_VRNFRELACAF,2) + ((ROUND(V_VRNFRELACAF,2) * V_VRPTOLVRITAF)/100))) OR
                      ((ROUND((VRUNITITEM - (VRDESCITEM/QTITEMPROD)),2) < (ROUND(V_VRNFRELACAF,2) - ((ROUND(V_VRNFRELACAF,2) * V_VRPTOLVRITAF)/100))) AND (V_IDCONSVLRMENAF = 'N'))) AND
                      (V_IDCONVRUNIAF <> w_N);
            END IF;          

            /* (AVISO) QUANTIDADE DE PRODUTO INFORMADA DIFERE DA QUANTIDADE RECEBIDA */
            SELECT SUM(QTENTRMERC)
            INTO V_QTENTRMERC
            FROM NFRELACAF N, ITEMAUTF I,
                (SELECT R.CDFILDEST, R.CDFILLANC, R.NRAUTOFORN, E.CDMARCA,
                         E.CDPRODUTO, R.DTPROGENTRE, E.QTENTRMERC
                  FROM NFRELACAF NF, RELACENTRAF R, ITEMENTREGA E
                  WHERE NF.CDFILIAL = P_CDFILIAL       AND
                        NF.NRLANCTONF = P_NRLANCTONF   AND
                        NF.CDFILDEST  = R.CDFILDEST    AND
                        NF.CDFILLANC  = R.CDFILLANC    AND
                        NF.NRAUTOFORN = R.NRAUTOFORN   AND
                        R.CDFILIAL    = E.CDFILIAL     AND
                        R.NRENTREGA   = E.NRENTREGA) EN

            WHERE (N.CDFILIAL    = P_CDFILIAL)                AND
                  (N.NRLANCTONF  = P_NRLANCTONF)              AND
                  (N.CDFILDEST   = I.CDFILDEST)               AND
                  (N.CDFILLANC   = I.CDFILLANC)               AND
                  (N.NRAUTOFORN  = I.NRAUTOFORN)              AND
                  (N.CDMARCA     = I.CDMARCA)                 AND
                  (N.CDPRODUTO   = I.CDPRODUTO)               AND
                  (N.DTPROGENTAF = I.DTPROGENTAF)             AND
                  (EN.CDFILDEST(+)   = I.CDFILDEST)           AND
                  (EN.CDFILLANC(+)   = I.CDFILLANC)           AND
                  (EN.NRAUTOFORN(+)  = I.NRAUTOFORN)          AND
                  (EN.CDMARCA(+)     = I.CDMARCA)             AND
                  (EN.CDPRODUTO(+)   = I.CDPRODUTO)           AND
                  (EN.DTPROGENTRE(+)  = I.DTPROGENTAF)         AND
                  (N.CDPRODUTO   = C_ITEMPRODUTO.CDPRODUTO)   AND
                  (N.CDMARCA     = C_ITEMPRODUTO.CDMARCA);

            SELECT SUM(QTITEMPROD)
            INTO V_QTITEMPROD
            FROM ITEMPRODUTO
            WHERE (CDFILIAL   = P_CDFILIAL)              AND
                  (NRLANCTONF = P_NRLANCTONF)            AND
                  (CDPRODUTO  = C_ITEMPRODUTO.CDPRODUTO) AND
                  (CDMARCA    = C_ITEMPRODUTO.CDMARCA);

            UPDATE ITEMPRODUTO
            SET IDAVISOSITEM = SUBSTR(IDAVISOSITEM,1,4) || w_1 || SUBSTR(IDAVISOSITEM,1,1)
            WHERE (CDFILIAL    = P_CDFILIAL)                AND
                  (NRLANCTONF  = P_NRLANCTONF)              AND
                  (NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD) AND
                  ((V_QTITEMPROD > (V_QTENTRMERC + ((ROUND(V_QTENTRMERC,3) * V_VRPTOLQTITAF)/100)))) AND
                  (V_IDCONQTDEREC <> w_N);

            V_DIFPROD := w_S;

            FOR C_PRODUTO  IN C_PRODAUTF LOOP
               IF C_ITEMPRODUTO.CDPRODESTO = C_PRODUTO.CDPRODESTO THEN
                  V_DIFPROD := w_N;
               END IF;
            END LOOP;

            UPDATE ITEMPRODUTO
            SET IDAVISOSITEM   = SUBSTR(IDAVISOSITEM,1,5) || w_1
            WHERE (CDFILIAL    = P_CDFILIAL)                AND
                  (NRLANCTONF  = P_NRLANCTONF)              AND
                  (NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD) AND
                  (V_IDPRODNFAF <> w_N)                     AND
                  (V_DIFPROD   = w_S);

         END LOOP;
      END IF;

      /* VERIFICAR SE A NOTA TEM AVISO */
      UPDATE NOTAFISCAL
      SET IDAVISOS = w_1|| SUBSTR(IDAVISOS,2,4)
      WHERE CDFILIAL   = P_CDFILIAL    AND
            NRLANCTONF = P_NRLANCTONF  AND
            NRLANCTONF IN (SELECT MAX(NRLANCTONF) AS NRLANCTONF
                           FROM ITEMPRODUTO
                           WHERE CDFILIAL   = P_CDFILIAL    AND
                                 NRLANCTONF = P_NRLANCTONF  AND
                                 INSTR(IDAVISOSITEM,w_1) > 0);

      UPDATE NOTAFISCAL
      SET IDAVISOS = w_1|| SUBSTR(IDAVISOS,2,4)
      WHERE CDFILIAL   = P_CDFILIAL    AND
            NRLANCTONF = P_NRLANCTONF  AND
            NRLANCTONF IN (SELECT MAX(NRLANCIMPOIT)
                           FROM TRABITPROIMPOS
                           WHERE CDFILIMPOIT  = P_CDFILIAL    AND
                                 NRLANCIMPOIT = P_NRLANCTONF  AND
                                 INSTR(IDAVISIMPOIT,w_1) > 0);

      UPDATE NOTAFISCAL
         SET IDAVISOS = w_1|| SUBSTR(IDAVISOS,2,4)
       WHERE CDFILIAL   = P_CDFILIAL    AND
             NRLANCTONF = P_NRLANCTONF  AND
             NRLANCTONF IN (SELECT MAX(NRLANCIMPTOT)
                              FROM TRABIMPOSNFTOT
                             WHERE CDFILIMPTOT  = P_CDFILIAL    AND
                                   NRLANCIMPTOT = P_NRLANCTONF  AND
                                   INSTR(IDAVISOSTOT,w_1) > 0);

      /* VERIFICAR SE A NOTA TEM ERRO */
      UPDATE NOTAFISCAL
         SET IDERROS = w_1 || SUBSTR(IDERROS,2,3)
       WHERE CDFILIAL   = P_CDFILIAL    AND
             NRLANCTONF = P_NRLANCTONF  AND
             NRLANCTONF IN (SELECT MAX(NRLANCTONF) AS NRLANCTONF
                              FROM ITEMPRODUTO
                             WHERE CDFILIAL   = P_CDFILIAL    AND
                                   NRLANCTONF = P_NRLANCTONF  AND
                                   INSTR(IDERROSITEM,w_1) > 0);

       UPDATE NOTAFISCAL
         SET IDERROS = w_1 || SUBSTR(IDERROS,2,3)
       WHERE CDFILIAL   = P_CDFILIAL    AND
             NRLANCTONF = P_NRLANCTONF  AND
             NRLANCTONF IN (SELECT MAX(NRLANCIMPOIT)
                              FROM TRABITPROIMPOS
                             WHERE CDFILIMPOIT  = P_CDFILIAL    AND
                                   NRLANCIMPOIT = P_NRLANCTONF  AND
                                   INSTR(IDERRIMPOIT,w_1) > 0);

       UPDATE NOTAFISCAL
          SET IDERROS = w_1 || SUBSTR(IDERROS,2,3)
        WHERE CDFILIAL   = P_CDFILIAL    AND
              NRLANCTONF = P_NRLANCTONF  AND
              NRLANCTONF IN (SELECT MAX(NRLANCIMPTOT)
                               FROM TRABIMPOSNFTOT
                              WHERE CDFILIMPTOT  = P_CDFILIAL    AND
                                    NRLANCIMPTOT = P_NRLANCTONF  AND
                                    INSTR(IDERROSTOT,w_1) > 0);
   END IF;
END;
END;
/