CREATE OR REPLACE FUNCTION SPED_BLOCOC_FOS(
   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_LIVROMAG     IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_PERFIL       IN NOTAFISCAL.IDSTATUS%TYPE,
   P_VERSAO       IN VARCHAR2) RETURN VARCHAR
AS
   V_CONTREGC        NUMBER;
   V_MOVIMENT        NUMBER;
   V_VL_BC_ICMS_ST   NUMBER;
   V_VL_ICMS_ST      NUMBER;
   V_VL_IP           NUMBER;
   V_TOTFORPER_S     NUMBER;
   V_TOTFORPER_E     NUMBER;
   V_VRFORAPER       NUMBER;
   V_VRFORAPER_E     NUMBER;
   V_VL_RED_BC       NUMBER;
   V_SEQUENCIAL      VARCHAR2(100);
   V_IDFRETE         VARCHAR2(1);
   V_IDSEGUR         VARCHAR2(1);
   V_IDOUTROS        VARCHAR2(1);
   V_CDIMPOSTRIB     VARCHAR2(02);
   V_IPI             VARCHAR2(02);
   V_PIS             VARCHAR2(02);
   V_COFINS          VARCHAR2(02);
   V_DTPERIODO       VARCHAR2(01);
   V_DADOS_2         VARCHAR2(10);
   V_IDSTATUS        VARCHAR2(01);
   V_ICMS            VARCHAR2(02);
   V_DTEMISSAO       DATE;
   V_DTENTRSAID      DATE;
   V_NRSEQUMAPAPU    VARCHAR2(10);
   V_PRODUTO         VARCHAR2(20);
   V_SEQUPROD        NUMBER;
   V_UNID            VARCHAR2(20);
   V_DTINICIAL       DATE;
   V_DTFINAL         DATE;
BEGIN
   V_DTINICIAL := P_DTINICIAL;
   v_DTFINAL  := P_DTFINAL;
   BEGIN
      SELECT CDIMPOSTOLIVRO
      INTO V_IPI
      FROM TPIMPOSLIVRO
      WHERE SGIMPOSTOLIVRO LIKE '%IPI'
        AND CDLIVROMAG = P_LIVROMAG;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
      V_IPI := NULL;
   END;
   BEGIN
      SELECT CDIMPOSTOLIVRO
      INTO V_PIS
      FROM TPIMPOSLIVRO
      WHERE SGIMPOSTOLIVRO LIKE '%PIS'
        AND CDLIVROMAG = P_LIVROMAG;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_PIS := NULL;
   END;
   BEGIN
      SELECT CDIMPOSTOLIVRO
      INTO V_COFINS
      FROM TPIMPOSLIVRO
      WHERE SGIMPOSTOLIVRO LIKE '%COFINS'
        AND CDLIVROMAG = P_LIVROMAG;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
      V_COFINS := NULL;
   END;
   BEGIN
      SELECT EN.IDENCARGO
      INTO V_IDFRETE
      FROM RELACENCARGO E, ENCARGO EN
      WHERE E.CDENCARGO = EN.CDENCARGO AND
            E.IDENCARGODOS LIKE '%FRET%';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDFRETE    := NULL;
   END;
   BEGIN
      SELECT EN.IDENCARGO
      INTO V_IDSEGUR
      FROM RELACENCARGO E, ENCARGO EN
      WHERE E.CDENCARGO = EN.CDENCARGO AND
            E.IDENCARGODOS LIKE '%SEGU%';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDSEGUR    := NULL;
   END;
   BEGIN
      SELECT EN.IDENCARGO
      INTO V_IDOUTROS
      FROM RELACENCARGO E, ENCARGO EN
      WHERE E.CDENCARGO = EN.CDENCARGO AND
            E.IDENCARGODOS LIKE '%OUTR%';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDOUTROS    := NULL;
   END;
   BEGIN
      SELECT CDIMPOSTOLIVRO
      INTO V_CDIMPOSTRIB
      FROM TPIMPOSLIVRO
      WHERE SGIMPOSTOLIVRO LIKE '%ICMS ST'
        AND CDLIVROMAG = P_LIVROMAG;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
      V_CDIMPOSTRIB := NULL;
   END;
   BEGIN
      SELECT NRSEQUMAPAPU
        INTO V_NRSEQUMAPAPU
        FROM MAPAAPURAC M
       WHERE M.CDEMPRESA = P_CDEMPRESA
         AND M.CDINSCESTA = P_CDINSCESTA
         AND M.CDIMPOSTO = P_CDIMPOSTO
         AND M.DTINIPERIODO = P_DTINICIAL
         AND M.DTFIMPERIODO = P_DTFINAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRSEQUMAPAPU := NULL;
   END;

   DELETE FROM TMP_REGDADOSSPED;
   COMMIT;

   FOR C_REGC100 IN(SELECT ('|C100'                                                              || '|' ||
                            NF.CAMPO2                                                            || '|' ||
                            NF.CAMPO3                                                            || '|' ||
                            DECODE(NF.IDORIGDESTNF, 'F', NF.CDFORNECED, DECODE(NF.IDORIGDESTNF, 'C', NF.CDCLIENTE , DECODE(NF.IDENTRSAIDOP, 'E', NF.CDFILIEMIT, NF.CDFILIRECE)))   || '|' ||
                            NF.CDMODDOCFISC                                                      || '|' ) AS DADOS_1,
                            (REPLACE(REPLACE(DECODE(NF.IDENTRSAIDOP, 'E', NF.CDSERIEENTR, NF.CDSERIE),'-',''),'.','')             || '|' ||
                             LTRIM(NF.NRNOTAFISC, '0')                                            || '|' ||
                             DECODE(NF.IDENTRSAIDOP, 'S' ,NRACESSONFE, '')                        || '|' ||  --chave da nota fiscal eletronica
                             DECODE(NF.IDSTATUS, 'C', '', TO_CHAR(NF.DTEMISSAO,'ddmmyyyy') )      || '|' ||
                             DECODE(NF.IDSTATUS, 'C', '', TO_CHAR(NF.DTENTRSAID,'ddmmyyyy'))      || '|' ||
                             REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRCONTABILNF,2), '999999999990.00')), '.', ',') || '|' ||
                             DECODE(NF.IDSTATUS, 'C', '', DECODE(PGTO.DTORIGVENREC, NF.DTEMISSAO, '0', '1'))                    || '|' || -- campo 13
                             DECODE(NF.IDSTATUS, 'C', '', REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_ENCARGO(NF.CDFILIAL, NF.NRLANCTONF, 'D'),2), '999999999990.00')), '.', ',')) || '|' ||
                             DECODE(NF.IDSTATUS, 'C', '', '0,00') || '|' ||  -- abatimento no tributado e nao comercial desconto ICMS
                             DECODE(NF.IDSTATUS, 'C', '', REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRPRODUTOS,2), '999999999990.00')), '.', ',')) || '|' ||
                             DECODE(NF.IDSTATUS, 'C', '', DECODE(NVL(NF.IDPGTOFRETE,'1'), '0', '1', '1', '0', '2', '1' , '9', '1'))                                               || '|' ||
                             REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUS, 'C', '', VALOR_ENCARGO(NF.CDFILIAL, NF.NRLANCTONF, V_IDFRETE)),2), '999999999990.00')), '.', ',') || '|' ||
                             REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUS, 'C', '', VALOR_ENCARGO(NF.CDFILIAL, NF.NRLANCTONF, V_IDSEGUR)),2), '999999999990.00')), '.', ',') || '|' ||
                             REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUS, 'C', '', VALOR_IMPOSTO_NF(NF.CDFILIAL, NF.NRLANCTONF,V_CDIMPOSTRIB, 'I')+VALOR_IMPOSTO_NF(NF.CDFILIAL, NF.NRLANCTONF, V_IPI, 'I')),2), '999999999990.00')), '.', ',')  || '|' || --campo20
                             REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUS, 'C', '', NF.VRBASECALCNF),2), '999999999990.00')), '.', ',') || '|' || -- campo 21
                             REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUS, 'C', '', NF.VRIMPOSTONF),2), '999999999990.00')), '.', ',') || '|' || -- campo 22
                             REPLACE(LTRIM(TO_CHAR('', '999999999990.00')), '.', ',') || '|' || -- base de calc susbt. trib
                             REPLACE(LTRIM(TO_CHAR('', '999999999990.00')), '.', ',') || '|' || -- valor do icms retido susbt. trib
                             REPLACE(LTRIM(TO_CHAR('', '999999999990.00')), '.', ',') || '|' || -- valor do IPI
                             REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUS, 'C', '', VALOR_IMPOSTO_NF(NF.CDFILIAL, NF.NRLANCTONF, V_PIS, 'I')),2), '999999999990.00')), '.', ',') || '|' || -- valor do PIS
                             REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUS, 'C', '', VALOR_IMPOSTO_NF(NF.CDFILIAL, NF.NRLANCTONF, V_COFINS, 'I')),2), '999999999990.00')), '.', ',') || '|' || -- valor do COFINS
                             DECODE(NF.IDSTATUS, 'C', '', '0,00') || '|' || -- pis retido por subst. trib.
                             DECODE(NF.IDSTATUS, 'C', '', '0,00') || '|'  ) AS DADOS_3,
                            NF.CDFILIAL AS CDFILIAL, NF.NRLANCTONF AS NRLANCTONF, NF.NRINSJURTRAN, NF.IDTPIJURTRAN, NF.VRPBRUTOVOLU, NF.VRPLIQUVOLU,
                            NF.NRPLACAVEIC, NF.DSQTDEVOLU, NF.DSESPECVOLU, NF.DSMARCAVOLU, NF.DSNUMEVOLU, NF.CDFISCOPERNF, NF.NRNOTAFISC AS NRNOTAFISC,
                            NF.DTEMISSAO, NF.DTENTRSAID, NF.IDENTRSAIDOP, NF.VRIMPOSTONF, NF.CDMODDOCFISC, NF.IDSTATUS
                    FROM (SELECT DECODE(IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                 DECODE(IDENTRSAIDOP, 'E', DECODE(IDSTATUS,'E','0','1'), '0') AS CAMPO3, NRACESSONFE,
                                 I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR,
                                 N.NRINSJURNF, N.IDSTATUS, N.DTENTRSAID, N.VRNOTAFISC, TO_CHAR(N.VRPRODUTOS) AS VRPRODUTOS, N.IDPGTOFRETE,
                                 N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,
                                 N.CDFILIEMIT, N.CDFILIRECE, N.DTEMISSAO, MAX(I.CDFISCOPERNF) AS CDFISCOPERNF, N.CDMODDOCFISC, N.CDCLIENTE, N.CDFORNECED, N.IDORIGDESTNF,
                                 TO_CHAR(SUM(I.VRCONTABILNF)) AS VRCONTABILNF, N.NRINSJURTRAN, N.IDTPIJURTRAN, N.VRPBRUTOVOLU, N.VRPLIQUVOLU,
                                 TO_CHAR(SUM(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0))) AS VRBASECALCNF,
                                 TO_CHAR(SUM(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0)))  AS VRIMPOSTONF,
                                 TO_CHAR(SUM(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0)))   AS VROUTRCRED,
                                 TO_CHAR(SUM(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,'1',DECODE(I.VRPERREDBCNF,0, 0,I.VRCONTABILNF-I.VRBASECALCNF), 0))) AS VRISENTRIB
                          FROM NOTAFISCAL N, IMPOSTONF I
                          WHERE I.NRSEQUMAPAPU    = V_NRSEQUMAPAPU
                            AND I.CDIMPOSTO       = P_CDIMPOSTO
                            AND (NVL(N.IDSISTEMA, 'DFS') <> 'REC'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'PAG'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'CMP'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'SUP'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'FIS')
                            AND N.IDENTRSAIDOP    = 'S'
                            AND NVL(I.IDIMPOSTONF, '1') <> '3'
                            AND N.CDFILIAL        = I.CDFILIAL
                            AND N.NRLANCTONF      = I.NRLANCTONF
                            AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                            AND N.CDMODDOCFISC IN ('01', '04','55')
                          GROUP BY DECODE(IDENTRSAIDOP, 'E', '0','1'), I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, N.CDSERIE,
                                 N.CDSERIEENTR, N.NRINSJURNF, N.IDSTATUS, N.DTENTRSAID, N.VRNOTAFISC, TO_CHAR(N.VRPRODUTOS), N.IDPGTOFRETE,
                                 N.DTEMISSAO, N.CDMODDOCFISC, N.CDCLIENTE, N.CDFORNECED, N.IDORIGDESTNF,
                                 N.CDFILIEMIT, N.CDFILIRECE, N.NRINSJURTRAN, N.IDTPIJURTRAN, N.VRPBRUTOVOLU, N.VRPLIQUVOLU,
                                 N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU, NRACESSONFE
                          UNION ALL
                          /******** NOTA CANCELADA *******/
                         SELECT DECODE(IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                '0' AS CAMPO3, '' AS NRACESSONFE,
                                N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR,
                                '' AS NRINSJURNF, N.IDSTATUS, N.DTENTRSAID, N.VRNOTAFISC, '' AS VRPRODUTOS, N.IDPGTOFRETE,
                                N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,
                                N.CDFILIEMIT, N.CDFILIRECE, N.DTEMISSAO, '' AS CDFISCOPERNF, N.CDMODDOCFISC, '' AS CDCLIENTE, '' AS CDFORNECED, N.IDORIGDESTNF,
                                '' AS VRCONTABILNF, N.NRINSJURTRAN, N.IDTPIJURTRAN, N.VRPBRUTOVOLU, N.VRPLIQUVOLU,
                                '' AS VRBASECALCNF,
                                '' AS VRIMPOSTONF,
                                '' AS VROUTRCRED,
                                '' AS VRISENTRIB
                           FROM NOTAFISCAL N
                          WHERE (NVL(N.IDSISTEMA, 'DFS') <> 'REC'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'PAG'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'CMP'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'SUP'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'FIS')
                            AND N.IDENTRSAIDOP = 'S'
                            AND N.IDSTATUS = 'C'
                            AND N.CDMODDOCFISC IN ('01', '04','55')
                            AND N.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL
                            AND N.CDINSCESTA = P_CDINSCESTA
                            AND N.CDEMPRESA = P_CDEMPRESA
                          GROUP BY DECODE(IDENTRSAIDOP, 'E', '0','1'), N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR,
                                   N.IDSTATUS, N.DTENTRSAID, N.VRNOTAFISC, N.IDPGTOFRETE, N.CDMODDOCFISC, N.CDCLIENTE,
                                   N.CDFORNECED, N.IDORIGDESTNF, N.CDFILIEMIT, N.CDFILIRECE, N.DTEMISSAO, N.NRINSJURTRAN, N.IDTPIJURTRAN,
                                   N.VRPBRUTOVOLU, N.VRPLIQUVOLU, N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU,
                                   N.DSNUMEVOLU, NRACESSONFE
                          UNION ALL
                          SELECT DECODE(IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                 DECODE(IDENTRSAIDOP, 'E', DECODE(IDSTATUS,'E','0','1'), '0') AS CAMPO3, NRACESSONFE,
                                 I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR,
                                 N.NRINSJURNF, N.IDSTATUS, N.DTENTRSAID, N.VRNOTAFISC, TO_CHAR(N.VRPRODUTOS) AS VRPRODUTOS, N.IDPGTOFRETE,
                                 N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,
                                 N.CDFILIEMIT, N.CDFILIRECE, N.DTEMISSAO, MAX(I.CDFISCOPERNF) AS CDFISCOPERNF, N.CDMODDOCFISC, N.CDCLIENTE, N.CDFORNECED, N.IDORIGDESTNF,
                                 TO_CHAR(SUM(I.VRCONTABILNF)) AS VRCONTABILNF, N.NRINSJURTRAN, N.IDTPIJURTRAN, N.VRPBRUTOVOLU, N.VRPLIQUVOLU,
                                 TO_CHAR(SUM(DECODE(I.CDFISCALNF,'1',I.VRBASECALCNF,0))) AS VRBASECALCNF,
                                 TO_CHAR(SUM(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0)))  AS VRIMPOSTONF,
                                 TO_CHAR(SUM(DECODE(I.CDFISCALNF,'3',I.VRCONTABILNF,0)))   AS VROUTRCRED,
                                 TO_CHAR(SUM(DECODE(I.CDFISCALNF,'2',I.VRCONTABILNF,'1',DECODE(I.VRPERREDBCNF,0, 0,I.VRCONTABILNF-I.VRBASECALCNF), 0))) AS VRISENTRIB
                          FROM NOTAFISCAL N, IMPOSTONF I
                          WHERE I.NRSEQUMAPAPU    = V_NRSEQUMAPAPU
                            AND I.CDIMPOSTO       = P_CDIMPOSTO
                            AND (NVL(N.IDSISTEMA, 'DFS')  <> 'REC'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'PAG'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'CMP'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'SUP'
                            AND NVL(N.IDSISTEMA, 'DFS')  <> 'FIS' )
                            AND N.IDENTRSAIDOP    = 'E'
                            AND NVL(I.IDIMPOSTONF, '1') <> '3'
                            AND N.CDFILIAL        = I.CDFILIAL
                            AND N.NRLANCTONF      = I.NRLANCTONF
                            AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                            AND N.CDMODDOCFISC IN ('01', '04','55')
                          GROUP BY DECODE(IDENTRSAIDOP, 'E', '0','1'), I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP,
                                 N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR, N.NRINSJURNF, N.IDSTATUS, N.DTENTRSAID, NRACESSONFE,
                                 N.VRNOTAFISC, TO_CHAR(N.VRPRODUTOS), N.IDPGTOFRETE, N.DTEMISSAO , N.CDMODDOCFISC, N.CDCLIENTE,
                                 N.CDFORNECED, N.IDORIGDESTNF, N.CDFILIEMIT, N.CDFILIRECE, N.NRINSJURTRAN, N.IDTPIJURTRAN, N.VRPBRUTOVOLU, N.VRPLIQUVOLU,
                                 N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU, NRACESSONFE) NF,
                         (SELECT MAX(C.DTORIGVENREC) DTORIGVENREC,  N.CDFILIAL, N.NRLANCTONF
                          FROM NOTAFISCAL N, CONTARECEBER C
                          WHERE N.CDFILIAL   = C.CDFILIAL   AND
                                N.NRLANCTONF = C.NRLANCTONF
                          GROUP BY N.CDFILIAL, N.NRLANCTONF) PGTO
                    WHERE NF.CDFILIAL     = PGTO.CDFILIAL(+) AND
                          NF.NRLANCTONF   = PGTO.NRLANCTONF(+)   )LOOP
      /* verifica a situacao do documento */
      IF C_REGC100.IDENTRSAIDOP = 'E' THEN
         IF C_REGC100.IDSTATUS = 'C' THEN
            /* documentos cancelados */
            V_DADOS_2 := '02|';
         ELSE
            IF NOT C_REGC100.DTENTRSAID BETWEEN P_DTINICIAL AND P_DTFINAL THEN
               /* documentos de entrada fora do periodo */
               V_DADOS_2 := '01|';
            ELSE
               V_DADOS_2 := '00|';
            END IF;
         END IF;
      ELSE
         IF C_REGC100.IDSTATUS = 'C' THEN
            /* documentos cancelados */
            V_DADOS_2 := '02|';
         ELSE
            IF NOT (C_REGC100.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL) AND NOT(C_REGC100.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL) THEN
               /* documentos de entrada fora do periodo */
               V_DADOS_2 := '01|';
            ELSE
               V_DADOS_2 := '00|';
            END IF;
         END IF;
      END IF;
      /* valor total de imposto fora do periodo - nf saida */
      V_VRFORAPER := 0;
      IF (V_DADOS_2 = '01|') AND (C_REGC100.IDENTRSAIDOP = 'S') THEN
         BEGIN
            SELECT SUM(VRIMPOSTONF)
            INTO V_VRFORAPER
            FROM IMPOSTONF
            WHERE CDFILIAL = C_REGC100.CDFILIAL
              AND NRLANCTONF = C_REGC100.NRLANCTONF;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_VRFORAPER := 0;
         END;
         V_TOTFORPER_S :=  NVL(V_TOTFORPER_S,0)+NVL(V_VRFORAPER,0);
      END IF;
      /* valor total de imposto fora do periodo - nf entrada */
      V_VRFORAPER_E := 0;
      IF (V_DADOS_2 = '01|') AND (C_REGC100.IDENTRSAIDOP = 'E') THEN
         BEGIN
            SELECT SUM(VRIMPOSTONF)
            INTO V_VRFORAPER_E
            FROM IMPOSTONF
            WHERE CDFILIAL = C_REGC100.CDFILIAL
              AND NRLANCTONF = C_REGC100.NRLANCTONF;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_VRFORAPER_E := 0;
         END;
         V_TOTFORPER_E :=  NVL(V_TOTFORPER_E,0)+NVL(V_VRFORAPER_E,0);
      END IF;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC100.DADOS_1||V_DADOS_2||C_REGC100.DADOS_3, P_NRSEQARQMAG);
      V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
      V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
      FOR C_REGC170 IN (SELECT (PR.CDARVPROD                                                                                                                                       || '|' ||
                                PR.NMPRODUTO                                                                                                                                       || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '999999999990.000')), '.', ',')                                                                      || '|' ||
                                PR.SGUNIDADE                                   												      || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(IT.VRTOTAITEM - IT.VRDESCITEM,2), '999999999990.00')), '.', ',')                                                                       || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')                                                                       || '|' ||
                                '0'                                                              || '|' ||
                                LPAD(NVL(IP.CDSITUTRIBIT,0), 3, '0')                             || '|' ||
                                IP.CDFISCOPERIT                                												      || '|' ||
                                IP.CDFISCOPERIT          || '|' || -- natureza da operacao campo 12
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRBASECALCIT, 0),2), '999999999990.00')), '.', ',')                                      || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRALIQITEM, 0),2), '999999999990.00')), '.', ',')                                        || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRIMPOSTOIT, 0),2), '999999999990.00')), '.', ',')                                        || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND('0',2), '999999999990.00')), '.', ',')   || '|' || --16
                                REPLACE(LTRIM(TO_CHAR(ROUND('0',2), '999999999990.00')), '.', ',')   || '|' || --17
                                REPLACE(LTRIM(TO_CHAR(ROUND('0',2), '999999999990.00')), '.', ',')   || '|' || --18
                                ''          	|| '|' || -- indicador de apurao do IPI CAMPO 19
                                ''         	|| '|' ||
                                '' 		|| '|' || -- Cdigo enquadramento IPI campo 21
                                REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')          || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')           || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')           || '|' ||
                                ''					      || '|' || -- cst pis   campo 25
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '50', 'B'),2), '999999999990.00')), '.', ',')           || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '50', 'A'),2), '999999999990.00')), '.', ',')           || '|' ||
                                '0,00'																	      || '|' || -- quantidade base de calculo
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '50', 'A'),2), '999999999990.0000')), '.', ',')         || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '50', 'I'),2), '999999999990.00')), '.', ',')           || '|' ||
                                ''					      || '|' || -- cst cofins campo 31
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '60', 'B'),2), '999999999990.00')), '.', ',')           || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '60', 'A'),2), '999999999990.00')), '.', ',')           || '|' ||
                                '0,00'																	      || '|' || -- quantidade base de calculo
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '60', 'A'),2), '999999999990.0000')), '.', ',')         || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '60', 'I'),2), '999999999990.00')), '.', ',')           || '|' ||
                                '' 																		      || '|'  ) AS ARQ_C170  -- codigo da conta analitica contabil deb/cred
                        FROM ITEMPRODUTO IT, PRODUTO PR, ITEMPROIMPOS IP
                        WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                          AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                          AND IP.CDIMPOSTO     = P_CDIMPOSTO
                          AND IT.CDFILIAL      = IP.CDFILIAL
                          AND IT.NRLANCTONF    = IP.NRLANCTONF
                          AND IT.NRSEQITPROD   = IP.NRSEQITPROD
                          AND IT.CDPRODUTO     = PR.CDPRODUTO
                          AND IP.NRITPROIMPOS  = '1'
                          AND C_REGC100.CDMODDOCFISC <> '55'
                        UNION ALL
                        SELECT (PR.CDARVPROD                                                                                                                                       || '|' ||
                                PR.NMPRODUTO                                                                                                                                       || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '999999999990.000')), '.', ',')                                                                      || '|' ||
                                PR.SGUNIDADE                                   												      || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(IT.VRTOTAITEM - IT.VRDESCITEM,2), '999999999990.00')), '.', ',')                                                                       || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')                                                                       || '|' ||
                                '0'                                                              || '|' ||
                                LPAD(NVL(IP.CDSITUTRIBIT,0), 3, '0')                             || '|' ||
                                IP.CDFISCOPERIT                                												      || '|' ||
                                IP.CDFISCOPERIT          || '|' || -- natureza da operacao campo 12
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRBASECALCIT, 0),2), '999999999990.00')), '.', ',')                                      || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRALIQITEM, 0),2), '999999999990.00')), '.', ',')                                        || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRIMPOSTOIT, 0),2), '999999999990.00')), '.', ',')                                        || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(NVL(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB,'A'),'0'),2), '999999999990.00')), '.', ',')   || '|' ||
                                '0,00'      	|| '|' ||
                                ''          	|| '|' || -- indicador de apurao do IPI CAMPO 19
                                ''         		|| '|' ||
                                ''           || '|' ||
                                '' 				      || '|' || -- Cdigo enquadramento IPI campo 21
                                REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')           || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')           || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')           || '|' ||
                                ''					      || '|' || -- cst pis   campo 25
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '50', 'B'),2), '999999999990.00')), '.', ',')           || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '50', 'A'),2), '999999999990.00')), '.', ',')           || '|' ||
                                '0,00'																	      || '|' || -- quantidade base de calculo
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '50', 'A'),2), '999999999990.0000')), '.', ',')         || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '50', 'I'),2), '999999999990.00')), '.', ',')           || '|' ||
                                ''					      || '|' || -- cst cofins campo 31
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '60', 'B'),2), '999999999990.00')), '.', ',')           || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '60', 'A'),2), '999999999990.00')), '.', ',')           || '|' ||
                                '0,00'																	      || '|' || -- quantidade base de calculo
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '60', 'A'),2), '999999999990.0000')), '.', ',')         || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '60', 'I'),2), '999999999990.00')), '.', ',')           || '|' ||
                                '' 																		      || '|'  ) AS ARQ_C170  -- codigo da conta analitica contabil deb/cred
                        FROM ITEMPRODUTO IT, PRODUTO PR, ITEMPROIMPOS IP
                        WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                          AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                          AND IP.CDIMPOSTO     = P_CDIMPOSTO
                          AND IT.CDFILIAL      = IP.CDFILIAL
                          AND IT.NRLANCTONF    = IP.NRLANCTONF
                          AND IT.NRSEQITPROD   = IP.NRSEQITPROD
                          AND IT.CDPRODUTO     = PR.CDPRODUTO
                          AND IP.NRITPROIMPOS  = '1'
                          AND C_REGC100.IDENTRSAIDOP = 'E'
                          AND C_REGC100.CDMODDOCFISC = '55' ) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C170'|| '|' ||V_SEQUENCIAL|| '|' || C_REGC170.ARQ_C170, P_NRSEQARQMAG);
         V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER(RTRIM(V_SEQUENCIAL))+1)), 3,'0');
         V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
      END LOOP;
      V_VL_BC_ICMS_ST := 0;
      V_VL_ICMS_ST := 0;
      V_VL_RED_BC := 0;
      V_VL_IP := 0;
      /* valores de ipi por nf */
      SELECT NVL(SUM(VRIMPOSTONF),0)
      INTO V_VL_IP
      FROM IMPOSTONF
      WHERE CDFILIAL   = C_REGC100.CDFILIAL
        AND NRLANCTONF = C_REGC100.NRLANCTONF
        AND CDIMPOSTO  = V_IPI;
      /* valores de sub. tributaria por nf */
      SELECT NVL(SUM(VRBASECALCNF),0), NVL(SUM(VRIMPOSTONF),0)
        INTO V_VL_BC_ICMS_ST, V_VL_ICMS_ST
        FROM IMPOSTONF
       WHERE CDFILIAL   = C_REGC100.CDFILIAL
         AND NRLANCTONF = C_REGC100.NRLANCTONF
         AND CDIMPOSTO  = V_CDIMPOSTRIB;
      /* valor reducao de base de calculo por nf */
      SELECT NVL(SUM(VRCONTABILNF),0)
        INTO V_VL_RED_BC
        FROM IMPOSTONF
       WHERE CDFILIAL   = C_REGC100.CDFILIAL
         AND NRLANCTONF = C_REGC100.NRLANCTONF
         AND CDIMPOSTO  = P_CDIMPOSTO
         AND IDIMPOSTONF = '2';
      IF P_VERSAO = '003' THEN         
         FOR C_REGC190 IN(SELECT ('|C190'|| '|' ||
                                  T.CST_ICMS|| '|' ||
                                  T.CFOP|| '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(T.ALIQ_ICMS,2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_OPR,2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_BC_ICMS,2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_ICMS,2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(0,2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_RED_BC,2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(0,2),'999999999990.00')), '.', ',')   || '|' ||
                                  '' || '|') ARQ_C190,
                                  T.VL_ICMS
                          FROM (SELECT CDSITUTRIBIT AS CST_ICMS,
                                       CDFISCOPERIT AS CFOP,
                                       DECODE(CDFISCALIT, '1', VRALIQITEM, 0) AS ALIQ_ICMS,
                                       SUM(VRCONTITIMP/((100-VRPERREDBCIT)/100)) AS VL_OPR,
                                       SUM(DECODE(CDFISCALIT, '1', DECODE(CDFISCALIT, '1', VRBASECALCIT, 0),0)) AS VL_BC_ICMS,
                                       SUM(DECODE(CDFISCALIT, '1', VRIMPOSTOIT, 0)) AS VL_ICMS,
                                       0 AS VL_BC_ICMS_ST,
                                       0 AS VL_ICMS_ST,
                                       SUM(VRCONTITIMP/((100-VRPERREDBCIT)/100) * (VRPERREDBCIT/100)) AS VL_RED_BC,
                                       0 AS VL_IPI,
                                       '' AS COD_OBS
                                  FROM ITEMPROIMPOS I
                                 WHERE I.CDFILIAL     = C_REGC100.CDFILIAL
                                   AND I.NRLANCTONF   = C_REGC100.NRLANCTONF
                                   AND NRITPROIMPOS <> '2'
                                   AND CDIMPOSTO = P_CDIMPOSTO
                                 GROUP BY CDSITUTRIBIT, CDFISCOPERIT, DECODE(CDFISCALIT, '1', VRALIQITEM, 0) ) T ) LOOP
            V_VL_IP := 0;
            V_VL_BC_ICMS_ST := 0;
            V_VL_ICMS_ST := 0;
            V_VL_RED_BC := 0;
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC190.ARQ_C190, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         END LOOP;
      END IF;
   END LOOP;
   FOR C_REGC300 IN(SELECT ('|C300'            || '|' ||
                            N.CDMODDOCFISC     || '|' ||
                            N.CDSERIE          || '|' ||
                            ''                 || '|' ||
                            LTRIM(NRNOTAFISC, '0')   || '|' ||
                            LTRIM(NRNOTAFISC, '0')   || '|' ||
                            TO_CHAR(N.DTEMISSAO,'ddmmyyyy')        || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(N.VRNOTAFISC,2), '999999999990.00')), '.', ',')    || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '50', 'I'),2), '999999999990.00')), '.', ',')    || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '60', 'I'),2), '999999999990.00')), '.', ',')    || '|' ||
                            ''     || '|' ) AS ARQ_C300, I.NRLANCTONF, I.CDFILIAL
                    FROM IMPOSTONF I, NOTAFISCAL N
                    WHERE I.CDIMPOSTO = P_CDIMPOSTO
                      AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                      AND N.CDMODDOCFISC = '02'
                      AND NVL(I.IDIMPOSTONF, '1') <> '3'
                      AND I.CDFILIAL = N.CDFILIAL
                      AND I.NRLANCTONF = N.NRLANCTONF
                      AND N.IDENTRSAIDOP = 'S'
                      AND P_PERFIL = 'B'
                    GROUP BY N.CDMODDOCFISC, N.CDSERIE, LTRIM(NRNOTAFISC, '0'), N.DTEMISSAO,N.VRNOTAFISC,
                             VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '50', 'I'),
                             VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '60', 'I'),
                             I.NRLANCTONF, I.CDFILIAL) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('20'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC300.ARQ_C300, P_NRSEQARQMAG);
         V_CONTREGC := NVL(V_CONTREGC,0) + 1;
      FOR C_REGC310 IN(SELECT ('|C310'       || '|' ||
                               LTRIM(N.NRNOTAFISC, '0')  || '|'  ) AS ARQ_C310
                       FROM NOTAFISCAL N
                       WHERE N.CDFILIAL = C_REGC300.CDFILIAL
                         AND N.NRLANCTONF = C_REGC300.NRLANCTONF
                         AND N.IDENTRSAIDOP = 'S'
                         AND N.IDSTATUS = 'C') LOOP
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('20'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC310.ARQ_C310, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;
      END LOOP;
      FOR C_REGC320 IN(SELECT ('|C320'         || '|' ||
                               CDSITUTRIBNF    || '|' ||
                               CDFISCOPERNF    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(VRALIQNF,2), '999999999990.00')), '.', ',')    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ABS(ROUND(SUM(VRCONTABILNF/((100-VRPERREDBCNF)/100)),2)), '999999999990.00')), '.', ',')    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VRBASECALCNF),2), '999999999990.00')), '.', ',')    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VRIMPOSTONF),2), '999999999990.00')), '.', ',')    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ABS(ROUND(SUM(VRCONTABILNF/((100-VRPERREDBCNF)/100) * (VRPERREDBCNF/100)),2)), '999999999990.00')), '.', ',')    || '|' ||
                               ''  || '|'  ) AS ARQ_C320, CDFILIAL, NRLANCTONF, CDSITUTRIBNF, CDFISCOPERNF, VRALIQNF
                       FROM IMPOSTONF
                       WHERE CDFILIAL = C_REGC300.CDFILIAL
                         AND NRLANCTONF = C_REGC300.NRLANCTONF
                         AND CDIMPOSTO  = P_CDIMPOSTO
                         AND IDIMPOSTONF <> '2' 
                       GROUP BY CDSITUTRIBNF, CDFISCOPERNF, VRALIQNF, CDFILIAL, NRLANCTONF) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('20'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC320.ARQ_C320, P_NRSEQARQMAG);
         V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         FOR C_REGC321 IN(SELECT ('|C321'         || '|' ||
                                  IT.CDPRODUTO    || '|' ||
                                  IT.QTITEMPROD   || '|' ||
                                  P.SGUNIDADE     || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VRCONTITIMP/((100-VRPERREDBCIT)/100)),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(SUM(IT.VRDESCITEM),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(SUM(I.VRBASECALCIT),2), '999999999990.00')), '.', ',')   || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(SUM(I.VRIMPOSTOIT),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '50', 'I')),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '60', 'I')),2), '999999999990.00')), '.', ',')    || '|'  ) AS ARQ_C321
                          FROM ITEMPROIMPOS I, ITEMPRODUTO IT, PRODUTO P
                          WHERE IT.CDFILIAL    = C_REGC320.CDFILIAL
                            AND IT.NRLANCTONF  = C_REGC320.NRLANCTONF
                            AND I.CDFILIAL     = IT.CDFILIAL
                            AND I.NRLANCTONF   = IT.NRLANCTONF
                            AND I.NRSEQITPROD  = IT.NRSEQITPROD
                            AND IT.CDPRODUTO   = P.CDPRODUTO
                            AND I.CDIMPOSTO    = P_CDIMPOSTO
                            AND VRALIQITEM = C_REGC320.VRALIQNF
                            AND CDSITUTRIBIT = C_REGC320.CDSITUTRIBNF
                            AND CDFISCOPERIT  = C_REGC320.CDFISCOPERNF
                          GROUP BY IT.QTITEMPROD, IT.CDPRODUTO, P.SGUNIDADE) LOOP
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('20'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC321.ARQ_C321, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         END LOOP;
      END LOOP;
   END LOOP;
   FOR C_REGC350 IN(SELECT ('|C350'            || '|' ||
                            N.CDSERIE          || '|' ||
                            ''                 || '|' ||
                            LTRIM(NRNOTAFISC, '0')         || '|' ||
                            TO_CHAR(N.DTEMISSAO,'ddmmyyyy')        || '|' ||
                            DECODE(N.CDSERIE, 'ECF', '', RPAD(DECODE(LENGTH(N.NRINSJURNF),20,SUBSTR(N.NRINSJURNF,7,14),REPLACE(REPLACE(REPLACE(REPLACE(N.NRINSJURNF, '.', ''), '/', ''), '-', ''),' ','')),14))|| '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(N.VRPRODUTOS,2), '999999999990.00')), '.', ',')    || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(N.VRNOTAFISC,2), '999999999990.00')), '.', ',')    || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(SUM(T.VRITEMENCA),2), '999999999990.00')), '.', ',')    || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '50', 'I'),2), '999999999990.00')), '.', ',')    || '|' ||
                            REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '60', 'I'),2), '999999999990.00')), '.', ',')    || '|' ||
                            ''     || '|' ) AS ARQ_C350, I.NRLANCTONF, I.CDFILIAL
                    FROM IMPOSTONF I, NOTAFISCAL N, ENCARGO E, ITEMENCARGO T
                    WHERE I.CDIMPOSTO = P_CDIMPOSTO
                      AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                      AND N.CDFILIAL = T.CDFILIAL
                      AND N.NRLANCTONF = T.NRLANCTONF
                      AND E.CDENCARGO = T.CDENCARGO
                      AND E.IDENCARGO = 'D'
                      AND N.CDMODDOCFISC = '02'
                      AND NVL(I.IDIMPOSTONF, '1') <> '3'
                      AND I.CDFILIAL = N.CDFILIAL
                      AND I.NRLANCTONF = N.NRLANCTONF
                      AND N.IDENTRSAIDOP = 'S'
                      AND P_PERFIL = 'A'
                    GROUP BY N.CDSERIE, LTRIM(NRNOTAFISC, '0'),TO_CHAR(N.DTEMISSAO,'ddmmyyyy'),
                             N.NRINSJURNF, N.VRPRODUTOS, N.VRNOTAFISC,
                             VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '50', 'I'),
                             VALOR_IMPOSTO_NF(I.CDFILIAL, I.NRLANCTONF, '60', 'I'),
                             I.NRLANCTONF, I.CDFILIAL) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('24'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC350.ARQ_C350, P_NRSEQARQMAG);
         V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         FOR C_REGC370 IN(SELECT ('|C370'                   || '|' ||
                                  SUBSTR(I.NRSEQITPROD,2,3) || '|' ||
                                  IT.CDPRODUTO              || '|' ||
                                  IT.QTITEMPROD             || '|' ||
                                  P.SGUNIDADE               || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND((I.VRCONTITIMP),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND((IT.VRDESCITEM),2), '999999999990.00')), '.', ',')    || '|' ) AS ARQ_C370
                          FROM ITEMPROIMPOS I, ITEMPRODUTO IT, PRODUTO P
                          WHERE IT.CDFILIAL    = C_REGC350.CDFILIAL
                            AND IT.NRLANCTONF  = C_REGC350.NRLANCTONF
                            AND I.CDFILIAL     = IT.CDFILIAL
                            AND I.NRLANCTONF   = IT.NRLANCTONF
                            AND I.NRSEQITPROD  = IT.NRSEQITPROD
                            AND IT.CDPRODUTO   = P.CDPRODUTO
                            AND I.CDIMPOSTO    = P_CDIMPOSTO) LOOP
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('24'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC370.ARQ_C370, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         END LOOP;
      FOR C_REGC390 IN(SELECT ('|C390'         || '|' ||
                               NF.CDSITUTRIBNF|| '|' ||
                               NF.CDFISCOPERNF    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRALIQNF,2), '999999999990.00')), '.', ',')    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRCONTABILNF,2), '999999999990.00')), '.', ',')    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRBASECALCNF,2), '999999999990.00')), '.', ',')    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRIMPOSTONF,2), '999999999990.00')), '.', ',')    || '|' ||
                               REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRREDBCALC,2), '999999999990.00')), '.', ',')    || '|' ||
                               ''  || '|'  ) AS ARQ_C390
                       FROM(SELECT NVL(CDSITUTRIBNF,'000') AS CDSITUTRIBNF,
                                   CDFISCOPERNF,
                                   VRALIQNF,
                                   SUM(VRCONTABILNF) AS VRCONTABILNF,
                                   SUM(VRBASECALCNF) AS VRBASECALCNF,
                                   SUM(VRIMPOSTONF) AS VRIMPOSTONF,
                                   SUM(DECODE(IDIMPOSTONF, '2', VRCONTABILNF, '0')) AS VRREDBCALC
                              FROM IMPOSTONF I
                             WHERE I.CDFILIAL = C_REGC350.CDFILIAL
                               AND I.NRLANCTONF = C_REGC350.NRLANCTONF
                               AND I.CDIMPOSTO  = P_CDIMPOSTO
                               AND IDIMPOSTONF <> '3'
                             GROUP BY NVL(CDSITUTRIBNF,'000'), CDFISCOPERNF, VRALIQNF) NF ) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('24'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC390.ARQ_C390, P_NRSEQARQMAG);
         V_CONTREGC := NVL(V_CONTREGC,0) + 1;
      END LOOP;
   END LOOP;
   /*  registro C400 */
   FOR C_REGC400 IN (SELECT '|C400'            || '|' ||
                            '2D'               || '|' ||
                            DSMODMARCAECF      || '|' ||
                            NRSERIEREDZ1       || '|' ||
                            CDCAIXA            || '|'   AS C400,
                            CDCAIXA, NRSERIEREDZ
                       FROM (SELECT SUBSTR(NRSERIEREDZ,1,21) AS NRSERIEREDZ1,
                                    SUBSTR(REPLACE(DSMODMARCAECF,'|','-'),1,20) AS DSMODMARCAECF,
                                    CDCAIXA, RZ.NRSERIEREDZ
                               FROM REDUCAOZ RZ ,FILIAL FI
                              WHERE RZ.DTREDUCAOZ BETWEEN V_DTINICIAL AND V_DTFINAL
                                AND RZ.CDFILIAL    = FI.CDFILIAL
                                AND FI.CDEMPRESA   = P_CDEMPRESA
                                AND FI.CDINSCESTA  = P_CDINSCESTA
                                AND NRSERIEREDZ IS NOT NULL
                              GROUP BY SUBSTR(NRSERIEREDZ,1,21), SUBSTR(REPLACE(DSMODMARCAECF,'|','-'),1,20) ,
                                       CDCAIXA, RZ.NRSERIEREDZ) )LOOP
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC400.C400, P_NRSEQARQMAG);
      V_CONTREGC := NVL(V_CONTREGC,0) + 1;
      FOR C_REGC405 IN (SELECT ('|C405'                                                                           || '|' ||
                                TO_CHAR(RZ.DTREDUCAOZ,'ddmmyyyy')                                                 || '|' ||
                                LPAD(LTRIM(NVL(RZ.NRREINIREDZ,'0'),'0'), 3, '0')                                           || '|' ||
                                LPAD(LTRIM(NVL(RZ.NRREDREDZ,'0'),'0'), 6, '0')                                             || '|' ||
                                LPAD(LTRIM(NVL(RZ.NRULTCFREDZ,'0'),'0'), 6, '0')                                           || '|' ||
                                REPLACE(LTRIM(TO_CHAR(TRUNC(RZ.VRGTREDZ,2), '999999999990.00')), '.', ',')        || '|' ||
                                REPLACE(LTRIM(TO_CHAR(TRUNC(RZ.VRVENBRUREDZ,2), '999999999990.00')), '.', ',')    || '|' ) AS C405,
                                DTREDUCAOZ
                          FROM REDUCAOZ RZ, FILIAL FI
                         WHERE RZ.CDFILIAL    = FI.CDFILIAL
                           AND RZ.DTREDUCAOZ BETWEEN V_DTINICIAL AND V_DTFINAL
                           AND FI.CDEMPRESA   = P_CDEMPRESA
                           AND FI.CDINSCESTA  = P_CDINSCESTA
                           AND RZ.CDCAIXA = C_REGC400.CDCAIXA
                           AND NRSERIEREDZ = C_REGC400.NRSERIEREDZ
                           AND VRVENBRUREDZ > 0
                         ORDER BY TO_CHAR(RZ.DTREDUCAOZ,'ddmmyyyy')) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC405.C405, P_NRSEQARQMAG);
         V_CONTREGC := NVL(V_CONTREGC,0) + 1;

         FOR C_420 IN (SELECT ('|C420'          || '|' ||
                                 VRALIQIMPRZ    || '|' ||
                                 VRBCALCIMPRZ   || '|' ||
                                 ''             || '|' ||               -- NR_TOT
                                 ''             || '|'  ) AS C420,      -- DESCR_NR_TOT
                                 VRALIQIMPRZ,
                                 DTREDUCAOZ
                            FROM (SELECT IDTPIMPOSFIS||RPAD(REPLACE(TO_CHAR(NVL(VRALIQIMPRZ,0)),',',''),4,'0') AS VRALIQIMPRZ,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(SUM(VRBCALCIMPRZ),2), '999999999990.00')), '.', ',') AS VRBCALCIMPRZ,
                                         DTREDUCAOZ
                                    FROM IMPOSTOREDZ R, IMPOSTO I
                                   WHERE R.CDCAIXA = C_REGC400.CDCAIXA
                                     AND R.DTREDUCAOZ = C_REGC405.DTREDUCAOZ
                                     AND R.CDIMPOSTO = I.CDIMPOSTO
                                   GROUP BY DTREDUCAOZ, IDTPIMPOSFIS||RPAD(REPLACE(TO_CHAR(NVL(VRALIQIMPRZ,0)),',',''),4,'0') )) LOOP
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_420.C420, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;
         END LOOP;
         FOR C_460 IN (SELECT '|C460'    || '|' ||
                           COD_MOD    || '|' ||
                           COD_SIT    || '|' ||
                           NUM_DOC    || '|' ||
                           DT_DOC     || '|' ||
                           REPLACE(LTRIM(TO_CHAR(TRUNC(VL_DOC,2), '999999999990.00')), '.', ',')|| '|' ||
                           VL_PIS     || '|' ||
                           VL_COFINS  || '|' ||
                           CPF_CNPJ   || '|' ||
                           NOM_ADQ    || '|'  AS C460,
                           IDSITUVENDA, NRSEQVENDA
                      FROM(SELECT '2D' AS COD_MOD,
                                  DECODE(V.IDSITUVENDA, 'O', '00', '01') AS COD_SIT,
                                  LTRIM(NRCUPOMFIS, '0') AS NUM_DOC,
                                  DECODE(V.IDSITUVENDA, 'O', TO_CHAR(DTENTRVENDA,'ddmmyyyy'), '') AS DT_DOC ,
                                  SUM(DECODE(V.IDSITUVENDA, 'O', REPLACE(LTRIM(TO_CHAR(TRUNC((QTPRODVEND * (VRUNITVEND + NVL(VRUNITVENDCL,0))) - VRDESITVEND + VRACRITVEND,2), '999999999990.00')), '.', ','),'')) AS VL_DOC,
                                  '0,00' AS VL_PIS,
                                  '0,00' AS VL_COFINS,
                                  DECODE(V.IDSITUVENDA, 'O', NRINSCRCONS, '') AS CPF_CNPJ,
                                  DECODE(V.IDSITUVENDA, 'O', NMCONSVEND, '') AS NOM_ADQ,
                                  V.NRSEQVENDA, V.IDSITUVENDA
                             FROM VENDA V, ITEMVENDA I
                            WHERE V.DTENTRVENDA  = C_REGC405.DTREDUCAOZ
                              AND V.IDSITUVENDA IN ('O', 'C')
                              AND V.CDCAIXA = C_REGC400.CDCAIXA
                              AND V.NRCUPOMFIS IS NOT NULL
                              AND V.CDFILIAL = I.CDFILIAL
                              AND V.CDCAIXA = I.CDCAIXA
                              AND VRUNITVEND > 0
                              AND V.NRSEQVENDA = I.NRSEQVENDA
                            GROUP BY DECODE(V.IDSITUVENDA, 'O', '00', '01'), LTRIM(NRCUPOMFIS, '0'),
                                     DECODE(V.IDSITUVENDA, 'O', TO_CHAR(DTENTRVENDA,'ddmmyyyy'), ''),
                                     DECODE(V.IDSITUVENDA, 'O', NRINSCRCONS, ''), DECODE(V.IDSITUVENDA, 'O', NMCONSVEND, ''),
                                     V.NRSEQVENDA, V.IDSITUVENDA) ) LOOP
--RAISE_APPLICATION_ERROR(-20501, TO_CHAR(C_REGC405.DTREDUCAOZ), TRUE);
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_460.C460, P_NRSEQARQMAG);
            V_CONTREGC := NVL(V_CONTREGC,0) + 1;

            FOR C_470 IN (SELECT REG        || '|' ||
                                 COD_ITEM   || '|' ||
                                 QTD        || '|' ||
                                 QTD_CANC   || '|' ||
                                 UNID       || '|' ||
                                 VL_ITEM    || '|' ||
                                 CST_ICMS   || '|' ||
                                 CFOP       || '|' ||
                                 ALIQ_ICMS  || '|' ||
                                 VL_PIS     || '|' ||
                                 VL_COFINS  || '|' AS C470,
                                 CDPRODUTO, UNID
                            FROM (SELECT '|C470' AS REG,
                                         CDARVPROD AS COD_ITEM,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(DECODE(C_460.IDSITUVENDA, 'O', QTPRODVEND, 0),2), '999999999990.00')), '.', ',') AS QTD,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(DECODE(C_460.IDSITUVENDA, 'C', QTPRODVEND, 0),2), '999999999990.00')), '.', ',') AS QTD_CANC,
                                         SGUNIDADE AS UNID,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC((QTPRODVEND * (VRUNITVEND + NVL(VRUNITVENDCL,0))) - VRDESITVEND + VRACRITVEND,2), '999999999990.00')), '.', ',') AS VL_ITEM,
                                         '000'  AS CST_ICMS,
                                         '5102' AS CFOP,
                                         REPLACE(LTRIM(TO_CHAR(TRUNC(IT.VRPEALPRODIT,2), '999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                         '0,00' AS VL_PIS,
                                         '0,00' AS VL_COFINS, P.CDPRODUTO
                                    FROM ITEMVENDA I, PRODUTO P, ITVENDAIMPOS IT
                                   WHERE I.NRSEQVENDA = C_460.NRSEQVENDA
                                     AND I.CDPRODUTO = P.CDPRODUTO
                                     AND IT.CDFILIAL = I.CDFILIAL
                                     AND IT.CDCAIXA = I.CDCAIXA
                                     AND IT.NRSEQVENDA = I.NRSEQVENDA
                                     AND IT.NRSEQUITVEND = I.NRSEQUITVEND) ) LOOP
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_470.C470, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;

              /* UNIDADE DE MEDIDA */
              SELECT MAX(CDCHAVEREG)
                 INTO V_UNID
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_470.UNID
                  AND CDREGISTRO = '0190';


               IF V_UNID IS NULL THEN
                  --RAISE_APPLICATION_ERROR(-20501, C_470.UNID, TRUE);
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                  VALUES
                    ('0190', V_SEQUPROD, C_470.UNID);
               END IF;

               /* CODIGO DO PRODUTO */
               SELECT MAX(CDCHAVEREG)
                 INTO V_PRODUTO
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_470.CDPRODUTO
                  AND CDREGISTRO = '0200';

               IF V_PRODUTO IS NULL THEN

                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                  VALUES
                    ('0200', V_SEQUPROD, C_470.CDPRODUTO);

               END IF;
            END LOOP;
         END LOOP;
      END LOOP;
   END LOOP;
   IF V_CONTREGC <> 0 THEN
      V_MOVIMENT := 0;
   ELSE
      V_MOVIMENT := 1;
   END IF;
   /* ABERTURA DO REGISTRO C */
   V_CONTREGC := NVL(V_CONTREGC,0) + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     ('18'||LPAD('0', 8, '0'), '|C001'|| '|' ||V_MOVIMENT|| '|', P_NRSEQARQMAG);
   /* ENCERRAMENTO DO BLOCO C */
   V_CONTREGC := NVL(V_CONTREGC,0) + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     ('40'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C990'|| '|' ||V_CONTREGC|| '|', P_NRSEQARQMAG);
   RETURN(LPAD(TO_CHAR(NVL(V_CONTREGC,0)),10, '0')||LPAD(TO_CHAR(NVL(V_TOTFORPER_S,0)),10,'0')||LPAD(TO_CHAR(NVL(V_VRFORAPER_E,0)),10,'0'));
END;
/

/*
            FOR C_425 IN (SELECT DISTINCT('|C425'        || '|' ||
                                 CDARVPROD       || '|' ||
                                 QTPRODVEND      || '|' ||
                                 SGUNIDADE       || '|' ||
                                 VRITEM          || '|' ||
                                 VRPIS           || '|' ||
                                 VRCOFINS        || '|'  ) AS C425
                            FROM (SELECT CDARVPROD,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(QTPRODVEND,2), '999999999990.000')), '.', ',')  AS QTPRODVEND,
                                         SGUNIDADE,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(QTPRODVEND * VRUNITVEND,2), '999999999990.00')), '.', ',')  AS VRITEM,
                                         '0,00' AS VRPIS,
                                         '0,00' AS VRCOFINS
                                    FROM PRODUTO P,
                                         (SELECT DISTINCT R.DTREDUCAOZ
                                           FROM REDUCAOZ R, IMPOSTOREDZ IR, IMPOSTO I
                                          WHERE IDTPIMPOSFIS||RPAD(REPLACE(TO_CHAR(NVL(IR.VRALIQIMPRZ,0)),',',''),4,'0') = C_420.VRALIQIMPRZ
                                            AND R.DTREDUCAOZ  BETWEEN P_DTINICIAL AND P_DTFINAL
                                            AND R.CDCAIXA = C_REGC400.CDCAIXA
                                            AND R.CDFILIAL = IR.CDFILIAL
                                            AND R.CDCAIXA = IR.CDCAIXA
                                            AND R.NRECFREDZ = IR.NRECFREDZ
                                            AND R.DTREDUCAOZ = IR.DTREDUCAOZ
                                            AND I.CDIMPOSTO = IR.CDIMPOSTO ) R,
                                         (SELECT V.CDFILIAL, V.CDCAIXA, V.NRSEQVENDA, I.CDPRODUTO, V.DTENTRVENDA,
                                                 NVL(I.VRUNITVEND,0) AS VRUNITVEND,
                                                 NVL(I.QTPRODVEND,0) AS QTPRODVEND
                                            FROM VENDA V, ITEMVENDA I
                                           WHERE V.DTENTRVENDA  BETWEEN P_DTINICIAL AND P_DTFINAL
                                             AND V.IDSITUVENDA = 'O'
                                             AND V.CDCAIXA = C_REGC400.CDCAIXA
                                             AND V.NRCUPOMFIS IS NOT NULL
                                             AND V.CDFILIAL = I.CDFILIAL
                                             AND V.CDCAIXA = I.CDCAIXA
                                             AND V.NRSEQVENDA = I.NRSEQVENDA ) I
                                   WHERE R.DTREDUCAOZ = I.DTENTRVENDA
                                     AND P.CDPRODUTO = I.CDPRODUTO) ) LOOP
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('25'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_425.C425, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;
            END LOOP;
            */

-- VERSAO --
BEGIN ATUVEROBJ('SPED_BLOCOC_FOS','5.11.000'); END;
/
