CREATE OR REPLACE FUNCTION REG54_A_B_C_SINTEGRA_RJ (
   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN EMPRESA.CDINSCESTA%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_CDPAIS       IN ESTADO.CDPAIS%TYPE,
   P_SGESTADO     IN VARCHAR2,
   P_NRSEQUENCIAL IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_CONTTOTAL    IN VARCHAR2,
   P_IPI          IN VARCHAR2) RETURN VARCHAR2
AS
   V_CONTGERAL      NUMBER;
   V_CONTREG        NUMBER;
   V_CONTREG54A     NUMBER;
   V_CONTREG54B     NUMBER;
   V_CONTREG54C     NUMBER;
   V_STTOTAL        VARCHAR2(160);
   V_CDARVPROD      VARCHAR2(20);
   V_SEQUENCIAL     VARCHAR2(03);
   V_CDFILIAL       VARCHAR2(04);
   V_NRLANCTONF     VARCHAR2(10);
   V_CDIMPOSAPUR    VARCHAR2(02);
   V_NRINSEST       VARCHAR2(255);
   V_IDENDEMIS      VARCHAR2(255);
   V_IDTPIJURCLIE   VARCHAR2(255);
   V_SGESTADO       VARCHAR2(255);
BEGIN
   V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
   V_CDFILIAL   := ' ';
   V_NRLANCTONF := ' ';
   SELECT MAX(CDIMPCARGATRIB)
     INTO V_CDIMPOSAPUR
     FROM IMPOSTO
    WHERE CDIMPOSTO = P_CDIMPOSTO;
   FOR C_ITEM IN (SELECT DISTINCT N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR,N.CDCLIENTE, N.CDFORNECED, N.DTENTRSAID, N.CDFILIEMIT,
                         N.CDFILIRECE, N.DTEMISSAO, N.CDMODDOCFISC, N.IDORIGDESTNF, N.IDENTRSAIDOP, N.CDFILIAL, N.NRLANCTONF,
                         N.IDTPIJURNF, N.NRINSJURNF, M.NRSEQUMAPAPU, N.CDINSCESTA
                    FROM NOTAFISCAL N, MODDOCFISC MD,
                         (SELECT I.CDFILIAL, I.NRLANCTONF, M.NRSEQUMAPAPU
                            FROM MAPAAPURAC M, IMPOSTONF I
                           WHERE M.CDEMPRESA    = P_CDEMPRESA
                             AND M.CDINSCESTA   = P_CDINSCESTA
                             AND M.DTINIPERIODO = P_DTINICIAL
                             AND M.DTFIMPERIODO = P_DTFINAL
                             AND M.CDIMPOSTO    = P_CDIMPOSTO
                             AND M.NRSEQUMAPAPU = I.NRSEQUMAPAPU
                           UNION ALL
                          SELECT I.CDFILIAL, I.NRLANCTONF, M.NRSEQUMAPAPU
                            FROM MAPAAPURAC M, IMPOSTONF I
                           WHERE M.CDEMPRESA    = P_CDEMPRESA
                             AND M.CDINSCESTA   = P_CDINSCESTA
                             AND M.DTINIPERIODO = P_DTINICIAL
                             AND M.DTFIMPERIODO = P_DTFINAL
                             AND M.CDIMPOSTO    = P_CDIMPOSTO
                             AND M.NRSEQUMAPAPU = I.NRSEQMAPOIMP) M
                   WHERE M.CDFILIAL      = N.CDFILIAL
                     AND M.NRLANCTONF    = N.NRLANCTONF
                     AND NVL(N.CDMODDOCFISC, '01') = MD.CDMODDOCFISC
                     AND MD.CDTPREGFISC  = '50'
                   ORDER BY N.NRNOTAFISC) LOOP
      IF NVL(C_ITEM.IDORIGDESTNF,'C') = 'C' THEN
         SELECT MAX(NRINSESTCLIE), MAX(NVL(IDENDEMISCLI,'P')), MAX(IDTPIJURCLIE)
           INTO V_NRINSEST, V_IDENDEMIS, V_IDTPIJURCLIE
           FROM CLIENTE
          WHERE CDCLIENTE = C_ITEM.CDCLIENTE;
         SELECT SGESTADO
           INTO V_SGESTADO
           FROM ENDECLIE
          WHERE CDCLIENTE = C_ITEM.CDCLIENTE
            AND IDTPENDECLIE = V_IDENDEMIS;
      ELSIF NVL(C_ITEM.IDORIGDESTNF,'C') = 'F' THEN
         SELECT MAX(NRINSESTFORN), MAX(NVL(IDENDEMISFOR,'P')), MAX(IDTPIJURFORN)
           INTO V_NRINSEST, V_IDENDEMIS, V_IDTPIJURCLIE
           FROM FORNECEDOR
          WHERE CDFORNECED = C_ITEM.CDFORNECED;
         SELECT SGESTADO
           INTO V_SGESTADO
           FROM ENDEFORN
          WHERE CDFORNECED = C_ITEM.CDFORNECED
            AND IDTPENDEFORN = V_IDENDEMIS;
      ELSIF NVL(C_ITEM.IDORIGDESTNF,'C') = 'L' THEN
         IF C_ITEM.IDENTRSAIDOP = 'S' THEN
            SELECT MAX(CDINSCESTA), MAX(NVL(IDENDEMISFIL,'P'))
              INTO V_NRINSEST, V_IDENDEMIS
              FROM FILIAL
             WHERE CDFILIAL = C_ITEM.CDFILIRECE;
            IF V_IDENDEMIS = 'I' THEN
               SELECT SGESTADO
                 INTO V_SGESTADO
                 FROM INSCRESTAD
                WHERE CDINSCESTA = C_ITEM.CDINSCESTA
                  AND CDEMPRESA  = P_CDEMPRESA;
            ELSE
               SELECT SGESTADO
                 INTO V_SGESTADO
                 FROM ENDEFILI
                WHERE CDFILIAL     = C_ITEM.CDFILIRECE
                  AND IDTPENDEFILI = V_IDENDEMIS;
            END IF;
         ELSE
            SELECT MAX(CDINSCESTA), MAX(NVL(IDENDEMISFIL,'P'))
              INTO V_NRINSEST, V_IDENDEMIS
              FROM FILIAL
             WHERE CDFILIAL = C_ITEM.CDFILIEMIT;
            IF V_IDENDEMIS = 'I' THEN
               SELECT SGESTADO
                 INTO V_SGESTADO
                 FROM INSCRESTAD
                WHERE CDINSCESTA = C_ITEM.CDINSCESTA
                  AND CDEMPRESA  = P_CDEMPRESA;
            ELSE
               SELECT SGESTADO
                 INTO V_SGESTADO
                 FROM ENDEFILI
                WHERE CDFILIAL     = C_ITEM.CDFILIEMIT
                  AND IDTPENDEFILI = V_IDENDEMIS;
            END IF;
         END IF;
      END IF;
      IF NVL(C_ITEM.IDORIGDESTNF,'C') = 'C' THEN
         /* CLIENTE */
         FOR C_REGISTRO54A IN (SELECT
                                   ('54'||
                                    LPAD(NVL(IT.NRINSJURNF, '00000000000000'),14,'0')||
                                    RPAD(NVL(IT.CDMODDOCFISC,'01'), 2, ' ')||
                                    RPAD(DECODE(RTRIM(NVL(IT.CDSERIE,' ')),'-',' ',REPLACE(NVL(IT.CDSERIE,' '),'-', '')),3,' ')||
                                    LPAD(SUBSTR(IT.NRNOTAFISC,LENGTH(IT.NRNOTAFISC)-5,6),6,'0')||
                                    RPAD(NVL(IT.CDFISCOPERNF,'0'),4,' ')||
                                    LPAD(NVL(TRIM(IT.CDSITUTRIBPR),'0'),3,'0')) AS DSINICIO,
                                   (RPAD(PR.CDARVPROD,14,' ')||
                                    LTRIM(REPLACE(TO_CHAR(ABS(IT.QTITEMPROD),'00000000.000'),'.',''))||
                                    LTRIM(REPLACE(TO_CHAR(ABS(IT.VRPRODUTO) ,'0000000000.00'),'.',''))||
                                    LTRIM(REPLACE(TO_CHAR(ABS(IT.VRDESCITEM),'0000000000.00'),'.',''))||
                                    LTRIM(REPLACE(TO_CHAR(NVL(ABS(IT.VRBASECALCIT),0),'0000000000.00'),'.',''))||
                                    '000000000000' ||
                                    LTRIM(REPLACE(TO_CHAR(NVL(ABS(VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, P_IPI, 'A')),0),'0000000000.00'),'.',''))||
                                    LTRIM(REPLACE(TO_CHAR(NVL(ABS(IT.VRALIQITEM),0),'00.00'),'.',''))) AS DSARQMAG,
                                    PR.CDARVPROD, NVL(IT.CDPRODNCMTRANS, PR.CDCLASFISC) AS CDCLASFISC, PR.NMPRODUTO, NVL(PR.SGUNIDADE, '  ') AS SGUNIDADE, NVL(IT.CDSITUTRIBPR, '   ') AS CDSITUTRIBPR,
                                    ABS(NVL(VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, P_IPI, 'A'),0)) AS VRALIQIPI,
                                    NVL(IT.VRALIQITEM,0) AS VRALIQICMS, NVL(IT.VRPERREDBCIT,0) VRPERREDBCIT,
                                    IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD
                                FROM PRODUTO PR,
                                     (SELECT /*+ INDEX(PK_ITEMPROIMPOS) II*/
                                             II.CDFILIAL, II.NRLANCTONF, DECODE(V_IDTPIJURCLIE,'I','','O', '',C_ITEM.NRINSJURNF) AS NRINSJURNF,
                                             C_ITEM.DTEMISSAO, C_ITEM.CDMODDOCFISC AS CDMODDOCFISC, NVL(C_ITEM.CDSERIE, C_ITEM.CDSERIEENTR) CDSERIE,
                                             V_SGESTADO, C_ITEM.NRNOTAFISC AS NRNOTAFISC, IT.NRSEQITPROD,
                                             IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                             IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                             DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')) AS VRALIQITEM,
                                             DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0) AS VRBASECALCIT,
                                             MAX(IT.CDFISOPEITEM) AS CDFISCOPERNF, MAX(II.VRPERREDBCIT) AS VRPERREDBCIT,
                                             IT.CDPRODNCMTRANS
                                      FROM ITEMPRODUTO IT, ITEMPROIMPOS II,
                                        (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('1','2','3')
                                         UNION ALL
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQMAPOIMP
                                            AND IP.CDIMPOSTO   = V_CDIMPOSAPUR
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
      /*00 NA SAIDA*/
                                         UNION
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
                                            AND NOT EXISTS(SELECT DISTINCT INF.CDFILIAL,INF.NRLANCTONF
                                                             FROM IMPOSTONF INF
                                                            WHERE INF.CDFILIAL   = IP.CDFILIAL
                                                              AND INF.NRLANCTONF = IP.NRLANCTONF
                                                              AND INF.CDIMPOSTO  = V_CDIMPOSAPUR) /*00 NA SAIDA*/ ) IP
                                      WHERE NVL(C_ITEM.IDORIGDESTNF, DECODE(C_ITEM.IDENTRSAIDOP, 'E', 'F', 'C')) = 'C'
                                        AND C_ITEM.CDFILIAL   = IP.CDFILIAL
                                        AND C_ITEM.NRLANCTONF = IP.NRLANCTONF
                                        AND C_ITEM.CDFILIAL   = IT.CDFILIAL
                                        AND C_ITEM.NRLANCTONF = IT.NRLANCTONF
                                        AND II.CDFILIAL       = IT.CDFILIAL
                                        AND II.NRLANCTONF     = IT.NRLANCTONF
                                        AND II.NRSEQITPROD    = IT.NRSEQITPROD
                                        AND II.CDIMPOSTO      = IP.CDIMPOSTO
                                        AND II.NRITPROIMPOS   IN ( '1', '2')
                                        AND SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '3'
                                        AND SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '7'
                                       GROUP BY II.CDFILIAL, II.NRLANCTONF, DECODE(V_IDTPIJURCLIE,'I','','O', '',C_ITEM.NRINSJURNF) , C_ITEM.DTEMISSAO,
                                               C_ITEM.CDMODDOCFISC, NVL(C_ITEM.CDSERIE, C_ITEM.CDSERIEENTR),
                                               V_SGESTADO, C_ITEM.NRNOTAFISC, IT.NRSEQITPROD,
                                               IT.CDPRODUTO, CDSITUTRIBPR, IT.QTITEMPROD, II.VRCONTITIMP, IT.VRTOTAITEM, IT.VRDESCITEM,
                                               DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')),
                                               DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0), IT.CDPRODNCMTRANS
                                      UNION ALL
                                      SELECT /*+ INDEX(PK_ITEMPROIMPOS) II*/
                                             II.CDFILIAL, II.NRLANCTONF, '00000000000000' NRINSJURNF, C_ITEM.DTEMISSAO,
                                             C_ITEM.CDMODDOCFISC AS CDMODDOCFISC, NVL(C_ITEM.CDSERIE, C_ITEM.CDSERIEENTR) CDSERIE,
                                             'EX' SGESTADO, C_ITEM.NRNOTAFISC AS NRNOTAFISC, IT.NRSEQITPROD,
                                             IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                             IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                             DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')) AS VRALIQITEM,
                                             DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0) AS VRBASECALCIT,
                                             MAX(IT.CDFISOPEITEM) AS CDFISCOPERNF, MAX(II.VRPERREDBCIT) AS VRPERREDBCIT,
                                             IT.CDPRODNCMTRANS
                                      FROM ITEMPRODUTO IT, ITEMPROIMPOS II,
                                        (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('1','2','3')
                                          UNION ALL
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQMAPOIMP
                                            AND IP.CDIMPOSTO   = V_CDIMPOSAPUR
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
      /*00 NA SAIDA*/
                                          UNION
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
                                            AND NOT EXISTS(SELECT DISTINCT INF.CDFILIAL,INF.NRLANCTONF
                                                             FROM IMPOSTONF INF
                                                            WHERE INF.CDFILIAL   = IP.CDFILIAL
                                                              AND INF.NRLANCTONF = IP.NRLANCTONF
                                                              AND INF.CDIMPOSTO  = V_CDIMPOSAPUR)
      /*00 NA SAIDA*/
                                                                                                 ) IP
                                      WHERE NVL(C_ITEM.IDORIGDESTNF, DECODE(C_ITEM.IDENTRSAIDOP, 'E', 'F', 'C')) = 'C'
                                        AND C_ITEM.CDFILIAL    = IP.CDFILIAL
                                        AND C_ITEM.NRLANCTONF  = IP.NRLANCTONF
                                        AND C_ITEM.CDFILIAL    = IT.CDFILIAL
                                        AND C_ITEM.NRLANCTONF  = IT.NRLANCTONF
                                        AND II.CDFILIAL        = IT.CDFILIAL
                                        AND II.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.NRSEQITPROD     = IT.NRSEQITPROD
                                        AND II.CDIMPOSTO       = IP.CDIMPOSTO
                                        AND II.NRITPROIMPOS    IN ( '1', '2')
                                        AND SUBSTR(II.CDFISCOPERIT, 1, 1) = '7'
                                      GROUP BY II.CDFILIAL, II.NRLANCTONF, C_ITEM.DTEMISSAO,
                                               C_ITEM.CDMODDOCFISC, NVL(C_ITEM.CDSERIE, C_ITEM.CDSERIEENTR),
                                               C_ITEM.NRNOTAFISC, IT.NRSEQITPROD,
                                               IT.CDPRODUTO, CDSITUTRIBPR, IT.QTITEMPROD, II.VRCONTITIMP, IT.VRTOTAITEM,IT.VRDESCITEM,
                                               DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')),
                                               DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0), IT.CDPRODNCMTRANS
                                      UNION ALL
                                      SELECT /*+ INDEX(PK_ITEMPROIMPOS) II*/
                                             II.CDFILIAL, II.NRLANCTONF, '00000000000000' NRINSJURNF, C_ITEM.DTEMISSAO,
                                             C_ITEM.CDMODDOCFISC AS CDMODDOCFISC, NVL(C_ITEM.CDSERIE, C_ITEM.CDSERIEENTR) CDSERIE,
                                             'EX' SGESTADO, C_ITEM.NRNOTAFISC AS NRNOTAFISC, IT.NRSEQITPROD,
                                             IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                             IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                             DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')) AS VRALIQITEM,
                                             DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0) AS VRBASECALCIT,
                                             MAX(IT.CDFISOPEITEM) AS CDFISCOPERNF, MAX(II.VRPERREDBCIT) AS VRPERREDBCIT,
                                             IT.CDPRODNCMTRANS
                                      FROM ITEMPRODUTO IT, ITEMPROIMPOS II,
                                        (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('1','2','3')
                                         UNION ALL
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQMAPOIMP
                                            AND IP.CDIMPOSTO   = V_CDIMPOSAPUR
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
      /*00 NA SAIDA*/
                                          UNION
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
                                            AND NOT EXISTS(SELECT DISTINCT INF.CDFILIAL,INF.NRLANCTONF
                                                             FROM IMPOSTONF INF
                                                            WHERE INF.CDFILIAL   = IP.CDFILIAL
                                                              AND INF.NRLANCTONF = IP.NRLANCTONF
                                                              AND INF.CDIMPOSTO  = V_CDIMPOSAPUR)
      /*00 NA SAIDA*/
                                                                                                 ) IP
                                      WHERE NVL(C_ITEM.IDORIGDESTNF, DECODE(C_ITEM.IDENTRSAIDOP, 'E', 'F', 'C')) = 'F'
                                        AND C_ITEM.CDFILIAL        = IP.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IP.NRLANCTONF
                                        AND C_ITEM.CDFILIAL        = IT.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.CDFILIAL        = IT.CDFILIAL
                                        AND II.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.NRSEQITPROD     = IT.NRSEQITPROD
                                        AND II.CDIMPOSTO       = IP.CDIMPOSTO
                                        AND II.NRITPROIMPOS    IN ( '1', '2')
                                        AND SUBSTR(II.CDFISCOPERIT, 1, 1) = '3'
                                      GROUP BY II.CDFILIAL, II.NRLANCTONF, C_ITEM.DTEMISSAO,
                                               C_ITEM.CDMODDOCFISC, NVL(C_ITEM.CDSERIE, C_ITEM.CDSERIEENTR),
                                               C_ITEM.NRNOTAFISC, IT.NRSEQITPROD,
                                               IT.CDPRODUTO, CDSITUTRIBPR, IT.QTITEMPROD, II.VRCONTITIMP, IT.VRTOTAITEM,
                                               IT.VRDESCITEM,
                                               DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')),
                                               DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0), IT.CDPRODNCMTRANS) IT
                                WHERE IT.CDPRODUTO = PR.CDPRODUTO
                                ORDER BY IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD) LOOP
            V_CONTREG54A := NVL(V_CONTREG54A,0)+1;
            IF ((C_REGISTRO54A.CDFILIAL||C_REGISTRO54A.NRLANCTONF) <> (V_CDFILIAL||V_NRLANCTONF)) THEN
               V_CDFILIAL   := C_REGISTRO54A.CDFILIAL;
               V_NRLANCTONF := C_REGISTRO54A.NRLANCTONF;
               V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
            END IF;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('05'||LPAD(RTRIM(TO_CHAR(V_CONTREG54A)), 8, '0'), C_REGISTRO54A.DSINICIO||V_SEQUENCIAL||C_REGISTRO54A.DSARQMAG, P_NRSEQUENCIAL);
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER(RTRIM(V_SEQUENCIAL))+1)), 3,'0');
            V_CONTREG := NVL(V_CONTREG,0) + 1;
            V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
            SELECT MAX(CDARVPROD)
            INTO V_CDARVPROD
            FROM TMP_REGISTRO_PRODUTO
            WHERE CDARVPROD = C_REGISTRO54A.CDARVPROD;
            IF V_CDARVPROD IS NULL THEN
               INSERT INTO TMP_REGISTRO_PRODUTO(CDTIPOREG, CDARVPROD, DTINICIAL, DTFINAL, CDCLASFISC, NMPRODUTO,
                                                SGUNIDADE, CDSITUTRIBPR, VRALIQIPI, VRALIQICMS, VRPERREDBCIT, VRBASESUBST)
               VALUES('75', C_REGISTRO54A.CDARVPROD, P_DTINICIAL, P_DTFINAL, C_REGISTRO54A.CDCLASFISC, C_REGISTRO54A.NMPRODUTO,
                      C_REGISTRO54A.SGUNIDADE, C_REGISTRO54A.CDSITUTRIBPR, C_REGISTRO54A.VRALIQIPI, C_REGISTRO54A.VRALIQICMS,
                      C_REGISTRO54A.VRPERREDBCIT, 0);
            END IF;
         END LOOP;
      END IF;
      V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
      V_CDFILIAL   := ' ';
      V_NRLANCTONF := ' ';
      V_CONTREG54B := NVL(V_CONTREG54A,0)+1;
      IF NVL(C_ITEM.IDORIGDESTNF,'C') = 'F' THEN
         /* FORNECEDOR */
         FOR C_REGISTRO54B IN (SELECT ('54'||
                                       LPAD(NVL(IT.NRINSJURNF, '00000000000000'),14,'0')||
                                       RPAD(NVL(IT.CDMODDOCFISC,'01'), 2, ' ')||
                                       RPAD(DECODE(RTRIM(NVL(IT.CDSERIE,' ')),'-',' ',REPLACE(NVL(IT.CDSERIE,' '),'-', '')),3,' ')||
                                       LPAD(SUBSTR(IT.NRNOTAFISC,LENGTH(IT.NRNOTAFISC)-5,6),6,'0')||
                                       RPAD(NVL(IT.CDFISCOPERNF,'0'),4,' ')||
                                       LPAD(NVL(IT.CDSITUTRIBPR,' '),3,'0')) AS DSINICIO,
                                      (RPAD(PR.CDARVPROD,14,' ')||
                                       LTRIM(REPLACE(TO_CHAR(ABS(IT.QTITEMPROD),'00000000.000'),'.',''))||
                                       LTRIM(REPLACE(TO_CHAR(ABS(IT.VRPRODUTO) ,'0000000000.00'),'.',''))||
                                       LTRIM(REPLACE(TO_CHAR(ABS(IT.VRDESCITEM),'0000000000.00'),'.',''))||
                                       LTRIM(REPLACE(TO_CHAR(NVL(ABS(IT.VRBASECALCIT),0),'0000000000.00'),'.',''))||
                                       '000000000000' ||
                                       LTRIM(REPLACE(TO_CHAR(NVL(ABS(VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, P_IPI, 'A')),0),'0000000000.00'),'.',''))||
                                       LTRIM(REPLACE(TO_CHAR(NVL(ABS(IT.VRALIQITEM),0),'00.00'),'.',''))) AS DSARQMAG,
                                       PR.CDARVPROD, NVL(IT.CDPRODNCMTRANS, PR.CDCLASFISC) AS CDCLASFISC, PR.NMPRODUTO, NVL(PR.SGUNIDADE, '  ') AS SGUNIDADE, NVL(IT.CDSITUTRIBPR, '   ') AS CDSITUTRIBPR,
                                       ABS(NVL(VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, P_IPI, 'A'),0)) AS VRALIQIPI,
                                       NVL(IT.VRALIQITEM,0) AS VRALIQICMS, NVL(IT.VRPERREDBCIT,0) VRPERREDBCIT,
                                       IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD
                                FROM PRODUTO PR,
                                     (SELECT /*+ INDEX(PK_ITEMPROIMPOS) II*/
                                             II.CDFILIAL, II.NRLANCTONF, DECODE(V_IDTPIJURCLIE,'I','','O', '',C_ITEM.NRINSJURNF) AS NRINSJURNF, C_ITEM.DTENTRSAID AS DTENTRSAID,
                                             C_ITEM.CDMODDOCFISC AS CDMODDOCFISC, NVL(C_ITEM.CDSERIEENTR, C_ITEM.CDSERIE) CDSERIE,
                                             V_SGESTADO, C_ITEM.NRNOTAFISC AS NRNOTAFISC, IT.NRSEQITPROD,
                                             IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                             IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                             DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')) AS VRALIQITEM,
                                             DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0) AS VRBASECALCIT,
                                             MAX(IT.CDFISOPEITEM) AS CDFISCOPERNF, MAX(II.VRPERREDBCIT) AS VRPERREDBCIT,
                                             IT.CDPRODNCMTRANS
                                      FROM ITEMPRODUTO IT, ITEMPROIMPOS II,
                                        (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('1','2','3')
                                         UNION ALL
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQMAPOIMP
                                            AND IP.CDIMPOSTO   = V_CDIMPOSAPUR
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
      /*00 NA SAIDA*/
                                         UNION
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
                                            AND NOT EXISTS(SELECT DISTINCT INF.CDFILIAL,INF.NRLANCTONF
                                                             FROM IMPOSTONF INF
                                                            WHERE INF.CDFILIAL   = IP.CDFILIAL
                                                              AND INF.NRLANCTONF = IP.NRLANCTONF
                                                              AND INF.CDIMPOSTO  = V_CDIMPOSAPUR)
      /*00 NA SAIDA*/
                                                                                         ) IP
                                      WHERE NVL(C_ITEM.IDORIGDESTNF, DECODE(C_ITEM.IDENTRSAIDOP, 'E', 'F', 'C')) = 'F'
                                        AND C_ITEM.CDFILIAL        = IP.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IP.NRLANCTONF
                                        AND C_ITEM.CDFILIAL        = IT.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.CDFILIAL        = IT.CDFILIAL
                                        AND II.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.NRSEQITPROD     = IT.NRSEQITPROD
                                        AND II.CDIMPOSTO       = IP.CDIMPOSTO
                                        AND II.NRITPROIMPOS    IN ( '1', '2')
                                        AND SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '3'
                                      GROUP BY II.CDFILIAL, II.NRLANCTONF, DECODE(V_IDTPIJURCLIE,'I','','O', '',C_ITEM.NRINSJURNF), C_ITEM.DTENTRSAID,
                                               C_ITEM.CDMODDOCFISC, NVL(C_ITEM.CDSERIEENTR,C_ITEM.CDSERIE),
                                               V_SGESTADO, C_ITEM.NRNOTAFISC, IT.NRSEQITPROD,
                                               IT.CDPRODUTO, CDSITUTRIBPR, IT.QTITEMPROD, II.VRCONTITIMP, IT.VRTOTAITEM,
                                               IT.VRDESCITEM,
                                               DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')),
                                               DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0), IT.CDPRODNCMTRANS
                                      UNION ALL
                                      SELECT /*+ INDEX(PK_ITEMPROIMPOS) II*/
                                             II.CDFILIAL, II.NRLANCTONF, '00000000000000' NRINSJURNF, C_ITEM.DTENTRSAID AS DTENTRSAID,
                                             C_ITEM.CDMODDOCFISC AS CDMODDOCFISC, NVL(C_ITEM.CDSERIEENTR, C_ITEM.CDSERIE) CDSERIE,
                                             'EX' SGESTADO, C_ITEM.NRNOTAFISC AS NRNOTAFISC, IT.NRSEQITPROD,
                                             IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                             IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                             DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')) AS VRALIQITEM,
                                             DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0) AS VRBASECALCIT,
                                             MAX(IT.CDFISOPEITEM) AS CDFISCOPERNF, MAX(II.VRPERREDBCIT) AS VRPERREDBCIT,
                                             IT.CDPRODNCMTRANS
                                      FROM ITEMPRODUTO IT, ITEMPROIMPOS II,
                                        (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('1','2','3')
                                         UNION ALL
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQMAPOIMP
                                            AND IP.CDIMPOSTO   = V_CDIMPOSAPUR
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
      /*00 NA SAIDA*/
                                         UNION
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
                                            AND NOT EXISTS(SELECT DISTINCT INF.CDFILIAL,INF.NRLANCTONF
                                                             FROM IMPOSTONF INF
                                                            WHERE INF.CDFILIAL   = IP.CDFILIAL
                                                              AND INF.NRLANCTONF = IP.NRLANCTONF
                                                              AND INF.CDIMPOSTO  = V_CDIMPOSAPUR)
      /*00 NA SAIDA*/
                                                                                                 ) IP
                                      WHERE NVL(C_ITEM.IDORIGDESTNF, DECODE(C_ITEM.IDENTRSAIDOP, 'E', 'F', 'C')) = 'C'
                                        AND C_ITEM.CDFILIAL        = IT.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IT.NRLANCTONF
                                        AND C_ITEM.CDFILIAL        = IP.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IP.NRLANCTONF
                                        AND II.CDFILIAL        = IT.CDFILIAL
                                        AND II.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.NRSEQITPROD     = IT.NRSEQITPROD
                                        AND II.CDIMPOSTO       = IP.CDIMPOSTO
                                        AND II.NRITPROIMPOS    IN ( '1', '2')
                                        AND SUBSTR(II.CDFISCOPERIT,1,1) = '3'
                                      GROUP BY II.CDFILIAL, II.NRLANCTONF, C_ITEM.DTENTRSAID,
                                               C_ITEM.CDMODDOCFISC, NVL(C_ITEM.CDSERIEENTR,C_ITEM.CDSERIE),
                                               C_ITEM.NRNOTAFISC, IT.NRSEQITPROD,
                                               IT.CDPRODUTO, CDSITUTRIBPR, IT.QTITEMPROD, II.VRCONTITIMP, IT.VRTOTAITEM,
                                               IT.VRDESCITEM,
                                               DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')),
                                               DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0), IT.CDPRODNCMTRANS ) IT
                                WHERE IT.CDPRODUTO = PR.CDPRODUTO
                                ORDER BY IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD) LOOP
            V_CONTREG54B := NVL(V_CONTREG54B,0)+1;
            IF ((C_REGISTRO54B.CDFILIAL||C_REGISTRO54B.NRLANCTONF) <> (V_CDFILIAL||V_NRLANCTONF)) THEN
               V_CDFILIAL   := C_REGISTRO54B.CDFILIAL;
               V_NRLANCTONF := C_REGISTRO54B.NRLANCTONF;
               V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
            END IF;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('06'||LPAD(RTRIM(TO_CHAR(V_CONTREG54B)), 8, '0'), C_REGISTRO54B.DSINICIO||V_SEQUENCIAL||C_REGISTRO54B.DSARQMAG, P_NRSEQUENCIAL);
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER(RTRIM(V_SEQUENCIAL))+1)),3,'0');
            V_CONTREG := NVL(V_CONTREG,0) + 1;
            V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
            SELECT MAX(CDARVPROD)
            INTO V_CDARVPROD
            FROM TMP_REGISTRO_PRODUTO
            WHERE CDARVPROD = C_REGISTRO54B.CDARVPROD;
            IF V_CDARVPROD IS NULL THEN
               INSERT INTO TMP_REGISTRO_PRODUTO(CDTIPOREG, CDARVPROD, DTINICIAL, DTFINAL, CDCLASFISC, NMPRODUTO,
                                                SGUNIDADE, CDSITUTRIBPR, VRALIQIPI, VRALIQICMS, VRPERREDBCIT, VRBASESUBST)
               VALUES('75', C_REGISTRO54B.CDARVPROD, P_DTINICIAL, P_DTFINAL, C_REGISTRO54B.CDCLASFISC, C_REGISTRO54B.NMPRODUTO,
                      C_REGISTRO54B.SGUNIDADE, C_REGISTRO54B.CDSITUTRIBPR, C_REGISTRO54B.VRALIQIPI, C_REGISTRO54B.VRALIQICMS,
                      C_REGISTRO54B.VRPERREDBCIT, 0);
            END IF;
         END LOOP;
      END IF;
      V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
      V_CDFILIAL   := ' ';
      V_NRLANCTONF := ' ';
      V_CONTREG54C := NVL(V_CONTREG54B,V_CONTREG54A)+1;
      IF NVL(C_ITEM.IDORIGDESTNF,'C') = 'L' THEN
         /* FILIAL */
         FOR C_REGISTRO54C IN (SELECT
                                   ('54'||
                                    RPAD(NVL(IT.NRINSJURNF, '00000000000000'),14,' ')||
                                    RPAD(NVL(IT.CDMODDOCFISC,'01'), 2, ' ')||
                                    RPAD(DECODE(RTRIM(NVL(IT.CDSERIE,' ')),'-',' ',REPLACE(NVL(IT.CDSERIE,' '),'-', '')),3,' ')||
                                    LPAD(SUBSTR(IT.NRNOTAFISC,LENGTH(IT.NRNOTAFISC)-5,6),6,'0')||
                                    RPAD(NVL(IT.CDFISCOPERNF,'0'),4,' ')||
                                    LPAD(NVL(IT.CDSITUTRIBPR,' '),3,'0')) AS DSINICIO,
                                   (RPAD(PR.CDARVPROD,14,' ')||
                                    LTRIM(REPLACE(TO_CHAR(ABS(IT.QTITEMPROD),'00000000.000'),'.',''))||
                                    LTRIM(REPLACE(TO_CHAR(ABS(IT.VRPRODUTO) ,'0000000000.00'),'.',''))||
                                    LTRIM(REPLACE(TO_CHAR(ABS(IT.VRDESCITEM),'0000000000.00'),'.',''))||
                                    LTRIM(REPLACE(TO_CHAR(NVL(ABS(IT.VRBASECALCIT),0),'0000000000.00'),'.',''))||
                                    '000000000000' ||
                                    LTRIM(REPLACE(TO_CHAR(NVL(ABS(VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, P_IPI, 'A')),0),'0000000000.00'),'.',''))||
                                    LTRIM(REPLACE(TO_CHAR(NVL(ABS(IT.VRALIQITEM),0),'00.00'),'.',''))) AS DSARQMAG,
                                    PR.CDARVPROD, NVL(IT.CDPRODNCMTRANS, PR.CDCLASFISC) AS CDCLASFISC, PR.NMPRODUTO, NVL(PR.SGUNIDADE, '  ') AS SGUNIDADE, NVL(IT.CDSITUTRIBPR, '   ') AS CDSITUTRIBPR,
                                    ABS(NVL(VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, P_IPI, 'A'),0)) AS VRALIQIPI,
                                    NVL(IT.VRALIQITEM,0) AS VRALIQICMS,
                                    NVL(IT.VRPERREDBCIT,0) VRPERREDBCIT,
                                    IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD
                                FROM PRODUTO PR,
                                     (SELECT /*+ INDEX(PK_ITEMPROIMPOS) II*/
                                             II.CDFILIAL, II.NRLANCTONF, REPLACE(REPLACE(REPLACE(C_ITEM.NRINSJURNF, '-', ''), '.', ''), '/', '') AS NRINSJURNF, C_ITEM.DTEMISSAO,
                                             C_ITEM.CDMODDOCFISC AS CDMODDOCFISC, NVL(C_ITEM.CDSERIE, C_ITEM.CDSERIEENTR) CDSERIE,
                                             V_SGESTADO AS SGESTADO, C_ITEM.NRNOTAFISC AS NRNOTAFISC, IT.NRSEQITPROD,
                                             IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                             IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                             II.VRALIQITEM AS VRALIQITEM,
                                             DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0) AS VRBASECALCIT,
                                             MAX(IT.CDFISOPEITEM) AS CDFISCOPERNF, MAX(II.VRPERREDBCIT) AS VRPERREDBCIT,
                                             IT.CDPRODNCMTRANS
                                      FROM ITEMPRODUTO IT, ITEMPROIMPOS II,
                                        (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQMAPOIMP
                                            AND IP.CDIMPOSTO   = V_CDIMPOSAPUR
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
      /*00 NA SAIDA*/
                                         UNION
                                         SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF,IP.CDIMPOSTO
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('5','6','7')
                                            AND NOT EXISTS(SELECT DISTINCT INF.CDFILIAL,INF.NRLANCTONF
                                                             FROM IMPOSTONF INF
                                                            WHERE INF.CDFILIAL   = IP.CDFILIAL
                                                              AND INF.NRLANCTONF = IP.NRLANCTONF
                                                              AND INF.CDIMPOSTO  = V_CDIMPOSAPUR)
      /*00 NA SAIDA*/
                                                                                                 ) IP
                                      WHERE C_ITEM.IDENTRSAIDOP    = 'S'                           
                                        AND NVL(C_ITEM.IDORIGDESTNF, DECODE(C_ITEM.IDENTRSAIDOP, 'E', 'F', 'C')) = 'L'
                                        AND C_ITEM.CDFILIAL        = IP.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IP.NRLANCTONF
                                        AND C_ITEM.CDFILIAL        = IT.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.CDFILIAL        = IT.CDFILIAL
                                        AND II.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.NRSEQITPROD     = IT.NRSEQITPROD
                                        AND II.CDIMPOSTO       = IP.CDIMPOSTO
                                        AND II.NRITPROIMPOS    IN ( '1', '2')
                                        AND SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '7'
                                      GROUP BY II.CDFILIAL, II.NRLANCTONF, REPLACE(REPLACE(REPLACE(C_ITEM.NRINSJURNF, '-', ''), '.', ''), '/', ''),
                                               C_ITEM.DTEMISSAO, C_ITEM.CDMODDOCFISC, NVL(C_ITEM.CDSERIE, C_ITEM.CDSERIEENTR),
                                               V_SGESTADO, C_ITEM.NRNOTAFISC, IT.NRSEQITPROD,
                                               IT.CDPRODUTO, CDSITUTRIBPR, IT.QTITEMPROD, II.VRCONTITIMP, IT.VRTOTAITEM,
                                               IT.VRDESCITEM, II.VRALIQITEM,
                                               DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0),IT.CDPRODNCMTRANS
                                      UNION ALL
                                      SELECT /*+ INDEX(PK_ITEMPROIMPOS) II*/
                                             II.CDFILIAL, II.NRLANCTONF, REPLACE(REPLACE(REPLACE(C_ITEM.NRINSJURNF, '-', ''), '.', ''), '/', '') AS NRINSJURNF,
                                             C_ITEM.DTENTRSAID AS DTENTRSAID,
                                             C_ITEM.CDMODDOCFISC, NVL(C_ITEM.CDSERIEENTR, C_ITEM.CDSERIE) CDSERIE,
                                             V_SGESTADO AS SGESTADO, C_ITEM.NRNOTAFISC AS NRNOTAFISC, IT.NRSEQITPROD,
                                             IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                             IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                             DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')) AS VRALIQITEM,
                                             DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0) AS VRBASECALCIT,
                                             MAX(IT.CDFISOPEITEM) AS CDFISCOPERNF, MAX(II.VRPERREDBCIT) AS VRPERREDBCIT,
                                             IT.CDPRODNCMTRANS
                                      FROM ITEMPRODUTO IT, ITEMPROIMPOS II,
                                        (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                           FROM IMPOSTONF IP
                                          WHERE C_ITEM.NRSEQUMAPAPU = IP.NRSEQUMAPAPU
                                            AND IP.CDIMPOSTO   = P_CDIMPOSTO
                                            AND SUBSTR(CDFISCOPERNF,1,1) IN ('1','2','3')) IP
                                      WHERE C_ITEM.IDENTRSAIDOP    = 'E'
                                        AND NVL(C_ITEM.IDORIGDESTNF, DECODE(C_ITEM.IDENTRSAIDOP, 'E', 'F', 'C')) = 'L'
                                        AND C_ITEM.CDFILIAL        = IP.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IP.NRLANCTONF
                                        AND C_ITEM.CDFILIAL        = IT.CDFILIAL
                                        AND C_ITEM.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.CDFILIAL        = IT.CDFILIAL
                                        AND II.NRLANCTONF      = IT.NRLANCTONF
                                        AND II.NRSEQITPROD     = IT.NRSEQITPROD
                                        AND II.CDIMPOSTO       = P_CDIMPOSTO
                                        AND II.NRITPROIMPOS    IN ( '1', '2')
                                        AND SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '3'
                                      GROUP BY II.CDFILIAL, II.NRLANCTONF, C_ITEM.NRINSJURNF, C_ITEM.DTENTRSAID,
                                               C_ITEM.CDMODDOCFISC, NVL(C_ITEM.CDSERIEENTR,C_ITEM.CDSERIE),
                                               V_SGESTADO, C_ITEM.NRNOTAFISC, IT.NRSEQITPROD,
                                               IT.CDPRODUTO, CDSITUTRIBPR, IT.QTITEMPROD, II.VRCONTITIMP, IT.VRTOTAITEM,
                                               IT.VRDESCITEM,
                                               DECODE(C_ITEM.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_CDIMPOSAPUR, 'A'), DECODE(II.CDFISCALIT, '1', II.VRALIQITEM, '0')),
                                               DECODE(II.CDFISCALIT,'1', II.VRBASECALCIT, 0), IT.CDPRODNCMTRANS ) IT
                                WHERE IT.CDPRODUTO = PR.CDPRODUTO
                                ORDER BY IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD) LOOP
            V_CONTREG54C := NVL(V_CONTREG54C,0)+1;
            IF ((C_REGISTRO54C.CDFILIAL||C_REGISTRO54C.NRLANCTONF) <> (V_CDFILIAL||V_NRLANCTONF)) THEN
               V_CDFILIAL   := C_REGISTRO54C.CDFILIAL;
               V_NRLANCTONF := C_REGISTRO54C.NRLANCTONF;
               V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
            END IF;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('07'||LPAD(RTRIM(TO_CHAR(V_CONTREG54C)), 8, '0'), C_REGISTRO54C.DSINICIO||V_SEQUENCIAL||C_REGISTRO54C.DSARQMAG, P_NRSEQUENCIAL);
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER(RTRIM(V_SEQUENCIAL))+1)),3,'0');
            V_CONTREG := NVL(V_CONTREG,0) + 1;
            V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
            SELECT MAX(CDARVPROD)
            INTO V_CDARVPROD
            FROM TMP_REGISTRO_PRODUTO
            WHERE CDARVPROD = C_REGISTRO54C.CDARVPROD;
            IF V_CDARVPROD IS NULL THEN
               INSERT INTO TMP_REGISTRO_PRODUTO(CDTIPOREG, CDARVPROD, DTINICIAL, DTFINAL, CDCLASFISC, NMPRODUTO,
                                                SGUNIDADE, CDSITUTRIBPR, VRALIQIPI, VRALIQICMS, VRPERREDBCIT, VRBASESUBST)
               VALUES('75', C_REGISTRO54C.CDARVPROD, P_DTINICIAL, P_DTFINAL, C_REGISTRO54C.CDCLASFISC, C_REGISTRO54C.NMPRODUTO,
                      C_REGISTRO54C.SGUNIDADE, C_REGISTRO54C.CDSITUTRIBPR, C_REGISTRO54C.VRALIQIPI, C_REGISTRO54C.VRALIQICMS,
                      C_REGISTRO54C.VRPERREDBCIT, 0);
            END IF;
         END LOOP;
      END IF;
   END LOOP;
   IF V_CONTREG > 0 THEN
      V_STTOTAL := P_CONTTOTAL||'54'||LPAD(RTRIM(TO_CHAR(V_CONTREG)),8,'0');
   END IF;
   RETURN(V_STTOTAL||';'||TO_CHAR(V_CONTGERAL));
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('REG54_A_B_C_SINTEGRA_RJ','5.05.166'); END;
/

