CREATE OR REPLACE FUNCTION ARQUIVO_54_GIM_PB(   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN EMPRESA.CDINSCESTA%TYPE,
   P_CDINSCESTAST IN NOTAFISCAL.CDINSCESTAST%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_CDPAIS       IN ESTADO.CDPAIS%TYPE,
   P_SGESTADO     IN VARCHAR2,
   P_SEQMAGNET    IN VARCHAR,
   P_CONTADOR     IN NUMBER,
   P_LIVRO        IN NOTAFISCAL.NRLANCTONF%TYPE) RETURN VARCHAR
AS
   V_CONTGERAL      NUMBER;
   V_CONTREG        NUMBER;
   V_CONTADOR       NUMBER;
   V_CDARVPROD      VARCHAR2(20);
   V_SEQUENCIAL     VARCHAR2(03);
   V_CDFILIAL       VARCHAR2(04);
   V_NRLANCTONF     VARCHAR2(10);
   V_IPI            VARCHAR2(02);
BEGIN
   V_CONTREG := 0;
   V_CONTADOR := P_CONTADOR;
   V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
   V_CDFILIAL   := ' ';
   V_NRLANCTONF := ' ';

   /* busca o codigo do imposto ipi */
   V_IPI := '10';

   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_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_IPI, 'A')),0),'0000000000.00'),'.',''))||
                              LTRIM(REPLACE(TO_CHAR(NVL(ABS(IT.VRALIQITEM),0),'00.00'),'.',''))) AS DSARQMAG,
                              PR.CDARVPROD, NVL(NVL(IT.CDPRODNCMTRANS,PR.CDCLASFISC), ' ')AS CDPRODNCMTRANS, PR.NMPRODUTO, NVL(PR.SGUNIDADE, '  ') AS SGUNIDADE, NVL(IT.CDSITUTRIBPR, '   ') AS CDSITUTRIBPR,
                              ABS(NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_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*/
                                       NF.CDFILIAL, NF.NRLANCTONF, DECODE(CL.IDTPIJURCLIE,'I','','O', '',CL.NRINSJURCLIE) AS NRINSJURNF, NF.DTEMISSAO,
                                       NF.CDMODDOCFISC, NVL(NF.CDSERIE, NF.CDSERIEENTR) CDSERIE,
                                       EC.SGESTADO, NF.NRNOTAFISC, IT.NRSEQITPROD,
                                       IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                       IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                       MAX(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 NOTAFISCAL NF, ITEMPRODUTO IT, ITEMPROIMPOS II, IMPOSTO IM,
                                     CLIENTE CL, ENDECLIE EC, MODDOCFISC MD,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO 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 = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO   = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IP
                                WHERE NVL(NF.IDORIGDESTNF, DECODE(NF.IDENTRSAIDOP, 'E', 'F', 'C')) = 'C' AND
                                      NF.CDFILIAL        = IP.CDFILIAL                  AND
                                      NF.NRLANCTONF      = IP.NRLANCTONF                AND
                                      NF.CDFILIAL        = IT.CDFILIAL                  AND
                                      NF.NRLANCTONF      = IT.NRLANCTONF                AND
                                      ((NF.CDINSCESTAST = P_CDINSCESTAST) OR (P_CDINSCESTAST IS NULL AND NF.CDINSCESTAST IS NULL)) AND
                                      NF.IDSTATUS IN ('A','E')                          AND
                                      II.CDFILIAL        = IT.CDFILIAL                  AND
                                      II.NRLANCTONF      = IT.NRLANCTONF                AND
                                      II.NRSEQITPROD     = IT.NRSEQITPROD               AND
                                      II.CDIMPOSTO       = IM.CDIMPOSTO                 AND
                                      IM.CDIMPOSTONFE    = P_CDIMPOSTO                  AND
                                      II.NRITPROIMPOS    <> '3'                         AND
                                      SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '3'       AND
                                      SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '7'       AND
                                      CL.CDCLIENTE(+)    = NF.CDCLIENTE                 AND
                                      EC.CDCLIENTE(+)    = CL.CDCLIENTE                 AND
                                      EC.IDTPENDECLIE(+) = NVL(CL.IDENDEMISCLI,'P')     AND
                                      EC.CDPAIS          = NVL(P_CDPAIS,EC.CDPAIS)      AND
                                      (INSTR(NVL(P_SGESTADO, EC.SGESTADO), EC.SGESTADO) > 0) AND
                                      NF.CDMODDOCFISC    = MD.CDMODDOCFISC              AND
                                      NVL(MD.CDTPREGFISC, '50')  = '50'

                                GROUP BY NF.CDFILIAL, NF.NRLANCTONF, DECODE(CL.IDTPIJURCLIE,'I','','O', '',CL.NRINSJURCLIE), NF.DTEMISSAO,
                                         NF.CDMODDOCFISC, NVL(NF.CDSERIE, NF.CDSERIEENTR),
                                         EC.SGESTADO, NF.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*/
                                       NF.CDFILIAL, NF.NRLANCTONF, '00000000000000' NRINSJURNF, NF.DTEMISSAO,
                                       NF.CDMODDOCFISC, NVL(NF.CDSERIE, NF.CDSERIEENTR) CDSERIE,
                                       'EX' SGESTADO, NF.NRNOTAFISC, IT.NRSEQITPROD,
                                       IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                       IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                       MAX(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 NOTAFISCAL NF, ITEMPRODUTO IT, ITEMPROIMPOS II, IMPOSTO IM,
                                     CLIENTE CL, ENDECLIE EC, MODDOCFISC MD,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO 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 = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO   = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IP
                                WHERE NVL(NF.IDORIGDESTNF, DECODE(NF.IDENTRSAIDOP, 'E', 'F', 'C')) = 'C' AND
                                      NF.CDFILIAL        = IP.CDFILIAL                       AND
                                      NF.NRLANCTONF      = IP.NRLANCTONF                     AND
                                      NF.CDFILIAL        = IT.CDFILIAL                       AND
                                      NF.NRLANCTONF      = IT.NRLANCTONF                     AND
                                      ((NF.CDINSCESTAST = P_CDINSCESTAST) OR (P_CDINSCESTAST IS NULL AND NF.CDINSCESTAST IS NULL)) AND
                                      NF.IDSTATUS IN ('A','E')                               AND
                                      II.CDFILIAL        = IT.CDFILIAL                       AND
                                      II.NRLANCTONF      = IT.NRLANCTONF                     AND
                                      II.NRSEQITPROD     = IT.NRSEQITPROD                    AND
                                      II.CDIMPOSTO       = IM.CDIMPOSTO                      AND
                                      IM.CDIMPOSTONFE    = P_CDIMPOSTO                       AND
                                      II.NRITPROIMPOS    <> '3'                              AND
                                      SUBSTR(II.CDFISCOPERIT, 1, 1) = '7'                    AND
                                      CL.CDCLIENTE(+)    = NF.CDCLIENTE                      AND
                                      EC.CDCLIENTE(+)    = CL.CDCLIENTE                      AND
                                      EC.IDTPENDECLIE(+) = NVL(CL.IDENDEMISCLI,'P')          AND
                                      EC.CDPAIS          = NVL(P_CDPAIS,EC.CDPAIS)           AND
                                      (INSTR(NVL(P_SGESTADO, EC.SGESTADO), EC.SGESTADO) > 0) AND
                                      NF.CDMODDOCFISC    = MD.CDMODDOCFISC                   AND
                                      NVL(MD.CDTPREGFISC, '50')  = '50'
                                GROUP BY NF.CDFILIAL, NF.NRLANCTONF, NF.DTEMISSAO,
                                         NF.CDMODDOCFISC, NVL(NF.CDSERIE, NF.CDSERIEENTR),
                                         NF.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*/
                                       NF.CDFILIAL, NF.NRLANCTONF, '00000000000000' NRINSJURNF, NF.DTEMISSAO,
                                       NF.CDMODDOCFISC, NVL(NF.CDSERIE, NF.CDSERIEENTR) CDSERIE,
                                       'EX' SGESTADO, NF.NRNOTAFISC, IT.NRSEQITPROD,
                                       IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                       IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                       MAX(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 NOTAFISCAL NF, ITEMPRODUTO IT, ITEMPROIMPOS II, IMPOSTO IM,
                                     FORNECEDOR FO, ENDEFORN EF, MODDOCFISC MD,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO 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 = IP.NRSEQUMAPAPU
                                     AND IP.CDIMPOSTO   = I.CDIMPOSTO
                                     AND I.CDIMPOSTONFE = P_CDIMPOSTO) IP
                                WHERE NVL(NF.IDORIGDESTNF, DECODE(NF.IDENTRSAIDOP, 'E', 'F', 'C')) = 'F'
                                  AND NF.CDFILIAL        = IP.CDFILIAL
                                  AND NF.NRLANCTONF      = IP.NRLANCTONF
                                  AND NF.CDFILIAL        = IT.CDFILIAL
                                  AND NF.NRLANCTONF      = IT.NRLANCTONF
                                  AND ((NF.CDINSCESTAST = P_CDINSCESTAST) OR (P_CDINSCESTAST IS NULL AND NF.CDINSCESTAST IS NULL))
                                  AND NF.IDSTATUS IN ('A','E')
                                  AND II.CDFILIAL        = IT.CDFILIAL
                                  AND II.NRLANCTONF      = IT.NRLANCTONF
                                  AND II.NRSEQITPROD     = IT.NRSEQITPROD
                                  AND II.CDIMPOSTO       = IM.CDIMPOSTO
                                  AND IM.CDIMPOSTONFE    = P_CDIMPOSTO
                                  AND II.NRITPROIMPOS    <> '3'
                                  AND SUBSTR(II.CDFISCOPERIT, 1, 1) = '3'
                                  AND FO.CDFORNECED(+)    = NF.CDFORNECED
                                  AND EF.CDFORNECED(+)    = FO.CDFORNECED
                                  AND EF.IDTPENDEFORN(+) = NVL(FO.IDENDEMISFOR,'P')
                                  AND EF.CDPAIS          = NVL(P_CDPAIS,EF.CDPAIS)
                                  AND (INSTR(NVL(P_SGESTADO, EF.SGESTADO), EF.SGESTADO) > 0)
                                  AND NF.CDMODDOCFISC    = MD.CDMODDOCFISC
                                  AND NVL(MD.CDTPREGFISC, '50')  = '50'
                                GROUP BY NF.CDFILIAL, NF.NRLANCTONF, NF.DTEMISSAO,
                                         NF.CDMODDOCFISC, NVL(NF.CDSERIE, NF.CDSERIEENTR),
                                         NF.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) IT

                          WHERE IT.CDPRODUTO = PR.CDPRODUTO
                          ORDER BY IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD) LOOP


      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;

      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('54'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO54A.DSINICIO||V_SEQUENCIAL||C_REGISTRO54A.DSARQMAG, P_SEQMAGNET);

      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.CDPRODNCMTRANS, C_REGISTRO54A.NMPRODUTO,
                C_REGISTRO54A.SGUNIDADE, C_REGISTRO54A.CDSITUTRIBPR, C_REGISTRO54A.VRALIQIPI, C_REGISTRO54A.VRALIQICMS,
                C_REGISTRO54A.VRPERREDBCIT, 0);
      END IF;
   END LOOP;

   V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
   V_CDFILIAL   := ' ';
   V_NRLANCTONF := ' ';

   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_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_IPI, 'A')),0),'0000000000.00'),'.',''))||
                                 LTRIM(REPLACE(TO_CHAR(NVL(ABS(IT.VRALIQITEM),0),'00.00'),'.',''))) AS DSARQMAG,
                                 PR.CDARVPROD, NVL(NVL(IT.CDPRODNCMTRANS,PR.CDCLASFISC), ' ') AS CDPRODNCMTRANS, PR.NMPRODUTO, NVL(PR.SGUNIDADE, '  ') AS SGUNIDADE, NVL(IT.CDSITUTRIBPR, '   ') AS CDSITUTRIBPR,
                                 ABS(NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_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*/
                                       NF.CDFILIAL, NF.NRLANCTONF, DECODE(FO.IDTPIJURFORN,'I','','O', '',FO.NRINSJURFORN) AS NRINSJURNF, NF.DTENTRSAID,
                                       NF.CDMODDOCFISC, NVL(NF.CDSERIEENTR, NF.CDSERIE) CDSERIE,
                                       EF.SGESTADO, NF.NRNOTAFISC, IT.NRSEQITPROD,
                                       IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                       IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                       MAX(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 NOTAFISCAL NF, ITEMPRODUTO IT, ITEMPROIMPOS II, IMPOSTO IM,
                                     FORNECEDOR FO, ENDEFORN EF, MODDOCFISC MD,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO 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 = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO   = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IP
                                WHERE NVL(NF.IDORIGDESTNF, DECODE(NF.IDENTRSAIDOP, 'E', 'F', 'C')) = 'F' AND
                                      NF.CDFILIAL        = IP.CDFILIAL                       AND
                                      NF.NRLANCTONF      = IP.NRLANCTONF                     AND
                                      NF.CDFILIAL        = IT.CDFILIAL                       AND
                                      NF.NRLANCTONF      = IT.NRLANCTONF                     AND
                                      ((NF.CDINSCESTAST = P_CDINSCESTAST) OR (P_CDINSCESTAST IS NULL AND NF.CDINSCESTAST IS NULL)) AND
                                      NF.IDSTATUS IN ('A','E')                               AND
                                      II.CDFILIAL        = IT.CDFILIAL                       AND
                                      II.NRLANCTONF      = IT.NRLANCTONF                     AND
                                      II.NRSEQITPROD     = IT.NRSEQITPROD                    AND
                                      II.CDIMPOSTO       = IM.CDIMPOSTO                      AND
                                      IM.CDIMPOSTONFE    = P_CDIMPOSTO                       AND
                                      II.NRITPROIMPOS    <> '3'                              AND
                                      SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '3'            AND
                                      FO.CDFORNECED(+)   = NF.CDFORNECED                     AND
                                      EF.CDFORNECED(+)   = FO.CDFORNECED                     AND
                                      EF.IDTPENDEFORN(+) = NVL(FO.IDENDEMISFOR,'P')          AND
                                      EF.CDPAIS          = NVL(P_CDPAIS,EF.CDPAIS)           AND
                                      (INSTR(NVL(P_SGESTADO, EF.SGESTADO), EF.SGESTADO) > 0) AND
                                      NF.CDMODDOCFISC    = MD.CDMODDOCFISC                   AND
                                      NVL(MD.CDTPREGFISC, '50')  = '50'
                                GROUP BY NF.CDFILIAL, NF.NRLANCTONF, DECODE(FO.IDTPIJURFORN,'I','','O', '',FO.NRINSJURFORN), NF.DTENTRSAID,
                                         NF.CDMODDOCFISC, NVL(NF.CDSERIEENTR,NF.CDSERIE),
                                         EF.SGESTADO, NF.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*/
                                       NF.CDFILIAL, NF.NRLANCTONF, '00000000000000' NRINSJURNF, NF.DTENTRSAID,
                                       NF.CDMODDOCFISC, NVL(NF.CDSERIEENTR, NF.CDSERIE) CDSERIE,
                                       'EX' SGESTADO, NF.NRNOTAFISC, IT.NRSEQITPROD,
                                       IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                       IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                       MAX(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 NOTAFISCAL NF, ITEMPRODUTO IT, ITEMPROIMPOS II, IMPOSTO IM,
                                     FORNECEDOR FO, ENDEFORN EF, MODDOCFISC MD,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO 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 = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO   = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IP
                                WHERE NVL(NF.IDORIGDESTNF, DECODE(NF.IDENTRSAIDOP, 'E', 'F', 'C')) = 'C' AND
                                      NF.CDFILIAL        = IT.CDFILIAL                       AND
                                      NF.NRLANCTONF      = IT.NRLANCTONF                     AND
                                      NF.CDFILIAL        = IP.CDFILIAL                       AND
                                      NF.NRLANCTONF      = IP.NRLANCTONF                     AND
                                      ((NF.CDINSCESTAST = P_CDINSCESTAST) OR (P_CDINSCESTAST IS NULL AND NF.CDINSCESTAST IS NULL)) AND
                                      NF.IDSTATUS IN ('A','E')                               AND
                                      II.CDFILIAL        = IT.CDFILIAL                       AND
                                      II.NRLANCTONF      = IT.NRLANCTONF                     AND
                                      II.NRSEQITPROD     = IT.NRSEQITPROD                    AND
                                      II.CDIMPOSTO       = IM.CDIMPOSTO                      AND
                                      IM.CDIMPOSTONFE    = P_CDIMPOSTO                       AND
                                      II.NRITPROIMPOS    <> '3'                              AND
                                      SUBSTR(II.CDFISCOPERIT,1,1) = '3'                      AND
                                      FO.CDFORNECED(+)   = NF.CDFORNECED                     AND
                                      EF.CDFORNECED(+)   = FO.CDFORNECED                     AND
                                      EF.IDTPENDEFORN(+) = NVL(FO.IDENDEMISFOR,'P')          AND
                                      EF.CDPAIS          = NVL(P_CDPAIS,EF.CDPAIS)           AND
                                      (INSTR(NVL(P_SGESTADO, EF.SGESTADO), EF.SGESTADO) > 0) AND
                                      NF.CDMODDOCFISC    = MD.CDMODDOCFISC                   AND
                                      NVL(MD.CDTPREGFISC, '50')  = '50'
                                GROUP BY NF.CDFILIAL, NF.NRLANCTONF, NF.DTENTRSAID,
                                         NF.CDMODDOCFISC, NVL(NF.CDSERIEENTR,NF.CDSERIE),
                                         NF.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 ) IT

                          WHERE IT.CDPRODUTO = PR.CDPRODUTO
                          ORDER BY IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD) LOOP

      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;

      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('54'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO54B.DSINICIO||V_SEQUENCIAL||C_REGISTRO54B.DSARQMAG, P_SEQMAGNET);

      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.CDPRODNCMTRANS, C_REGISTRO54B.NMPRODUTO,
                C_REGISTRO54B.SGUNIDADE, C_REGISTRO54B.CDSITUTRIBPR, C_REGISTRO54B.VRALIQIPI, C_REGISTRO54B.VRALIQICMS,
                C_REGISTRO54B.VRPERREDBCIT, 0);
      END IF;

   END LOOP;

   V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
   V_CDFILIAL   := ' ';
   V_NRLANCTONF := ' ';

   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_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_IPI, 'A')),0),'0000000000.00'),'.',''))||
                              LTRIM(REPLACE(TO_CHAR(NVL(ABS(IT.VRALIQITEM),0),'00.00'),'.',''))) AS DSARQMAG,
                              PR.CDARVPROD, NVL(NVL(IT.CDPRODNCMTRANS,PR.CDCLASFISC), ' ')AS CDPRODNCMTRANS, PR.NMPRODUTO, NVL(PR.SGUNIDADE, '  ') AS SGUNIDADE, NVL(IT.CDSITUTRIBPR, '   ') AS CDSITUTRIBPR,
                              ABS(NVL(VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD, V_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*/
                                       NF.CDFILIAL, NF.NRLANCTONF, REPLACE(REPLACE(REPLACE(FI.NRINSJURFILI, '-', ''), '.', ''), '/', '') AS NRINSJURNF, NF.DTEMISSAO,
                                       NF.CDMODDOCFISC, NVL(NF.CDSERIE, NF.CDSERIEENTR) CDSERIE,
                                       DECODE(NVL(FI.IDENDEMISFIL,'P'),'I',IE.SGESTADO,EF.SGESTADO) AS SGESTADO,
                                       NF.NRNOTAFISC, IT.NRSEQITPROD, IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                       IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                       MAX(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 NOTAFISCAL NF, ITEMPRODUTO IT, ITEMPROIMPOS II, IMPOSTO IM,
                                     FILIAL FI, ENDEFILI EF, MODDOCFISC MD, INSCRESTAD IE,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO 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 = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO   = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IP
                                WHERE NF.IDENTRSAIDOP    = 'S'                           AND
                                      NVL(NF.IDORIGDESTNF, DECODE(NF.IDENTRSAIDOP, 'E', 'F', 'C')) = 'L' AND
                                      NF.CDFILIAL        = IP.CDFILIAL                   AND
                                      NF.NRLANCTONF      = IP.NRLANCTONF                 AND
                                      NF.CDFILIAL        = IT.CDFILIAL                   AND
                                      NF.NRLANCTONF      = IT.NRLANCTONF                 AND
                                      ((NF.CDINSCESTAST = P_CDINSCESTAST) OR (P_CDINSCESTAST IS NULL AND NF.CDINSCESTAST IS NULL)) AND
                                      NF.IDSTATUS IN ('A','E')                           AND
                                      II.CDFILIAL        = IT.CDFILIAL                   AND
                                      II.NRLANCTONF      = IT.NRLANCTONF                 AND
                                      II.NRSEQITPROD     = IT.NRSEQITPROD                AND
                                      II.CDIMPOSTO       = IM.CDIMPOSTO                  AND
                                      IM.CDIMPOSTONFE    = P_CDIMPOSTO                   AND
                                      II.NRITPROIMPOS    <> '3'                          AND
                                      SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '7'        AND
                                      FI.CDFILIAL(+)     = NF.CDFILIRECE                 AND
                                      EF.CDFILIAL(+)     = FI.CDFILIAL                   AND
                                      EF.IDTPENDEFILI(+) = NVL(FI.IDENDEMISFIL,'P')      AND
                                      NF.CDINSCESTA      = IE.CDINSCESTA                 AND
                                      IE.CDEMPRESA       = P_CDEMPRESA                   AND
                                      NVL(EF.CDPAIS,IE.CDPAIS) = NVL(P_CDPAIS,NVL(EF.CDPAIS,IE.CDPAIS)) AND
                                      (INSTR(NVL(P_SGESTADO, NVL(EF.SGESTADO,IE.SGESTADO)), NVL(EF.SGESTADO,IE.SGESTADO)) > 0) AND
                                      NF.CDMODDOCFISC    = MD.CDMODDOCFISC               AND
                                      NVL(MD.CDTPREGFISC, '50')  = '50'

                                GROUP BY NF.CDFILIAL, NF.NRLANCTONF, FI.NRINSJURFILI, NF.DTEMISSAO,
                                         NF.CDMODDOCFISC, NVL(NF.CDSERIE, NF.CDSERIEENTR),
                                         DECODE(NVL(FI.IDENDEMISFIL,'P'),'I',IE.SGESTADO,EF.SGESTADO), NF.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*/
                                       NF.CDFILIAL, NF.NRLANCTONF, REPLACE(REPLACE(REPLACE(FI.NRINSJURFILI, '-', ''), '.', ''), '/', '') AS NRINSJURNF, NF.DTENTRSAID,
                                       NF.CDMODDOCFISC, NVL(NF.CDSERIEENTR, NF.CDSERIE) CDSERIE,
                                       DECODE(NVL(FI.IDENDEMISFIL,'P'),'I',IE.SGESTADO,EF.SGESTADO) AS SGESTADO, NF.NRNOTAFISC,
                                       IT.NRSEQITPROD, IT.CDPRODUTO, LPAD(CDSITUTRIBPR,3,'0') CDSITUTRIBPR,
                                       IT.QTITEMPROD, (II.VRCONTITIMP) VRPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM,
                                       MAX(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 NOTAFISCAL NF, ITEMPRODUTO IT, ITEMPROIMPOS II, IMPOSTO IM,
                                     FILIAL FI, ENDEFILI EF, MODDOCFISC MD, INSCRESTAD IE,
                                  (SELECT DISTINCT IP.CDFILIAL, IP.NRLANCTONF
                                   FROM MAPAAPURAC M, IMPOSTONF IP, IMPOSTO 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 = IP.NRSEQUMAPAPU AND
                                         IP.CDIMPOSTO   = I.CDIMPOSTO     AND
                                         I.CDIMPOSTONFE = P_CDIMPOSTO) IP
                                WHERE NF.IDENTRSAIDOP    = 'E'                                           AND
                                      NVL(NF.IDORIGDESTNF, DECODE(NF.IDENTRSAIDOP, 'E', 'F', 'C')) = 'L' AND
                                      NF.CDFILIAL        = IP.CDFILIAL                  AND
                                      NF.NRLANCTONF      = IP.NRLANCTONF                AND
                                      NF.CDFILIAL        = IT.CDFILIAL                  AND
                                      NF.NRLANCTONF      = IT.NRLANCTONF                AND
                                      ((NF.CDINSCESTAST = P_CDINSCESTAST) OR (P_CDINSCESTAST IS NULL AND NF.CDINSCESTAST IS NULL)) AND
                                      NF.IDSTATUS IN ('A','E')                          AND
                                      II.CDFILIAL        = IT.CDFILIAL                  AND
                                      II.NRLANCTONF      = IT.NRLANCTONF                AND
                                      II.NRSEQITPROD     = IT.NRSEQITPROD               AND
                                      II.CDIMPOSTO       = IM.CDIMPOSTO                 AND
                                      IM.CDIMPOSTONFE    = P_CDIMPOSTO                  AND
                                      II.NRITPROIMPOS    <> '3'                         AND
                                      SUBSTR(NVL(II.CDFISCOPERIT,' '),1,1) <> '3'       AND
                                      FI.CDFILIAL(+)     = NF.CDFILIEMIT                AND
                                      EF.CDFILIAL(+)     = FI.CDFILIAL                  AND
                                      EF.IDTPENDEFILI(+) = NVL(FI.IDENDEMISFIL,'P')     AND
                                      NF.CDINSCESTA      = IE.CDINSCESTA                AND
                                      IE.CDEMPRESA       = P_CDEMPRESA                  AND
                                      NVL(EF.CDPAIS,IE.CDPAIS) = NVL(P_CDPAIS,NVL(EF.CDPAIS,IE.CDPAIS)) AND
                                      (INSTR(NVL(P_SGESTADO, NVL(EF.SGESTADO,IE.SGESTADO)), NVL(EF.SGESTADO,IE.SGESTADO)) > 0) AND
                                      NF.CDMODDOCFISC    = MD.CDMODDOCFISC              AND
                                      NVL(MD.CDTPREGFISC, '50')  = '50'
                                GROUP BY NF.CDFILIAL, NF.NRLANCTONF, FI.NRINSJURFILI, NF.DTENTRSAID,
                                         NF.CDMODDOCFISC, NVL(NF.CDSERIEENTR,NF.CDSERIE),
                                         DECODE(NVL(FI.IDENDEMISFIL,'P'),'I',IE.SGESTADO,EF.SGESTADO), NF.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 ) IT
                          WHERE IT.CDPRODUTO = PR.CDPRODUTO
                          ORDER BY IT.CDFILIAL, IT.NRLANCTONF, IT.NRSEQITPROD) LOOP

      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;

      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('54'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO54C.DSINICIO||V_SEQUENCIAL||C_REGISTRO54C.DSARQMAG, P_SEQMAGNET);

      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.CDPRODNCMTRANS, C_REGISTRO54C.NMPRODUTO,
                C_REGISTRO54C.SGUNIDADE, C_REGISTRO54C.CDSITUTRIBPR, C_REGISTRO54C.VRALIQIPI, C_REGISTRO54C.VRALIQICMS,
                C_REGISTRO54C.VRPERREDBCIT, 0);
      END IF;
   END LOOP;

   IF V_CONTREG > 0 THEN
      RETURN(TO_CHAR(LPAD(NVL(V_CONTGERAL,'0'),10,0))||TO_CHAR(LPAD(V_CONTADOR,10,0)));
   END IF;

   IF V_CONTREG = 0 THEN
      RETURN('0');
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_54_GIM_PB','6.39.013'); END;
/

