CREATE OR REPLACE FUNCTION ARQUIVO_70_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 ESTADO.SGESTADO%TYPE,
   P_SEQMAGNET    IN VARCHAR,
   P_CONTADOR     IN NUMBER,
   P_DTPOSICAO    IN NOTAFISCAL.DTEMISSAO%TYPE,
   P_VERSAO       IN NOTAFISCAL.CDEMPRESA%TYPE,
   P_REGISTRO     IN VARCHAR) RETURN VARCHAR
AS
   V_CONTGERAL      NUMBER;
   V_CONTREG        NUMBER;
   V_CONTADOR       NUMBER;
   V_SEQUENCIAL     VARCHAR2(03);
   V_CDARVPROD      VARCHAR2(20);
   V_CDFILIAL       VARCHAR2(04);
   V_NRLANCTONF     VARCHAR2(10);
BEGIN
   V_CONTADOR := P_CONTADOR;
   V_CONTREG  := 0;
   IF SUBSTR(P_REGISTRO,8,1) = 'S' THEN
      FOR C_REGISTRO70 IN (SELECT SUBSTR('70'          ||
                                         NRINSJURNF    ||
                                         NRINSESTFORN  ||
                                         DTENTRSAID    ||
                                         SGESTADO      ||
                                         CDMODDOCFISC  ||
                                         CDSERIEENTR   ||
                                         NRNOTAFISC    ||
                                         CDFISCOPERNF  ||
                                         VRNOTAFISC    ||
                                         VRBASECALCNF  ||
                                         VRIMPOSTONF   ||
                                         VRISENTRIB    ||
                                         VROUTRCRED    ||
                                         IDPGTOFRETE   ||
                                         IDSTATUS,1 , 126) AS DSARQMAG, DTENTRSAID
                           FROM(SELECT '70',
                                       RPAD(NVL(NF.NRINSJURNF,'00000000000000'),14,' ') AS NRINSJURNF,
                                       RPAD(NVL(REPLACE(REPLACE(REPLACE(FO.NRINSESTFORN,'.',''),',',''),'-',''),'ISENTO'),14,' ') AS NRINSESTFORN,
                                       TO_CHAR(NF.DTENTRSAID,'yyyymmdd') AS DTENTRSAID,
                                       RPAD(NVL(EF.SGESTADO,' '),2,' ') AS SGESTADO,
                                       RPAD(NVL(NF.CDMODDOCFISC,' '), 2, ' ') AS CDMODDOCFISC,
                                       RPAD(REPLACE(NVL(NF.CDSERIEENTR,' '),'-',' '),3,' ') AS CDSERIEENTR,
                                       LPAD(SUBSTR(NF.NRNOTAFISC,LENGTH(NF.NRNOTAFISC)-5,6),6,'0') AS NRNOTAFISC,
                                       RPAD(NVL(NF.CDFISCOPERNF,'0'),4,' ') AS CDFISCOPERNF,
                                       LTRIM(REPLACE(TO_CHAR(NF.VRNOTAFISC, '00000000000.00'),'.',''))   AS VRNOTAFISC,
                                       LTRIM(REPLACE(TO_CHAR(NF.VRBASECALCNF,'000000000000.00'),'.','')) AS VRBASECALCNF,
                                       LTRIM(REPLACE(TO_CHAR(NF.VRIMPOSTONF, '000000000000.00'),'.','')) AS VRIMPOSTONF,
                                       LTRIM(REPLACE(TO_CHAR(NF.VRISENTRIB,  '000000000000.00'),'.','')) AS VRISENTRIB,
                                       LTRIM(REPLACE(TO_CHAR(NF.VROUTRCRED,  '000000000000.00'),'.','')) AS VROUTRCRED,
                                       RPAD(DECODE(NVL(NF.IDPGTOFRETE, '0'), '0', '1','1','2','2','2','9','0'), 1, ' ') AS IDPGTOFRETE,
                                       DECODE(NF.IDSTATUS, 'C', 'S', 'N') AS IDSTATUS
                                FROM FORNECEDOR FO, ENDEFORN EF,
                                   (SELECT N.CDFILIAL, N.NRLANCTONF, N.NRINSJURNF, N.CDFORNECED, N.DTENTRSAID, N.CDSERIEENTR,
                                           N.NRNOTAFISC, N.VRNOTAFISC, N.IDSTATUS, N.IDPGTOFRETE, I.CDFISCOPERNF, N.CDMODDOCFISC,
                                           SUM(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0)) AS VRBASECALCNF,
                                           SUM(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0))  AS VRIMPOSTONF,
                                           SUM(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,0)) AS VRISENTRIB,
                                           SUM(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0)) AS VROUTRCRED
                                     FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I, MODDOCFISC MD, IMPOSTO IP
                                     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
                                           (I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU)  AND
                                           (IP.CDIMPOSTONFE   = P_CDIMPOSTO)     AND
                                           (IP.CDIMPOSTO      = I.CDIMPOSTO)     AND
                                           (NVL(I.IDIMPOSTONF, '1') <> '3')      AND
                                           (N.CDFILIAL   = I.CDFILIAL)           AND
                                           (N.NRLANCTONF = I.NRLANCTONF)         AND
                                           ((N.CDINSCESTAST = P_CDINSCESTAST) OR (P_CDINSCESTAST IS NULL AND N.CDINSCESTAST IS NULL)) AND
                                           (N.IDENTRSAIDOP = 'E' )               AND
                                           (N.IDSTATUS IN ('A', 'E'))            AND
                                           (N.CDMODDOCFISC    = MD.CDMODDOCFISC) AND
                                           (MD.CDTPREGFISC    = '70')
                                     GROUP BY N.CDFILIAL, N.NRLANCTONF, N.NRINSJURNF, N.CDFORNECED, N.DTENTRSAID, N.CDSERIEENTR,
                                           N.NRNOTAFISC, N.VRNOTAFISC, N.IDSTATUS, N.IDPGTOFRETE,I.CDFISCOPERNF, N.CDMODDOCFISC) NF
                                WHERE (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) )NF
                           ORDER BY DTENTRSAID) LOOP
         V_CONTADOR  := NVL(V_CONTADOR,0)+1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('70'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO70.DSARQMAG, P_SEQMAGNET);
         V_CONTREG   := V_CONTREG + 1;
         V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
      END LOOP;
   END IF;
  /************************************************ REGISTRO 74 *****************************************************/
   IF SUBSTR(P_REGISTRO,9,1) = 'S' THEN
      FOR C_REGISTRO74 IN (SELECT SUBSTR('74'||
                                  TO_CHAR(DTINVEFISCAL,'yyyymmdd')||
                                  RPAD(CDARVPROD,14, ' ')||
                                  LTRIM(REPLACE(TO_CHAR(QTESTINVFISC,'00000000000.00'),'.',''))||
                                  LTRIM(REPLACE(TO_CHAR(VRTOTINVFISC,'00000000000.00'),'.',''))||
                                  '1'||
                                  RPAD('0', 14, '0')||
                                  RPAD(' ', 14, ' ')||
                                  RPAD(' ',  2, ' ')||
                                  RPAD(' ', 45, ' '), 1, 126) AS DSARQMAG,
                                  CDARVPROD, NVL(CDCLASFISC, ' ') AS CDCLASFISC, NMPRODUTO, SGUNIDADE, QTESTINVFISC
                             FROM (SELECT SUM(I.VRTOTINVFISC) AS VRTOTINVFISC, SUM(I.QTESTINVFISC) AS QTESTINVFISC,
                                          I.DTINVEFISCAL, P.CDARVPROD, NVL(I.CDCLASFISC,P.CDCLASFISC) AS CDCLASFISC,
                                          P.NMPRODUTO, P.SGUNIDADE
                                     FROM INVENTFISC I, PRODUTO P
                                    WHERE I.CDEMPRESA    = P_CDEMPRESA
                                      AND I.CDINSCESTA   = P_CDINSCESTA
                                      AND I.DTINVEFISCAL = (SELECT MAX(I.DTINVEFISCAL)
                                                              FROM INVENTFISC I
                                                             WHERE I.CDEMPRESA    = P_CDEMPRESA
                                                               AND I.CDINSCESTA   = P_CDINSCESTA
                                                               AND I.DTINVEFISCAL <= P_DTPOSICAO)
                                      AND I.CDPRODUTO    = P.CDPRODUTO
                                      AND P.CDPRODUTO    = P.CDPRODESTO
                                    GROUP BY I.DTINVEFISCAL, P.CDARVPROD, NVL(I.CDCLASFISC,P.CDCLASFISC), P.NMPRODUTO, P.SGUNIDADE)
                            WHERE QTESTINVFISC <> 0
                              AND VRTOTINVFISC <> 0
                            ORDER BY TO_CHAR(DTINVEFISCAL,'yyyymmdd'), CDARVPROD) LOOP
         V_CONTADOR  := NVL(V_CONTADOR,0)+1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('74'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO74.DSARQMAG, P_SEQMAGNET);
         V_CONTREG   := V_CONTREG + 1;
         V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
         SELECT MAX(CDARVPROD)
         INTO V_CDARVPROD
         FROM TMP_REGISTRO_PRODUTO
         WHERE CDARVPROD = C_REGISTRO74.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_REGISTRO74.CDARVPROD, P_DTINICIAL, P_DTFINAL, C_REGISTRO74.CDCLASFISC, C_REGISTRO74.NMPRODUTO,
                   C_REGISTRO74.SGUNIDADE, '040', 0.00, 0.00, 0.00, 0);
         END IF;
      END LOOP;
   END IF;
  /************************************************ REGISTRO 75 *****************************************************/
   IF SUBSTR(P_REGISTRO,10,1) = 'S' THEN
      IF P_VERSAO = '3' THEN
         FOR C_REGISTRO75 IN (SELECT SUBSTR(CDTIPOREG ||
                                            TO_CHAR(DTINICIAL, 'YYYYMMDD')||
                                            TO_CHAR(DTFINAL  , 'YYYYMMDD')||
                                            RPAD(CDARVPROD, 14, ' ')      ||
                                            RPAD(CDCLASFISC, 8, ' ')  ||
                                            RPAD(TRIM(NMPRODUTO), 53, ' ')      ||
                                            RPAD(SGUNIDADE, 6, ' ')       ||
                                            LTRIM(REPLACE(TO_CHAR(VRALIQIPI, '000.00'),'.',''))||
                                            LTRIM(REPLACE(TO_CHAR(VRALIQICMS, '00.00'),'.',''))||
                                            LTRIM(REPLACE(TO_CHAR(VRPERREDBCIT, '000.00'),'.',''))||
                                            '0000000000000', 1, 126) AS DSARQMAG
                              FROM TMP_REGISTRO_PRODUTO
                              ORDER BY CDARVPROD) LOOP
            V_CONTADOR := NVL(V_CONTADOR,0)+1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('75'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO75.DSARQMAG, P_SEQMAGNET);
            V_CONTREG   := V_CONTREG + 1;
            V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
         END LOOP;
      ELSE
         FOR C_REGISTRO75 IN (SELECT SUBSTR(CDTIPOREG ||
                                            TO_CHAR(DTINICIAL, 'YYYYMMDD')||
                                            TO_CHAR(DTFINAL  , 'YYYYMMDD')||
                                            RPAD(CDARVPROD, 14, ' ') ||
                                            RPAD(CDCLASFISC, 8, ' ')  ||
                                            RPAD(TRIM(NMPRODUTO), 53, ' ') ||
                                            RPAD(SGUNIDADE, 6, ' ')  ||
                                            RPAD(CDSITUTRIBPR,3, ' ') ||
                                            LTRIM(REPLACE(TO_CHAR(VRALIQIPI, '00.00'),'.',''))||
                                            LTRIM(REPLACE(TO_CHAR(VRALIQICMS, '00.00'),'.',''))||
                                            LTRIM(REPLACE(TO_CHAR(VRPERREDBCIT, '00.00'),'.',''))||
                                            '0000000000000', 1, 126) AS DSARQMAG
                              FROM TMP_REGISTRO_PRODUTO
                              ORDER BY CDARVPROD) LOOP
            V_CONTADOR := NVL(V_CONTADOR,0)+1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('75'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO75.DSARQMAG, P_SEQMAGNET);
            V_CONTREG   := V_CONTREG + 1;
            V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
         END LOOP;
      END IF;
   END IF;
  /************************************************ REGISTRO 76 *****************************************************/
   IF SUBSTR(P_REGISTRO,11,1) = 'S' THEN
      FOR C_REGISTRO_76 IN (SELECT '76'||
                                    NF.NRINSJURNF||
                                    NF.NRINSEST||
                                    NF.CDMODDOCFISC||
                                    NF.CDSERIEENTR||
                                    NF.SUBSERIE||
                                    NF.NRNOTAFISC||
                                    NF.CDFISCOPERNF||
                                    NF.RECEITA||
                                    NF.DTEMISSAO||
                                    NF.SGESTADO||
                                    NF.VRTOTAL||
                                    NF.VRBASECALCNF||
                                    NF.VRICMS||
                                    NF.VRISENTA||
                                    NF.VROUTRAS||
                                    NF.VRALIQ||
                                    NF.SITUACAO AS DSARQMAG
                            FROM(SELECT RPAD(NVL(N.NRINSJURNF,'00000000000000'),14,' ') AS NRINSJURNF,
                                        RPAD(NVL(REPLACE(REPLACE(REPLACE(NRINSESTCLIE,'.',''),',',''),'-',''),'ISENTO'),14,' ') AS NRINSEST,
                                        N.CDMODDOCFISC,
                                        RPAD(REPLACE(NVL(DECODE(IDENTRSAIDOP,'S',N.CDSERIE,N.CDSERIEENTR),' '),'-',' '),2,' ') AS CDSERIEENTR,
                                        RPAD(' ',2,' ')  AS SUBSERIE,
                                        LPAD(N.NRNOTAFISC,10,'0') AS NRNOTAFISC,
                                        CDFISCOPERNF,
                                        '1' AS RECEITA,
                                        TO_CHAR(DTEMISSAO,'yyyymmdd')AS DTEMISSAO,
                                        EC.SGESTADO,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRCONTABILNF,0), '00000000000.00'),'.','')) AS VRTOTAL,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRBASECALCNF,0), '00000000000.00'),'.',''))AS VRBASECALCNF,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRIMPOSTONF,0), '0000000000.00'),'.','')) AS VRICMS,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'2',VRCONTABILNF,0), '0000000000.00'),'.','')) AS VRISENTA,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'3',VRCONTABILNF,0), '0000000000.00'),'.','')) AS VROUTRAS,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRALIQNF,0), '00'),'.','')) AS VRALIQ,
                                        DECODE(IDSTATUS, 'C', 'S', 'N') AS SITUACAO
                                   FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I,
                                        CLIENTE C, ENDECLIE EC, IMPOSTO IP
                                  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 N.IDSTATUS IN ('A', 'E')
                                    AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                    AND IP.CDIMPOSTO      = I.CDIMPOSTO
                                    AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                    AND N.CDMODDOCFISC IN ('21', '22')
                                    AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                                    AND N.CDFILIAL        = I.CDFILIAL
                                    AND N.NRLANCTONF      = I.NRLANCTONF
                                    AND N.CDCLIENTE       = C.CDCLIENTE
                                    AND C.CDCLIENTE       = EC.CDCLIENTE
                                    AND EC.IDTPENDECLIE   = 'P'
                                    AND IDORIGDESTNF      = 'C'
                                  UNION ALL
                                 SELECT RPAD(NVL(N.NRINSJURNF,'00000000000000'),14,' ') AS NRINSJURNF,
                                        RPAD(NVL(REPLACE(REPLACE(REPLACE(NRINSESTFORN,'.',''),',',''),'-',''),'ISENTO'),14,' ') AS NRINSEST,
                                        N.CDMODDOCFISC,
                                        RPAD(REPLACE(NVL(DECODE(IDENTRSAIDOP,'S',N.CDSERIE,N.CDSERIEENTR),' '),'-',' '),2,' ') AS CDSERIEENTR,
                                        RPAD(' ',2,' ') AS SUBSERIE,
                                        LPAD(N.NRNOTAFISC,10,'0') AS NRNOTAFISC,
                                        CDFISCOPERNF,
                                        '1' AS RECEITA,
                                        TO_CHAR(DTEMISSAO,'yyyymmdd')AS DTEMISSAO,
                                        EF.SGESTADO,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRCONTABILNF,0), '00000000000.00'),'.','')) AS VRTOTAL,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRBASECALCNF,0), '00000000000.00'),'.',''))AS VRBASECALCNF,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRIMPOSTONF,0), '0000000000.00'),'.','')) AS VRICMS,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'2',VRCONTABILNF,0), '0000000000.00'),'.','')) AS VRISENTA,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'3',VRCONTABILNF,0), '0000000000.00'),'.','')) AS VROUTRAS,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRALIQNF,0), '00'),'.','')) AS VRALIQ,
                                        DECODE(IDSTATUS, 'C', 'S', 'N') AS SITUACAO
                                   FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I,
                                        FORNECEDOR F, ENDEFORN EF, IMPOSTO IP
                                  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 N.IDSTATUS IN ('A', 'E')
                                    AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                    AND IP.CDIMPOSTO      = I.CDIMPOSTO
                                    AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                    AND N.CDMODDOCFISC IN ('21', '22')
                                    AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                                    AND N.CDFILIAL        = I.CDFILIAL
                                    AND N.NRLANCTONF      = I.NRLANCTONF
                                    AND N.CDFORNECED      = F.CDFORNECED
                                    AND F.CDFORNECED      = EF.CDFORNECED
                                    AND EF.IDTPENDEFORN   = 'P'
                                    AND IDORIGDESTNF      = 'F'
                                  UNION ALL
                                 SELECT RPAD(NVL(N.NRINSJURNF,'00000000000000'),14,' ') AS NRINSJURNF,
                                        RPAD(NVL(REPLACE(REPLACE(REPLACE(F.CDINSCESTA,'.',''),',',''),'-',''),'ISENTO'),14,' ') AS NRINSEST,
                                        N.CDMODDOCFISC,
                                        RPAD(REPLACE(NVL(DECODE(IDENTRSAIDOP,'S',N.CDSERIE,N.CDSERIEENTR),' '),'-',' '),2,' ') AS CDSERIEENTR,
                                        RPAD(' ',2,' ') AS SUBSERIE,
                                        LPAD(N.NRNOTAFISC,10,'0') AS NRNOTAFISC,
                                        CDFISCOPERNF,
                                        '1' AS RECEITA,
                                        TO_CHAR(DTEMISSAO,'yyyymmdd')AS DTEMISSAO,
                                        EF.SGESTADO,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRCONTABILNF,0), '00000000000.00'),'.','')) AS VRTOTAL,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRBASECALCNF,0), '00000000000.00'),'.',''))AS VRBASECALCNF,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRIMPOSTONF,0), '0000000000.00'),'.','')) AS VRICMS,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'2',VRCONTABILNF,0), '0000000000.00'),'.','')) AS VRISENTA,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'3',VRCONTABILNF,0), '0000000000.00'),'.','')) AS VROUTRAS,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRALIQNF,0), '00'),'.','')) AS VRALIQ,
                                        DECODE(IDSTATUS, 'C', 'S', 'N') AS SITUACAO
                                   FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I,
                                        FILIAL F, ENDEFILI EF, IMPOSTO IP
                                  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 N.IDSTATUS IN ('A', 'E')
                                    AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                    AND IP.CDIMPOSTO      = I.CDIMPOSTO
                                    AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                    AND N.CDMODDOCFISC IN ('21', '22')
                                    AND IDENTRSAIDOP      = 'E'
                                    AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                                    AND N.CDFILIAL        = I.CDFILIAL
                                    AND N.NRLANCTONF      = I.NRLANCTONF
                                    AND N.CDFILIRECE      = F.CDFILIAL
                                    AND F.CDFILIAL        = EF.CDFILIAL
                                    AND EF.IDTPENDEFILI   = 'P'
                                    AND IDORIGDESTNF      = 'L'
                                  UNION ALL
                                 SELECT RPAD(NVL(N.NRINSJURNF,'00000000000000'),14,' ') AS NRINSJURNF,
                                        RPAD(NVL(REPLACE(REPLACE(REPLACE(F.CDINSCESTA,'.',''),',',''),'-',''),'ISENTO'),14,' ') AS NRINSEST,
                                        N.CDMODDOCFISC,
                                        RPAD(REPLACE(NVL(DECODE(IDENTRSAIDOP,'S',N.CDSERIE,N.CDSERIEENTR),' '),'-',' '),2,' ') AS CDSERIEENTR,
                                        RPAD(' ',2,' ') AS SUBSERIE,
                                        LPAD(N.NRNOTAFISC,10,'0') AS NRNOTAFISC,
                                        CDFISCOPERNF,
                                        '1' AS RECEITA,
                                        TO_CHAR(DTEMISSAO,'yyyymmdd')AS DTEMISSAO,
                                        EF.SGESTADO,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRCONTABILNF,0), '00000000000.00'),'.','')) AS VRTOTAL,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRBASECALCNF,0), '00000000000.00'),'.',''))AS VRBASECALCNF,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRIMPOSTONF,0), '0000000000.00'),'.','')) AS VRICMS,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'2',VRCONTABILNF,0), '0000000000.00'),'.','')) AS VRISENTA,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'3',VRCONTABILNF,0), '0000000000.00'),'.','')) AS VROUTRAS,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(IP.CDIMPOSTONFE, P_CDIMPOSTO, VRALIQNF,0), '00'),'.','')) AS VRALIQ,
                                        DECODE(IDSTATUS, 'C', 'S', 'N') AS SITUACAO
                                   FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I,
                                        FILIAL F, ENDEFILI EF, IMPOSTO IP
                                  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 N.IDSTATUS IN ('A', 'E')
                                    AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                    AND IP.CDIMPOSTO      = I.CDIMPOSTO
                                    AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                    AND N.CDMODDOCFISC IN ('21', '22')
                                    AND IDENTRSAIDOP      = 'S'
                                    AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                                    AND N.CDFILIAL        = I.CDFILIAL
                                    AND N.NRLANCTONF      = I.NRLANCTONF
                                    AND N.CDFILIEMIT      = F.CDFILIAL
                                    AND F.CDFILIAL        = EF.CDFILIAL
                                    AND EF.IDTPENDEFILI   = 'P'
                                    AND IDORIGDESTNF      = 'L') NF ) LOOP
         V_CONTADOR := NVL(V_CONTADOR,0)+1;
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('76'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO_76.DSARQMAG, P_SEQMAGNET);
      END LOOP;
   END IF;
  /************************************************ REGISTRO 77 *****************************************************/
   IF SUBSTR(P_REGISTRO,12,1) = 'S' THEN
      V_CDFILIAL   := ' ';
      V_NRLANCTONF := ' ';
      DELETE FROM TMP_REGISTRO_PRODUTO;
      FOR C_REGISTRO_77 IN (SELECT '77'||
                                   NF.NRINSJURNF||
                                   NF.CDMODDOCFISC||
                                   NF.CDSERIEENTR||
                                   NF.SUBSERIE||
                                   NF.NRNOTAFISC||
                                   NF.CDFISCOPERIT||
                                   NF.RECEITA AS INICIO,
                                   NF.CDARVPROD||
                                   NF.QTITEMPROD||
                                   NF.VRCONTITEM||
                                   NF.VRDESCITEM||
                                   NF.VRBASECALCIT||
                                   NF.VRALIQITEM||
                                   NF.OPERADORA||
                                   NF.CDTERMINAL AS FINAL, CDFILIAL, NRLANCTONF,
                                   NF.CDPRODNCMTRANS, NMPRODUTO, SGUNIDADE, CDSITUTRIBPR, CDARVPROD
                            FROM(SELECT RPAD(NVL(N.NRINSJURNF,'00000000000000'),14,' ') AS NRINSJURNF,
                                        N.CDMODDOCFISC,
                                        RPAD(REPLACE(NVL(DECODE(IDENTRSAIDOP,'S',N.CDSERIE,N.CDSERIEENTR),' '),'-',' '),2,' ') AS CDSERIEENTR,
                                        RPAD(' ',2,' ')  AS SUBSERIE,
                                        LPAD(N.NRNOTAFISC,10,'0') AS NRNOTAFISC,
                                        IP.CDFISCOPERIT,
                                        '1' AS RECEITA,
                                        LPAD(NVL(IP.NRSEQITPROD,0), 3, '0') AS NRSEQITPROD,
                                        RPAD(P.CDARVPROD,11,' ') AS CDARVPROD,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.QTITEMPROD,0), '0000000000.000'),'.','')) AS QTITEMPROD,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.VRCONTITEM,0), '0000000000.00'),'.','')) AS VRCONTITEM,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.VRDESCITEM,0), '0000000000.00'),'.','')) AS VRDESCITEM,
                                        LTRIM(REPLACE(TO_CHAR(NVL(VRBASECALCIT,0), '0000000000.00'),'.','')) AS VRBASECALCIT,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'3',0,NVL(VRALIQITEM,0)),'00'),'.','')) AS VRALIQITEM,
                                        RPAD('0',14,'0') AS OPERADORA,
                                        RPAD('0',10,'0') AS CDTERMINAL, N.CDFILIAL, N.NRLANCTONF,
                                        NVL(IT.CDPRODNCMTRANS,P.CDCLASFISC) AS CDPRODNCMTRANS, P.NMPRODUTO, P.SGUNIDADE, IT.CDSITUTRIBPR
                                   FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I,
                                        ITEMPRODUTO IT, ITEMPROIMPOS IP, PRODUTO P,
                                        CLIENTE C, ENDECLIE EC, IMPOSTO II
                                  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 N.IDSTATUS IN ('A', 'E')
                                    AND II.CDIMPOSTONFE   = P_CDIMPOSTO
                                    AND II.CDIMPOSTO      = I.CDIMPOSTO
                                    AND II.CDIMPOSTO      = IP.CDIMPOSTO
                                    AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                    AND N.CDMODDOCFISC IN ('21', '22')
                                    AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                                    AND N.CDFILIAL   = I.CDFILIAL
                                    AND N.NRLANCTONF = I.NRLANCTONF
                                    AND N.CDFILIAL = IT.CDFILIAL
                                    AND N.NRLANCTONF = IT.NRLANCTONF
                                    AND N.CDFILIAL = IP.CDFILIAL
                                    AND N.NRLANCTONF = IP.NRLANCTONF
                                    AND IT.CDFILIAL = IP.CDFILIAL
                                    AND IT.NRLANCTONF = IP.NRLANCTONF
                                    AND IT.NRSEQITPROD = IP.NRSEQITPROD
                                    AND P.CDPRODUTO = IT.CDPRODUTO
                                    AND N.CDCLIENTE = C.CDCLIENTE
                                    AND C.CDCLIENTE = EC.CDCLIENTE
                                    AND IDTPENDECLIE = 'P'
                                    AND IDORIGDESTNF = 'C'
                                 UNION ALL
                                 SELECT RPAD(NVL(N.NRINSJURNF,'00000000000000'),14,' ') AS NRINSJURNF,
                                        N.CDMODDOCFISC,
                                        RPAD(REPLACE(NVL(DECODE(IDENTRSAIDOP,'S',N.CDSERIE,N.CDSERIEENTR),' '),'-',' '),2,' ') AS CDSERIEENTR,
                                        RPAD(' ',2,' ')  AS SUBSERIE,
                                        LPAD(N.NRNOTAFISC,10,'0') AS NRNOTAFISC,
                                        IP.CDFISCOPERIT,
                                        '1' AS RECEITA,
                                        LPAD(NVL(IP.NRSEQITPROD,0), 3, '0') AS NRSEQITPROD,
                                        RPAD(P.CDARVPROD,11,' ') AS CDARVPROD,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.QTITEMPROD,0), '0000000000.000'),'.','')) AS QTITEMPROD,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.VRCONTITEM,0), '0000000000.00'),'.','')) AS VRCONTITEM,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.VRDESCITEM,0), '0000000000.00'),'.','')) AS VRDESCITEM,
                                        LTRIM(REPLACE(TO_CHAR(NVL(VRBASECALCIT,0), '0000000000.00'),'.','')) AS VRBASECALCIT,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'3',0,NVL(VRALIQITEM,0)),'00'),'.','')) AS VRALIQITEM,
                                        RPAD('0',14,'0') AS OPERADORA,
                                        RPAD('0',10,'0') AS CDTERMINAL, N.CDFILIAL, N.NRLANCTONF,
                                        NVL(IT.CDPRODNCMTRANS,P.CDCLASFISC) AS CDPRODNCMTRANS, P.NMPRODUTO, P.SGUNIDADE, IT.CDSITUTRIBPR
                                   FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I,
                                        ITEMPRODUTO IT, ITEMPROIMPOS IP, PRODUTO P,
                                        FORNECEDOR F, ENDEFORN EF, IMPOSTO II
                                  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 N.IDSTATUS IN ('A', 'E')
                                    AND II.CDIMPOSTONFE   = P_CDIMPOSTO
                                    AND II.CDIMPOSTO      = I.CDIMPOSTO
                                    AND II.CDIMPOSTO      = IP.CDIMPOSTO
                                    AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                    AND N.CDMODDOCFISC IN ('21', '22')
                                    AND IDTPENDEFORN = 'P'
                                    AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                                    AND N.CDFILIAL   = I.CDFILIAL
                                    AND N.NRLANCTONF = I.NRLANCTONF
                                    AND N.CDFILIAL = IT.CDFILIAL
                                    AND N.NRLANCTONF = IT.NRLANCTONF
                                    AND N.CDFILIAL = IP.CDFILIAL
                                    AND N.NRLANCTONF = IP.NRLANCTONF
                                    AND IT.CDFILIAL = IP.CDFILIAL
                                    AND IT.NRLANCTONF = IP.NRLANCTONF
                                    AND IT.NRSEQITPROD = IP.NRSEQITPROD
                                    AND P.CDPRODUTO = IT.CDPRODUTO
                                    AND N.CDFORNECED = F.CDFORNECED
                                    AND F.CDFORNECED = EF.CDFORNECED
                                    AND IDORIGDESTNF = 'F'
                                 UNION ALL
                                 SELECT RPAD(NVL(N.NRINSJURNF,'00000000000000'),14,' ') AS NRINSJURNF,
                                        N.CDMODDOCFISC,
                                        RPAD(REPLACE(NVL(DECODE(IDENTRSAIDOP,'S',N.CDSERIE,N.CDSERIEENTR),' '),'-',' '),2,' ') AS CDSERIEENTR,
                                        RPAD(' ',2,' ')  AS SUBSERIE,
                                        LPAD(N.NRNOTAFISC,10,'0') AS NRNOTAFISC,
                                        IP.CDFISCOPERIT,
                                        '1' AS RECEITA,
                                        LPAD(NVL(IP.NRSEQITPROD,0), 3, '0') AS NRSEQITPROD,
                                        RPAD(P.CDARVPROD,11,' ') AS CDARVPROD,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.QTITEMPROD,0), '0000000000.000'),'.','')) AS QTITEMPROD,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.VRCONTITEM,0), '0000000000.00'),'.','')) AS VRCONTITEM,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.VRDESCITEM,0), '0000000000.00'),'.','')) AS VRDESCITEM,
                                        LTRIM(REPLACE(TO_CHAR(NVL(VRBASECALCIT,0), '0000000000.00'),'.','')) AS VRBASECALCIT,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'3',0,NVL(VRALIQITEM,0)),'00'),'.','')) AS VRALIQITEM,
                                        RPAD('0',14,'0') AS OPERADORA,
                                        RPAD('0',10,'0') AS CDTERMINAL, N.CDFILIAL, N.NRLANCTONF,
                                        NVL(IT.CDPRODNCMTRANS,P.CDCLASFISC) AS CDPRODNCMTRANS, P.NMPRODUTO, P.SGUNIDADE, IT.CDSITUTRIBPR
                                   FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I,
                                        ITEMPRODUTO IT, ITEMPROIMPOS IP, PRODUTO P,
                                        FILIAL F, ENDEFILI EF, IMPOSTO II
                                  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 N.IDSTATUS IN ('A', 'E')
                                    AND II.CDIMPOSTONFE   = P_CDIMPOSTO
                                    AND II.CDIMPOSTO      = I.CDIMPOSTO
                                    AND II.CDIMPOSTO      = IP.CDIMPOSTO
                                    AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                    AND N.CDMODDOCFISC IN ('21', '22')
                                    AND IDTPENDEFILI = 'P'
                                    AND IDENTRSAIDOP = 'E'
                                    AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                                    AND N.CDFILIAL   = I.CDFILIAL
                                    AND N.NRLANCTONF = I.NRLANCTONF
                                    AND N.CDFILIAL = IT.CDFILIAL
                                    AND N.NRLANCTONF = IT.NRLANCTONF
                                    AND N.CDFILIAL = IP.CDFILIAL
                                    AND N.NRLANCTONF = IP.NRLANCTONF
                                    AND IT.CDFILIAL = IP.CDFILIAL
                                    AND IT.NRLANCTONF = IP.NRLANCTONF
                                    AND IT.NRSEQITPROD = IP.NRSEQITPROD
                                    AND P.CDPRODUTO = IT.CDPRODUTO
                                    AND N.CDFILIRECE = F.CDFILIAL
                                    AND F.CDFILIAL = EF.CDFILIAL
                                    AND IDORIGDESTNF = 'L'
                                 UNION ALL
                                 SELECT RPAD(NVL(N.NRINSJURNF,'00000000000000'),14,' ') AS NRINSJURNF,
                                        N.CDMODDOCFISC,
                                        RPAD(REPLACE(NVL(DECODE(IDENTRSAIDOP,'S',N.CDSERIE,N.CDSERIEENTR),' '),'-',' '),2,' ') AS CDSERIEENTR,
                                        RPAD(' ',2,' ')  AS SUBSERIE,
                                        LPAD(N.NRNOTAFISC,10,'0') AS NRNOTAFISC,
                                        IP.CDFISCOPERIT,
                                        '1' AS RECEITA,
                                        LPAD(NVL(IP.NRSEQITPROD,0), 3, '0') AS NRSEQITPROD,
                                        RPAD(NVL(P.CDARVPROD,' '),11,' ') AS CDARVPROD,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.QTITEMPROD,0), '0000000000.000'),'.','')) AS QTITEMPROD,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.VRCONTITEM,0), '0000000000.00'),'.','')) AS VRCONTITEM,
                                        LTRIM(REPLACE(TO_CHAR(NVL(IT.VRDESCITEM,0), '0000000000.00'),'.','')) AS VRDESCITEM,
                                        LTRIM(REPLACE(TO_CHAR(NVL(VRBASECALCIT,0), '0000000000.00'),'.','')) AS VRBASECALCIT,
                                        LTRIM(REPLACE(TO_CHAR(DECODE(I.CDFISCALNF,'3',0,NVL(VRALIQITEM,0)),'00'),'.','')) AS VRALIQITEM,
                                        RPAD('0',14,'0') AS OPERADORA,
                                        RPAD('0',10,'0') AS CDTERMINAL, N.CDFILIAL, N.NRLANCTONF,
                                        NVL(IT.CDPRODNCMTRANS,P.CDCLASFISC) AS CDPRODNCMTRANS, P.NMPRODUTO, P.SGUNIDADE, IT.CDSITUTRIBPR
                                   FROM MAPAAPURAC M, NOTAFISCAL N, IMPOSTONF I,
                                        ITEMPRODUTO IT, ITEMPROIMPOS IP, PRODUTO P,
                                        FILIAL F, ENDEFILI EF, IMPOSTO II
                                  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 N.IDSTATUS IN ('A', 'E')
                                    AND II.CDIMPOSTONFE   = P_CDIMPOSTO
                                    AND II.CDIMPOSTO      = I.CDIMPOSTO 
                                    AND II.CDIMPOSTO      = IP.CDIMPOSTO
                                    AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                    AND N.CDMODDOCFISC IN ('21', '22')
                                    AND IDTPENDEFILI = 'P'
                                    AND IDENTRSAIDOP = 'S'
                                    AND I.NRSEQUMAPAPU    = M.NRSEQUMAPAPU
                                    AND N.CDFILIAL   = I.CDFILIAL
                                    AND N.NRLANCTONF = I.NRLANCTONF
                                    AND N.CDFILIAL = IT.CDFILIAL
                                    AND N.NRLANCTONF = IT.NRLANCTONF
                                    AND N.CDFILIAL = IP.CDFILIAL
                                    AND N.NRLANCTONF = IP.NRLANCTONF
                                    AND IT.CDFILIAL = IP.CDFILIAL
                                    AND IT.NRLANCTONF = IP.NRLANCTONF
                                    AND IT.NRSEQITPROD = IP.NRSEQITPROD
                                    AND P.CDPRODUTO = IT.CDPRODUTO
                                    AND N.CDFILIEMIT = F.CDFILIAL
                                    AND F.CDFILIAL = EF.CDFILIAL
                                    AND IDORIGDESTNF = 'L') NF ) LOOP
         IF ((C_REGISTRO_77.CDFILIAL||C_REGISTRO_77.NRLANCTONF) <> (V_CDFILIAL||V_NRLANCTONF)) THEN
            V_CDFILIAL   := C_REGISTRO_77.CDFILIAL;
            V_NRLANCTONF := C_REGISTRO_77.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
           ('77'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO_77.INICIO||V_SEQUENCIAL||C_REGISTRO_77.FINAL, P_SEQMAGNET);
         SELECT MAX(CDARVPROD)
         INTO V_CDARVPROD
         FROM TMP_REGISTRO_PRODUTO
         WHERE CDARVPROD = C_REGISTRO_77.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_REGISTRO_77.CDARVPROD, P_DTINICIAL, P_DTFINAL, C_REGISTRO_77.CDPRODNCMTRANS, C_REGISTRO_77.NMPRODUTO,
                   C_REGISTRO_77.SGUNIDADE, C_REGISTRO_77.CDSITUTRIBPR, 0.00, 0.00, 0.00, 0.00);
         END IF;
      END LOOP;
      IF NOT SUBSTR(P_REGISTRO,10,1) = 'S' THEN
         FOR C_REGISTRO75 IN (SELECT SUBSTR(CDTIPOREG                                                        ||
                                            TO_CHAR(DTINICIAL, 'YYYYMMDD')                                   ||
                                            TO_CHAR(DTFINAL  , 'YYYYMMDD')                                   ||
                                            RPAD(NVL(CDARVPROD,' '), 14, ' ')                                ||
                                            RPAD(NVL(CDCLASFISC,' '), 8, ' ')                                ||
                                            RPAD(TRIM(NVL(NMPRODUTO, ' ')), 53, ' ')                         ||
                                            RPAD(NVL(SGUNIDADE,' '), 6, ' ')                                 ||
                                            LTRIM(REPLACE(TO_CHAR(NVL(VRALIQIPI,'0'), '000.00'),'.',''))     ||
                                            LTRIM(REPLACE(TO_CHAR(NVL(VRALIQICMS,'0'), '00.00'),'.',''))     ||
                                            LTRIM(REPLACE(TO_CHAR(NVL(VRPERREDBCIT,'0'), '000.00'),'.',''))  ||
                                            '0000000000000', 1, 126) AS DSARQMAG
                              FROM TMP_REGISTRO_PRODUTO
                              ORDER BY CDARVPROD) LOOP
            V_CONTADOR := NVL(V_CONTADOR,0)+1;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('75'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO75.DSARQMAG, P_SEQMAGNET);
            V_CONTREG   := NVL(V_CONTREG,0) + 1;
            V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
         END LOOP;
      END IF;
   END IF;
   IF V_CONTREG > 0 THEN
      RETURN(TO_CHAR(LPAD(V_CONTGERAL,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_70_GIM_PB','6.39.013'); END;
/

