                  SELECT NVL(MAX(VRIMPOSTOIT),0)
                    INTO V_VRIMPORELAC
                    FROM ITEMPROIMPOS
                   WHERE CDFILIAL     = P_FILIAL
                     AND NRLANCTONF   = P_LANCTONF
                     AND NRSEQITPROD  = C_ITEMPRODUTO.NRSEQITPROD
                     AND CDIMPOSTO    = C_INCIDENCIA.CDIMPOBASERET
                     AND NRITPROIMPOS = W_1;
                  IF V_VRIMPORELAC <> 0 THEN
                     V_LIQUIITEM := V_VRIMPORELAC;
                  END IF;
               ELSE
                  V_LIQUIITEM := C_ITEMPRODUTO.VRTOTAITEM - C_ITEMPRODUTO.VRDESCITEM;   
               END IF;
               V_ALIQUTMP := 0;
               V_SIMPLES  := 'N';
               V_UTIBB    := 'N';
               V_VRADICALIQFECP := C_INCIDENCIA.VRADICALIQFECP;
               V_CDBENEFFISCAL := NULL;
--integração fiscal especifica com Compass_Argentina atraves da importação de aliquota do imposto IBB
--A aliquota de imposto 03(ibb) sera cadastrada na tabela paramintegraibb e tera prioridade sobre o
-- cadastro de aliquota realizado em sistema
               IF C_INCIDENCIA.CDIMPOTEMP = '03' THEN
                  IF V_IDORIGDESTNF = 'C' THEN
                     V_ALIQUTMP := 0;
                     V_DTVINGENTE := NVL(V_DTINIREFEFAT, P_INIVALID);
                     SELECT NVL(MAX(VRALIQUOTAIBB),NULL)
                     INTO   V_ALIQUTMP
                     FROM PARAMINTEGRAIBB
                     WHERE CDCLIENTE = P_CLIENTE AND
                           V_DTVINGENTE BETWEEN DTVIGINICIALIBB AND DTVIGFINALIBB;
                     IF V_ALIQUTMP IS NOT NULL THEN
                        V_NRALIQTEMP   := 0;
                        V_PARDEDTMP    := 0;
                        V_PREDBCTMP    := 0;
                        V_TRATIMPST    := w_0;
                        V_PBCSUBTRIB   := 0;
                        V_IDPMCBASECAL := 'N';
                        V_UTIBB        := 'S';
                     END IF;
                  END IF;
               END IF;
               /*VERIFICA CST POR IMPOSTO*/
               V_IMPOICMS := NULL;
               V_CDGRUPOFISCAUX := NULL;
               SELECT MAX(G.CDGRUPOFISC)
                 INTO V_CDGRUPOFISCAUX
                 FROM GRUPOFISCIMPFIL G,
                      (SELECT CDFILIEMIT AS CDFILIAL
                         FROM NOTAFISCAL
                        WHERE CDFILIAL = P_FILIAL
                              AND NRLANCTONF = P_LANCTONF) F
                WHERE G.CDFILIAL = F.CDFILIAL
                  AND NOT G.CDGRUPOFISC IS NULL
                  AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
               IF V_IDORIGDESTNF = 'C' THEN
                  SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                    INTO V_CDGRUPOFISCAUX
                    FROM GRFISIMPIECLIE G,
                         (SELECT CDCLIENTE
                            FROM NOTAFISCAL
                           WHERE CDFILIAL = P_FILIAL
                             AND NRLANCTONF = P_LANCTONF) N
                   WHERE G.CDCLIENTE = N.CDCLIENTE
                     AND G.CDEMPRESA = P_EMPRESA
                     AND G.CDINSCESTA = P_INSCESTA
                     AND NOT G.CDGRUPOFISC IS NULL
                     AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
               ELSE
                  SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                    INTO V_CDGRUPOFISCAUX
                    FROM GRFISIMPIEFORN G,
                        (SELECT CDFORNECED
                           FROM NOTAFISCAL
                          WHERE CDFILIAL = P_FILIAL
                            AND NRLANCTONF = P_LANCTONF) N
                   WHERE G.CDFORNECED = N.CDFORNECED
                     AND G.CDEMPRESA = P_EMPRESA
                     AND G.CDINSCESTA = P_INSCESTA
                     AND NOT G.CDGRUPOFISC IS NULL
                     AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
               END IF;
               IF V_CDGRUPOFISCAUX IS NULL THEN
                  CONSULTA_CST_NFE(w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISC,C_INCIDENCIA.CDIMPOTEMP, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, C_ITEMPRODUTO.IDORIGEMPROD);
               ELSE
                  CONSULTA_CST_NFE(w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISCAUX,C_INCIDENCIA.CDIMPOTEMP, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, C_ITEMPRODUTO.IDORIGEMPROD);
               END IF;
               V_CDSIMPLESNACIONAL := NULL;
               V_CDFAIXA := NULL;
               V_BCICMSSTRETIDO := 0;
               V_VRICMSSTRETIDO := 0;
               V_CDREGRAITALIQ  := NULL;
               /*SIMPLES NACIONAL*/
               IF (V_CDSITUCRT <> '3') THEN
                  CALCULA_ALIQUOTA_SN(P_EMPRESA, 
                                      P_TIPOOPER, 
                                      P_INIVALID,
                                      SUBSTR(V_FISOPEITEM, 2, 3), 
                                      C_INCIDENCIA.CDIMPOSTONFE, 
                                      V_VRSUBLIMITESN,
                                      'N',
                                      V_RBT12,
                                      V_RBA,
                                      V_CDSIMPLESNACIONAL,
                                      V_CDFAIXA,
                                      V_VRALIQSIMPLES);
                  IF (V_CDFAIXA IS NOT NULL) OR ((C_INCIDENCIA.CDIMPOSTONFE IN ('00', '31')) AND (V_RBT12 >= 3600000)) THEN
                     UPDATE ITEMPRODUTO
                        SET CDSIMPLESNACIONAL = V_CDSIMPLESNACIONAL
                      WHERE CDFILIAL    = P_FILIAL
                        AND NRLANCTONF  = P_LANCTONF
                        AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
                     V_ALIQUTMP      := V_VRALIQSIMPLES;
                     V_CDREGRAITALIQ := C_INCIDENCIA.CDREGRATMP;
                     V_NRALIQTEMP    := 0;
                     V_PARDEDTMP     := 0;
                     V_PREDBCTMP     := 0;
                     V_TRATIMPST     := w_0;
                     V_PBCSUBTRIB    := 0;
                     V_IDPMCBASECAL  := 'N';
                     V_SIMPLES       := 'S';
                  END IF;
                  IF (V_CDFISCOPERSN = '500') OR (SUBSTR(V_CDFISCOPERSN,2, 3) = '500') THEN
                     V_CDIMPOSICMS  := null;
                     SELECT MAX(CDIMPOSTO)
                       INTO V_CDIMPOSICMS
                       FROM IMPOSTO
                      WHERE CDIMPOSTONFE = '00'
                        AND CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
                     IF V_CDIMPOSICMS IS NOT NULL THEN
                        V_ALIQUTMP := 0;
                     END IF;
                     /*Verifica se o imposto de st (hj o sistema só aceita o icms st)*/
                     /*IMPOSTO DE SUBSTITUICAO TRIBUTARIA*/
                     V_CDIMPOSTRIB  := null;
                     V_IDVRIMPSTRIB := null;
                     FOR C_ICMSST IN (SELECT CDIMPOSTRIB
                                        FROM IMPOSTO
                                       WHERE CDIMPOSTRIB = C_INCIDENCIA.CDIMPOTEMP)
                     LOOP
                        V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTRIB||'_';
                     END LOOP;
                     V_BCICMSSTRETIDO := 0;
                     V_VRICMSSTRETIDO := 0;
                     IF V_CDIMPOSTRIB IS NOT NULL THEN
                        IF V_IDUTILLOTE = 'S' THEN
                           SELECT MAX(IDUTCTRLLOTE)
                             INTO V_IDUTCTRLLOTE
                             FROM PRODFILI
                            WHERE CDFILIAL  = V_CDFILIEMIT
                              AND CDPRODUTO = C_ITEMPRODUTO.CDPRODUTO;
                           IF V_IDUTCTRLLOTE = 'S' THEN
                              SELECT MAX(NRLOTEESTQ)
                                INTO V_NRLOTEESTQ
                                FROM ITEMPRODUTO
                               WHERE CDFILIAL    = P_FILIAL
                                 AND NRLANCTONF  = P_LANCTONF
                                 AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
                              V_MEDIA := 0;
                              V_BCICMSSTRETIDO := 0;
                              V_VRICMSSTRETIDO := 0;
                              FOR C_ICMSSTRETIDO IN (SELECT II.CDFILIAL, II.NRLANCTONF, II.NRSEQITPROD, II.CDIMPOSTO,
                                                            ROUND(II.VRBASECALCIT/I.QTITEMPROD,2) AS VRBASE, ROUND(II.VRIMPOSTOIT/I.QTITEMPROD,2) AS VRIMPOSTO
                                                       FROM NOTAFISCAL N, ITEMPRODUTO I, ITEMPROIMPOS II
                                                      WHERE N.CDFILIAL = I.CDFILIAL
                                                        AND N.NRLANCTONF = I.NRLANCTONF
                                                        AND I.CDFILIAL = II.CDFILIAL
                                                        AND I.NRLANCTONF = II.NRLANCTONF
                                                        AND I.NRSEQITPROD = II.NRSEQITPROD
                                                        AND II.NRITPROIMPOS = '1'
                                                        AND N.IDENTRSAIDOP = 'E'
                                                        AND I.CDPRODUTO = C_ITEMPRODUTO.CDPRODUTO
                                                        AND I.NRLOTEESTQ = V_NRLOTEESTQ
                                                        AND N.DTENTRSAID <= P_INIVALID
                                                        AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0))
                              LOOP
                                  V_MEDIA := V_MEDIA + 1;
                                  V_BCICMSSTRETIDO := V_BCICMSSTRETIDO + C_ICMSSTRETIDO.VRBASE;
                                  V_VRICMSSTRETIDO := V_VRICMSSTRETIDO + C_ICMSSTRETIDO.VRIMPOSTO;
                              END LOOP;
                              IF V_MEDIA > 0 THEN
                                 V_BCICMSSTRETIDO := ROUND(V_BCICMSSTRETIDO/V_MEDIA,2);
                                 V_VRICMSSTRETIDO := ROUND(V_VRICMSSTRETIDO/V_MEDIA,2);
                              ELSE
                                 V_BCICMSSTRETIDO := 0;
                                 V_VRICMSSTRETIDO := 0;
                              END IF;
                              V_BCICMSSTRETIDO := ROUND(V_BCICMSSTRETIDO * C_ITEMPRODUTO.QTITEMPROD,2);
                              V_VRICMSSTRETIDO := ROUND(V_VRICMSSTRETIDO * C_ITEMPRODUTO.QTITEMPROD,2);
                              IF V_BCICMSSTRETIDO <> 0 THEN
                                 SELECT COUNT(*)
                                   INTO V_EXISTDSDADOSADIC
                                   FROM DADOSADIC
                                  WHERE CDFILIAL = P_FILIAL
                                    AND NRLANCTONF = P_LANCTONF
                                    AND DSDADOADIC LIKE 'ICMS retido anteriormente por substituição tributária: BC %';
                                 IF V_EXISTDSDADOSADIC = 0 THEN
                                    /*PREENCHE AS MENSAGENS ATRELADAS A INSCRICAO ESTADUAL*/
                                    INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                                                   VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, 'ICMS retido anteriormente por substituição tributária: BC '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')))|| ' Valor '|| DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99'))));
                                    V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
                                 ELSE
                                    UPDATE DADOSADIC
                                       SET DSDADOADIC = 'ICMS retido anteriormente por substituição tributária: BC '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')))|| ' Valor '|| DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')))
                                      WHERE CDFILIAL = P_FILIAL
                                        AND NRLANCTONF = P_LANCTONF
                                        AND DSDADOADIC LIKE 'ICMS retido anteriormente por substituição tributária: BC %';
                                 END IF;
                              END IF;
                           END IF;
                        ELSE
                           V_MEDIA := 0;
                           V_BCICMSSTRETIDO := 0;
                           V_VRICMSSTRETIDO := 0;
                           SELECT MAX(N.DTENTRSAID), MAX(II.CDIMPOSTO), MAX(I.CDPRODUTO),
                                  ROUND(SUM(II.VRBASECALCIT)/SUM(I.QTITEMPROD),2) AS VRBASE, ROUND(SUM(II.VRIMPOSTOIT)/SUM(I.QTITEMPROD),2) AS VRIMPOSTO
                             INTO V_DTENTRSAID, V_CDIMPOSTOI, V_CDPRODUTO, V_BCICMSSTRETIDO, V_VRICMSSTRETIDO
                             FROM NOTAFISCAL N, ITEMPRODUTO I, ITEMPROIMPOS II
                            WHERE N.CDFILIAL = I.CDFILIAL
                              AND N.NRLANCTONF = I.NRLANCTONF
                              AND I.CDFILIAL = II.CDFILIAL
                              AND I.NRLANCTONF = II.NRLANCTONF
                              AND I.NRSEQITPROD = II.NRSEQITPROD
                              AND II.NRITPROIMPOS = '1'
                              AND N.IDENTRSAIDOP = 'E'
                              AND I.CDPRODUTO = C_ITEMPRODUTO.CDPRODUTO
                              AND N.DTENTRSAID <= P_INIVALID
                              AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0);
                           V_BCICMSSTRETIDO := ROUND(V_BCICMSSTRETIDO * C_ITEMPRODUTO.QTITEMPROD,2);
                           V_VRICMSSTRETIDO := ROUND(V_VRICMSSTRETIDO * C_ITEMPRODUTO.QTITEMPROD,2);
                           IF V_BCICMSSTRETIDO <> 0 THEN
                              SELECT COUNT(*)
                                INTO V_EXISTDSDADOSADIC
                                FROM DADOSADIC
                               WHERE CDFILIAL = P_FILIAL
                                 AND NRLANCTONF = P_LANCTONF
                                 AND DSDADOADIC LIKE 'ICMS retido anteriormente por substituição tributária: BC %';
                              IF V_EXISTDSDADOSADIC = 0 THEN
                                 /*PREENCHE AS MENSAGENS ATRELADAS A INSCRICAO ESTADUAL*/
                                 INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                                                VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, 'ICMS retido anteriormente por substituição tributária: BC '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')))|| ' Valor '|| DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99'))));
                                 V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
                              ELSE
                                 UPDATE DADOSADIC
                                    SET DSDADOADIC = 'ICMS retido anteriormente por substituição tributária: BC '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_BCICMSSTRETIDO, 2), '9999999999999.99')))|| ' Valor '|| DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRICMSSTRETIDO, 2), '9999999999999.99')))
                                  WHERE CDFILIAL = P_FILIAL
                                    AND NRLANCTONF = P_LANCTONF
                                    AND DSDADOADIC LIKE 'ICMS retido anteriormente por substituição tributária: BC %';
                              END IF;
                           END IF;
                        END IF;
                     END IF;
                  END IF;
               END IF;
               IF (V_SIMPLES = 'N') OR ((V_SIMPLES = 'S') AND (C_INCIDENCIA.CDIMPOSTONFE = '00') AND (V_RBA > (V_VRSUBLIMITESN * 1.2))) THEN
                  DELETE ALIQTEMP
                   WHERE NRALIQTEMP = V_ALIQTEMP;
                  V_CDSIMPLESNACIONAL := NULL;
                  V_CDFAIXA := NULL;
                  V_VRADICALIQFECP := NULL;
                  IF (V_UTIBB = 'N')  THEN
                     IF C_INCIDENCIA.IDUTMUNIORIG = w_D THEN
                        SELECT MAX(IDGRUPFISSRV)
                        INTO V_UTGRUPFISCS
                        FROM IMPOSTO
                        WHERE CDIMPOSTO   = C_INCIDENCIA.CDIMPOTEMP;
                        IF V_UTGRUPFISCS = 'S' THEN
                           V_CDGRPFISCSV := NULL;
                           SELECT MAX(CDGRUPOFISC)
                           INTO V_CDGRPFISCSV
                           FROM SERVCLIE SC, SERVICO S
                           WHERE SC.CDFILIAL   = V_CDFILIEMIT AND
                                 SC.CDCLIENTE  = P_CLIENTE    AND
                                 SC.CDFILIAL   = S.CDFILIAL   AND
                                 SC.CDSERVFILI = S.CDSERVFILI AND
                                 S.CDPRODUTO   = C_ITEMPRODUTO.CDPRODUTO;
                           IF V_CDGRPFISCSV IS NULL THEN
                              SELECT MAX(CDGRUPOFISC)
                              INTO V_CDGRPFISCSV
                              FROM CLIENFILIAL
                             WHERE CDFILIAL   = V_CDFILIEMIT AND
                                   CDCLIENTE  = P_CLIENTE ;
                           END IF;
                           CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISORIG,
                                             V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRPFISCSV,P_TIPOOPER,
                                             NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                        ELSE
                            /* CONSIDERA O MUNICIPIO DE DESTINO */
                            CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISORIG,
                                              V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                              NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                        END IF;
                     ELSE
                       /* CONSIDERA O MUNICIPIO DE ORIGEM */
                        SELECT MAX(IDGRUPFISSRV)
                        INTO V_UTGRUPFISCS
                        FROM IMPOSTO
                        WHERE CDIMPOSTO   = C_INCIDENCIA.CDIMPOTEMP;
                        IF V_UTGRUPFISCS = 'S' THEN
                           V_CDGRPFISCSV := NULL;
                           SELECT MAX(CDGRUPOFISC)
                           INTO V_CDGRPFISCSV
                           FROM SERVCLIE SC, SERVICO S
                           WHERE SC.CDFILIAL   = V_CDFILIEMIT AND
                                 SC.CDCLIENTE  = P_CLIENTE    AND
                                 SC.CDFILIAL   = S.CDFILIAL   AND
                                 SC.CDSERVFILI = S.CDSERVFILI AND
                                 S.CDPRODUTO   = C_ITEMPRODUTO.CDPRODUTO;
                           IF V_CDGRPFISCSV IS NULL THEN
                              SELECT MAX(CDGRUPOFISC)
                              INTO V_CDGRPFISCSV
                              FROM CLIENFILIAL
                             WHERE CDFILIAL   = V_CDFILIEMIT AND
                                   CDCLIENTE  = P_CLIENTE ;
                           END IF;
                           CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISORIG,
                                             V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRPFISCSV, P_TIPOOPER,
                                             NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                        ELSE
                            CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISORIG,
                                              V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                              NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                        END IF;
                     END IF;
                     V_NRALIQTEMP      := 0;
                     V_PARDEDTMP       := 0;
                     V_PREDBCTMP       := 0;
                     V_ALIQUTMP        := 0;
                     V_IMPOSTOIT       := 0;
                     V_VRADICALIQFECP  := NULL;
                     V_IDCONSMVAPMC    := '3';
                     V_IDCONSCOMPBCPMC := '3';
                     V_IDIMPODESONERA  := w_N;
                     V_IDCONSDESONVRNF := w_N;
                     SELECT NVL(MAX(NRALIQTEMP),0),      NVL(MAX(VRPARDEDTMP),0),     NVL(MAX(VRPREDBCTMP),0),        NVL(MAX(VRALIQUTMP),0),
                            NVL(MAX(IDTRIMPSTTMP), w_0), NVL(MAX(VRPBCSTTMP),0),      NVL(MAX(IDPMCBSCALTMP), w_S),   MAX(CDREGRATMP),
                            MAX(CDFISCOPER),             MAX(IDDIFEALIQ),             MAX(IDDIFALIQCUSPR),            MAX(VRADICALIQFECP),
                            MAX(CDBENEFFISCAL),          NVL(MAX(IDCONSMVAPMC), '3'), NVL(MAX(IDCONSCOMPBCPMC), '3'), NVL(MAX(IDCONSDIFALIQ), 'N'),
                            MAX(IDIMPODESONERA),         MAX(IDCONSDESONVRNF)
                     INTO   V_NRALIQTEMP,                V_PARDEDTMP,                 V_PREDBCTMP,                    V_ALIQUTMP,
                            V_TRATIMPST,                 V_PBCSUBTRIB,                V_IDPMCBASECAL,                 V_CDREGRAITALIQ,
                            V_CDFISCOPERREGRA,           V_IDDIFEALIQREGRA,           V_IDDIFALIQCUSPRREGRA,          V_VRADICALIQFECP,
                            V_CDBENEFFISCAL,             V_IDCONSMVAPMC,              V_IDCONSCOMPBCPMC,              V_IDCONSDIFALIQ,
                            V_IDIMPODESONERA,            V_IDCONSDESONVRNF
                     FROM ALIQTEMP
                     WHERE (NRALIQTEMP = V_ALIQTEMP);
                     V_ICMS := 'N';
                     FOR C_ICMS IN (SELECT CDIMPOSTO
                                      FROM IMPOSTONFE N, IMPOSTO I
                                     WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                       AND N.CDIMPOSTONFE = '00'
                                       AND I.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP)
                     LOOP
                        V_ICMS := 'S';
                     END LOOP;
                     IF V_ICMS = 'S' THEN
                        IF (V_CDFISCOPERREGRA IS NOT NULL) THEN
                           V_CDFISCOPERREGRA   := V_CFOP1DIGT||V_CDFISCOPERREGRA;
                           V_DSCDFISOPEITREGRA := SUBSTR(DESCRICAO_CFOP(V_CDFISCOPERREGRA),1,45);
                           IF V_CFOPINFUSU <> 'S' THEN
                              V_FISOPEITEM := V_CDFISCOPERREGRA;
                           END IF;
                        END IF;
                        IF V_CFOPINFUSU <> 'S' THEN
                           IF (V_DSCDFISOPEITREGRA IS NOT NULL) THEN
                              UPDATE NOTAFISCAL
                                 SET DSCDFISOPENF = V_DSCDFISOPEITREGRA
                               WHERE CDFILIAL   = P_FILIAL   AND
                                     NRLANCTONF = P_LANCTONF;
                           ELSE
                              UPDATE NOTAFISCAL
                                 SET DSCDFISOPENF = (SELECT MAX(DSCFOPTMP)
                                                       FROM CFOPTEMP
                                                      WHERE NRCFOPTEMP = V_CFOPTEMP)
                               WHERE CDFILIAL   = P_FILIAL
                                 AND NRLANCTONF = P_LANCTONF;
                           END IF;
                        END IF;
                        UPDATE ITEMPRODUTO
                           SET CDFISOPEITEM  = V_FISOPEITEM,
                               CDSUFXCFOPPR  = V_SUFXCFOPPR,
                               CDSITUTRIBPR  = V_SITUTRIBPR,
                               DSCDFISOPEIT  = V_DSCDFISOPEIT,
                               CDFISCOPERSN  = V_CDFISCOPERSN
                         WHERE CDFILIAL    = P_FILIAL AND
                               NRLANCTONF  = P_LANCTONF AND
                               NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
                     END IF;
                  END IF;
               END IF;
               V_CDIMPOSTRIB  := null;
               V_IDVRIMPSTRIB := null;
               /*IMPOSTO DE SUBSTITUICAO TRIBUTARIA*/
               FOR C_ICMSST IN (SELECT CDIMPOSTO
                                      FROM IMPOSTO
                                     WHERE CDIMPOSTRIB = C_INCIDENCIA.CDIMPOTEMP)
               LOOP
                  V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
               END LOOP;
               -- UTILIZA O PRÓPRIO VALOR DO IMPOSTO NA SUA BASE DE CÁLCULO (IMPOSTO POR DENTRO)
               BEGIN
                  SELECT NVL(MAX(IDUTILPVRIMPBC), 'N')
                    INTO V_IDUTILPVRIMPBC
                    FROM IMPOSTO
                   WHERE CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_IDUTILPVRIMPBC := 'N';
               END;
               V_TAXABASECALC := (100 - V_PREDBCTMP)/100;
               V_TAXAALIQUOTA := (V_ALIQUTMP/100);
               V_TAXABCSUBSTR := 1 + (V_PBCSUBTRIB/100);
               V_BASECALCIT   := V_LIQUIITEM * V_TAXABASECALC;
               -- EM MG O CÁLCULO DA BASE É REALIZADO DE UMA FORMA DIFERENTE DE ACORDO COM A 
               -- ORIENTAÇÃO TRIBUTÁRIA DOLT/SUTRI Nº 003/2016. PARA ESTE PROCESSO É NECESSÁRIO
               -- CALCULAR O IMPOSTO POR DENTRO (ELE COMPONDO A BASE DE CÁLCULO DELE MESMO)
               IF (V_IDCALCDIFALDENT = 'S') THEN
                  IF ((V_PAISDEST = '0055') AND (UPPER(V_ESTADEST) = 'MG')) AND ((V_PAISORIG = '0055') AND (UPPER(V_ESTAORIG) = 'MG')) THEN
                     IF (V_IDCONSFINAL = 'S') THEN
                        IF (V_VRADICALIQFECP IS NOT NULL) AND (V_VRADICALIQFECP > 0) THEN
                           IF (V_IDUTILPVRIMPBC = 'S') THEN
                              IF C_INCIDENCIA.CDIMPOSTONFE = '00' THEN
                                 V_BASECALCIT := V_BASECALCIT/((100-V_ALIQUTMP)/100);
                              END IF;
                           END IF;
                        END IF;
                    END IF;
                  ELSIF ((V_PAISORIG = '0055') AND (UPPER(V_ESTAORIG) = 'MG')) THEN
                     V_ALIQICMSDEST := 0;
                     V_ALIQFECPDEST := 0;
                     IF (V_IDINTEREST = 'S') AND (V_IDCONSFINAL = 'S') AND (V_IDCONTRIBICMSAUX = 'N') AND (V_IDPRESENCIAL <> '1') THEN
                        IF C_INCIDENCIA.CDIMPOSTONFE = '00' THEN
                           -- CONSULTANDO A ALÍQUOTA INTERNA DO IMPOSTO
                           DELETE 
                             FROM ALIQTEMP
                            WHERE NRALIQTEMP = V_ALIQTEMP;
                           IF C_INCIDENCIA.IDUTMUNIORIG = w_D THEN
                              CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                                V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRPFISCSV, P_TIPOOPER,
                                                NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                           ELSE
                              CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                                V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                                NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                           END IF;
                           BEGIN
                              SELECT MAX(VRALIQUTMP)
                                INTO V_ALIQICMSDEST
                                FROM ALIQTEMP
                               WHERE NRALIQTEMP = V_ALIQTEMP;
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_ALIQICMSDEST := NULL;
                           END;
                           IF V_ALIQICMSDEST IS NULL THEN
                              BEGIN
                                 SELECT NVL(CONSULTA_QUADRO_ALIQUOTAS(V_ESTADEST, V_ESTADEST), 0)
                                   INTO V_ALIQICMSDEST
                                   FROM DUAL;
                              EXCEPTION
                                 WHEN NO_DATA_FOUND THEN
                                    V_ALIQICMSDEST := 0;
                              END;
                           END IF;
                            -- CONSULTANDO A ALÍQUOTA DO FCP (FUNDO DE COMBATE A POBREZA)
                           BEGIN
                              SELECT CDIMPOSTO
                                INTO V_CDIMPOSTOFECP
                                FROM IMPOSTO
                               WHERE CDIMPOSTONFE = '01';
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_CDIMPOSTOFECP := NULL;
                           END;
                           IF V_CDIMPOSTOFECP IS NOT NULL THEN
                              DELETE
                                FROM ALIQTEMP
                               WHERE NRALIQTEMP = V_ALIQTEMP;
                              --VERIFICANDO SE EXISTE A INCIDÊNCIA DE FECP PARA O PRODUTO
                              IF C_INCIDENCIA.IDUTMUNIORIG = w_D THEN
                                 CONSULTA_ALIQUOTA(V_CDIMPOSTOFECP, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                                   V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRPFISCSV, P_TIPOOPER,
                                                   NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                              ELSE
                                 CONSULTA_ALIQUOTA(V_CDIMPOSTOFECP, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                                   V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                                   NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                              END IF;
                              BEGIN
                                 SELECT NVL(MAX(VRALIQUTMP), 0)
                                   INTO V_ALIQFECPDEST
                                   FROM ALIQTEMP
                                  WHERE NRALIQTEMP = V_ALIQTEMP;
                              EXCEPTION
                                 WHEN NO_DATA_FOUND THEN
                                    V_ALIQFECPDEST := 0;
                              END;
                           ELSE
                              V_ALIQFECPDEST := 0;
                           END IF;                        
                           -- REALIZANDO O CÁLCULO DA BASE COM O IMPOSTO POR DENTRO
                           -- BASE DE CÁLCULO REAL = BASE DE CÁLCULO / (1 - ((ALIQUOTA DO IMPOSTO + ALIQUOTA DO FCP) / 100)
                           V_BASECALCIT := V_BASECALCIT/((100-(V_ALIQICMSDEST+V_ALIQFECPDEST))/100);
                        END IF;
                     END IF;
                  END IF;
               END IF;
               IF V_CDIMPOSTRIB IS NULL THEN
                  V_IDTRIMPSTII := '';
                  V_PERDBCALC := 100 - V_PREDBCTMP;
                  IF V_PBCSUBTRIB > 0 THEN
                     V_BASECALCIT := (V_BASECALCIT * V_TAXABCSUBSTR);
                     V_PREDBCTMP  := V_PBCSUBTRIB;
                     V_PERDBCALC  := 0;
                  END IF;
                  IF (C_INCIDENCIA.IDUTVLRQTDEIMPO = 'S') THEN
                     SELECT NVL(MAX(VRFATOCONVES), 1)
                       INTO V_VRFATOCONVES
                       FROM CONVPRODUTO
                      WHERE CDPRODUTO = C_ITEMPRODUTO.CDPRODUTO;
                     V_BASECALCTO  := (C_ITEMPRODUTO.QTITEMPROD * V_VRFATOCONVES);
                     V_IMPOSTOIT   := ((C_ITEMPRODUTO.QTITEMPROD * V_VRFATOCONVES) * V_ALIQUTMP) - V_PARDEDTMP;
                  ELSE
                     V_BASECALCTO  := V_BASECALCIT;
                     V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
                  END IF;
               ELSE
                  IF SUBSTR(V_SITUTRIBPR, 2, 2) = '60' THEN
                     V_IDTRIMPSTII := w_2;
                  ELSE 
                     V_IDTRIMPSTII := V_TRATIMPST;
                  END IF;
                  IF NVL(V_IDPMCBASECAL, W_N) = W_S THEN
                     CALCULA_BC_PMC(P_FILIAL,
                                    P_LANCTONF,
                                    C_ITEMPRODUTO.NRSEQITPROD,
                                    C_INCIDENCIA.CDIMPOTEMP,
                                    V_IDCONSMVAPMC,
                                    V_IDCONSCOMPBCPMC,
                                    V_PAISDEST,
                                    V_ESTADEST,
                                    C_ITEMPRODUTO.CDPRODUTO,
                                    C_ITEMPRODUTO.QTITEMPROD,
                                    C_ITEMPRODUTO.VRUNITITEM,
                                    C_ITEMPRODUTO.VRDESCITEM,
                                    V_PBCSUBTRIB,
                                    V_PREDBCTMP,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL,
                                    'N',
                                    V_BASECALCTO);
                     V_IMPOSTOIT := (V_BASECALCTO * V_TAXAALIQUOTA) - V_PARDEDTMP;
                  ELSIF (V_IDCONSDIFALIQ = 'S') AND (C_INCIDENCIA.CDIMPOSTONFE = '80') THEN
                     CONSIDERA_DIFAL_ICMS(P_FILIAL,
                                          P_LANCTONF,
                                          C_ITEMPRODUTO.NRSEQITPROD,
                                          V_CDGRPFISCSV,
                                          V_PAISDEST,
                                          V_ESTADEST,
                                          V_MUNIDEST,
                                          V_PBCSUBTRIB,
                                          V_PREDBCTMP,
                                          V_BASECALCTO,
                                          V_IMPOSTOIT,
                                          V_VRADICFECP);
                  ELSE
                     UPDATE ITEMPRODUTO
                        SET IDUTILBCPMC = 'N'
                      WHERE CDFILIAL = P_FILIAL
                        AND NRLANCTONF = P_LANCTONF
                        AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
                     V_PERDBCALC := 100 - V_PREDBCTMP;
                     IF V_PBCSUBTRIB > 0 THEN
                        V_BASECALCIT := (V_BASECALCIT * V_TAXABCSUBSTR);
                        V_PREDBCTMP  := V_PBCSUBTRIB;
                        V_PERDBCALC  := 0;
                     END IF;
                     V_BASECALCTO  := V_BASECALCIT;
                     V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
                  END IF;
               END IF; 
               V_CONTABCALC  := V_LIQUIITEM - (V_LIQUIITEM * V_TAXABASECALC);
               IF (V_IMPOSTOIT < 0) THEN
                  V_IMPOSTOIT := 0;
               END IF;
               IF (V_IDCONSDIFALIQ = 'N') THEN
                  V_VRADICFECPST := 0;
                  V_VRADICFECPSTAUX := 0;
                  IF (V_VRADICALIQFECP IS NOT NULL) AND (V_VRADICALIQFECP > 0) THEN
                     V_VRADICFECP := ROUND(V_BASECALCTO*(V_VRADICALIQFECP/100), 2);
                  END IF;
                  IF (V_CDIMPOSTRIB IS NOT NULL) THEN
                     V_VRIMPOSST   := 0;
                     V_VRIMPOSTOST := 0;
                     FOR C_SUBSTRIB IN (SELECT NVL(IDVRIMPSTRIB, 'N') IDVRIMPSTRIB, I.CDIMPOSTO
                                          FROM ITEMPROIMPOS I, IMPOSTO II
                                         WHERE (CDFILIAL     = P_FILIAL)
                                           AND (NRLANCTONF   = P_LANCTONF)
                                           AND (NRSEQITPROD  = C_ITEMPRODUTO.NRSEQITPROD)
                                           AND (NRITPROIMPOS = '1')
                                           AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0)
                                           AND (I.CDIMPOSTO = II.CDIMPOSTO))
                     LOOP
                        IF (C_SUBSTRIB.IDVRIMPSTRIB = w_S) THEN
                           Begin
                              SELECT NVL(ROUND(MAX(VRIMPOSTOIT), 2),0),
                                     NVL(ROUND(MAX(VRADICFECP), 2),0)
                              INTO   V_VRIMPOSST,
                                     V_VRADICFECPSTAUX
                              FROM ITEMPROIMPOS I
                              WHERE (CDFILIAL     = P_FILIAL)                   AND
                                    (NRLANCTONF   = P_LANCTONF)                 AND
                                    (NRSEQITPROD  = C_ITEMPRODUTO.NRSEQITPROD)  AND
                                    (CDIMPOSTO    = C_SUBSTRIB.CDIMPOSTO)       AND
                                    (INSTR(V_CDIMPOSTRIB,CDIMPOSTO||'_') > 0)   AND
                                    (NRITPROIMPOS = w_1);
                           Exception
                              When No_Data_Found Then
                                 V_VRIMPOSST := 0;
                                 V_VRADICFECPSTAUX := 0;
                           End;
                        ELSE
                           V_VRIMPOSST := 0;
                           V_VRADICFECPSTAUX := 0;
                        END IF;
                        V_VRIMPOSTOST := V_VRIMPOSTOST + V_VRIMPOSST;
                        V_VRADICFECPST := V_VRADICFECPST + V_VRADICFECPSTAUX;
                     END LOOP;
                     V_IMPOSTOIT := V_IMPOSTOIT - V_VRIMPOSTOST;
                     IF V_IMPOSTOIT < 0 THEN
                        V_IMPOSTOIT := 0;
                     END IF;
                  END IF;
                  IF (V_VRADICALIQFECP IS NOT NULL) AND (V_VRADICALIQFECP > 0) THEN
                     V_VRADICFECP := V_VRADICFECP - V_VRADICFECPST;
                  END IF;
               END IF;
               /* Rotina para calcular o ICMS ST considerando % carga tributária média */
               V_IDIMPSTCARGTRIB := 'N';
               IF V_CDIMPOSTRIB IS NOT NULL THEN
                  BEGIN
                     SELECT NVL(VRCNAEBC, 0),
                            NVL(VRMVA, 0)
                       INTO V_VRPRODUTOBC,
                            V_VRMVA
                       FROM VRPERCCARGTRIBCNAE
                      WHERE IDENTRSAIDOP = w_S
                        AND CDTIPOOPER   = P_TIPOOPER
                        AND CDPAIS       = V_PAISDEST
                        AND SGESTADO     = V_ESTADEST
                        AND CDCNAE       = V_CDCNAECLIE
                        AND IDOPTANTESN  = V_IDOPTANTESNCLIE;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_VRPRODUTOBC := 0;
                        V_VRMVA := 0;
                  END;
                  IF V_VRPRODUTOBC = 0 THEN
                     BEGIN
                        SELECT NVL(VRPRODUTOBC, 0)
                          INTO V_VRPRODUTOBC
                          FROM VRPERCCARGTRIB
                         WHERE IDENTRSAIDOP = w_S
                           AND CDTIPOOPER   = P_TIPOOPER
                           AND CDPAIS       = V_PAISDEST
                           AND SGESTADO     = V_ESTADEST
                           AND CDPRODUTO    = C_ITEMPRODUTO.CDPRODUTO;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_VRPRODUTOBC := 0;
                     END;
                  END IF;
                  IF (V_VRPRODUTOBC > 0) THEN
                     BEGIN
                        SELECT I.CDIMPOSTO, I.VRIMPOSTOIT
                          INTO V_CDIMPOSTOICMS, V_VRIMPOSTOICMS
                          FROM IMPOSTONFE N, IMPOSTO P, ITEMPROIMPOS I
                         WHERE N.CDIMPOSTONFE = P.CDIMPOSTONFE
                           AND N.CDIMPOSTONFE = '00'
                           AND I.CDFILIAL     = P_FILIAL
                           AND I.NRLANCTONF   = P_LANCTONF
                           AND I.NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD
                           AND I.CDIMPOSTO = NVL(P.CDIMPOSREGNORMA, P.CDIMPOSTO)
                           AND NOT P.CDIMPOSTRIB IS NULL
                           AND I.NRITPROIMPOS = '1';
                     EXCEPTION
                        When No_Data_Found Then
                           V_CDIMPOSTOICMS := NULL; V_VRIMPOSTOICMS := 0;
                     END;
                     DELETE ALIQTEMP
                      WHERE NRALIQTEMP = V_ALIQTEMP;
                     /* Verifica grupo fiscal */
                     SELECT MAX(IDGRUPFISSRV)
                     INTO V_UTGRUPFISCS
                     FROM IMPOSTO
                     WHERE CDIMPOSTO = V_CDIMPOSTOICMS;
                     IF V_UTGRUPFISCS = 'S' THEN
                        V_CDGRPFISCSV := NULL;
                        SELECT MAX(CDGRUPOFISC)
                        INTO V_CDGRPFISCSV
                        FROM SERVCLIE SC, SERVICO S
                        WHERE SC.CDFILIAL   = V_CDFILIEMIT AND
                              SC.CDCLIENTE  = P_CLIENTE    AND
                              SC.CDFILIAL   = S.CDFILIAL   AND
                              SC.CDSERVFILI = S.CDSERVFILI AND
                              S.CDPRODUTO   = C_ITEMPRODUTO.CDPRODUTO;
                        IF V_CDGRPFISCSV IS NULL THEN
                           SELECT MAX(CDGRUPOFISC)
                           INTO V_CDGRPFISCSV
                           FROM CLIENFILIAL
                          WHERE CDFILIAL   = V_CDFILIEMIT AND
                                CDCLIENTE  = P_CLIENTE;
                        END IF;
                        CONSULTA_ALIQUOTA(V_CDIMPOSTOICMS, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                          V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRPFISCSV, P_TIPOOPER,
                                          NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                     ELSE
                        CONSULTA_ALIQUOTA(V_CDIMPOSTOICMS, w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                          V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                          NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPRODUTO.IDORIGEMPROD);
                     END IF;
                     SELECT NVL(MAX(VRALIQUTMP),0)
                       INTO V_VRALIQUTMPDEST
                       FROM ALIQTEMP
                      WHERE (NRALIQTEMP = V_ALIQTEMP);
                     IF (V_VRALIQUTMPDEST <> 0) THEN
                        V_IMPOSTOIT  :=  ROUND(V_BASECALCIT * (V_VRPRODUTOBC/100), 2);
                        V_BASECALCTO :=  ROUND(((V_VRIMPOSTOICMS + V_IMPOSTOIT) / (V_VRALIQUTMPDEST/100)),5);  
                        V_IDIMPSTCARGTRIB := 'S';
                     END IF;
                     DELETE ALIQTEMP
                      WHERE NRALIQTEMP = V_ALIQTEMP;
                  END IF;
               END IF;
               /* ROTINA PARA CALCULAR O DIFERIMENTO DO IMPOSTO */
               V_PERDIFERE    := NULL;
               V_IMPOSTOOPERA := NULL;
               V_IMPOSTODIFER := NULL;
               IF C_INCIDENCIA.CDIMPOSTONFE = '00' THEN
                  IF V_CDSITTRIBTMP = 51 THEN
                     BEGIN
                        SELECT PD.VRPERDIFERE, 'S' 
                          INTO V_VRPERDIFERE, V_IDDIFECLI
                          FROM PRODDIFEREIMPCLI PD, PRODUTO PR
                         WHERE PD.CDPRODUTO = PR.CDPRODUTO
                           AND PD.CDPAIS = V_PAISORIG
                           AND PD.SGESTADO = V_ESTAORIG
                           AND PD.CDCLIENTE = P_CLIENTE
                           AND PR.CDARVPROD IN (SELECT MAX(PR.CDARVPROD) AS CDARVPROD
                                                  FROM PRODDIFEREIMPCLI PD, PRODUTO PR
                                                 WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                                   AND PD.CDPAIS = V_PAISORIG
                                                   AND PD.SGESTADO = V_ESTAORIG
                                                   AND PD.CDCLIENTE = P_CLIENTE
                                                   AND INSTR(NVL(RTRIM(C_ITEMPRODUTO.CDARVPROD), PR.CDARVPROD), PR.CDARVPROD) = 1);
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_VRPERDIFERE := NULL; V_IDDIFECLI := 'N';
                     END;
                     IF V_IDDIFECLI = 'N' THEN
                        BEGIN
                           SELECT PD.VRPERDIFERE
                             INTO V_VRPERDIFERE
                             FROM PRODDIFEREIMPOS PD, PRODUTO PR
                            WHERE PD.CDPRODUTO = PR.CDPRODUTO
                              AND PD.CDPAIS = V_PAISORIG
                              AND PD.SGESTADO = V_ESTAORIG
                              AND PR.CDARVPROD IN (SELECT MAX(PR.CDARVPROD) AS CDARVPROD
                                                     FROM PRODDIFEREIMPOS PD, PRODUTO PR
                                                    WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                                      AND PD.CDPAIS = V_PAISORIG
                                                      AND PD.SGESTADO = V_ESTAORIG
                                                      AND INSTR(NVL(RTRIM(C_ITEMPRODUTO.CDARVPROD), PR.CDARVPROD), PR.CDARVPROD) = 1);
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_VRPERDIFERE := NULL;
                        END;
                     END IF;
                     IF V_VRPERDIFERE IS NOT NULL THEN
                        V_PERDIFERE    := ROUND(V_VRPERDIFERE, 2);
                        V_IMPOSTOOPERA := ROUND(V_IMPOSTOIT, 2);
                        /* 
                           Foi comentado este cálculo pois ao implementá-lo de acordo com a resolução nº13 de 14 de fevereiro de 2019
                           a sefaz passou a rejeitar as notas com a rejeição 352 (Rejeicao: Valor do ICMS Diferido no CST=51 difere do 
                           produto Valor ICMS Operacao e percentual diferimento)
                        */
                        --IF (V_CDPAISEMIT = 0055) AND (V_SGESTADOEMIT = RJ) THEN
                        --   V_IMPOSTODIFER := ROUND((((V_BASECALCTO / (1 - V_TAXAALIQUOTA)) * V_TAXAALIQUOTA) * (V_VRPERDIFERE/100)), 2);
                        --ELSE
                           V_IMPOSTODIFER := ROUND((V_IMPOSTOOPERA * (V_VRPERDIFERE/100)), 2);
                        --END IF;
                        V_IMPOSTOIT := ROUND(V_IMPOSTOOPERA, 2) - ROUND(V_IMPOSTODIFER, 2);
                     END IF;
                  END IF;
               END IF;
               V_BASECALCIRRF := V_BASECALCTO;
               IF (V_ALIQUTMP = 0) AND (V_IMPOSTOIT = 0) THEN
                  V_BASECALCTO := 0;
               END IF;
               SELECT NVL(MAX(IDPERVISIMPSCRE),'S')
                 INTO V_IDPERVISIMPSCRE
                 FROM PARAMGERAL;
               IF V_IDPERVISIMPSCRE = 'S' THEN
                  V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
               ELSE
                  IF (C_INCIDENCIA.CDFISCTEMP = '1') THEN
                     V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
                  ELSE
                     V_VRIMPOSTOVISUIT := 0;
                  END IF;
               END IF;
               V_ICMS := 'N';
               FOR C_ICMS IN (SELECT CDIMPOSTO
                                FROM IMPOSTONFE N, IMPOSTO I
                               WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                 AND N.CDIMPOSTONFE = '00'
                                 AND I.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP)
               LOOP
                  V_ICMS := 'S';
               END LOOP;
               Begin
                  SELECT CDFILIAL
                  INTO   V_CHAVE
                  FROM ITEMPROIMPOS
                  WHERE CDFILIAL     = P_FILIAL                 AND
                        NRLANCTONF   = P_LANCTONF               AND
                        NRSEQITPROD  = C_ITEMPRODUTO.NRSEQITPROD  AND
                        CDIMPOSTO    = C_INCIDENCIA.CDIMPOTEMP AND
                        NRITPROIMPOS = w_1;
               Exception
                  When No_Data_Found Then
                     V_CHAVE := Null;
               End;
               IF V_CHAVE IS NULL THEN
                  IF (V_ICMS = 'S') THEN
                     IF C_ITEMPRODUTO.CDCFOPINFUSU IS NULL THEN
                        V_CDGRUPOFISCAUX := NULL;
                        SELECT MAX(G.CDGRUPOFISC)
                          INTO V_CDGRUPOFISCAUX
                          FROM GRUPOFISCIMPFIL G,
                               (SELECT CDFILIEMIT AS CDFILIAL
                                  FROM NOTAFISCAL
                                 WHERE CDFILIAL = P_FILIAL
                                       AND NRLANCTONF = P_LANCTONF) F
                         WHERE G.CDFILIAL = F.CDFILIAL
                           AND NOT G.CDGRUPOFISC IS NULL
                           AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
                        IF V_IDORIGDESTNF = 'C' THEN
                           SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                             INTO V_CDGRUPOFISCAUX
                             FROM GRFISIMPIECLIE G,
                                  (SELECT CDCLIENTE
                                     FROM NOTAFISCAL
                                    WHERE CDFILIAL = P_FILIAL
                                      AND NRLANCTONF = P_LANCTONF) N
                            WHERE G.CDCLIENTE = N.CDCLIENTE
                              AND G.CDEMPRESA = P_EMPRESA
                              AND G.CDINSCESTA = P_INSCESTA
                              AND NOT G.CDGRUPOFISC IS NULL
                              AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
                        ELSE
                           SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                             INTO V_CDGRUPOFISCAUX
                             FROM GRFISIMPIEFORN G,
                                 (SELECT CDFORNECED
                                    FROM NOTAFISCAL
                                   WHERE CDFILIAL = P_FILIAL
                                     AND NRLANCTONF = P_LANCTONF) N
                            WHERE G.CDFORNECED = N.CDFORNECED
                              AND G.CDEMPRESA = P_EMPRESA
                              AND G.CDINSCESTA = P_INSCESTA
                              AND NOT G.CDGRUPOFISC IS NULL
                              AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
                        END IF;
                        IF (V_CDGRUPOFISCAUX IS NOT NULL) THEN
                           CONSULTA_CFOP(w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_PAISORIG,
                                         V_ESTAORIG, V_PAISCFOP, V_ESTACFOP, P_FILIAL, P_LANCTONF, V_CDGRUPOFISCAUX, P_INIVALID, C_ITEMPRODUTO.IDORIGEMPROD);
                        ELSE
                           CONSULTA_CFOP(w_S, P_EMPRESA, C_ITEMPRODUTO.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_PAISORIG,
                                         V_ESTAORIG, V_PAISCFOP, V_ESTACFOP, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_INIVALID, C_ITEMPRODUTO.IDORIGEMPROD);
                        END IF;
                        IF (V_CDFISCOPERREGRA IS NOT NULL) THEN
                           IF (LENGTH(V_CDFISCOPERREGRA) < 4) THEN
                              V_CDFISCOPERREGRA := V_CFOP1DIGT||V_CDFISCOPERREGRA;
                           END IF;
                           V_DSCDFISOPEITREGRA := SUBSTR(DESCRICAO_CFOP(V_CDFISCOPERREGRA),1,45);
                        END IF;
                        IF (V_DSCDFISOPEITREGRA IS NOT NULL) THEN
                           UPDATE NOTAFISCAL
                              SET DSCDFISOPENF = V_DSCDFISOPEITREGRA
                            WHERE CDFILIAL   = P_FILIAL   AND
                                  NRLANCTONF = P_LANCTONF;
                        ELSE
                           UPDATE NOTAFISCAL
                              SET DSCDFISOPENF = (SELECT MAX(DSCFOPTMP)
                                                    FROM CFOPTEMP
                                                   WHERE NRCFOPTEMP = V_CFOPTEMP)
                            WHERE CDFILIAL   = P_FILIAL
                              AND NRLANCTONF = P_LANCTONF;
                        END IF;
                        SELECT MAX(CDFISOPETMP), MAX(CDSUFCFOPTMP), SUBSTR(MAX(DSCFOPTMP),1,50)
                          INTO V_FISOPEITEM    , V_SUFXCFOPPR, V_DSCDFISOPEIT
                          FROM CFOPTEMP
                         WHERE NRCFOPTEMP = V_CFOPTEMP;
                        UPDATE ITEMPRODUTO
                           SET CDFISOPEITEM  = NVL(V_CDFISCOPERREGRA, V_FISOPEITEM),
                               CDSUFXCFOPPR  = V_SUFXCFOPPR,
                               DSCDFISOPEIT  = NVL(V_DSCDFISOPEITREGRA, V_DSCDFISOPEIT)
                         WHERE CDFILIAL    = P_FILIAL AND
                               NRLANCTONF  = P_LANCTONF AND
                               NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
                        DELETE CFOPTEMP
                         WHERE NRCFOPTEMP = V_CFOPTEMP;
                     END IF;
                     Begin
                        SELECT MAX(CDCSTDIG1PROD)
                          INTO V_CST1DIGT
                          FROM PRODUTO
                         WHERE (CDARVPROD = C_ITEMPRODUTO.CDARVPROD);
                        IF V_CST1DIGT IS NULL THEN
                           SELECT MAX(CDCSTDIG1SAI)
                             INTO   V_CST1DIGT
                             FROM INSCRESTAD
                            WHERE (CDEMPRESA  = P_EMPRESA) AND
                                  (CDINSCESTA = P_INSCESTA);
                        END IF;
                     Exception
                        When No_Data_Found Then
                           V_CST1DIGT := Null;
                     End;
                     IF (V_IDUTORIGPROD = 'S') AND (C_ITEMPRODUTO.IDORIGEMPROD = 'E') THEN
                        V_CST1DIGT := '2';
                     END IF;
                     UPDATE ITEMPRODUTO
                        SET CDSITUTRIBPR  = V_CST1DIGT||NVL(V_CDSITTRIBTMP, SUBSTR(V_SITUTRIBPR, 2,2))
                      WHERE CDFILIAL    = P_FILIAL AND
                            NRLANCTONF  = P_LANCTONF AND
                            NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
                     V_SITUTRIBPR := V_CST1DIGT||NVL(V_CDSITTRIBTMP, SUBSTR(V_SITUTRIBPR, 2,2));
                  END IF;
                  IF (NVL(V_CFOPINFUSU, 'N') <> 'S') AND (V_CDFISCOPERREGRA IS NOT NULL) THEN
                     V_FISOPEITEM := NVL(V_CDFISCOPERREGRA, V_FISOPEITEM);
                     V_DSCDFISOPEITREGRA := SUBSTR(DESCRICAO_CFOP(V_CDFISCOPERREGRA),1,45);
                  END IF;
                  /*IF (V_VRADICALIQFECP IS NOT NULL) AND (V_VRADICALIQFECP > 0) THEN
                     V_VRADICFECP := ROUND(V_BASECALCTO*(V_VRADICALIQFECP/100), 2);
                  END IF;*/
                  INSERT INTO ITEMPROIMPOS(
                     CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS, CDFISCALIT,
                     CDSITUTRIBIT, CDFISCOPERIT, CDSUFXCFOPIT, VRALIQITEM, VRPERREDBCIT, VRPARDEDIT,
                     VRCONTITIMP, VRBASECALCIT, VRIMPOSTOIT, IDERROSIMPIT, IDAVISIMPIT, IDTRIMPSTII,
                     CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                     CDSIMPLESNACIONAL, CDFAIXA, VRBCICMSSTRETIDO, VRICMSSTRETIDO, CDREGRAIT, CDDETALHEPROD,
                     VRIMPOSTOVISUIT, VRBCALCIRRF, VRPERDIFEREIT, VRIMPOSTOOPERAIT, VRIMPOSTODIFERIT,
                     CDENQLEGALIPI, CDCEST, IDIMPSTCARGTRIB, VRADICALIQFECP, VRADICFECP,
                     VRALIQINTEDEST, VRALIQFCP, CDBENEFFISCAL, IDCONSDIFALIQ, IDDESONERAREGRA, IDCONSDESONVRNF)
                  VALUES(
                     P_FILIAL, P_LANCTONF, C_ITEMPRODUTO.NRSEQITPROD, C_INCIDENCIA.CDIMPOTEMP, w_1, C_INCIDENCIA.CDFISCTEMP,
                     V_SITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, V_ALIQUTMP, V_PREDBCTMP, V_PARDEDTMP,
                     ROUND(V_BASECALCTO,5), ROUND(V_BASECALCTO,2), V_IMPOSTOIT, w_00000, w_00000, V_IDTRIMPSTII,   
                     V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE,
                     V_CDSIMPLESNACIONAL, V_CDFAIXA, V_BCICMSSTRETIDO, V_VRICMSSTRETIDO, V_CDREGRAITALIQ, C_INCIDENCIA.CDDETALHEPROD,
                     V_VRIMPOSTOVISUIT, V_BASECALCIRRF, V_PERDIFERE, V_IMPOSTOOPERA, V_IMPOSTODIFER,
                     C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST, V_IDIMPSTCARGTRIB, V_VRADICALIQFECP, V_VRADICFECP, 
                     V_ALIQICMSDEST, V_ALIQFECPDEST, V_CDBENEFFISCAL, NVL(V_IDCONSDIFALIQ, 'N'), V_IDIMPODESONERA, V_IDCONSDESONVRNF);
                  IF (V_PREDBCTMP > 0) AND (V_PBCSUBTRIB = 0) THEN
                     IF V_SITUTRIBPR = '060' THEN
                        INSERT INTO ITEMPROIMPOS(
                           CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS, CDFISCALIT,
                           CDSITUTRIBIT, CDFISCOPERIT, CDSUFXCFOPIT, VRALIQITEM, VRPERREDBCIT, VRPARDEDIT,
                           VRCONTITIMP, VRBASECALCIT, VRIMPOSTOIT, IDERROSIMPIT, IDAVISIMPIT, CDSITUTRIBITNFE,
                           IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, CDSIMPLESNACIONAL,
                           CDFAIXA, VRBCICMSSTRETIDO, VRICMSSTRETIDO, CDREGRAIT, CDDETALHEPROD,
                           VRIMPOSTOVISUIT, CDENQLEGALIPI, CDCEST, IDIMPSTCARGTRIB)
                        VALUES(
                           P_FILIAL, P_LANCTONF, C_ITEMPRODUTO.NRSEQITPROD, C_INCIDENCIA.CDIMPOTEMP, w_2, w_2,
                           V_SITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, 0, V_PERDBCALC, V_PARDEDTMP,
                           V_CONTABCALC, 0, 0, w_00000, w_00000, V_CDSITTRIBTMP,
                           V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, V_CDSIMPLESNACIONAL,
                           V_CDFAIXA, V_BCICMSSTRETIDO, V_VRICMSSTRETIDO, V_CDREGRAITALIQ, C_INCIDENCIA.CDDETALHEPROD,
                           V_VRIMPOSTOVISUIT, C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST, V_IDIMPSTCARGTRIB);
                     ELSE
                        INSERT INTO ITEMPROIMPOS(
                           CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS, CDFISCALIT,
                           CDSITUTRIBIT, CDFISCOPERIT, CDSUFXCFOPIT, VRALIQITEM, VRPERREDBCIT, VRPARDEDIT,
                           VRCONTITIMP, VRBASECALCIT, VRIMPOSTOIT, IDERROSIMPIT, IDAVISIMPIT, CDSITUTRIBITNFE,
                           IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, CDSIMPLESNACIONAL,
                           CDFAIXA, VRBCICMSSTRETIDO, VRICMSSTRETIDO, CDREGRAIT, CDDETALHEPROD,
                           VRIMPOSTOVISUIT, CDENQLEGALIPI, CDCEST, IDIMPSTCARGTRIB)
                        VALUES(
                           P_FILIAL, P_LANCTONF, C_ITEMPRODUTO.NRSEQITPROD, C_INCIDENCIA.CDIMPOTEMP, w_2, w_2,
                           V_SITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, 0, V_PERDBCALC, V_PARDEDTMP,
                           V_CONTABCALC, 0, 0, w_00000, w_00000, V_CDSITTRIBTMP,
                           V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, V_CDSIMPLESNACIONAL,
                           V_CDFAIXA, V_BCICMSSTRETIDO, V_VRICMSSTRETIDO, V_CDREGRAITALIQ, C_INCIDENCIA.CDDETALHEPROD,
                           V_VRIMPOSTOVISUIT, C_INCIDENCIA.CDENQLEGALIPI, C_INCIDENCIA.CDCEST, V_IDIMPSTCARGTRIB);
                     END IF;
                  END IF;
               END IF;
               FOR C_ALIQTEMP IN (SELECT DSMENSALITMP
                                  FROM ALIQTEMP
                                  WHERE (NRALIQTEMP = V_ALIQTEMP)  AND
                                        (DSMENSALITMP IS NOT NULL) AND
                                        (NOT EXISTS(SELECT DSDADOADIC
                                                    FROM DADOSADIC
                                                    WHERE (CDFILIAL   = P_FILIAL)   AND
                                                          (NRLANCTONF = P_LANCTONF) AND
                                                          (DSDADOADIC LIKE ALIQTEMP.DSMENSALITMP || '%')))) LOOP
                  IF NOT(C_ALIQTEMP.DSMENSALITMP IS NULL) THEN
                     /*PREENCHE AS MENSAGENS ATRELADAS A ALIQUOTA*/
                     INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                                    VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, C_ALIQTEMP.DSMENSALITMP);
                     V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
                  END IF;
               END LOOP;
               DELETE ALIQTEMP
                WHERE NRALIQTEMP = V_ALIQTEMP;
               V_VRADICALIQFECP := NULL;
               V_VRADICFECP := NULL;
            END LOOP; --FIM DO LOOP DAS INCIDÊNCIAS
            DELETE INCITEMP
            WHERE NRINCITEMP = V_INCITEMP;
         ELSE
            /*IMPOSTOS INFORMADOS PELO USUARIO*/
            FOR C_IMPOSINFUSU IN (SELECT II.CDIMPOSTO, II.CDFISCINFUSU,
                                         II.VRALIQINFUSU, II.VRRDBCINFUSU, II.VRPDEDINFUSU,
                                         I.IDTRATIMPO, I.IDCONSISTENT, I.IDIMPORECUP
                                  FROM IMPOSINFUSU II, IMPOSTO I
                                  WHERE (II.CDFILIAL    = P_FILIAL)                  AND
                                        (II.NRLANCTONF  = P_LANCTONF)                AND
                                        (II.NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD) AND
                                        (II.CDIMPOSTO   = I.CDIMPOSTO)) LOOP
               V_TAXABASECALC := (100 - C_IMPOSINFUSU.VRRDBCINFUSU)/100;
               V_TAXAALIQUOTA := (C_IMPOSINFUSU.VRALIQINFUSU/100);
               V_IMPOSTOIT  := ((V_LIQUIITEM * V_TAXABASECALC) * V_TAXAALIQUOTA) - C_IMPOSINFUSU.VRPDEDINFUSU;
               V_CONTABCALC := V_LIQUIITEM - (V_LIQUIITEM * V_TAXABASECALC);
               V_PERDBCALC  := 100 - C_IMPOSINFUSU.VRRDBCINFUSU;
               IF (V_IMPOSTOIT < 0) THEN
                  V_IMPOSTOIT := 0;
               END IF;
               IF (C_IMPOSINFUSU.VRALIQINFUSU = 0) AND (V_IMPOSTOIT = 0) THEN
                  V_BASECALCIT := 0;
               ELSE
                  V_BASECALCIT := V_LIQUIITEM * V_TAXABASECALC;
               END IF;
               Begin
                  SELECT CDFILIAL
                  INTO   V_CHAVE
                  FROM ITEMPROIMPOS
                  WHERE CDFILIAL     = P_FILIAL                 AND
                        NRLANCTONF   = P_LANCTONF               AND
                        NRSEQITPROD  = C_ITEMPRODUTO.NRSEQITPROD  AND
                        CDIMPOSTO    = C_IMPOSINFUSU.CDIMPOSTO AND
                        NRITPROIMPOS = w_1;
               Exception
                  When No_Data_Found Then
                     V_CHAVE := Null;
               End;
               SELECT NVL(MAX(IDPERVISIMPSCRE),'S')
                 INTO V_IDPERVISIMPSCRE
                 FROM PARAMGERAL;
               IF V_IDPERVISIMPSCRE = 'S' THEN
                  V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
               ELSE
                  IF (C_IMPOSINFUSU.CDFISCINFUSU = '1') THEN
                     V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
                  ELSE
                     V_VRIMPOSTOVISUIT := 0;
                  END IF;
               END IF;
               IF V_CHAVE IS NULL THEN
                  INSERT INTO ITEMPROIMPOS(
                     CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS, CDFISCALIT, CDSITUTRIBIT,
                     CDFISCOPERIT, CDSUFXCFOPIT, VRALIQITEM, VRPERREDBCIT, VRPARDEDIT, VRCONTITIMP,
                     VRBASECALCIT, VRIMPOSTOIT, IDERROSIMPIT, IDAVISIMPIT, VRIMPOSTOVISUIT)
                  VALUES(
                     P_FILIAL, P_LANCTONF, C_ITEMPRODUTO.NRSEQITPROD, C_IMPOSINFUSU.CDIMPOSTO, w_1, C_IMPOSINFUSU.CDFISCINFUSU, V_SITUTRIBPR,
                     V_FISOPEITEM, V_SUFXCFOPPR, C_IMPOSINFUSU.VRALIQINFUSU, C_IMPOSINFUSU.VRRDBCINFUSU, C_IMPOSINFUSU.VRPDEDINFUSU, V_BASECALCIT,
                     V_BASECALCIT, V_IMPOSTOIT, w_00000, w_00000, V_VRIMPOSTOVISUIT);
                  IF V_PREDBCTMP > 0 THEN
                     INSERT INTO ITEMPROIMPOS(
                        CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO, NRITPROIMPOS, CDFISCALIT, CDSITUTRIBIT,
                        CDFISCOPERIT, CDSUFXCFOPIT, VRALIQITEM, VRPERREDBCIT, VRPARDEDIT, VRCONTITIMP,
                        VRBASECALCIT, VRIMPOSTOIT, IDERROSIMPIT, IDAVISIMPIT, VRIMPOSTOVISUIT)
                     VALUES(
                        P_FILIAL, P_LANCTONF, C_ITEMPRODUTO.NRSEQITPROD, C_IMPOSINFUSU.CDIMPOSTO, w_2, w_2, V_SITUTRIBPR,
                        V_FISOPEITEM, V_SUFXCFOPPR, 0, V_PERDBCALC, C_IMPOSINFUSU.VRPDEDINFUSU, ROUND(V_CONTABCALC,5),  
                        0, 0, w_00000, w_00000, 0);
                  END IF;
               END IF;
            END LOOP; --FIM DO LOOP DOS IMPOSTOS INFORMADOS PELO USUARIO
         END IF;
      END LOOP; --FIM DO LOOP DOS PRODUTOS
      DELETE FROM REGRANOTA
      WHERE CDFILIAL = P_FILIAL
        AND NRLANCTONF = P_LANCTONF;
      FOR C_IMPOSNOTA IN (SELECT DISTINCT CDREGRAIT
                            FROM ITEMPROIMPOS
                           WHERE CDFILIAL = P_FILIAL
                             AND NRLANCTONF = P_LANCTONF
                             AND CDREGRAIT IS NOT NULL
                          ORDER BY CDREGRAIT)
      LOOP
         FOR C_REGRAMSG IN (SELECT CDREGRAFISC, DSMENSREGRAFISC
                              FROM TABLE(MSG_REGRAFISCAL(C_IMPOSNOTA.CDREGRAIT, P_EMPRESA, 'S', P_TIPOOPER, P_INSCESTA)) M
                             WHERE NOT EXISTS(SELECT DSDADOADIC
                                             FROM DADOSADIC
                                             WHERE (CDFILIAL   = P_FILIAL)   AND
                                                   (NRLANCTONF = P_LANCTONF) AND
                                                   (DSDADOADIC = M.DSMENSREGRAFISC))) LOOP
            INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                           VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, C_REGRAMSG.DSMENSREGRAFISC);
            V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
         END LOOP;
         INSERT INTO REGRANOTA(CDFILIAL, NRLANCTONF, CDREGRAFISC)
                           VALUES(P_FILIAL, P_LANCTONF, C_IMPOSNOTA.CDREGRAIT);
      END LOOP;
      --RATEIO DOS ENCARGOS--------------------------------------------------------------------------------------------------------
      FOR C_ITEMENCARGO IN (SELECT IE.CDENCARGO AS CDENCARGO, VRITEMENCA, IDENCARGO,
                                   EN.CDCTBLENTREN, EN.CDCTBLSAIDEN
                            FROM ITEMENCARGO IE, ENCARGO EN
                            WHERE (IE.CDFILIAL   = P_FILIAL)     AND
                                  (IE.NRLANCTONF = P_LANCTONF)   AND
                                  (IE.VRITEMENCA > 0)            AND
                                  (EN.CDENCARGO  = IE.CDENCARGO)) LOOP
         V_TOTAPRODENCA := 0;
         FOR C_VRLIQRATEIO IN (SELECT DISTINCT(IP.NRSEQITPROD) AS NRSEQITPROD, (VRTOTAITEM - VRDESCITEM) AS VRLIQUIDO
                               FROM ITEMPRODUTO IP, ITEMPROIMPOS II
                               WHERE (IP.CDFILIAL    = P_FILIAL)       AND
                                     (IP.NRLANCTONF  = P_LANCTONF)     AND
                                     (II.CDFILIAL    = IP.CDFILIAL)    AND
                                     (II.NRLANCTONF  = IP.NRLANCTONF)  AND
                                     (II.NRSEQITPROD = IP.NRSEQITPROD) AND
                                     (EXISTS(SELECT BC.CDIMPOSTO
                                             FROM BASECALC BC
                                             WHERE (BC.CDENCARGO = C_ITEMENCARGO.CDENCARGO) AND
                                                   (BC.CDIMPOSTO = II.CDIMPOSTO)            AND
                                                   (BC.CDIMPOSTO IN (SELECT B.CDIMPOSTO
                                                                    FROM BASECALC B
                                                                    WHERE B.CDENCARGO = C_ITEMENCARGO.CDENCARGO))))) LOOP
            IF NOT (C_VRLIQRATEIO.VRLIQUIDO IS NULL) THEN
               V_TOTAPRODENCA := V_TOTAPRODENCA + C_VRLIQRATEIO.VRLIQUIDO;
            END IF;
         END LOOP;
         IF V_TOTAPRODENCA = 0 THEN
            SELECT SUM(VRTOTAITEM - VRDESCITEM)
            INTO   V_TOTAPRODENCA
            FROM ITEMPRODUTO IP
            WHERE (IP.CDFILIAL   = P_FILIAL)   AND
                  (IP.NRLANCTONF = P_LANCTONF);
            IF NOT V_TOTAPRODENCA IS NULL THEN
               FOR C_ATUAVRCONT IN (SELECT IP.NRSEQITPROD, (VRTOTAITEM - VRDESCITEM) AS VRLIQUIDO
                                    FROM ITEMPRODUTO IP
                                    WHERE (IP.CDFILIAL    = P_FILIAL) AND
                                          (IP.NRLANCTONF  = P_LANCTONF)) LOOP
                  IF (C_ITEMENCARGO.IDENCARGO = w_A) THEN
                     V_VRBRUTO  := (C_ITEMENCARGO.VRITEMENCA/V_TOTAPRODENCA);
                  ELSIF (C_ITEMENCARGO.IDENCARGO = w_D) THEN
                     V_VRBRUTO  := - (C_ITEMENCARGO.VRITEMENCA/V_TOTAPRODENCA);
                  ELSE
                     V_VRBRUTO := 0;
                  END IF;
                  IF (C_ITEMENCARGO.CDCTBLSAIDEN IS NULL) THEN
                     V_VRCONTAB := V_VRBRUTO;
                  ELSE
                     V_VRCONTAB := 0;
                  END IF;
                  IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                     UPDATE ITEMPRODUTO
                     SET VRBRUTITEM = TRUNC(VRBRUTITEM + (V_VRBRUTO  * (VRTOTAITEM - VRDESCITEM)),2),
                         VRCONTITEM = TRUNC(VRCONTITEM + (V_VRCONTAB * (VRTOTAITEM - VRDESCITEM)),2)
                     WHERE CDFILIAL    = P_FILIAL   AND
                           NRLANCTONF  = P_LANCTONF AND
                           NRSEQITPROD = C_ATUAVRCONT.NRSEQITPROD;
                  ELSE
                     UPDATE ITEMPRODUTO
                     SET VRBRUTITEM = ROUND(VRBRUTITEM + (V_VRBRUTO  * (VRTOTAITEM - VRDESCITEM)),5),
                         VRCONTITEM = ROUND(VRCONTITEM + (V_VRCONTAB * (VRTOTAITEM - VRDESCITEM)),5)
                     WHERE CDFILIAL    = P_FILIAL   AND
                           NRLANCTONF  = P_LANCTONF AND
                           NRSEQITPROD = C_ATUAVRCONT.NRSEQITPROD;
                  END IF;
               END LOOP;
            END IF;
         ELSE
            FOR C_ATUAVRCONT IN (SELECT DISTINCT(IP.NRSEQITPROD) AS NRSEQITPROD, (VRTOTAITEM - VRDESCITEM) AS VRLIQUIDO
                                 FROM ITEMPRODUTO IP, ITEMPROIMPOS II
                                 WHERE (IP.CDFILIAL    = P_FILIAL)       AND
                                       (IP.NRLANCTONF  = P_LANCTONF)     AND
                                       (II.CDFILIAL    = IP.CDFILIAL)    AND
                                       (II.NRLANCTONF  = IP.NRLANCTONF)  AND
                                       (II.NRSEQITPROD = IP.NRSEQITPROD) AND
                                       (EXISTS(SELECT BC.CDIMPOSTO
                                               FROM BASECALC BC
                                               WHERE (BC.CDENCARGO = C_ITEMENCARGO.CDENCARGO) AND
                                                     (BC.CDIMPOSTO = II.CDIMPOSTO)            AND
                                                     (BC.CDIMPOSTO IN (SELECT B.CDIMPOSTO
                                                                       FROM BASECALC B
                                                                       WHERE B.CDENCARGO = C_ITEMENCARGO.CDENCARGO))))) LOOP
               IF (C_ITEMENCARGO.IDENCARGO = w_A) THEN
                  V_VRBRUTO  := (C_ITEMENCARGO.VRITEMENCA/V_TOTAPRODENCA);
               ELSIF (C_ITEMENCARGO.IDENCARGO = w_D) THEN
                  V_VRBRUTO  := - (C_ITEMENCARGO.VRITEMENCA/V_TOTAPRODENCA);
               ELSE
                  V_VRBRUTO := 0;
               END IF;
               IF (C_ITEMENCARGO.CDCTBLSAIDEN IS NULL) THEN
                  V_VRCONTAB := V_VRBRUTO;
               ELSE
                  V_VRCONTAB := 0;
               END IF;
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                  UPDATE ITEMPRODUTO
                  SET VRBRUTITEM = TRUNC(VRBRUTITEM + (V_VRBRUTO  * (VRTOTAITEM - VRDESCITEM)),2),
                      VRCONTITEM = TRUNC(VRCONTITEM + (V_VRCONTAB * (VRTOTAITEM - VRDESCITEM)),2)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_ATUAVRCONT.NRSEQITPROD;
               ELSE
                  UPDATE ITEMPRODUTO
                  SET VRBRUTITEM = ROUND(VRBRUTITEM + (V_VRBRUTO  * (VRTOTAITEM - VRDESCITEM)),5),
                      VRCONTITEM = ROUND(VRCONTITEM + (V_VRCONTAB * (VRTOTAITEM - VRDESCITEM)),5)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_ATUAVRCONT.NRSEQITPROD;
               END IF;
            END LOOP;
            FOR C_ITEMPROIMPOS IN (SELECT II.NRSEQITPROD, II.NRITPROIMPOS, II.CDIMPOSTO, II.VRPERREDBCIT, II.VRALIQITEM,
                                          II.VRPARDEDIT, II.IDTRIMPSTII, (VRTOTAITEM - VRDESCITEM) AS VRLIQUIDO,
                                          NVL(II.VRALIQINTEDEST, 0) AS VRALIQINTEDEST, NVL(II.VRALIQFCP, 0) AS VRALIQFCP,
                                          NVL(IP.IDUTILBCPMC, 'N') AS IDUTILBCPMC, I.CDIMPOSTONFE, NVL(II.IDCONSDIFALIQ, 'N') AS IDCONSDIFALIQ 
                                   FROM ITEMPROIMPOS II, ITEMPRODUTO IP, BASECALC BC, IMPOSTO I
                                   WHERE (II.CDFILIAL     = P_FILIAL)                AND
                                         (II.NRLANCTONF   = P_LANCTONF)              AND
                                         (IP.CDFILIAL     = P_FILIAL)                AND
                                         (IP.NRLANCTONF   = P_LANCTONF)              AND
                                         (IP.NRSEQITPROD  = II.NRSEQITPROD)          AND
                                         (BC.CDIMPOSTO    = II.CDIMPOSTO)            AND
                                         (II.CDIMPOSTO    = I.CDIMPOSTO)             AND
                                         (BC.CDENCARGO    = C_ITEMENCARGO.CDENCARGO)
                                   ORDER BY 1, 2, 3) LOOP
               IF NOT((C_ITEMPROIMPOS.IDUTILBCPMC = W_S) AND (C_ITEMPROIMPOS.CDIMPOSTONFE = '80')) OR (C_ITEMPROIMPOS.IDUTILBCPMC <> W_S) THEN
                  IF C_ITEMPROIMPOS.IDCONSDIFALIQ <> W_S THEN
                     SELECT SUM(VRTOTAITEM - VRDESCITEM)
                       INTO V_TOTAPRENIMPO
                       FROM ITEMPRODUTO IP
                      WHERE IP.CDFILIAL   = P_FILIAL
                        AND IP.NRLANCTONF = P_LANCTONF;
                     IF (V_TOTAPRODENCA <> 0) THEN
                        IF (C_ITEMENCARGO.IDENCARGO = w_A) THEN
                           V_PRODUENCARGO := +(C_ITEMENCARGO.VRITEMENCA/V_TOTAPRENIMPO)*C_ITEMPROIMPOS.VRLIQUIDO;
                        ELSIF (C_ITEMENCARGO.IDENCARGO = w_D) THEN
                           V_PRODUENCARGO := -(C_ITEMENCARGO.VRITEMENCA/V_TOTAPRENIMPO)*C_ITEMPROIMPOS.VRLIQUIDO;
                        ELSE
                           V_PRODUENCARGO := 0;
                        END IF;
                     END IF;
                     V_IDVRIMPSTRIB := null;
                     BEGIN
                        SELECT CASE COUNT(*) WHEN 0 THEN 'M' ELSE 'R' END
                          INTO V_MAJORAOUREDUZ
                          FROM ITEMPROIMPOS
                         WHERE CDFILIAL = P_FILIAL 
                           AND NRLANCTONF = P_LANCTONF
                           AND NRSEQITPROD = C_ITEMPROIMPOS.NRSEQITPROD
                           AND CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO
                           AND NRITPROIMPOS = w_2;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_MAJORAOUREDUZ := 'M';
                     END;
                     V_VRIMPOSTOST := 0;
                     IF V_MAJORAOUREDUZ = 'R' THEN
                        V_TAXABASECALC := (100 - C_ITEMPROIMPOS.VRPERREDBCIT)/100;
                        V_TAXAALIQUOTA := (C_ITEMPROIMPOS.VRALIQITEM/100);
                     ELSE
                        V_TAXABASECALC := 1 + (C_ITEMPROIMPOS.VRPERREDBCIT)/100;
                        V_TAXAALIQUOTA := (C_ITEMPROIMPOS.VRALIQITEM/100);
                        SELECT NVL(MAX(IDVRIMPSTRIB), 'N')
                          INTO V_IDVRIMPSTRIB
                          FROM ITEMPROIMPOS I, IMPOSTO II
                         WHERE (CDFILIAL     = P_FILIAL)
                           AND (NRLANCTONF   = P_LANCTONF)
                           AND (NRSEQITPROD  = C_ITEMPROIMPOS.NRITPROIMPOS)
                           AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0)
                           AND (I.CDIMPOSTO = II.CDIMPOSTO);
                        IF (C_ITEMPROIMPOS.IDTRIMPSTII <> w_1) AND (NVL(V_IDVRIMPSTRIB,w_N) = w_S) THEN
                           Begin
                              SELECT NVL(MAX(VRIMPOSTOIT),0)
                              INTO   V_VRIMPOSTOST
                              FROM ITEMPROIMPOS I
                              WHERE (CDFILIAL     = P_FILIAL)                   AND
                                    (NRLANCTONF   = P_LANCTONF)                 AND
                                    (INSTR(V_CDIMPOSTRIB,CDIMPOSTO||'_') > 0)   AND
                                    (NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS);
                           Exception
                              When No_Data_Found Then
                                 V_VRIMPOSTOST := 0;
                           End;
                        END IF;
                     END IF;
                     V_ICMS := 'N';
                     FOR C_ICMS IN (SELECT CDIMPOSTO
                                      FROM IMPOSTONFE N, IMPOSTO I
                                     WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                       AND N.CDIMPOSTONFE = '00'
                                       AND I.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO)
                     LOOP
                        V_ICMS := 'S';
                     END LOOP;
                     SELECT NVL(MAX(IDPERVISIMPSCRE), 'S')
                       INTO V_IDPERVISIMPSCRE
                       FROM PARAMGERAL;
                     UPDATE ITEMPROIMPOS
                        SET VRCONTITIMP  = VRCONTITIMP  + (V_PRODUENCARGO * V_TAXABASECALC),
                            VRBASECALCIT = VRBASECALCIT + ((V_PRODUENCARGO * V_TAXABASECALC) / (1 - ((C_ITEMPROIMPOS.VRALIQINTEDEST + C_ITEMPROIMPOS.VRALIQFCP) / 100))),
                            VRIMPOSTOIT  = (VRIMPOSTOIT  + (((V_PRODUENCARGO * V_TAXABASECALC) / (1 - ((C_ITEMPROIMPOS.VRALIQINTEDEST + C_ITEMPROIMPOS.VRALIQFCP) / 100))) * V_TAXAALIQUOTA)) - (((V_VRIMPOSTOST * V_TAXABASECALC) / (1 - ((C_ITEMPROIMPOS.VRALIQINTEDEST + C_ITEMPROIMPOS.VRALIQFCP) / 100))) * V_TAXAALIQUOTA),
                            VRIMPOSTOVISUIT = DECODE(V_IDPERVISIMPSCRE, 'S', (VRIMPOSTOIT  + ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA)) - ((V_VRIMPOSTOST * V_TAXABASECALC) * V_TAXAALIQUOTA),  DECODE(CDFISCALIT , '1', (VRIMPOSTOIT  + ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA)) - ((V_VRIMPOSTOST * V_TAXABASECALC) * V_TAXAALIQUOTA),0)),
                            VRADICFECP  = VRADICFECP  + ROUND((V_PRODUENCARGO * (VRADICALIQFECP / 100) * V_TAXABASECALC),2)
                      WHERE (CDFILIAL     = P_FILIAL)                   AND
                            (NRLANCTONF   = P_LANCTONF)                 AND
                            (NRSEQITPROD  = C_ITEMPROIMPOS.NRSEQITPROD) AND
                            (CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO)   AND
                            (NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS);
                  END IF;
               END IF;
               /* ROTINA PARA RECALCULAR O DIFERIMENTO DO IMPOSTO */
               BEGIN
                  SELECT MAX(VRIMPOSTOOPERAIT), MAX(VRPERDIFEREIT)
                    INTO V_VRIMPOSTOOPERAIT, V_VRPERDIFEREIT
                    FROM ITEMPROIMPOS
                   WHERE (CDFILIAL     = P_FILIAL)                    AND
                         (NRLANCTONF   = P_LANCTONF)                  AND
                         (NRSEQITPROD  = C_ITEMPROIMPOS.NRSEQITPROD)  AND
                         (CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO)    AND
                         (NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS) AND
                         (VRPERDIFEREIT <> 0);
               EXCEPTION
                  When No_Data_Found Then
                     V_VRIMPOSTOOPERAIT := 0; V_VRPERDIFEREIT := 0;
               END;
               IF V_VRPERDIFEREIT <> 0 THEN
                  V_VRIMPOSTOOPERAIT := ROUND((V_VRIMPOSTOOPERAIT + ((ROUND(V_PRODUENCARGO, 2) * V_TAXABASECALC) * V_TAXAALIQUOTA)) - ((V_VRIMPOSTOST * V_TAXABASECALC) * V_TAXAALIQUOTA),2);
                  /*
                     Foi comentado este cálculo pois ao implementá-lo de acordo com a resolução nº13 de 14 de fevereiro de 2019
                     a sefaz passou a rejeitar as notas com a rejeição 352 (Rejeicao: Valor do ICMS Diferido no CST=51 difere do 
                     produto Valor ICMS Operacao e percentual diferimento)
                  */
                  --IF (V_CDPAISEMIT = 0055) AND (V_SGESTADOEMIT = RJ) THEN
                  --   V_VRIMPOSTODIFERIT := ROUND((((V_TAXABASECALC / (1 - V_TAXAALIQUOTA)) * V_TAXAALIQUOTA) * (V_VRPERDIFEREIT/100)), 2);
                  --ELSE
                     V_VRIMPOSTODIFERIT := ROUND((V_VRIMPOSTOOPERAIT * V_VRPERDIFEREIT/100), 2);
                  --END IF;
                  UPDATE ITEMPROIMPOS
                     SET VRIMPOSTOOPERAIT = VRIMPOSTOOPERAIT + V_VRIMPOSTOOPERAIT,
                         VRIMPOSTODIFERIT = VRIMPOSTODIFERIT + V_VRIMPOSTODIFERIT,
                         VRIMPOSTOIT = VRIMPOSTOIT + (V_VRIMPOSTOOPERAIT - V_VRIMPOSTODIFERIT),
                         VRIMPOSTOVISUIT = VRIMPOSTOVISUIT + (V_VRIMPOSTOOPERAIT - V_VRIMPOSTODIFERIT)
                   WHERE (CDFILIAL     = P_FILIAL)                    AND
                         (NRLANCTONF   = P_LANCTONF)                  AND
                         (NRSEQITPROD  = C_ITEMPROIMPOS.NRSEQITPROD)  AND
                         (CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO)    AND
                         (NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS) AND
                         (VRPERDIFEREIT <> 0);
               END IF;
            END LOOP;
         END IF;
      END LOOP;
      UPDATE ITEMPROIMPOS
      SET VRIMPOSTOIT = 0,
          VRIMPOSTOVISUIT = 0
      WHERE (NRLANCTONF  = P_LANCTONF) AND
            (CDFILIAL    = P_FILIAL)   AND
            (VRIMPOSTOIT < 0);
      UPDATE ITEMPROIMPOS
      SET VRBASECALCIT = 0
      WHERE (NRLANCTONF  = P_LANCTONF) AND
            (CDFILIAL    = P_FILIAL)   AND
            (VRBASECALCIT < 0);
      --CÁLCULO DO VALOR DOS IMPOSTOS QUE CONTÉM OUTROS IMPOSTOS NA SUA BASE DE CÁLCULO
      FOR C_PRODUTO IN (SELECT IP.NRSEQITPROD AS NRSEQITPROD, II.CDIMPOSTO, VRPERREDBCIT,
                               VRALIQITEM, VRPARDEDIT, CDFISCALIT, VRIMPOSTOIT, IDTRATIMPO,
                               IM.IDIMPORECUP, IM.CDCCTBLPASS, IM.CDCCTBLATIV, NRITPROIMPOS,
                               IDTRIMPSTII, IM.IDTIPCONTAB, II.VRBASECALCIT, II.IDIMPSTCARGTRIB,
                               IP.CDPRODUTO, P.CDARVPROD, IP.IDORIGEMPROD, IM.IDDESCRFONTE,
                               NVL(IP.IDUTILBCPMC, W_N) AS IDUTILBCPMC, NVL(VRADICFECP, 0) AS VRADICFECP,
                               NVL(VRADICALIQFECP, 0) AS VRADICALIQFECP, NVL(II.IDCONSDIFALIQ, W_N) AS IDCONSDIFALIQ 
                        FROM ITEMPRODUTO IP, ITEMPROIMPOS II, IMPOSTO IM, PRODUTO P
                        WHERE (IP.CDFILIAL     = P_FILIAL)      AND
                              (IP.NRLANCTONF   = P_LANCTONF)    AND
                              (II.CDFILIAL     = IP.CDFILIAL)   AND
                              (II.NRLANCTONF   = IP.NRLANCTONF) AND
                              (IM.CDIMPOSTO    = II.CDIMPOSTO)  AND
                              (II.NRITPROIMPOS = w_1)           AND
                              (II.NRSEQITPROD  = IP.NRSEQITPROD) AND
                              P.CDPRODUTO = IP.CDPRODUTO
                        ORDER BY NRSEQITPROD, CDIMPOSTO) LOOP
         V_IDIMPOBCALTPOP := NULL;
         SELECT TO_CHAR(MAX('S')) , NVL(SUM(VRIMPOSTOIT),0) AS VRIMPOBCALC
           INTO   V_IDIMPOBCALTPOP, V_TOTAIMPBCALC
           FROM ITEMPRODUTO IP, ITEMPROIMPOS II, BASECALCTPOPER BC , IMPOSTO I
          WHERE (BC.CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)
            AND (IP.CDFILIAL     = P_FILIAL)
            AND (IP.NRLANCTONF   = P_LANCTONF)
            AND (II.NRLANCTONF   = IP.NRLANCTONF)
            AND (II.CDFILIAL     = IP.CDFILIAL)
            AND (IP.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
            AND (II.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
            AND (II.NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS)
            AND ((SUBSTR(IP.CDSITUTRIBPR,2,2) = BC.CDCSTIMPOTPOP)  OR (BC.CDCSTIMPOTPOP  IS NULL))
            AND ((SUBSTR(IP.CDFISOPEITEM,2,4) = BC.CDCFOPIMPOTPOP) OR (BC.CDCFOPIMPOTPOP IS NULL))
            AND ((C_PRODUTO.CDFISCALIT        = BC.CDFISCIMPOTPOP) OR (BC.CDFISCIMPOTPOP IS NULL))
            AND (BC.IDENTRSAIDOP = 'S')
            AND (BC.CDTIPOOPER = P_TIPOOPER)
            AND (II.CDIMPOSTO = BC.CDIMPOBCALC)
            AND (BC.CDIMPOBCALC = I.CDIMPOSTO)
            AND (NVL(BC.IDTRATIMPOBCTPOP, I.IDTRATIMPO) <> 'D');
         IF V_IDIMPOBCALTPOP IS NULL THEN
            SELECT NVL(SUM(VRIMPOSTOIT),0) AS VRIMPOBCALC
              INTO   V_TOTAIMPBCALC
              FROM ITEMPRODUTO IP, ITEMPROIMPOS II, BASECALC BC, IMPOSTO I
             WHERE (BC.CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)    AND
                   (IP.CDFILIAL     = P_FILIAL)               AND
                   (IP.NRLANCTONF   = P_LANCTONF)             AND
                   (II.NRLANCTONF   = IP.NRLANCTONF)          AND
                   (II.CDFILIAL     = IP.CDFILIAL)            AND
                   (IP.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                   (II.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                   (II.NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS) AND
                   ((SUBSTR(IP.CDSITUTRIBPR,2,2) = BC.CDCSTIMPO)  OR (BC.CDCSTIMPO  IS NULL)) AND
                   ((SUBSTR(IP.CDFISOPEITEM,2,4) = BC.CDCFOPIMPO) OR (BC.CDCFOPIMPO IS NULL)) AND
                   ((C_PRODUTO.CDFISCALIT        = BC.CDFISCIMPO) OR (BC.CDFISCIMPO IS NULL)) AND
                   (II.CDIMPOSTO = BC.CDIMPOBCALC) AND
                   (BC.IDTIPOBCALC   = w_I)AND
                   (BC.CDIMPOBCALC = I.CDIMPOSTO)  AND
                   (NVL(BC.IDTRATIMPOBC, I.IDTRATIMPO) <> 'D');
         END IF;
         V_IDIMBCALTPOPDE := NULL;
         SELECT TO_CHAR(MAX('S')), (NVL(SUM(VRIMPOSTOIT),0)*-1) AS VRIMPOBCALC
           INTO V_IDIMBCALTPOPDE, V_TOTAIMPBCDES
           FROM ITEMPRODUTO IP, ITEMPROIMPOS II, BASECALCTPOPER BC, IMPOSTO I
          WHERE (BC.CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)
            AND (IP.CDFILIAL     = P_FILIAL)
            AND (IP.NRLANCTONF   = P_LANCTONF)
            AND (II.NRLANCTONF   = IP.NRLANCTONF)
            AND (II.CDFILIAL     = IP.CDFILIAL)
            AND (IP.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
            AND (II.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
            AND (II.NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS)
            AND ((SUBSTR(IP.CDSITUTRIBPR,2,2) = BC.CDCSTIMPOTPOP)  OR (BC.CDCSTIMPOTPOP  IS NULL))
            AND ((SUBSTR(IP.CDFISOPEITEM,2,4) = BC.CDCFOPIMPOTPOP) OR (BC.CDCFOPIMPOTPOP IS NULL))
            AND ((C_PRODUTO.CDFISCALIT        = BC.CDFISCIMPOTPOP) OR (BC.CDFISCIMPOTPOP IS NULL))
            AND (BC.IDENTRSAIDOP = 'S')
            AND (BC.CDTIPOOPER = P_TIPOOPER)
            AND (II.CDIMPOSTO = BC.CDIMPOBCALC)
            AND (BC.CDIMPOBCALC = I.CDIMPOSTO)
            AND (NVL(BC.IDTRATIMPOBCTPOP, I.IDTRATIMPO) = 'D');
         IF V_IDIMBCALTPOPDE IS NULL THEN
            SELECT (NVL(SUM(VRIMPOSTOIT),0)*-1) AS VRIMPOBCALC INTO V_TOTAIMPBCDES
              FROM ITEMPRODUTO IP, ITEMPROIMPOS II, BASECALC BC, IMPOSTO I
             WHERE (BC.CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)    AND
                   (IP.CDFILIAL     = P_FILIAL)               AND
                   (IP.NRLANCTONF   = P_LANCTONF)             AND
                   (II.NRLANCTONF   = IP.NRLANCTONF)          AND
                   (II.CDFILIAL     = IP.CDFILIAL)            AND
                   (IP.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                   (II.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                   (II.NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS) AND
                   ((SUBSTR(IP.CDSITUTRIBPR,2,2) = BC.CDCSTIMPO)  OR (BC.CDCSTIMPO  IS NULL)) AND
                   ((SUBSTR(IP.CDFISOPEITEM,2,4) = BC.CDCFOPIMPO) OR (BC.CDCFOPIMPO IS NULL)) AND
                   ((C_PRODUTO.CDFISCALIT        = BC.CDFISCIMPO) OR (BC.CDFISCIMPO IS NULL)) AND
                   (II.CDIMPOSTO = BC.CDIMPOBCALC) AND
                   (BC.IDTIPOBCALC   = 'I')        AND
                   (BC.CDIMPOBCALC = I.CDIMPOSTO)  AND
                   (NVL(BC.IDTRATIMPOBC, I.IDTRATIMPO) = 'D');
         END IF;
         V_VRIMPOSTO := C_PRODUTO.VRIMPOSTOIT;
         /*IMPOSTO PARA SUBSTITUIÇÃO TRIBUTARIA*/
         V_CDIMPOSTRIB  := null;
         V_IDVRIMPSTRIB := null;
         FOR C_ICMSST IN (SELECT CDIMPOSTO
                            FROM IMPOSTO
                           WHERE CDIMPOSTRIB = C_PRODUTO.CDIMPOSTO)
         LOOP
            V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
         END LOOP;
         V_VRIMPOSTOST := 0;
         /*Só irá fazer o cálculo de outro imposto compondo a base de cálculo,
           se o valor do mesmo não estiver zerado OU
           se for o imposto de st, pois o icms pode ter sido recalculado(por ter alguem compondo sua bc)
           e o imposto st precisa retirar o valor final do st*/
         IF (NOT(V_TOTAIMPBCALC IS NULL) OR (V_TOTAIMPBCALC <> 0)) OR
            (NOT(V_TOTAIMPBCDES IS NULL) OR (V_TOTAIMPBCDES <> 0)) OR
            (V_CDIMPOSTRIB IS NOT NULL) THEN
            IF V_CDIMPOSTRIB IS NULL THEN
               V_TAXABASECALC := (100 - C_PRODUTO.VRPERREDBCIT)/100;
               V_TAXAALIQUOTA := (C_PRODUTO.VRALIQITEM/100);
               V_VRIMPOSTO    := V_VRIMPOSTO + (((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * V_TAXAALIQUOTA);
               SELECT NVL(MAX(IDPERVISIMPSCRE), 'S')
                 INTO V_IDPERVISIMPSCRE
                 FROM PARAMGERAL;
               UPDATE ITEMPROIMPOS
                  SET VRBASECALCIT = VRBASECALCIT + ((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC),
                      VRIMPOSTOIT  = VRIMPOSTOIT  + (((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * V_TAXAALIQUOTA),
                      VRIMPOSTOVISUIT = DECODE(V_IDPERVISIMPSCRE, 'S', VRIMPOSTOIT  + (((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * V_TAXAALIQUOTA), DECODE(CDFISCALIT, '1', VRIMPOSTOIT  + (((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * V_TAXAALIQUOTA), 0)),
                      VRADICFECP  = VRADICFECP  + ROUND((((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * (VRADICALIQFECP/100)),2)
                WHERE (CDFILIAL     = P_FILIAL)              AND
                      (NRLANCTONF   = P_LANCTONF)            AND
                      (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD) AND
                      (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)   AND
                      (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS);
               /* ROTINA PARA RECALCULAR O DIFERIMENTO DO IMPOSTO */
               IF (NVL(V_TOTAIMPBCALC, 0) > 0) OR (NVL(V_TOTAIMPBCDES, 0) > 0) THEN
                  BEGIN
                     SELECT MAX(VRIMPOSTOOPERAIT), MAX(VRPERDIFEREIT)
                       INTO V_VRIMPOSTOOPERAIT, V_VRPERDIFEREIT
                       FROM ITEMPROIMPOS
                      WHERE (CDFILIAL     = P_FILIAL)               AND
                            (NRLANCTONF   = P_LANCTONF)             AND
                            (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                            (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)    AND
                            (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS) AND
                            (VRPERDIFEREIT <> 0);
                  EXCEPTION
                     When No_Data_Found Then
                        V_VRIMPOSTOOPERAIT := 0; V_VRPERDIFEREIT := 0;
                  END;
                  IF V_VRPERDIFEREIT <> 0 THEN
                     V_VRIMPOSTOOPERAIT := ROUND(((ROUND((V_TOTAIMPBCALC + V_TOTAIMPBCDES), 2) * V_TAXABASECALC) * V_TAXAALIQUOTA),2);
                     /*
                        Foi comentado este cálculo pois ao implementá-lo de acordo com a resolução nº13 de 14 de fevereiro de 2019
                        a sefaz passou a rejeitar as notas com a rejeição 352 (Rejeicao: Valor do ICMS Diferido no CST=51 difere do
                        produto Valor ICMS Operacao e percentual diferimento)
                     */
                     --IF (V_CDPAISEMIT = 0055) AND (V_SGESTADOEMIT = RJ) THEN
                     --   V_VRIMPOSTODIFERIT := ROUND((((V_TAXABASECALC / (1 - V_TAXAALIQUOTA)) * V_TAXAALIQUOTA) * (V_VRPERDIFEREIT/100)), 2);
                     --ELSE
                        V_VRIMPOSTODIFERIT := ROUND((V_VRIMPOSTOOPERAIT * V_VRPERDIFEREIT/100), 2);
                     --END IF;
                     UPDATE ITEMPROIMPOS
                        SET VRIMPOSTOOPERAIT = VRIMPOSTOOPERAIT + V_VRIMPOSTOOPERAIT,
                            VRIMPOSTODIFERIT = VRIMPOSTODIFERIT + V_VRIMPOSTODIFERIT,
                            VRIMPOSTOIT = VRIMPOSTOIT + (V_VRIMPOSTOOPERAIT - V_VRIMPOSTODIFERIT),
                            VRIMPOSTOVISUIT = VRIMPOSTOVISUIT + (V_VRIMPOSTOOPERAIT - V_VRIMPOSTODIFERIT)
                      WHERE (CDFILIAL     = P_FILIAL)               AND
                            (NRLANCTONF   = P_LANCTONF)             AND
                            (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                            (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)    AND
                            (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS) AND
                            (VRPERDIFEREIT <> 0);
                  END IF;
               END IF;
            ELSE
               V_VRIMPOSST   := 0;
               V_VRIMPOSTOST := 0;
               FOR C_SUBSTRIB IN (SELECT NVL(IDVRIMPSTRIB, 'N') IDVRIMPSTRIB, I.CDIMPOSTO
                                    FROM ITEMPROIMPOS I, IMPOSTO II
                                   WHERE (CDFILIAL     = P_FILIAL)
                                     AND (NRLANCTONF   = P_LANCTONF)
                                     AND (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
                                     AND (NRITPROIMPOS = '1')
                                     AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0)
                                     AND (I.CDIMPOSTO = II.CDIMPOSTO))
               LOOP
                  IF (C_SUBSTRIB.IDVRIMPSTRIB = w_S) THEN
                     Begin
                        SELECT NVL(ROUND(MAX(VRIMPOSTOIT), 2),0)
                        INTO   V_VRIMPOSST
                        FROM ITEMPROIMPOS I
                        WHERE (CDFILIAL     = P_FILIAL)                   AND
                              (NRLANCTONF   = P_LANCTONF)                 AND
                              (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                              (CDIMPOSTO    = C_SUBSTRIB.CDIMPOSTO)       AND
                              (INSTR(V_CDIMPOSTRIB,CDIMPOSTO||'_') > 0)   AND
                              (NRITPROIMPOS = w_1);
                     Exception
                        When No_Data_Found Then
                           V_VRIMPOSST := 0;
                     End;
                  ELSE
                     V_VRIMPOSST := 0;
                  END IF;
                  V_VRIMPOSTOST := V_VRIMPOSTOST + V_VRIMPOSST;
               END LOOP;
               IF (C_PRODUTO.IDUTILBCPMC = W_S) THEN 
                  V_TAXAALIQUOTA := C_PRODUTO.VRALIQITEM / 100;
                  V_VRIMPOSTO := C_PRODUTO.VRBASECALCIT * V_TAXAALIQUOTA;
               ELSIF (C_PRODUTO.IDCONSDIFALIQ = W_S) THEN
                  V_TAXAALIQUOTA := C_PRODUTO.VRALIQITEM / 100;
                  V_VRIMPOSTO := C_PRODUTO.VRIMPOSTOIT;
               ELSE
                  BEGIN
                     SELECT CASE COUNT(*) WHEN 0 THEN 'M' ELSE 'R' END
                       INTO V_MAJORAOUREDUZ
                       FROM ITEMPROIMPOS
                      WHERE CDFILIAL = P_FILIAL 
                        AND NRLANCTONF = P_LANCTONF
                        AND NRSEQITPROD = C_PRODUTO.NRSEQITPROD
                        AND CDIMPOSTO = C_PRODUTO.CDIMPOSTO
                        AND NRITPROIMPOS = w_2;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_MAJORAOUREDUZ := 'M';
                  END;
                  IF V_MAJORAOUREDUZ = 'R' THEN
                     V_TAXABASECALC := (100 - C_PRODUTO.VRPERREDBCIT)/100;
                  ELSE
                     V_TAXABASECALC := 1 + (C_PRODUTO.VRPERREDBCIT)/100;
                  END IF;
                  V_TAXAALIQUOTA := (C_PRODUTO.VRALIQITEM/100);
                  V_VRIMPOSTO := ((C_PRODUTO.VRBASECALCIT + ((V_TOTAIMPBCALC + V_TOTAIMPBCDES) * V_TAXABASECALC)) * V_TAXAALIQUOTA);
               END IF;
               SELECT NVL(MAX(IDPERVISIMPSCRE), 'S')
                 INTO V_IDPERVISIMPSCRE
                 FROM PARAMGERAL;
               -- ESSA CONDIÇÃO SERÁ FEITA PORQUE A BASE DE CÁLCULO NÃO SERÁ ALTERADO QUANDO O PRODUTO TIVER CONSIDERANDO 
               -- O DIFERENCIAL DE ALÍQUOTA DO ICMS, ISTO PORQUE A BASE DE CÁLCULO JÁ FOI CALCULADA NA PROCEDURE CONSIDERA_DIFAL_ICMS
               IF (C_PRODUTO.IDCONSDIFALIQ = W_S) THEN
                  UPDATE ITEMPROIMPOS
                     SET VRIMPOSTOVISUIT = DECODE(V_IDPERVISIMPSCRE, 'S', (VRIMPOSTOIT), DECODE(CDFISCALIT, '1', (VRIMPOSTOIT),0))
                   WHERE (CDFILIAL     = P_FILIAL)              AND
                         (NRLANCTONF   = P_LANCTONF)            AND
                         (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD) AND
                         (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)   AND
                         (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS);
               ELSIF C_PRODUTO.IDIMPSTCARGTRIB <> 'S' THEN
                  IF V_VRIMPOSTOST > V_VRIMPOSTO THEN
                     UPDATE ITEMPROIMPOS
                     SET VRBASECALCIT = VRBASECALCIT + ((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC),
                         VRIMPOSTOIT  = 0,
                         VRIMPOSTOVISUIT = 0
                     WHERE (CDFILIAL     = P_FILIAL)              AND
                           (NRLANCTONF   = P_LANCTONF)            AND
                           (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD) AND
                           (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)   AND
                           (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS);
                     V_VRIMPOSTO := 0;
                  ELSE
                     -- ESSA CONDIÇÃO SERÁ FEITA PORQUE A BASE DE CÁLCULO NÃO SERÁ ALTERADO QUANDO O PRODUTO TIVER PMC,
                     -- ISTO PORQUE A BASE DE CÁLCULO JÁ FOI CALCULADA NA PROCEDURE CALCULA_BC_PMC
                     IF (C_PRODUTO.IDUTILBCPMC = W_S) THEN
                        UPDATE ITEMPROIMPOS
                           SET VRBASECALCIT = VRBASECALCIT,
                               VRIMPOSTOIT  = V_VRIMPOSTO - V_VRIMPOSTOST,
                               VRIMPOSTOVISUIT = DECODE(V_IDPERVISIMPSCRE, 'S', (V_VRIMPOSTO - V_VRIMPOSTOST), DECODE(CDFISCALIT, '1', (V_VRIMPOSTO - V_VRIMPOSTOST),0))
                         WHERE (CDFILIAL     = P_FILIAL)              AND
                               (NRLANCTONF   = P_LANCTONF)            AND
                               (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD) AND
                               (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)   AND
                               (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS);
                     ELSE
                        UPDATE ITEMPROIMPOS
                           SET VRBASECALCIT = VRBASECALCIT + ((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC),
                               VRIMPOSTOIT  = V_VRIMPOSTO - V_VRIMPOSTOST,
                               VRIMPOSTOVISUIT = DECODE(V_IDPERVISIMPSCRE, 'S', (V_VRIMPOSTO - V_VRIMPOSTOST), DECODE(CDFISCALIT, '1', (V_VRIMPOSTO - V_VRIMPOSTOST),0)),
                               VRADICFECP  = VRADICFECP + ROUND((((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * (VRADICALIQFECP/100)),2)
                         WHERE (CDFILIAL     = P_FILIAL)              AND
                               (NRLANCTONF   = P_LANCTONF)            AND
                               (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD) AND
                               (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)   AND
                               (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS);
                     END IF;
                     V_VRIMPOSTO := V_VRIMPOSTO - V_VRIMPOSTOST;
                  END IF;
               END IF;
            END IF;
            /* Rotina para calcular o ICMS ST considerando % carga tributária média */
            IF V_CDIMPOSTRIB IS NOT NULL THEN
               BEGIN
                  SELECT NVL(VRCNAEBC, 0),
                         NVL(VRMVA, 0)
                    INTO V_VRPRODUTOBC,
                         V_VRMVA
                    FROM VRPERCCARGTRIBCNAE
                   WHERE IDENTRSAIDOP = w_S
                     AND CDTIPOOPER   = P_TIPOOPER
                     AND CDPAIS       = V_PAISDEST
                     AND SGESTADO     = V_ESTADEST
                     AND CDCNAE       = V_CDCNAECLIE
                     AND IDOPTANTESN  = V_IDOPTANTESNCLIE;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_VRPRODUTOBC := 0;
                     V_VRMVA := 0;
               END;
               IF V_VRPRODUTOBC = 0 THEN
                  BEGIN
                     SELECT NVL(VRPRODUTOBC, 0)
                       INTO V_VRPRODUTOBC
                       FROM VRPERCCARGTRIB
                      WHERE IDENTRSAIDOP = w_S
                        AND CDTIPOOPER   = P_TIPOOPER
                        AND CDPAIS       = V_PAISDEST
                        AND SGESTADO     = V_ESTADEST
                        AND CDPRODUTO    = C_PRODUTO.CDPRODUTO;
                  EXCEPTION
                     When No_Data_Found Then
                        V_VRPRODUTOBC := 0;
                  END;
               END IF;
               IF (V_VRPRODUTOBC > 0) THEN
                  BEGIN
                     SELECT I.CDIMPOSTO, I.VRIMPOSTOIT, I.VRBASECALCIT
                       INTO V_CDIMPOSTOICMS, V_VRIMPOSTOICMS, V_VRBASECALCICMS
                       FROM IMPOSTONFE N, IMPOSTO P, ITEMPROIMPOS I
                      WHERE N.CDIMPOSTONFE = P.CDIMPOSTONFE
                        AND N.CDIMPOSTONFE = '00'
                        AND I.CDFILIAL     = P_FILIAL
                        AND I.NRLANCTONF   = P_LANCTONF
                        AND I.NRSEQITPROD = C_PRODUTO.NRSEQITPROD
                        AND I.CDIMPOSTO = NVL(P.CDIMPOSREGNORMA, P.CDIMPOSTO)
                        AND NOT P.CDIMPOSTRIB IS NULL
                        AND I.NRITPROIMPOS = '1';
                  EXCEPTION
                     When No_Data_Found Then
                        V_CDIMPOSTOICMS := NULL; V_VRIMPOSTOICMS := 0; V_VRBASECALCICMS := 0;
                  END;
                  DELETE ALIQTEMP
                   WHERE NRALIQTEMP = V_ALIQTEMP;
                  /* Verifica grupo fiscal */
                  SELECT MAX(IDGRUPFISSRV)
                  INTO V_UTGRUPFISCS
                  FROM IMPOSTO
                  WHERE CDIMPOSTO = V_CDIMPOSTOICMS;
                  IF V_UTGRUPFISCS = 'S' THEN
                     V_CDGRPFISCSV := NULL;
                     SELECT MAX(CDGRUPOFISC)
                     INTO V_CDGRPFISCSV
                     FROM SERVCLIE SC, SERVICO S
                     WHERE SC.CDFILIAL   = V_CDFILIEMIT AND
                           SC.CDCLIENTE  = P_CLIENTE    AND
                           SC.CDFILIAL   = S.CDFILIAL   AND
                           SC.CDSERVFILI = S.CDSERVFILI AND
                           S.CDPRODUTO   = C_PRODUTO.CDPRODUTO;
                     IF V_CDGRPFISCSV IS NULL THEN
                        SELECT MAX(CDGRUPOFISC)
                        INTO V_CDGRPFISCSV
                        FROM CLIENFILIAL
                       WHERE CDFILIAL   = V_CDFILIEMIT AND
                             CDCLIENTE  = P_CLIENTE;
                     END IF;
                     CONSULTA_ALIQUOTA(V_CDIMPOSTOICMS, w_S, P_EMPRESA, C_PRODUTO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                       V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRPFISCSV, P_TIPOOPER,
                                       NULL, NULL, NULL, NULL, NULL, NULL, C_PRODUTO.IDORIGEMPROD);
                  ELSE
                     CONSULTA_ALIQUOTA(V_CDIMPOSTOICMS, w_S, P_EMPRESA, C_PRODUTO.CDARVPROD, P_INSCESTA, V_PAISDEST,
                                       V_ESTADEST, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                       NULL, NULL, NULL, NULL, NULL, NULL, C_PRODUTO.IDORIGEMPROD);
                  END IF;
                  SELECT NVL(MAX(VRALIQUTMP),0)
                    INTO V_VRALIQUTMPDEST
                    FROM ALIQTEMP
                   WHERE (NRALIQTEMP = V_ALIQTEMP);
                  IF (V_VRALIQUTMPDEST <> 0) THEN
                     IF V_VRMVA > 0 THEN
                        V_VRBASECALCICMS :=  /*V_VRBASECALCICMS + */(V_VRBASECALCICMS * (V_VRMVA/100));
                     END IF;
                     V_IMPOSTOIT  :=  ROUND(V_VRBASECALCICMS * (V_VRPRODUTOBC/100), 2);
                     V_BASECALCTO :=  ROUND(((V_VRIMPOSTOICMS + V_IMPOSTOIT) / (V_VRALIQUTMPDEST/100)),5);  
                     UPDATE ITEMPROIMPOS
                        SET VRBASECALCIT = V_BASECALCTO,
                            VRIMPOSTOIT  = V_IMPOSTOIT,
                            VRIMPOSTOVISUIT = DECODE(V_IDPERVISIMPSCRE, 'S', V_IMPOSTOIT, DECODE(CDFISCALIT, '1', V_IMPOSTOIT,0))
                      WHERE (CDFILIAL     = P_FILIAL)              AND
                            (NRLANCTONF   = P_LANCTONF)            AND
                            (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD) AND
                            (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)   AND
                            (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS);
                  END IF;
                  DELETE ALIQTEMP
                   WHERE NRALIQTEMP = V_ALIQTEMP;
               END IF;
            END IF;
         END IF;
         V_IMPOCONTAB := 0;
         IF (C_PRODUTO.IDTRATIMPO = w_A) THEN
            IF (((C_PRODUTO.IDIMPORECUP = w_S)  AND (C_PRODUTO.CDFISCALIT = w_1)) OR
                ((C_PRODUTO.IDIMPORECUP <> w_S) AND (C_PRODUTO.CDCCTBLPASS IS NOT NULL))) AND
               (V_CDIMPOSTRIB IS NULL) AND (C_PRODUTO.IDTIPCONTAB = w_D) THEN
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                  UPDATE ITEMPRODUTO
                  SET VRBRUTITEM = TRUNC((VRBRUTITEM + V_VRIMPOSTO),2)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               ELSE
                  UPDATE ITEMPRODUTO
                  SET VRBRUTITEM = ROUND((VRBRUTITEM + V_VRIMPOSTO),5)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               END IF;
            ELSE
               IF ((V_CDIMPOSTRIB IS NOT NULL) AND (C_PRODUTO.IDTRIMPSTII = w_0)) OR
                   (V_CDIMPOSTRIB IS NULL) THEN
                  IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                     UPDATE ITEMPRODUTO
                     SET VRBRUTITEM = TRUNC((VRBRUTITEM + V_VRIMPOSTO + C_PRODUTO.VRADICFECP), 2),
                         VRCONTITEM = TRUNC((VRCONTITEM + V_VRIMPOSTO + C_PRODUTO.VRADICFECP), 2)
                     WHERE CDFILIAL    = P_FILIAL   AND
                           NRLANCTONF  = P_LANCTONF AND
                           NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
                  ELSE
                     UPDATE ITEMPRODUTO
                     SET VRBRUTITEM = ROUND(VRBRUTITEM + V_VRIMPOSTO + C_PRODUTO.VRADICFECP, 5),
                         VRCONTITEM = ROUND(VRCONTITEM + V_VRIMPOSTO + C_PRODUTO.VRADICFECP, 5)
                     WHERE CDFILIAL    = P_FILIAL   AND
                           NRLANCTONF  = P_LANCTONF AND
                           NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
                  END IF;
               END IF;
            END IF;
         ELSIF (C_PRODUTO.IDTRATIMPO = w_D) THEN
            IF ((C_PRODUTO.IDIMPORECUP = w_S) AND (C_PRODUTO.CDFISCALIT = w_1)) OR
               ((C_PRODUTO.IDIMPORECUP <> w_S) AND (C_PRODUTO.CDCCTBLATIV IS NOT NULL)) AND
               (C_PRODUTO.IDTIPCONTAB = w_D) THEN
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                  UPDATE ITEMPRODUTO
                  SET VRBRUTITEM = TRUNC((VRBRUTITEM - V_VRIMPOSTO),2)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               ELSE
                  UPDATE ITEMPRODUTO
                  SET VRBRUTITEM = ROUND(VRBRUTITEM - V_VRIMPOSTO,5)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               END IF;
            ELSE
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                  UPDATE ITEMPRODUTO
                  SET VRBRUTITEM = TRUNC((VRBRUTITEM - V_VRIMPOSTO),2),
                      VRCONTITEM = TRUNC((VRCONTITEM - V_VRIMPOSTO),2)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               ELSE
                  UPDATE ITEMPRODUTO
                  SET VRBRUTITEM = ROUND(VRBRUTITEM - V_VRIMPOSTO,5),
                      VRCONTITEM = ROUND(VRCONTITEM - V_VRIMPOSTO,5)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               END IF;
            END IF;
         ELSIF (C_PRODUTO.IDTRATIMPO = w_R) THEN
            IF (C_PRODUTO.IDIMPORECUP = w_S) THEN
               IF (C_PRODUTO.CDCCTBLATIV IS NULL) OR
                  (C_PRODUTO.CDFISCALIT <> w_1) THEN
                  V_IMPOCONTAB := - V_VRIMPOSTO;
               END IF;
            ELSE
               IF (C_PRODUTO.CDCCTBLATIV IS NULL) THEN
                  V_IMPOCONTAB := - V_VRIMPOSTO;
               ELSE
                  IF C_PRODUTO.IDDESCRFONTE = 'N' THEN
                     V_IMPOCONTAB := - V_VRIMPOSTO;
                     UPDATE ITEMPRODUTO
                     SET VRBRUTITEM = ROUND(VRBRUTITEM - V_VRIMPOSTO,5)
                     WHERE CDFILIAL    = P_FILIAL   AND
                           NRLANCTONF  = P_LANCTONF AND
                           NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
                  END IF;
               END IF;
            END IF;
            IF C_PRODUTO.IDTIPCONTAB = w_D THEN
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                  UPDATE ITEMPRODUTO
                  SET VRCONTITEM = TRUNC((VRCONTITEM + V_IMPOCONTAB),2)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               ELSE
                  UPDATE ITEMPRODUTO
                  SET VRCONTITEM = VRCONTITEM + V_IMPOCONTAB
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               END IF;
            END IF;
         ELSIF C_PRODUTO.IDTRATIMPO = w_V THEN
            IF (V_ORIGDESTOP = w_L) THEN
               V_IDDESCIMPRECCONT := 'N';
               BEGIN
                  SELECT NVL(IDDESCIMPRECCOFI, 'N')
                    INTO V_IDDESCIMPRECCONT
                    FROM TIPOOPERAC
                   WHERE CDTIPOOPER   = P_TIPOOPER
                     AND IDORIGDESTOP = w_L
                     AND IDENTRSAIDOP = w_S;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_IDDESCIMPRECCONT := 'N';
               END;
            END IF;
            IF (C_PRODUTO.IDIMPORECUP = w_S) AND (V_IDORIGDESTNF = w_L) AND (V_IDDESCIMPRECCONT = 'N')THEN
               IF (C_PRODUTO.CDCCTBLPASS IS NOT NULL) AND
                  (C_PRODUTO.CDFISCALIT = w_1) THEN
                  V_IMPOCONTAB := - V_VRIMPOSTO;
               END IF;
            END IF;
            IF C_PRODUTO.IDTIPCONTAB = w_D THEN
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                  UPDATE ITEMPRODUTO
                  SET VRCONTITEM = TRUNC((VRCONTITEM + V_IMPOCONTAB),2)
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               ELSE
                  UPDATE ITEMPRODUTO
                  SET VRCONTITEM = VRCONTITEM + V_IMPOCONTAB
                  WHERE CDFILIAL    = P_FILIAL   AND
                        NRLANCTONF  = P_LANCTONF AND
                        NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
               END IF;
            END IF;
         END IF;
      END LOOP;
      FOR C_ITEM IN (SELECT II.CDIMPOSTO,  I.NRSEQITPROD, II.NRITPROIMPOS
                  FROM ITEMPRODUTO I, ITEMPROIMPOS II
                  WHERE I.CDFILIAL    = P_FILIAL       AND
                        I.NRLANCTONF  = P_LANCTONF     AND
                        I.CDFILIAL    = II.CDFILIAL    AND
                        I.NRLANCTONF  = II.NRLANCTONF  AND
                        I.NRSEQITPROD = II.NRSEQITPROD ) LOOP
         V_ICMS := 'N';
         FOR C_ICMS IN (SELECT CDIMPOSTO
                          FROM IMPOSTONFE N, IMPOSTO I
                         WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                           AND N.CDIMPOSTONFE = '00'
                           AND I.CDIMPOSTO = C_ITEM.CDIMPOSTO)
         LOOP
            V_ICMS := 'S';
         END LOOP;
         IF (V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')  AND (V_ICMS = 'S') THEN
            UPDATE ITEMPROIMPOS
               SET VRCONTITIMP  = TRUNC(VRCONTITIMP,2),
                   VRBASECALCIT = TRUNC(VRBASECALCIT,2),
                   VRIMPOSTOIT  = TRUNC(VRIMPOSTOIT,2),
                   VRIMPOSTOVISUIT = TRUNC(VRIMPOSTOVISUIT,2)
             WHERE (CDFILIAL     = P_FILIAL) AND
                   (NRLANCTONF   = P_LANCTONF) AND
                   (CDIMPOSTO    = C_ITEM.CDIMPOSTO) AND
                   (NRSEQITPROD  = C_ITEM.NRSEQITPROD) AND
                   (NRITPROIMPOS = C_ITEM.NRITPROIMPOS);
         ELSE
           UPDATE ITEMPROIMPOS
               SET VRCONTITIMP  = ROUND(VRCONTITIMP,5),    
                   VRBASECALCIT = ROUND(VRBASECALCIT,2),
                   VRIMPOSTOIT  = ROUND(VRIMPOSTOIT,2),
                   VRIMPOSTOVISUIT = ROUND(VRIMPOSTOVISUIT,2)
             WHERE (CDFILIAL     = P_FILIAL) AND
                   (NRLANCTONF   = P_LANCTONF) AND
                   (CDIMPOSTO    = C_ITEM.CDIMPOSTO) AND
                   (NRSEQITPROD  = C_ITEM.NRSEQITPROD) AND
                   (NRITPROIMPOS = C_ITEM.NRITPROIMPOS);
         END IF;
      END LOOP;
   --FIM DA ATUALIZAÇÃO NA TABELA DE ITEMPROIMPOS
   --TOTALIZAÇÃO DOS IMPOSTOS
      DELETE IMPOSNFTOT
      WHERE (CDFILIAL = P_FILIAL) AND
            (NRLANCTONF = P_LANCTONF);
      FOR C_IMPOSNFTOT IN (SELECT IT.CDIMPOSTO
                           FROM ITEMPROIMPOS IT
                           WHERE (IT.CDFILIAL   = P_FILIAL) AND
                                 (IT.NRLANCTONF = P_LANCTONF)
                           GROUP BY IT.CDIMPOSTO) LOOP
         V_IMPOSTOIT  := 0;
         V_BASECALCIT := 0;
         SELECT NVL(SUM(IT.VRIMPOSTOIT),0), 
                NVL(SUM(IT.VRBASECALCIT),0), 
                NVL(SUM(IT.VRADICFECP),0)
         INTO   V_IMPOSTOIT, 
                V_BASECALCIT,
                V_VRADICFECPNFTOT
         FROM ITEMPROIMPOS IT, IMPOSTO I
         WHERE (IT.CDFILIAL   = P_FILIAL)               AND
               (IT.NRLANCTONF = P_LANCTONF)             AND
               (IT.CDIMPOSTO  = C_IMPOSNFTOT.CDIMPOSTO) AND
               (IT.CDIMPOSTO  = I.CDIMPOSTO);
         V_ICMS := 'N';
         FOR C_ICMS IN (SELECT CDIMPOSTO
                          FROM IMPOSTONFE N, IMPOSTO I
                         WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                           AND N.CDIMPOSTONFE = '00'
                           AND I.CDIMPOSTO = C_IMPOSNFTOT.CDIMPOSTO)
         LOOP
            V_ICMS := 'S';
         END LOOP;
         IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
            INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF,
                                   VRADICFECP)
                           VALUES (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, TRUNC(V_IMPOSTOIT ,V_NRDECITOTANF), TRUNC(V_BASECALCIT, V_NRDECITOTANF),
                                   V_VRADICFECPNFTOT);
         ELSE
            INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF,
                                   VRADICFECP)
                           VALUES (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, ROUND(V_IMPOSTOIT ,V_NRDECITOTANF), ROUND(V_BASECALCIT, V_NRDECITOTANF),
                                   V_VRADICFECPNFTOT);
         END IF;
      END LOOP;
   --INCLUSÃO DOS IMPOSTOS DE CORPO DE NOTA EM CASO DE NOTA FISCAL--------------------------------------------------------------------------
      V_SEQUIMPOSTNF := w_01;
      DELETE IMPOSTONF
      WHERE (CDFILIAL   = P_FILIAL) AND
            (NRLANCTONF = P_LANCTONF);
      CONSULTA_INCIDENCIA(w_S, P_EMPRESA, '', P_INSCESTA, P_TIPOOPER, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, NULL, V_IDINTEREST);
      FOR C_INCIDENCIA IN (SELECT DISTINCT (CDIMPOTEMP) AS CDIMPOTEMP, CDFISCTEMP, IDUTMUNIORIG,
                                  IDTRATIMPTMP, IDIMPORECUP, CDDETALHEPROD, IP.CDIMPOSTONFE
                           FROM INCITEMP IT, IMPOSTO IP
                           WHERE (IT.NRINCITEMP  = V_INCITEMP)       AND
                                 (IT.CDIMPOTEMP  = IP.CDIMPOSTO)     AND
                                 (IP.IDPRODNOTA  = w_C)) LOOP
         V_ALIQUTMP := 0;
         V_UTIBB    := 'N';
         V_SIMPLES  := 'N';
--integração fiscal especifica com Compass_Argentina atraves da importação de aliquota do imposto IBB
--A aliquota de imposto 03(ibb) sera cadastrada na tabela paramintegraibb e tera prioridade sobre o
-- cadastro de aliquota realizado em sistema
         /*VERIFICA CST POR IMPOSTO*/
         V_IMPOICMS := NULL;
         V_CDGRUPOFISCAUX := NULL;
         SELECT MAX(G.CDGRUPOFISC)
           INTO V_CDGRUPOFISCAUX
           FROM GRUPOFISCIMPFIL G,
                (SELECT CDFILIEMIT AS CDFILIAL
                   FROM NOTAFISCAL
                  WHERE CDFILIAL = P_FILIAL
                        AND NRLANCTONF = P_LANCTONF) F
          WHERE G.CDFILIAL = F.CDFILIAL
            AND NOT G.CDGRUPOFISC IS NULL
            AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
         IF V_IDORIGDESTNF = 'C' THEN
            SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
              INTO V_CDGRUPOFISCAUX
              FROM GRFISIMPIECLIE G,
                   (SELECT CDCLIENTE
                      FROM NOTAFISCAL
                     WHERE CDFILIAL = P_FILIAL
                       AND NRLANCTONF = P_LANCTONF) N
             WHERE G.CDCLIENTE = N.CDCLIENTE
               AND G.CDEMPRESA = P_EMPRESA
               AND G.CDINSCESTA = P_INSCESTA
               AND NOT G.CDGRUPOFISC IS NULL
               AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
         ELSE
            SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
              INTO V_CDGRUPOFISCAUX
              FROM GRFISIMPIEFORN G,
                  (SELECT CDFORNECED
                     FROM NOTAFISCAL
                    WHERE CDFILIAL = P_FILIAL
                      AND NRLANCTONF = P_LANCTONF) N
             WHERE G.CDFORNECED = N.CDFORNECED
               AND G.CDEMPRESA = P_EMPRESA
               AND G.CDINSCESTA = P_INSCESTA
               AND NOT G.CDGRUPOFISC IS NULL
               AND G.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP;
         END IF;
         IF V_CDGRUPOFISCAUX IS NULL THEN
            CONSULTA_CST_NFE(w_S, P_EMPRESA, '', P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISC,C_INCIDENCIA.CDIMPOTEMP, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);
         ELSE
            CONSULTA_CST_NFE(w_S, P_EMPRESA, '', P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISCAUX,C_INCIDENCIA.CDIMPOTEMP, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);
         END IF;
         V_CDSIMPLESNACIONAL := NULL;
         V_CDFAIXA := NULL;
         /*SIMPLES NACIONAL*/
         IF (V_CDSITUCRT <> '3') THEN
            CALCULA_ALIQUOTA_SN(P_EMPRESA, 
                                P_TIPOOPER, 
                                P_INIVALID, 
                                SUBSTR(V_FISOPEITEM, 2, 3), 
                                C_INCIDENCIA.CDIMPOSTONFE, 
                                V_VRSUBLIMITESN,
                                'N',
                                V_RBT12,
                                V_RBA,
                                V_CDSIMPLESNACIONAL,
                                V_CDFAIXA,
                                V_VRALIQSIMPLES);
            IF (V_CDFAIXA IS NOT NULL) OR ((C_INCIDENCIA.CDIMPOSTONFE IN ('00', '31')) AND (V_RBT12 >= 3600000)) THEN
               V_ALIQUTMP     := V_VRALIQSIMPLES;
               V_NRALIQTEMP   := 0;
               V_PARDEDTMP    := 0;
               V_PREDBCTMP    := 0;
               V_TRATIMPST    := w_0;
               V_PBCSUBTRIB   := 0;
               V_IDPMCBASECAL := 'N';
               V_SIMPLES      := 'S';
            END IF;
         END IF;
         IF C_INCIDENCIA.CDIMPOTEMP = '03' THEN
            IF V_IDORIGDESTNF = 'C' THEN
               V_ALIQUTMP := NULL;
               V_DTVINGENTE := NVL(V_DTINIREFEFAT, P_INIVALID);
               SELECT NVL(MAX(VRALIQUOTAIBB),NULL)
               INTO   V_ALIQUTMP
               FROM PARAMINTEGRAIBB
               WHERE CDCLIENTE = P_CLIENTE AND
                     V_DTVINGENTE BETWEEN DTVIGINICIALIBB AND DTVIGFINALIBB;
               IF V_ALIQUTMP IS NOT NULL THEN
                  V_NRALIQTEMP   := 0;
                  V_PARDEDTMP    := 0;
                  V_PREDBCTMP    := 0;
                  V_TRATIMPST    := w_0;
                  V_PBCSUBTRIB   := 0;
                  V_IDPMCBASECAL := 'N';
                  V_UTIBB        := 'S';
               END IF;
            END IF;
         END IF;
         IF (V_SIMPLES = 'N') THEN
            V_CDSIMPLESNACIONAL := NULL;
            V_CDFAIXA := NULL;
            IF (V_UTIBB = 'N')  THEN
               V_ALIQUTMP  := 0;
               V_PARDEDTMP := 0;
               V_PREDBCTMP := 0;
               V_ALIQUTMP  := 0;
               IF C_INCIDENCIA.IDUTMUNIORIG = w_D THEN
                  /* CONSIDERA O MUNICIPIO DE DESTINO */
                  CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, w_S, P_EMPRESA, '', P_INSCESTA, V_PAISORIG,
                                    V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER);
               ELSE
                  /* CONSIDERA O MUNICIPIO DE ORIGEM*/
                  CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, w_S, P_EMPRESA, '', P_INSCESTA, V_PAISORIG,
                                    V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER);
               END IF;
               SELECT MAX(NRALIQTEMP), MAX(VRPARDEDTMP),            MAX(VRPREDBCTMP),
                      MAX(VRALIQUTMP), NVL(MAX(IDTRIMPSTTMP), w_0), NVL(MAX(VRPBCSTTMP),0)
               INTO   V_NRALIQTEMP, V_PARDEDTMP, V_PREDBCTMP,
                      V_ALIQUTMP,   V_TRATIMPST, V_PBCSUBTRIB
               FROM ALIQTEMP
               WHERE (NRALIQTEMP = V_ALIQTEMP);
               IF (V_ALIQUTMP IS NULL) THEN
                  V_ALIQUTMP := 0;
               END IF;
            END IF;
         END IF;
         V_PRODUENCARGO := 0;
         SELECT SUM(VRITEMENCA) AS VRSOMAITEM
         INTO   V_SOMAITEM
         FROM ITEMENCARGO IE, ENCARGO EN, BASECALC BC
         WHERE (IE.CDFILIAL   = P_FILIAL)                AND
               (IE.NRLANCTONF = P_LANCTONF)              AND
               (EN.CDENCARGO  = IE.CDENCARGO)            AND
               (BC.CDENCARGO  = IE.CDENCARGO)            AND
               (BC.CDIMPOSTO  = C_INCIDENCIA.CDIMPOTEMP) AND
               (EN.IDENCARGO  = w_A);
         SELECT SUM(VRITEMENCA) AS VRDESCITEM
         INTO   V_DESCITEM
         FROM ITEMENCARGO IE, ENCARGO EN, BASECALC BC
         WHERE (IE.CDFILIAL   = P_FILIAL)                AND
               (IE.NRLANCTONF = P_LANCTONF)              AND
               (EN.CDENCARGO  = IE.CDENCARGO)            AND
               (BC.CDENCARGO  = IE.CDENCARGO)            AND
               (BC.CDIMPOSTO  = C_INCIDENCIA.CDIMPOTEMP) AND
               (EN.IDENCARGO  = w_D);
         IF V_SOMAITEM IS NULL THEN
            V_SOMAITEM := 0;
         END IF;
         IF V_DESCITEM IS NULL THEN
            V_DESCITEM := 0;
         END IF;
         IF (V_ALIQUTMP IS NULL) THEN --VERIFICAR VALOR DA ALÍQUOTA P/ IMPOSTO RECUPERÁVEL COM COD. FISCAL = 3
            V_ALIQUTMP := 0;
         END IF;
         IF (V_PREDBCTMP IS NULL) THEN
            V_PREDBCTMP := 0;
         END IF;
         IF (V_PARDEDTMP IS NULL) THEN
             V_PARDEDTMP := 0;
         END IF;
         V_PRODUENCARGO := V_SOMAITEM - V_DESCITEM;
         V_TAXABASECALC := (100 - V_PREDBCTMP)/100;
         V_TAXAALIQUOTA := (V_ALIQUTMP/100);
         V_TAXABCSUBSTR := 1 + (V_PBCSUBTRIB/100);
         V_BASECALCIT   := (V_LIQUIDO + V_PRODUENCARGO) * V_TAXABASECALC;
         V_PERDBCALC    := 100 - V_PREDBCTMP;
         IF V_PBCSUBTRIB > 0 THEN
            V_BASECALCIT := (V_BASECALCIT * V_TAXABCSUBSTR);
            V_PREDBCTMP  := V_PBCSUBTRIB;
            V_PERDBCALC  := 0;
         END IF;
         V_CONTIMPO     := V_BASECALCIT;
         V_IMPOSTOIT    := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
         IF V_BASECALCIT < 0 THEN
            V_BASECALCIT := 0;
         END IF;
         V_CONTABCALC := (V_LIQUIDO + V_PRODUENCARGO) - ((V_LIQUIDO + V_PRODUENCARGO) * V_TAXABASECALC);
         V_PERDBCALC  := 100 - V_PREDBCTMP;
         IF V_IMPOSTOIT < 0 THEN
            V_IMPOSTOIT := 0;
         END IF;
         V_ICMS := 'N';
         FOR C_ICMS IN (SELECT CDIMPOSTO
                          FROM IMPOSTONFE N, IMPOSTO I
                         WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                           AND N.CDIMPOSTONFE = '00'
                           AND I.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP)
         LOOP
            V_ICMS := 'S';
         END LOOP;
         IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
            INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO              , VRTOTIMPONF                              , VRTOTBCALCNF)
                           VALUES (P_FILIAL, P_LANCTONF, C_INCIDENCIA.CDIMPOTEMP, TRUNC(NVL(V_IMPOSTOIT,0), V_NRDECITOTANF), TRUNC(NVL(V_BASECALCIT,0),V_NRDECITOTANF));
         ELSE
            INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO              , VRTOTIMPONF                              , VRTOTBCALCNF)
                           VALUES (P_FILIAL, P_LANCTONF, C_INCIDENCIA.CDIMPOTEMP, ROUND(NVL(V_IMPOSTOIT,0), V_NRDECITOTANF), ROUND(NVL(V_BASECALCIT,0),V_NRDECITOTANF));
         END IF;
         IF (C_INCIDENCIA.IDIMPORECUP = w_S) OR (C_INCIDENCIA.IDIMPORECUP <> w_S AND V_ALIQUTMP <> 0) THEN
            IF (C_INCIDENCIA.IDIMPORECUP = w_S) AND
               (C_INCIDENCIA.CDFISCTEMP <> w_1) THEN
               V_BASECALCIT := 0;
               V_ALIQUTMP   := 0;
               V_IMPOSTOIT  := 0;
               V_PREDBCTMP  := 0;
               V_PARDEDTMP  := 0;
            END IF;
            V_ICMS := 'N';
            FOR C_ICMS IN (SELECT CDIMPOSTO
                             FROM IMPOSTONFE N, IMPOSTO I
                            WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                              AND N.CDIMPOSTONFE = '00'
                              AND I.CDIMPOSTO = C_INCIDENCIA.CDIMPOTEMP)
            LOOP
               V_ICMS := 'S';
            END LOOP;
            IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
               INSERT INTO IMPOSTONF(
                  CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                  VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, IDTRIMPOSTNF, CDSITUTRIBIMNFE,  CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
               VALUES(
                  P_FILIAL, P_LANCTONF, C_INCIDENCIA.CDIMPOTEMP, V_SEQUIMPOSTNF, TRUNC(V_CONTIMPO,2), V_PREDBCTMP,
                  TRUNC(V_BASECALCIT,2), V_ALIQUTMP, TRUNC(V_PARDEDTMP,2), TRUNC(V_IMPOSTOIT,2), C_INCIDENCIA.CDFISCTEMP, V_TRATIMPST, V_CDSITTRIBTMP, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);
            ELSE
               INSERT INTO IMPOSTONF(
                  CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                  VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, IDTRIMPOSTNF, CDSITUTRIBIMNFE, CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
               VALUES(
                  P_FILIAL, P_LANCTONF, C_INCIDENCIA.CDIMPOTEMP, V_SEQUIMPOSTNF, ROUND(V_CONTIMPO,2), V_PREDBCTMP,
                  ROUND(V_BASECALCIT,2), V_ALIQUTMP, ROUND(V_PARDEDTMP,2), ROUND(V_IMPOSTOIT,2), C_INCIDENCIA.CDFISCTEMP, V_TRATIMPST, V_CDSITTRIBTMP, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);
            END IF;
            V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
            IF (C_INCIDENCIA.IDIMPORECUP = w_S) AND
               (V_PREDBCTMP > 0) AND
               (C_INCIDENCIA.CDFISCTEMP = w_1) THEN
               INSERT INTO IMPOSTONF(
                  CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                  VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, CDSITUTRIBIMNFE,
				      CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
               VALUES(
                  P_FILIAL, P_LANCTONF, C_INCIDENCIA.CDIMPOTEMP, V_SEQUIMPOSTNF, V_CONTABCALC, V_PERDBCALC,
                  0, 0, V_PARDEDTMP, 0, w_2, V_CDSITTRIBTMP, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);
               V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
            END IF;
         END IF;
         DELETE ALIQTEMP
         WHERE NRALIQTEMP = V_ALIQTEMP;
      END LOOP;
      V_TOTIMPOCONT := 0;
      V_TOTIMPOBRUT := 0;
      /*CONTABILIZACAO DOS IMPOSTOS DE CORPO DE NOTA*/
      FOR C_IMPOSCONTAB IN (SELECT VRTOTIMPONF, IDIMPORECUP, CDCCTBLPASS, CDCCTBLATIV,
                                   IDTRATIMPO,  CDFISCTEMP, IDTIPCONTAB
                            FROM IMPOSNFTOT INF, IMPOSTO IP, INCITEMP IT
                            WHERE CDFILIAL      = P_FILIAL      AND
                                  NRLANCTONF    = P_LANCTONF    AND
                                  IP.CDIMPOSTO  = INF.CDIMPOSTO AND
                                  IT.CDIMPOTEMP = IP.CDIMPOSTO  AND
                                  IT.NRINCITEMP = V_INCITEMP    AND
                                  IP.IDPRODNOTA = w_C) LOOP
         IF (C_IMPOSCONTAB.IDTRATIMPO = w_A) THEN
            IF C_IMPOSCONTAB.IDTIPCONTAB = w_D THEN
               V_TOTIMPOCONT := V_TOTIMPOCONT + C_IMPOSCONTAB.VRTOTIMPONF;
            END IF;
            V_TOTIMPOBRUT := V_TOTIMPOBRUT + C_IMPOSCONTAB.VRTOTIMPONF;
         ELSIF (C_IMPOSCONTAB.IDTRATIMPO = w_D) THEN
            IF C_IMPOSCONTAB.IDTIPCONTAB = w_D THEN
               V_TOTIMPOCONT := V_TOTIMPOCONT - C_IMPOSCONTAB.VRTOTIMPONF;
            END IF;
            V_TOTIMPOBRUT := V_TOTIMPOBRUT - C_IMPOSCONTAB.VRTOTIMPONF;
         ELSIF (C_IMPOSCONTAB.IDTRATIMPO = w_R) THEN
            IF (C_IMPOSCONTAB.IDIMPORECUP = w_S) THEN
               IF (C_IMPOSCONTAB.CDCCTBLATIV IS NULL) OR
                  (C_IMPOSCONTAB.CDFISCTEMP <> w_1) AND (C_IMPOSCONTAB.IDTIPCONTAB = w_D) THEN
                  V_TOTIMPOCONT := V_TOTIMPOCONT - C_IMPOSCONTAB.VRTOTIMPONF;
               END IF;
            ELSE
               IF (C_IMPOSCONTAB.CDCCTBLATIV IS NULL) AND (C_IMPOSCONTAB.IDTIPCONTAB = w_D) THEN
                  V_TOTIMPOCONT := V_TOTIMPOCONT - C_IMPOSCONTAB.VRTOTIMPONF;
               END IF;
            END IF;
         END IF;
      END LOOP;
      IF (V_TOTIMPOBRUT <> 0) OR (V_TOTIMPOCONT <> 0) THEN
         FOR C_ITPRODUTO IN (SELECT NRSEQITPROD, (VRTOTAITEM - VRDESCITEM) AS VRLIQUIDO
                             FROM ITEMPRODUTO
                             WHERE CDFILIAL   = P_FILIAL AND
                                   NRLANCTONF = P_LANCTONF)LOOP
            IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
               UPDATE ITEMPRODUTO
               SET VRBRUTITEM = TRUNC(VRBRUTITEM + ((V_TOTIMPOBRUT / V_LIQUIDO) * C_ITPRODUTO.VRLIQUIDO),2),
                   VRCONTITEM = TRUNC(VRCONTITEM + ((V_TOTIMPOCONT / V_LIQUIDO) * C_ITPRODUTO.VRLIQUIDO),2)
               WHERE CDFILIAL    = P_FILIAL   AND
                     NRLANCTONF  = P_LANCTONF AND
                     NRSEQITPROD = C_ITPRODUTO.NRSEQITPROD;
            ELSE
               UPDATE ITEMPRODUTO
               SET VRBRUTITEM = ROUND(VRBRUTITEM + ((V_TOTIMPOBRUT / V_LIQUIDO) * C_ITPRODUTO.VRLIQUIDO),5),
                   VRCONTITEM = ROUND(VRCONTITEM + ((V_TOTIMPOCONT / V_LIQUIDO) * C_ITPRODUTO.VRLIQUIDO),5)
               WHERE CDFILIAL    = P_FILIAL   AND
                     NRLANCTONF  = P_LANCTONF AND
                     NRSEQITPROD = C_ITPRODUTO.NRSEQITPROD;
            END IF;
         END LOOP;
      END IF;
      DELETE INCITEMP
      WHERE NRINCITEMP = V_INCITEMP;
      SELECT SUM(NVL(VRBCICMSSTRETIDO,0)), SUM(NVL(VRICMSSTRETIDO,0))
        INTO V_BCICMSRET, V_ICMSRET
        FROM ITEMPROIMPOS
       WHERE CDFILIAL = P_FILIAL
         AND NRLANCTONF = P_LANCTONF;
      IF V_ICMSRET <> 0 THEN
         SELECT COUNT(*)
           INTO V_EXISTDSDADOSADIC
           FROM DADOSADIC
          WHERE CDFILIAL = P_FILIAL
            AND NRLANCTONF = P_LANCTONF
            AND DSDADOADIC LIKE 'ICMS retido anteriormente por substituição tributária: BC %';
         IF V_EXISTDSDADOSADIC = 0 THEN
            /*PREENCHE AS MENSAGENS ATRELADAS A INSCRICAO ESTADUAL*/
            INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                           VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, 'ICMS retido anteriormente por substituição tributária: BC '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_BCICMSRET, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_BCICMSRET, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_BCICMSRET, 2), '9999999999999.99')))|| ' Valor '|| DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_ICMSRET, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_ICMSRET, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_ICMSRET, 2), '9999999999999.99'))));
            V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
         ELSE
            UPDATE DADOSADIC
               SET DSDADOADIC = 'ICMS retido anteriormente por substituição tributária: BC '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_BCICMSRET, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_BCICMSRET, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_BCICMSRET, 2), '9999999999999.99')))|| ' Valor '|| DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_ICMSRET, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_ICMSRET, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_ICMSRET, 2), '9999999999999.99')))
             WHERE CDFILIAL = P_FILIAL
               AND NRLANCTONF = P_LANCTONF
               AND DSDADOADIC LIKE 'ICMS retido anteriormente por substituição tributária: BC %';
         END IF;
      END IF;
      FOR C_ITEMPRODUTO_DIFAL IN (SELECT NRSEQITPROD
                                    FROM ITEMPRODUTO
                                   WHERE CDFILIAL = P_FILIAL
                                     AND NRLANCTONF = P_LANCTONF) LOOP
         GERA_ICMS_INTERESTADUAL(P_FILIAL, P_LANCTONF, C_ITEMPRODUTO_DIFAL.NRSEQITPROD, P_INIVALID);
      END LOOP;
      SELECT MAX(NRDADOADIC)
        INTO V_DADOADIC
        FROM DADOSADIC
       WHERE (CDFILIAL = P_FILIAL) 
         AND (NRLANCTONF = P_LANCTONF);
      IF (V_DADOADIC IS NULL) THEN
         V_DADOADIC := W_001;
      ELSE
         V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
      END IF;
-- FIM DA TOTALIZAÇÃO DOS IMPOSTOS
-- NOTAS COM DOCUMENTO DE ORIGEM
   ELSIF (P_TIPOOPER IN ('07','10')) OR (V_IDOPERDEVONF = w_S) OR (V_ESTORNOCANC = w_S) THEN
      DELETE FROM ITEMPROIMPOS
      WHERE CDFILIAL   = P_FILIAL AND
            NRLANCTONF = P_LANCTONF;
      DELETE FROM ITEMPROIMPOSORIG
      WHERE CDFILIAL   = P_FILIAL AND
            NRLANCTONF = P_LANCTONF;
      V_VRTOTALIPI := 0;
      V_VRTOTALICMSST := 0;
      V_CDENCARGOICMST := NULL;
      V_CDENQLEGALIPI := NULL;
      V_CDCEST := NULL;
      SELECT NVL(MAX(IDGERICMSTDESPA), 'N'), MAX(CDENCARGOICMST), NVL(MAX(IDPEINFICMSTDEME), 'N'), NVL(MAX(IDGERIPIDESPA), 'N')
        INTO V_IDGERICMSTDESPA, V_CDENCARGOICMST, V_IDPEINFICMSTDEME, V_IDGERIPIDESPA
        FROM TIPOOPERAC
       WHERE IDENTRSAIDOP = 'S'
         AND CDTIPOOPER = P_TIPOOPER;
      SELECT MAX(IDUTCTRLFISSAID)
        INTO V_IDUTCTRLFISSAID
        FROM PARAMGERAL;
      SELECT MAX(IDUTCTRLFISSAID)
        INTO V_IDUTCTRLFISFIL
        FROM PARAMFILIAL
       WHERE CDFILIAL = V_CDFILIEMIT;
      IF V_IDUTCTRLFISSAID = 'S' THEN
         V_IDUTCTRLFIS := 'S';
      ELSE
         IF V_IDUTCTRLFISFIL = 'S' THEN
            V_IDUTCTRLFIS := 'S';
         ELSE
            V_IDUTCTRLFIS := 'N';
         END IF;
      END IF;
      V_UPDENCIMPXML := 'N';
      FOR C_ITEMPROD IN (SELECT IT.*, P.CDARVPROD
                         FROM ITEMPRODUTO IT, PRODUTO P
                         WHERE IT.CDFILIAL   = P_FILIAL   AND
                               IT.NRLANCTONF = P_LANCTONF AND
                               IT.CDPRODUTO  = P.CDPRODUTO) LOOP
         V_LIQUIITEM := C_ITEMPROD.VRTOTAITEM - C_ITEMPROD.VRDESCITEM;
         CONSULTA_CST(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, V_IDINTEREST, V_IDINTERNA, NULL, C_ITEMPROD.IDORIGEMPROD);
         SELECT MAX(CDSITTRIBTMP)
         INTO   V_SITUTRIBPR
         FROM CSTTEMP
         WHERE (NRCSTTEMP = V_CSTTEMP);
         IF (V_IDOPERDEVONF <> w_S) THEN
            FOR C_CSTTEMP IN (SELECT DSMENSCSTTMP
                              FROM CSTTEMP
                              WHERE (NRCSTTEMP = V_CSTTEMP) AND
                                    (DSMENSCSTTMP IS NOT NULL) AND
                                    (NOT EXISTS(SELECT DSDADOADIC
                                                FROM DADOSADIC
                                                WHERE (CDFILIAL   = P_FILIAL)   AND
                                                      (NRLANCTONF = P_LANCTONF) AND
                                                      (DSDADOADIC LIKE CSTTEMP.DSMENSCSTTMP || '%')))) LOOP
               IF NOT (C_CSTTEMP.DSMENSCSTTMP IS NULL) THEN
                  /*PREENCHE AS MENSAGENS ATRELADAS AO CST*/
                  INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                                 VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, C_CSTTEMP.DSMENSCSTTMP);
                  V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
               END IF;
            END LOOP;
         END IF;
         CONSULTA_CFOP(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_PAISORIG,
                        V_ESTAORIG, V_PAISCFOP, V_ESTACFOP, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_INIVALID, C_ITEMPROD.IDORIGEMPROD);
         FOR C_CFOPTEMP IN (SELECT DSMENSCFOTMP
                            FROM CFOPTEMP
                            WHERE (NRCFOPTEMP = V_CFOPTEMP) AND
                                  (DSMENSCFOTMP IS NOT NULL) AND
                                  (NOT EXISTS(SELECT DSDADOADIC
                                              FROM DADOSADIC
                                              WHERE (CDFILIAL   = P_FILIAL)   AND
                                                    (NRLANCTONF = P_LANCTONF) AND
                                                    (DSDADOADIC LIKE CFOPTEMP.DSMENSCFOTMP || '%')))) LOOP
            IF NOT(C_CFOPTEMP.DSMENSCFOTMP IS NULL) THEN
               /*PREENCHE AS MENSAGENS ATRELADAS AO CFOP*/
               INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                              VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, C_CFOPTEMP.DSMENSCFOTMP);
               V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
            END IF;
         END LOOP;
         IF (NVL(V_CFOPINFUSU, 'N') <> 'S') AND (V_CDFISCOPERREGRA IS NOT NULL) THEN
            V_DSCDFISOPEITREGRA := SUBSTR(DESCRICAO_CFOP(V_CDFISCOPERREGRA),1,45);
         END IF;
         IF (V_DSCDFISOPEITREGRA IS NOT NULL) THEN
            UPDATE NOTAFISCAL
               SET DSCDFISOPENF = V_DSCDFISOPEITREGRA
             WHERE CDFILIAL   = P_FILIAL   AND
                   NRLANCTONF = P_LANCTONF;
         ELSE
            UPDATE NOTAFISCAL
               SET DSCDFISOPENF = (SELECT MAX(DSCFOPTMP)
                                     FROM CFOPTEMP
                                    WHERE NRCFOPTEMP = V_CFOPTEMP)
             WHERE CDFILIAL   = P_FILIAL
               AND NRLANCTONF = P_LANCTONF;
         END IF;
         SELECT MAX(CDFISOPETMP), MAX(CDSUFCFOPTMP),SUBSTR(MAX(DSCFOPTMP),1,50)
         INTO   V_FISOPEITEM,     V_SUFXCFOPPR,     V_DSCDFISOPEIT
         FROM CFOPTEMP
         WHERE NRCFOPTEMP = V_CFOPTEMP;
         CONSULTA_CSOSN('S', P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC,V_IDINTEREST, V_CDFISCOPERSN);
         IF (NVL(V_CFOPINFUSU, 'N') <> 'S') AND (V_CDFISCOPERREGRA IS NOT NULL) THEN
            V_FISOPEITEM := NVL(V_CDFISCOPERREGRA, V_FISOPEITEM);
            V_DSCDFISOPEITREGRA := SUBSTR(DESCRICAO_CFOP(V_CDFISCOPERREGRA),1,45);
         END IF;
         UPDATE ITEMPRODUTO
         SET CDFISOPEITEM = V_FISOPEITEM,
             CDSUFXCFOPPR = V_SUFXCFOPPR,
             CDSITUTRIBPR  = NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR),
             DSCDFISOPEIT = V_DSCDFISOPEIT,
             CDFISCOPERSN = V_CDFISCOPERSN
         WHERE CDFILIAL    = P_FILIAL     AND
               NRLANCTONF  = P_LANCTONF   AND
               NRSEQITPROD = C_ITEMPROD.NRSEQITPROD;
         IF V_IDOPERDEVONF = w_S THEN
            FOR C_ITEMNOTAFISC IN (SELECT CDFILIALIT, NRLANCTONFIT
                                   FROM ITEMNOTAFISC
                                   WHERE CDFILIAL   = P_FILIAL AND
                                         NRLANCTONF = P_LANCTONF) LOOP
               IF V_IDDEVOLIST = w_S THEN /*SE A NOTA POSSUI VARIOS DOCUMENTOS DE ORIGEM*/
                  SELECT SUM(I.QTITEMPROD)
                    INTO V_SOMAQITEMPROD
                    FROM ITEMPRODUTO I
                   WHERE CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT                                               AND
                         NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT                                             AND
                         CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                                    AND
                         ROUND(VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF)      AND
                         (NVL(NRLOTEESTQ, ' ')  = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                           AND
                         (NVL(NRSUBLOTE , ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')));
                  SELECT NVL(MAX(C_ITEMPROD.QTITEMPROD / V_SOMAQITEMPROD),1) AS VRFATOCONV,
                         NVL(MAX(VRBRUTITEM),0), NVL(MAX(VRCONTITEM),0)
                  INTO   V_VRFATOCONV, V_VRBRUTO, V_VRCONTAB
                  FROM ITEMPRODUTO I
                  WHERE CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT                                               AND
                        NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT                                             AND
                        CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                                    AND
                        ROUND(VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF)      AND
                        (NVL(NRLOTEESTQ, ' ')  = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                           AND
                        (NVL(NRSUBLOTE , ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')));
                  IF V_CDINTEGRACAO IS NOT NULL THEN
                     IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                        UPDATE ITEMPRODUTO
                        SET VRCONTITEM = TRUNC((V_VRCONTAB * V_VRFATOCONV),2),
                            VRBRUTITEM = TRUNC((V_VRBRUTO * V_VRFATOCONV),2)
                        WHERE CDFILIAL    = P_FILIAL                                                          AND
                              NRLANCTONF  = P_LANCTONF                                                        AND
                              CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                              AND
                             ROUND(VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF) AND
                             (NVL(NRLOTEESTQ, ' ')  = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                      AND
                             (NVL(NRSUBLOTE , ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')));
                     ELSE
                        UPDATE ITEMPRODUTO
                        SET VRCONTITEM = ROUND(V_VRCONTAB * V_VRFATOCONV,5),
                            VRBRUTITEM = ROUND(V_VRBRUTO * V_VRFATOCONV,5)
                        WHERE CDFILIAL    = P_FILIAL                                                          AND
                              NRLANCTONF  = P_LANCTONF                                                        AND
                              CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                              AND
                              VRUNITITEM  = C_ITEMPROD.VRUNITITEM                                             AND
                             (NVL(NRLOTEESTQ, ' ')  = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                      AND
                             (NVL(NRSUBLOTE , ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')));
                     END IF;
                  END IF;
                  /*IMPOSTOS POR ITEM DA NOTA DE ORIGEM*/
                  FOR C_ITEMPROIMPOS IN (SELECT VRPERREDBCIT, NRITPROIMPOS, IT.CDIMPOSTO, CDFISCALIT,
                                                VRALIQITEM, VRPARDEDIT, SUM(VRCONTITIMP) AS VRCONTITIMP,
                                                SUM(VRBASECALCIT) AS VRBASECALCIT, SUM(VRIMPOSTOIT) AS VRIMPOSTOIT,
                                                CDSITUTRIBITNFE AS CDSITUTRIBNFE, IDMODBCITNFE AS IDMODBCNFE, IDMODBCSTITNFE AS IDMODBCSTNFE,
                                                CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII, NVL(IDDIFALIQCUSPRI, 'N') AS IDDIFALIQCUSPRI,
                                                IM.CDIMPOSTONFE, IT.CDENQLEGALIPI, IT.CDCEST, IT.CDBENEFFISCAL
                                          FROM ITEMPROIMPOS IT, ITEMPRODUTO I, IMPOSTO IM
                                         WHERE IT.CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT                                         AND
                                               IT.NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT                                       AND
                                               I.CDFILIAL     = C_ITEMNOTAFISC.CDFILIALIT                                         AND
                                               I.NRLANCTONF   = C_ITEMNOTAFISC.NRLANCTONFIT                                       AND
                                               IT.NRSEQITPROD = I.NRSEQITPROD                                                     AND
                                               IT.CDIMPOSTO   = IM.CDIMPOSTO                                                      AND
                                               I.CDPRODUTO = C_ITEMPROD.CDPRODUTO                                                 AND
                                               IT.NRITPROIMPOS = '1'                                                              AND
                                               ROUND(I.VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF) AND
                                               (NVL(I.NRLOTEESTQ, ' ') = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                       AND
                                               (NVL(I.NRSUBLOTE, ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE,  ' ')))
                                         GROUP BY VRPERREDBCIT, IT.CDIMPOSTO, NRITPROIMPOS, CDFISCALIT, VRALIQITEM,
                                                  VRPARDEDIT,   CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE,
                                                  CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII, IDDIFALIQCUSPRI,
                                                  IM.CDIMPOSTONFE, IT.CDENQLEGALIPI, IT.CDCEST
                                         ORDER BY CDIMPOSTO) LOOP
                     V_CDBENEFFISCAL := NULL;
                     CONSULTA_INCIDENCIA(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC,
                                        NULL, V_IDINTEREST, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPROD.IDORIGEMPROD);
                     IF V_CDSITTRIBTMP IS NULL THEN
                        V_CDSITTRIBTMP := SUBSTR(V_SITUTRIBPR, 2, 2);
                     END IF;
                     BEGIN
                        SELECT MAX(CDFISCTEMP), MAX(CDDETALHEPROD), MAX(CDENQLEGALIPI), MAX(CDCEST)
                          INTO V_CDFISCTEMP, V_CDDETALHEPRODTEMP, V_CDENQLEGALIPI, V_CDCEST
                          FROM INCITEMP IT, IMPOSTO IP
                        WHERE IT.NRINCITEMP = V_INCITEMP
                          AND IP.CDIMPOSTO  = IT.CDIMPOTEMP
                          AND IP.IDPRODNOTA = w_P
                          AND IT.CDIMPOTEMP = C_ITEMPROIMPOS.CDIMPOSTO;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_CDFISCTEMP        := NULL;
                           V_CDDETALHEPRODTEMP := NULL;
                           V_CDENQLEGALIPI     := NULL;
                           V_CDCEST            := NULL;
                     END;
                     IF (C_ITEMPROIMPOS.VRPERREDBCIT > 0.00) AND
                        (C_ITEMPROIMPOS.NRITPROIMPOS = w_2) THEN
                        V_CDFISCTEMP := w_2;
                     ELSE
                        IF (V_CDFISCTEMP IS NULL) THEN
                            V_CDFISCTEMP := C_ITEMPROIMPOS.CDFISCALIT;
                        END IF;
                     END IF;
                     IF V_CDENQLEGALIPI IS NULL THEN
                        V_CDENQLEGALIPI := C_ITEMPROIMPOS.CDENQLEGALIPI;
                     END IF;
                     IF V_CDCEST IS NULL THEN
                        V_CDCEST := C_ITEMPROIMPOS.CDCEST;
                     END IF;
                     DELETE INCITEMP
                      WHERE NRINCITEMP = V_INCITEMP;
                     /*VERIFICA CST POR IMPOSTO*/
                     V_IMPOICMS := NULL;
                     V_CDGRUPOFISCAUX := NULL;
                     SELECT MAX(G.CDGRUPOFISC)
                       INTO V_CDGRUPOFISCAUX
                       FROM GRUPOFISCIMPFIL G,
                            (SELECT CDFILIEMIT AS CDFILIAL
                               FROM NOTAFISCAL
                              WHERE CDFILIAL = P_FILIAL
                                    AND NRLANCTONF = P_LANCTONF) F
                      WHERE G.CDFILIAL = F.CDFILIAL
                        AND NOT G.CDGRUPOFISC IS NULL
                        AND G.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                     IF V_IDORIGDESTNF = 'C' THEN
                        SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                          INTO V_CDGRUPOFISCAUX
                          FROM GRFISIMPIECLIE G,
                               (SELECT CDCLIENTE
                                  FROM NOTAFISCAL
                                 WHERE CDFILIAL = P_FILIAL
                                   AND NRLANCTONF = P_LANCTONF) N
                         WHERE G.CDCLIENTE = N.CDCLIENTE
                           AND G.CDEMPRESA = P_EMPRESA
                           AND G.CDINSCESTA = P_INSCESTA
                           AND NOT G.CDGRUPOFISC IS NULL
                           AND G.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                     ELSE
                        SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                          INTO V_CDGRUPOFISCAUX
                          FROM GRFISIMPIEFORN G,
                              (SELECT CDFORNECED
                                 FROM NOTAFISCAL
                                WHERE CDFILIAL = P_FILIAL
                                  AND NRLANCTONF = P_LANCTONF) N
                         WHERE G.CDFORNECED = N.CDFORNECED
                           AND G.CDEMPRESA = P_EMPRESA
                           AND G.CDINSCESTA = P_INSCESTA
                           AND NOT G.CDGRUPOFISC IS NULL
                           AND G.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                     END IF;
                     IF V_CDGRUPOFISCAUX IS NULL THEN
                        CONSULTA_CST_NFE(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISC,C_ITEMPROIMPOS.CDIMPOSTO, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, C_ITEMPROD.IDORIGEMPROD);
                     ELSE
                        CONSULTA_CST_NFE(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISCAUX,C_ITEMPROIMPOS.CDIMPOSTO, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, C_ITEMPROD.IDORIGEMPROD);
                     END IF;
                     IF (V_CDSITTRIBTMP IS NULL) THEN
                        V_CDSITUTRIBNFE := C_ITEMPROIMPOS.CDSITUTRIBNFE;
                     ELSE
                        V_CDSITUTRIBNFE := V_CDSITTRIBTMP;
                     END IF;
                     IF (V_IDMODBCNFE IS NULL) THEN
                        V_IDMODBCNFEIT := C_ITEMPROIMPOS.IDMODBCNFE;
                     ELSE
                        V_IDMODBCNFEIT := V_IDMODBCNFE;
                     END IF;
                     IF (V_IDMODBCSTNFE IS NULL) THEN
                        V_IDMODBCSTNFEIT := C_ITEMPROIMPOS.IDMODBCSTNFE;
                     ELSE
                        V_IDMODBCSTNFEIT := V_IDMODBCSTNFE;
                     END IF;
                     IF (C_ITEMPROIMPOS.CDTPCREDPICOITE IS NULL) THEN
                        V_CDTPCRPICOSTNFEIT := V_CDTPCRPICOSTNFE;
                     ELSE
                        V_CDTPCRPICOSTNFEIT := C_ITEMPROIMPOS.CDTPCREDPICOITE;
                     END IF;
                     IF (C_ITEMPROIMPOS.CDNATUBCALPCIT IS NULL) THEN
                        V_CDNATUBCPCSTNFEIT := V_CDNATUBCPCSTNFE;
                     ELSE
                        V_CDNATUBCPCSTNFEIT := C_ITEMPROIMPOS.CDNATUBCALPCIT;
                     END IF;
                     IF (V_IDMOTIDESSTNFE IS NULL) THEN
                        V_IDMOTIDESSTNFE := C_ITEMPROIMPOS.IDMOTDESONERAIT;
                     ELSE
                        V_IDMOTIDESSTNFE := V_IDMOTIDESSTNFE;
                     END IF;
                     SELECT NVL(MAX(IDUTMUNIORIG),'N')
                       INTO V_IDUTMUNIORIG
                       FROM IMPOSTO
                      WHERE CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                     IF ((V_IDUTMUNIORIG = 'O') OR (V_IDUTMUNIORIG = 'N')) THEN
                        CONSULTA_ALIQUOTA(C_ITEMPROIMPOS.CDIMPOSTO, w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, V_PAISORIG,
                                          V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                          NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPROD.IDORIGEMPROD);
                     ELSE
                        CONSULTA_ALIQUOTA(C_ITEMPROIMPOS.CDIMPOSTO, w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, V_PAISORIG,
                                          V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                          NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPROD.IDORIGEMPROD);
                     END IF;
                     V_NRALIQTEMP := 0;
                     V_PARDEDTMP  := 0;
                     V_PREDBCTMP  := 0;
                     V_ALIQUTMP   := 0;
                     V_IMPOSTOIT  := 0;
                     V_CDTIPOOPER    := NULL;
                     V_CDREGRAITALIQ := NULL;
                     SELECT NVL(MAX(NRALIQTEMP),0),      NVL(MAX(VRPARDEDTMP),0), NVL(MAX(VRPREDBCTMP),0), NVL(MAX(VRALIQUTMP),0),
                            NVL(MAX(IDTRIMPSTTMP), w_0), NVL(MAX(VRPBCSTTMP),0),  NVL(MAX(IDPMCBSCALTMP), w_S),
                            MAX(CDTIPOOPERTMP),          MAX(CDREGRATMP),
                            MAX(CDFISCOPER),             MAX(IDDIFEALIQ),         MAX(IDDIFALIQCUSPR),
                            MAX(CDBENEFFISCAL)
                     INTO   V_NRALIQTEMP,                V_PARDEDTMP,             V_PREDBCTMP,             V_ALIQUTMP,
                            V_TRATIMPST,                 V_PBCSUBTRIB,            V_IDPMCBASECAL,
                            V_CDTIPOOPER,                V_CDREGRAITALIQ,
                            V_CDFISCOPERREGRA,           V_IDDIFEALIQREGRA,       V_IDDIFALIQCUSPRREGRA,
                            V_CDBENEFFISCAL
                     FROM ALIQTEMP
                     WHERE (NRALIQTEMP = V_ALIQTEMP);
                     IF V_CDBENEFFISCAL IS NULL THEN
                        V_CDBENEFFISCAL := C_ITEMPROIMPOS.CDBENEFFISCAL;
                     END IF;
                     /*Verifica alíquota parametrizada por tipo de operação desde que não seja nota de estorno de cancelamento*/
                     IF V_PAISORIG = V_PAISCFOP THEN
                        IF V_ESTAORIG = V_ESTACFOP THEN
                           V_CFOP1DIGT := '5';
                        ELSE
                           V_CFOP1DIGT := '6';
                        END IF;
                     ELSE
                        V_CFOP1DIGT := '7';
                     END IF;
                     IF V_CDFISCOPERREGRA IS NOT NULL THEN
                        IF (LENGTH(V_CDFISCOPERREGRA) < 4) THEN
                           V_CDFISCOPERREGRA := V_CFOP1DIGT||V_CDFISCOPERREGRA;
                        END IF;
                     END IF;
                     IF ((V_NRALIQTEMP IS NOT NULL) AND (V_CDTIPOOPER IS NOT NULL) AND (V_ESTORNOCANC <> w_S))THEN
                        IF /*(C_ITEMPROIMPOS.VRPERREDBCIT > 0.00) AND*/
                           (C_ITEMPROIMPOS.NRITPROIMPOS = w_2) THEN
                           V_CDIMPOSTRIB  := null;
                           V_IDVRIMPSTRIB := null;
                        ELSE
                           V_CDIMPOSTRIB  := null;
                           V_IDVRIMPSTRIB := null;
                           /*IMPOSTO DE SUBSTITUICAO TRIBUTARIA*/
                           FOR C_ICMSST IN (SELECT CDIMPOSTO
                                              FROM IMPOSTO
                                             WHERE CDIMPOSTRIB = C_ITEMPROIMPOS.CDIMPOSTO)
                           LOOP
                              V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
                           END LOOP;
                           V_TAXABASECALC := (100 - V_PREDBCTMP)/100;
                           V_TAXAALIQUOTA := (V_ALIQUTMP/100);
                           V_TAXABCSUBSTR := 1 + (V_PBCSUBTRIB/100);
                           V_BASECALCIT   := V_LIQUIITEM * V_TAXABASECALC;
                           V_PERDBCALC    := 100 - V_PREDBCTMP;
                           IF V_PBCSUBTRIB > 0 THEN
                              V_BASECALCIT := (V_BASECALCIT * V_TAXABCSUBSTR);
                              V_PREDBCTMP  := V_PBCSUBTRIB;
                              V_PERDBCALC  := 0;
                           END IF;
                           V_BASECALCTO  := V_BASECALCIT;
                           V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
                           IF V_CDIMPOSTRIB IS NULL THEN
                              V_IDTRIMPSTII := '';
                           ELSE
                              IF SUBSTR(V_SITUTRIBPR, 2, 2) = '60' THEN
                                 V_IDTRIMPSTII := w_2;
                              ELSE 
                                 V_IDTRIMPSTII := V_TRATIMPST;
                              END IF;
                           END IF;
                           V_CONTABCALC  := V_LIQUIITEM - (V_LIQUIITEM * V_TAXABASECALC);
                           IF V_CDIMPOSTRIB IS NOT NULL THEN
                              SELECT NVL(MAX(IDVRIMPSTRIB), 'N')
                                INTO V_IDVRIMPSTRIB
                                FROM ITEMPROIMPOS I, IMPOSTO II
                               WHERE (CDFILIAL     = P_FILIAL)
                                 AND (NRLANCTONF   = P_LANCTONF)
                                 AND (NRSEQITPROD  = C_ITEMPROD.NRSEQITPROD)
                                 AND(INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0)
                                 AND (I.CDIMPOSTO = II.CDIMPOSTO);
                              IF (V_TRATIMPST = w_1) OR (NVL(V_IDVRIMPSTRIB,w_N) = w_N) THEN
                                 V_VRIMPOSTOST := 0;
                              ELSE
                                 Begin
                                    SELECT NVL(MAX(VRIMPOSTOIT),0)
                                      INTO   V_VRIMPOSTOST
                                      FROM ITEMPROIMPOS I
                                     WHERE (CDFILIAL     = P_FILIAL)
                                       AND (NRLANCTONF   = P_LANCTONF)
                                       AND (NRSEQITPROD  = C_ITEMPROD.NRSEQITPROD)
                                       AND (INSTR(V_CDIMPOSTRIB,CDIMPOSTO||'_') > 0)
                                       AND (NRITPROIMPOS = w_1);
                                 Exception
                                    When No_Data_Found Then
                                       V_VRIMPOSTOST := 0;
                                 End;
                              END IF;
                              V_IMPOSTOIT := V_IMPOSTOIT - V_VRIMPOSTOST;
                              IF V_IMPOSTOIT < 0 THEN
                                 V_IMPOSTOIT := 0;
                              END IF;
                           END IF;
                           IF (V_ALIQUTMP = 0) AND (V_IMPOSTOIT = 0) THEN
                              V_BASECALCTO := 0;
                           END IF;
                           Begin
                             SELECT CDFILIAL
                             INTO   V_CHAVE
                             FROM ITEMPROIMPOS
                             WHERE CDFILIAL     = P_FILIAL                 AND
                                   NRLANCTONF   = P_LANCTONF               AND
                                   NRSEQITPROD  = C_ITEMPROD.NRSEQITPROD   AND
                                   CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO AND
                                   NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS;
                          Exception
                             When No_Data_Found Then
                                V_CHAVE := Null;
                          End;
                          IF V_CHAVE IS NULL THEN
                             V_ICMS := 'N';
                             FOR C_ICMS IN (SELECT CDIMPOSTO
                                              FROM IMPOSTONFE N, IMPOSTO I
                                             WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                               AND N.CDIMPOSTONFE = '00'
                                               AND I.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO)
                             LOOP
                                V_ICMS := 'S';
                             END LOOP;
                             IF (V_ICMS = 'S') THEN
                                V_CDGRUPOFISCAUX := NULL;
                                SELECT MAX(G.CDGRUPOFISC)
                                  INTO V_CDGRUPOFISCAUX
                                  FROM GRUPOFISCIMPFIL G,
                                       (SELECT CDFILIEMIT AS CDFILIAL
                                          FROM NOTAFISCAL
                                         WHERE CDFILIAL = P_FILIAL
                                               AND NRLANCTONF = P_LANCTONF) F
                                 WHERE G.CDFILIAL = F.CDFILIAL
                                   AND NOT G.CDGRUPOFISC IS NULL
                                   AND G.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                                IF V_IDORIGDESTNF = 'C' THEN
                                   SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                                     INTO V_CDGRUPOFISCAUX
                                     FROM GRFISIMPIECLIE G,
                                          (SELECT CDCLIENTE
                                             FROM NOTAFISCAL
                                            WHERE CDFILIAL = P_FILIAL
                                              AND NRLANCTONF = P_LANCTONF) N
                                    WHERE G.CDCLIENTE = N.CDCLIENTE
                                      AND G.CDEMPRESA = P_EMPRESA
                                      AND G.CDINSCESTA = P_INSCESTA
                                      AND NOT G.CDGRUPOFISC IS NULL
                                      AND G.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                                ELSE
                                   SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                                     INTO V_CDGRUPOFISCAUX
                                     FROM GRFISIMPIEFORN G,
                                         (SELECT CDFORNECED
                                            FROM NOTAFISCAL
                                           WHERE CDFILIAL = P_FILIAL
                                             AND NRLANCTONF = P_LANCTONF) N
                                    WHERE G.CDFORNECED = N.CDFORNECED
                                      AND G.CDEMPRESA = P_EMPRESA
                                      AND G.CDINSCESTA = P_INSCESTA
                                      AND NOT G.CDGRUPOFISC IS NULL
                                      AND G.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                                END IF;
                                IF (V_CDGRUPOFISCAUX IS NOT NULL) THEN
                                   CONSULTA_CFOP(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_PAISORIG,
                                                 V_ESTAORIG, V_PAISCFOP, V_ESTACFOP, P_FILIAL, P_LANCTONF, V_CDGRUPOFISCAUX, P_INIVALID, C_ITEMPROD.IDORIGEMPROD);
                                ELSE
                                   CONSULTA_CFOP(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_PAISORIG,
                                                 V_ESTAORIG, V_PAISCFOP, V_ESTACFOP, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_INIVALID, C_ITEMPROD.IDORIGEMPROD);
                                END IF;
                                IF (NVL(V_CFOPINFUSU, 'N') <> 'S') AND (V_CDFISCOPERREGRA IS NOT NULL) THEN
                                   V_FISOPEITEM        := NVL(V_CDFISCOPERREGRA, V_FISOPEITEM);
                                   V_DSCDFISOPEITREGRA := SUBSTR(DESCRICAO_CFOP(V_CDFISCOPERREGRA),1,45);
                                END IF;
                                UPDATE NOTAFISCAL
                                   SET DSCDFISOPENF = NVL(V_DSCDFISOPEITREGRA, (SELECT MAX(DSCFOPTMP)
                                                                                    FROM CFOPTEMP
                                                                                   WHERE NRCFOPTEMP = V_CFOPTEMP))
                                 WHERE CDFILIAL   = P_FILIAL   AND
                                       NRLANCTONF = P_LANCTONF;
                                SELECT MAX(CDFISOPETMP), MAX(CDSUFCFOPTMP), SUBSTR(MAX(DSCFOPTMP),1,50)
                                  INTO V_FISOPEITEM    , V_SUFXCFOPPR, V_DSCDFISOPEIT
                                  FROM CFOPTEMP
                                 WHERE NRCFOPTEMP = V_CFOPTEMP;
                                IF (NVL(V_CFOPINFUSU, 'N') <> 'S') AND (V_CDFISCOPERREGRA IS NOT NULL) THEN
                                   V_FISOPEITEM        := NVL(V_CDFISCOPERREGRA, V_FISOPEITEM);
                                   V_DSCDFISOPEITREGRA := SUBSTR(DESCRICAO_CFOP(V_CDFISCOPERREGRA),1,45);
                                END IF;
                                UPDATE ITEMPRODUTO
                                   SET CDFISOPEITEM  = V_FISOPEITEM,
                                       CDSUFXCFOPPR  = V_SUFXCFOPPR,
                                       DSCDFISOPEIT  = V_DSCDFISOPEIT
                                 WHERE CDFILIAL    = P_FILIAL AND
                                       NRLANCTONF  = P_LANCTONF AND
                                       NRSEQITPROD = C_ITEMPROD.NRSEQITPROD;
                                DELETE CFOPTEMP
                                 WHERE NRCFOPTEMP = V_CFOPTEMP;
                                Begin
                                   SELECT MAX(CDCSTDIG1PROD)
                                     INTO V_CST1DIGT
                                     FROM PRODUTO
                                    WHERE (CDARVPROD = C_ITEMPROD.CDARVPROD);
                                   IF V_CST1DIGT IS NULL THEN
                                      SELECT MAX(CDCSTDIG1SAI)
                                        INTO   V_CST1DIGT
                                        FROM INSCRESTAD
                                       WHERE (CDEMPRESA  = P_EMPRESA) AND
                                             (CDINSCESTA = P_INSCESTA);
                                   END IF;
                                Exception
                                   When No_Data_Found Then
                                      V_CST1DIGT := Null;
                                End;
                                IF (V_IDUTORIGPROD = 'S') AND (C_ITEMPROD.IDORIGEMPROD = 'E') THEN
                                   V_CST1DIGT := '2';
                                END IF;
                                UPDATE ITEMPRODUTO
                                   SET CDSITUTRIBPR  = V_CST1DIGT||NVL(V_CDSITTRIBTMP, SUBSTR(V_SITUTRIBPR, 2,2))
                                 WHERE CDFILIAL    = P_FILIAL AND
                                       NRLANCTONF  = P_LANCTONF AND
                                       NRSEQITPROD = C_ITEMPROD.NRSEQITPROD;
                                V_SITUTRIBPR := V_CST1DIGT||NVL(V_CDSITTRIBTMP, SUBSTR(V_SITUTRIBPR, 2,2));
                             END IF;
                             SELECT NVL(MAX(IDPERVISIMPSCRE), 'S')
                               INTO V_IDPERVISIMPSCRE
                               FROM PARAMGERAL;
                             IF V_IDPERVISIMPSCRE = 'S' THEN
                                V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
                             ELSE
                                IF (V_CDFISCTEMP = '1') THEN
                                   V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
                                ELSE
                                   V_VRIMPOSTOVISUIT := 0;
                                END IF;
                             END IF;
                             /* ROTINA PARA CALCULAR O DIFERIMENTO DO IMPOSTO */
                             V_PERDIFERE    := NULL;
                             V_IMPOSTOOPERA := NULL;
                             V_IMPOSTODIFER := NULL;
                             IF C_ITEMPROIMPOS.CDIMPOSTONFE = '00' THEN
                                IF V_CDSITTRIBTMP = 51 THEN
                                   BEGIN
                                      SELECT PD.VRPERDIFERE, 'S'
                                        INTO V_VRPERDIFERE, V_IDDIFECLI
                                        FROM PRODDIFEREIMPCLI PD, PRODUTO PR
                                       WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                         AND PD.CDPAIS = V_PAISORIG
                                         AND PD.SGESTADO = V_ESTAORIG
                                         AND PD.CDCLIENTE = P_CLIENTE
                                         AND PR.CDARVPROD IN (SELECT MAX(PR.CDARVPROD) AS CDARVPROD
                                                                FROM PRODDIFEREIMPCLI PD, PRODUTO PR
                                                               WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                                                 AND PD.CDPAIS = V_PAISORIG
                                                                 AND PD.SGESTADO = V_ESTAORIG
                                                                 AND PD.CDCLIENTE = P_CLIENTE
                                                                 AND INSTR(NVL(RTRIM(C_ITEMPROD.CDARVPROD), PR.CDARVPROD), PR.CDARVPROD) = 1);
                                   EXCEPTION
                                      WHEN NO_DATA_FOUND THEN
                                         V_VRPERDIFERE := NULL; V_IDDIFECLI := 'N';
                                   END;
                                   IF V_IDDIFECLI = 'N' THEN
                                      BEGIN
                                         SELECT PD.VRPERDIFERE
                                           INTO V_VRPERDIFERE
                                           FROM PRODDIFEREIMPOS PD, PRODUTO PR
                                          WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                            AND PD.CDPAIS = V_PAISORIG
                                            AND PD.SGESTADO = V_ESTAORIG
                                            AND PR.CDARVPROD IN (SELECT MAX(PR.CDARVPROD) AS CDARVPROD
                                                                   FROM PRODDIFEREIMPOS PD, PRODUTO PR
                                                                  WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                                                    AND PD.CDPAIS = V_PAISORIG
                                                                    AND PD.SGESTADO = V_ESTAORIG
                                                                    AND INSTR(NVL(RTRIM(C_ITEMPROD.CDARVPROD), PR.CDARVPROD), PR.CDARVPROD) = 1);
                                      EXCEPTION
                                         WHEN NO_DATA_FOUND THEN
                                            V_VRPERDIFERE := NULL;
                                      END;
                                   END IF;
                                   IF V_VRPERDIFERE IS NOT NULL THEN
                                      V_PERDIFERE    := V_VRPERDIFERE;
                                      V_IMPOSTOOPERA := V_IMPOSTOIT;
                                      V_IMPOSTODIFER := V_IMPOSTOOPERA * (V_VRPERDIFERE/100);
                                      V_IMPOSTOIT := V_IMPOSTOOPERA - V_IMPOSTODIFER;
                                   END IF;
                                END IF;
                             END IF;
                             IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                                INSERT INTO ITEMPROIMPOS(
                                   CDFILIAL,        NRLANCTONF,   NRSEQITPROD,    CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                   CDSITUTRIBIT,    CDFISCOPERIT, CDSUFXCFOPIT,   VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                   VRCONTITIMP,     VRBASECALCIT, VRIMPOSTOIT,    IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                   CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                   CDDETALHEPROD, VRIMPOSTOVISUIT, CDREGRAIT, VRPERDIFEREIT, VRIMPOSTOOPERAIT, VRIMPOSTODIFERIT,
                                   CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                                VALUES(
                                   P_FILIAL,                P_LANCTONF,     C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, w_1,                 V_CDFISCTEMP,
                                   NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM,   V_SUFXCFOPPR,           V_ALIQUTMP,               V_PREDBCTMP,         V_PARDEDTMP,
                                   TRUNC(V_BASECALCTO,2),   V_BASECALCTO,   TRUNC(V_IMPOSTOIT,2),   w_00000,                  w_00000,             V_IDTRIMPSTII,
                                   V_CDSITUTRIBNFE,         V_IDMODBCNFEIT, V_IDMODBCSTNFEIT,       V_CDTPCRPICOSTNFEIT,      V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE,
                                   V_CDDETALHEPRODTEMP, TRUNC(V_VRIMPOSTOVISUIT,2), V_CDREGRAITALIQ, V_PERDIFERE, V_IMPOSTOOPERA, V_IMPOSTODIFER,
                                   V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                             ELSE
                                INSERT INTO ITEMPROIMPOS(
                                   CDFILIAL,        NRLANCTONF,   NRSEQITPROD,     CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                   CDSITUTRIBIT,    CDFISCOPERIT, CDSUFXCFOPIT,    VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                   VRCONTITIMP,     VRBASECALCIT, VRIMPOSTOIT,     IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                   CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE,  CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                   CDDETALHEPROD, VRIMPOSTOVISUIT,   CDREGRAIT, VRPERDIFEREIT, VRIMPOSTOOPERAIT, VRIMPOSTODIFERIT,
                                   CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                                VALUES(
                                   P_FILIAL,                P_LANCTONF,     C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, w_1,                 V_CDFISCTEMP,
                                   NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM,   V_SUFXCFOPPR,           V_ALIQUTMP,               V_PREDBCTMP,         V_PARDEDTMP,
                                   ROUND(V_BASECALCTO,5),   ROUND(V_BASECALCTO,2),   ROUND(V_IMPOSTOIT,2),   w_00000,                  w_00000,             V_IDTRIMPSTII,  
                                   V_CDSITUTRIBNFE,         V_IDMODBCNFEIT, V_IDMODBCSTNFEIT,       V_CDTPCRPICOSTNFEIT,      V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE,
                                   V_CDDETALHEPRODTEMP, ROUND(V_VRIMPOSTOVISUIT,2), V_CDREGRAITALIQ, V_PERDIFERE, V_IMPOSTOOPERA, V_IMPOSTODIFER,
                                   V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                             END IF;
                             IF (C_ITEMPROIMPOS.NRITPROIMPOS = w_1) THEN
                                IF (V_PREDBCTMP > 0) AND (V_PBCSUBTRIB = 0) THEN
                                   IF C_ITEMPROD.CDSITUTRIBPR = '060' THEN
                                      INSERT INTO ITEMPROIMPOS(
                                         CDFILIAL,        NRLANCTONF,      NRSEQITPROD,     CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                         CDSITUTRIBIT,    CDFISCOPERIT,    CDSUFXCFOPIT,    VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                         VRCONTITIMP,     VRBASECALCIT,    VRIMPOSTOIT,     IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                         CDSITUTRIBITNFE, IDMODBCITNFE,    IDMODBCSTITNFE,  CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                         CDDETALHEPROD, VRIMPOSTOVISUIT,   CDREGRAIT, CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                                      VALUES(
                                         P_FILIAL,                P_LANCTONF,      C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, w_2,                 w_2,
                                         NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM,    V_SUFXCFOPPR,           0,                        V_PERDBCALC,         V_PARDEDTMP,
                                         V_CONTABCALC,            0,               0,                      w_00000,                  w_00000,             V_IDTRIMPSTII,
                                         V_CDSITUTRIBNFE,         V_IDMODBCNFEIT,  V_IDMODBCSTNFEIT,       V_CDTPCRPICOSTNFEIT,      V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE,
                                         V_CDDETALHEPRODTEMP, 0, V_CDREGRAITALIQ, V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                                   ELSE
                                      INSERT INTO ITEMPROIMPOS(
                                         CDFILIAL,        NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                         CDSITUTRIBIT,    CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                         VRCONTITIMP,     VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                         CDSITUTRIBITNFE, IDMODBCITNFE,   IDMODBCSTITNFE,  CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                         CDDETALHEPROD,   VRIMPOSTOVISUIT,   CDREGRAIT, CDENQLEGALIPI,      CDCEST,         CDBENEFFISCAL)
                                      VALUES(
                                         P_FILIAL,                P_LANCTONF,     C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, w_2,                 w_2,
                                         NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM,   V_SUFXCFOPPR,           0,                        V_PERDBCALC,         V_PARDEDTMP,
                                         V_CONTABCALC,            0,              0,                      w_00000,                  w_00000,             V_IDTRIMPSTII,
                                         V_CDSITUTRIBNFE,         V_IDMODBCNFEIT, V_IDMODBCSTNFEIT,       V_CDTPCRPICOSTNFEIT,      V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE,
                                         V_CDDETALHEPRODTEMP, 0, V_CDREGRAITALIQ, V_CDENQLEGALIPI,        V_CDCEST,                 V_CDBENEFFISCAL);
                                   END IF;
                                END IF;
                             END IF;
                          END IF;
                       END IF;
                     ELSE
                        Begin
                           SELECT CDFILIAL
                           INTO   V_CHAVE
                           FROM ITEMPROIMPOS
                           WHERE CDFILIAL     = P_FILIAL                 AND
                                 NRLANCTONF   = P_LANCTONF               AND
                                 NRSEQITPROD  = C_ITEMPROD.NRSEQITPROD   AND
                                 CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO AND
                                 NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS;
                        Exception
                           When No_Data_Found Then
                              V_CHAVE := Null;
                        End;
                        SELECT NVL(MAX(IDPERVISIMPSCRE), 'S')
                          INTO V_IDPERVISIMPSCRE
                          FROM PARAMGERAL;
                        IF V_IDPERVISIMPSCRE = 'S' THEN
                           V_VRIMPOSTOVISUIT := (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV);
                        ELSE
                           IF (V_CDFISCTEMP = '1') THEN
                              V_VRIMPOSTOVISUIT := (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV);
                           ELSE
                              V_VRIMPOSTOVISUIT := 0;
                           END IF;
                        END IF;
                        IF V_CHAVE IS NULL THEN
                           V_ICMS := 'N';
                           FOR C_ICMS IN (SELECT CDIMPOSTO
                                            FROM IMPOSTONFE N, IMPOSTO I
                                           WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                             AND N.CDIMPOSTONFE = '00'
                                             AND I.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO)
                           LOOP
                              V_ICMS := 'S';
                           END LOOP;
                           IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                              INSERT INTO ITEMPROIMPOS(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                       CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                       VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                       IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                       CDDETALHEPROD, VRIMPOSTOVISUIT, CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                              VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, V_CDFISCTEMP,
                                     NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, TRUNC((C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),2),
                                     TRUNC((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),2), TRUNC((C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),2),
                                     TRUNC((C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV),2), w_00000, w_00000, V_CDSITUTRIBNFE,
                                     V_IDMODBCNFEIT, V_IDMODBCSTNFEIT, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE, C_ITEMPROIMPOS.IDTRIMPSTII,
                                     V_CDDETALHEPRODTEMP, V_VRIMPOSTOVISUIT, V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                           ELSE
                              IF (C_ITEMPROIMPOS.NRITPROIMPOS = w_3)  THEN
                                 /*Inserir a linha de diferencial de alíquota*/
                                 V_DIFALIQ := 0;
                                 SELECT MAX(VRALIQITEM)
                                   INTO V_DIFALIQ
                                   FROM ITEMPROIMPOS IT, ITEMPRODUTO I
                                  WHERE IT.CDFILIAL     = C_ITEMNOTAFISC.CDFILIALIT
                                    AND IT.NRLANCTONF   = C_ITEMNOTAFISC.NRLANCTONFIT
                                    AND I.CDFILIAL      = C_ITEMNOTAFISC.CDFILIALIT
                                    AND I.NRLANCTONF    = C_ITEMNOTAFISC.NRLANCTONFIT
                                    AND IT.NRSEQITPROD  = I.NRSEQITPROD
                                    AND I.CDPRODUTO     = C_ITEMPROD.CDPRODUTO
                                    AND ROUND(I.VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF)
                                    AND (NVL(NRLOTEESTQ, ' ') = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))
                                    AND (NVL(NRSUBLOTE, ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')))
                                    AND IT.CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO
                                    AND IT.NRITPROIMPOS = w_1;
                                 IF  (V_ALIQUTMP = V_DIFALIQ) THEN
                                    INSERT INTO ITEMPROIMPOS(
                                       CDFILIAL,        NRLANCTONF,   NRSEQITPROD,    CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                       CDSITUTRIBIT,    CDFISCOPERIT, CDSUFXCFOPIT,   VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                       VRCONTITIMP,     VRBASECALCIT, VRIMPOSTOIT,    IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                       CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                       CDDETALHEPROD,   VRIMPOSTOVISUIT,   CDREGRAIT, IDDIFALIQCUSPRI, CDENQLEGALIPI,  CDCEST,
                                       CDBENEFFISCAL)
                                    VALUES(
                                       P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                       C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, (C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),
                                       ROUND((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),5), (C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV), 
                                       (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), w_00000, w_00000, C_ITEMPROIMPOS.IDTRIMPSTII, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                       C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'),
                                       V_CDDETALHEPRODTEMP, (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), V_CDREGRAITALIQ, C_ITEMPROIMPOS.IDDIFALIQCUSPRI, V_CDENQLEGALIPI, V_CDCEST,
                                       V_CDBENEFFISCAL);
                                 END IF;
                                 /*Inserir a linha de diferencial de alíquota*/
                              ELSE
                                 INSERT INTO ITEMPROIMPOS(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                          CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                          VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                          IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                          CDDETALHEPROD, VRIMPOSTOVISUIT, IDDIFALIQCUSPRI, CDENQLEGALIPI, CDCEST,         CDBENEFFISCAL)
                                 VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, V_CDFISCTEMP,
                                        NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, (C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),
                                        ROUND((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),5), (C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),  
                                        (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), w_00000, w_00000, V_CDSITUTRIBNFE,
                                        V_IDMODBCNFEIT, V_IDMODBCSTNFEIT, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE, C_ITEMPROIMPOS.IDTRIMPSTII,
                                        V_CDDETALHEPRODTEMP, V_VRIMPOSTOVISUIT, C_ITEMPROIMPOS.IDDIFALIQCUSPRI, V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                              END IF;
                           END IF;
                        END IF;
                     END IF;
                     DELETE ALIQTEMP
                      WHERE NRALIQTEMP = V_ALIQTEMP;
                  END LOOP;
               ELSE /*SE A NOTA POSSUI UM UNICO DOCUMENTO DE ORIGEM*/
                  Begin
                     SELECT SUM(I.QTITEMPROD)
                       INTO V_SOMAQITEMPROD
                       FROM ITEMPRODUTO I
                      WHERE CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT                                          AND
                            NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT                                        AND
                            CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                               AND
                            ROUND(VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF) AND
                            (NVL(NRLOTEESTQ, ' ')  = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                      AND
                            (NVL(NRSUBLOTE , ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')));
                     SELECT NVL(MAX(C_ITEMPROD.QTITEMPROD / V_SOMAQITEMPROD),1) AS VRFATOCONV,
                            NVL(SUM(VRBRUTITEM),0), NVL(SUM(VRCONTITEM),0), NVL(SUM(VRENCARGOIMPXML), 0)
                       INTO V_VRFATOCONV, V_VRBRUTO, V_VRCONTAB, V_VRENCARGOIMPXML
                       FROM ITEMPRODUTO I
                      WHERE CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT                                          AND
                            NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT                                        AND
                            CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                               AND
                            ROUND(VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF) AND
                            (NVL(NRLOTEESTQ, ' ')  = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                      AND
                            (NVL(NRSUBLOTE , ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')));
                  Exception
                     When No_Data_Found Then
                        V_VRFATOCONV := 1; V_VRBRUTO := 0; V_VRCONTAB := 0;
                  End;
                 /*IF P_TIPOOPER = 03 THEN
                    Begin
                       SELECT NVL(C_ITEMPROD.VRTOTAITEM / SUM(I.VRTOTAITEM) ,1) AS VRFATOCONV,
                              NVL(SUM(VRBRUTITEM),0), NVL(SUM(VRCONTITEM),0)
                       INTO   V_VRFATOCONV, V_VRBRUTO, V_VRCONTAB
                       FROM ITEMPRODUTO I
                       WHERE CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT                                          AND
                             NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT                                        AND
                             CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                               AND
                             ROUND(VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF) AND
                             (NVL(NRLOTEESTQ,  )  = (NVL(C_ITEMPROD.NRLOTEESTQ,  )))                      AND
                             (NVL(NRSUBLOTE ,  )  = (NVL(C_ITEMPROD.NRSUBLOTE ,  )));
                    Exception
                       When No_Data_Found Then
                          V_VRFATOCONV := 1; V_VRBRUTO := 0; V_VRCONTAB := 0;
                    End;
                 END IF;*/
                  IF P_TIPOOPER <> '03' THEN
                    UPDATE ITEMPRODUTO
                       SET VRCONTITEMORIG = ROUND(V_VRCONTAB * V_VRFATOCONV,5),
                           VRBRUTITEMORIG = ROUND(V_VRBRUTO * V_VRFATOCONV,5),
                           VRCONTITEM = ROUND(V_VRCONTAB * V_VRFATOCONV,5),
                           VRBRUTITEM = ROUND(V_VRBRUTO * V_VRFATOCONV,5),
                           VRENCARGOIMPXML = ROUND(V_VRENCARGOIMPXML * V_VRFATOCONV,2)
                     WHERE CDFILIAL    = P_FILIAL                                                           AND
                           NRLANCTONF  = P_LANCTONF                                                         AND
                           CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                               AND
                           ROUND(VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF) AND
                           (NVL(NRLOTEESTQ, ' ') = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                        AND
                           (NVL(NRSUBLOTE , ' ') = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')));
                  END IF;
                  IF V_CDINTEGRACAO IS NOT NULL THEN
                     IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                        UPDATE ITEMPRODUTO
                        SET VRCONTITEM = TRUNC((V_VRCONTAB * V_VRFATOCONV),2),
                            VRBRUTITEM = TRUNC((V_VRBRUTO * V_VRFATOCONV),2)
                        WHERE CDFILIAL    = P_FILIAL                                                           AND
                              NRLANCTONF  = P_LANCTONF                                                         AND
                              CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                               AND
                              ROUND(VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF) AND
                             (NVL(NRLOTEESTQ, ' ') = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                        AND
                             (NVL(NRSUBLOTE , ' ') = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')));
                     ELSE
                        UPDATE ITEMPRODUTO
                        SET VRCONTITEM = ROUND(V_VRCONTAB * V_VRFATOCONV,5),
                            VRBRUTITEM = ROUND(V_VRBRUTO * V_VRFATOCONV,5)
                        WHERE CDFILIAL    = P_FILIAL                                                           AND
                              NRLANCTONF  = P_LANCTONF                                                         AND
                              CDPRODUTO   = C_ITEMPROD.CDPRODUTO                                               AND
                              ROUND(VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF) AND
                             (NVL(NRLOTEESTQ, ' ') = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                        AND
                             (NVL(NRSUBLOTE , ' ') = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')));
                     END IF;
                  END IF;
                  V_CDIMPOSRECALC := NULL;
                  UPDATE ITEMPRODUTO
                     SET IDUTILBCPMC = 'N'
                   WHERE CDFILIAL = P_FILIAL
                     AND NRLANCTONF = P_LANCTONF
                     AND NRSEQITPROD = C_ITEMPROD.NRSEQITPROD;
                  IF (V_IDCONTCONFORIG = 'N') THEN
                     UPDATE ITEMPRODUTO
                        SET VRCONTITEMORIG = (VRTOTAITEM - VRDESCITEM),
                            VRCONTITEM     = (VRTOTAITEM - VRDESCITEM),
                            VRBRUTITEMORIG = VRTOTAITEM,
                            VRBRUTITEM     = VRTOTAITEM
                      WHERE CDFILIAL       = P_FILIAL
                        AND NRLANCTONF     = P_LANCTONF
                        AND NRSEQITPROD    = C_ITEMPROD.NRSEQITPROD;
                  END IF;
                  /*IMPOSTOS POR ITEM DA NOTA DE ORIGEM*/
                  FOR C_ITEMPROIMPOS IN (SELECT VRPERREDBCIT, NRITPROIMPOS, IT.CDIMPOSTO, CDFISCALIT,
                                                VRALIQITEM, VRPARDEDIT, SUM(VRCONTITIMP) AS VRCONTITIMP,
                                                SUM(VRBASECALCIT) AS VRBASECALCIT, SUM(VRIMPOSTOIT) AS VRIMPOSTOIT,
                                                CDSITUTRIBITNFE AS CDSITUTRIBNFE, IDMODBCITNFE AS IDMODBCNFE, IDMODBCSTITNFE AS IDMODBCSTNFE,
                                                CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII, NVL(IDDIFALIQCUSPRI, 'N') AS IDDIFALIQCUSPRI,
                                                IM.CDIMPOSTONFE, IT.CDENQLEGALIPI, IT.CDCEST,
                                                NVL(IT.VRADICFECP, 0) AS VRADICFECP, NVL(SUM(IT.VRADICALIQFECP), 0) AS VRADICALIQFECP,
                                                IT.CDBENEFFISCAL, I.CDFISOPEITEM
                                          FROM ITEMPROIMPOS IT, ITEMPRODUTO I, IMPOSTO IM
                                         WHERE IT.CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT                                         AND
                                               IT.NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT                                       AND
                                               I.CDFILIAL     = C_ITEMNOTAFISC.CDFILIALIT                                         AND
                                               I.NRLANCTONF   = C_ITEMNOTAFISC.NRLANCTONFIT                                       AND
                                               IT.NRSEQITPROD = I.NRSEQITPROD                                                     AND
                                               IT.CDIMPOSTO   = IM.CDIMPOSTO                                                      AND
                                               I.CDPRODUTO = C_ITEMPROD.CDPRODUTO                                                 AND
                                               IT.NRITPROIMPOS = '1'                                                              AND
                                               ROUND(I.VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF) AND
                                               (NVL(I.NRLOTEESTQ, ' ') = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))                       AND
                                               (NVL(I.NRSUBLOTE, ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE,  ' ')))
                                         GROUP BY VRPERREDBCIT, IT.CDIMPOSTO, NRITPROIMPOS, CDFISCALIT, VRALIQITEM,
                                                  VRPARDEDIT,   CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE,
                                                  CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII, IDDIFALIQCUSPRI,
                                                  IM.CDIMPOSTONFE, IT.CDENQLEGALIPI, IT.CDCEST, IT.VRADICFECP,
                                                  IT.CDBENEFFISCAL, I.CDFISOPEITEM
                                         ORDER BY CDIMPOSTO) LOOP
                     /*IMPOSTO DE SUBSTITUICAO TRIBUTARIA*/
                     V_CDIMPOSTRIB  := null;
                     V_CDBENEFFISCAL := NULL;
                     FOR C_ICMSST IN (SELECT CDIMPOSTO
                                        FROM IMPOSTO
                                       WHERE CDIMPOSTRIB = C_ITEMPROIMPOS.CDIMPOSTO)
                     LOOP
                        V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
                     END LOOP;
                     V_CDIMPOSIPI := null;
                     SELECT NVL(MAX(IDGERIPIDESPA), 'N'), MAX(CDIMPOSTOIPI)
                       INTO V_IDGERIPIDESPA, V_CDIMPOSIPI
                       FROM TIPOOPERAC
                      WHERE IDENTRSAIDOP = 'S'
                        AND CDTIPOOPER = P_TIPOOPER
                        AND CDIMPOSTOIPI = C_ITEMPROIMPOS.CDIMPOSTO;
                     IF (V_IDGERICMSTDESPA = 'S') AND (V_CDIMPOSTRIB IS NOT NULL) AND (V_IDUTCTRLFIS = 'S') THEN
                        IF NOT((C_ITEMPROIMPOS.VRPERREDBCIT > 0.00) AND (C_ITEMPROIMPOS.NRITPROIMPOS = w_2)) THEN
                           V_VRTOTALICMSST := V_VRTOTALICMSST + (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV);
                        END IF;
                        IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                           INSERT INTO ITEMPROIMPOSORIG(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                    CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                    VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                    IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                    CDDETALHEPROD, CDENQLEGALIPI, CDCEST,          CDBENEFFISCAL)
                           VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                  C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, TRUNC((C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),2),
                                  TRUNC((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),2), TRUNC((C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),2),
                                  TRUNC((C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV),2), w_00000, w_00000, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                  C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'), C_ITEMPROIMPOS.IDTRIMPSTII,
                                  V_CDDETALHEPRODTEMP, C_ITEMPROIMPOS.CDENQLEGALIPI, C_ITEMPROIMPOS.CDCEST, C_ITEMPROIMPOS.CDBENEFFISCAL);
                        ELSE
                           INSERT INTO ITEMPROIMPOSORIG(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                    CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                    VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                    IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                    CDDETALHEPROD, CDENQLEGALIPI, CDCEST,          CDBENEFFISCAL)
                           VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                  C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, (C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),
                                  ROUND((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),2), (C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),
                                  (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), w_00000, w_00000, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                  C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'), C_ITEMPROIMPOS.IDTRIMPSTII,
                                  V_CDDETALHEPRODTEMP, C_ITEMPROIMPOS.CDENQLEGALIPI, C_ITEMPROIMPOS.CDCEST, C_ITEMPROIMPOS.CDBENEFFISCAL);
                        END IF;
                     END IF;
                     IF (V_IDGERIPIDESPA = 'S') AND (V_CDIMPOSIPI IS NOT NULL) AND (V_IDUTCTRLFIS = 'S') THEN
                        IF NOT((C_ITEMPROIMPOS.VRPERREDBCIT > 0.00) AND (C_ITEMPROIMPOS.NRITPROIMPOS = w_2)) THEN
                           V_VRTOTALIPI := V_VRTOTALIPI + (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV);
                        END IF;
                        IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                           INSERT INTO ITEMPROIMPOSORIG(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                    CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                    VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                    IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                    CDDETALHEPROD, CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                           VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                  C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, TRUNC((C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),2),
                                  TRUNC((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),2), TRUNC((C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),2),
                                  TRUNC((C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV),2), w_00000, w_00000, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                  C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'), C_ITEMPROIMPOS.IDTRIMPSTII,
                                  V_CDDETALHEPRODTEMP, C_ITEMPROIMPOS.CDENQLEGALIPI, C_ITEMPROIMPOS.CDCEST, C_ITEMPROIMPOS.CDBENEFFISCAL);
                        ELSE
                           INSERT INTO ITEMPROIMPOSORIG(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                    CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                    VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                    IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                    CDDETALHEPROD, CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                           VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                  C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, (C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),
                                  ROUND((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),5), (C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),  
                                  (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), w_00000, w_00000, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                  C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'), C_ITEMPROIMPOS.IDTRIMPSTII,
                                  V_CDDETALHEPRODTEMP, C_ITEMPROIMPOS.CDENQLEGALIPI, C_ITEMPROIMPOS.CDCEST, C_ITEMPROIMPOS.CDBENEFFISCAL);
                        END IF;
                     END IF;
                     IF (NOT((V_IDGERICMSTDESPA = 'S') AND (V_CDIMPOSTRIB IS NOT NULL) OR (V_IDGERIPIDESPA = 'S')
                        AND (V_CDIMPOSIPI IS NOT NULL)) AND (V_IDUTCTRLFIS = 'S')) OR (V_IDUTCTRLFIS = 'N') THEN
                        -- Verifica se há grupo fiscal por servico
                        SELECT MAX(CDGRUPOFISC)
                        INTO V_CDGRPFISCSV
                        FROM SERVCLIE SC, SERVICO S
                        WHERE SC.CDFILIAL   = V_CDFILIEMIT AND
                              SC.CDCLIENTE  = P_CLIENTE    AND
                              SC.CDFILIAL   = S.CDFILIAL   AND
                              SC.CDSERVFILI = S.CDSERVFILI AND
                              S.CDPRODUTO   = C_ITEMPROD.CDPRODUTO;
                        IF V_CDGRPFISCSV IS NULL THEN
                        -- Verifica se há grupo fiscal por cliente da filial
                           SELECT MAX(CDGRUPOFISC)
                           INTO V_CDGRPFISCSV
                           FROM CLIENFILIAL
                           WHERE CDFILIAL   = V_CDFILIEMIT AND
                                 CDCLIENTE  = P_CLIENTE;
                        END IF;
                        CONSULTA_INCIDENCIA(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC,
                                            V_CDGRPFISCSV, V_IDINTEREST, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPROD.IDORIGEMPROD);
                        BEGIN
                           SELECT MAX(CDFISCTEMP), MAX(CDDETALHEPROD), MAX(CDENQLEGALIPI), MAX(CDCEST)
                             INTO   V_CDFISCTEMP, V_CDDETALHEPRODTEMP, V_CDENQLEGALIPI, V_CDCEST
                            FROM INCITEMP IT, IMPOSTO IP
                            WHERE IT.NRINCITEMP = V_INCITEMP
                              AND IP.CDIMPOSTO  = IT.CDIMPOTEMP
                              AND IP.IDPRODNOTA = w_P
                              AND IT.CDIMPOTEMP = C_ITEMPROIMPOS.CDIMPOSTO;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_CDFISCTEMP        := NULL;
                              V_CDDETALHEPRODTEMP := NULL;
                              V_CDENQLEGALIPI     := NULL;
                              V_CDCEST            := NULL;
                        END;
                        IF (C_ITEMPROIMPOS.VRPERREDBCIT > 0.00) AND
                           (C_ITEMPROIMPOS.NRITPROIMPOS = w_2) THEN
                           V_CDFISCTEMP := w_2;
                        ELSE
                           IF (V_CDFISCTEMP IS NULL) THEN
                               V_CDFISCTEMP := C_ITEMPROIMPOS.CDFISCALIT;
                           END IF;
                        END IF;
                        IF V_CDENQLEGALIPI IS NULL THEN
                           V_CDENQLEGALIPI := C_ITEMPROIMPOS.CDENQLEGALIPI;
                        END IF;
                        IF V_CDCEST IS NULL THEN
                           V_CDCEST := C_ITEMPROIMPOS.CDCEST;
                        END IF;
                        DELETE INCITEMP
                         WHERE NRINCITEMP = V_INCITEMP;
                        /*VERIFICA CST POR IMPOSTO*/
                        V_IMPOICMS := NULL;
                        V_CDGRUPOFISCAUX := NULL;
                        SELECT MAX(G.CDGRUPOFISC)
                          INTO V_CDGRUPOFISCAUX
                          FROM GRUPOFISCIMPFIL G,
                               (SELECT CDFILIEMIT AS CDFILIAL
                                  FROM NOTAFISCAL
                                 WHERE CDFILIAL = P_FILIAL
                                       AND NRLANCTONF = P_LANCTONF) F
                         WHERE G.CDFILIAL = F.CDFILIAL
                           AND NOT G.CDGRUPOFISC IS NULL
                           AND G.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                        IF V_IDORIGDESTNF = 'C' THEN
                           SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                             INTO V_CDGRUPOFISCAUX
                             FROM GRFISIMPIECLIE G,
                                  (SELECT CDCLIENTE
                                     FROM NOTAFISCAL
                                    WHERE CDFILIAL = P_FILIAL
                                      AND NRLANCTONF = P_LANCTONF) N
                            WHERE G.CDCLIENTE = N.CDCLIENTE
                              AND G.CDEMPRESA = P_EMPRESA
                              AND G.CDINSCESTA = P_INSCESTA
                              AND NOT G.CDGRUPOFISC IS NULL
                              AND G.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                        ELSE
                           SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                             INTO V_CDGRUPOFISCAUX
                             FROM GRFISIMPIEFORN G,
                                 (SELECT CDFORNECED
                                    FROM NOTAFISCAL
                                   WHERE CDFILIAL = P_FILIAL
                                     AND NRLANCTONF = P_LANCTONF) N
                            WHERE G.CDFORNECED = N.CDFORNECED
                              AND G.CDEMPRESA = P_EMPRESA
                              AND G.CDINSCESTA = P_INSCESTA
                              AND NOT G.CDGRUPOFISC IS NULL
                              AND G.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                        END IF;
                        IF V_CDGRUPOFISCAUX IS NULL THEN
                           CONSULTA_CST_NFE(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISC,C_ITEMPROIMPOS.CDIMPOSTO, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, C_ITEMPROD.IDORIGEMPROD);
                        ELSE
                           CONSULTA_CST_NFE(w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISCAUX,C_ITEMPROIMPOS.CDIMPOSTO, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, C_ITEMPROD.IDORIGEMPROD);
                        END IF;
                        IF (V_CDSITTRIBTMP IS NULL) THEN
                           V_CDSITUTRIBNFE := C_ITEMPROIMPOS.CDSITUTRIBNFE;
                        ELSE
                           V_CDSITUTRIBNFE := V_CDSITTRIBTMP;
                        END IF;
                        IF (V_IDMODBCNFE IS NULL) THEN
                           V_IDMODBCNFEIT := C_ITEMPROIMPOS.IDMODBCNFE;
                        ELSE
                           V_IDMODBCNFEIT := V_IDMODBCNFE;
                        END IF;
                        IF (V_IDMODBCSTNFE IS NULL) THEN
                           V_IDMODBCSTNFEIT := C_ITEMPROIMPOS.IDMODBCSTNFE;
                        ELSE
                           V_IDMODBCSTNFEIT := V_IDMODBCSTNFE;
                        END IF;
                        IF (C_ITEMPROIMPOS.CDTPCREDPICOITE IS NULL) THEN
                           V_CDTPCRPICOSTNFEIT := V_CDTPCRPICOSTNFE;
                        ELSE
                           V_CDTPCRPICOSTNFEIT := C_ITEMPROIMPOS.CDTPCREDPICOITE;
                        END IF;
                        IF (C_ITEMPROIMPOS.CDNATUBCALPCIT IS NULL) THEN
                           V_CDNATUBCPCSTNFEIT := V_CDNATUBCPCSTNFE;
                        ELSE
                           V_CDNATUBCPCSTNFEIT := C_ITEMPROIMPOS.CDNATUBCALPCIT;
                        END IF;
                        IF (V_IDMOTIDESSTNFE IS NULL) THEN
                           V_IDMOTIDESSTNFE := C_ITEMPROIMPOS.IDMOTDESONERAIT;
                        ELSE
                           V_IDMOTIDESSTNFE := V_IDMOTIDESSTNFE;
                        END IF;
                        SELECT NVL(MAX(IDUTMUNIORIG),'N')
                          INTO V_IDUTMUNIORIG
                          FROM IMPOSTO
                         WHERE CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                        IF ((V_IDUTMUNIORIG = 'O') OR (V_IDUTMUNIORIG = 'N')) THEN
                           CONSULTA_ALIQUOTA(C_ITEMPROIMPOS.CDIMPOSTO, w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, V_PAISORIG,
                                             V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                             NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPROD.IDORIGEMPROD);
                        ELSE
                           CONSULTA_ALIQUOTA(C_ITEMPROIMPOS.CDIMPOSTO, w_S, P_EMPRESA, C_ITEMPROD.CDARVPROD, P_INSCESTA, V_PAISORIG,
                                             V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER,
                                             NULL, NULL, NULL, NULL, NULL, NULL, C_ITEMPROD.IDORIGEMPROD);
                        END IF;
                        V_NRALIQTEMP := 0;
                        V_PARDEDTMP  := 0;
                        V_PREDBCTMP  := 0;
                        V_ALIQUTMP   := 0;
                        V_IMPOSTOIT  := 0;
                        V_CDTIPOOPER    := NULL;
                        V_CDREGRAITALIQ := NULL;
                        SELECT NVL(MAX(NRALIQTEMP),0),      NVL(MAX(VRPARDEDTMP),0), NVL(MAX(VRPREDBCTMP),0), NVL(MAX(VRALIQUTMP),0),
                               NVL(MAX(IDTRIMPSTTMP), w_0), NVL(MAX(VRPBCSTTMP),0),  NVL(MAX(IDPMCBSCALTMP), w_S),
                               MAX(CDTIPOOPERTMP),          MAX(CDREGRATMP),
                               MAX(CDFISCOPER),             MAX(IDDIFEALIQ),         MAX(IDDIFALIQCUSPR),
                               MAX(CDBENEFFISCAL),          NVL(MAX(IDCONSMVAPMC), '3'), NVL(MAX(IDCONSCOMPBCPMC), '3')
                        INTO   V_NRALIQTEMP,                V_PARDEDTMP,             V_PREDBCTMP,             V_ALIQUTMP,
                               V_TRATIMPST,                 V_PBCSUBTRIB,            V_IDPMCBASECAL,
                               V_CDTIPOOPER,                V_CDREGRAITALIQ,
                               V_CDFISCOPERREGRA,           V_IDDIFEALIQREGRA,       V_IDDIFALIQCUSPRREGRA,
                               V_CDBENEFFISCAL,             V_IDCONSMVAPMC,          V_IDCONSCOMPBCPMC
                        FROM ALIQTEMP
                        WHERE (NRALIQTEMP = V_ALIQTEMP);
                        IF V_CDBENEFFISCAL IS NULL THEN
                           V_CDBENEFFISCAL := C_ITEMPROIMPOS.CDBENEFFISCAL;
                        END IF;
                        IF V_PAISORIG = V_PAISCFOP THEN
                           IF V_ESTAORIG = V_ESTACFOP THEN
                              V_CFOP1DIGT := '5';
                           ELSE
                              V_CFOP1DIGT := '6';
                           END IF;
                        ELSE
                           V_CFOP1DIGT := '7';
                        END IF;
                        IF V_CDFISCOPERREGRA IS NOT NULL THEN
                           IF (LENGTH(V_CDFISCOPERREGRA) < 4) THEN
                              V_CDFISCOPERREGRA := V_CFOP1DIGT||V_CDFISCOPERREGRA;
                           END IF;
                        ELSE
                           IF C_ITEMPROIMPOS.CDFISOPEITEM IS NOT NULL THEN
                              V_CDFISCOPERREGRA := V_CFOP1DIGT||SUBSTR(C_ITEMPROIMPOS.CDFISOPEITEM, 2, 3);
                           END IF;
                        END IF;       
                        V_ICMS := 'N';
                        FOR C_ICMS IN (SELECT CDIMPOSTO
                                         FROM IMPOSTONFE N, IMPOSTO I
                                        WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                          AND N.CDIMPOSTONFE = '00'
                                          AND I.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO)
                        LOOP
                           V_ICMS := 'S';
                        END LOOP;
                        IF (V_ICMS = 'S') THEN
                           IF (NVL(V_CFOPINFUSU, 'N') <> 'S') AND (V_CDFISCOPERREGRA IS NOT NULL) THEN
                              V_FISOPEITEM        := NVL(V_CDFISCOPERREGRA, V_FISOPEITEM);
                              V_DSCDFISOPEITREGRA := SUBSTR(DESCRICAO_CFOP(V_CDFISCOPERREGRA),1,45);
                           END IF;
                           UPDATE NOTAFISCAL
                              SET DSCDFISOPENF = NVL(V_DSCDFISOPEITREGRA, (SELECT MAX(DSCFOPTMP)
                                                                               FROM CFOPTEMP
                                                                              WHERE NRCFOPTEMP = V_CFOPTEMP))
                            WHERE CDFILIAL   = P_FILIAL   AND
                                  NRLANCTONF = P_LANCTONF;
                           SELECT MAX(CDFISOPETMP), MAX(CDSUFCFOPTMP), SUBSTR(MAX(DSCFOPTMP),1,50)
                             INTO V_FISOPEITEM    , V_SUFXCFOPPR, V_DSCDFISOPEIT
                             FROM CFOPTEMP
                            WHERE NRCFOPTEMP = V_CFOPTEMP;
                           IF (NVL(V_CFOPINFUSU, 'N') <> 'S') AND (V_CDFISCOPERREGRA IS NOT NULL) THEN
                              V_FISOPEITEM        := NVL(V_CDFISCOPERREGRA, V_FISOPEITEM);
                              V_DSCDFISOPEITREGRA := SUBSTR(DESCRICAO_CFOP(V_CDFISCOPERREGRA),1,45);
                           END IF;
                           UPDATE ITEMPRODUTO
                              SET CDFISOPEITEM  = V_FISOPEITEM,
                                  CDSUFXCFOPPR  = V_SUFXCFOPPR,
                                  DSCDFISOPEIT  = V_DSCDFISOPEIT
                            WHERE CDFILIAL    = P_FILIAL AND
                                  NRLANCTONF  = P_LANCTONF AND
                                  NRSEQITPROD = C_ITEMPROD.NRSEQITPROD;
                        END IF;
                        /*Verifica alíquota parametrizada por tipo de operação desde que não seja nota de estorno de cancelamento*/
                        IF ((V_NRALIQTEMP IS NOT NULL) AND (V_CDTIPOOPER IS NOT NULL) AND (V_ESTORNOCANC <> w_S)) THEN
                           V_CDIMPOSRECALC := V_CDIMPOSRECALC||C_ITEMPROIMPOS.CDIMPOSTO||'_';
                           IF (C_ITEMPROIMPOS.NRITPROIMPOS = w_2) THEN
                              V_CDIMPOSTRIB  := null;
                              V_IDVRIMPSTRIB := null;
                           ELSE
                              V_CDIMPOSTRIB  := null;
                              V_IDVRIMPSTRIB := null;
                              /*IMPOSTO DE SUBSTITUICAO TRIBUTARIA*/
                              FOR C_ICMSST IN (SELECT CDIMPOSTO
                                                 FROM IMPOSTO
                                                WHERE CDIMPOSTRIB = C_ITEMPROIMPOS.CDIMPOSTO)
                              LOOP
                                 V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
                              END LOOP;
                              V_TAXABASECALC := (100 - V_PREDBCTMP)/100;
                              V_TAXAALIQUOTA := (V_ALIQUTMP/100);
                              V_TAXABCSUBSTR := 1 + (V_PBCSUBTRIB/100);
                              V_BASECALCIT   := V_LIQUIITEM * V_TAXABASECALC;
                              IF V_CDIMPOSTRIB IS NULL THEN
                                 V_IDTRIMPSTII := '';
                              ELSE
                                 IF SUBSTR(V_SITUTRIBPR, 2, 2) = '60' THEN
                                    V_IDTRIMPSTII := 2;
                                 ELSE 
                                    V_IDTRIMPSTII := V_TRATIMPST;
                                 END IF;
                                 IF NVL(V_IDPMCBASECAL, W_N) = W_S THEN
                                    CALCULA_BC_PMC(P_FILIAL,
                                                   P_LANCTONF,
                                                   C_ITEMPROD.NRSEQITPROD,
                                                   C_ITEMPROIMPOS.CDIMPOSTO,
                                                   V_IDCONSMVAPMC,
                                                   V_IDCONSCOMPBCPMC,
                                                   V_PAISDEST,
                                                   V_ESTADEST,
                                                   C_ITEMPROD.CDPRODUTO,
                                                   C_ITEMPROD.QTITEMPROD,
                                                   C_ITEMPROD.VRUNITITEM,
                                                   C_ITEMPROD.VRDESCITEM,
                                                   V_PBCSUBTRIB,
                                                   V_PREDBCTMP,
                                                   NULL,
                                                   NULL,
                                                   NULL,
                                                   NULL,
                                                   NULL,
                                                   NULL,
                                                   'N',
                                                   V_BASECALCTO);
                                    V_IMPOSTOIT := (V_BASECALCTO * V_TAXAALIQUOTA) - V_PARDEDTMP;
                                 ELSE
                                    V_BASECALCTO  := V_BASECALCIT;
                                    V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
                                 END IF;
                                 SELECT NVL(MAX(IDVRIMPSTRIB), 'N')
                                   INTO V_IDVRIMPSTRIB
                                   FROM ITEMPROIMPOS I, IMPOSTO II
                                  WHERE (CDFILIAL = P_FILIAL)
                                    AND (NRLANCTONF = P_LANCTONF)
                                    AND (NRSEQITPROD = C_ITEMPROD.NRSEQITPROD)
                                    AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0)
                                    AND (I.CDIMPOSTO = II.CDIMPOSTO);
                                 IF (V_TRATIMPST = W_1) OR (NVL(V_IDVRIMPSTRIB, W_N) = W_N) THEN
                                    V_VRIMPOSTOST := 0;
                                 ELSE
                                    BEGIN
                                       SELECT NVL(MAX(VRIMPOSTOIT),0)
                                         INTO V_VRIMPOSTOST
                                         FROM ITEMPROIMPOS I
                                        WHERE (CDFILIAL = P_FILIAL)
                                          AND (NRLANCTONF = P_LANCTONF)
                                          AND (NRSEQITPROD = C_ITEMPROD.NRSEQITPROD)
                                          AND (INSTR(V_CDIMPOSTRIB,CDIMPOSTO||'_') > 0)
                                          AND (NRITPROIMPOS = w_1);
                                    EXCEPTION
                                       WHEN NO_DATA_FOUND THEN BEGIN
                                          V_VRIMPOSTOST := 0;
                                       END;
                                    END;
                                 END IF;
                                 V_IMPOSTOIT := V_IMPOSTOIT - V_VRIMPOSTOST;
                                 IF V_IMPOSTOIT < 0 THEN
                                    V_IMPOSTOIT := 0;
                                 END IF;
                                 IF (V_IDCONTCONFORIG = 'N') THEN
                                    IF NVL(V_IDPMCBASECAL, W_N) = W_S THEN
                                       UPDATE ITEMPRODUTO
                                          SET VRBRUTITEM = VRBRUTITEM + V_IMPOSTOIT,
                                              VRCONTITEM = VRCONTITEM + V_IMPOSTOIT
                                        WHERE CDFILIAL = P_FILIAL
                                          AND NRLANCTONF = P_LANCTONF
                                          AND NRSEQITPROD = C_ITEMPROD.NRSEQITPROD;
                                     END IF;
                                 END IF;
                              END IF;
                              IF NVL(V_IDPMCBASECAL, W_N) = W_N THEN
                                 V_PERDBCALC    := 100 - V_PREDBCTMP;
                                 IF V_PBCSUBTRIB > 0 THEN
                                    V_BASECALCIT := (V_BASECALCIT * V_TAXABCSUBSTR);
                                    V_PREDBCTMP  := V_PBCSUBTRIB;
                                    V_PERDBCALC  := 0;
                                 END IF;
                                 SELECT NVL(MAX(IDUTVLRQTDEIMPO), 'N')
                                   INTO V_IDUTVLRQTDEIMPO
                                   FROM IMPOSTO
                                  WHERE CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                                 IF (V_IDUTVLRQTDEIMPO = 'S') THEN
                                    SELECT NVL(MAX(VRFATOCONVES), 1)
                                      INTO V_VRFATOCONVES
                                      FROM CONVPRODUTO
                                     WHERE CDPRODUTO = C_ITEMPROD.CDPRODUTO;
                                    V_BASECALCTO  := (C_ITEMPROD.QTITEMPROD * V_VRFATOCONVES);
                                    V_IMPOSTOIT   := ((C_ITEMPROD.QTITEMPROD * V_VRFATOCONVES) * V_ALIQUTMP) - V_PARDEDTMP;
                                 ELSE
                                    V_BASECALCTO  := V_BASECALCIT;
                                    V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
                                 END IF;
                              END IF;
                              V_CONTABCALC  := V_LIQUIITEM - (V_LIQUIITEM * V_TAXABASECALC);
                              IF (V_ALIQUTMP = 0) AND (V_IMPOSTOIT = 0) THEN
                                 V_BASECALCTO := 0;
                              END IF;
                              -- Alterar o valor contábil caso o parâmetro Contabiliza conforme NF de origem esteja desmarcado
                              IF (V_IDCONTCONFORIG = 'N') THEN
                                 V_IMPOCONTAB  := 0;
                                 V_IDIMPORECUP := NULL;
                                 V_CDCCTBLATIV := NULL;
                                 V_IDTRATIMPO  := NULL;
                                 SELECT NVL(IDIMPORECUP, 'N'), IDTRATIMPO, CDCCTBLATIV
                                   INTO V_IDIMPORECUP, V_IDTRATIMPO, V_CDCCTBLATIV
                                   FROM IMPOSTO
                                  WHERE CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                                 IF (V_IDIMPORECUP = W_S) THEN
                                    IF (V_CDFISCTEMP = W_1) THEN
                                       IF (V_CDCCTBLATIV IS NOT NULL) THEN
                                          IF V_IDTRATIMPO = W_V THEN
                                             V_IMPOCONTAB := - V_IMPOSTOIT;
                                          END IF;
                                       END IF;
                                    ELSE
                                       IF V_IDTRATIMPO = W_A THEN
                                          V_IMPOCONTAB := V_IMPOSTOIT;
                                       ELSIF V_IDTRATIMPO IN (W_D,W_R) THEN
                                          V_IMPOCONTAB := - V_IMPOSTOIT;
                                       END IF;
                                    END IF;
                                 END IF;
                                 IF (ABS(NVL(V_IMPOCONTAB, 0)) > 0) THEN
                                    UPDATE ITEMPRODUTO
                                       SET VRCONTITEMORIG = (VRCONTITEM) + NVL(V_IMPOCONTAB, 0),
                                           VRCONTITEM     = (VRCONTITEM) + NVL(V_IMPOCONTAB, 0)
                                     WHERE CDFILIAL       = P_FILIAL
                                       AND NRLANCTONF     = P_LANCTONF
                                       AND NRSEQITPROD    = C_ITEMPROD.NRSEQITPROD;
                                 END IF;
                              END IF;
                              -- Alterar o valor contábil caso o parâmetro Contabiliza conforme NF de origem esteja desmarcado
                              Begin
                                 SELECT CDFILIAL
                                 INTO   V_CHAVE
                                 FROM ITEMPROIMPOS
                                 WHERE CDFILIAL     = P_FILIAL                 AND
                                       NRLANCTONF   = P_LANCTONF               AND
                                       NRSEQITPROD  = C_ITEMPROD.NRSEQITPROD   AND
                                       CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO AND
                                       NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS;
                              Exception
                                 When No_Data_Found Then
                                    V_CHAVE := Null;
                              End;
                              SELECT NVL(MAX(IDPERVISIMPSCRE),'S')
                                INTO V_IDPERVISIMPSCRE
                                FROM PARAMGERAL;
                              IF V_IDPERVISIMPSCRE = 'S' THEN
                                 V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
                              ELSE
                                 IF (V_CDFISCTEMP = '1') THEN
                                    V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
                                 ELSE
                                    V_VRIMPOSTOVISUIT := 0;
                                 END IF;
                              END IF;
                              /* ROTINA PARA CALCULAR O DIFERIMENTO DO IMPOSTO */
                              V_PERDIFERE    := NULL;
                              V_IMPOSTOOPERA := NULL;
                              V_IMPOSTODIFER := NULL;
                              IF C_ITEMPROIMPOS.CDIMPOSTONFE = '00' THEN
                                 IF V_CDSITTRIBTMP = 51 THEN
                                    BEGIN
                                       SELECT PD.VRPERDIFERE, 'S'
                                         INTO V_VRPERDIFERE, V_IDDIFECLI
                                         FROM PRODDIFEREIMPCLI PD, PRODUTO PR
                                        WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                          AND PD.CDPAIS = V_PAISORIG
                                          AND PD.SGESTADO = V_ESTAORIG
                                          AND PD.CDCLIENTE = P_CLIENTE
                                          AND PR.CDARVPROD IN (SELECT MAX(PR.CDARVPROD) AS CDARVPROD
                                                                 FROM PRODDIFEREIMPCLI PD, PRODUTO PR
                                                                WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                                                  AND PD.CDPAIS = V_PAISORIG
                                                                  AND PD.SGESTADO = V_ESTAORIG
                                                                  AND PD.CDCLIENTE = P_CLIENTE
                                                                  AND INSTR(NVL(RTRIM(C_ITEMPROD.CDARVPROD), PR.CDARVPROD), PR.CDARVPROD) = 1);
                                    EXCEPTION
                                       WHEN NO_DATA_FOUND THEN
                                          V_VRPERDIFERE := NULL; V_IDDIFECLI := 'N';
                                    END;
                                    IF V_IDDIFECLI = 'N' THEN
                                       BEGIN
                                          SELECT PD.VRPERDIFERE
                                            INTO V_VRPERDIFERE
                                            FROM PRODDIFEREIMPOS PD, PRODUTO PR
                                           WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                             AND PD.CDPAIS = V_PAISORIG
                                             AND PD.SGESTADO = V_ESTAORIG
                                             AND PR.CDARVPROD IN (SELECT MAX(PR.CDARVPROD) AS CDARVPROD
                                                                    FROM PRODDIFEREIMPOS PD, PRODUTO PR
                                                                   WHERE PD.CDPRODUTO = PR.CDPRODUTO
                                                                     AND PD.CDPAIS = V_PAISORIG
                                                                     AND PD.SGESTADO = V_ESTAORIG
                                                                     AND INSTR(NVL(RTRIM(C_ITEMPROD.CDARVPROD), PR.CDARVPROD), PR.CDARVPROD) = 1);
                                       EXCEPTION
                                          WHEN NO_DATA_FOUND THEN
                                             V_VRPERDIFERE := NULL;
                                       END;
                                    END IF;
                                    IF V_VRPERDIFERE IS NOT NULL THEN
                                       V_PERDIFERE    := V_VRPERDIFERE;
                                       V_IMPOSTOOPERA := V_IMPOSTOIT;
                                       V_IMPOSTODIFER := V_IMPOSTOOPERA * (V_VRPERDIFERE/100);
                                       V_IMPOSTOIT := V_IMPOSTOOPERA - V_IMPOSTODIFER;
                                    END IF;
                                 END IF;
                              END IF;
                              IF V_CHAVE IS NULL THEN
                                 V_ICMS := 'N';
                                 FOR C_ICMS IN (SELECT CDIMPOSTO
                                                  FROM IMPOSTONFE N, IMPOSTO I
                                                 WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                                   AND N.CDIMPOSTONFE = '00'
                                                   AND I.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO)
                                 LOOP
                                    V_ICMS := 'S';
                                 END LOOP;
                                 IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                                    INSERT INTO ITEMPROIMPOS(
                                       CDFILIAL,        NRLANCTONF,   NRSEQITPROD,    CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                       CDSITUTRIBIT,    CDFISCOPERIT, CDSUFXCFOPIT,   VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                       VRCONTITIMP,     VRBASECALCIT, VRIMPOSTOIT,    IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                       CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                       CDDETALHEPROD,   VRIMPOSTOVISUIT,   CDREGRAIT, VRPERDIFEREIT, VRIMPOSTOOPERAIT, VRIMPOSTODIFERIT,
                                       CDENQLEGALIPI,   CDCEST,            CDBENEFFISCAL)
                                    VALUES(
                                       P_FILIAL,                P_LANCTONF,     C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, w_1,                 V_CDFISCTEMP,
                                       NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM,   V_SUFXCFOPPR,           V_ALIQUTMP,               V_PREDBCTMP,         V_PARDEDTMP,
                                       ROUND(V_BASECALCTO,2),   V_BASECALCTO,   TRUNC(V_IMPOSTOIT,2),   w_00000,                  w_00000,             V_IDTRIMPSTII,
                                       V_CDSITUTRIBNFE,         V_IDMODBCNFEIT, V_IDMODBCSTNFEIT,       V_CDTPCRPICOSTNFEIT,      V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE,
                                       V_CDDETALHEPRODTEMP, TRUNC(V_VRIMPOSTOVISUIT,2), V_CDREGRAITALIQ, V_PERDIFERE, V_IMPOSTOOPERA, V_IMPOSTODIFER,
                                       V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                                 ELSE
                                    IF (C_ITEMPROIMPOS.NRITPROIMPOS = w_3)  THEN
                                       /*Inserir a linha de diferencial de alíquota*/
                                       V_DIFALIQ := 0;
                                       SELECT MAX(VRALIQITEM)
                                         INTO V_DIFALIQ
                                         FROM ITEMPROIMPOS IT, ITEMPRODUTO I
                                        WHERE IT.CDFILIAL     = C_ITEMNOTAFISC.CDFILIALIT
                                          AND IT.NRLANCTONF   = C_ITEMNOTAFISC.NRLANCTONFIT
                                          AND I.CDFILIAL      = C_ITEMNOTAFISC.CDFILIALIT
                                          AND I.NRLANCTONF    = C_ITEMNOTAFISC.NRLANCTONFIT
                                          AND IT.NRSEQITPROD  = I.NRSEQITPROD
                                          AND I.CDPRODUTO     = C_ITEMPROD.CDPRODUTO
                                          AND ROUND(I.VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF)
                                          AND (NVL(NRLOTEESTQ, ' ') = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))
                                          AND (NVL(NRSUBLOTE, ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')))
                                          AND IT.CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO
                                          AND IT.NRITPROIMPOS = w_1;
                                       IF  (V_ALIQUTMP = V_DIFALIQ) THEN
                                          INSERT INTO ITEMPROIMPOS(
                                             CDFILIAL,        NRLANCTONF,   NRSEQITPROD,    CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                             CDSITUTRIBIT,    CDFISCOPERIT, CDSUFXCFOPIT,   VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                             VRCONTITIMP,     VRBASECALCIT, VRIMPOSTOIT,    IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                             CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                             CDDETALHEPROD,   VRIMPOSTOVISUIT, CDREGRAIT, IDDIFALIQCUSPRI,
                                             CDENQLEGALIPI,   CDCEST,          CDBENEFFISCAL)
                                          VALUES(
                                             P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                             C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, (C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),
                                             ROUND((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),5), (C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),
                                             (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), w_00000, w_00000, C_ITEMPROIMPOS.IDTRIMPSTII, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                             C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'),
                                             V_CDDETALHEPRODTEMP, (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), V_CDREGRAITALIQ, C_ITEMPROIMPOS.IDDIFALIQCUSPRI,
                                             V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                                       /*Inserir a linha de diferencial de alíquota*/
                                       END IF;
                                    ELSE
                                       INSERT INTO ITEMPROIMPOS(
                                          CDFILIAL,        NRLANCTONF,   NRSEQITPROD,    CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                          CDSITUTRIBIT,    CDFISCOPERIT, CDSUFXCFOPIT,   VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                          VRCONTITIMP,     VRBASECALCIT, VRIMPOSTOIT,    IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                          CDSITUTRIBITNFE, IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                          CDDETALHEPROD,   VRIMPOSTOVISUIT,   CDREGRAIT, IDDIFALIQCUSPRI, VRPERDIFEREIT, VRIMPOSTOOPERAIT,
                                          VRIMPOSTODIFERIT, CDENQLEGALIPI, CDCEST, VRADICFECP, VRADICALIQFECP, 
                                          CDBENEFFISCAL)
                                       VALUES(
                                          P_FILIAL,                P_LANCTONF,     C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, w_1,               V_CDFISCTEMP,
                                          NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM,   V_SUFXCFOPPR,           V_ALIQUTMP,               V_PREDBCTMP,         V_PARDEDTMP,
                                          ROUND(V_BASECALCTO,5),   ROUND(V_BASECALCTO,2),   ROUND(V_IMPOSTOIT,2),   w_00000,                  w_00000,             V_IDTRIMPSTII,
                                          V_CDSITUTRIBNFE,         V_IDMODBCNFEIT, V_IDMODBCSTNFEIT,       V_CDTPCRPICOSTNFEIT,      V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE,
                                          V_CDDETALHEPRODTEMP,     ROUND(V_VRIMPOSTOVISUIT,2),     V_CDREGRAITALIQ, C_ITEMPROIMPOS.IDDIFALIQCUSPRI, V_PERDIFERE, V_IMPOSTOOPERA,
                                          V_IMPOSTODIFER, V_CDENQLEGALIPI, V_CDCEST, (C_ITEMPROIMPOS.VRADICFECP * V_VRFATOCONV), C_ITEMPROIMPOS.VRADICALIQFECP,
                                          V_CDBENEFFISCAL);
                                    END IF;
                                 END IF;
                                 IF (C_ITEMPROIMPOS.NRITPROIMPOS = w_1) THEN
                                    IF (V_PREDBCTMP > 0) AND (V_PBCSUBTRIB = 0) THEN
                                       IF C_ITEMPROD.CDSITUTRIBPR = '060' THEN
                                          INSERT INTO ITEMPROIMPOS(
                                             CDFILIAL,        NRLANCTONF,     NRSEQITPROD,    CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                             CDSITUTRIBIT,    CDFISCOPERIT,   CDSUFXCFOPIT,   VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                             VRCONTITIMP,     VRBASECALCIT,   VRIMPOSTOIT,    IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                             CDSITUTRIBITNFE, IDMODBCITNFE,   IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                             CDDETALHEPROD,   VRIMPOSTOVISUIT,   CDREGRAIT, CDENQLEGALIPI, CDCEST, VRADICFECP, VRADICALIQFECP,
                                             CDBENEFFISCAL)
                                          VALUES(
                                             P_FILIAL,                P_LANCTONF,     C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, w_2,                 w_2,
                                             NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM,   V_SUFXCFOPPR,           0,                        V_PERDBCALC,         V_PARDEDTMP,
                                             V_CONTABCALC,            0,              0,                      w_00000,                  w_00000,             V_IDTRIMPSTII,
                                             V_CDSITUTRIBNFE,         V_IDMODBCNFEIT, V_IDMODBCSTNFEIT,       V_CDTPCRPICOSTNFEIT,      V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE,
                                             V_CDDETALHEPRODTEMP, 0,     V_CDREGRAITALIQ, V_CDENQLEGALIPI, V_CDCEST, (C_ITEMPROIMPOS.VRADICFECP * V_VRFATOCONV), C_ITEMPROIMPOS.VRADICALIQFECP,
                                             V_CDBENEFFISCAL);
                                       ELSE
                                          INSERT INTO ITEMPROIMPOS(
                                             CDFILIAL,        NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,       NRITPROIMPOS,   CDFISCALIT,
                                             CDSITUTRIBIT,    CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,      VRPERREDBCIT,   VRPARDEDIT,
                                             VRCONTITIMP,     VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,    IDAVISIMPIT,    IDTRIMPSTII,
                                             CDSITUTRIBITNFE, IDMODBCITNFE,   IDMODBCSTITNFE,  CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT,
                                             CDDETALHEPROD,   VRIMPOSTOVISUIT,   CDREGRAIT, CDENQLEGALIPI, CDCEST, VRADICFECP, VRADICALIQFECP,
                                             CDBENEFFISCAL)
                                          VALUES(
                                             P_FILIAL,                P_LANCTONF,     C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, w_2,                 w_2,
                                             NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM,   V_SUFXCFOPPR,           0,                        V_PERDBCALC,         V_PARDEDTMP,
                                             V_CONTABCALC,            0,              0,                      w_00000,                  w_00000,             V_IDTRIMPSTII,
                                             V_CDSITUTRIBNFE,         V_IDMODBCNFEIT, V_IDMODBCSTNFEIT,       V_CDTPCRPICOSTNFEIT,      V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE,
                                             V_CDDETALHEPRODTEMP,     0,     V_CDREGRAITALIQ, V_CDENQLEGALIPI, V_CDCEST, (C_ITEMPROIMPOS.VRADICFECP * V_VRFATOCONV), C_ITEMPROIMPOS.VRADICALIQFECP,
                                             V_CDBENEFFISCAL);
                                       END IF;
                                    END IF;
                                 END IF;
                                 IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                                    INSERT INTO ITEMPROIMPOSORIG(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                             CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                             VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                             IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                             CDDETALHEPROD, CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                                    VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                           C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, TRUNC((C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),2),
                                           TRUNC((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),5), TRUNC((C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),2), 
                                           TRUNC((C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV),2), w_00000, w_00000, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                           C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'), C_ITEMPROIMPOS.IDTRIMPSTII,
                                           V_CDDETALHEPRODTEMP, V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                                 ELSE
                                    INSERT INTO ITEMPROIMPOSORIG(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                             CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                             VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                             IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                             CDDETALHEPROD, CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                                    VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                           C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, (C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),
                                           ROUND((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),5), (C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV), 
                                           (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), w_00000, w_00000, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                           C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'), C_ITEMPROIMPOS.IDTRIMPSTII,
                                           V_CDDETALHEPRODTEMP, V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                                 END IF;
                              END IF;
                           END IF;
                        ELSE
                           Begin
                              SELECT CDFILIAL
                              INTO   V_CHAVE
                              FROM ITEMPROIMPOS
                              WHERE CDFILIAL     = P_FILIAL                 AND
                                    NRLANCTONF   = P_LANCTONF               AND
                                    NRSEQITPROD  = C_ITEMPROD.NRSEQITPROD   AND
                                    CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO AND
                                    NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS;
                           Exception
                              When No_Data_Found Then
                                 V_CHAVE := Null;
                           End;
                           SELECT NVL(MAX(IDPERVISIMPSCRE), 'S')
                             INTO V_IDPERVISIMPSCRE
                             FROM PARAMGERAL;
                           IF V_IDPERVISIMPSCRE = 'S' THEN
                              V_VRIMPOSTOVISUIT := (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV);
                           ELSE
                              IF (V_CDFISCTEMP = '1') THEN
                                V_VRIMPOSTOVISUIT := (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV);
                              ELSE
                                V_VRIMPOSTOVISUIT := 0;
                              END IF;
                           END IF;
                           -- Alterar o valor contábil caso o parâmetro Contabiliza conforme NF de origem esteja desmarcado
                           IF (V_IDCONTCONFORIG =  'N') THEN
                              V_IMPOCONTAB  := 0;
                              V_IDIMPORECUP := NULL;
                              V_CDCCTBLATIV := NULL;
                              V_IDTRATIMPO  := NULL;
                              SELECT NVL(IDIMPORECUP, 'N'), IDTRATIMPO, CDCCTBLATIV
                                INTO V_IDIMPORECUP, V_IDTRATIMPO, V_CDCCTBLATIV
                                FROM IMPOSTO
                               WHERE CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO;
                              IF (V_IDIMPORECUP = W_S) THEN
                                 IF (V_CDFISCTEMP = W_1) THEN
                                    IF (V_CDCCTBLATIV IS NOT NULL) THEN
                                       IF V_IDTRATIMPO = W_V THEN
                                          V_IMPOCONTAB := - C_ITEMPROIMPOS.VRIMPOSTOIT;
                                       END IF;
                                    END IF;
                                 ELSE
                                    IF V_IDTRATIMPO = W_A THEN
                                       V_IMPOCONTAB := V_IMPOSTOIT;
                                    ELSIF V_IDTRATIMPO IN (W_D,W_R) THEN
                                       V_IMPOCONTAB := - C_ITEMPROIMPOS.VRIMPOSTOIT;
                                    END IF;
                                 END IF;
                              END IF;
                              IF (ABS(NVL(V_IMPOCONTAB, 0)) > 0) THEN
                                 UPDATE ITEMPRODUTO
                                    SET VRCONTITEMORIG = (VRCONTITEM) + NVL(V_IMPOCONTAB, 0),
                                        VRCONTITEM     = (VRCONTITEM) + NVL(V_IMPOCONTAB, 0)
                                  WHERE CDFILIAL       = P_FILIAL
                                    AND NRLANCTONF     = P_LANCTONF
                                    AND NRSEQITPROD    = C_ITEMPROD.NRSEQITPROD;
                              END IF;
                           END IF;
                           -- Alterar o valor contábil caso o parâmetro Contabiliza conforme NF de origem esteja desmarcado
                           IF V_CHAVE IS NULL THEN
                              V_ICMS := 'N';
                              FOR C_ICMS IN (SELECT CDIMPOSTO
                                               FROM IMPOSTONFE N, IMPOSTO I
                                              WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                                AND N.CDIMPOSTONFE = '00'
                                                AND I.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO)
                              LOOP
                                 V_ICMS := 'S';
                              END LOOP;
                              IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                                 INSERT INTO ITEMPROIMPOS(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                          CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                          VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                          IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                          CDDETALHEPROD, VRIMPOSTOVISUIT, CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                                 VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, V_CDFISCTEMP,
                                        NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, TRUNC((C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),2),
                                        TRUNC((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),2), TRUNC((C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),2),
                                        TRUNC((C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV),2), w_00000, w_00000, V_CDSITUTRIBNFE,
                                        V_IDMODBCNFEIT, V_IDMODBCSTNFEIT, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE, C_ITEMPROIMPOS.IDTRIMPSTII,
                                        V_CDDETALHEPRODTEMP, TRUNC(V_VRIMPOSTOVISUIT,2), V_CDENQLEGALIPI, V_CDCEST, V_CDBENEFFISCAL);
                              ELSE
                                 INSERT INTO ITEMPROIMPOS(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                          CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                          VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                          IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                          CDDETALHEPROD, VRIMPOSTOVISUIT, CDENQLEGALIPI, CDCEST, VRADICFECP, VRADICALIQFECP, 
                                                          CDBENEFFISCAL)
                                 VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, V_CDFISCTEMP,
                                        NVL(V_SITUTRIBPR, C_ITEMPROD.CDSITUTRIBPR), V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, (C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),
                                        ROUND((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),5), (C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),  
                                        (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), w_00000, w_00000, V_CDSITUTRIBNFE,
                                        V_IDMODBCNFEIT, V_IDMODBCSTNFEIT, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE, C_ITEMPROIMPOS.IDTRIMPSTII,
                                        V_CDDETALHEPRODTEMP, ROUND(V_VRIMPOSTOVISUIT,2), V_CDENQLEGALIPI, V_CDCEST, (C_ITEMPROIMPOS.VRADICFECP * V_VRFATOCONV), C_ITEMPROIMPOS.VRADICALIQFECP,
                                        V_CDBENEFFISCAL);
                              END IF;
                              IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                                 INSERT INTO ITEMPROIMPOSORIG(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                          CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                          VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                          IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                          CDDETALHEPROD, CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                                 VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                        C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, TRUNC((C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),2),
                                        TRUNC((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),2), TRUNC((C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV),2),
                                        TRUNC((C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV),2), w_00000, w_00000, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                        C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'), C_ITEMPROIMPOS.IDTRIMPSTII,
                                        V_CDDETALHEPRODTEMP, C_ITEMPROIMPOS.CDENQLEGALIPI, C_ITEMPROIMPOS.CDCEST, V_CDBENEFFISCAL);
                              ELSE
                                 INSERT INTO ITEMPROIMPOSORIG(CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                                          CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                                          VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                                          IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                                          CDDETALHEPROD, CDENQLEGALIPI, CDCEST, CDBENEFFISCAL)
                                 VALUES(P_FILIAL, P_LANCTONF, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDFISCALIT,
                                        C_ITEMPROD.CDSITUTRIBPR, V_FISOPEITEM, V_SUFXCFOPPR, C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRPERREDBCIT, (C_ITEMPROIMPOS.VRPARDEDIT * V_VRFATOCONV),
                                        ROUND((C_ITEMPROIMPOS.VRCONTITIMP * V_VRFATOCONV),5), (C_ITEMPROIMPOS.VRBASECALCIT * V_VRFATOCONV), 
                                        (C_ITEMPROIMPOS.VRIMPOSTOIT * V_VRFATOCONV), w_00000, w_00000, C_ITEMPROIMPOS.CDSITUTRIBNFE,
                                        C_ITEMPROIMPOS.IDMODBCNFE, C_ITEMPROIMPOS.IDMODBCSTNFE, C_ITEMPROIMPOS.CDTPCREDPICOITE, C_ITEMPROIMPOS.CDNATUBCALPCIT, NVL(C_ITEMPROIMPOS.IDMOTDESONERAIT,'9'), C_ITEMPROIMPOS.IDTRIMPSTII,
                                        V_CDDETALHEPRODTEMP, C_ITEMPROIMPOS.CDENQLEGALIPI, C_ITEMPROIMPOS.CDCEST, V_CDBENEFFISCAL);
                              END IF;
                           END IF;
                        END IF;
                     END IF;
                     DELETE ALIQTEMP
                      WHERE NRALIQTEMP = V_ALIQTEMP;
                  END LOOP;
                  /*Imposto gerado como encargo na importaçdo de xml */
                  BEGIN
                     SELECT NVL(IDNFGERADAVIAXML, 'N')
                       INTO V_IDNFORIGVIAXML
                       FROM NOTAFISCAL N, ITEMPRODUTO IP
                      WHERE N.CDFILIAL   = C_ITEMNOTAFISC.CDFILIALIT
                        AND N.NRLANCTONF = C_ITEMNOTAFISC.NRLANCTONFIT
                        AND N.CDFILIAL   = IP.CDFILIAL
                        AND N.NRLANCTONF = IP.NRLANCTONF
                        AND IP.VRENCARGOIMPXML > 0
                        AND ROWNUM = 1;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_IDNFORIGVIAXML := 'N';
                  END;
                  /*Imposto gerado como encargo na importaçdo de xml*/
                  IF (V_IDNFORIGVIAXML = 'S') AND (V_UPDENCIMPXML = 'N') THEN
                     FOR C_ENCARGO IN (SELECT IMP.CDENCARGOIMPXML
                                         FROM IMPOSTONFE NFE, IMPOSTO IMP
                                        WHERE IMP.CDIMPOSTOXML(+) = NFE.CDIMPOSTONFE
                                          AND IMP.CDENCARGOIMPXML IS NOT NULL
                                         ORDER BY NFE.CDIMPOSTONFE) LOOP
                        UPDATE ITEMENCARGO
                           SET VRITEMENCA = 0
                         WHERE CDFILIAL   = P_FILIAL
                           AND NRLANCTONF = P_LANCTONF
                           AND CDENCARGO  = C_ENCARGO.CDENCARGOIMPXML
                           AND IDALTERADO = 'N';
                     END LOOP;
                     V_UPDENCIMPXML := 'S';
                  END IF;
                  IF (V_IDNFORIGVIAXML = 'S') THEN
                     BEGIN
                        SELECT SUM(I.VRENCARGOIMPXML), SUM(VRTOTAITEM - VRDESCITEM) AS VRLIQUORIG, I.NRSEQITPROD, N.NRACESSONFE
                          INTO V_VRENCARGOIMPXML, V_VRLIQUORIG, V_NRSEQITORIG, V_NRACESSONFORIG
                          FROM ITEMPRODUTO I, NOTAFISCAL N
                         WHERE I.CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT
                           AND I.NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT
                           AND I.CDPRODUTO   = C_ITEMPROD.CDPRODUTO
                           AND I.CDFILIAL    = N.CDFILIAL
                           AND I.NRLANCTONF  = N.NRLANCTONF
                           AND ROUND(I.VRUNITITEM, V_NRDECITOTANF) = ROUND(C_ITEMPROD.VRUNITITEM, V_NRDECITOTANF)
                           AND (NVL(I.NRLOTEESTQ, ' ')  = (NVL(C_ITEMPROD.NRLOTEESTQ, ' ')))
                           AND (NVL(I.NRSUBLOTE , ' ')  = (NVL(C_ITEMPROD.NRSUBLOTE , ' ')))
                           AND  I.VRENCARGOIMPXML > 0
                           AND ROWNUM = 1 
                         GROUP BY I.NRSEQITPROD, N.NRACESSONFE;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                           V_VRENCARGOIMPXML := 0; V_VRLIQUORIG := 0; V_NRSEQITORIG := NULL; V_NRACESSONFORIG := NULL;
                     END;
                     IF (V_VRENCARGOIMPXML > 0) THEN
                        V_VRFATOCONV :=  (C_ITEMPROD.VRTOTAITEM - C_ITEMPROD.VRDESCITEM)/ V_VRLIQUORIG;
                        FOR C_ENCARGOXML IN (SELECT IMP.CDENCARGOIMPXML, IMP.CDIMPOSTO
                                               FROM IMPOSTONFE NFE, IMPOSTO IMP
                                              WHERE IMP.CDIMPOSTOXML(+) = NFE.CDIMPOSTONFE
                                                AND IMP.CDENCARGOIMPXML IS NOT NULL) LOOP
                           BEGIN
                              SELECT IT.VRIMPOSTOIT
                                INTO V_VRIMPENCXMLORIG
                                FROM INTG_TRABITPROIMPOS IT, IMPOSTO I
                               WHERE IT.NRACESSONFE  = V_NRACESSONFORIG
                                 AND IT.CDIMPOSTOIT  = I.CDIMPOSTO
                                 AND LPAD(IT.NRSEQIMPOIT, 4, '0') = V_NRSEQITORIG
                                 AND IT.CDIMPOSTOIT = C_ENCARGOXML.CDIMPOSTO;
                           EXCEPTION
                              WHEN NO_DATA_FOUND THEN
                                 V_VRIMPENCXMLORIG := 0;
                           END;
                           IF V_VRIMPENCXMLORIG > 0 THEN
                              UPDATE ITEMENCARGO
                                 SET VRITEMENCA = VRITEMENCA + (V_VRIMPENCXMLORIG * V_VRFATOCONV)
                               WHERE CDFILIAL   = P_FILIAL
                                 AND NRLANCTONF = P_LANCTONF
                                 AND CDENCARGO  = C_ENCARGOXML.CDENCARGOIMPXML;
                           END IF;
                        END LOOP;
                     END IF;
                  END IF; -- Pegar o valor proporcional do imposto
               END IF;
            END LOOP;
         END IF;
         DELETE CFOPTEMP
          WHERE NRCFOPTEMP = V_CFOPTEMP;
         IF (V_IDOPERDEVONF <> w_S) THEN
            IF V_IDDOCREPLIC IS NOT NULL THEN
               IF LENGTH(V_SITUTRIBPR) = 3 THEN
                  V_SITUTRIBPR := SUBSTR(V_SITUTRIBPR,1,1) || w_90;
               ELSE
                  V_SITUTRIBPR := w_90;
               END IF;
            END IF;
            UPDATE ITEMPRODUTO
            SET CDSITUTRIBPR  = V_SITUTRIBPR
            WHERE CDFILIAL    = P_FILIAL   AND
                  NRLANCTONF  = P_LANCTONF AND
                  NRSEQITPROD = C_ITEMPROD.NRSEQITPROD;
         END IF;
         DELETE CSTTEMP
         WHERE NRCSTTEMP = V_CSTTEMP;
         V_IMPOCONTAB := 0;
      END LOOP;
      SELECT MAX(IDUTCTRLFISSAID)
        INTO V_IDUTCTRLFISSAID
        FROM PARAMGERAL;
      SELECT MAX(IDUTCTRLFISSAID)
        INTO V_IDUTCTRLFISFIL
        FROM PARAMFILIAL
       WHERE CDFILIAL = V_CDFILIEMIT;
      IF V_IDUTCTRLFISSAID = 'S' THEN
         V_IDUTCTRLFIS := 'S';
      ELSE
         IF V_IDUTCTRLFISFIL = 'S' THEN
            V_IDUTCTRLFIS := 'S';
         ELSE
            V_IDUTCTRLFIS := 'N';
         END IF;
      END IF;
      SELECT NVL(MAX(IDGERICMSTDESPA), 'N'), MAX(CDENCARGOICMST), NVL(MAX(IDPEINFICMSTDEME), 'N'), NVL(MAX(IDGERIPIDESPA), 'N')
        INTO V_IDGERICMSTDESPA, V_CDENCARGOICMST, V_IDPEINFICMSTDEME, V_IDGERIPIDESPA
        FROM TIPOOPERAC
       WHERE IDENTRSAIDOP = 'S'
         AND CDTIPOOPER = P_TIPOOPER;
      IF ((V_IDGERICMSTDESPA = 'S') OR (V_IDGERIPIDESPA = 'S')) AND (V_IDUTCTRLFIS = 'S') AND (V_IDNFORIGVIAXML = 'N') THEN
         UPDATE ITEMENCARGO
            SET VRITEMENCA = 0
          WHERE CDFILIAL = P_FILIAL
            AND NRLANCTONF = P_LANCTONF
            AND IDALTERADO = 'N';
      END IF;
      V_VRENCAICMSSTIT := 0;
      FOR C_ITEMNOTAFISC IN (SELECT CDFILIALIT, NRLANCTONFIT
                             FROM ITEMNOTAFISC
                             WHERE CDFILIAL   = P_FILIAL AND
                                   NRLANCTONF = P_LANCTONF) LOOP
         SELECT SUM(VRTOTAITEM - VRDESCITEM) AS VRLIQUORIG
         INTO   V_VRLIQUORIG
         FROM ITEMPRODUTO I
         WHERE CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT AND
               NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT;
         IF V_VRLIQUORIG = 0 THEN
            V_VRFATOCONV := 0;
         ELSE
            V_VRFATOCONV := V_LIQUIDO / V_VRLIQUORIG;
         END IF;
         IF (V_IDNFORIGVIAXML = 'N') THEN
            SELECT SUM(VRITEMENCA)
              INTO V_VRENCAICMSSTIT
              FROM ITEMENCARGO
             WHERE CDFILIAL = C_ITEMNOTAFISC.CDFILIALIT
               AND NRLANCTONF = C_ITEMNOTAFISC.NRLANCTONFIT
               AND CDENCARGO = V_CDENCARGOICMST;
            FOR C_ITEMENCARGO IN (SELECT CDENCARGO, SUM(VRITEMENCA) AS VRITEMENCA
                                  FROM ITEMENCARGO
                                  WHERE CDFILIAL   = C_ITEMNOTAFISC.CDFILIALIT   AND
                                        NRLANCTONF = C_ITEMNOTAFISC.NRLANCTONFIT AND
                                        VRITEMENCA > 0
                                  GROUP BY CDENCARGO) LOOP
                SELECT NVL(MAX(IDALTERADO), 'N')
                  INTO V_IDALTERADO_ENCA
                  FROM ITEMENCARGO
                WHERE CDFILIAL   = P_FILIAL
                  AND NRLANCTONF = P_LANCTONF
                  AND CDENCARGO = C_ITEMENCARGO.CDENCARGO;
                IF V_IDALTERADO_ENCA = 'N' THEN
                   UPDATE ITEMENCARGO
                   SET VRITEMENCA = (C_ITEMENCARGO.VRITEMENCA * V_VRFATOCONV)
                   WHERE CDFILIAL   = P_FILIAL   AND
                         NRLANCTONF = P_LANCTONF AND
                         CDENCARGO  = C_ITEMENCARGO.CDENCARGO;
                END IF;
            END LOOP;
         END IF;   
      END LOOP;
      /*CALCULA DESCONTO DE BONIFICAÇÃO NA DEVOLUÇÃO DE NOTAS */
      SELECT MAX(CDENCDESCBONDEV)
        INTO V_CDENCDESCBONDEV
        FROM PARAMGERAL;
      IF V_CDENCDESCBONDEV IS NOT NULL THEN
         SELECT NVL(MAX(IDCONDESBONFORN), 'S') INTO V_IDCONDESBONFORN
           FROM PARAMFILIAL
          WHERE CDFILIAL = V_CDFILIEMIT;
         SELECT MAX(F.CDAJUCTPAG), MAX(F.VRPEDESCFORN), TO_DATE(NVL(MAX(DTINIVRPDESC), '01/01/0100'))
           INTO V_CDAJUCTPAG, V_VRPEDESCFORN, V_DTINIVRPDESC
           FROM FORNECEDOR F
          WHERE (F.CDFORNECED   = P_CLIENTE)
            AND (NOT F.VRPEDESCFORN IS NULL)
            AND (F.IDTPDESCFORN = 'B');
         IF (V_VRPEDESCFORN IS NOT NULL) AND (V_VRPEDESCFORN <> 0) AND (V_IDCONDESBONFORN = 'S') THEN
            IF (P_INIVALID >= V_DTINIVRPDESC) THEN
               V_VRENCARBONI := ROUND(V_LIQUIDO * (V_VRPEDESCFORN/100),2);
               UPDATE ITEMENCARGO
               SET VRITEMENCA = V_VRENCARBONI
               WHERE CDFILIAL = P_FILIAL
                 AND NRLANCTONF = P_LANCTONF
                 AND CDENCARGO = V_CDENCDESCBONDEV;
            END IF;
         END IF;
      END IF;
      DELETE FROM DADOSADIC
       WHERE CDFILIAL   = P_FILIAL
         AND NRLANCTONF = P_LANCTONF
         AND DSDADOADIC LIKE 'Base de Cálculo do ICMS ST%';
      DELETE FROM DADOSADIC
       WHERE CDFILIAL   = P_FILIAL
         AND NRLANCTONF = P_LANCTONF
         AND DSDADOADIC LIKE 'ICMS ST: R$%';
      DELETE FROM DADOSADIC
       WHERE CDFILIAL   = P_FILIAL
         AND NRLANCTONF = P_LANCTONF
         AND DSDADOADIC LIKE 'Valor de IPI: R$%';
      IF (V_IDUTCTRLFIS = 'S') THEN
         IF V_IDGERICMSTDESPA = 'S' THEN
            IF V_CDENCARGOICMST IS NOT NULL THEN
               SELECT NVL(MAX(IDALTERADO), 'N')
                 INTO V_IDALTERADO_ENCA
                 FROM ITEMENCARGO
               WHERE CDFILIAL   = P_FILIAL
                 AND NRLANCTONF = P_LANCTONF
                 AND CDENCARGO = V_CDENCARGOICMST;
               IF V_IDALTERADO_ENCA = 'N' THEN
                  UPDATE ITEMENCARGO
                     SET VRITEMENCA = 0
                   WHERE CDFILIAL = P_FILIAL
                     AND NRLANCTONF = P_LANCTONF
                     AND CDENCARGO = V_CDENCARGOICMST;
               END IF;
               SELECT MAX(CDENCARGO)
                 INTO V_CDENCARGOOD
                 FROM ENCARGO
                WHERE CDENCARGONFE = '04';
               IF V_CDENCARGOOD IS NOT NULL THEN
                  SELECT NVL(MAX(IDALTERADO), 'N')
                    INTO V_IDALTERADO_ENCA
                    FROM ITEMENCARGO
                   WHERE CDFILIAL = P_FILIAL
                     AND NRLANCTONF = P_LANCTONF
                     AND CDENCARGO = V_CDENCARGOOD;
                  IF V_IDALTERADO_ENCA = 'N' THEN
                     IF (V_VRENCAICMSSTIT > 0) OR (V_VRTOTALICMSST > 0) THEN
                        IF V_IDPEINFICMSTDEME = 'S' THEN
                           SELECT NVL(SUM(VRICMSST),0), NVL(SUM(VRBASECALC),0)
                             INTO V_VRICMSST, V_VRBASECALC
                             FROM INFICMSST
                            WHERE CDFILIAL = P_FILIAL
                              AND NRLANCTONF = P_LANCTONF;
                           IF V_VRICMSST > 0 THEN
                              UPDATE ITEMENCARGO
                                 SET VRITEMENCA = ROUND(VRITEMENCA + V_VRICMSST,2)
                               WHERE CDFILIAL = P_FILIAL
                                 AND NRLANCTONF = P_LANCTONF
                                 AND CDENCARGO = V_CDENCARGOOD;
                              IF V_VRBASECALC > 0 THEN
                                 SELECT COUNT(*)
                                   INTO V_EXISTDSDADOSADIC
                                   FROM DADOSADIC
                                  WHERE CDFILIAL = P_FILIAL
                                    AND NRLANCTONF = P_LANCTONF
                                    AND DSDADOADIC LIKE 'Base de Cálculo do ICMS ST%';
                                 IF V_EXISTDSDADOSADIC = 0 THEN
                                    INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                                                   VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, 'Base de Cálculo do ICMS ST: R$'||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRBASECALC , 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRBASECALC , 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRBASECALC , 2), '9999999999999.99')))|| ' ICMS ST: R$'|| DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99'))));
                                    V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
                                 ELSE
                                    UPDATE DADOSADIC
                                       SET DSDADOADIC = 'Base de Cálculo do ICMS ST: R$'||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRBASECALC, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRBASECALC, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRBASECALC, 2), '9999999999999.99')))|| ' ICMS ST: R$'|| DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')))
                                      WHERE CDFILIAL = P_FILIAL
                                        AND NRLANCTONF = P_LANCTONF
                                        AND DSDADOADIC LIKE 'Base de Cálculo do ICMS ST%';
                                 END IF;
                              ELSE
                                 SELECT COUNT(*)
                                   INTO V_EXISTDSDADOSADIC
                                   FROM DADOSADIC
                                  WHERE CDFILIAL = P_FILIAL
                                    AND NRLANCTONF = P_LANCTONF
                                    AND DSDADOADIC LIKE 'ICMS ST: R$%';
                                 IF V_EXISTDSDADOSADIC = 0 THEN
                                    INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                                                   VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, 'ICMS ST: R$ '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99'))));
                                    V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
                                 ELSE
                                    UPDATE DADOSADIC
                                       SET DSDADOADIC = 'ICMS ST: R$ '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRICMSST, 2), '9999999999999.99')))
                                      WHERE CDFILIAL = P_FILIAL
                                        AND NRLANCTONF = P_LANCTONF
                                        AND DSDADOADIC LIKE 'ICMS ST: R$%';
                                 END IF;
                              END IF;
                           END IF;
                        ELSE
                           UPDATE ITEMENCARGO
                              SET VRITEMENCA = ROUND(VRITEMENCA + (V_VRENCAICMSSTIT * V_VRFATOCONV) + V_VRTOTALICMSST,2)
                            WHERE CDFILIAL = P_FILIAL
                              AND NRLANCTONF = P_LANCTONF
                              AND CDENCARGO = V_CDENCARGOOD;
                           SELECT COUNT(*)
                             INTO V_EXISTDSDADOSADIC
                             FROM DADOSADIC
                            WHERE CDFILIAL = P_FILIAL
                              AND NRLANCTONF = P_LANCTONF
                              AND DSDADOADIC LIKE 'ICMS ST: R$%';
                           IF V_EXISTDSDADOSADIC = 0 THEN
                              INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                                             VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, 'ICMS ST: R$ '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRTOTALICMSST + (V_VRENCAICMSSTIT * V_VRFATOCONV), 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRTOTALICMSST + (V_VRENCAICMSSTIT * V_VRFATOCONV), 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRTOTALICMSST + (V_VRENCAICMSSTIT * V_VRFATOCONV), 2), '9999999999999.99'))));
                              V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
                           ELSE
                              UPDATE DADOSADIC
                                 SET DSDADOADIC = 'ICMS ST: R$ '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRTOTALICMSST + (V_VRENCAICMSSTIT * V_VRFATOCONV), 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRTOTALICMSST + (V_VRENCAICMSSTIT * V_VRFATOCONV), 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRTOTALICMSST + (V_VRENCAICMSSTIT * V_VRFATOCONV), 2), '9999999999999.99')))
                                WHERE CDFILIAL = P_FILIAL
                                  AND NRLANCTONF = P_LANCTONF
                                  AND DSDADOADIC LIKE 'ICMS ST: R$%';
                           END IF;
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END IF;
         END IF;
         IF (V_IDGERIPIDESPA = 'S') THEN
            SELECT MAX(CDENCARGO)
              INTO V_CDENCARGOOD
              FROM ENCARGO
             WHERE CDENCARGONFE = '04';
            IF V_CDENCARGOOD IS NOT NULL THEN
               SELECT NVL(MAX(IDALTERADO), 'N')
                 INTO V_IDALTERADO_ENCA
                 FROM ITEMENCARGO
                WHERE CDFILIAL   = P_FILIAL
                  AND NRLANCTONF = P_LANCTONF
                  AND CDENCARGO  = V_CDENCARGOOD;
               IF V_IDALTERADO_ENCA = 'N' THEN
                  IF (V_VRTOTALIPI > 0) THEN
                     UPDATE ITEMENCARGO
                        SET VRITEMENCA = ROUND(VRITEMENCA + V_VRTOTALIPI, 2)
                      WHERE CDFILIAL = P_FILIAL
                        AND NRLANCTONF = P_LANCTONF
                        AND CDENCARGO = V_CDENCARGOOD;
                     SELECT COUNT(*)
                       INTO V_EXISTDSDADOSADIC
                       FROM DADOSADIC
                      WHERE CDFILIAL = P_FILIAL
                        AND NRLANCTONF = P_LANCTONF
                        AND DSDADOADIC LIKE 'Valor de IPI: R$%';
                     IF (V_EXISTDSDADOSADIC = 0) THEN
                        INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                                       VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, 'Valor de IPI: R$ '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRTOTALIPI, 2),'9999999999999.99')),1, 1),'.','0'||LTRIM(TO_CHAR(ROUND(V_VRTOTALIPI, 2),'9999999999999.99')),LTRIM(TO_CHAR(ROUND(V_VRTOTALIPI, 2),'9999999999999.99'))));
                        V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
                     ELSE
                        UPDATE DADOSADIC
                           SET DSDADOADIC = 'Valor de IPI: R$ '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(V_VRTOTALIPI, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(V_VRTOTALIPI, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(V_VRTOTALIPI, 2), '9999999999999.99')))
                          WHERE CDFILIAL = P_FILIAL
                            AND NRLANCTONF = P_LANCTONF
                            AND DSDADOADIC LIKE 'Valor de IPI: R$%';
                     END IF;
                  END IF;
               END IF;
            END IF;
         END IF;
      END IF;
      --RATEIO DOS ENCARGOS--------------------------------------------------------------------------------------------------------
      FOR C_ITEMENCARGO IN (SELECT IE.CDENCARGO AS CDENCARGO, VRITEMENCA, IDENCARGO,
                                   EN.CDCTBLENTREN, EN.CDCTBLSAIDEN
                            FROM ITEMENCARGO IE, ENCARGO EN
                            WHERE (IE.CDFILIAL   = P_FILIAL)     AND
                                  (IE.NRLANCTONF = P_LANCTONF)   AND
                                  (IE.VRITEMENCA > 0)            AND
                                  (EN.CDENCARGO  = IE.CDENCARGO)) LOOP
         V_TOTAPRODENCA := 0;
         FOR C_VRLIQRATEIO IN (SELECT DISTINCT(IP.NRSEQITPROD) AS NRSEQITPROD, (VRTOTAITEM - VRDESCITEM) AS VRLIQUIDO
                               FROM ITEMPRODUTO IP, ITEMPROIMPOS II
                               WHERE (IP.CDFILIAL    = P_FILIAL)       AND
                                     (IP.NRLANCTONF  = P_LANCTONF)     AND
                                     (II.CDFILIAL    = IP.CDFILIAL)    AND
                                     (II.NRLANCTONF  = IP.NRLANCTONF)  AND
                                     (II.NRSEQITPROD = IP.NRSEQITPROD) AND
                                     (EXISTS(SELECT BC.CDIMPOSTO
                                             FROM BASECALC BC
                                             WHERE (BC.CDENCARGO = C_ITEMENCARGO.CDENCARGO) AND
                                                   (BC.CDIMPOSTO = II.CDIMPOSTO)            AND
                                                   (BC.CDIMPOSTO IN (SELECT B.CDIMPOSTO
                                                                    FROM BASECALC B
                                                                    WHERE B.CDENCARGO = C_ITEMENCARGO.CDENCARGO))))) LOOP
            IF NOT (C_VRLIQRATEIO.VRLIQUIDO IS NULL) THEN
               V_TOTAPRODENCA := V_TOTAPRODENCA + C_VRLIQRATEIO.VRLIQUIDO;
            END IF;
         END LOOP;
         IF V_TOTAPRODENCA = 0 THEN
            IF (V_IDCONTCONFORIG = 'N') THEN
               SELECT SUM(VRTOTAITEM - VRDESCITEM)
               INTO   V_TOTAPRODENCA
               FROM ITEMPRODUTO IP
               WHERE (IP.CDFILIAL   = P_FILIAL)   AND
                     (IP.NRLANCTONF = P_LANCTONF);
               IF NOT V_TOTAPRODENCA IS NULL THEN
                  FOR C_ATUAVRCONT IN (SELECT IP.NRSEQITPROD, (VRTOTAITEM - VRDESCITEM) AS VRLIQUIDO
                                       FROM ITEMPRODUTO IP
                                       WHERE (IP.CDFILIAL    = P_FILIAL) AND
                                             (IP.NRLANCTONF  = P_LANCTONF)) LOOP
                     IF (C_ITEMENCARGO.IDENCARGO = w_A) THEN
                        V_VRBRUTO  := (C_ITEMENCARGO.VRITEMENCA/V_TOTAPRODENCA);
                     ELSIF (C_ITEMENCARGO.IDENCARGO = w_D) THEN
                        V_VRBRUTO  := - (C_ITEMENCARGO.VRITEMENCA/V_TOTAPRODENCA);
                     ELSE
                        V_VRBRUTO := 0;
                     END IF;
                     IF (C_ITEMENCARGO.CDCTBLSAIDEN IS NULL) THEN
                        V_VRCONTAB := V_VRBRUTO;
                     ELSE
                        V_VRCONTAB := 0;
                     END IF;
                     IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                        UPDATE ITEMPRODUTO
                        SET VRBRUTITEM = TRUNC(VRBRUTITEM + (V_VRBRUTO  * (VRTOTAITEM - VRDESCITEM)),2),
                            VRCONTITEM = TRUNC(VRCONTITEM + (V_VRCONTAB * (VRTOTAITEM - VRDESCITEM)),2)
                        WHERE CDFILIAL    = P_FILIAL   AND
                              NRLANCTONF  = P_LANCTONF AND
                              NRSEQITPROD = C_ATUAVRCONT.NRSEQITPROD;
                     ELSE
                        UPDATE ITEMPRODUTO
                        SET VRBRUTITEM = ROUND(VRBRUTITEM + (V_VRBRUTO  * (VRTOTAITEM - VRDESCITEM)),5),
                            VRCONTITEM = ROUND(VRCONTITEM + (V_VRCONTAB * (VRTOTAITEM - VRDESCITEM)),5)
                        WHERE CDFILIAL    = P_FILIAL   AND
                              NRLANCTONF  = P_LANCTONF AND
                              NRSEQITPROD = C_ATUAVRCONT.NRSEQITPROD;
                     END IF;
                  END LOOP;
               END IF;
            END IF;
         ELSE
            IF (V_IDCONTCONFORIG = 'N') THEN
               FOR C_ATUAVRCONT IN (SELECT DISTINCT(IP.NRSEQITPROD) AS NRSEQITPROD, (VRTOTAITEM - VRDESCITEM) AS VRLIQUIDO
                                    FROM ITEMPRODUTO IP, ITEMPROIMPOS II
                                    WHERE (IP.CDFILIAL    = P_FILIAL)       AND
                                          (IP.NRLANCTONF  = P_LANCTONF)     AND
                                          (II.CDFILIAL    = IP.CDFILIAL)    AND
                                          (II.NRLANCTONF  = IP.NRLANCTONF)  AND
                                          (II.NRSEQITPROD = IP.NRSEQITPROD) AND
                                          (EXISTS(SELECT BC.CDIMPOSTO
                                                  FROM BASECALC BC
                                                  WHERE (BC.CDENCARGO = C_ITEMENCARGO.CDENCARGO) AND
                                                        (BC.CDIMPOSTO = II.CDIMPOSTO)            AND
                                                        (BC.CDIMPOSTO IN (SELECT B.CDIMPOSTO
                                                                          FROM BASECALC B
                                                                          WHERE B.CDENCARGO = C_ITEMENCARGO.CDENCARGO))))) LOOP
                  IF (C_ITEMENCARGO.IDENCARGO = w_A) THEN
                     V_VRBRUTO  := (C_ITEMENCARGO.VRITEMENCA/V_TOTAPRODENCA);
                  ELSIF (C_ITEMENCARGO.IDENCARGO = w_D) THEN
                     V_VRBRUTO  := - (C_ITEMENCARGO.VRITEMENCA/V_TOTAPRODENCA);
                  ELSE
                     V_VRBRUTO := 0;
                  END IF;
                  IF (C_ITEMENCARGO.CDCTBLSAIDEN IS NULL) THEN
                     V_VRCONTAB := V_VRBRUTO;
                  ELSE
                     V_VRCONTAB := 0;
                  END IF;
                  IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                     UPDATE ITEMPRODUTO
                     SET VRBRUTITEM = TRUNC(VRBRUTITEM + (V_VRBRUTO  * (VRTOTAITEM - VRDESCITEM)),2),
                         VRCONTITEM = TRUNC(VRCONTITEM + (V_VRCONTAB * (VRTOTAITEM - VRDESCITEM)),2)
                     WHERE CDFILIAL    = P_FILIAL   AND
                           NRLANCTONF  = P_LANCTONF AND
                           NRSEQITPROD = C_ATUAVRCONT.NRSEQITPROD;
                  ELSE
                     UPDATE ITEMPRODUTO
                     SET VRBRUTITEM = ROUND(VRBRUTITEM + (V_VRBRUTO  * (VRTOTAITEM - VRDESCITEM)),5),
                         VRCONTITEM = ROUND(VRCONTITEM + (V_VRCONTAB * (VRTOTAITEM - VRDESCITEM)),5)
                     WHERE CDFILIAL    = P_FILIAL   AND
                           NRLANCTONF  = P_LANCTONF AND
                           NRSEQITPROD = C_ATUAVRCONT.NRSEQITPROD;
                  END IF;
               END LOOP;
            END IF;
            FOR C_ITEMPROIMPOS IN (SELECT II.NRSEQITPROD, II.NRITPROIMPOS, II.CDIMPOSTO, II.VRPERREDBCIT, II.VRALIQITEM,
                                          II.VRPARDEDIT, II.IDTRIMPSTII, (VRTOTAITEM - VRDESCITEM) AS VRLIQUIDO,
                                          NVL(II.VRALIQINTEDEST, 0) AS VRALIQINTEDEST, NVL(II.VRALIQFCP, 0) AS VRALIQFCP,
                                          NVL(IP.IDUTILBCPMC, 'N') AS IDUTILBCPMC, I.CDIMPOSTONFE
                                   FROM ITEMPROIMPOS II, ITEMPRODUTO IP, BASECALC BC, IMPOSTO I
                                   WHERE (II.CDFILIAL     = P_FILIAL)                AND
                                         (II.NRLANCTONF   = P_LANCTONF)              AND
                                         (IP.CDFILIAL     = P_FILIAL)                AND
                                         (IP.NRLANCTONF   = P_LANCTONF)              AND
                                         (IP.NRSEQITPROD  = II.NRSEQITPROD)          AND
                                         (BC.CDIMPOSTO    = II.CDIMPOSTO)            AND
                                         (II.CDIMPOSTO    = I.CDIMPOSTO)             AND
                                         (BC.CDENCARGO    = C_ITEMENCARGO.CDENCARGO)
                                   ORDER BY 1, 2, 3) LOOP
               IF NOT((C_ITEMPROIMPOS.IDUTILBCPMC = W_S) AND (C_ITEMPROIMPOS.CDIMPOSTONFE = '80')) OR (C_ITEMPROIMPOS.IDUTILBCPMC <> W_S) THEN
                  SELECT SUM(VRTOTAITEM - VRDESCITEM)
                    INTO V_TOTAPRENIMPO
                    FROM ITEMPRODUTO IP
                   WHERE IP.CDFILIAL   = P_FILIAL
                     AND IP.NRLANCTONF = P_LANCTONF;
                  IF (V_TOTAPRODENCA <> 0) THEN
                     IF (C_ITEMENCARGO.IDENCARGO = w_A) THEN
                        V_PRODUENCARGO := +(C_ITEMENCARGO.VRITEMENCA/V_TOTAPRENIMPO)*C_ITEMPROIMPOS.VRLIQUIDO;
                     ELSIF (C_ITEMENCARGO.IDENCARGO = w_D) THEN
                        V_PRODUENCARGO := -(C_ITEMENCARGO.VRITEMENCA/V_TOTAPRENIMPO)*C_ITEMPROIMPOS.VRLIQUIDO;
                     ELSE
                        V_PRODUENCARGO := 0;
                     END IF;
                  END IF;
                  V_IDVRIMPSTRIB := null;
                  BEGIN
                     SELECT CASE COUNT(*) WHEN 0 THEN 'M' ELSE 'R' END
                       INTO V_MAJORAOUREDUZ
                       FROM ITEMPROIMPOS
                      WHERE CDFILIAL = P_FILIAL 
                        AND NRLANCTONF = P_LANCTONF
                        AND NRSEQITPROD = C_ITEMPROIMPOS.NRSEQITPROD
                        AND CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO
                        AND NRITPROIMPOS = w_2;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_MAJORAOUREDUZ := 'M';
                  END;
                  V_VRIMPOSTOST := 0;
                  IF V_MAJORAOUREDUZ = 'R' THEN
                     V_TAXABASECALC := (100 - C_ITEMPROIMPOS.VRPERREDBCIT)/100;
                     V_TAXAALIQUOTA := (C_ITEMPROIMPOS.VRALIQITEM/100);
                  ELSE
                     V_TAXABASECALC := 1 + (C_ITEMPROIMPOS.VRPERREDBCIT)/100;
                     V_TAXAALIQUOTA := (C_ITEMPROIMPOS.VRALIQITEM/100);
                     SELECT NVL(MAX(IDVRIMPSTRIB), 'N')
                       INTO V_IDVRIMPSTRIB
                       FROM ITEMPROIMPOS I, IMPOSTO II
                      WHERE (CDFILIAL     = P_FILIAL)
                        AND (NRLANCTONF   = P_LANCTONF)
                        AND (NRSEQITPROD  = C_ITEMPROIMPOS.NRITPROIMPOS)
                        AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0)
                        AND (I.CDIMPOSTO = II.CDIMPOSTO);
                     IF (C_ITEMPROIMPOS.IDTRIMPSTII <> w_1) AND (NVL(V_IDVRIMPSTRIB,w_N) = w_S) THEN
                        Begin
                           SELECT NVL(MAX(VRIMPOSTOIT),0)
                           INTO   V_VRIMPOSTOST
                           FROM ITEMPROIMPOS I
                           WHERE (CDFILIAL     = P_FILIAL)                   AND
                                 (NRLANCTONF   = P_LANCTONF)                 AND
                                 (INSTR(V_CDIMPOSTRIB,CDIMPOSTO||'_') > 0)   AND
                                 (NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS);
                        Exception
                           When No_Data_Found Then
                              V_VRIMPOSTOST := 0;
                        End;
                     END IF;
                  END IF;
                  V_ICMS := 'N';
                  FOR C_ICMS IN (SELECT CDIMPOSTO
                                   FROM IMPOSTONFE N, IMPOSTO I
                                  WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                    AND N.CDIMPOSTONFE = '00'
                                    AND I.CDIMPOSTO = C_ITEMPROIMPOS.CDIMPOSTO)
                  LOOP
                     V_ICMS := 'S';
                  END LOOP;
                  SELECT NVL(MAX(IDPERVISIMPSCRE), 'S')
                    INTO V_IDPERVISIMPSCRE
                    FROM PARAMGERAL;
                  UPDATE ITEMPROIMPOS
                     SET VRCONTITIMP  = VRCONTITIMP  + (V_PRODUENCARGO * V_TAXABASECALC),
                         VRBASECALCIT = VRBASECALCIT + ((V_PRODUENCARGO * V_TAXABASECALC) / (1 - ((C_ITEMPROIMPOS.VRALIQINTEDEST + C_ITEMPROIMPOS.VRALIQFCP) / 100))),
                         VRIMPOSTOIT  = (VRIMPOSTOIT  + (((V_PRODUENCARGO * V_TAXABASECALC) / (1 - ((C_ITEMPROIMPOS.VRALIQINTEDEST + C_ITEMPROIMPOS.VRALIQFCP) / 100))) * V_TAXAALIQUOTA)) - (((V_VRIMPOSTOST * V_TAXABASECALC) / (1 - ((C_ITEMPROIMPOS.VRALIQINTEDEST + C_ITEMPROIMPOS.VRALIQFCP) / 100))) * V_TAXAALIQUOTA),
                         VRIMPOSTOVISUIT = DECODE(V_IDPERVISIMPSCRE, 'S', (VRIMPOSTOIT  + ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA)) - ((V_VRIMPOSTOST * V_TAXABASECALC) * V_TAXAALIQUOTA),  DECODE(CDFISCALIT , '1', (VRIMPOSTOIT  + ((V_PRODUENCARGO * V_TAXABASECALC) * V_TAXAALIQUOTA)) - ((V_VRIMPOSTOST * V_TAXABASECALC) * V_TAXAALIQUOTA),0)),
                         VRADICFECP  = VRADICFECP  + ROUND((V_PRODUENCARGO * (VRADICALIQFECP / 100) * V_TAXABASECALC),2)
                   WHERE (CDFILIAL     = P_FILIAL)                   AND
                         (NRLANCTONF   = P_LANCTONF)                 AND
                         (NRSEQITPROD  = C_ITEMPROIMPOS.NRSEQITPROD) AND
                         (CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO)   AND
                         (NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS);
               END IF;
               /* ROTINA PARA RECALCULAR O DIFERIMENTO DO IMPOSTO */
               BEGIN
                  SELECT MAX(VRIMPOSTOOPERAIT), MAX(VRPERDIFEREIT)
                    INTO V_VRIMPOSTOOPERAIT, V_VRPERDIFEREIT
                    FROM ITEMPROIMPOS
                   WHERE (CDFILIAL     = P_FILIAL)                    AND
                         (NRLANCTONF   = P_LANCTONF)                  AND
                         (NRSEQITPROD  = C_ITEMPROIMPOS.NRSEQITPROD)  AND
                         (CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO)    AND
                         (NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS) AND
                         (VRPERDIFEREIT <> 0);
               EXCEPTION
                  When No_Data_Found Then
                     V_VRIMPOSTOOPERAIT := 0; V_VRPERDIFEREIT := 0;
               END;
               IF V_VRPERDIFEREIT <> 0 THEN
                  V_VRIMPOSTOOPERAIT := ROUND((V_VRIMPOSTOOPERAIT + ((ROUND(V_PRODUENCARGO, 2) * V_TAXABASECALC) * V_TAXAALIQUOTA)) - ((V_VRIMPOSTOST * V_TAXABASECALC) * V_TAXAALIQUOTA),2);
                  V_VRIMPOSTODIFERIT := ROUND((V_VRIMPOSTOOPERAIT * V_VRPERDIFEREIT/100), 2);
                  UPDATE ITEMPROIMPOS
                     SET VRIMPOSTOOPERAIT = V_VRIMPOSTOOPERAIT,
                         VRIMPOSTODIFERIT = V_VRIMPOSTODIFERIT,
                         VRIMPOSTOIT = (V_VRIMPOSTOOPERAIT - V_VRIMPOSTODIFERIT),
                         VRIMPOSTOVISUIT = (V_VRIMPOSTOOPERAIT - V_VRIMPOSTODIFERIT)
                   WHERE (CDFILIAL     = P_FILIAL)                    AND
                         (NRLANCTONF   = P_LANCTONF)                  AND
                         (NRSEQITPROD  = C_ITEMPROIMPOS.NRSEQITPROD)  AND
                         (CDIMPOSTO    = C_ITEMPROIMPOS.CDIMPOSTO)    AND
                         (NRITPROIMPOS = C_ITEMPROIMPOS.NRITPROIMPOS) AND
                         (VRPERDIFEREIT <> 0);
               END IF;
            END LOOP;
         END IF;
      END LOOP;
      FOR C_PRODUTO IN (SELECT IP.NRSEQITPROD AS NRSEQITPROD, II.CDIMPOSTO, VRPERREDBCIT,
                               VRALIQITEM, VRPARDEDIT, CDFISCALIT, VRIMPOSTOIT, IDTRATIMPO,
                               IM.IDIMPORECUP, IM.CDCCTBLPASS, IM.CDCCTBLATIV, NRITPROIMPOS,
                               IDTRIMPSTII, IM.IDTIPCONTAB, II.VRBASECALCIT, IP.IDUTILBCPMC,
                               IM.CDIMPOSTONFE
                        FROM ITEMPRODUTO IP, ITEMPROIMPOS II, IMPOSTO IM
                        WHERE (IP.CDFILIAL     = P_FILIAL)      AND
                              (IP.NRLANCTONF   = P_LANCTONF)    AND
                              (II.CDFILIAL     = IP.CDFILIAL)   AND
                              (II.NRLANCTONF   = IP.NRLANCTONF) AND
                              (IM.CDIMPOSTO    = II.CDIMPOSTO)  AND
                              --(INSTR(V_CDIMPOSRECALC,II.CDIMPOSTO||_) > 0) AND
                              (II.NRITPROIMPOS = w_1)           AND
                              (II.NRSEQITPROD  = IP.NRSEQITPROD)
                        ORDER BY NRSEQITPROD, CDIMPOSTO) LOOP
         IF NOT((C_PRODUTO.IDUTILBCPMC = W_S) AND (C_PRODUTO.CDIMPOSTONFE = '80')) OR (C_PRODUTO.IDUTILBCPMC <> W_S) THEN
            V_IDIMPOBCALTPOP := NULL;
            SELECT TO_CHAR(MAX('S')) , NVL(SUM(VRIMPOSTOIT),0) AS VRIMPOBCALC
              INTO   V_IDIMPOBCALTPOP, V_TOTAIMPBCALC
              FROM ITEMPRODUTO IP, ITEMPROIMPOS II, BASECALCTPOPER BC , IMPOSTO I
             WHERE (BC.CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)
               AND (IP.CDFILIAL     = P_FILIAL)
               AND (IP.NRLANCTONF   = P_LANCTONF)
               AND (II.NRLANCTONF   = IP.NRLANCTONF)
               AND (II.CDFILIAL     = IP.CDFILIAL)
               AND (IP.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
               AND (II.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
               AND (II.NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS)
               AND ((SUBSTR(IP.CDSITUTRIBPR,2,2) = BC.CDCSTIMPOTPOP)  OR (BC.CDCSTIMPOTPOP  IS NULL))
               AND ((SUBSTR(IP.CDFISOPEITEM,2,4) = BC.CDCFOPIMPOTPOP) OR (BC.CDCFOPIMPOTPOP IS NULL))
               AND ((C_PRODUTO.CDFISCALIT        = BC.CDFISCIMPOTPOP) OR (BC.CDFISCIMPOTPOP IS NULL))
               AND (BC.IDENTRSAIDOP = 'S')
               AND (BC.CDTIPOOPER = P_TIPOOPER)
               AND (II.CDIMPOSTO = BC.CDIMPOBCALC)
               AND (BC.CDIMPOBCALC = I.CDIMPOSTO)
               AND (NVL(BC.IDTRATIMPOBCTPOP, I.IDTRATIMPO) <> 'D');
            IF V_IDIMPOBCALTPOP IS NULL THEN
               SELECT NVL(SUM(VRIMPOSTOIT),0) AS VRIMPOBCALC
                 INTO   V_TOTAIMPBCALC
                 FROM ITEMPRODUTO IP, ITEMPROIMPOS II, BASECALC BC, IMPOSTO I
                WHERE (BC.CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)    AND
                      (IP.CDFILIAL     = P_FILIAL)               AND
                      (IP.NRLANCTONF   = P_LANCTONF)             AND
                      (II.NRLANCTONF   = IP.NRLANCTONF)          AND
                      (II.CDFILIAL     = IP.CDFILIAL)            AND
                      (IP.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                      (II.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                      (II.NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS) AND
                      ((SUBSTR(IP.CDSITUTRIBPR,2,2) = BC.CDCSTIMPO)  OR (BC.CDCSTIMPO  IS NULL)) AND
                      ((SUBSTR(IP.CDFISOPEITEM,2,4) = BC.CDCFOPIMPO) OR (BC.CDCFOPIMPO IS NULL)) AND
                      ((C_PRODUTO.CDFISCALIT        = BC.CDFISCIMPO) OR (BC.CDFISCIMPO IS NULL)) AND
                      (II.CDIMPOSTO = BC.CDIMPOBCALC) AND
                      (BC.IDTIPOBCALC   = w_I)AND
                      (BC.CDIMPOBCALC = I.CDIMPOSTO)  AND
                      (NVL(BC.IDTRATIMPOBC, I.IDTRATIMPO) <> 'D');
            END IF;
            V_IDIMBCALTPOPDE := NULL;
            SELECT TO_CHAR(MAX('S')), (NVL(SUM(VRIMPOSTOIT),0)*-1) AS VRIMPOBCALC
              INTO V_IDIMBCALTPOPDE, V_TOTAIMPBCDES
              FROM ITEMPRODUTO IP, ITEMPROIMPOS II, BASECALCTPOPER BC, IMPOSTO I
             WHERE (BC.CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)
               AND (IP.CDFILIAL     = P_FILIAL)
               AND (IP.NRLANCTONF   = P_LANCTONF)
               AND (II.NRLANCTONF   = IP.NRLANCTONF)
               AND (II.CDFILIAL     = IP.CDFILIAL)
               AND (IP.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
               AND (II.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
               AND (II.NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS)
               AND ((SUBSTR(IP.CDSITUTRIBPR,2,2) = BC.CDCSTIMPOTPOP)  OR (BC.CDCSTIMPOTPOP  IS NULL))
               AND ((SUBSTR(IP.CDFISOPEITEM,2,4) = BC.CDCFOPIMPOTPOP) OR (BC.CDCFOPIMPOTPOP IS NULL))
               AND ((C_PRODUTO.CDFISCALIT        = BC.CDFISCIMPOTPOP) OR (BC.CDFISCIMPOTPOP IS NULL))
               AND (BC.IDENTRSAIDOP = 'S')
               AND (BC.CDTIPOOPER = P_TIPOOPER)
               AND (II.CDIMPOSTO = BC.CDIMPOBCALC)
               AND (BC.CDIMPOBCALC = I.CDIMPOSTO)
               AND (NVL(BC.IDTRATIMPOBCTPOP, I.IDTRATIMPO) = 'D');
            IF V_IDIMBCALTPOPDE IS NULL THEN
               SELECT (NVL(SUM(VRIMPOSTOIT),0)*-1) AS VRIMPOBCALC INTO V_TOTAIMPBCDES
                 FROM ITEMPRODUTO IP, ITEMPROIMPOS II, BASECALC BC, IMPOSTO I
                WHERE (BC.CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)    AND
                      (IP.CDFILIAL     = P_FILIAL)               AND
                      (IP.NRLANCTONF   = P_LANCTONF)             AND
                      (II.NRLANCTONF   = IP.NRLANCTONF)          AND
                      (II.CDFILIAL     = IP.CDFILIAL)            AND
                      (IP.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                      (II.NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                      (II.NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS) AND
                      ((SUBSTR(IP.CDSITUTRIBPR,2,2) = BC.CDCSTIMPO)  OR (BC.CDCSTIMPO  IS NULL)) AND
                      ((SUBSTR(IP.CDFISOPEITEM,2,4) = BC.CDCFOPIMPO) OR (BC.CDCFOPIMPO IS NULL)) AND
                      ((C_PRODUTO.CDFISCALIT        = BC.CDFISCIMPO) OR (BC.CDFISCIMPO IS NULL)) AND
                      (II.CDIMPOSTO = BC.CDIMPOBCALC) AND
                      (BC.IDTIPOBCALC   = 'I')        AND
                      (BC.CDIMPOBCALC = I.CDIMPOSTO)  AND
                      (NVL(BC.IDTRATIMPOBC, I.IDTRATIMPO) = 'D');
            END IF;
            V_VRIMPOSTO := C_PRODUTO.VRIMPOSTOIT;
            /*IMPOSTO PARA SUBSTITUIÇÃO TRIBUTARIA*/
            V_CDIMPOSTRIB  := null;
            V_IDVRIMPSTRIB := null;
            FOR C_ICMSST IN (SELECT CDIMPOSTO
                               FROM IMPOSTO
                              WHERE CDIMPOSTRIB = C_PRODUTO.CDIMPOSTO)
            LOOP
               V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
            END LOOP;
            V_VRIMPOSTOST := 0;
            /*Só irá fazer o cálculo de outro imposto compondo a base de cálculo,
              se o valor do mesmo não estiver zerado OU
              se for o imposto de st, pois o icms pode ter sido recalculado(por ter alguem compondo sua bc)
              e o imposto st precisa retirar o valor final do st*/
            IF (NOT(V_TOTAIMPBCALC IS NULL) OR (V_TOTAIMPBCALC <> 0)) OR
               (NOT(V_TOTAIMPBCDES IS NULL) OR (V_TOTAIMPBCDES <> 0)) OR
               (V_CDIMPOSTRIB IS NOT NULL) THEN
               SELECT NVL(MAX(IDPERVISIMPSCRE),'S')
                 INTO V_IDPERVISIMPSCRE
                 FROM PARAMGERAL;
               IF V_CDIMPOSTRIB IS NULL THEN
                  V_TAXABASECALC := (100 - C_PRODUTO.VRPERREDBCIT)/100;
                  V_TAXAALIQUOTA := (C_PRODUTO.VRALIQITEM/100);
                  V_VRIMPOSTO    := V_VRIMPOSTO + (((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * V_TAXAALIQUOTA);
                  UPDATE ITEMPROIMPOS
                     SET VRBASECALCIT = VRBASECALCIT + ((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC),
                         VRIMPOSTOIT  = VRIMPOSTOIT  + (((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * V_TAXAALIQUOTA),
                         VRIMPOSTOVISUIT = DECODE(V_IDPERVISIMPSCRE, 'S', VRIMPOSTOIT  + (((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * V_TAXAALIQUOTA), DECODE(CDFISCALIT, '1', VRIMPOSTOIT  + (((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC) * V_TAXAALIQUOTA), 0))
                   WHERE (CDFILIAL     = P_FILIAL)              AND
                         (NRLANCTONF   = P_LANCTONF)            AND
                         (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD) AND
                         (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)   AND
                         (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS);
                  /* ROTINA PARA RECALCULAR O DIFERIMENTO DO IMPOSTO */
                  BEGIN
                     SELECT MAX(VRIMPOSTOOPERAIT), MAX(VRPERDIFEREIT)
                       INTO V_VRIMPOSTOOPERAIT, V_VRPERDIFEREIT
                       FROM ITEMPROIMPOS
                      WHERE (CDFILIAL     = P_FILIAL)               AND
                            (NRLANCTONF   = P_LANCTONF)             AND
                            (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                            (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)    AND
                            (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS) AND
                            (VRPERDIFEREIT <> 0);
                  EXCEPTION
                     When No_Data_Found Then
                        V_VRIMPOSTOOPERAIT := 0; V_VRPERDIFEREIT := 0;
                  END;
                  IF V_VRPERDIFEREIT <> 0 THEN
                     V_VRIMPOSTOOPERAIT := ROUND((V_VRIMPOSTOOPERAIT + ((ROUND((V_TOTAIMPBCALC+V_TOTAIMPBCDES), 2) * V_TAXABASECALC) * V_TAXAALIQUOTA)),2);
                     V_VRIMPOSTODIFERIT := ROUND((V_VRIMPOSTOOPERAIT * V_VRPERDIFEREIT/100), 2);
                     UPDATE ITEMPROIMPOS
                        SET VRIMPOSTOOPERAIT = V_VRIMPOSTOOPERAIT,
                            VRIMPOSTODIFERIT = V_VRIMPOSTODIFERIT,
                            VRIMPOSTOIT = (V_VRIMPOSTOOPERAIT - V_VRIMPOSTODIFERIT),
                            VRIMPOSTOVISUIT = (V_VRIMPOSTOOPERAIT - V_VRIMPOSTODIFERIT)
                      WHERE (CDFILIAL     = P_FILIAL)               AND
                            (NRLANCTONF   = P_LANCTONF)             AND
                            (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                            (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)    AND
                            (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS) AND
                            (VRPERDIFEREIT <> 0);
                  END IF;
               ELSE
                  V_VRIMPOSST   := 0;
                  V_VRIMPOSTOST := 0;
                  FOR C_SUBSTRIB IN (SELECT NVL(IDVRIMPSTRIB, 'N') IDVRIMPSTRIB, I.CDIMPOSTO
                                       FROM ITEMPROIMPOS I, IMPOSTO II
                                      WHERE (CDFILIAL     = P_FILIAL)
                                        AND (NRLANCTONF   = P_LANCTONF)
                                        AND (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)
                                        AND (NRITPROIMPOS = '1')
                                        AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0)
                                        AND (I.CDIMPOSTO = II.CDIMPOSTO))
                  LOOP
                     IF (C_SUBSTRIB.IDVRIMPSTRIB = w_S) THEN
                        Begin
                           SELECT NVL(ROUND(MAX(VRIMPOSTOIT), 2),0)
                           INTO   V_VRIMPOSST
                           FROM ITEMPROIMPOS I
                           WHERE (CDFILIAL     = P_FILIAL)                   AND
                                 (NRLANCTONF   = P_LANCTONF)                 AND
                                 (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD)  AND
                                 (CDIMPOSTO    = C_SUBSTRIB.CDIMPOSTO)       AND
                                 (INSTR(V_CDIMPOSTRIB,CDIMPOSTO||'_') > 0)   AND
                                 (NRITPROIMPOS = w_1);
                        Exception
                           When No_Data_Found Then
                              V_VRIMPOSST := 0;
                        End;
                     ELSE
                        V_VRIMPOSST := 0;
                     END IF;
                     V_VRIMPOSTOST := V_VRIMPOSTOST + V_VRIMPOSST;
                  END LOOP;
                  V_TAXABASECALC := 1 + (C_PRODUTO.VRPERREDBCIT)/100;
                  V_TAXAALIQUOTA := (C_PRODUTO.VRALIQITEM/100);
                  V_VRIMPOSTO := ((C_PRODUTO.VRBASECALCIT+ ((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC)) * V_TAXAALIQUOTA);
                  SELECT NVL(MAX(IDPERVISIMPSCRE), 'S')
                    INTO V_IDPERVISIMPSCRE
                    FROM PARAMGERAL;
                  IF V_VRIMPOSTOST > V_VRIMPOSTO THEN
                     UPDATE ITEMPROIMPOS
                     SET VRBASECALCIT = VRBASECALCIT + ((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC),
                         VRIMPOSTOIT  = 0,
                         VRIMPOSTOVISUIT = 0
                     WHERE (CDFILIAL     = P_FILIAL)              AND
                           (NRLANCTONF   = P_LANCTONF)            AND
                           (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD) AND
                           (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)   AND
                           (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS);
                     V_VRIMPOSTO := 0;
                  ELSE
                     UPDATE ITEMPROIMPOS
                        SET VRBASECALCIT = VRBASECALCIT + ((V_TOTAIMPBCALC+V_TOTAIMPBCDES) * V_TAXABASECALC),
                            VRIMPOSTOIT  = V_VRIMPOSTO - V_VRIMPOSTOST,
                            VRIMPOSTOVISUIT = DECODE(V_IDPERVISIMPSCRE, 'S', V_VRIMPOSTO - V_VRIMPOSTOST, DECODE(CDFISCALIT, '1', V_VRIMPOSTO - V_VRIMPOSTOST, 0))
                      WHERE (CDFILIAL     = P_FILIAL)              AND
                            (NRLANCTONF   = P_LANCTONF)            AND
                            (NRSEQITPROD  = C_PRODUTO.NRSEQITPROD) AND
                            (CDIMPOSTO    = C_PRODUTO.CDIMPOSTO)   AND
                            (NRITPROIMPOS = C_PRODUTO.NRITPROIMPOS);
                     V_VRIMPOSTO := V_VRIMPOSTO - V_VRIMPOSTOST;
                     IF (V_IDCONTCONFORIG = 'N') THEN
                        UPDATE ITEMPRODUTO
                           SET VRBRUTITEM = VRBRUTITEM + V_VRIMPOSTO,
                               VRCONTITEM = VRCONTITEM + V_VRIMPOSTO
                         WHERE CDFILIAL = P_FILIAL
                           AND NRLANCTONF = P_LANCTONF
                           AND NRSEQITPROD = C_PRODUTO.NRSEQITPROD;
                     END IF;
                  END IF;
               END IF;
            END IF;
         END IF;
      END LOOP;
      IF V_IDOPERDEVONF = w_S THEN
         V_SEQUIMPOSTNF := w_01;
         DELETE ITEMPRODUTO
         WHERE (CDFILIAL   = P_FILIAL)   AND
               (NRLANCTONF = P_LANCTONF) AND
               (QTITEMPROD = 0);
         DELETE IMPOSNFTOT
         WHERE (CDFILIAL   = P_FILIAL) AND
               (NRLANCTONF = P_LANCTONF);
         --TOTALIZAÇÃO DOS IMPOSTOS
         FOR C_IMPOSNFTOT IN (SELECT IT.CDIMPOSTO
                              FROM ITEMPROIMPOS IT
                              WHERE (IT.CDFILIAL   = P_FILIAL) AND
                                    (IT.NRLANCTONF = P_LANCTONF)
                              GROUP BY IT.CDIMPOSTO) LOOP
            V_IMPOSTOIT  := 0;
            V_BASECALCIT := 0;
            SELECT NVL(SUM(IT.VRIMPOSTOIT),0), 
                   NVL(SUM(IT.VRBASECALCIT),0),
                   NVL(SUM(IT.VRADICFECP),0)
            INTO   V_IMPOSTOIT,
                   V_BASECALCIT,
                   V_VRADICFECPNFTOT
            FROM ITEMPROIMPOS IT, IMPOSTO I
            WHERE (IT.CDFILIAL   = P_FILIAL)               AND
                  (IT.NRLANCTONF = P_LANCTONF)             AND
                  (IT.CDIMPOSTO  = C_IMPOSNFTOT.CDIMPOSTO) AND
                  (IT.CDIMPOSTO  = I.CDIMPOSTO);
            V_ICMS := 'N';
            FOR C_ICMS IN (SELECT CDIMPOSTO
                             FROM IMPOSTONFE N, IMPOSTO I
                            WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                              AND N.CDIMPOSTONFE = '00'
                              AND I.CDIMPOSTO = C_IMPOSNFTOT.CDIMPOSTO)
            LOOP
               V_ICMS := 'S';
            END LOOP;
            IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
               INSERT INTO IMPOSNFTOT
                  (CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF,
                   VRADICFECP)
               VALUES 
                  (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, TRUNC(V_IMPOSTOIT ,V_NRDECITOTANF), TRUNC(V_BASECALCIT,V_NRDECITOTANF),
                   V_VRADICFECPNFTOT);
            ELSE
               INSERT INTO IMPOSNFTOT
                  (CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF,
                   VRADICFECP)
               VALUES
                  (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, ROUND(V_IMPOSTOIT ,V_NRDECITOTANF), ROUND(V_BASECALCIT,V_NRDECITOTANF),
                   V_VRADICFECPNFTOT);
            END IF;
         END LOOP;
         DELETE IMPOSTONF
         WHERE (CDFILIAL   = P_FILIAL) AND
               (NRLANCTONF = P_LANCTONF);
         FOR C_ITEMNOTAFISC IN (SELECT CDFILIALIT, NRLANCTONFIT
                                FROM ITEMNOTAFISC
                                WHERE CDFILIAL   = P_FILIAL AND
                                      NRLANCTONF = P_LANCTONF) LOOP
            SELECT SUM(VRTOTAITEM - VRDESCITEM) AS VRLIQUORIG
            INTO   V_VRLIQUORIG
            FROM ITEMPRODUTO I
            WHERE CDFILIAL    = C_ITEMNOTAFISC.CDFILIALIT AND
                  NRLANCTONF  = C_ITEMNOTAFISC.NRLANCTONFIT;
            IF V_VRLIQUORIG = 0 THEN
               V_VRFATOCONV := 0;
            ELSE
               V_VRFATOCONV := V_LIQUIDO / V_VRLIQUORIG;
            END IF;
            /*IMPOSTOS DE CORPO DA NOTA DE ORIGEM*/
            FOR C_IMPOSNFTOT IN (SELECT IT.CDIMPOSTO, IT.VRTOTIMPONF, IT.VRTOTBCALCNF
                                 FROM IMPOSNFTOT IT, IMPOSTO IP
                                 WHERE IT.CDFILIAL   = C_ITEMNOTAFISC.CDFILIALIT   AND
                                       IT.NRLANCTONF = C_ITEMNOTAFISC.NRLANCTONFIT AND
                                       IT.CDIMPOSTO  = IP.CDIMPOSTO AND
                                       IP.IDPRODNOTA = w_C) LOOP
               SELECT NVL(MAX(IDUTMUNIORIG),'N')
                 INTO V_IDUTMUNIORIG
                 FROM IMPOSTO
                WHERE CDIMPOSTO = C_IMPOSNFTOT.CDIMPOSTO;
               IF ((V_IDUTMUNIORIG = 'O') OR (V_IDUTMUNIORIG = 'N')) THEN
                  CONSULTA_ALIQUOTA(C_IMPOSNFTOT.CDIMPOSTO, w_S, P_EMPRESA, '', P_INSCESTA, V_PAISORIG,
                                    V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER);
               ELSE
                  CONSULTA_ALIQUOTA(C_IMPOSNFTOT.CDIMPOSTO, w_S, P_EMPRESA, '', P_INSCESTA, V_PAISORIG,
                                    V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER);
               END IF;
               SELECT MAX(NRALIQTEMP), MAX(VRPARDEDTMP), MAX(VRPREDBCTMP), MAX(VRALIQUTMP), NVL(MAX(IDTRIMPSTTMP), w_0), MAX(CDTIPOOPERTMP)
                 INTO   V_NRALIQTEMP,    V_PARDEDTMP,      V_PREDBCTMP,      V_ALIQUTMP,      V_TRATIMPST, V_CDTIPOOPER
                 FROM ALIQTEMP
                WHERE (NRALIQTEMP = V_ALIQTEMP);
               IF ((V_NRALIQTEMP IS NOT NULL) AND (V_CDTIPOOPER IS NOT NULL) AND (V_ESTORNOCANC <> w_S)) THEN
                  IF (V_ALIQUTMP IS NULL) THEN
                     V_ALIQUTMP := 0;
                  END IF;
                  V_PRODUENCARGO := 0;
                  SELECT SUM(VRITEMENCA) AS VRSOMAITEM
                    INTO   V_SOMAITEM
                    FROM ITEMENCARGO IE, ENCARGO EN, BASECALC BC
                   WHERE (IE.CDFILIAL   = P_FILIAL)
                     AND (IE.NRLANCTONF = P_LANCTONF)
                     AND (EN.CDENCARGO  = IE.CDENCARGO)
                     AND (BC.CDENCARGO  = IE.CDENCARGO)
                     AND (BC.CDIMPOSTO  = C_IMPOSNFTOT.CDIMPOSTO)
                     AND (EN.IDENCARGO  = w_A);
                  SELECT SUM(VRITEMENCA) AS VRDESCITEM
                    INTO   V_DESCITEM
                    FROM ITEMENCARGO IE, ENCARGO EN, BASECALC BC
                   WHERE (IE.CDFILIAL   = P_FILIAL)
                     AND (IE.NRLANCTONF = P_LANCTONF)
                     AND (EN.CDENCARGO  = IE.CDENCARGO)
                     AND (BC.CDENCARGO  = IE.CDENCARGO)
                     AND (BC.CDIMPOSTO  = C_IMPOSNFTOT.CDIMPOSTO)
                     AND (EN.IDENCARGO  = w_D);
                  IF V_SOMAITEM IS NULL THEN
                     V_SOMAITEM := 0;
                  END IF;
                  IF V_DESCITEM IS NULL THEN
                     V_DESCITEM := 0;
                  END IF;
                  IF (V_ALIQUTMP IS NULL) THEN
                     V_ALIQUTMP := 0;
                  END IF;
                  IF (V_PREDBCTMP IS NULL) THEN
                     V_PREDBCTMP := 0;
                  END IF;
                  IF (V_PARDEDTMP IS NULL) THEN
                     V_PARDEDTMP := 0;
                  END IF;
                  V_PRODUENCARGO := V_SOMAITEM - V_DESCITEM;
                  V_TAXABASECALC := (100 - V_PREDBCTMP)/100;
                  V_TAXAALIQUOTA := (V_ALIQUTMP/100);
                  V_BASECALCIT   := (V_LIQUIDO + V_PRODUENCARGO) * V_TAXABASECALC;
                  V_CONTIMPO     := V_BASECALCIT;
                  V_IMPOSTOIT    := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
                  IF V_BASECALCIT < 0 THEN
                     V_BASECALCIT := 0;
                  END IF;
                  V_CONTABCALC := (V_LIQUIDO + V_PRODUENCARGO) - ((V_LIQUIDO + V_PRODUENCARGO) * V_TAXABASECALC);
                  V_PERDBCALC  := 100 - V_PREDBCTMP;
                  IF V_IMPOSTOIT < 0 THEN
                     V_IMPOSTOIT := 0;
                  END IF;
                  V_ICMS := 'N';
                  FOR C_ICMS IN (SELECT CDIMPOSTO
                                   FROM IMPOSTONFE N, IMPOSTO I
                                  WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                    AND N.CDIMPOSTONFE = '00'
                                    AND I.CDIMPOSTO = C_IMPOSNFTOT.CDIMPOSTO)
                  LOOP
                     V_ICMS := 'S';
                  END LOOP;
                  IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                     INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF)
                                       VALUES (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, TRUNC(V_IMPOSTOIT ,V_NRDECITOTANF), TRUNC(V_BASECALCIT,V_NRDECITOTANF));
                  ELSE
                     INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF)
                                       VALUES (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, ROUND(V_IMPOSTOIT ,V_NRDECITOTANF), ROUND(V_BASECALCIT,V_NRDECITOTANF));
                  END IF;
               ELSE
                  V_IMPOSTOIT  := (C_IMPOSNFTOT.VRTOTIMPONF * V_VRFATOCONV);
                  V_BASECALCIT := (C_IMPOSNFTOT.VRTOTBCALCNF * V_VRFATOCONV);
                  V_ALIQUTMP   := NULL;
                  V_UTIBB      := 'N';
                  --integração fiscal especifica com Compass_Argentina atraves da importação de aliquota do imposto IBB
                  --A aliquota de imposto 03(ibb) sera cadastrada na tabela paramintegraibb e tera prioridade sobre o
                  -- cadastro de aliquota realizado em sistema
                  /*IF C_IMPOSNFTOT.CDIMPOSTO = 03 THEN
                     IF V_IDORIGDESTNF = C THEN
                        V_DTVINGENTE := NVL(V_DTINIREFEFAT, P_INIVALID);
                        SELECT NVL(MAX(VRALIQUOTAIBB),NULL)
                        INTO   V_ALIQUTMP
                        FROM PARAMINTEGRAIBB
                        WHERE CDCLIENTE = P_CLIENTE AND
                              V_DTVINGENTE BETWEEN DTVIGINICIALIBB AND DTVIGFINALIBB;
                        IF V_ALIQUTMP IS NOT NULL THEN
                           V_UTIBB := S;
                        END IF;
                     END IF;
                  END IF;
                  IF V_UTIBB = S THEN
                     INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF)
                                    VALUES (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, ROUND((V_LIQUIDO * (V_ALIQUTMP/100))  ,V_NRDECITOTANF), ROUND(V_LIQUIDO,V_NRDECITOTANF));
                  ELSE*/
                     V_ICMS := 'N';
                     FOR C_ICMS IN (SELECT CDIMPOSTO
                                      FROM IMPOSTONFE N, IMPOSTO I
                                     WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                       AND N.CDIMPOSTONFE = '00'
                                       AND I.CDIMPOSTO = C_IMPOSNFTOT.CDIMPOSTO)
                      LOOP
                         V_ICMS := 'S';
                      END LOOP;
                     IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                        INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF)
                                       VALUES (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, TRUNC(V_IMPOSTOIT ,V_NRDECITOTANF), TRUNC(V_BASECALCIT,V_NRDECITOTANF));
                     ELSE
                        INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF)
                                       VALUES (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, ROUND(V_IMPOSTOIT ,V_NRDECITOTANF), ROUND(V_BASECALCIT,V_NRDECITOTANF));
                     END IF;
                  --END IF;
               END IF;
               DELETE ALIQTEMP
               WHERE NRALIQTEMP = V_ALIQTEMP;
            END LOOP;
            FOR C_IMPOSTONF IN (SELECT IT.CDIMPOSTO, IT.NRIMPOSNF, IT.VRCONTABILNF, IT.VRPERREDBCNF, IT.VRBASECALCNF,
                                       IT.VRALIQNF, IT.VRPARDEDNF, IT.VRIMPOSTONF, IT.CDFISCALNF, IT.CDSITUTRIBNF,
                                       IT.CDFISCOPERNF, IT.CDSUFXCFOPNF, IDTRIMPOSTNF, IT.CDSITUTRIBIMNFE, IP.IDIMPORECUP,
									                     IT.CDTPCREDPICONF, IT.CDNATUBCALPCNF, IT.IDMOTDESONERANF
                                FROM IMPOSTONF IT, IMPOSTO IP
                                WHERE IT.CDFILIAL   = C_ITEMNOTAFISC.CDFILIALIT   AND
                                      IT.NRLANCTONF = C_ITEMNOTAFISC.NRLANCTONFIT AND
                                      IT.CDIMPOSTO  = IP.CDIMPOSTO                AND
                                      IP.IDPRODNOTA = w_C) LOOP
               /*VERIFICA CST POR IMPOSTO*/
               V_IMPOICMS := NULL;
               V_CDGRUPOFISCAUX := NULL;
               SELECT MAX(G.CDGRUPOFISC)
                 INTO V_CDGRUPOFISCAUX
                 FROM GRUPOFISCIMPFIL G,
                      (SELECT CDFILIEMIT AS CDFILIAL
                         FROM NOTAFISCAL
                        WHERE CDFILIAL = P_FILIAL
                              AND NRLANCTONF = P_LANCTONF) F
                WHERE G.CDFILIAL = F.CDFILIAL
                  AND NOT G.CDGRUPOFISC IS NULL
                  AND G.CDIMPOSTO = C_IMPOSTONF.CDIMPOSTO;
               IF V_IDORIGDESTNF = 'C' THEN
                  SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                    INTO V_CDGRUPOFISCAUX
                    FROM GRFISIMPIECLIE G,
                         (SELECT CDCLIENTE
                            FROM NOTAFISCAL
                           WHERE CDFILIAL = P_FILIAL
                             AND NRLANCTONF = P_LANCTONF) N
                   WHERE G.CDCLIENTE = N.CDCLIENTE
                     AND G.CDEMPRESA = P_EMPRESA
                     AND G.CDINSCESTA = P_INSCESTA
                     AND NOT G.CDGRUPOFISC IS NULL
                     AND G.CDIMPOSTO = C_IMPOSTONF.CDIMPOSTO;
               ELSE
                  SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                    INTO V_CDGRUPOFISCAUX
                    FROM GRFISIMPIEFORN G,
                        (SELECT CDFORNECED
                           FROM NOTAFISCAL
                          WHERE CDFILIAL = P_FILIAL
                            AND NRLANCTONF = P_LANCTONF) N
                   WHERE G.CDFORNECED = N.CDFORNECED
                     AND G.CDEMPRESA = P_EMPRESA
                     AND G.CDINSCESTA = P_INSCESTA
                     AND NOT G.CDGRUPOFISC IS NULL
                     AND G.CDIMPOSTO = C_IMPOSTONF.CDIMPOSTO;
               END IF;
               IF V_CDGRUPOFISCAUX IS NULL THEN
                  CONSULTA_CST_NFE(w_S, P_EMPRESA, '', P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISC,C_IMPOSTONF.CDIMPOSTO, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);
               ELSE
                  CONSULTA_CST_NFE(w_S, P_EMPRESA, '', P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISCAUX,C_IMPOSTONF.CDIMPOSTO, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);
               END IF;
               IF (C_IMPOSTONF.CDSITUTRIBIMNFE IS NULL) THEN
                  V_CDSITUTRIBNFE := V_CDSITTRIBTMP;
               ELSE
                  V_CDSITUTRIBNFE := C_IMPOSTONF.CDSITUTRIBIMNFE;
               END IF;
               IF (C_IMPOSTONF.CDTPCREDPICONF IS NULL) THEN
                  V_CDTPCRPICOSTNFEIT := V_CDTPCRPICOSTNFE;
               ELSE
                  V_CDTPCRPICOSTNFEIT := C_IMPOSTONF.CDTPCREDPICONF;
               END IF;
               IF (C_IMPOSTONF.CDNATUBCALPCNF IS NULL) THEN
                  V_CDNATUBCPCSTNFEIT := V_CDNATUBCPCSTNFE;
               ELSE
                  V_CDNATUBCPCSTNFEIT := C_IMPOSTONF.CDNATUBCALPCNF;
               END IF;
               IF (C_IMPOSTONF.IDMOTDESONERANF IS NULL) THEN
                  V_IDMOTIDESSTNFE := V_IDMOTIDESSTNFE;
               ELSE
                  V_IDMOTIDESSTNFE := C_IMPOSTONF.IDMOTDESONERANF;
               END IF;
               SELECT NVL(MAX(IDUTMUNIORIG),'N')
                 INTO V_IDUTMUNIORIG
                 FROM IMPOSTO
                WHERE CDIMPOSTO = C_IMPOSTONF.CDIMPOSTO;
               IF ((V_IDUTMUNIORIG = 'O') OR (V_IDUTMUNIORIG = 'N')) THEN
                  CONSULTA_ALIQUOTA(C_IMPOSTONF.CDIMPOSTO, w_S, P_EMPRESA, '', P_INSCESTA, V_PAISORIG,
                                    V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIORIG, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC,P_TIPOOPER);
               ELSE
                  CONSULTA_ALIQUOTA(C_IMPOSTONF.CDIMPOSTO, w_S, P_EMPRESA, '', P_INSCESTA, V_PAISORIG,
                                    V_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_LIQUIITEM, P_INIVALID, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_TIPOOPER);
               END IF;
               SELECT MAX(NRALIQTEMP), MAX(VRPARDEDTMP), MAX(VRPREDBCTMP), MAX(VRALIQUTMP), NVL(MAX(IDTRIMPSTTMP), w_0), MAX(CDTIPOOPERTMP)
                 INTO   V_NRALIQTEMP,    V_PARDEDTMP,      V_PREDBCTMP,      V_ALIQUTMP,      V_TRATIMPST, V_CDTIPOOPER
                 FROM ALIQTEMP
                WHERE (NRALIQTEMP = V_ALIQTEMP);
               IF ((V_NRALIQTEMP IS NOT NULL) AND (V_CDTIPOOPER IS NOT NULL)) THEN
                  IF (V_ALIQUTMP IS NULL) THEN
                     V_ALIQUTMP := 0;
                  END IF;
                  V_PRODUENCARGO := 0;
                  SELECT SUM(VRITEMENCA) AS VRSOMAITEM
                    INTO   V_SOMAITEM
                    FROM ITEMENCARGO IE, ENCARGO EN, BASECALC BC
                   WHERE (IE.CDFILIAL   = P_FILIAL)
                     AND (IE.NRLANCTONF = P_LANCTONF)
                     AND (EN.CDENCARGO  = IE.CDENCARGO)
                     AND (BC.CDENCARGO  = IE.CDENCARGO)
                     AND (BC.CDIMPOSTO  = C_IMPOSTONF.CDIMPOSTO)
                     AND (EN.IDENCARGO  = w_A);
                  SELECT SUM(VRITEMENCA) AS VRDESCITEM
                    INTO   V_DESCITEM
                    FROM ITEMENCARGO IE, ENCARGO EN, BASECALC BC
                   WHERE (IE.CDFILIAL   = P_FILIAL)
                     AND (IE.NRLANCTONF = P_LANCTONF)
                     AND (EN.CDENCARGO  = IE.CDENCARGO)
                     AND (BC.CDENCARGO  = IE.CDENCARGO)
                     AND (BC.CDIMPOSTO  = C_IMPOSTONF.CDIMPOSTO)
                     AND (EN.IDENCARGO  = w_D);
                  IF V_SOMAITEM IS NULL THEN
                     V_SOMAITEM := 0;
                  END IF;
                  IF V_DESCITEM IS NULL THEN
                     V_DESCITEM := 0;
                  END IF;
                  IF (V_ALIQUTMP IS NULL) THEN
                     V_ALIQUTMP := 0;
                  END IF;
                  IF (V_PREDBCTMP IS NULL) THEN
                     V_PREDBCTMP := 0;
                  END IF;
                  IF (V_PARDEDTMP IS NULL) THEN
                     V_PARDEDTMP := 0;
                  END IF;
                  V_PRODUENCARGO := V_SOMAITEM - V_DESCITEM;
                  V_TAXABASECALC := (100 - V_PREDBCTMP)/100;
                  V_TAXAALIQUOTA := (V_ALIQUTMP/100);
                  V_BASECALCIT   := (V_LIQUIDO + V_PRODUENCARGO) * V_TAXABASECALC;
                  V_CONTIMPO     := V_BASECALCIT;
                  V_IMPOSTOIT    := (V_BASECALCIT * V_TAXAALIQUOTA) - V_PARDEDTMP;
                  IF V_BASECALCIT < 0 THEN
                     V_BASECALCIT := 0;
                  END IF;
                  V_CONTABCALC := (V_LIQUIDO + V_PRODUENCARGO) - ((V_LIQUIDO + V_PRODUENCARGO) * V_TAXABASECALC);
                  V_PERDBCALC  := 100 - V_PREDBCTMP;
                  IF V_IMPOSTOIT < 0 THEN
                     V_IMPOSTOIT := 0;
                  END IF;
                  IF (C_IMPOSTONF.IDIMPORECUP = w_S) OR (C_IMPOSTONF.IDIMPORECUP <> w_S AND V_ALIQUTMP <> 0) THEN
                     IF ((C_IMPOSTONF.IDIMPORECUP = w_S) AND (C_IMPOSTONF.CDFISCALNF <> w_1)) THEN
                        V_BASECALCIT := 0;
                        V_ALIQUTMP   := 0;
                        V_IMPOSTOIT  := 0;
                        V_PREDBCTMP  := 0;
                        V_PARDEDTMP  := 0;
                     END IF;
                     V_ICMS := 'N';
                     FOR C_ICMS IN (SELECT CDIMPOSTO
                                      FROM IMPOSTONFE N, IMPOSTO I
                                     WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                       AND N.CDIMPOSTONFE = '00'
                                       AND I.CDIMPOSTO = C_IMPOSTONF.CDIMPOSTO)
                     LOOP
                        V_ICMS := 'S';
                     END LOOP;
                     IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                        INSERT INTO IMPOSTONF(
                           CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                           VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, IDTRIMPOSTNF, CDSITUTRIBIMNFE, CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
                        VALUES(
                           P_FILIAL, P_LANCTONF, C_IMPOSTONF.CDIMPOSTO, C_IMPOSTONF.NRIMPOSNF, TRUNC(V_CONTIMPO,2), V_PREDBCTMP,
                           TRUNC(V_BASECALCIT,2), V_ALIQUTMP, TRUNC(V_PARDEDTMP,2), TRUNC(V_IMPOSTOIT,2), C_IMPOSTONF.CDSUFXCFOPNF, C_IMPOSTONF.IDTRIMPOSTNF, V_CDSITTRIBTMP, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE);
                     ELSE
                        INSERT INTO IMPOSTONF(
                           CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                           VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, IDTRIMPOSTNF, CDSITUTRIBIMNFE, CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
                        VALUES(
                           P_FILIAL, P_LANCTONF, C_IMPOSTONF.CDIMPOSTO, C_IMPOSTONF.NRIMPOSNF, ROUND(V_CONTIMPO,2), V_PREDBCTMP,
                           V_BASECALCIT, V_ALIQUTMP, V_PARDEDTMP, V_IMPOSTOIT, C_IMPOSTONF.CDSUFXCFOPNF, C_IMPOSTONF.IDTRIMPOSTNF, V_CDSITTRIBTMP, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE);
                     END IF;
                     IF ((C_IMPOSTONF.IDIMPORECUP = w_S) AND (V_PREDBCTMP > 0) AND (C_IMPOSTONF.CDFISCALNF = w_1)) THEN
                        INSERT INTO IMPOSTONF(
                           CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                           VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, CDSITUTRIBIMNFE, CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
                        VALUES(
                           P_FILIAL, P_LANCTONF, C_IMPOSTONF.CDIMPOSTO, C_IMPOSTONF.NRIMPOSNF, V_CONTABCALC, V_PERDBCALC,
                           0, 0, V_PARDEDTMP, 0, w_2, V_CDSITTRIBTMP, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE);
                     END IF;
                  END IF;
                  DELETE ALIQTEMP
                  WHERE NRALIQTEMP = V_ALIQTEMP;
               ELSE
                  V_ALIQUTMP   := NULL;
                  V_UTIBB      := 'N';
                  --integração fiscal especifica com Compass_Argentina atraves da importação de aliquota do imposto IBB
                  --A aliquota de imposto 03(ibb) sera cadastrada na tabela paramintegraibb e tera prioridade sobre o
                  -- cadastro de aliquota realizado em sistema
                  /*IF C_IMPOSTONF.CDIMPOSTO = 03 THEN
                     IF V_IDORIGDESTNF = C THEN
                        V_DTVINGENTE := NVL(V_DTINIREFEFAT, P_INIVALID);
                        SELECT NVL(MAX(VRALIQUOTAIBB),NULL)
                        INTO   V_ALIQUTMP
                        FROM PARAMINTEGRAIBB
                        WHERE CDCLIENTE = P_CLIENTE AND
                              V_DTVINGENTE BETWEEN DTVIGINICIALIBB AND DTVIGFINALIBB;
                        IF V_ALIQUTMP IS NOT NULL THEN
                           V_UTIBB := S;
                        END IF;
                     END IF;
                  END IF;*/
                  IF V_UTIBB = 'S' THEN
                     V_ICMS := 'N';
                     FOR C_ICMS IN (SELECT CDIMPOSTO
                                      FROM IMPOSTONFE N, IMPOSTO I
                                     WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                       AND N.CDIMPOSTONFE = '00'
                                       AND I.CDIMPOSTO = C_IMPOSTONF.CDIMPOSTO)
                     LOOP
                        V_ICMS := 'S';
                     END LOOP;
                     IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                        INSERT INTO IMPOSTONF(
                           CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                           VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, CDSITUTRIBNF, CDFISCOPERNF, CDSUFXCFOPNF, IDTRIMPOSTNF, CDSITUTRIBIMNFE, CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
                        VALUES(P_FILIAL, P_LANCTONF, C_IMPOSTONF.CDIMPOSTO, C_IMPOSTONF.NRIMPOSNF,
                               V_LIQUIDO, 0, V_LIQUIDO,
                               V_ALIQUTMP, 0, TRUNC((V_LIQUIDO * (V_ALIQUTMP/100)),2), C_IMPOSTONF.CDFISCALNF,
                               C_IMPOSTONF.CDSITUTRIBNF, C_IMPOSTONF.CDFISCOPERNF, C_IMPOSTONF.CDSUFXCFOPNF, C_IMPOSTONF.IDTRIMPOSTNF, C_IMPOSTONF.CDSITUTRIBIMNFE, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE);
                     ELSE
                        INSERT INTO IMPOSTONF(
                           CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                           VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, CDSITUTRIBNF, CDFISCOPERNF, CDSUFXCFOPNF, IDTRIMPOSTNF, CDSITUTRIBIMNFE, CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
                        VALUES(P_FILIAL, P_LANCTONF, C_IMPOSTONF.CDIMPOSTO, C_IMPOSTONF.NRIMPOSNF,
                               V_LIQUIDO, 0, V_LIQUIDO,
                               V_ALIQUTMP, 0, (V_LIQUIDO * (V_ALIQUTMP/100)), C_IMPOSTONF.CDFISCALNF,
                               C_IMPOSTONF.CDSITUTRIBNF, C_IMPOSTONF.CDFISCOPERNF, C_IMPOSTONF.CDSUFXCFOPNF, C_IMPOSTONF.IDTRIMPOSTNF, C_IMPOSTONF.CDSITUTRIBIMNFE, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE);
                     END IF;
                  ELSE
                     V_ICMS := 'N';
                     FOR C_ICMS IN (SELECT CDIMPOSTO
                                      FROM IMPOSTONFE N, IMPOSTO I
                                     WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                                       AND N.CDIMPOSTONFE = '00'
                                       AND I.CDIMPOSTO = C_IMPOSTONF.CDIMPOSTO)
                     LOOP
                        V_ICMS := 'S';
                     END LOOP;
                     IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                        INSERT INTO IMPOSTONF(
                           CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                           VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, CDSITUTRIBNF, CDFISCOPERNF, CDSUFXCFOPNF, IDTRIMPOSTNF, CDSITUTRIBIMNFE, CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
                        VALUES(P_FILIAL, P_LANCTONF, C_IMPOSTONF.CDIMPOSTO, C_IMPOSTONF.NRIMPOSNF,
                               TRUNC((C_IMPOSTONF.VRCONTABILNF * V_VRFATOCONV),2), C_IMPOSTONF.VRPERREDBCNF, TRUNC((C_IMPOSTONF.VRBASECALCNF * V_VRFATOCONV),2),
                               C_IMPOSTONF.VRALIQNF, TRUNC((C_IMPOSTONF.VRPARDEDNF * V_VRFATOCONV),2), TRUNC((C_IMPOSTONF.VRIMPOSTONF * V_VRFATOCONV),2), C_IMPOSTONF.CDFISCALNF,
                               C_IMPOSTONF.CDSITUTRIBNF, C_IMPOSTONF.CDFISCOPERNF, C_IMPOSTONF.CDSUFXCFOPNF, C_IMPOSTONF.IDTRIMPOSTNF, C_IMPOSTONF.CDSITUTRIBIMNFE, V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE);
                     ELSE
                        INSERT INTO IMPOSTONF(
                           CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
                           VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, CDSITUTRIBNF, CDFISCOPERNF, CDSUFXCFOPNF, IDTRIMPOSTNF, CDSITUTRIBIMNFE, CDTPCREDPICONF, CDNATUBCALPCNF, IDMOTDESONERANF)
                        VALUES(P_FILIAL, P_LANCTONF, C_IMPOSTONF.CDIMPOSTO, C_IMPOSTONF.NRIMPOSNF,
                               ROUND((C_IMPOSTONF.VRCONTABILNF * V_VRFATOCONV),2), C_IMPOSTONF.VRPERREDBCNF, (C_IMPOSTONF.VRBASECALCNF * V_VRFATOCONV),
                               C_IMPOSTONF.VRALIQNF, (C_IMPOSTONF.VRPARDEDNF * V_VRFATOCONV), (C_IMPOSTONF.VRIMPOSTONF * V_VRFATOCONV), C_IMPOSTONF.CDFISCALNF,
                               C_IMPOSTONF.CDSITUTRIBNF, C_IMPOSTONF.CDFISCOPERNF, C_IMPOSTONF.CDSUFXCFOPNF, C_IMPOSTONF.IDTRIMPOSTNF, C_IMPOSTONF.CDSITUTRIBIMNFE,  V_CDTPCRPICOSTNFEIT, V_CDNATUBCPCSTNFEIT, V_IDMOTIDESSTNFE);
                     END IF;
                  END IF;
                   V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
               END IF;
               DELETE ALIQTEMP
               WHERE NRALIQTEMP = V_ALIQTEMP;
            END LOOP;
         END LOOP;
         DELETE FROM DADOSADIC
         WHERE CDFILIAL = P_FILIAL
           AND NRLANCTONF = P_LANCTONF
           AND DSDADOADIC LIKE 'Nota de Origem:%';   
         IF (V_IDCONTCONFORIG = 'N') THEN
            DELETE ITEMPROIMPOSORIG
             WHERE CDFILIAL   = P_FILIAL
               AND NRLANCTONF = P_LANCTONF;
            FOR C_IMPOSTOITENS IN (SELECT CDFILIAL,     NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                          CDSITUTRIBIT, CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                          VRCONTITIMP,  VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                          IDMODBCITNFE, IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                          CDDETALHEPROD, CDENQLEGALIPI, CDCEST,          CDBENEFFISCAL
                                     FROM ITEMPROIMPOS
                                    WHERE CDFILIAL   = P_FILIAL
                                      AND NRLANCTONF = P_LANCTONF) LOOP
               INSERT INTO ITEMPROIMPOSORIG(CDFILIAL,      NRLANCTONF,     NRSEQITPROD,     CDIMPOSTO,      NRITPROIMPOS,    CDFISCALIT,
                                            CDSITUTRIBIT,  CDFISCOPERIT,   CDSUFXCFOPIT,    VRALIQITEM,     VRPERREDBCIT,    VRPARDEDIT,
                                            VRCONTITIMP,   VRBASECALCIT,   VRIMPOSTOIT,     IDERROSIMPIT,   IDAVISIMPIT,     CDSITUTRIBITNFE,
                                            IDMODBCITNFE,  IDMODBCSTITNFE, CDTPCREDPICOITE, CDNATUBCALPCIT, IDMOTDESONERAIT, IDTRIMPSTII,
                                            CDDETALHEPROD, CDENQLEGALIPI,  CDCEST,          CDBENEFFISCAL)
                    VALUES (C_IMPOSTOITENS.CDFILIAL,      C_IMPOSTOITENS.NRLANCTONF,     C_IMPOSTOITENS.NRSEQITPROD,     C_IMPOSTOITENS.CDIMPOSTO,      C_IMPOSTOITENS.NRITPROIMPOS,    C_IMPOSTOITENS.CDFISCALIT,
                            C_IMPOSTOITENS.CDSITUTRIBIT,  C_IMPOSTOITENS.CDFISCOPERIT,   C_IMPOSTOITENS.CDSUFXCFOPIT,    C_IMPOSTOITENS.VRALIQITEM,     C_IMPOSTOITENS.VRPERREDBCIT,    C_IMPOSTOITENS.VRPARDEDIT,
                            C_IMPOSTOITENS.VRCONTITIMP,   C_IMPOSTOITENS.VRBASECALCIT,   C_IMPOSTOITENS.VRIMPOSTOIT,     C_IMPOSTOITENS.IDERROSIMPIT,   C_IMPOSTOITENS.IDAVISIMPIT,     C_IMPOSTOITENS.CDSITUTRIBITNFE,
                            C_IMPOSTOITENS.IDMODBCITNFE,  C_IMPOSTOITENS.IDMODBCSTITNFE, C_IMPOSTOITENS.CDTPCREDPICOITE, C_IMPOSTOITENS.CDNATUBCALPCIT, C_IMPOSTOITENS.IDMOTDESONERAIT, C_IMPOSTOITENS.IDTRIMPSTII,
                            C_IMPOSTOITENS.CDDETALHEPROD, C_IMPOSTOITENS.CDENQLEGALIPI,  C_IMPOSTOITENS.CDCEST,          C_IMPOSTOITENS.CDBENEFFISCAL);
            END LOOP;
         END IF;
         FOR C_ITEMNOTAFISC IN (SELECT IT.CDFILIALIT, IT.NRLANCTONFIT, NVL(N.NRNOTAFISCALELE, N.NRNOTAFISC) AS NRNOTA,
                                       TO_CHAR(N.DTEMISSAO,  'DD/MM/YYYY') AS DTEMISSAO, N.VRNOTAFISC
                             FROM ITEMNOTAFISC IT, NOTAFISCAL N
                             WHERE IT.CDFILIAL   = P_FILIAL
                               AND IT.NRLANCTONF = P_LANCTONF
                               AND IT.CDFILIALIT   = N.CDFILIAL
                               AND IT.NRLANCTONFIT = N.NRLANCTONF) LOOP
            INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
                           VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, 'Nota de Origem: '|| C_ITEMNOTAFISC.NRNOTA||' (Emissão: '|| C_ITEMNOTAFISC.DTEMISSAO||' Total: '||DECODE(SUBSTR(LTRIM(TO_CHAR(ROUND(C_ITEMNOTAFISC.VRNOTAFISC, 2), '9999999999999.99')), 1, 1), '.', '0'||LTRIM(TO_CHAR(ROUND(C_ITEMNOTAFISC.VRNOTAFISC, 2), '9999999999999.99')), LTRIM(TO_CHAR(ROUND(C_ITEMNOTAFISC.VRNOTAFISC, 2), '9999999999999.99')))|| ' )');
            V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
         END LOOP;
      END IF;
-------------------
   ELSIF (P_TIPOOPER = w_03) THEN /*NOTA DE COMPLEMENTO*/
      FOR C_IMPOSITEMCOM IN (SELECT II.NRSEQITPROD,  II.CDIMPOSTO,     II.NRITPROIMPOS,   II.VRCONTITIMP,
                                    II.VRPERREDBCIT, II.VRBASECALCIT,  II.VRALIQITEM,     II.VRPARDEDIT,
                                    II.VRIMPOSTOIT,  II.CDFISCALIT,    I.IDUTVLRQTDEIMPO, II.CDSITUTRIBIT ,
                                    II.CDFISCOPERIT, II.VRPERMAJBCIT,  IT.CDPRODUTO,      IT.QTITEMPROD, 
                                    P.CDARVPROD,     II.CDENQLEGALIPI, II.CDCEST
                               FROM ITEMPROIMPOS II, IMPOSTO I, ITEMPRODUTO IT, PRODUTO P
                              WHERE (II.CDFILIAL   = P_FILIAL)
                                AND (II.NRLANCTONF = P_LANCTONF)
                                AND (II.NRITPROIMPOS = '1')
                                AND (II.CDIMPOSTO = I.CDIMPOSTO)
                                AND (P.CDPRODUTO  = IT.CDPRODUTO)
                                AND (II.CDFILIAL = IT.CDFILIAL)
                                AND (II.NRLANCTONF = IT.NRLANCTONF)
                                AND (II.NRSEQITPROD = IT.NRSEQITPROD)
                           ORDER BY II.NRSEQITPROD, II.CDIMPOSTO, II.NRITPROIMPOS) LOOP
         /*VERIFICA CSOSN POR IMPOSTO*/
         CONSULTA_CSOSN('S', P_EMPRESA, C_IMPOSITEMCOM.CDARVPROD, P_INSCESTA, P_TIPOOPER, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, V_IDINTEREST, V_CDFISCOPERSN);
         UPDATE ITEMPRODUTO
            SET CDFISCOPERSN  = V_CDFISCOPERSN
          WHERE CDFILIAL    = P_FILIAL
            AND NRLANCTONF  = P_LANCTONF
            AND NRSEQITPROD = C_IMPOSITEMCOM.NRSEQITPROD;
         IF (C_IMPOSITEMCOM.VRIMPOSTOIT = 0)  THEN
            IF (C_IMPOSITEMCOM.VRALIQITEM <> 0)  THEN
               V_TAXAALIQUOTA := (C_IMPOSITEMCOM.VRALIQITEM/100);
               V_TAXABASECALC := (100 - C_IMPOSITEMCOM.VRPERREDBCIT)/100;
               V_TAXABCSUBSTR := (100 + C_IMPOSITEMCOM.VRPERMAJBCIT)/100;
               SELECT MAX(VRTOTAITEM), MAX(QTITEMPROD)
                 INTO V_VRTOTAITEM, V_QTITEMPROD
                 FROM ITEMPRODUTO
                WHERE CDFILIAL = P_FILIAL
                  AND NRLANCTONF = P_LANCTONF
                  AND NRSEQITPROD = C_IMPOSITEMCOM.NRSEQITPROD;
               IF C_IMPOSITEMCOM.VRBASECALCIT > 0 THEN
                  V_BASECALCIT := C_IMPOSITEMCOM.VRBASECALCIT;
               ELSE
                  V_BASECALCIT := V_VRTOTAITEM * V_TAXABASECALC;
               END IF;
               V_PERDBCALC := 100 - C_IMPOSITEMCOM.VRPERREDBCIT;
               V_CONTABCALC  := V_VRTOTAITEM - (V_VRTOTAITEM * V_TAXABASECALC);
               IF C_IMPOSITEMCOM.VRPERMAJBCIT > 0 THEN
                  SELECT MAX(CDIMPOSTO)
                    INTO V_CDIMPOSTO
                    FROM IMPOSTO
                   WHERE CDIMPOSTRIB = C_IMPOSITEMCOM.CDIMPOSTO;
                  IF (V_CDIMPOSTO IS NOT NULL) THEN
                     V_BASECALCIT := (V_BASECALCIT * V_TAXABCSUBSTR);
                  ELSE
                     UPDATE ITEMPROIMPOS
                     SET VRPERMAJBCIT = 0
                    WHERE CDFILIAL = P_FILIAL
                      AND NRLANCTONF = P_LANCTONF
                      AND NRSEQITPROD = C_IMPOSITEMCOM.NRSEQITPROD
                      AND CDIMPOSTO = C_IMPOSITEMCOM.CDIMPOSTO;
                  END IF;
               END IF;
               /* ICMS-ST para nota fiscal de complento */
               V_CDIMPOSTRIB  := null;
               V_IDVRIMPSTRIB := null;
               /*IMPOSTO DE SUBSTITUICAO TRIBUTARIA*/
               FOR C_ICMSST IN (SELECT CDIMPOSTO
                                      FROM IMPOSTO
                                     WHERE CDIMPOSTRIB = C_IMPOSITEMCOM.CDIMPOSTO)
               LOOP
                  V_CDIMPOSTRIB := V_CDIMPOSTRIB||C_ICMSST.CDIMPOSTO||'_';
               END LOOP;
               IF V_CDIMPOSTRIB IS NOT NULL THEN
                  SELECT NVL(MAX(IDVRIMPSTRIB), 'N')
                    INTO V_IDVRIMPSTRIB
                    FROM ITEMPROIMPOS I, IMPOSTO II
                   WHERE (CDFILIAL     = P_FILIAL)
                     AND (NRLANCTONF   = P_LANCTONF)
                     AND (NRSEQITPROD  = C_IMPOSITEMCOM.NRSEQITPROD)
                     AND (INSTR(V_CDIMPOSTRIB,II.CDIMPOSTO||'_') > 0)
                     AND (I.CDIMPOSTO = II.CDIMPOSTO);
                  IF (NVL(V_IDVRIMPSTRIB,w_N) = w_N) THEN
                     V_VRIMPOSTOST := 0;
                  ELSE
                     Begin
                        SELECT NVL(MAX(VRIMPOSTOIT),0)
                        INTO   V_VRIMPOSTOST
                        FROM ITEMPROIMPOS I
                        WHERE (CDFILIAL     = P_FILIAL)                   AND
                              (NRLANCTONF   = P_LANCTONF)                 AND
                              (NRSEQITPROD  = C_IMPOSITEMCOM.NRSEQITPROD) AND
                              (INSTR(V_CDIMPOSTRIB,CDIMPOSTO||'_') > 0)  AND
                              (NRITPROIMPOS = w_1);
                     Exception
                        When No_Data_Found Then
                           V_VRIMPOSTOST := 0;
                     End;
                  END IF;
               END IF;
               /* ICMS-ST para nota fiscal de complento */
               IF (C_IMPOSITEMCOM.IDUTVLRQTDEIMPO = 'S') THEN
                  SELECT NVL(MAX(VRFATOCONVES), 1)
                    INTO V_VRFATOCONVES
                    FROM CONVPRODUTO
                   WHERE CDPRODUTO = C_IMPOSITEMCOM.CDPRODUTO;
                  IF (NVL(V_IDVRIMPSTRIB,w_N) = w_N) THEN
                     V_BASECALCTO  := (V_QTITEMPROD * V_VRFATOCONVES);
                     V_IMPOSTOIT   := ((V_QTITEMPROD * V_VRFATOCONVES) * C_IMPOSITEMCOM.VRALIQITEM) - C_IMPOSITEMCOM.VRPARDEDIT;
                  ELSE
                     V_BASECALCTO  := (V_QTITEMPROD * V_VRFATOCONVES);
                     V_IMPOSTOIT   := ((V_QTITEMPROD * V_VRFATOCONVES) * C_IMPOSITEMCOM.VRALIQITEM) - C_IMPOSITEMCOM.VRPARDEDIT - V_VRIMPOSTOST;
                     IF V_IMPOSTOIT < 0 THEN
                        V_IMPOSTOIT := 0;
                     END IF;
                  END IF;
               ELSE
                  IF (NVL(V_IDVRIMPSTRIB,w_N) = w_N) THEN
                     V_BASECALCTO  := V_BASECALCIT;
                     V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - C_IMPOSITEMCOM.VRPARDEDIT;
                  ELSE
                     V_BASECALCTO  := V_BASECALCIT;
                     V_IMPOSTOIT   := (V_BASECALCIT * V_TAXAALIQUOTA) - C_IMPOSITEMCOM.VRPARDEDIT - V_VRIMPOSTOST;
                     IF V_IMPOSTOIT < 0 THEN
                        V_IMPOSTOIT := 0;
                     END IF;
                  END IF;
               END IF;
               SELECT NVL(MAX(IDPERVISIMPSCRE),'S')
                 INTO V_IDPERVISIMPSCRE
                 FROM PARAMGERAL;
               IF V_IDPERVISIMPSCRE = 'S' THEN
                  V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
               ELSE
                  IF C_IMPOSITEMCOM.CDFISCALIT = '1' THEN
                     V_VRIMPOSTOVISUIT := V_IMPOSTOIT;
                  ELSE
                     V_VRIMPOSTOVISUIT := 0;
                  END IF;
               END IF;
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
                  UPDATE ITEMPROIMPOS
                     SET VRCONTITIMP = V_BASECALCTO,
                         VRBASECALCIT = V_BASECALCTO,
                         VRIMPOSTOIT = TRUNC(V_IMPOSTOIT,2),
                         VRIMPOSTOVISUIT = TRUNC(VRIMPOSTOVISUIT,2)
                    WHERE CDFILIAL = P_FILIAL
                      AND NRLANCTONF = P_LANCTONF
                      AND NRSEQITPROD = C_IMPOSITEMCOM.NRSEQITPROD
                      AND CDIMPOSTO = C_IMPOSITEMCOM.CDIMPOSTO
                      AND NRITPROIMPOS = '1';
               ELSE
                  UPDATE ITEMPROIMPOS
                     SET VRCONTITIMP = V_BASECALCTO,
                         VRBASECALCIT = V_BASECALCTO,
                         VRIMPOSTOIT = ROUND(V_IMPOSTOIT,2),
                         VRIMPOSTOVISUIT = ROUND(V_VRIMPOSTOVISUIT,2)
                    WHERE CDFILIAL = P_FILIAL
                      AND NRLANCTONF = P_LANCTONF
                      AND NRSEQITPROD = C_IMPOSITEMCOM.NRSEQITPROD
                      AND CDIMPOSTO = C_IMPOSITEMCOM.CDIMPOSTO
                      AND NRITPROIMPOS = '1';
               END IF;
               IF (C_IMPOSITEMCOM.VRPERREDBCIT > 0)  THEN
                  Begin
                     SELECT CDFILIAL
                     INTO   V_CHAVE
                     FROM ITEMPROIMPOS
                     WHERE CDFILIAL     = P_FILIAL                 AND
                           NRLANCTONF   = P_LANCTONF               AND
                           NRSEQITPROD  = C_IMPOSITEMCOM.NRSEQITPROD  AND
                           CDIMPOSTO    = C_IMPOSITEMCOM.CDIMPOSTO AND
                           NRITPROIMPOS = '2';
                  Exception
                     When No_Data_Found Then
                        V_CHAVE := Null;
                  End;
                  IF V_CHAVE IS NULL THEN
                     INSERT INTO ITEMPROIMPOS(
                        CDFILIAL, NRLANCTONF, NRSEQITPROD, CDIMPOSTO,
                        NRITPROIMPOS, CDFISCALIT, VRALIQITEM, VRPERREDBCIT,
                        VRCONTITIMP, VRBASECALCIT, VRIMPOSTOIT, VRPARDEDIT,
                        CDSITUTRIBIT, CDFISCOPERIT, VRIMPOSTOVISUIT, CDENQLEGALIPI,
                        CDCEST)
                     VALUES(
                        P_FILIAL, P_LANCTONF, C_IMPOSITEMCOM.NRSEQITPROD, C_IMPOSITEMCOM.CDIMPOSTO,
                        '2', '2', 0, V_PERDBCALC,
                        V_CONTABCALC, 0, 0, 0,
                        C_IMPOSITEMCOM.CDSITUTRIBIT, C_IMPOSITEMCOM.CDFISCOPERIT, 0, C_IMPOSITEMCOM.CDENQLEGALIPI,
                        C_IMPOSITEMCOM.CDCEST);
                  ELSE
                     UPDATE ITEMPROIMPOS
                        SET VRCONTITIMP = V_CONTABCALC,
                            VRBASECALCIT = 0,
                            VRIMPOSTOIT = 0,
                            VRPERREDBCIT = V_PERDBCALC,
                            VRIMPOSTOVISUIT = 0
                    WHERE CDFILIAL = P_FILIAL
                      AND NRLANCTONF = P_LANCTONF
                      AND NRSEQITPROD = C_IMPOSITEMCOM.NRSEQITPROD
                      AND CDIMPOSTO = C_IMPOSITEMCOM.CDIMPOSTO
                      AND NRITPROIMPOS = '2';
                  END IF;
               END IF;
            END IF;
         END IF;
      END LOOP;
   --TOTALIZAÇÃO DOS IMPOSTOS
      FOR C_IMPOSNFCOMP IN (SELECT IT.CDFILIAL, IT.NRLANCTONF, IT.CDIMPOSTO
                           FROM IMPOSNFTOT IT, IMPOSTO I
                           WHERE IT.CDIMPOSTO  = I.CDIMPOSTO AND
                                 I.IDPRODNOTA  <> w_C        AND
                                 IT.CDFILIAL   = P_FILIAL    AND
                                 IT.NRLANCTONF = P_LANCTONF) LOOP
          DELETE IMPOSNFTOT
          WHERE (CDFILIAL   = P_FILIAL) AND
                (NRLANCTONF = P_LANCTONF) AND
                (CDIMPOSTO = C_IMPOSNFCOMP.CDIMPOSTO);
      END LOOP;
      DELETE IMPOSTONF
      WHERE (CDFILIAL   = P_FILIAL) AND
            (NRLANCTONF = P_LANCTONF);
      FOR C_IMPOSNFTOT IN (SELECT CDIMPOSTO, 
                                  NVL(SUM(ROUND(VRIMPOSTOIT,2)),0) AS VRIMPOSTOIT, 
                                  NVL(SUM(ROUND(VRBASECALCIT,2)),0) AS VRBASECALCIT,
                                  NVL(SUM(ROUND(VRADICFECP,2)),0) AS VRADICFECP
                           FROM ITEMPROIMPOS
                           WHERE (CDFILIAL   = P_FILIAL)   AND
                                 (NRLANCTONF = P_LANCTONF)
                           GROUP BY CDIMPOSTO) LOOP
         V_ICMS := 'N';
         FOR C_ICMS IN (SELECT CDIMPOSTO
                          FROM IMPOSTONFE N, IMPOSTO I
                         WHERE N.CDIMPOSTONFE = I.CDIMPOSTONFE
                           AND N.CDIMPOSTONFE = '00'
                           AND I.CDIMPOSTO = C_IMPOSNFTOT.CDIMPOSTO)
         LOOP
             V_ICMS := 'S';
         END LOOP;
         IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (V_ICMS = 'S') THEN
            INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF,
                                   VRADICFECP)
                           VALUES (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, TRUNC(C_IMPOSNFTOT.VRIMPOSTOIT,V_NRDECITOTANF), TRUNC(C_IMPOSNFTOT.VRBASECALCIT,V_NRDECITOTANF),
                                   C_IMPOSNFTOT.VRADICFECP);
         ELSE
            INSERT INTO IMPOSNFTOT(CDFILIAL, NRLANCTONF, CDIMPOSTO, VRTOTIMPONF, VRTOTBCALCNF,
                                   VRADICFECP)
                           VALUES (P_FILIAL, P_LANCTONF, C_IMPOSNFTOT.CDIMPOSTO, ROUND(C_IMPOSNFTOT.VRIMPOSTOIT,V_NRDECITOTANF), ROUND(C_IMPOSNFTOT.VRBASECALCIT,V_NRDECITOTANF),
                                   C_IMPOSNFTOT.VRADICFECP);
         END IF;
      END LOOP;
      UPDATE ITEMPROIMPOS
         SET IDMODBCITNFE = '3'
       WHERE CDFILIAL = P_FILIAL
         AND NRLANCTONF = P_LANCTONF;
   ELSIF (P_TIPOOPER = w_09) OR (P_TIPOOPER = w_12) THEN /*VALE DE TRANSFERENCIA E NOTA DE DEBITO/CREDITO*/
      IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
         UPDATE ITEMPRODUTO
         SET VRBRUTITEM   = TRUNC((VRTOTAITEM - VRDESCITEM),2),
             VRCONTITEM   = TRUNC((VRTOTAITEM - VRDESCITEM),2)
         WHERE CDFILIAL   = P_FILIAL   AND
               NRLANCTONF = P_LANCTONF;
      ELSE
         UPDATE ITEMPRODUTO
         SET VRBRUTITEM   = ROUND((VRTOTAITEM - VRDESCITEM),5),
             VRCONTITEM   = ROUND((VRTOTAITEM - VRDESCITEM),5)
         WHERE CDFILIAL   = P_FILIAL   AND
               NRLANCTONF = P_LANCTONF;
      END IF;
      FOR C_ITPROD IN (SELECT I.CDPRODUTO, I.CDCFOPINFUSU, I.CDSUFXINFUSU, I.CDCSTINFUSU, I.NRSEQITPROD,
                              P.CDARVPROD, I.IDORIGEMPROD
                       FROM ITEMPRODUTO I, PRODUTO P
                       WHERE CDFILIAL   = P_FILIAL
                         AND NRLANCTONF = P_LANCTONF
                         AND I.CDPRODUTO = P.CDPRODUTO) LOOP
         CONSULTA_CST(w_S, P_EMPRESA, C_ITPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, V_IDINTEREST, V_IDINTERNA, NULL, C_ITPROD.IDORIGEMPROD);
         FOR C_CSTTEMP IN (SELECT DSMENSCSTTMP
                           FROM CSTTEMP
                           WHERE (NRCSTTEMP = V_CSTTEMP)    AND
                                 (DSMENSCSTTMP IS NOT NULL) AND
                                 (NOT EXISTS(SELECT DSDADOADIC
                                             FROM DADOSADIC
                                             WHERE (CDFILIAL   = P_FILIAL)   AND
                                                   (NRLANCTONF = P_LANCTONF) AND
                                                   (DSDADOADIC LIKE CSTTEMP.DSMENSCSTTMP || '%')))) LOOP
            IF NOT (C_CSTTEMP.DSMENSCSTTMP IS NULL) THEN
               INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
               VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, C_CSTTEMP.DSMENSCSTTMP);
               V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
            END IF;
         END LOOP;
         CONSULTA_CFOP(w_S, P_EMPRESA, C_ITPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_PAISORIG,
                       V_ESTAORIG, V_PAISCFOP, V_ESTACFOP, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, P_INIVALID, C_ITPROD.IDORIGEMPROD);
         FOR C_CFOPTEMP IN (SELECT DSMENSCFOTMP
                            FROM CFOPTEMP
                            WHERE (NRCFOPTEMP = V_CFOPTEMP)  AND
                                  (DSMENSCFOTMP IS NOT NULL) AND
                                  (NOT EXISTS(SELECT DSDADOADIC
                                              FROM DADOSADIC
                                              WHERE (CDFILIAL   = P_FILIAL)   AND
                                                    (NRLANCTONF = P_LANCTONF) AND
                                                    (DSDADOADIC LIKE CFOPTEMP.DSMENSCFOTMP || '%')))) LOOP
            IF NOT (C_CFOPTEMP.DSMENSCFOTMP IS NULL) THEN
               INSERT INTO DADOSADIC(CDFILIAL, NRLANCTONF, NRDADOADIC, DSDADOADIC)
               VALUES(P_FILIAL, P_LANCTONF, V_DADOADIC, C_CFOPTEMP.DSMENSCFOTMP);
               V_DADOADIC := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_DADOADIC) + 1)),3,0);
            END IF;
         END LOOP;
         IF C_ITPROD.CDCFOPINFUSU IS NULL THEN
            UPDATE NOTAFISCAL
            SET DSCDFISOPENF = (SELECT MAX(DSCFOPTMP)
                                  FROM CFOPTEMP
                                  WHERE NRCFOPTEMP = V_CFOPTEMP)
            WHERE CDFILIAL   = P_FILIAL   AND
                  NRLANCTONF = P_LANCTONF;
            SELECT MAX(CDFISOPETMP), MAX(CDSUFCFOPTMP), SUBSTR(MAX(DSCFOPTMP),1,50)
            INTO   V_FISOPEITEM    , V_SUFXCFOPPR, V_DSCDFISOPEIT
            FROM CFOPTEMP
            WHERE NRCFOPTEMP = V_CFOPTEMP;
            SELECT MAX(CDSITTRIBTMP)
            INTO   V_SITUTRIBPR
            FROM CSTTEMP
            WHERE NRCSTTEMP = V_CSTTEMP;
         ELSE
            V_CFOPINFUSU := 'S';
            V_FISOPEITEM := C_ITPROD.CDCFOPINFUSU;
            V_SUFXCFOPPR := C_ITPROD.CDSUFXINFUSU;
            V_SITUTRIBPR := C_ITPROD.CDCSTINFUSU;
         END IF;
         IF V_IDDOCREPLIC IS NOT NULL THEN
            IF LENGTH(V_SITUTRIBPR) = 3 THEN
               V_SITUTRIBPR := SUBSTR(V_SITUTRIBPR,1,1) || w_90;
            ELSE
               V_SITUTRIBPR := w_90;
            END IF;
         END IF;
         CONSULTA_CSOSN('S', P_EMPRESA, C_ITPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, P_FILIAL, P_LANCTONF, V_CDGRUPOFISC, V_IDINTEREST,V_CDFISCOPERSN);
         UPDATE ITEMPRODUTO
         SET CDFISOPEITEM  = V_FISOPEITEM,
             CDSUFXCFOPPR  = V_SUFXCFOPPR,
             CDSITUTRIBPR  = V_SITUTRIBPR,
             DSCDFISOPEIT  = V_DSCDFISOPEIT,
             CDFISCOPERSN  = V_CDFISCOPERSN
         WHERE CDFILIAL    = P_FILIAL   AND
               NRLANCTONF  = P_LANCTONF AND
               NRSEQITPROD = C_ITPROD.NRSEQITPROD;
         DELETE CFOPTEMP
         WHERE NRCFOPTEMP = V_CFOPTEMP;
         DELETE CSTTEMP
         WHERE NRCSTTEMP = V_CSTTEMP;
         /*Consulta de nfe*/
         V_CDSITTRIBTMP    := NULL;
         V_IDMODBCNFE      := NULL;
         V_IDMODBCSTNFE    := NULL;
         V_CDTPCRPICOSTNFE := NULL;
         V_CDNATUBCPCSTNFE := NULL;
         FOR C_ITPROI IN (SELECT II.CDIMPOSTO, II.NRITPROIMPOS
                            FROM ITEMPROIMPOS II, IMPOSTO I
                           WHERE II.CDFILIAL   = P_FILIAL
                             AND II.NRLANCTONF = P_LANCTONF
                             AND II.NRSEQITPROD = C_ITPROD.NRSEQITPROD
                             AND II.CDIMPOSTO = I.CDIMPOSTO
                             AND I.IDPRODNOTA = 'P'
                          ORDER BY II.CDIMPOSTO) LOOP
             V_IMPOICMS := NULL;
             V_CDGRUPOFISCAUX := NULL;
             SELECT MAX(G.CDGRUPOFISC)
              INTO V_CDGRUPOFISCAUX
              FROM GRUPOFISCIMPFIL G,
                   (SELECT CDFILIEMIT AS CDFILIAL
                      FROM NOTAFISCAL
                     WHERE CDFILIAL = P_FILIAL
                           AND NRLANCTONF = P_LANCTONF) F
             WHERE G.CDFILIAL = F.CDFILIAL
               AND NOT G.CDGRUPOFISC IS NULL
               AND G.CDIMPOSTO = C_ITPROI.CDIMPOSTO;
             IF V_IDORIGDESTNF = 'C' THEN
                SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                 INTO V_CDGRUPOFISCAUX
                 FROM GRFISIMPIECLIE G,
                      (SELECT CDCLIENTE
                         FROM NOTAFISCAL
                        WHERE CDFILIAL = P_FILIAL
                          AND NRLANCTONF = P_LANCTONF) N
                WHERE G.CDCLIENTE = N.CDCLIENTE
                  AND G.CDEMPRESA = P_EMPRESA
                  AND G.CDINSCESTA = P_INSCESTA
                  AND NOT G.CDGRUPOFISC IS NULL
                  AND G.CDIMPOSTO = C_ITPROI.CDIMPOSTO;
             ELSE
                SELECT NVL(MAX(G.CDGRUPOFISC), V_CDGRUPOFISCAUX)
                  INTO V_CDGRUPOFISCAUX
                  FROM GRFISIMPIEFORN G,
                      (SELECT CDFORNECED
                         FROM NOTAFISCAL
                        WHERE CDFILIAL = P_FILIAL
                          AND NRLANCTONF = P_LANCTONF) N
                 WHERE G.CDFORNECED = N.CDFORNECED
                   AND G.CDEMPRESA = P_EMPRESA
                   AND G.CDINSCESTA = P_INSCESTA
                   AND NOT G.CDGRUPOFISC IS NULL
                   AND G.CDIMPOSTO = C_ITPROI.CDIMPOSTO;
             END IF;
             IF V_CDGRUPOFISCAUX IS NULL THEN
               CONSULTA_CST_NFE(w_S, P_EMPRESA, C_ITPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISC,C_ITPROI.CDIMPOSTO, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, C_ITPROD.IDORIGEMPROD);
             ELSE
               CONSULTA_CST_NFE(w_S, P_EMPRESA, C_ITPROD.CDARVPROD, P_INSCESTA, P_TIPOOPER, V_CDGRUPOFISCAUX,C_ITPROI.CDIMPOSTO, V_IDINTEREST, V_IDINTERNA, P_FILIAL, P_LANCTONF, '', V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, C_ITPROD.IDORIGEMPROD);
             END IF;
             UPDATE ITEMPROIMPOS
               SET CDSITUTRIBITNFE = V_CDSITTRIBTMP,
                   IDMODBCITNFE = V_IDMODBCNFE,
                   IDMODBCSTITNFE = V_IDMODBCSTNFE,
                   CDTPCREDPICOITE = V_CDTPCRPICOSTNFE,
                   CDNATUBCALPCIT  = V_CDNATUBCPCSTNFE,
                   IDMOTDESONERAIT = V_IDMOTIDESSTNFE
               WHERE (CDFILIAL     = P_FILIAL)                   AND
                     (NRLANCTONF   = P_LANCTONF)                 AND
                     (NRSEQITPROD  = C_ITPROD.NRSEQITPROD) AND
                     (CDIMPOSTO    = C_ITPROI.CDIMPOSTO)   AND
                     (NRITPROIMPOS = C_ITPROI.NRITPROIMPOS);
         END LOOP;
      END LOOP;
   END IF;
   V_DIFERENCA := 0;
   /* RATEIO DOS ENCARGOS PARA OS PRODUTOS DA NOTA. ESTE RATEIO É NECESSÁRIO PARA A NOTA FISCAL ELETRÔNICA */
   FOR C_ITEMENCARGO IN (SELECT ENCARGO.CDENCARGONFE, SUM(ITEMENCARGO.VRITEMENCA) AS VRITEMENCA
                           FROM ITEMENCARGO, ENCARGO
                          WHERE ITEMENCARGO.CDENCARGO = ENCARGO.CDENCARGO
                            AND ENCARGO.CDENCARGONFE IN ('01', '02', '03', '04', '05')
                            AND NVL(ITEMENCARGO.VRITEMENCA, 0) >= 0
                            AND CDFILIAL = P_FILIAL
                            AND NRLANCTONF = P_LANCTONF
                          GROUP BY ENCARGO.CDENCARGONFE) LOOP
      /* ENCARGO FRETE */
      IF (C_ITEMENCARGO.CDENCARGONFE = '01') THEN
         IF (C_ITEMENCARGO.VRITEMENCA > 0) THEN
            FOR C_ITEMPRODUTO IN (SELECT NRSEQITPROD, VRTOTAITEM
                                    FROM ITEMPRODUTO
                                   WHERE CDFILIAL = P_FILIAL
                                     AND NRLANCTONF = P_LANCTONF) LOOP
               SELECT ITEMPRODUTO.VRTOTAITEM,
                      TOTAL.SOMATOTAITEM,
                      DECODE(TOTAL.SOMATOTAITEM, 0, 0, ((100 * ITEMPRODUTO.VRTOTAITEM) / TOTAL.SOMATOTAITEM)) AS PERCENTITEM
                 INTO V_VRTOTAITEM, V_SOMATOTAITEM, V_PERCENTITEM
                 FROM ITEMPRODUTO, (SELECT CDFILIAL, NRLANCTONF, SUM(VRTOTAITEM) SOMATOTAITEM
                                      FROM ITEMPRODUTO
                                     WHERE CDFILIAL = P_FILIAL
                                       AND NRLANCTONF = P_LANCTONF
                                     GROUP BY CDFILIAL, NRLANCTONF) TOTAL
                WHERE ITEMPRODUTO.CDFILIAL = TOTAL.CDFILIAL
                  AND ITEMPRODUTO.NRLANCTONF = TOTAL.NRLANCTONF
                  AND ITEMPRODUTO.NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                  UPDATE ITEMPRODUTO
                     SET VRFRETEITEM = TRUNC(C_ITEMENCARGO.VRITEMENCA * (V_PERCENTITEM / 100), 2)
                   WHERE CDFILIAL  = P_FILIAL
                     AND NRLANCTONF = P_LANCTONF
                     AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               ELSE
                  UPDATE ITEMPRODUTO
                     SET VRFRETEITEM = ROUND(C_ITEMENCARGO.VRITEMENCA * (V_PERCENTITEM / 100), 2)
                   WHERE CDFILIAL  = P_FILIAL
                     AND NRLANCTONF = P_LANCTONF
                     AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               END IF;
            END LOOP;
            /*AJUSTE DE VALORES*/
            SELECT SUM(VRFRETEITEM)
              INTO V_VRFRETEITEM
              FROM ITEMPRODUTO
             WHERE CDFILIAL = P_FILIAL
               AND NRLANCTONF = P_LANCTONF;
            V_DIFERENCA := C_ITEMENCARGO.VRITEMENCA - V_VRFRETEITEM;
            IF V_DIFERENCA <> 0 THEN
               UPDATE ITEMPRODUTO
                  SET VRFRETEITEM = VRFRETEITEM + V_DIFERENCA
                WHERE CDFILIAL = P_FILIAL
                  AND NRLANCTONF = P_LANCTONF
                  AND NRSEQITPROD = (SELECT MAX(NRSEQITPROD)
                                      FROM ITEMPRODUTO
                                      WHERE CDFILIAL = P_FILIAL
                                        AND NRLANCTONF = P_LANCTONF
                                        AND VRTOTAITEM = (SELECT MAX(VRTOTAITEM)
                                                           FROM ITEMPRODUTO
                                                          WHERE CDFILIAL = P_FILIAL
                                                            AND NRLANCTONF = P_LANCTONF));
            END IF;
         ELSE
            UPDATE ITEMPRODUTO
               SET VRFRETEITEM = 0
             WHERE CDFILIAL  = P_FILIAL
               AND NRLANCTONF = P_LANCTONF;
         END IF;
      END IF;
      /* ENCARGO SEGURO */
      IF (C_ITEMENCARGO.CDENCARGONFE = '02') THEN
         IF (C_ITEMENCARGO.VRITEMENCA > 0) THEN
            FOR C_ITEMPRODUTO IN (SELECT NRSEQITPROD, VRTOTAITEM
                                    FROM ITEMPRODUTO
                                   WHERE CDFILIAL = P_FILIAL
                                     AND NRLANCTONF = P_LANCTONF) LOOP
               SELECT ITEMPRODUTO.VRTOTAITEM,
                      TOTAL.SOMATOTAITEM,
                      DECODE(TOTAL.SOMATOTAITEM, 0, 0, ((100 * ITEMPRODUTO.VRTOTAITEM) / TOTAL.SOMATOTAITEM)) AS PERCENTITEM
                 INTO V_VRTOTAITEM, V_SOMATOTAITEM, V_PERCENTITEM
                 FROM ITEMPRODUTO, (SELECT CDFILIAL, NRLANCTONF, SUM(VRTOTAITEM) SOMATOTAITEM
                                      FROM ITEMPRODUTO
                                     WHERE CDFILIAL = P_FILIAL
                                       AND NRLANCTONF = P_LANCTONF
                                     GROUP BY CDFILIAL, NRLANCTONF) TOTAL
                WHERE ITEMPRODUTO.CDFILIAL = TOTAL.CDFILIAL
                  AND ITEMPRODUTO.NRLANCTONF = TOTAL.NRLANCTONF
                  AND ITEMPRODUTO.NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                  UPDATE ITEMPRODUTO
                     SET VRSEGURITEM = TRUNC(C_ITEMENCARGO.VRITEMENCA * (V_PERCENTITEM / 100), 2)
                   WHERE CDFILIAL  = P_FILIAL
                     AND NRLANCTONF = P_LANCTONF
                     AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               ELSE
                  UPDATE ITEMPRODUTO
                     SET VRSEGURITEM = ROUND(C_ITEMENCARGO.VRITEMENCA * (V_PERCENTITEM / 100), 2)
                   WHERE CDFILIAL  = P_FILIAL
                     AND NRLANCTONF = P_LANCTONF
                     AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               END IF;
            END LOOP;
            /*AJUSTE DE VALORES*/
            SELECT SUM(VRSEGURITEM)
              INTO V_VRSEGURITEM
              FROM ITEMPRODUTO
             WHERE CDFILIAL = P_FILIAL
               AND NRLANCTONF = P_LANCTONF;
            V_DIFERENCA := C_ITEMENCARGO.VRITEMENCA - V_VRSEGURITEM;
            IF V_DIFERENCA <> 0 THEN
               UPDATE ITEMPRODUTO
                  SET VRSEGURITEM = VRSEGURITEM + V_DIFERENCA
                WHERE CDFILIAL = P_FILIAL
                  AND NRLANCTONF = P_LANCTONF
                  AND NRSEQITPROD = (SELECT MAX(NRSEQITPROD)
                                      FROM ITEMPRODUTO
                                      WHERE CDFILIAL = P_FILIAL
                                        AND NRLANCTONF = P_LANCTONF
                                        AND VRTOTAITEM = (SELECT MAX(VRTOTAITEM)
                                                           FROM ITEMPRODUTO
                                                          WHERE CDFILIAL = P_FILIAL
                                                            AND NRLANCTONF = P_LANCTONF));
            END IF;
         ELSE
            UPDATE ITEMPRODUTO
               SET VRSEGURITEM = 0
             WHERE CDFILIAL  = P_FILIAL
               AND NRLANCTONF = P_LANCTONF;
         END IF;
      END IF;
      /* ENCARGO DESCONTO */
      IF (C_ITEMENCARGO.CDENCARGONFE = '03') THEN
         IF (C_ITEMENCARGO.VRITEMENCA > 0) THEN
            FOR C_ITEMPRODUTO IN (SELECT NRSEQITPROD, VRTOTAITEM
                                    FROM ITEMPRODUTO
                                   WHERE CDFILIAL = P_FILIAL
                                     AND NRLANCTONF = P_LANCTONF) LOOP
               SELECT ITEMPRODUTO.VRTOTAITEM,
                      TOTAL.SOMATOTAITEM,
                      DECODE(TOTAL.SOMATOTAITEM, 0, 0, ((100 * ITEMPRODUTO.VRTOTAITEM) / TOTAL.SOMATOTAITEM)) AS PERCENTITEM
                 INTO V_VRTOTAITEM, V_SOMATOTAITEM, V_PERCENTITEM
                 FROM ITEMPRODUTO, (SELECT CDFILIAL, NRLANCTONF, SUM(VRTOTAITEM) SOMATOTAITEM
                                      FROM ITEMPRODUTO
                                     WHERE CDFILIAL = P_FILIAL
                                       AND NRLANCTONF = P_LANCTONF
                                     GROUP BY CDFILIAL, NRLANCTONF) TOTAL
                WHERE ITEMPRODUTO.CDFILIAL = TOTAL.CDFILIAL
                  AND ITEMPRODUTO.NRLANCTONF = TOTAL.NRLANCTONF
                  AND ITEMPRODUTO.NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               IF ((V_IDSISTEMA = 'VND') OR (V_IDSISTEMA = 'PDV')) AND (NVL(V_IDORIGDESTNF, 'C') <> 'L') THEN
                  UPDATE ITEMPRODUTO
                     SET VRENCARGODESC = TRUNC(VRDESCITEM + (C_ITEMENCARGO.VRITEMENCA * (V_PERCENTITEM / 100)), 2)
                   WHERE CDFILIAL  = P_FILIAL
                     AND NRLANCTONF = P_LANCTONF
                     AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               ELSE
                  UPDATE ITEMPRODUTO
                     SET VRENCARGODESC = ROUND(VRDESCITEM + (C_ITEMENCARGO.VRITEMENCA * (V_PERCENTITEM / 100)), 2)
                   WHERE CDFILIAL  = P_FILIAL
                     AND NRLANCTONF = P_LANCTONF
                     AND NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;
               END IF;
            END LOOP;
            /*AJUSTE DE VALORES*/
            SELECT SUM(VRENCARGODESC)
              INTO V_VRENCARGODESC
              FROM ITEMPRODUTO
             WHERE CDFILIAL = P_FILIAL
               AND NRLANCTONF = P_LANCTONF;
            SELECT SUM(VRDESCITEM)
              INTO V_SUMVRDESCITEM
              FROM ITEMPRODUTO
             WHERE CDFILIAL  = P_FILIAL
               AND NRLANCTONF = P_LANCTONF;
            V_DIFERENCA := (C_ITEMENCARGO.VRITEMENCA+V_SUMVRDESCITEM) - V_VRENCARGODESC;
            IF V_DIFERENCA <> 0 THEN
               UPDATE ITEMPRODUTO
                  SET VRENCARGODESC = VRENCARGODESC + V_DIFERENCA
                WHERE CDFILIAL = P_FILIAL
                  AND NRLANCTONF = P_LANCTONF
                  AND NRSEQITPROD = (SELECT MAX(NRSEQITPROD)
                                      FROM ITEMPRODUTO
                                      WHERE CDFILIAL = P_FILIAL
                                        AND NRLANCTONF = P_LANCTONF
                                        AND VRTOTAITEM = (SELECT MAX(VRTOTAITEM)
                                                           FROM ITEMPRODUTO
                                                          WHERE CDFILIAL = P_FILIAL
                                                            AND NRLANCTONF = P_LANCTONF));
            END IF;
         ELSE
