CREATE OR REPLACE FUNCTION SPED_BLOCOC(
   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_CDLIVROMAGADIC  IN LIVROMAGNETICO.CDLIVROMAG%TYPE,
   P_NRSEQARQMAG     IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_PERFIL          IN NOTAFISCAL.IDSTATUS%TYPE,
   P_VERSAO          IN VARCHAR2,
   P_IDDIFALIQ       IN VARCHAR2,
   P_TOTALIZA        IN NUMBER,
   P_IDINFCOMPLST    IN DADOSGERSPED.IDINFCOMPLST%TYPE,
   P_NRDIAMEDIAEST   IN DADOSGERSPED.NRDIAMEDIAEST%TYPE,
   P_IDGERACAMPO11ST IN DADOSGERSPED.IDGERACAMPO11ST%TYPE,
   P_ATIVIDADE       IN VARCHAR2,
   P_IDCONSPRODEST   IN DADOSGERSPED.IDCONSPRODEST%TYPE,
   P_GERAFORSALE     IN VARCHAR2 DEFAULT 'N') RETURN VARCHAR
AS
   V_CONTREGC        NUMBER;
   V_SEQADIC         NUMBER;
   V_MOVIMENT        NUMBER;
   V_TOTFORPER_S     NUMBER;
   V_TOTFORPER_E     NUMBER;
   V_VRFORAPER       NUMBER;
   V_VRFORAPER_E     NUMBER;
   V_SEQUPROD        NUMBER;
   V_SEQUENCIAL      VARCHAR2(100);
   V_IDFRETE         VARCHAR2(01);
   V_IDSEGUR         VARCHAR2(01);
   V_IDOUTROS        VARCHAR2(01);
   V_IDDESCONTO      VARCHAR2(01);
   V_PIS             VARCHAR2(02);
   V_COFINS          VARCHAR2(02);
   V_IPI             VARCHAR2(02);
   V_CDIMPOSTRIB     VARCHAR2(02);
   V_DTPERIODO       VARCHAR2(01);
   V_DATA            VARCHAR2(10);
   V_IDSTATUS        VARCHAR2(01);
   V_ICMS            VARCHAR2(02);
   TIPOOPER_COMPL_S  VARCHAR(02);
   TIPOOPER_COMPL_E  VARCHAR(02);
   V_DTEMISSAO       DATE;
   V_DTENTRSAID      DATE;
   V_NRSEQUMAPAPU    VARCHAR2(10);
   V_COD_NAT         VARCHAR2(100);
   V_COD_PAR         VARCHAR2(100);
   V_UNID            VARCHAR2(100);
   V_PRODUTO         VARCHAR2(100);
   V_VRDEBESP        NUMBER;
   V_LINHA           VARCHAR2(255);
   V_NR_TOT          VARCHAR2(02);
   V_DESCR_NR_TOT    VARCHAR2(255);
   V_REG_C197        VARCHAR2(1000);
   V_COD_OBS         VARCHAR2(1000);
   V_SGESTADO        VARCHAR2(02);
   V_CDAJUSTEC195    VARCHAR2(10);
   V_IDGERREGC197    VARCHAR2(10);
   V_CDSITUCRT       VARCHAR2(01);
   V_VENDABRUTA      FLOAT;
   V_VENDACANC       FLOAT;
   V_IDDIFEALIQUOTA  VARCHAR2(02);
   V_CFOPC195        VARCHAR2(01);
   V_IDANTICMSARROZ  VARCHAR2(01);
   V_CDTPOPANTARROZ  VARCHAR2(03);
   V_CDAJUSTEXTEMP   VARCHAR2(10);
   V_IDTPCARGATRIB   VARCHAR2(01);
   V_CODC195         VARCHAR2(06);
   V_TXTC195         VARCHAR2(500);
   V_TIPOC113        VARCHAR2(02);
   V_C195            VARCHAR2(06);
   V_NRSEQITEM       NUMBER;
   V_IDATUESTTPOP    VARCHAR2(01);
   V_CONTCTBL        VARCHAR2(100);
   V_UNIDADE         VARCHAR2(100);
   V_PARTICIPANTE    VARCHAR2(50);
   V_SEQITEM         NUMBER;
   V_C595            VARCHAR2(06);
   V_CODC595         VARCHAR2(06);
   V_TXTC595         VARCHAR2(500);
   V_REG_C597        VARCHAR2(1000);
   V_IDALIQCARGASPED VARCHAR2(01);
   V_IDSOMAFECP      VARCHAR2(01);
   V_IDSOMAFECPST    VARCHAR2(01);
   V_GERAC116        VARCHAR2(01);
   V_CDTPOPCREDSTRS  VARCHAR2(03);
   V_CDAJCREDSTRS    VARCHAR2(10);
BEGIN
   V_SEQUPROD := NVL(P_TOTALIZA,0);

   /*VERIFICA O TRATAMENTO DO FECP NAS SAIDAS, SE SERA SOMADO AO VALOR DO IMPOSTO OU NAO. NAS ENTRADAS ELE SEMPRE E CONSIDERADO*/
   BEGIN
      SELECT IDSOMAFECP
        INTO V_IDSOMAFECP
        FROM AJUSTEFECP
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND CDIMPOSTO    = '00';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDSOMAFECP := 'S';
   END;
   V_IDSOMAFECP := NVL(V_IDSOMAFECP,'S');

   /*VERIFICA O TRATAMENTO DO FECP PARA O ICMS ST NAS SAIDAS, SE SERA SOMADO AO VALOR DO IMPOSTO OU NAO. NAS ENTRADAS ELE SEMPRE E CONSIDERADO*/
   BEGIN
      SELECT IDSOMAFECP
        INTO V_IDSOMAFECPST
        FROM AJUSTEFECP
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND CDIMPOSTO    = '80';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDSOMAFECPST := 'S';
   END;
   V_IDSOMAFECPST := NVL(V_IDSOMAFECPST,'S');

   /*VERIFICA O TIPO DE OPERACAO UTILIZADO PARA ADJUCACAO DO ICMS ST*/
   BEGIN
      SELECT P.IDESCREDSTRS||P.CDTPOPCREDSTRS, P.CDAJCREDSTRS
        INTO V_CDTPOPCREDSTRS, V_CDAJCREDSTRS
        FROM PARAMESTSPED P
       WHERE P.CDEMPRESA  = P_CDEMPRESA
         AND P.CDINSCESTA = P_CDINSCESTA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDTPOPCREDSTRS := '';
         V_CDAJCREDSTRS   := '';
   END;

   /*Busca o tipo de operacao de complemento */
   BEGIN
      SELECT MAX(TIPOOPER_S), MAX(TIPOOPER_E)
        INTO TIPOOPER_COMPL_S, TIPOOPER_COMPL_E
        FROM (SELECT DECODE(IDENTRSAIDOP,'S',CDTIPOOPER,NULL) AS TIPOOPER_S,
                     DECODE(IDENTRSAIDOP,'E',CDTIPOOPER,NULL) AS TIPOOPER_E
                FROM TIPOOPERAC
               WHERE UPPER(NMTIPOOPER) LIKE '%NF COMPLEMENTO%'
                 AND CDTIPOOPER <> '03');
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         TIPOOPER_COMPL_S := NULL;
         TIPOOPER_COMPL_E := NULL;
   END;

   BEGIN
      SELECT MAX(EN.IDENCARGO)
        INTO V_IDFRETE
        FROM ENCARGO EN
       WHERE EN.CDENCARGONFE = '01';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
           V_IDFRETE     := NULL;
   END;

   BEGIN
      SELECT MAX(EN.IDENCARGO)
        INTO V_IDSEGUR
        FROM ENCARGO EN
       WHERE EN.CDENCARGONFE = '02';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
           V_IDSEGUR     := NULL;
   END;

   BEGIN
      SELECT MAX(EN.IDENCARGO)
        INTO V_IDDESCONTO
        FROM ENCARGO EN
       WHERE EN.CDENCARGONFE = '03';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
           V_IDDESCONTO  := NULL;
   END;

   BEGIN
      SELECT MAX(EN.IDENCARGO)
        INTO V_IDOUTROS
        FROM ENCARGO EN
       WHERE EN.CDENCARGONFE = '04';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
           V_IDOUTROS    := NULL;
   END;

   V_IPI         := '10';
   V_PIS         := '50';
   V_COFINS      := '60';
   V_CDIMPOSTRIB := '80';

   /*VERIFICA SE A EMPRESA EDO SIMPLES NACIONAL, 1 - SIMPLES NACIONAL, 2 - SIMPLES COM EXCESSO DE SUBLIMITE, 3 - REGIME NORMAL*/
   SELECT NVL(MAX(CDSITUCRT), '3') CDSITUCRT
     INTO V_CDSITUCRT
     FROM PARAMEMPRESA
    WHERE CDEMPRESA = P_CDEMPRESA;

   IF V_CDSITUCRT IN ('2','3') THEN
      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;
   END IF;

   IF V_CDSITUCRT = '1' THEN
      BEGIN
         SELECT NRSEQUMAPAPU
           INTO V_NRSEQUMAPAPU
           FROM MAPAAPURACSN M
          WHERE M.CDEMPRESA    = P_CDEMPRESA
            AND M.DTINIPERIODO = P_DTINICIAL
            AND M.DTFIMPERIODO = P_DTFINAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_NRSEQUMAPAPU := NULL;
      END;
   END IF;

   /*ESTADO DA IE*/
   SELECT MAX(SGESTADO)
     INTO V_SGESTADO
     FROM INSCRESTAD
    WHERE CDEMPRESA  = P_CDEMPRESA
      AND CDINSCESTA = P_CDINSCESTA;

   V_IDTPCARGATRIB := 'N';
   /*VERIFICA SE EXISTE CARGA TRIBUTARIA GERADA COMO OUTROS DEBITOS/CREDITOS,
   CASO CONTRARIO CONSIDERA COMO SE FOSSE DEBITO/CREDITO NORMAL*/
   BEGIN
      SELECT MAX('O') AS IDTPCARGATRIB
        INTO V_IDTPCARGATRIB
        FROM ITMAPARECUP
       WHERE NRSEQUMAPAPU = V_NRSEQUMAPAPU
         AND IDTIPOITMAPR IN ('OC','OD')
         AND IDCARGATRIBUTARIA = 'S';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDTPCARGATRIB := 'N';
   END;

   V_IDALIQCARGASPED := 'N';
   BEGIN
      SELECT IDEXIBECARGASPED
        INTO V_IDALIQCARGASPED
        FROM PARAMFISCAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDALIQCARGASPED := 'N';
   END;

   V_SEQADIC := 000001; --codigo da informacao complementar, registro C110.

   IF V_CDSITUCRT IN ('2','3') THEN
      FOR C_REGC100 IN(SELECT '|C100'                                                              || '|' ||
                              NF.CAMPO2                                                            || '|' ||
                              (CASE WHEN ((NF.CDSERIEENTR BETWEEN '890' AND '899') AND (NF.CDMODDOCFISC = '55')) THEN '1' ELSE NF.CAMPO3 END) || '|' AS DADOS_1,
                              --DECODE(NF.IDSTATUSNFE,'D','',DECODE(NF.IDSTATUS,'C','',REPLACE(REPLACE(REPLACE(REPLACE(NF.IDORIGDESTNF||DECODE(NF.IDORIGDESTNF, 'F', NF.CDFORNECED, DECODE(NF.IDORIGDESTNF, 'C', NF.CDCLIENTE , DECODE(NF.IDENTRSAIDOP, 'E', NF.CDFILIEMIT, NF.CDFILIRECE))),'.'),'-'),'/'),' '))) || '|' ||
                              NF.CDMODDOCFISC                                                      || '|' ||
                              DECODE(NF.IDSTATUSNFE, 'D', '04', DECODE(NF.IDSTATUSNFE, 'I', '05',DECODE(IDSTATUS,'C','02',DECODE(NF.IDREGESPECIAL,'S','08', DECODE(NF.IDENTRSAIDOP,'S', (CASE WHEN DTEMISSAO < P_DTINICIAL THEN DECODE(NF.CDTIPOOPER,'03','07',TIPOOPER_COMPL_S,'07','01') ELSE DECODE(NF.CDTIPOOPER,'03','06',TIPOOPER_COMPL_S,'06','00') END),(CASE WHEN ((NF.CDSERIEENTR BETWEEN '890' AND '899') AND (NF.CDMODDOCFISC = '55')) THEN '08'ELSE (CASE WHEN DTENTRSAID < P_DTINICIAL THEN DECODE(NF.CDTIPOOPER,'03','07',TIPOOPER_COMPL_E,'07','01')ELSE DECODE(NF.CDTIPOOPER,'03','06',TIPOOPER_COMPL_E,'06','00') END) END))))))||'|'||
                              CASE WHEN LENGTH(NF.NRACESSONFE) = '44' THEN SUBSTR(NRACESSONFE,23,3) ELSE REPLACE(REPLACE(DECODE(NF.IDENTRSAIDOP, 'E', NF.CDSERIEENTR, NF.CDSERIE),'-',''),'.','') END  || '|' ||
                              DECODE(NF.IDENTRSAIDOP,'E',LTRIM(NF.NRNOTAFISC,'0'), DECODE(NF.NRACESSONFE, '', LTRIM(REPLACE(LTRIM(NF.NRNOTAFISC, '0'),' ')),DECODE(NF.IDSTATUS,'C',LTRIM(NF.NRNOTAFISC,'0'),LTRIM(NF.NRNOTAFISCALELE, '0')) )  )|| '|' ||
                              DECODE(NF.CDMODDOCFISC,'04','',NRACESSONFE)                               || '|' ||  --chave da nota fiscal eletronica
                              DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',TO_CHAR(NF.DTEMISSAO,'ddmmyyyy'))) )      || '|' AS DADOS_2,
                              REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRCONTABILNF,2), '999999999990.00')), '.', ',') || '|' ||
                              DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',DECODE(NVL(NF.QTDEPARC,0),0,'2',1,'0','1'))))    || '|' || -- campo 13
                              DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',REPLACE(LTRIM(TO_CHAR((SELECT SUM(ROUND(NVL(VRENCARGODESC,0),2)) FROM ITEMPRODUTO WHERE CDFILIAL = NF.CDFILIAL AND NRLANCTONF = NF.NRLANCTONF AND NRSEQITPROD IS NOT NULL), '999999999990.00')), '.', ',')))) || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','', NVL(NF.VRIMPDESONERADO,0)))),2), '999999999990.00')), '.', ',') || '|' ||  -- abatimento no tributado e nao comercial desconto ICMS
                              DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRPRODUTOS,2)+NVL((SELECT SUM(ROUND(NVL(VRDESCITEM,0),2) + ROUND(NVL(VRENCARGOIMPXML,0),2)) FROM ITEMPRODUTO WHERE CDFILIAL = NF.CDFILIAL AND NRLANCTONF = NF.NRLANCTONF AND NRSEQITPROD IS NOT NULL),0), '999999999990.00')), '.', ',')))) || '|' ||
                              DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',RPAD(NVL(NF.IDPGTOFRETE, '9'), 1, ' '))))                                       || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',VALOR_ENCARGO_NFE(NF.CDFILIAL, NF.NRLANCTONF, V_IDFRETE,  '01')))),2), '999999999990.00')), '.', ',') || '|' ||
                              REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',VALOR_ENCARGO_NFE(NF.CDFILIAL, NF.NRLANCTONF, V_IDSEGUR,  '02')))),2), '999999999990.00')), '.', ',') || '|' ||
                              /*REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUS, 'C', '', ABS(VALOR_IMPOSTO_NF(NF.CDFILIAL, NF.NRLANCTONF,V_CDIMPOSTRIB, 'I','1')) + ABS(VALOR_IMPOSTO_NF(NF.CDFILIAL, NF.NRLANCTONF, V_IPI, 'I','1'))),2), '999999999990.00')), '.', ',')  || '|' || --campo20*/
                              /*REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',VALOR_ENCARGO_NFE(NF.CDFILIAL, NF.NRLANCTONF, V_IDOUTROS, '04')))),2), '999999999990.00')), '.', ',') || '|' || -- Campo 20*/
                              DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',REPLACE(LTRIM(TO_CHAR(ROUND((SELECT NVL(SUM(I.VRITEMENCA),0)
                                                                                                                                                       FROM ITEMENCARGO I, ENCARGO E
                                                                                                                                                      WHERE I.CDFILIAL     = NF.CDFILIAL
                                                                                                                                                        AND I.NRLANCTONF   = NF.NRLANCTONF
                                                                                                                                                        AND E.CDENCARGO    = I.CDENCARGO
                                                                                                                                                        AND E.IDENCARGO    = 'A'
                                                                                                                                                        AND E.CDENCARGONFE = '04'
                                                                                                                                                        AND I.CDENCARGO NOT IN (SELECT NVL(I.CDENCARGOIMPXML,'XX') AS CDENCARGOIMPXML
                                                                                                                                                                                  FROM IMPOSTO I
                                                                                                                                                                                 WHERE I.CDIMPOSTONFE IN ('80','10'))),2), '999999999990.00')), '.', ',')))) || '|' || -- Campo 20 outras obrigacoes acessorias
                              REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',NF.VRBASECALCNF))),2), '999999999990.00')), '.', ',') || '|' || -- campo 21
                              REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',NF.VRIMPOSTONF))),2), '999999999990.00')), '.', ',') || '|' || -- campo 22
                              REPLACE(LTRIM(TO_CHAR(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS,'C','', DECODE(NF.IDSTATUSNFE,'I','',VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF,'80','B','1')))), '999999999990.00')), '.', ',') || '|' || -- base de calc susbt. trib
                              REPLACE(LTRIM(TO_CHAR(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS,'C','', DECODE(NF.IDSTATUSNFE,'I','',VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF,'80','I','1') + CASE WHEN V_IDSOMAFECPST = 'S' OR NF.IDENTRSAIDOP = 'E' THEN VALOR_FECP_NFE(NF.CDFILIAL, NF.NRLANCTONF,'80','I','1') ELSE 0 END))), '999999999990.00')), '.', ',') || '|' || -- valor do icms retido susbt. trib
                              REPLACE(LTRIM(TO_CHAR(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS,'C','', DECODE(NF.IDSTATUSNFE,'I','',VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF,'10','I','1')))), '999999999990.00')), '.', ',') || '|' || -- valor do IPI
                              REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF, '50', 'I','1')))),2), '999999999990.00')), '.', ',') || '|' || -- valor do PIS
                              REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','',VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF, '60', 'I','1')))),2), '999999999990.00')), '.', ',') || '|' || -- valor do COFINS
                              DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','','0,00'))) || '|' || -- pis retido por subst. trib.
                              DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NF.IDSTATUSNFE,'I','','0,00'))) || '|'  AS DADOS_3,
                              NF.CDFILIAL AS CDFILIAL, NF.NRLANCTONF AS NRLANCTONF, '' AS CDCAIXA, 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, NF.IDORIGDESTNF,
                              DECODE(NF.IDSTATUSNFE,'D','',DECODE(NF.IDSTATUS,'C','',NF.IDORIGDESTNF||DECODE(NF.IDORIGDESTNF, 'F', NF.CDFORNECED, DECODE(NF.IDORIGDESTNF, 'C', NF.CDCLIENTE , DECODE(NF.IDENTRSAIDOP, 'E', NF.CDFILIEMIT, NF.CDFILIRECE))))) AS COD_PAR,
                              DECODE(NF.IDSTATUSNFE, 'D', '04', DECODE(NF.IDSTATUSNFE, 'I', '05',DECODE(IDSTATUS,'C','02',DECODE(NF.IDREGESPECIAL,'S','08',DECODE(NF.IDENTRSAIDOP,'S', (CASE WHEN DTEMISSAO < P_DTINICIAL THEN DECODE(NF.CDTIPOOPER,'03','07',TIPOOPER_COMPL_S,'07','01') ELSE DECODE(NF.CDTIPOOPER,'03','06',TIPOOPER_COMPL_S,'06','00') END),(CASE WHEN ((NF.CDSERIEENTR BETWEEN '890' AND '899') AND (NF.CDMODDOCFISC = '55')) THEN '08'ELSE (CASE WHEN DTENTRSAID < P_DTINICIAL THEN DECODE(NF.CDTIPOOPER,'03','07',TIPOOPER_COMPL_E,'07','01')ELSE DECODE(NF.CDTIPOOPER,'03','06',TIPOOPER_COMPL_E,'06','00') END) END)))))) AS SITU_DOC,
                              NF.IDSTATUSNFE, NVL(NF.IDSIMPLESNACIONAL,'N') AS IDSIMPLESNACIONAL, NF.QTDEPARC, NF.CDTIPOOPER, DECODE(NF.IDENTRSAIDOP,'S',NF.CDFILIEMIT,NF.CDFILIRECE) AS FILIAL,
                              NF.COD_DA, NF.NUM_DA
                         FROM (/*NF DE SAIDA COM E SEM CARGA TRIBUTARIA*/
                               SELECT N.CAMPO2, N.CAMPO3, N.NRACESSONFE, N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, N.CDSERIE, N.CDSERIEENTR,
                                      N.NRINSJURNF, N.IDSTATUS,N.IDREGESPECIAL,N.DTENTRSAID, N.VRNOTAFISC, N.VRPRODUTOS, N.IDPGTOFRETE,
                                      N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,
                                      N.CDFILIEMIT, N.CDFILIRECE, N.DTEMISSAO, N.CDFISCOPERNF, N.CDMODDOCFISC, N.CDCLIENTE, N.CDFORNECED, N.IDORIGDESTNF,
                                      N.VRCONTABILNF, N.NRINSJURTRAN, N.IDTPIJURTRAN, N.VRPBRUTOVOLU, N.VRPLIQUVOLU,
                                      TO_CHAR(NVL(VALOR_IMPOSTO_NFE_CARGA(N.CDFILIAL, N.NRLANCTONF, 'B', 'I', '1'),0)/* - NVL(N.VRBASEC197,0)*/ +
                                              NVL(DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,VALOR_IMPOSTO_NFE_CARGA(N.CDFILIAL,N.NRLANCTONF,'B','C','1'))),0)) AS VRBASECALCNF,
                                      TO_CHAR(NVL(VALOR_IMPOSTO_NFE_CARGA(N.CDFILIAL, N.NRLANCTONF, 'I', 'I', '1'),0)/* - NVL(N.VRIMPOSTOC197,0)*/ +
                                              DECODE(V_IDSOMAFECP,'S',NVL(VALOR_FECP_NFE(N.CDFILIAL, N.NRLANCTONF, '00', 'I', '1'),0),0) +
                                              NVL(DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_NFE_CARGA(N.CDFILIAL,N.NRLANCTONF,'I','C','1')),0)) AS VRIMPOSTONF,
                                      N.VROUTRCRED, N.VRISENTRIB, N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE, N.IDSIMPLESNACIONAL, N.QTDEPARC,
                                      (SELECT NVL(SUM(I.VRIMPOSTOIT + DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0)),0) FROM ITEMPROIMPOS I, IMPOSTO IM WHERE I.IDIMPDESONERAIT = 'S' AND I.CDFILIAL = N.CDFILIAL AND I.NRLANCTONF = N.NRLANCTONF AND I.CDIMPOSTO = IM.CDIMPOSTO AND IM.CDIMPOSTONFE = P_CDIMPOSTO) AS VRIMPDESONERADO,
                                      N.COD_DA, N.NUM_DA
                                FROM (SELECT DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                             DECODE(N.IDENTRSAIDOP, 'E', DECODE(IDSTATUSNFE,'A','0',DECODE(IDSTATUS,'E','0','1')), '0') AS CAMPO3, NRACESSONFE,
                                             I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR,
                                             N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL,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,
                                             /*NVL(C_197.VRBASEC197,0)*/ 0 AS VRBASEC197, /*NVL(C_197.VRIMPOSTOC197,0)*/ 0 AS VRIMPOSTOC197,
                                             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,
                                             N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE,  DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N') AS IDSIMPLESNACIONAL,
                                             MAX((SELECT COUNT(DISTINCT NRPARCELAREC)
                                                    FROM CONTARECEBER C
                                                   WHERE N.CDFILIAL    = C.CDFILIAL
                                                     AND N.NRLANCTONF  = C.NRLANCTONF
                                                     AND C.NRLANBOLREC IS NOT NULL)) AS QTDEPARC,
                                             N.CDMODDOCARREC AS COD_DA, N.NRDOCARRECAD AS NUM_DA
                                        FROM NOTAFISCAL N, IMPOSTONF I, IMPOSTO IP, PARAMFORNE PF, SERIENF SS, SERIEENTR SE, TIPOOPERAC T/*,
                                             (SELECT NVL(SUM(IT.VRBASECALCIT),0) AS VRBASEC197,
                                                     NVL(SUM(ROUND(IT.VRIMPOSTOIT + DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICFECP,0),0),2)),0)  AS VRIMPOSTOC197,
                                                     DT.CDFILIAL, DT.NRLANCTONF
                                                FROM ITEMPROIMPOS IT, IMPOSTO IM, DETITMAPARECUP DT
                                               WHERE IT.CDFILIAL     = DT.CDFILIAL
                                                 AND IT.NRLANCTONF   = DT.NRLANCTONF
                                                 AND IT.NRSEQITPROD  = DT.NRSEQITPROD
                                                 AND IT.CDIMPOSTO    = DT.CDIMPOSTO
                                                 AND IT.NRITPROIMPOS = DT.NRITPROIMPOS
                                                 AND IT.NRITPROIMPOS = '1'
                                                 AND IT.CDFISCALIT   = '1'
                                                 AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                                                 AND IM.CDIMPOSTONFE = '00'
                                                 AND DT.IDTIPOITMAPR <> 'VI'
                                               GROUP BY DT.CDFILIAL, DT.NRLANCTONF) C_197*/
                                       WHERE I.NRSEQUMAPAPU    = V_NRSEQUMAPAPU
                                         AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                         AND IP.CDIMPOSTO      = I.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') <> 'TMP'
                                         AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                         AND N.IDENTRSAIDOP    = 'S'
                                         AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                         AND N.CDFILIAL        = I.CDFILIAL
                                         AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                         AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                         AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                         AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                         AND SS.CDSERIE(+)     = N.CDSERIE
                                         AND N.NRLANCTONF      = I.NRLANCTONF
                                         AND N.CDFORNECED      = PF.CDFORNECED(+)
                                         AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                         AND N.CDMODDOCFISC IN ('01','1B','04','55')
                                         AND NVL(P_GERAFORSALE,'N') = 'N'
                                        -- AND N.CDFILIAL        = C_197.CDFILIAL(+)
                                        -- AND N.NRLANCTONF      = C_197.NRLANCTONF(+)
										           AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
										           AND T.CDTIPOOPER      = N.CDTIPOOPER
                                       GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE),
                                                      NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR), N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL, 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, N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE,
                                                      DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N'), /*NVL(C_197.VRBASEC197,0), NVL(C_197.VRIMPOSTOC197,0),*/
                                                      N.CDMODDOCARREC, N.NRDOCARRECAD)N
                                UNION ALL
                               /******** NOTA CANCELADA *******/
                               SELECT DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                      '0' AS CAMPO3, NRACESSONFE,
                                      N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR,
                                      '' AS NRINSJURNF, N.IDSTATUS,'' AS IDREGESPECIAL, N.DTENTRSAID, N.VRNOTAFISC, '' AS VRPRODUTOS, N.IDPGTOFRETE,
                                      N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,
                                      '' AS CDFILIEMIT, '' AS 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,
                                      NRNOTAFISCALELE,
                                      N.CDTIPOOPER, N.IDSTATUSNFE, 'N' AS IDSIMPLESNACIONAL,
                                      0 AS QTDEPARC, 0 AS VRIMPDESONERADO, '' AS COD_DA, '' AS NUM_DA
                                 FROM NOTAFISCAL N, SERIENF SS, SERIEENTR SE
                                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') <> 'TMP'
                                  AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                  AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                  AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                  AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                  AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                  AND SS.CDSERIE(+)     = N.CDSERIE
                                  AND N.IDENTRSAIDOP = 'S'
                                  AND N.IDSTATUS = 'C'
                                  AND N.CDMODDOCFISC IN ('01','1B','04','55')
                                  AND N.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL
                                  AND N.CDINSCESTA = P_CDINSCESTA
                                  AND N.CDEMPRESA = P_CDEMPRESA
                                GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE), NVL(SE.CDSERIEENTRNFE,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, N.NRACESSONFE, N.CDTIPOOPER, N.IDSTATUSNFE, N.NRNOTAFISCALELE
                                UNION ALL
                               /******** NOTA DENEGADA *******/
                               /*Nao e possivel escriturar nota denegada na entrada, entao mesmo que seja a transmissao de uma entrada
                                 deve ser escriturada como saida para manter o controle de numeracao*/
                               SELECT '1' AS CAMPO2,
                                      '0' AS CAMPO3, NRACESSONFE,
                                      '' AS CDFILIAL, '' AS NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(NVL(SS.CDSERIENFE,N.CDSERIE),NVL(SE.CDSERIEENTR,N.CDSERIEENTR)) AS CDSERIE, '' AS CDSERIEENTR,
                                      '' AS NRINSJURNF, N.IDSTATUS,'' AS IDREGESPECIAL, N.DTENTRSAID, N.VRNOTAFISC, '' AS VRPRODUTOS, N.IDPGTOFRETE,
                                      '' AS NRPLACAVEIC, '' AS DSQTDEVOLU, '' AS DSESPECVOLU, '' AS DSMARCAVOLU, N.DSNUMEVOLU,
                                      '' CDFILIEMIT, '' 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,
                                      NRNOTAFISCALELE,
                                      N.CDTIPOOPER, N.IDSTATUSNFE, 'N' AS IDSIMPLESNACIONAL,
                                      0 AS QTDEPARC, 0 AS VRIMPDESONERADO, '' AS COD_DA, '' AS NUM_DA
                                 FROM NOTAFISCAL N, SERIENF SS, SERIEENTR SE
                                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') <> 'TMP'
                                  AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                  AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                  AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                  AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                  AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                  AND SS.CDSERIE(+)     = N.CDSERIE
                                  AND N.IDSTATUSNFE     = 'D'
                                  AND N.NRACESSONFE     IS NOT NULL
                                  AND N.CDMODDOCFISC    IN ('01','1B','04','55')
                                  AND N.DTEMISSAO       BETWEEN P_DTINICIAL AND P_DTFINAL
                                  AND N.CDINSCESTA      = P_CDINSCESTA
                                  AND N.CDEMPRESA       = P_CDEMPRESA
                                GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC,
                                         NVL(NVL(SS.CDSERIENFE,N.CDSERIE),NVL(SE.CDSERIEENTR,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, N.NRACESSONFE, N.CDTIPOOPER, N.IDSTATUSNFE, N.NRNOTAFISCALELE
                                UNION ALL
                               /******** NUMERACAO INUTILIZADA *******/
                               SELECT '1' AS CAMPO2,
                                      '0' AS CAMPO3,
                                      ''  AS NRACESSONFE,
                                      ''  AS CDFILIAL, '' AS NRLANCTONF,
                                      'S' AS IDENTRSAIDOP, N.NRINUTILNFELE AS NRNOTAFISC, N.CDSERIE AS CDSERIE, '' AS CDSERIEENTR,
                                      '' AS NRINSJURNF, 'I' AS IDSTATUS,'' AS IDREGESPECIAL, N.DTINUTNFE AS DTENTRSAID, 0 AS VRNOTAFISC, '' AS VRPRODUTOS, '' AS IDPGTOFRETE,
                                      '' AS NRPLACAVEIC, '' AS DSQTDEVOLU, '' AS DSESPECVOLU, '' AS DSMARCAVOLU, '' AS DSNUMEVOLU,
                                      '' CDFILIEMIT, '' CDFILIRECE, N.DTINUTNFE AS DTEMISSAO, '' AS CDFISCOPERNF, '55' AS CDMODDOCFISC, '' AS CDCLIENTE, '' AS CDFORNECED, '' AS IDORIGDESTNF,
                                      '' AS VRCONTABILNF, '' AS NNRINSJURTRAN, '' AS IDTPIJURTRAN, 0 AS VRPBRUTOVOLU, 0 AS VRPLIQUVOLU,
                                      '' AS VRBASECALCNF,
                                      '' AS VRIMPOSTONF,
                                      '' AS VROUTRCRED,
                                      '' AS VRISENTRIB,
                                      N.NRINUTILNFELE AS NRNOTAFISCALELE,
                                      '' AS CDTIPOOPER, 'I' AS IDSTATUSNFE, 'N' AS IDSIMPLESNACIONAL,
                                      0 AS QTDEPARC, 0 AS VRIMPDESONERADO, '' AS COD_DA, '' AS NUM_DA
                                 FROM NUMINUTILNFELE N
                                WHERE TRUNC(N.DTINUTNFE)BETWEEN P_DTINICIAL AND P_DTFINAL
                                  AND N.CDEMPRESA  = P_CDEMPRESA
                                  AND N.CDINSCESTA = P_CDINSCESTA
                                  AND N.NRPROTOCINUTNFE  <> 'TEKNISA'
                                UNION ALL
                               /*NF ENTRADA COM E SEM CARGA TRIBUTARIA*/
                               SELECT N.CAMPO2, N.CAMPO3, N.NRACESSONFE, N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, N.CDSERIE,
                                      N.CDSERIEENTR, N.NRINSJURNF, N.IDSTATUS, N.IDREGESPECIAL, N.DTENTRSAID, N.VRNOTAFISC, N.VRPRODUTOS, N.IDPGTOFRETE,
                                      N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU, N.CDFILIEMIT, N.CDFILIRECE,
                                      N.DTEMISSAO, N.CDFISCOPERNF, N.CDMODDOCFISC, N.CDCLIENTE, N.CDFORNECED, N.IDORIGDESTNF,
                                      N.VRCONTABILNF, N.NRINSJURTRAN, N.IDTPIJURTRAN, N.VRPBRUTOVOLU, N.VRPLIQUVOLU,
                                      TO_CHAR(NVL(VALOR_IMPOSTO_NFE_CARGA(N.CDFILIAL, N.NRLANCTONF, 'B', 'I', '1'),0) /*- NVL(N.VRBASEC197,0)*/ +
                                              NVL(DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,VALOR_IMPOSTO_NFE_CARGA(N.CDFILIAL,N.NRLANCTONF,'B','C','1'))),0)) AS VRBASECALCNF,
                                      TO_CHAR(NVL(VALOR_IMPOSTO_NFE_CARGA(N.CDFILIAL, N.NRLANCTONF, 'I', 'I', '1'),0) /*- NVL(N.VRIMPOSTOC197,0)*/ +
                                              NVL(VALOR_FECP_NFE(N.CDFILIAL, N.NRLANCTONF, '00', 'I', '1'),0) +
                                              NVL(DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,VALOR_IMPOSTO_NFE_CARGA(N.CDFILIAL,N.NRLANCTONF,'I','C','1'))),0)) AS VRIMPOSTONF,
                                      N.VROUTRCRED, N.VRISENTRIB, N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE, N.IDSIMPLESNACIONAL, N.QTDEPARC,
                                      (SELECT NVL(SUM(I.VRIMPOSTOIT + NVL(I.VRADICFECP,0)),0) FROM ITEMPROIMPOS I, IMPOSTO IM WHERE I.IDIMPDESONERAIT = 'S' AND I.CDFILIAL = N.CDFILIAL AND I.NRLANCTONF = N.NRLANCTONF AND I.CDIMPOSTO = IM.CDIMPOSTO AND IM.CDIMPOSTONFE = P_CDIMPOSTO) VRIMPDESONERADO,
                                      N.COD_DA, N.NUM_DA
                                 FROM (SELECT DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                              DECODE(N.IDENTRSAIDOP, 'E', DECODE(IDSTATUSNFE,'A','0',DECODE(IDSTATUS,'E','0','1')), '0') AS CAMPO3, NRACESSONFE,
                                              I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR,
                                              N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL, 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,
                                              /*NVL(C_197.VRBASEC197,0)*/ 0 AS VRBASEC197, /*NVL(C_197.VRIMPOSTOC197,0)*/ 0 AS VRIMPOSTOC197,
                                              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,
                                              N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE, DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N') AS IDSIMPLESNACIONAL,
                                              MAX((SELECT COUNT(DISTINCT NRPARCELAPAG)
                                                     FROM CONTAPAGAR C
                                                    WHERE N.CDFILIAL    = C.CDFILIAL
                                                      AND N.NRLANCTONF  = C.NRLANCTONF
                                                      AND C.NRLANBOLPAG IS NOT NULL)) AS QTDEPARC,
                                              N.CDMODDOCARREC AS COD_DA, N.NRDOCARRECAD AS NUM_DA
                                         FROM NOTAFISCAL N, IMPOSTONF I, IMPOSTO IP, PARAMFORNE PF, SERIEENTR SE, SERIENF SS, TIPOOPERAC T/*,
                                              (SELECT NVL(SUM(IT.VRBASECALCIT),0) AS VRBASEC197,
                                                      NVL(SUM(ROUND(IT.VRIMPOSTOIT + NVL(IT.VRADICFECP,0),2)),0)  AS VRIMPOSTOC197,
                                                      DT.CDFILIAL, DT.NRLANCTONF
                                                 FROM ITEMPROIMPOS IT, IMPOSTO IM, DETITMAPARECUP DT, ITMAPARECUP IM
                                                WHERE IT.CDFILIAL     = DT.CDFILIAL
                                                  AND IT.NRLANCTONF   = DT.NRLANCTONF
                                                  AND IT.NRSEQITPROD  = DT.NRSEQITPROD
                                                  AND IT.CDIMPOSTO    = DT.CDIMPOSTO
                                                  AND IT.NRITPROIMPOS = DT.NRITPROIMPOS
                                                  AND IT.NRITPROIMPOS = '1'
                                                  AND IT.CDFISCALIT   = '1'
                                                  AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                                                  AND IM.CDIMPOSTONFE = '00'
                                                  AND DT.IDTIPOITMAPR <> 'VI'
                                                  AND IM.NRSEQUMAPAPU = DT.NRSEQUMAPAPU
                                                  AND IM.IDTIPOITMAPR = DT.IDTIPOITMAPR
                                                  AND IM.NRSEQITMAPR  = DT.NRSEQITMAPR
                                                  AND IM.IDESTSUBAPUR = 'N'
                                                GROUP BY DT.CDFILIAL, DT.NRLANCTONF) C_197*/
                                        WHERE I.NRSEQUMAPAPU    = V_NRSEQUMAPAPU
                                          AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                          AND IP.CDIMPOSTO      = I.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')  <> 'TMP'
                                          AND NVL(N.IDSISTEMA, 'DFS')  <> 'FIS' )
                                          AND N.IDENTRSAIDOP    = 'E'
                                          AND PF.CDFORNECED(+)  = N.CDFORNECED
                                          AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                          AND N.CDFILIAL        = I.CDFILIAL
                                          AND N.NRLANCTONF      = I.NRLANCTONF
                                          AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                          AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                          AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                          AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                          AND SS.CDSERIE(+)     = N.CDSERIE
                                          AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                          AND N.CDMODDOCFISC IN ('01','1B','04','55')
                                          AND NVL(P_GERAFORSALE,'N') = 'N'
                                          --AND N.CDFILIAL        = C_197.CDFILIAL(+)
                                          --AND N.NRLANCTONF      = C_197.NRLANCTONF(+)
                                          AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                          AND T.CDTIPOOPER      = N.CDTIPOOPER
                                        GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP,
                                               N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE), NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR), N.NRINSJURNF, N.IDSTATUS, T.IDREGESPECIAL, 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, N.NRNOTAFISCALELE, N.CDTIPOOPER, IDSTATUSNFE,
                                               DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N'), /*NVL(C_197.VRBASEC197,0), NVL(C_197.VRIMPOSTOC197,0),*/
                                               N.CDMODDOCARREC, N.NRDOCARRECAD)N
                                UNION
                               /*******NOTAS FISCAIS FORSALE - C100 FOS ***********/
                               SELECT DECODE(NF.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                      DECODE(NF.IDENTRSAIDOP, 'E', DECODE(IDSTATUS,'E','0','1'), '0') AS CAMPO3, NRACESSONFE,
                                      NF.CDFILIAL, NF.NRLANCTONF, NF.IDENTRSAIDOP, NF.NRNOTAFISC, NF.CDSERIE, NF.CDSERIEENTR,
                                      NF.NRINSJURNF, NF.IDSTATUS, NF.IDREGESPECIAL, NF.DTENTRSAID, NF.VRNOTAFISC, TO_CHAR(NF.VRPRODUTOS) AS VRPRODUTOS, NF.IDPGTOFRETE,
                                      NF.NRPLACAVEIC, NF.DSQTDEVOLU, NF.DSESPECVOLU, NF.DSMARCAVOLU, NF.DSNUMEVOLU,
                                      NF.CDFILIEMIT, NF.CDFILIRECE, NF.DTEMISSAO, MAX(NF.CDFISCOPERNF) AS CDFISCOPERNF, NF.CDMODDOCFISC, NF.CDCLIENTE, NF.CDFORNECED, NF.IDORIGDESTNF,
                                      NF.VRCONTABILNF AS VRCONTABILNF,
                                      NF.NRINSJURTRAN, NF.IDTPIJURTRAN, NF.VRPBRUTOVOLU, NF.VRPLIQUVOLU,
                                      TO_CHAR(VRBASECALCNF) AS VRBASECALCNF,
                                      TO_CHAR(VRIMPOSTONF),
                                      TO_CHAR(VROUTRCRED),
                                      TO_CHAR(VRISENTRIB),
                                      NF.NRNOTAFISCALELE,
                                      NF.CDTIPOOPER, '' AS IDSTATUSNFE, NF.IDSIMPLESNACIONAL, NF.QTDEPARC,
                                      (SELECT NVL(SUM(I.VRIMPOSTOIT + DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0)),0) FROM ITEMPROIMPOS I, IMPOSTO IM WHERE I.IDIMPDESONERAIT = 'S' AND I.CDFILIAL = NF.CDFILIAL AND I.NRLANCTONF = NF.NRLANCTONF AND I.CDIMPOSTO = IM.CDIMPOSTO AND IM.CDIMPOSTONFE = P_CDIMPOSTO) VRIMPDESONERADO,
                                      NF.COD_DA, NF.NUM_DA
                                 FROM ENDECLIE EC, ENDEFORN EF, ENDEFILI EFI,
                                      (SELECT DECODE(NVL(N.IDORIGDESTNF,DECODE(N.IDENTRSAIDOP,'S','C','F')),'C',CL.NRINSJURCLIE,'F',FO.NRINSJURFORN,'L',FI.NRINSJURFILI) AS NRINSJURNF,
                                              DECODE(NVL(N.IDORIGDESTNF,DECODE(N.IDENTRSAIDOP,'S','C','F')),'C',CL.NRINSESTCLIE,'F',FO.NRINSESTFORN,'L',FI.CDINSCESTA  ) AS NRINSEST,
                                              N.IDORIGDESTNF, N.CDCLIENTE, N.CDFORNECED, N.CDFILIEMIT, N.CDFILIRECE, N.NRNOTAFISC, I.CDFISCOPERNF,
                                              N.NRLANCTONF,N.CDFILIAL, N.IDENTRSAIDOP, DECODE(N.CDMODDOCFISC,'2D','55',N.CDMODDOCFISC) AS CDMODDOCFISC,
                                              NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR, N.DTENTRSAID, N.DTEMISSAO,
                                              TO_CHAR(SUM(VRCONTABILNF)) AS VRCONTABILNF,
                                              DECODE(I.CDFISCALNF,'1',SUM(VRBASECALCNF),0) AS VRBASECALCNF,
                                              DECODE(I.CDFISCALNF,'2',SUM(VRCONTABILNF),'1',SUM(DECODE(I.VRPERREDBCNF,0, 0,I.VRCONTABILNF-I.VRBASECALCNF)),0) AS VRISENTRIB,
                                              DECODE(I.CDFISCALNF,'3',SUM(VRCONTABILNF),0) AS VROUTRCRED,
                                              DECODE(I.CDFISCALNF,'1',SUM(VRIMPOSTONF + DECODE(V_IDSOMAFECP,'S',NVL(VRADICFECP,0),0)),0) AS VRIMPOSTONF,
                                              SUM(N.VRPRODUTOS) AS VRPRODUTOS,
                                              N.NRINSJURTRAN,N.IDTPIJURTRAN, N.VRPBRUTOVOLU, N.VRPLIQUVOLU, N.NRNOTAFISCALELE, I.VRPERREDBCNF,N.IDSTATUS,T.IDREGESPECIAL, N.IDPGTOFRETE,
                                              N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,N.VRNOTAFISC, NRACESSONFE, N.CDTIPOOPER,
                                              DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N') AS IDSIMPLESNACIONAL,
                                              MAX((SELECT COUNT(DISTINCT NRPARCELAREC)
                                                     FROM CONTARECEBER C
                                                    WHERE N.CDFILIAL    = C.CDFILIAL
                                                      AND N.NRLANCTONF  = C.NRLANCTONF
                                                      AND C.NRLANBOLREC IS NOT NULL)) AS QTDEPARC, N.CDMODDOCARREC AS COD_DA, N.NRDOCARRECAD AS NUM_DA
                                         FROM NOTAFISCAL N, IMPOSTONF I, IMPOSTO IMP, CLIENTE CL, FORNECEDOR FO, FILIAL FI, PARAMFORNE PF, SERIENF SS, SERIEENTR SE, TIPOOPERAC T
                                        WHERE N.CDFILIAL     = I.CDFILIAL
                                          AND N.NRLANCTONF   = I.NRLANCTONF
                                          AND IMP.CDIMPOSTO  = I.CDIMPOSTO
                                          AND N.CDCLIENTE    = CL.CDCLIENTE(+)
                                          AND N.CDFORNECED   = FO.CDFORNECED(+)
                                          AND N.CDFORNECED   = PF.CDFORNECED(+)
                                          AND N.CDFILIRECE   = FI.CDFILIAL(+)
                                          AND N.CDFILIEMIT   = FI.CDFILIAL(+)
                                          AND DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL
                                          AND ((N.IDSTATUS   = 'A' OR N.IDSTATUS = 'E') OR (N.IDSTATUSNFE = 'A' OR N.IDSTATUSNFE = 'T'))
                                          AND N.CDINSCESTA   = P_CDINSCESTA
                                          AND IMP.CDIMPOSTONFE = P_CDIMPOSTO
                                          AND (I.IDIMPOSTONF <> '3' OR IDIMPOSTONF IS NULL)
                                          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')  <> 'TMP'
                                          AND NVL(N.IDSISTEMA, 'DFS')  <> 'FIS')
                                          AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                          AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                          AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                          AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                          AND SS.CDSERIE(+)     = N.CDSERIE
                                          AND N.CDMODDOCFISC IN ('01','1B','04','55','2D')
                                          AND N.IDENTRSAIDOP = 'S'
                                          AND NVL(P_GERAFORSALE,'N') = 'S'
										            AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                          AND T.CDTIPOOPER      = N.CDTIPOOPER
                                        GROUP BY DECODE(NVL(N.IDORIGDESTNF,DECODE(N.IDENTRSAIDOP,'S','C','F')),'C',CL.NRINSJURCLIE,'F',FO.NRINSJURFORN,'L',FI.NRINSJURFILI),
                                                 DECODE(NVL(N.IDORIGDESTNF,DECODE(N.IDENTRSAIDOP,'S','C','F')),'C',CL.NRINSESTCLIE,'F',FO.NRINSESTFORN,'L',FI.CDINSCESTA),N.IDORIGDESTNF,
                                                 N.CDCLIENTE, N.CDFORNECED, N.CDFILIEMIT, N.CDFILIRECE, N.NRNOTAFISC, I.CDFISCOPERNF, N.NRLANCTONF,
                                                 N.CDFILIAL, N.IDENTRSAIDOP, N.CDMODDOCFISC, NVL(SS.CDSERIENFE,N.CDSERIE), NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR),
                                                 N.DTENTRSAID, N.DTEMISSAO,  I.CDFISCALNF, N.NRINSJURTRAN, N.IDTPIJURTRAN, N.VRPBRUTOVOLU,
                                                 N.VRPLIQUVOLU,N.NRNOTAFISCALELE,I.VRPERREDBCNF,N.IDSTATUS,T.IDREGESPECIAL, IDPGTOFRETE,
                                                 N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,N.VRNOTAFISC,NRACESSONFE,N.CDTIPOOPER,
                                                 DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N'), N.CDMODDOCARREC, N.NRDOCARRECAD)NF
                                WHERE NF.CDCLIENTE    = EC.CDCLIENTE(+)
                                  AND NF.CDFORNECED   = EF.CDFORNECED(+)
                                  AND NF.CDFILIEMIT   = EFI.CDFILIAL(+)
                                  AND NF.CDFILIRECE   = EFI.CDFILIAL(+)
                                  AND IDTPENDECLIE(+) = 'P'
                                  AND IDTPENDEFORN(+) = 'P'
                                  AND IDTPENDEFILI(+) = 'P'
                                  AND NVL(P_GERAFORSALE,'N') = 'S'
                                GROUP BY DECODE(NF.IDENTRSAIDOP, 'E', '0','1'),
                                         DECODE(NF.IDENTRSAIDOP, 'E', DECODE(IDSTATUS,'E','0','1'), '0'), NRACESSONFE,
                                         NF.CDFILIAL, NF.NRLANCTONF, NF.IDENTRSAIDOP, NF.NRNOTAFISC, NF.CDSERIE, NF.CDSERIEENTR,
                                         NF.NRINSJURNF, NF.IDSTATUS,NF.IDREGESPECIAL, NF.DTENTRSAID, NF.VRNOTAFISC, TO_CHAR(NF.VRPRODUTOS), NF.IDPGTOFRETE,
                                         NF.NRPLACAVEIC, NF.DSQTDEVOLU, NF.DSESPECVOLU, NF.DSMARCAVOLU, NF.DSNUMEVOLU,
                                         NF.CDFILIEMIT, NF.CDFILIRECE, NF.DTEMISSAO, NF.CDMODDOCFISC, NF.CDCLIENTE, NF.CDFORNECED, NF.IDORIGDESTNF,
                                         NF.VRCONTABILNF, NF.NRINSJURTRAN, NF.IDTPIJURTRAN, NF.VRPBRUTOVOLU, NF.VRPLIQUVOLU,
                                         TO_CHAR(VRBASECALCNF), TO_CHAR(VRIMPOSTONF), TO_CHAR(VROUTRCRED), TO_CHAR(VRISENTRIB),
                                         NF.NRNOTAFISCALELE,NF.CDTIPOOPER,NF.IDSIMPLESNACIONAL, NF.QTDEPARC, NF.CDTIPOOPER, NF.COD_DA, NF.NUM_DA)NF
                        UNION ALL
                       /*Registro C100 para Nota Fiscal Eletronica para Consumidor Final (NFC-e)*/
                       SELECT  '|C100'                                                                || '|' ||
                               NF.CAMPO2                                                              || '|' ||
                               NF.CAMPO3                                                              || '|' AS DADOS_1,
                               --''                                                                   || '|' ||
                               NF.CDMODDOCFISC                                                        || '|' ||
                               DECODE(NF.IDSTATUSNFCE,'D','04','I','05','C','02',CASE WHEN NF.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL THEN '00' ELSE '01' END) || '|' ||
                               REPLACE(REPLACE(NF.CDSERIE,'-',''),'.','')                             || '|' ||
                               LTRIM(NF.NRNOTAFISC,'0')                                               || '|' ||
                               NF.NRACESSONFCE                                                        || '|' ||
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','',TO_CHAR(NF.DTEMISSAO,'ddmmyyyy')) || '|' AS DADOS_2,
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','',REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRCONTABILNF,2),'999999999990.00')),'.',','))  || '|' || -- Campo 12 - Valor total do documento fiscal
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','','0')                                                                           || '|' || -- Campo 13 - Indicador de Pagamento
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','',REPLACE(LTRIM(TO_CHAR(NF.VRDESITVEND,'999999999990.00')),'.',','))             || '|' || -- Campo 14 - Valor total do desconto
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','','0,00')                                                                        || '|' || -- Campo 15 - Abatimento naotributado e naocomercial - desconto ICMS
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','',REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRCONTABILNF+NF.VRDESITVEND,2),'999999999990.00')),'.',',')) || '|' || -- Campo 16 - Valor total das mercadorias e servicos
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','','9')                                                                           || '|' || -- Campo 17 - Indicador de Frete
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','','0,00')                                                                        || '|' || -- Campo 18 - Valor de Frete
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','','0,00')                                                                        || '|' || -- Campo 19 - Valor do Seguro
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','','0,00')                                                                        || '|' || -- Campo 20 - Outras despesas acessorias
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','',REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRBASECALC,2 ),'999999999990.00')), '.', ','))  || '|' || -- Campo 21 - Valor da base de calculo do ICMS
                               DECODE(NF.IDSTATUSNFCE,'D','','I','','C','',REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRIMPOSTONF,2),'999999999990.00')), '.', ','))  || '|' || -- Campo 22 - Valor do ICMS
                               '' || '|' || -- base de calc susbt. trib
                               '' || '|' || -- valor do icms retido susbt. trib
                               '' || '|' || -- valor do IPI
                               '' || '|' || -- valor do PIS
                               '' || '|' || -- valor do COFINS
                               '' || '|' || -- pis retido por subst. trib.
                               '' || '|' AS DADOS_3,
                               NF.CDFILIAL, NF.NRLANCTONF, NF.CDCAIXA, '' AS NRINSJURTRAN, '' AS IDTPIJURTRAN, 0 AS VRPBRUTOVOLU, 0 AS VRPLIQUVOLU,
                               '' AS NRPLACAVEIC, '' AS DSQTDEVOLU, '' AS DSESPECVOLU, '' AS DSMARCAVOLU, '' AS DSNUMEVOLU, '' AS CDFISCOPERNF, NF.NRNOTAFISC AS NRNOTAFISC,
                               NF.DTEMISSAO, NF.DTENTRSAID, NF.IDENTRSAIDOP, TO_CHAR(NF.VRIMPOSTONF) AS VRIMPOSTONF, NF.CDMODDOCFISC, NF.IDSTATUS, NF.IDORIGDESTNF,
                               '' AS COD_PAR, DECODE(NF.IDSTATUSNFCE,'D','04','I','05','C','02',CASE WHEN NF.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL THEN '00' ELSE '01' END) AS SITU_DOC,
                               NF.IDSTATUSNFCE AS IDSTATUSNFE, 'N' AS IDSIMPLESNACIONAL, NF.QTDEPARC, NF.CDTIPOOPER, NF.CDFILIAL AS FILIAL, '' AS COD_DA, '' AS NUM_DA
                         FROM (SELECT '1' AS CAMPO2, '0' AS CAMPO3, '65' AS CDMODDOCFISC, VE.IDSTATUSNFCE, VE.CDSERIENFCE AS CDSERIE, VE.NRNOTAFISCALCE AS NRNOTAFISC,
                                      SUM(TRUNC(IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0)),2)-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0)) AS VRCONTABILNF,
                                      SUM(VRDESITVEND) AS VRDESITVEND, VE.NRACESSONFCE, TRUNC(VE.DTEMISSAONFCE) AS DTEMISSAO,
                                      SUM(DECODE(IM.IDTPIMPOSFIS,'T',CASE WHEN II.VRPRBCREDUICMS > 0
                                                                          THEN II.VRIMPOPRODREDUZ+DECODE(V_IDSOMAFECP,'S',NVL(II.VRIMPICMSFCP,0),0)
                                                                          ELSE TRUNC(II.VRIMPOPRODIT,2)+DECODE(V_IDSOMAFECP,'S',NVL(II.VRIMPICMSFCP,0),0) END,0)) AS VRIMPOSTONF,
                                      SUM(DECODE(IM.IDTPIMPOSFIS,'T',CASE WHEN II.VRPRBCREDUICMS > 0
                                                                          THEN II.VRBASECALCREDUZ
                                                                          ELSE TRUNC(IT.QTPRODVEND*(IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0)),2)-NVL(IT.VRDESITVEND,0)+NVL(IT.VRACRITVEND,0) END,0)) AS VRBASECALC,
                                      VE.CDFILIAL AS CDFILIAL, VE.NRSEQVENDA AS NRLANCTONF, VE.CDCAIXA, VE.DTENTRVENDA AS DTENTRSAID, 'S' AS IDENTRSAIDOP,
                                      '' AS IDSTATUS, '' AS IDORIGDESTNF, 1 AS QTDEPARC, P.CDTPOPERCNTREC AS CDTIPOOPER
                                 FROM VENDA VE, ITEMVENDA IT, ITVENDAIMPOS II, FILIAL FI, PARAVEND P, IMPOSTO IM,
                                      (SELECT DISTINCT R.CDFILIALVENDA, R.CDCAIXA, R.NRSEQVENDA, N.DTEMISSAO
                                         FROM RELACNFCEAPROV R, NOTAFISCAL N, IMPOSTONF I
                                        WHERE R.CDFILIALNOTA = I.CDFILIAL
                                          AND R.NRLANCTONF   = I.NRLANCTONF
                                          AND I.NRSEQUMAPAPU = V_NRSEQUMAPAPU
                                          AND I.CDFILIAL     = N.CDFILIAL
                                          AND I.NRLANCTONF   = N.NRLANCTONF
                                          AND N.IDSISTEMA    = 'FIS') RE
                                WHERE VE.CDEMPRESA       = P_CDEMPRESA
                                  AND VE.IDSITUVENDA     = 'O'
                                  AND VE.IDTPAMBNFCE     = '1'
                                  AND VE.IDSTATUSNFCE    = 'A'
                                  AND VE.IDAPROVACAONFCE = 'A'
                                  AND VE.CDFILIAL        = IT.CDFILIAL
                                  AND VE.NRSEQVENDA      = IT.NRSEQVENDA
                                  AND VE.CDCAIXA         = IT.CDCAIXA
                                  AND IT.CDFILIAL        = RE.CDFILIALVENDA
                                  AND IT.CDCAIXA         = RE.CDCAIXA
                                  AND IT.NRSEQVENDA      = RE.NRSEQVENDA
                                  AND IT.CDFILIAL        = II.CDFILIAL
                                  AND IT.CDCAIXA         = II.CDCAIXA
                                  AND IT.NRSEQVENDA      = II.NRSEQVENDA
                                  AND IT.NRSEQUITVEND    = II.NRSEQUITVEND
                                  AND VE.IDTPEMISVEND    = 'N'
                                  AND VE.CDFILIAL        = FI.CDFILIAL
                                  AND FI.CDEMPRESA       = P_CDEMPRESA
                                  AND FI.CDINSCESTA      = P_CDINSCESTA
                                  AND FI.CDFILIAL        = P.CDFILIAL
                                  AND IM.CDIMPOSTO       = II.CDIMPOSTO
                                GROUP BY VE.IDSTATUSNFCE, VE.CDSERIENFCE, VE.NRNOTAFISCALCE, VE.NRACESSONFCE,
                                         TRUNC(VE.DTEMISSAONFCE), VE.CDFILIAL, VE.NRSEQVENDA, VE.CDCAIXA,
                                         VE.DTENTRVENDA, P.CDTPOPERCNTREC
                                UNION ALL
                               SELECT '1' AS CAMPO2, '0' AS CAMPO3, '65' AS CDMODDOCFISC, VE.IDSTATUSNFCE, VE.CDSERIENFCE AS CDSERIE, VE.NRNOTAFISCALCE AS NRNOTAFISC,
                                      0 AS VRCONTABILNF, 0 AS VRDESITVEND, VE.NRACESSONFCE, TRUNC(VE.DTEMISSAONFCE) AS DTEMISSAO, 0 AS VRIMPOSTONF, 0 AS VRBASECALC,
                                      VE.CDFILIAL AS CDFILIAL, VE.NRSEQVENDA AS NRLANCTONF, VE.CDCAIXA, VE.DTENTRVENDA AS DTENTRSAID, 'S' AS IDENTRSAIDOP,
                                      'C' AS IDSTATUS, '' AS IDORIGDESTNF, 0 AS QTDEPARC, '' AS CDTIPOOPER
                                 FROM VENDA VE, FILIAL FI
                                WHERE VE.CDEMPRESA       = P_CDEMPRESA
                                  AND TRUNC(VE.DTEMISSAONFCE) BETWEEN P_DTINICIAL AND P_DTFINAL
                                  AND VE.IDSITUVENDA     = 'C'
                                  AND VE.IDTPAMBNFCE     = '1'
                                  AND VE.IDSTATUSNFCE    = 'C'
                                  AND VE.IDTPEMISVEND    = 'N'
                                  AND VE.CDFILIAL        = FI.CDFILIAL
                                  AND FI.CDEMPRESA       = P_CDEMPRESA
                                  AND FI.CDINSCESTA      = P_CDINSCESTA
                                UNION ALL
                               /******** NUMERACAO INUTILIZADA *******/
                               SELECT '1' AS CAMPO2, '0' AS CAMPO3, '65' AS CDMODDOCFISC, 'I' AS IDSTATUSNFCE, I.CDSERIENFCE AS CDSERIE,
                                      I.NRNOTAFISCALCE AS NRNOTAFISC, 0 AS VRCONTABILNF, 0 AS VRDESITVEND, ''  AS NRACESSONFE,
                                      TRUNC(I.DTHRINUTNFCE) AS DTEMISSAO, 0 AS VRIMPOSTONF, 0 AS VRBASECALC, I.CDFILIAL,
                                      '' AS NRSEQVENDA, I.CDCAIXA, I.DTENTRVENDA AS DTENTRSAID, 'S' AS IDENTRSAIDOP, 'I' AS IDSTATUS,
                                      '' AS IDORIGDESTNF, 0 AS QTDEPARC, '' AS CDTIPOOPER
                                 FROM INUTILIZANFCE I, FILIAL F
                                WHERE I.CDFILIAL    = F.CDFILIAL
                                  AND F.CDEMPRESA   = P_CDEMPRESA
                                  AND F.CDINSCESTA  = P_CDINSCESTA
                                  AND TRUNC(I.DTHRINUTNFCE) BETWEEN P_DTINICIAL AND P_DTFINAL
                                  AND I.IDTPAMBNFCE = '1')NF)LOOP

         IF C_REGC100.IDSTATUS = 'C' OR C_REGC100.IDSTATUSNFE = 'D' OR C_REGC100.IDSTATUSNFE = 'I' THEN
            V_DATA := '|';
         ELSE
            IF C_REGC100.IDENTRSAIDOP = 'E' THEN
               V_DATA := TO_CHAR(C_REGC100.DTENTRSAID,'ddmmyyyy') || '|';
            ELSE
               V_DATA := TO_CHAR(C_REGC100.DTEMISSAO,'ddmmyyyy') || '|';
            END IF;
         END IF;

         V_VRDEBESP := 0;
         /* valor total de imposto fora do periodo - nf saida */
         V_VRFORAPER := 0;
         IF (C_REGC100.SITU_DOC IN ('01', '07')) AND (C_REGC100.IDENTRSAIDOP = 'S') THEN
            V_VRFORAPER   := C_REGC100.VRIMPOSTONF;
            V_TOTFORPER_S := NVL(V_TOTFORPER_S,0)+NVL(V_VRFORAPER,0);
         END IF;

         V_PARTICIPANTE := NULL;
         /* CODIGO DO PARTICIPANTE */
         SELECT MAX(CDCHAVEREG)
           INTO V_COD_PAR
           FROM TMP_REGDADOSSPED
          WHERE CDCHAVEREG = C_REGC100.COD_PAR
            AND CDREGISTRO = '0150';

         IF (V_COD_PAR IS NULL) AND (C_REGC100.COD_PAR IS NOT NULL) THEN
            V_PARTICIPANTE := RETORNA_PARTICIPANTE_SPED(C_REGC100.COD_PAR);
            IF V_PARTICIPANTE IS NULL THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
               VALUES
                 ('0150', V_SEQUPROD, C_REGC100.COD_PAR);
            END IF;
         END IF;

         V_PARTICIPANTE := CASE WHEN C_REGC100.IDSTATUS = 'C' OR C_REGC100.IDSTATUSNFE = 'D' OR C_REGC100.IDSTATUSNFE = 'I' THEN '|'
                                ELSE REPLACE(REPLACE(REPLACE(REPLACE(NVL(V_PARTICIPANTE,C_REGC100.COD_PAR),'.'),'-'),'/'),' ')||'|' END;

         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC100.DADOS_1||V_PARTICIPANTE||C_REGC100.DADOS_2||V_DATA||C_REGC100.DADOS_3, P_NRSEQARQMAG);
         V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
         V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

         /*REGISTRO C101 - INFORMACAO COMPLEMENTAR DOS DOCUMENTOS FISCAIS QUANDO DAS
          OPERACOES INTERESTADUAIS DESTINADAS A CONSUMIDOR FINAL NAO CONTRIBUINTE EC 87/15*/
         FOR C_REGC101 IN (SELECT '|C101'                                                           || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(VRFCP,       '999999999990.00')), '.', ',') || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(VRDIFALDEST, '999999999990.00')), '.', ',') || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(VRDIFALORIG, '999999999990.00')), '.', ',') || '|'  AS ARQC101
                             FROM (SELECT SUM(ROUND(DECODE(C_REGC100.IDENTRSAIDOP,'S',I.VRIMPOFCP,     DECODE(C.IDTIPOCFOP,'D',I.VRIMPOFCP,     0)),2)) AS VRFCP,
                                          SUM(ROUND(DECODE(C_REGC100.IDENTRSAIDOP,'S',I.VRIMPOPARTDEST,DECODE(C.IDTIPOCFOP,'D',I.VRIMPOPARTDEST,0)),2)) AS VRDIFALDEST,
                                          SUM(ROUND(DECODE(C_REGC100.IDENTRSAIDOP,'S',I.VRIMPOPARTREME,DECODE(C.IDTIPOCFOP,'D',I.VRIMPOPARTREME,0)),2)) AS VRDIFALORIG
                                     FROM ITEMPROIMPOS I, IMPOSTO IM, CFOPPADRAO C
                                    WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                      AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                      AND I.CDIMPOSTO     = IM.CDIMPOSTO
                                      AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                      AND I.CDFISCOPERIT  = C.CDCFOPPADRAO
                                      AND C_REGC100.CDMODDOCFISC = '55'
                                      AND C_REGC100.IDENTRSAIDOP = 'S')
                            WHERE VRFCP > 0 OR VRDIFALDEST > 0 OR VRDIFALORIG > 0) LOOP

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC101.ARQC101 , P_NRSEQARQMAG);
            V_CONTREGC   :=  NVL(V_CONTREGC,0) + 1;
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
         END LOOP;

         V_COD_PAR := null;

         /*REGISTRO C110 - OBSERVACAO DO DOCUMENTO*/
         FOR C_REGC110 IN (SELECT '|C110'                                  ||'|'||
                                  LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0')    ||'|'||
                                  'CUPOM FISCAL REFERENCIADO - COO NUMERO '||TRIM(D.NRCOOCUPOMNF)||'|'  AS DSREGC110,
                                  '|C114'                                  ||'|'||
                                  '2D'                                     ||'|'||
                                  SUBSTR(D.NRSERIEIMPNF,1,21)              ||'|'||
                                  SUBSTR(LPAD(D.NRCOOCUPOMNF,10,'0'),5,6)  ||'|'||
                                  TO_CHAR(C_REGC100.DTEMISSAO,'DDMMYYYY')  ||'|' AS DSREGC114
                             FROM DADOSNF D, NOTAFISCAL N
                            WHERE D.NRLANCTONF  = C_REGC100.NRLANCTONF
                              AND D.CDFILIAL    = C_REGC100.CDFILIAL
                              AND D.NRECFCUPOMNF IS NOT NULL
                              AND D.NRLANCTONF  = N.NRLANCTONF
                              AND D.CDFILIAL    = N.CDFILIAL
                              AND C_REGC100.CDMODDOCFISC <> '65'
                              AND NOT ((N.IDSTATUSNFE = 'A') OR (N.CDMODDOCFISC = '55')))LOOP

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC110.DSREGC110 , P_NRSEQARQMAG);
            V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

            /*REGISTRO C114 - CUPOM FISCAL REFERENCIADO*/
            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC110.DSREGC114 , P_NRSEQARQMAG);
            V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
            V_SEQADIC := V_SEQADIC +1;
         END LOOP;

         /*REGISTRO C110 - OBSERVACAO DO DOCUMENTO -- DADOSADIC*/
         FOR C_REGC110 IN (SELECT TRIM(RETIRA_CARACTER_ESPECIAL(D.DSDADOADIC,'S')) AS DSREGC110
                             FROM DADOSADIC D, NOTAFISCAL N
                            WHERE D.NRLANCTONF = C_REGC100.NRLANCTONF
                              AND D.CDFILIAL   = C_REGC100.CDFILIAL
                              AND D.NRLANCTONF = N.NRLANCTONF
                              AND D.CDFILIAL   = N.CDFILIAL
                              AND C_REGC100.CDMODDOCFISC <> '65'
                              AND TRIM(RETIRA_CARACTER_ESPECIAL(D.DSDADOADIC,'S')) IS NOT NULL)LOOP

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C110|'||LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0')||'|'||TRIM(SUBSTR(C_REGC110.DSREGC110, 0,499))||'|', P_NRSEQARQMAG);
            V_CONTREGC   := NVL(V_CONTREGC,0) + 1;
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

            V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
            INSERT INTO TMP_REGDADOSSPED
               (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
            VALUES
               ('0450', V_SEQUPROD, LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0'), TRIM(SUBSTR(C_REGC110.DSREGC110, 0, 499)));
            V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
            V_SEQADIC    := V_SEQADIC +1;
         END LOOP;

         V_TIPOC113 := '';
         /* C113 - Documento Fiscal Referenciado*/
         FOR C_REGC113 IN (SELECT REG        ||'|'||
                                  IND_OPER   ||'|'||
                                  IND_EMIT   ||'|' AS DADOS_1,
                                  COD_MOD    ||'|'||
                                  SER        ||'|'||
                                  SUB        ||'|'||
                                  NUM_DOC    ||'|'||
                                  DT_DOC     ||'|'||
                                  CASE WHEN TO_NUMBER(P_VERSAO) >= 11 THEN CHV_DOCe ||'|' ELSE '' END AS DSREG_C113,
                                  COD_PART, PARTICIPANTE, IDTIPO, DSDESCRICAO
                             FROM (/*Nota Fiscal de Produtor Rural Referenciada*/
                                   SELECT '|C113' AS REG,
                                          '0' AS IND_OPER,
                                          DECODE(SUBSTR(NP.NRACESSONFE,7,14),REPLACE(REPLACE(REPLACE(I.NRINSJURIEST,'-',''),'.',''),'/',''),'0','1') AS IND_EMIT,
                                          'F'||NVL(F.CDFORNECED,NVL(NP.NRINSJURIEST,SUBSTR(NP.NRACESSONFE,7,14))) AS  COD_PART,
                                          NVL(NP.CDMODDOCFISC,SUBSTR(NP.NRACESSONFE,21,2)) AS COD_MOD,
                                          NVL(NP.CDSERIE,SUBSTR(NP.NRACESSONFE,23,3)) AS SER,
                                          '' AS SUB,
                                          LPAD(NVL(NP.NRNOTAFISC,SUBSTR(NP.NRACESSONFE,26,9)),9,'0') AS NUM_DOC,
                                          TO_CHAR(NP.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                          NP.NRACESSONFE AS CHV_DOCe,
                                          CASE WHEN F.CDFORNECED IS NULL THEN '' ELSE 'F'||F.CDFORNECED END AS PARTICIPANTE, 'PR' AS IDTIPO,
                                          'NOTAS FISCAIS DE PRODUTOR RURAL REFERENCIADAS A NF: ' AS DSDESCRICAO
                                     FROM NFPRODRURAL NP, FORNECEDOR F, INSCRESTAD I
                                    WHERE NP.CDFILIAL     = C_REGC100.CDFILIAL
                                      AND NP.NRLANCTONF   = C_REGC100.NRLANCTONF
                                      AND I.CDINSCESTA    = P_CDINSCESTA
                                      AND C_REGC100.CDMODDOCFISC <> '65'
                                      AND NVL(NP.NRINSJURIEST,SUBSTR(NP.NRACESSONFE,7,14)) = REPLACE(REPLACE(REPLACE(F.NRINSJURFORN(+),'-',''),'.',''),'/','')
                                    UNION ALL
                                   /*Informacao de Documentos Fiscais Referenciados*/
                                   SELECT '|C113' AS REG,
                                          DECODE(NF.IDENTRSAIDOP,'S','1','0') AS IND_OPER,
                                          DECODE(SUBSTR(IR.CDREFNFE,7,14),REPLACE(REPLACE(REPLACE(I.NRINSJURIEST,'-',''),'.',''),'/',''),'0','1') AS IND_EMIT,
                                          NVL(IR.NRINSJUREMITDOC,SUBSTR(IR.CDREFNFE,7,14)) AS  COD_PART,
                                          NVL(IR.CDMOD,SUBSTR(IR.CDREFNFE,21,2)) AS COD_MOD,
                                          NVL(IR.CDSERIE,SUBSTR(IR.CDREFNFE,23,3)) AS SER,
                                          '' AS SUB,
                                          LPAD(NVL(IR.NRNF,SUBSTR(IR.CDREFNFE,26,9)),9,'0') AS NUM_DOC,
                                          TO_CHAR(NVL(IR.DTEMISSAO,NF.DTEMISSAO),'ddmmyyyy') AS DT_DOC,
                                          IR.CDREFNFE AS CHV_DOCe,
                                          MAX((SELECT 'F'||CDFORNECED AS CDPART
                                                 FROM FORNECEDOR F
                                                WHERE NVL(IR.NRINSJUREMITDOC,SUBSTR(IR.CDREFNFE,7,14)) = REPLACE(REPLACE(REPLACE(F.NRINSJURFORN(+),'-',''),'.',''),'/','')
                                                  AND NVL(NF.IDENTRSAIDOP,'E') = 'E'
                                                UNION
                                               SELECT 'C'||CDCLIENTE AS CDPART
                                                 FROM CLIENTE C
                                                WHERE NVL(IR.NRINSJUREMITDOC,SUBSTR(IR.CDREFNFE,7,14)) = REPLACE(REPLACE(REPLACE(C.NRINSJURCLIE(+),'-',''),'.',''),'/','')
                                                  AND NVL(NF.IDENTRSAIDOP,'E') = 'S')) AS PARTICIPANTE,
                                          DECODE(V_CDTPOPCREDSTRS,C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER,'ST','DR') AS IDTIPO,
                                          'DOCUMENTOS FISCAIS REFERENCIADAS A NF: ' AS DSDESCRICAO
                                     FROM INFDOCFISCREFE IR, NOTAFISCAL NF, INSCRESTAD I
                                    WHERE IR.CDFILIAL     = C_REGC100.CDFILIAL
                                      AND IR.NRLANCTONF   = C_REGC100.NRLANCTONF
                                      AND IR.IDTPDOCREFE  IN ('refNFe','refNF')
                                      AND I.CDINSCESTA    = P_CDINSCESTA
                                      AND C_REGC100.CDMODDOCFISC <> '65'
                                      AND IR.CDREFNFE    = NF.NRACESSONFE(+)
                                    GROUP BY NF.IDENTRSAIDOP, IR.CDREFNFE, I.NRINSJURIEST, IR.NRINSJUREMITDOC, IR.CDMOD, IR.CDSERIE,
                                          IR.NRNF, IR.DTEMISSAO, NF.DTEMISSAO
                                    UNION ALL
                                   /*NFC-e Referenciada - Substituicao de Cupom*/
                                   SELECT '|C113' AS REG,
                                          '1' AS IND_OPER,
                                          '0' AS IND_EMIT,
                                          NVL(FI.NRINSJURFILI,SUBSTR(DN.NRACESSONFCENF,7,14)) AS  COD_PART,
                                          SUBSTR(DN.NRACESSONFCENF,21,2) AS COD_MOD,
                                          NVL(VE.CDSERIENFCE,SUBSTR(DN.NRACESSONFCENF,23,3)) AS SER,
                                          '' AS SUB,
                                          LPAD(NVL(VE.NRNOTAFISCALCE,SUBSTR(DN.NRACESSONFCENF,26,9)),9,'0') AS NUM_DOC,
                                          TO_CHAR(TRUNC(VE.DTEMISSAONFCE),'ddmmyyyy') AS DT_DOC,
                                          DN.NRACESSONFCENF AS CHV_DOCe,
                                          'L'||FI.CDFILIAL AS PARTICIPANTE, 'SC' AS TIPO,
                                          'NOTAS FISCAIS DE CONSUMIDOR ELETRONICAS REFERENCIADAS A NF: ' AS DSDESCRICAO
                                     FROM DADOSNF DN, VENDA VE, FILIAL FI
                                    WHERE DN.CDFILIAL       = C_REGC100.CDFILIAL
                                      AND DN.NRLANCTONF     = C_REGC100.NRLANCTONF
                                      AND SUBSTR(DN.NRACESSONFCENF,1,3) <> 'CFe'
                                      AND DN.NRACESSONFCENF = VE.NRACESSONFCE
                                      AND VE.CDFILIAL       = FI.CDFILIAL
                                      AND C_REGC100.CDMODDOCFISC <> '65'
                                    UNION ALL
                                    /*Documento com NF de Origem*/
                                   SELECT '|C113' AS REG,
                                          DECODE(NF.IDENTRSAIDOP,'S','1','0') AS IND_OPER,
                                          DECODE(NF.IDENTRSAIDOP, 'E', DECODE(NF.IDSTATUSNFE,'A','0',DECODE(NF.IDSTATUS,'E','0','1')), '0') AS IND_EMIT,
                                          NVL(SUBSTR(NF.NRACESSONFE,7,14),REPLACE(REPLACE(REPLACE(NF.NRINSJURNF,'/'),'-'),'.')) AS  COD_PART,
                                          NF.CDMODDOCFISC AS COD_MOD,
                                          NVL(SUBSTR(NF.NRACESSONFE,23,3),REPLACE(REPLACE(DECODE(NF.IDENTRSAIDOP,'E',NF.CDSERIEENTR, NF.CDSERIE),'-'),'.')) AS SER,
                                          '' AS SUB,
                                          LPAD(NVL(NF.NRNOTAFISCALELE,NF.NRNOTAFISC),9,'0') AS NUM_DOC,
                                          TO_CHAR(NF.DTEMISSAO,'DDMMYYYY') AS DT_DOC,
                                          NF.NRACESSONFE AS CHV_DOCe,
                                          NF.IDORIGDESTNF||DECODE(NF.IDORIGDESTNF, 'F', NF.CDFORNECED, DECODE(NF.IDORIGDESTNF, 'C', NF.CDCLIENTE , DECODE(NF.IDENTRSAIDOP, 'E', NF.CDFILIEMIT, NF.CDFILIRECE))) AS PARTICIPANTE, 'DO' AS IDTIPO,
                                          'DOCUMENTOS FISCAIS DE ORIGEM REFERENCIADOS A NF: ' AS DSDESCRICAO
                                     FROM ITEMNOTAFISC IT, NOTAFISCAL NF
                                    WHERE IT.CDFILIAL     = C_REGC100.CDFILIAL
                                      AND IT.NRLANCTONF   = C_REGC100.NRLANCTONF
                                      AND C_REGC100.CDMODDOCFISC <> '65'
                                      AND IT.CDFILIALIT   = NF.CDFILIAL
                                      AND IT.NRLANCTONFIT = NF.NRLANCTONF
                                      AND NF.CDEMPRESA    = P_CDEMPRESA
                                      AND NF.CDINSCESTA   = P_CDINSCESTA
                                      AND NF.CDMODDOCFISC NOT IN ('57','67')
                                      AND NF.CDTIPOOPER   <> '10')
                             ORDER BY IDTIPO) LOOP

            IF (V_TIPOC113 IS NULL) OR (V_TIPOC113 <> C_REGC113.IDTIPO) THEN
               INSERT INTO ARQMAGNETICO
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                  ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C110|'||LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0')||'|'||DECODE(C_REGC113.IDTIPO,'ST',V_CDAJCREDSTRS,C_REGC113.DSDESCRICAO||C_REGC100.NRNOTAFISC)||'|', P_NRSEQARQMAG);
               V_CONTREGC   := NVL(V_CONTREGC,0) + 1;
               V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
               VALUES
                  ('0450', V_SEQUPROD, LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0'), DECODE(C_REGC113.IDTIPO,'ST',V_CDAJCREDSTRS,C_REGC113.DSDESCRICAO||C_REGC100.NRNOTAFISC));
               V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
               V_SEQADIC    := V_SEQADIC +1;
            END IF;
            V_TIPOC113 := C_REGC113.IDTIPO;

            /* VERIFICA SE O CNPJ ESTA CADASTRADO NO SISTEMA - O REGISTRO 0150 E OBRIGATORIO */
            IF C_REGC113.PARTICIPANTE IS NOT NULL THEN
               V_PARTICIPANTE := '';
               /* CODIGO DO PARTICIPANTE */
               SELECT MAX(CDCHAVEREG)
                 INTO V_COD_PAR
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_REGC113.PARTICIPANTE
                  AND CDREGISTRO = '0150';

               IF (V_COD_PAR IS NULL) AND (C_REGC113.PARTICIPANTE IS NOT NULL) THEN
                  V_PARTICIPANTE := RETORNA_PARTICIPANTE_SPED(C_REGC113.PARTICIPANTE);
                  IF V_PARTICIPANTE IS NULL THEN
                     V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                     INSERT INTO TMP_REGDADOSSPED
                       (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                     VALUES
                       ('0150', V_SEQUPROD, C_REGC113.PARTICIPANTE);
                  END IF;
               END IF;
            END IF;

            V_PARTICIPANTE := REPLACE(REPLACE(REPLACE(REPLACE(NVL(V_PARTICIPANTE,C_REGC113.PARTICIPANTE),'.'),'-'),'/'),' ')||'|';

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC113.DADOS_1||V_PARTICIPANTE||C_REGC113.DSREG_C113, P_NRSEQARQMAG);
            V_CONTREGC   := NVL(V_CONTREGC,0) + 1;
         END LOOP;

         V_GERAC116 := 'N';
         /* C116 - Cupom Fiscal Eletronico Referenciado*/
         FOR C_REGC116 IN (SELECT REG        ||'|'||
                                  COD_MOD    ||'|'||
								          NR_SAT     ||'|'||
                                  CHV_CFE    ||'|'||
                                  NUM_CFE    ||'|'||
                                  DT_DOC     ||'|' AS DSREG_C116
                             FROM (/*NFC-e Referenciada - Substituicao de Cupom*/
                                   SELECT '|C116' AS REG,
                                          SUBSTR(DN.NRACESSONFCENF,24,2) AS COD_MOD,
                                          VE.CDSERIESAT AS NR_SAT,
                                          SUBSTR(DN.NRACESSONFCENF,4,44) AS CHV_CFE,
                                          LPAD(NVL(VE.NRNOTAFISCALCE,0),6,'0') AS NUM_CFE,
                                          TO_CHAR(VE.DTEMISSAONFCE,'DDMMYYYY') AS DT_DOC
                                     FROM DADOSNF DN, VENDA VE
                                    WHERE DN.CDFILIAL       = C_REGC100.CDFILIAL
                                      AND DN.NRLANCTONF     = C_REGC100.NRLANCTONF
                                      AND SUBSTR(DN.NRACESSONFCENF,1,3) = 'CFe'
                                      AND DN.NRACESSONFCENF = VE.NRACESSONFCE
                                      AND C_REGC100.CDMODDOCFISC <> '65')) LOOP

            IF V_GERAC116 = 'N' THEN
               INSERT INTO ARQMAGNETICO
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                  ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C110|'||LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0')||'|'||'CUPOM FISCAL ELETRONICO REFERENCIADOS A NF: ' ||C_REGC100.NRNOTAFISC||'|', P_NRSEQARQMAG);
               V_CONTREGC   := NVL(V_CONTREGC,0) + 1;
               V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
               VALUES
                  ('0450', V_SEQUPROD, LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0'), 'CUPOM FISCAL ELETRONICO REFERENCIADOS A NF: '||C_REGC100.NRNOTAFISC);
               V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
               V_SEQADIC    := V_SEQADIC +1;
            END IF;
            V_GERAC116 := 'S';

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC116.DSREG_C116, P_NRSEQARQMAG);
            V_CONTREGC   := NVL(V_CONTREGC,0) + 1;
         END LOOP;

         /*QUANDO A OPERACAO FOR A PRAZO PARA AS ENTRADAS NOS MODELOS 1 E 1A DEVER SER GERADOS OS REGISTROS REFERENTES A FATURA*/
         IF (C_REGC100.CDMODDOCFISC IN ('01','1A')) AND (C_REGC100.QTDEPARC > 1) THEN
            /*REGISTRO C140*/
            FOR C_REGC140 IN (SELECT '|C140'                                                               ||'|'||
                                     DECODE(N.IDENTRSAIDOP,'E','1','S','0')                                ||'|'||
                                     DECODE(F.IDTPTITULOCRED,'XX','',F.IDTPTITULOCRED)                     ||'|'||
                                     TRIM(DECODE(F.IDTPTITULOCRED,'99',F.DSTPTITULOCRED,''))               ||'|'||
                                     LTRIM(N.NRNOTAFISC,'0')                                               ||'|'||
                                     LPAD(RTRIM(TO_CHAR(C_REGC100.QTDEPARC)),2,'0')                        ||'|'||
                                     REPLACE(LTRIM(TO_CHAR(SUM(C.VRORIGPAG),'999999999990.00')), '.', ',') ||'|' AS DSREG_C140
                                FROM NOTAFISCAL N, FORNECEDOR F, CONTAPAGAR C
                               WHERE N.CDFILIAL   = C_REGC100.CDFILIAL
                                 AND N.NRLANCTONF = C_REGC100.NRLANCTONF
                                 AND N.CDFORNECED = F.CDFORNECED
                                 AND N.CDFILIAL   = C.CDFILIAL
                                 AND N.NRLANCTONF = C.NRLANCTONF
                               GROUP BY N.IDENTRSAIDOP, F.IDTPTITULOCRED, F.DSTPTITULOCRED,
                                     N.NRNOTAFISC, C_REGC100.QTDEPARC) LOOP

               INSERT INTO ARQMAGNETICO
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                  ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC140.DSREG_C140, P_NRSEQARQMAG);
               V_CONTREGC   := NVL(V_CONTREGC,0) + 1;
            END LOOP;

            /*REGISTRO C141*/
            FOR C_REGC141 IN (SELECT '|C141'                                                               ||'|'||
                                     LPAD(RTRIM(TO_CHAR(C.NRPARCELAPAG)),2,'0')                             ||'|'||
                                     TO_CHAR(C.DTORIGVENPAG,'DDMMYYYY')                                    ||'|'||
                                     REPLACE(LTRIM(TO_CHAR(SUM(C.VRORIGPAG),'999999999990.00')), '.', ',') ||'|' AS DSREG_C141
                                FROM CONTAPAGAR C
                               WHERE C.CDFILIAL   = C_REGC100.CDFILIAL
                                 AND C.NRLANCTONF = C_REGC100.NRLANCTONF
                               GROUP BY C.NRPARCELAPAG, C.DTORIGVENPAG
                               ORDER BY C.NRPARCELAPAG) LOOP

               INSERT INTO ARQMAGNETICO
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                  ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC141.DSREG_C141, P_NRSEQARQMAG);
               V_CONTREGC   := NVL(V_CONTREGC,0) + 1;
            END LOOP;
         END IF;

         BEGIN
            SELECT IDATUESTTPOP
              INTO V_IDATUESTTPOP
              FROM TIPOOPERAC
             WHERE CDTIPOOPER   = C_REGC100.CDTIPOOPER
               AND IDENTRSAIDOP = C_REGC100.IDENTRSAIDOP;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_IDATUESTTPOP := 'N';
         END;

         /*REGISTRO C170*/
         FOR C_REGC170 IN (SELECT COD_ITEM       || '|' ||
                                  DESCR_COMPL    || '|' ||
                                  QTD            || '|' AS PARTE1,
                                                    '|' ||
                                  VL_ITEM        || '|' ||
                                  VL_DESC        || '|' ||
                                  IND_MOV        || '|' ||
                                  CST_ICMS       || '|' ||
                                  CFOP           || '|' ||
                                  COD_NAT        || '|' || -- natureza da operacao campo 12
                                  VL_BC_ICMS     || '|' ||
                                  ALIQ_ICMS      || '|' ||
                                  VL_ICMS        || '|' ||
                                  VL_BC_ICMS_ST  || '|' || --16
                                  ALIQ_ST        || '|' || --17
                                  VL_ICMS_ST     || '|' || --18
                                  ''             || '|' || -- indicador de apurao do IPI CAMPO 19
                                  CST_IPI        || '|' || -- cst ipi campo 20
                                  '' 		       || '|' || -- Cdigo enquadramento IPI campo 21
                                  VL_BC_IPI      || '|' || --22
                                  ALIQ_IPI       || '|' || --23
                                  VL_IPI         || '|' || --24
                                  CST_PIS        || '|' || -- cst pis   campo 25
                                  VL_BC_PIS      || '|' ||
                                  ALIQ_PIS       || '|' ||
                                  ''   		    || '|' || -- quantidade base de calculo
                                  ''             || '|' || -- aliquota pis quantidade
                                  VL_PIS         || '|' ||
                                  CST_COFINS     || '|' || -- cst cofins campo 31
                                  VL_BC_COFINS   || '|' ||
                                  ALIQ_COFINS    || '|' ||
                                  ''	          || '|' || -- quantidade base de calculo
                                  ''             || '|' || -- aliquota cofins quantidade
                                  VL_COFINS      || '|' ||
                                  COD_CTA        || '|' ||
                                  CASE WHEN P_VERSAO >= '013' THEN VL_ABAT_NT || '|' ELSE '' END AS ARQ_C170,  -- codigo da conta analitica contabil deb/cred
                                  CDPRODUTO, CFOP AS CDFISCOPERIT, DSCDFISOPEIT, CDCLASFISC, COD_CTA, SGUNIDADE, VRFATOCONV, CDEXPPROD, IDPRODEST, NRSEQITPROD
                             FROM (/* NOTAS NAO ELETRONICAS COM E SEM CARGA TRIBUTARIA */
                                  SELECT DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD) AS COD_ITEM,
                                         DECODE(C_REGC100.IDENTRSAIDOP, 'E', TRIM(RETIRA_CARACTER_ESPECIAL(IT.DSPRODEMIS, 'S'))) AS DESCR_COMPL,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '999999999990.000')), '.', ',') AS QTD,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRTOTAITEM,0),2)+ROUND(NVL(VRENCARGOIMPXML,0),2), '999999999990.00')), '.', ',') AS VL_ITEM,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRENCARGODESC,0),2), '999999999990.00')), '.', ',') AS VL_DESC,
                                         DECODE(NVL(V_IDATUESTTPOP,'N'),'N','1','0') AS IND_MOV,
                                         LPAD(NVL(IP.CDSITUTRIBIT,0),3,'0') AS CST_ICMS,
                                         IP.CDFISCOPERIT AS CFOP,
                                         C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER AS COD_NAT,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                                          THEN */NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'B','I','1'),0)
                                                                          /*ELSE 0 END */+
                                                                          DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'B','C','1'),0))), 2), '999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                                          THEN */ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'A',DECODE(V_IDALIQCARGASPED,'N','I',''),'1'),0) +
                                                                               DECODE(IP.CDFISCALIT,'1', CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(IP.VRADICALIQFECP,0) ELSE 0 END,0)
                                                                          /*ELSE 0 END*/,2),'999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                                          THEN */ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'I','I','1'),0)
                                                                          /*ELSE 0 END*/ +
                                                                     CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E'
                                                                          THEN NVL(VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, '00', 'I', '1', '1'),0)
                                                                          ELSE 0 END +
                                                                     CASE WHEN (V_IDTPCARGATRIB = 'O') OR ((V_SGESTADO = 'SP') AND (C_REGC100.IDENTRSAIDOP = 'E'))
                                                                          THEN 0
                                                                          ELSE NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'I','C','1'),0) END,2), '999999999990.00')), '.', ',') AS VL_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS_ST,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1') +
                                                                     CASE WHEN V_IDSOMAFECPST = 'S' OR C_REGC100.IDENTRSAIDOP = 'E'
                                                                          THEN VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1')
                                                                          ELSE 0 END,2), '999999999990.00')), '.', ',') AS ALIQ_ST,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1') +
                                                                     CASE WHEN V_IDSOMAFECPST = 'S' OR C_REGC100.IDENTRSAIDOP = 'E'
                                                                          THEN VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1')
                                                                          ELSE 0 END,2), '999999999990.00')), '.', ',') AS VL_ICMS_ST,
                                         (SELECT MAX(CDSITUTRIBITNFE)
                                            FROM ITEMPROIMPOS N, IMPOSTO IM
                                           WHERE IP.CDFILIAL     = N.CDFILIAL
                                             AND IP.NRLANCTONF   = N.NRLANCTONF
                                             AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                             AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                             AND N.NRITPROIMPOS  = '1'
                                             AND IM.CDIMPOSTONFE = V_IPI)  AS CST_IPI,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_IPI,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_IPI,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_IPI,
                                         (SELECT MAX(CDSITUTRIBITNFE)
                                            FROM ITEMPROIMPOS N, IMPOSTO IM
                                           WHERE IP.CDFILIAL     = N.CDFILIAL
                                             AND IP.NRLANCTONF   = N.NRLANCTONF
                                             AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                             AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                             AND N.NRITPROIMPOS  = '1'
                                             AND IM.CDIMPOSTONFE = V_PIS) AS CST_PIS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_PIS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_PIS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,
                                         (SELECT MAX(CDSITUTRIBITNFE)
                                            FROM ITEMPROIMPOS N, IMPOSTO IM
                                           WHERE IP.CDFILIAL     = N.CDFILIAL
                                             AND IP.NRLANCTONF   = N.NRLANCTONF
                                             AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                             AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                             AND N.NRITPROIMPOS  = '1'
                                             AND IM.CDIMPOSTONFE = V_COFINS) AS CST_COFINS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_COFINS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_COFINS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                         CONTCTBL_SPED(C_REGC100.IDENTRSAIDOP, DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO), C_REGC100.CDTIPOOPER, C_REGC100.FILIAL) AS COD_CTA,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.IDIMPDESONERAIT,'S',NVL(IP.VRIMPOSTOIT+CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(IP.VRADICFECP,0) ELSE 0 END,0),0),2), '999999999990.00')), '.', ',') AS VL_ABAT_NT,
                                         DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO) AS CDPRODUTO, IT.DSCDFISOPEIT, IT.CDPRODNCMTRANS AS CDCLASFISC, PR.SGUNIDADE, PR.VRFATOCONV,
                                         '' AS CDEXPPROD, DECODE(PA.CDPRODUTO,PR.CDPRODUTO,'S','N') AS IDPRODEST, IT.NRSEQITPROD
                                    FROM ITEMPRODUTO IT, PRODUTO PR, PRODUTO PA, ITEMPROIMPOS IP, IMPOSTO IM, DETITMAPARECUP DT
                                   WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                                     AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                     AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                     AND IM.CDIMPOSTO     = IP.CDIMPOSTO
                                     AND IT.CDFILIAL      = IP.CDFILIAL
                                     AND IT.NRLANCTONF    = IP.NRLANCTONF
                                     AND IT.NRSEQITPROD   = IP.NRSEQITPROD
                                     AND IT.CDPRODUTO     = PR.CDPRODUTO
                                     AND PR.CDPRODESTO    = PA.CDPRODUTO(+)
                                     AND IP.NRITPROIMPOS  =  '1'
                                     AND C_REGC100.CDMODDOCFISC NOT IN ('55','65')
                                     AND (ROUND(IT.QTITEMPROD,3) > 0 OR C_REGC100.SITU_DOC IN ('06','07','08'))
                                     AND IP.CDFILIAL      = DT.CDFILIAL(+)
                                     AND IP.NRLANCTONF    = DT.NRLANCTONF(+)
                                     AND IP.CDIMPOSTO     = DT.CDIMPOSTO(+)
                                     AND IP.NRSEQITPROD   = DT.NRSEQITPROD(+)
                                     AND IP.NRITPROIMPOS  = DT.NRITPROIMPOS(+)
                                   UNION ALL
                                  /*NOTAS DE ENTRADA ELETRONICAS COM E SEM CARGA TRIBUTARIA */
                                  SELECT DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD) AS COD_ITEM,
                                         DECODE(C_REGC100.IDENTRSAIDOP, 'E', TRIM(RETIRA_CARACTER_ESPECIAL(IT.DSPRODEMIS, 'S'))) AS DESCR_COMPL,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '999999999990.000')), '.', ',') AS QTD,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRTOTAITEM,0),2)+ROUND(NVL(VRENCARGOIMPXML,0),2), '999999999990.00')), '.', ',') AS VL_ITEM,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRENCARGODESC,0),2), '999999999990.00')), '.', ',') AS VL_DESC,
                                         DECODE(NVL(V_IDATUESTTPOP,'N'),'N','1','0') AS IND_MOV,
                                         LPAD(NVL(IP.CDSITUTRIBIT,0),3,'0') AS CST_ICMS,
                                         IP.CDFISCOPERIT AS CFOP,
                                         C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER AS COD_NAT,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                                          THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'B','I','1'),0)
                                                                         /* ELSE 0 END*/ +
                                                                     DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'B','C','1'),0))),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                                          THEN */NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'A',DECODE(V_IDALIQCARGASPED,'N','I',''),'1'),0) + DECODE(IP.CDFISCALIT,'1',NVL(IP.VRADICALIQFECP,0),0)
                                                                         /* ELSE 0 END*/,2),'999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                                          THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'I','I','1'),0)
                                                                         /* ELSE 0 END */+
                                                                     NVL(VALOR_FECP_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'00','I','1','1'),0) +
                                                                     DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'I','C','1'),0))),2), '999999999990.00')), '.', ',') AS VL_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS_ST,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1') + VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_ST,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1') + VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_ICMS_ST,
                                         (SELECT MAX(CDSITUTRIBITNFE)
                                            FROM ITEMPROIMPOS N, IMPOSTO IM
                                           WHERE IP.CDFILIAL     = N.CDFILIAL
                                             AND IP.NRLANCTONF   = N.NRLANCTONF
                                             AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                             AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                             AND N.NRITPROIMPOS  = '1'
                                             AND IM.CDIMPOSTONFE = V_IPI) AS CST_IPI,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_IPI,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_IPI,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_IPI,
                                         (SELECT MAX(CDSITUTRIBITNFE)
                                            FROM ITEMPROIMPOS N, IMPOSTO IM
                                           WHERE IP.CDFILIAL     = N.CDFILIAL
                                             AND IP.NRLANCTONF   = N.NRLANCTONF
                                             AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                             AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                             AND N.NRITPROIMPOS  = '1'
                                             AND IM.CDIMPOSTONFE = V_PIS) AS CST_PIS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_PIS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_PIS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,
                                         (SELECT MAX(CDSITUTRIBITNFE)
                                            FROM ITEMPROIMPOS N, IMPOSTO IM
                                           WHERE IP.CDFILIAL     = N.CDFILIAL
                                             AND IP.NRLANCTONF   = N.NRLANCTONF
                                             AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                             AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                             AND N.NRITPROIMPOS  = '1'
                                             AND IM.CDIMPOSTONFE = V_COFINS) AS CST_COFINS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_COFINS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_COFINS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                         CONTCTBL_SPED(C_REGC100.IDENTRSAIDOP, DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO), C_REGC100.CDTIPOOPER, C_REGC100.FILIAL) AS COD_CTA,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.IDIMPDESONERAIT,'S',NVL(IP.VRIMPOSTOIT+NVL(IP.VRADICFECP,0),0),0),2), '999999999990.00')), '.', ',') AS VL_ABAT_NT,
                                         DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO) AS CDPRODUTO, IT.DSCDFISOPEIT, IT.CDPRODNCMTRANS AS CDCLASFISC, PR.SGUNIDADE, PR.VRFATOCONV,
                                         (SELECT CP.CDEXPPROD FROM CODEXPPROD CP WHERE CP.CDLIVROMAG = P_LIVROMAG AND CP.CDPRODUTO = IT.CDPRODUTO) AS CDEXPPROD,
                                         DECODE(PA.CDPRODUTO,PR.CDPRODUTO,'S','N') AS IDPRODEST, IT.NRSEQITPROD
                                    FROM ITEMPRODUTO IT, PRODUTO PR, PRODUTO PA, ITEMPROIMPOS IP, IMPOSTO IM, DETITMAPARECUP DT
                                   WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                                     AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                     AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                     AND IM.CDIMPOSTO     = IP.CDIMPOSTO
                                     AND IT.CDFILIAL      = IP.CDFILIAL
                                     AND IT.NRLANCTONF    = IP.NRLANCTONF
                                     AND IT.NRSEQITPROD   = IP.NRSEQITPROD
                                     AND IT.CDPRODUTO     = PR.CDPRODUTO
                                     AND PR.CDPRODESTO    = PA.CDPRODUTO(+)
                                     AND IP.NRITPROIMPOS  =  '1'
                                     AND C_REGC100.IDENTRSAIDOP = 'E'
                                     AND C_REGC100.CDMODDOCFISC = '55'
                                     AND C_REGC100.IDSTATUSNFE <> 'A'
                                     AND (ROUND(IT.QTITEMPROD,3) > 0 OR C_REGC100.SITU_DOC IN ('06','07','08'))
                                     AND IP.CDFILIAL      = DT.CDFILIAL(+)
                                     AND IP.NRLANCTONF    = DT.NRLANCTONF(+)
                                     AND IP.CDIMPOSTO     = DT.CDIMPOSTO(+)
                                     AND IP.NRSEQITPROD   = DT.NRSEQITPROD(+)
                                     AND IP.NRITPROIMPOS  = DT.NRITPROIMPOS(+)
                                   UNION ALL
                                   /*NOTAS DE SAIDAS ELETRONICAS COM E SEM CARGA TRIBUTARIA - COM CODIGO DE INFORMACAO ADICIONAL PARA O ESTADO DE PE */
                                  SELECT DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD) AS COD_ITEM,
                                         ' ' AS DESCR_COMPL,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '999999999990.000')), '.', ',') AS QTD,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRTOTAITEM,0),2)+ROUND(NVL(VRENCARGOIMPXML,0),2), '999999999990.00')), '.', ',') AS VL_ITEM,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRENCARGODESC,0),2), '999999999990.00')), '.', ',') AS VL_DESC,
                                         DECODE(NVL(V_IDATUESTTPOP,'N'),'N','1','0') AS IND_MOV,
                                         LPAD(NVL(IP.CDSITUTRIBIT,0),3,'0') AS CST_ICMS,
                                         IP.CDFISCOPERIT AS CFOP,
                                         C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER AS COD_NAT,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                                          THEN */ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'B','I','1'),0)
                                                                         /* ELSE 0 END */+
                                                                     DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'B','C','1'),0))),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                                          THEN */ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'A',DECODE(V_IDALIQCARGASPED,'N','I',''),'1'),0) + DECODE(IP.CDFISCALIT,'1',DECODE(V_IDSOMAFECP,'S',NVL(IP.VRADICALIQFECP,0),0),0)
                                                                         /* ELSE 0 END*/,2),'999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                                          THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'I','I','1'),0)
                                                                         /* ELSE 0 END */+
                                                                     DECODE(V_IDSOMAFECP,'S',NVL(VALOR_FECP_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'00','I','1','1'),0),0) +
                                                                     DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'I','C','1'),0))),2), '999999999990.00')), '.', ',') AS VL_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS_ST,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1') + DECODE(V_IDSOMAFECPST,'S',VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1'),0),2), '999999999990.00')), '.', ',') AS ALIQ_ST,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1') + DECODE(V_IDSOMAFECPST,'S',VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1'),0),2), '999999999990.00')), '.', ',') AS VL_ICMS_ST,
                                         (SELECT MAX(CDSITUTRIBITNFE)
                                            FROM ITEMPROIMPOS N, IMPOSTO IM
                                           WHERE IP.CDFILIAL     = N.CDFILIAL
                                             AND IP.NRLANCTONF   = N.NRLANCTONF
                                             AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                             AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                             AND N.NRITPROIMPOS  = '1'
                                             AND IM.CDIMPOSTONFE = V_IPI) AS CST_IPI,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_IPI,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_IPI,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_IPI,
                                         (SELECT MAX(CDSITUTRIBITNFE)
                                            FROM ITEMPROIMPOS N, IMPOSTO IM
                                           WHERE IP.CDFILIAL     = N.CDFILIAL
                                             AND IP.NRLANCTONF   = N.NRLANCTONF
                                             AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                             AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                             AND N.NRITPROIMPOS  = '1'
                                             AND IM.CDIMPOSTONFE = V_PIS) AS CST_PIS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_PIS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_PIS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,
                                         (SELECT MAX(CDSITUTRIBITNFE)
                                            FROM ITEMPROIMPOS N, IMPOSTO IM
                                           WHERE IP.CDFILIAL     = N.CDFILIAL
                                             AND IP.NRLANCTONF   = N.NRLANCTONF
                                             AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                             AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                             AND N.NRITPROIMPOS  = '1'
                                             AND IM.CDIMPOSTONFE = V_COFINS) AS CST_COFINS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_COFINS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_COFINS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                         CONTCTBL_SPED(C_REGC100.IDENTRSAIDOP, DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO), C_REGC100.CDTIPOOPER, C_REGC100.FILIAL) AS COD_CTA,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.IDIMPDESONERAIT,'S',NVL(IP.VRIMPOSTOIT,0)+DECODE(V_IDSOMAFECP,'S',NVL(IP.VRADICFECP,0),0),0),2), '999999999990.00')), '.', ',') AS VL_ABAT_NT,
                                         DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO) AS CDPRODUTO, IT.DSCDFISOPEIT, IT.CDPRODNCMTRANS AS CDCLASFISC, PR.SGUNIDADE, PR.VRFATOCONV,
                                         CP.CDEXPPROD, DECODE(PA.CDPRODUTO,PR.CDPRODUTO,'S','N') AS IDPRODEST, IT.NRSEQITPROD
                                    FROM ITEMPRODUTO IT, PRODUTO PR, PRODUTO PA, ITEMPROIMPOS IP, IMPOSTO IM, DETITMAPARECUP DT, CODEXPPROD CP
                                   WHERE V_SGESTADO       = 'PE'
                                     AND IT.CDFILIAL      = C_REGC100.CDFILIAL
                                     AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                     AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                     AND IM.CDIMPOSTO     = IP.CDIMPOSTO
                                     AND IT.CDFILIAL      = IP.CDFILIAL
                                     AND IT.NRLANCTONF    = IP.NRLANCTONF
                                     AND IT.NRSEQITPROD   = IP.NRSEQITPROD
                                     AND IT.CDPRODUTO     = PR.CDPRODUTO
                                     AND PR.CDPRODESTO    = PA.CDPRODUTO(+)
                                     AND IP.NRITPROIMPOS  =  '1'
                                     AND C_REGC100.IDENTRSAIDOP = 'S'
                                     AND C_REGC100.CDMODDOCFISC = '55'
                                     AND (ROUND(IT.QTITEMPROD,3) > 0 OR C_REGC100.SITU_DOC IN ('06','07','08'))
                                     AND IP.CDFILIAL      = DT.CDFILIAL(+)
                                     AND IP.NRLANCTONF    = DT.NRLANCTONF(+)
                                     AND IP.CDIMPOSTO     = DT.CDIMPOSTO(+)
                                     AND IP.NRSEQITPROD   = DT.NRSEQITPROD(+)
                                     AND IP.NRITPROIMPOS  = DT.NRITPROIMPOS(+)
                                     AND CP.CDLIVROMAG    = P_LIVROMAG
                                     AND CP.CDPRODUTO     = IT.CDPRODUTO
                                     AND CP.CDEXPPROD     IS NOT NULL)
                                   ORDER BY NRSEQITPROD) LOOP

            /* CODIGO DA NATUREZA DA OPERACAO */
            SELECT MAX(CDCHAVEREG)
              INTO V_COD_NAT
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER
               AND CDREGISTRO = '0400';

            IF (V_COD_NAT IS NULL) AND (C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1, CDCAMPO2)
               VALUES
                 ('0400', V_SEQUPROD, C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER,C_REGC100.IDENTRSAIDOP,C_REGC100.CDTIPOOPER);
            END IF;
            V_COD_NAT := NULL;

            V_UNIDADE := C_REGC170.SGUNIDADE||CASE WHEN C_REGC170.IDPRODEST = 'N' THEN CONT_FATCONVERSAO_SPED(C_REGC170.SGUNIDADE, C_REGC170.VRFATOCONV) ELSE '' END;

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

            IF (V_UNID IS NULL) AND (V_UNIDADE IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCAMPO1, CDCAMPO2, CDCAMPO3, CDCHAVEREG)
               VALUES
                 ('0190', V_SEQUPROD, C_REGC170.SGUNIDADE, C_REGC170.VRFATOCONV, SUBSTR(V_UNIDADE,LENGTH(C_REGC170.SGUNIDADE)+1,LENGTH(V_UNIDADE)), V_UNIDADE);
            END IF;
            V_UNID := NULL;

            /* CODIGO DO PRODUTO */
            SELECT MAX(CDCHAVEREG)
              INTO V_PRODUTO
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_REGC170.CDPRODUTO
               AND CDREGISTRO = '0200';
            IF (V_PRODUTO IS NULL) AND (C_REGC170.CDPRODUTO IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3)
               VALUES
                 ('0200', V_SEQUPROD, C_REGC170.CDPRODUTO, C_REGC170.CDCLASFISC);
            END IF;
            V_PRODUTO := NULL;

            /* PLANO DE CONTAS REFERENCIAL */
            SELECT MAX(CDCHAVEREG)
              INTO V_CONTCTBL
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_REGC170.COD_CTA
               AND CDREGISTRO = '0500';

            IF (V_CONTCTBL IS NULL) AND (C_REGC170.COD_CTA IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
               VALUES
                  ('0500', V_SEQUPROD, C_REGC170.COD_CTA);
            END IF;
            V_CONTCTBL := NULL;

            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C170'|| '|' ||V_SEQUENCIAL|| '|' || C_REGC170.PARTE1 ||V_UNIDADE||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;

            IF (V_SGESTADO = 'PE') AND (C_REGC170.CDEXPPROD IS NOT NULL) AND (P_VERSAO >= '013') THEN
               INSERT INTO ARQMAGNETICO
                  (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                  ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C177'|| '|' || C_REGC170.CDEXPPROD || '|', P_NRSEQARQMAG);
               V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
            END IF;

            IF P_IDINFCOMPLST = 'S' THEN
               /*REGISTRO C180: INFORMACOES COMPLEMENTARES DAS OPERACOES DE SAIDA DE MERCADORIAS SUJEITAS A SUBSTITUICAO TRIBUTARIA */
               IF (C_REGC100.CDMODDOCFISC IN ('01','1B','04','55','65')) AND (C_REGC100.IDENTRSAIDOP = 'E') THEN
                  FOR C_REGC180 IN (SELECT '|C180'                 ||'|'||
                                             COD_RESP_RET            ||'|'||
                                             QUANT_CONV              ||'|'||
                                             UNID                    ||'|'||
                                             VL_UNIT_CONV            ||'|'||
                                             VL_UNIT_ICMS_OP_CONV    ||'|'||
                                             VL_UNIT_BC_ICMS_ST_CONV ||'|'||
                                             VL_UNIT_ICMS_ST_CONV    ||'|'||
                                             VL_UNIT_FCP_ST_CONV     ||'|'||
                                             COD_DA                  ||'|'||
                                             NUM_DA                  ||'|'  AS ARQ_C180
                                        FROM (SELECT DECODE(NVL(MR.IDSUBSTSOLIDST,'N'),'S','3',CASE WHEN IP.CDSITUTRIBIT IN ('10','30','70') THEN '1' WHEN IP.CDSITUTRIBIT = '60' THEN '3' END) AS COD_RESP_RET,
                                                     REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3), '999999999990.000')), '.', ',') AS QUANT_CONV,
                                                     DECODE(P_IDCONSPRODEST,'S',NVL(PA.SGUNIDADE,PR.SGUNIDADE),PR.SGUNIDADE) AS UNID,
                                                     REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRTOTAITEM,0),2)+ROUND(NVL(VRENCARGOIMPXML,0),2), '999999999990.00')), '.', ',') AS VL_UNIT_CONV,
                                                     REPLACE(LTRIM(TO_CHAR(ROUND((VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'05','I','1') / ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3)),2), '999999999990.00')), '.', ',') AS VL_UNIT_ICMS_OP_CONV,
                                                     REPLACE(LTRIM(TO_CHAR(ROUND((VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'06','B','1') / ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3)),2) , '999999999990.00')), '.', ',') AS VL_UNIT_BC_ICMS_ST_CONV,
                                                     REPLACE(LTRIM(TO_CHAR(ROUND(((VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'06','I','1') + NVL(VALOR_FECP_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'06','I','1'),0)) / ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3)),2), '999999999990.00')), '.', ',') AS VL_UNIT_ICMS_ST_CONV,
                                                     REPLACE(LTRIM(TO_CHAR(ROUND((NVL(VALOR_FECP_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'06','I','1'),0) / ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3)),2), '999999999990.00')), '.', ',') AS VL_UNIT_FCP_ST_CONV,
                                                     C_REGC100.COD_DA, C_REGC100.NUM_DA
                                                FROM ITEMPRODUTO IT, PRODUTO PR, PRODUTO PA, ITEMPROIMPOS IP, IMPOSTO IM, ITMAPARECUP MR
                                               WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                                                 AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                                 AND IT.NRSEQITPROD   = C_REGC170.NRSEQITPROD
                                                 AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                                 AND IM.CDIMPOSTO     = IP.CDIMPOSTO
                                                 AND IT.CDFILIAL      = IP.CDFILIAL
                                                 AND IT.NRLANCTONF    = IP.NRLANCTONF
                                                 AND IT.NRSEQITPROD   = IP.NRSEQITPROD
                                                 AND IT.CDPRODUTO     = PR.CDPRODUTO
                                                 AND PR.CDPRODESTO    = PA.CDPRODUTO(+)
                                                 AND IP.NRITPROIMPOS  =  '1'
                                                 AND C_REGC100.CDMODDOCFISC IN ('01','1B','04','55')
                                                 AND IP.CDFILIAL      = MR.CDFILIAL(+)
                                                 AND IP.NRLANCTONF    = MR.NRLANCTONF(+)
                                                 AND V_NRSEQUMAPAPU   = MR.NRSEQUMAPAPU(+)
                                                 AND EXISTS (SELECT I.CDFILIAL, I.NRLANCTONF
                                                               FROM ITEMPROIMPOS I, IMPOSTO M
                                                              WHERE I.CDFILIAL    = IT.CDFILIAL
                                                                AND I.NRLANCTONF  = IT.NRLANCTONF
                                                                AND I.NRSEQITPROD = IT.NRSEQITPROD
                                                                AND I.CDIMPOSTO   = M.CDIMPOSTO
                                                                AND M.CDIMPOSTONFE IN ('05','06')))) LOOP
                     INSERT INTO ARQMAGNETICO
                        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                     VALUES
                        ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC180.ARQ_C180, P_NRSEQARQMAG);
                     V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                  END LOOP;
               END IF;
            END IF;
         END LOOP;

         IF P_IDINFCOMPLST = 'S' THEN
            /*REGISTRO C180: INFORMACOES COMPLEMENTARES DAS OPERACOES DE SAIDA DE MERCADORIAS SUJEITAS A SUBSTITUICAO TRIBUTARIA */
            IF (C_REGC100.CDMODDOCFISC IN ('01','1B','04','55','65')) AND (C_REGC100.IDENTRSAIDOP = 'S') THEN
                FOR C_REGC185 IN (SELECT '|C185'                                                                                ||'|'||
                                         NUM_ITEM                                                                               ||'|'||
                                         COD_ITEM                                                                               ||'|'||
                                         CST_ICMS                                                                               ||'|'||
                                         CFOP                                                                                   ||'|'||
                                         CASE WHEN VL_UNIT_ICMS_ST_CONV_COMPL > 0 THEN COD_MOT_COMPL
                                              WHEN VL_UNIT_ICMS_ST_CONV_REST  > 0 THEN COD_MOT_REST
                                              ELSE NVL(COD_MOT_COMPL,COD_MOT_REST) END                                          ||'|'||
                                         REPLACE(LTRIM(TO_CHAR(QUANT_CONV, '999999999990.000')), '.', ',')                      ||'|'||
                                         UNID                                                                                   ||'|'||
                                         REPLACE(LTRIM(TO_CHAR(VL_UNIT_CONV, '999999999990.00')), '.', ',')                     ||'|'||
                                         REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_NA_OPERACAO_CONV, '999999999990.00')), '.', ',')    ||'|'||
                                         REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_OP_CONV, '999999999990.00')), '.', ',')             ||'|'||
                                         REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_OP_ESTOQUE_CONV, '999999999990.00')), '.', ',')     ||'|'||
                                         REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_ST_ESTOQUE_CONV, '999999999990.00')), '.', ',')     ||'|'||
                                         REPLACE(LTRIM(TO_CHAR(VL_UNIT_FCP_ICMS_ST_ESTOQUE_C, '999999999990.00')), '.', ',')    ||'|'||
                                         REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_REST > 0
                                                                    THEN VL_UNIT_ICMS_ST_CONV_REST ELSE 0 END, '999999999990.000')), '.', ',')  ||'|'|| /*VL_UNIT_ICMS_ST_CONV_REST*/
                                         REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_REST > 0
                                                                    THEN (VL_UNIT_FCP_ICMS_ST_ESTOQUE_C/DECODE(VL_UNIT_ICMS_ST_ESTOQUE_CONV,0,1,VL_UNIT_ICMS_ST_ESTOQUE_CONV)) * VL_UNIT_ICMS_ST_CONV_REST
                                                                    ELSE 0 END , '999999999990.000')), '.', ',') ||'|'|| /*VL_UNIT_FCP_ST_CONV_REST*/
                                         REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_COMPL > 0
                                                                    THEN VL_UNIT_ICMS_ST_CONV_COMPL ELSE 0 END, '999999999990.000')), '.', ',') ||'|'|| /*VL_UNIT_ICMS_ST_CONV_COMPL*/
                                         REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_REST > 0
                                                                    THEN (VL_UNIT_FCP_ICMS_ST_ESTOQUE_C/DECODE(VL_UNIT_ICMS_ST_ESTOQUE_CONV,0,1,VL_UNIT_ICMS_ST_ESTOQUE_CONV)) * VL_UNIT_ICMS_ST_CONV_COMPL
                                                                    ELSE 0 END , '999999999990.000')), '.', ',') ||'|'  AS ARQ_C185 /*VL_UNIT_FCP_ST_CONV_COMP */
                                    FROM (SELECT NUM_ITEM, COD_ITEM, CST_ICMS, CFOP, COD_MOT_COMPL, COD_MOT_REST,
                                                 QUANT_CONV, UNID, VL_UNIT_CONV, VL_UNIT_ICMS_NA_OPERACAO_CONV,
                                                 DECODE(P_IDGERACAMPO11ST,'S',VL_UNIT_ICMS_OP_CONV,0) AS VL_UNIT_ICMS_OP_CONV,
                                                 VL_UNIT_ICMS_OP_ESTOQUE_CONV, VL_UNIT_ICMS_ST_ESTOQUE_CONV, VL_UNIT_FCP_ICMS_ST_ESTOQUE_C,
                                                 CASE WHEN SUBSTR(COD_MOT_REST,3,1) = '1' THEN VL_UNIT_ICMS_OP_ESTOQUE_CONV + VL_UNIT_ICMS_ST_ESTOQUE_CONV - VL_UNIT_ICMS_NA_OPERACAO_CONV
                                                      WHEN SUBSTR(COD_MOT_REST,3,1) = '2' THEN VL_UNIT_ICMS_OP_ESTOQUE_CONV + VL_UNIT_ICMS_ST_ESTOQUE_CONV - VL_UNIT_ICMS_OP_CONV
                                                      ELSE 0 END AS VL_UNIT_ICMS_ST_CONV_REST,
                                                 (VL_UNIT_ICMS_NA_OPERACAO_CONV - VL_UNIT_ICMS_OP_ESTOQUE_CONV - VL_UNIT_ICMS_ST_ESTOQUE_CONV) AS VL_UNIT_ICMS_ST_CONV_COMPL
                                            FROM (SELECT IP.NRSEQITPROD AS NUM_ITEM,
                                                 DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD) AS COD_ITEM,
                                                 IP.CDSITUTRIBIT AS CST_ICMS,
                                                 IP.CDFISCOPERIT AS CFOP,
                                                 IP.CDCOMPST AS COD_MOT_COMPL, IP.CDRESTST AS COD_MOT_REST,
                                                 ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3) AS QUANT_CONV,
                                                 DECODE(P_IDCONSPRODEST,'S',NVL(PA.SGUNIDADE,PR.SGUNIDADE),PR.SGUNIDADE) AS UNID,
                                                 ROUND(NVL(IT.VRTOTAITEM,0),2)+ROUND(NVL(VRENCARGOIMPXML,0),2) AS VL_UNIT_CONV,
                                                 ROUND((VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'05','I','1') / ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3)),2)  AS VL_UNIT_ICMS_NA_OPERACAO_CONV,
                                                 ROUND(IP.VRIMPITENTRUNIT,2) AS VL_UNIT_ICMS_OP_CONV,
                                                 '0,00' AS VL_UNIT_ICMS_OP_ESTOQUE_CONV,
                                                 '0,00' AS VL_UNIT_ICMS_ST_ESTOQUE_CONV,
                                                 '0,00' AS VL_UNIT_FCP_ICMS_ST_ESTOQUE_C
                                            FROM ITEMPRODUTO IT, PRODUTO PR, PRODUTO PA, ITEMPROIMPOS IP, IMPOSTO IM, ITMAPARECUP MR
                                           WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                                             AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                             AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                             AND IM.CDIMPOSTO     = IP.CDIMPOSTO
                                             AND IT.CDFILIAL      = IP.CDFILIAL
                                             AND IT.NRLANCTONF    = IP.NRLANCTONF
                                             AND IT.NRSEQITPROD   = IP.NRSEQITPROD
                                             AND IT.CDPRODUTO     = PR.CDPRODUTO
                                             AND PR.CDPRODESTO    = PA.CDPRODUTO(+)
                                             AND IP.NRITPROIMPOS  =  '1'
                                             AND C_REGC100.CDMODDOCFISC IN ('01','1B','04','55')
                                             AND IP.CDFILIAL      = MR.CDFILIAL(+)
                                             AND IP.NRLANCTONF    = MR.NRLANCTONF(+)
                                             AND V_NRSEQUMAPAPU   = MR.NRSEQUMAPAPU(+)
                                             AND EXISTS (SELECT I.CDFILIAL, I.NRLANCTONF
                                                           FROM ITEMPROIMPOS I, IMPOSTO M
                                                          WHERE I.CDFILIAL    = IT.CDFILIAL
                                                            AND I.NRLANCTONF  = IT.NRLANCTONF
                                                            AND I.NRSEQITPROD = IT.NRSEQITPROD
                                                            AND I.CDIMPOSTO   = M.CDIMPOSTO
                                                            AND M.CDIMPOSTONFE IN ('05'))))) LOOP
                  INSERT INTO ARQMAGNETICO
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                  VALUES
                     ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC185.ARQ_C185, P_NRSEQARQMAG);
                  V_CONTREGC := NVL(V_CONTREGC,0) + 1;
               END LOOP;
            END IF;
         END IF;

         IF P_VERSAO NOT IN ( '001', '002') THEN
            /*REGISTRO C190*/
            /* PASSADO O CSOSN APENAS QUANDO A EMPRESA E DO SIMPLES E O FORNECEDOR TAMBEM, CASO A EMPRESA NAO SEJA DO SIMPLES, MAIS O FORNECEDOR
            SIM, MANDA O CST MESMO. NESTE CASO A EMPRESA NAO E SIMPLES */
            FOR C_REGC190 IN(SELECT ('|C190'      || '|' ||
                                     T.CST_ICMS   || '|' ||
                                     T.CFOP       || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(NVL(T.ALIQ_ICMS,0),2), '999999999990.00')), '.', ',') || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(NVL(T.VL_OPR,0),2),    '999999999990.00')), '.', ',') || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(NVL(T.VL_BC_ICMS,0),2),'999999999990.00')), '.', ',') || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(NVL(T.VL_ICMS,0),2),   '999999999990.00')), '.', ',') || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(NVL(T.BC_ST,0),2),     '999999999990.00')), '.', ',') || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(NVL(T.IMP_ST,0),2),    '999999999990.00')), '.', ',') || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(NVL(T.VL_RED_BC,0),2), '999999999990.00')), '.', ',') || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(NVL(VL_IPI,0),2),      '999999999990.00')), '.', ',') || '|' ||
                                     T.COD_OBS || '|') ARQ_C190,
                                     T.VL_ICMS, T.CFOP AS COD_NAT, T.CST_ICMS, T.ALIQ_ICMS, T.COD_OBS, T.DSCFOPLMAG
                               FROM (/*NOTAS DE SAIDA COM E SEM CARGA TRIBUTARIA*/
                                     SELECT I.CDSITUTRIBIT AS CST_ICMS,
                                            I.CDFISCOPERIT AS CFOP,
                                            /*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                 THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'A',DECODE(V_IDALIQCARGASPED,'N','I',''),'1'),0) + DECODE(V_IDSOMAFECP,'S',DECODE(I.CDFISCALIT,'1',NVL(I.VRADICALIQFECP,0),0),0)
                                                /* ELSE 0 END */ AS ALIQ_ICMS,
                                            SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0) VL_OPR,
                                            SUM(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                     THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'B','I','1'),0) +
                                                          DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'B','C','1'),0)))
                                                     /*ELSE 0 END*/) AS VL_BC_ICMS,
                                            SUM(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                     THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'I','I','1'),0) +
                                                          DECODE(V_IDSOMAFECP,'S',NVL(VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'00','I','1','1'),0),0) +
                                                          DECODE(V_IDTPCARGATRIB,'O',0,NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'I','C','1'),0))
                                                     /*ELSE 0 END*/) AS VL_ICMS,
                                            NVL(SUM(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                         THEN*/ DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL),0)
                                                         /*ELSE 0 END*/),0) AS VL_RED_BC,
                                            SUM((SELECT NVL(SUM(VRIMPOSTOIT),0)
                                                   FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                  WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                        (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                        (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                        (IT.NRITPROIMPOS = '1'                  ) AND
                                                        (IT.CDFISCALIT   = '1'                  ) AND
                                                        (IM.CDIMPOSTONFE = '10'                 ) AND
                                                        I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                            C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                            SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                            SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + DECODE(V_IDSOMAFECPST,'S',VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1'),0)) AS IMP_ST,
                                            C.DSCFOPLMAG
                                       FROM ITEMPROIMPOS I, ITEMPRODUTO P, IMPOSTO IM, DETITMAPARECUP DT, CFOPLIVROMAGTPOP C
                                      WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                        AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                        AND C_REGC100.IDENTRSAIDOP = 'S'
                                        AND C_REGC100.CDMODDOCFISC <> '65'
                                        AND I.NRITPROIMPOS  = '1'
                                        AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                        AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                        AND I.CDFILIAL      = DT.CDFILIAL(+)
                                        AND I.NRLANCTONF    = DT.NRLANCTONF(+)
                                        AND I.CDIMPOSTO     = DT.CDIMPOSTO(+)
                                        AND I.NRSEQITPROD   = DT.NRSEQITPROD(+)
                                        AND I.NRITPROIMPOS  = DT.NRITPROIMPOS(+)
                                        AND I.CDFILIAL      = P.CDFILIAL
                                        AND I.NRLANCTONF    = P.NRLANCTONF
                                        AND I.NRSEQITPROD   = P.NRSEQITPROD
                                        AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                        AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                        AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                        AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                        AND (ROUND(P.QTITEMPROD,3) > 0 OR C_REGC100.SITU_DOC IN ('06','07','08'))
                                      GROUP BY I.CDSITUTRIBIT, I.CDFISCOPERIT, C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG,
                                               /*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                    THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'A',DECODE(V_IDALIQCARGASPED,'N','I',''),'1'),0) + DECODE(V_IDSOMAFECP,'S',DECODE(I.CDFISCALIT,'1',NVL(I.VRADICALIQFECP,0),0),0)
                                                    /*ELSE 0 END*/
                                      UNION ALL
                                     /*NOTAS DE ENTRADA COM E SEM CARGA TRIBUTARIA*/
                                     SELECT I.CDSITUTRIBIT AS CST_ICMS,
                                            I.CDFISCOPERIT AS CFOP,
                                            /*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                 THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'A',DECODE(V_IDALIQCARGASPED,'N','I',''),'1'),0) + DECODE(I.CDFISCALIT,'1',NVL(I.VRADICALIQFECP,0),0)
                                                 /*ELSE 0 END*/ AS ALIQ_ICMS,
                                            SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0) VL_OPR,
                                            SUM(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                     THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'B','I','1'),0) +
                                                          DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'B','C','1'),0)))
                                                     /*ELSE 0 END*/) AS VL_BC_ICMS,
                                            SUM(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                     THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'I','I','1'),0) +
                                                          NVL(VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'00','I','1','1'),0) +
                                                          DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'I','C','1'),0)))
                                                     /*ELSE 0 END*/) AS VL_ICMS,
                                            NVL(SUM(/*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                         THEN*/ DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL),0)
                                                         /*ELSE 0 END*/),0) AS VL_RED_BC,
                                            SUM((SELECT NVL(SUM(VRIMPOSTOIT),0)
                                                   FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                  WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                        (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                        (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                        (IT.NRITPROIMPOS = '1'                  ) AND
                                                        (IT.CDFISCALIT   = '1'                  ) AND
                                                        (IM.CDIMPOSTONFE = '10'                 ) AND
                                                        I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                            C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                            SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                            SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1')) AS IMP_ST,
                                            C.DSCFOPLMAG
                                       FROM ITEMPROIMPOS I, ITEMPRODUTO P, IMPOSTO IM, DETITMAPARECUP DT, CFOPLIVROMAGTPOP C
                                      WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                        AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                        AND C_REGC100.IDENTRSAIDOP = 'E'
                                        AND C_REGC100.CDMODDOCFISC <> '65'
                                        AND I.NRITPROIMPOS  = '1'
                                        AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                        AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                        AND I.CDFILIAL      = DT.CDFILIAL(+)
                                        AND I.NRLANCTONF    = DT.NRLANCTONF(+)
                                        AND I.CDIMPOSTO     = DT.CDIMPOSTO(+)
                                        AND I.NRSEQITPROD   = DT.NRSEQITPROD(+)
                                        AND I.NRITPROIMPOS  = DT.NRITPROIMPOS(+)
                                        AND I.CDFILIAL      = P.CDFILIAL
                                        AND I.NRLANCTONF    = P.NRLANCTONF
                                        AND I.NRSEQITPROD   = P.NRSEQITPROD
                                        AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                        AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                        AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                        AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                        AND (ROUND(P.QTITEMPROD,3) > 0 OR C_REGC100.SITU_DOC IN ('06','07','08'))
                                      GROUP BY I.CDSITUTRIBIT, I.CDFISCOPERIT, C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG,
                                               /*CASE WHEN DT.NRSEQDETITMAPA IS NULL OR DT.IDTIPOITMAPR = 'VI'
                                                    THEN*/ NVL(VALOR_IMPOSTO_ITEM_NFE_CARGA(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'A',DECODE(V_IDALIQCARGASPED,'N','I',''),'1'),0) + DECODE(I.CDFISCALIT,'1',NVL(I.VRADICALIQFECP,0),0)
                                                    /*ELSE 0 END*/
                                      UNION ALL
                                     /*NFC-e*/
                                     SELECT NVL(LPAD(CDCSTPRODI,3,'0'), (CASE WHEN IM.IDTPIMPOSFIS = 'T' THEN '000'
                                                                              WHEN IM.IDTPIMPOSFIS = 'I' THEN '040'
                                                                              WHEN IM.IDTPIMPOSFIS = 'F' THEN '060'
                                                                              WHEN IM.IDTPIMPOSFIS = 'N' THEN '041' END)) AS CST_ICMS,
                                            NVL(CDCFOPPROD, '5102') AS CFOP,
                                            ROUND(IP.VRPEALPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(IP.VRALIQFCP,0),0),2) AS ALIQ_ICMS,
                                            SUM(TRUNC((IT.QTPRODVEND  * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0),2)) AS VL_OPR,
                                            SUM(CASE WHEN IM.IDTPIMPOSFIS = 'T' AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                                     THEN CASE WHEN IP.VRPRBCREDUICMS > 0
                                                               THEN IP.VRBASECALCREDUZ
                                                               ELSE TRUNC((IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0))) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0),2) END
                                                     ELSE 0 END) AS VL_BC_ICMS,
                                            SUM(CASE WHEN IM.IDTPIMPOSFIS = 'T' AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                                     THEN CASE WHEN IP.VRPRBCREDUICMS > 0
                                                               THEN NVL(IP.VRIMPOPRODREDUZ,0)+DECODE(V_IDSOMAFECP,'S',NVL(IP.VRIMPICMSFCP,0),0)
                                                               ELSE NVL(VRIMPOPRODIT,0)+DECODE(V_IDSOMAFECP,'S',NVL(IP.VRIMPICMSFCP,0),0) END
                                                     ELSE 0 END) AS VL_ICMS,
                                            SUM(DECODE(IP.VRPRBCREDUICMS,0,0,IP.VRBCREDUZICMS)) AS VL_RED_BC,
                                            0 AS VL_IPI,
                                            C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                            0 AS BC_ST,
                                            0 AS IMP_ST,
                                            C.DSCFOPLMAG
                                       FROM ITEMVENDA IT, ITVENDAIMPOS IP, IMPOSTO IM, CFOPLIVROMAGTPOP C
                                      WHERE IT.CDFILIAL     = C_REGC100.CDFILIAL
                                        AND IT.CDCAIXA      = C_REGC100.CDCAIXA
                                        AND IT.NRSEQVENDA   = C_REGC100.NRLANCTONF
                                        AND IT.CDFILIAL     = IP.CDFILIAL
                                        AND IT.CDCAIXA      = IP.CDCAIXA
                                        AND IT.NRSEQUITVEND = IP.NRSEQUITVEND
                                        AND IT.NRSEQVENDA   = IP.NRSEQVENDA
                                        AND IP.CDIMPOSTO    = IM.CDIMPOSTO
                                        AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                        AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                        AND C.CDCFOPLMAG(+)   = IP.CDCFOPPROD
                                        AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                        AND NVL(C_REGC100.IDSTATUS,'A') <>  'C'
                                      GROUP BY NVL(LPAD(CDCSTPRODI,3,'0'), (CASE WHEN IM.IDTPIMPOSFIS = 'T' THEN '000'
                                                                 WHEN IM.IDTPIMPOSFIS = 'I' THEN '040'
                                                                 WHEN IM.IDTPIMPOSFIS = 'F' THEN '060'
                                                                 WHEN IM.IDTPIMPOSFIS = 'N' THEN '041' END)),
                                                 NVL(CDCFOPPROD, '5102'), IP.VRPEALPRODIT+DECODE(V_IDSOMAFECP,'S',NVL(IP.VRALIQFCP,0),0), C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG) T) LOOP

               V_COD_OBS := NULL;
               SELECT MAX(CDCHAVEREG)
                 INTO V_COD_OBS
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_REGC190.COD_OBS
                  AND CDREGISTRO = '0460';

               IF (V_COD_OBS IS NULL) AND (C_REGC190.COD_OBS IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                     (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
                  VALUES
                     ('0460', V_SEQUPROD, C_REGC190.COD_OBS, C_REGC190.DSCFOPLMAG);
               END IF;

               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;

               IF P_VERSAO >= '013' THEN
                  FOR C_REGC191 IN (SELECT '|C191'         || '|' ||
                                           REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN SUBSTR(IT.CDSITUTRIBIT,2,2) IN ('00','10','20','51','70','90') AND IM.CDIMPOSTONFE = '00'
                                                THEN ROUND(IT.VRADICFECP,2)
                                                ELSE 0 END),'999999999990.00')), '.', ',') || '|' ||
                                           REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN SUBSTR(IT.CDSITUTRIBIT,2,2) IN ('10','30','70','90') AND IM.CDIMPOSTONFE = '80'
                                                THEN ROUND(IT.VRADICFECP,2)
                                                ELSE 0 END),'999999999990.00')), '.', ',') || '|' ||
                                           REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN SUBSTR(IT.CDSITUTRIBIT,2,2) IN ('60')
                                                THEN ROUND(IT.VRADICFECP,2)
                                                ELSE 0 END),'999999999990.00')), '.', ',') || '|' AS ARQ_C191, IT.CDFISCOPERIT
                                      FROM ITEMPROIMPOS IT, ITEMPROIMPOS II, IMPOSTO IM, PARAMESTSPED PE
                                     WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                                       AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                       AND IT.CDFISCOPERIT  = C_REGC190.COD_NAT
                                       AND IT.CDSITUTRIBIT  = C_REGC190.CST_ICMS
                                       AND IT.VRALIQITEM + CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(IT.VRADICALIQFECP,0) ELSE 0 END = C_REGC190.ALIQ_ICMS
                                       AND IT.NRITPROIMPOS  = '1'
                                       AND IT.CDFILIAL      = II.CDFILIAL
                                       AND IT.NRLANCTONF    = II.NRLANCTONF
                                       AND IT.NRSEQITPROD   = II.NRSEQITPROD
                                       AND II.CDFISCOPERIT  = C_REGC190.COD_NAT
                                       AND II.CDSITUTRIBIT  = C_REGC190.CST_ICMS
                                       AND II.CDIMPOSTO     = IM.CDIMPOSTO
                                       AND IM.CDIMPOSTONFE  IN ('00','80')
                                       AND II.VRADICFECP    > 0
                                       AND PE.CDEMPRESA     = P_CDEMPRESA
                                       AND PE.CDINSCESTA    = P_CDINSCESTA
                                       AND PE.IDFECPC191    = 'S'
                                     GROUP BY IT.CDFISCOPERIT) LOOP
                      INSERT INTO ARQMAGNETICO
                         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                      VALUES
                         ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC191.ARQ_C191, P_NRSEQARQMAG);
                      V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                  END LOOP;
               END IF;
            END LOOP;
         END IF;

         V_C195 := 'N';
         /* GERACAO DOS REGISTROS COM DIFERENCIAL DE ALIQUOTA E ANTECIPACAO DE ICMS DO ARROZ*/
         FOR C_REGC197 IN (SELECT COD_AJ, DESCR_COMPL_AJ, COD_ITEM, IDTIPOITMAPR,
                                  REPLACE(LTRIM(TO_CHAR(ROUND(VL_BC_ICMS,2),'999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                  REPLACE(LTRIM(TO_CHAR(ROUND(ALIQ_ICMS+NVL(ALIQ_FECP,0),2), '999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                  REPLACE(LTRIM(TO_CHAR(ROUND(VL_ICMS,2),   '999999999990.00')), '.', ',') AS VL_ICMS,
                                  REPLACE(LTRIM(TO_CHAR(ROUND(VL_OUTROS,2), '999999999990.00')), '.', ',') AS VL_OUTROS,
                                  CDCFOPPADRAO, CDCLASFISC, CDPRODUTO
                             FROM (SELECT IM.CDAJUSTEITMAPA AS COD_AJ, IM.DSITEMMAPR AS DESCR_COMPL_AJ, '' AS COD_ITEM,
                                          0 AS VL_BC_ICMS, 0 AS ALIQ_ICMS, 0 AS ALIQ_FECP,
                                          IM.VRITEMMAPR AS VL_ICMS,
                                          IM.VROUTROSITMAPR AS VL_OUTROS, IM.IDTIPOITMAPR,
                                          IM.CDCFOPPADRAO AS CDCFOPPADRAO, '' AS CDCLASFISC, '' AS CDPRODUTO
                                     FROM ITMAPARECUP IM
                                    WHERE IM.CDFILIAL     = C_REGC100.CDFILIAL
                                      AND IM.NRLANCTONF   = C_REGC100.NRLANCTONF
                                      AND C_REGC100.CDMODDOCFISC <> '65'
                                      AND IM.IDCOMPOEC197 = 'D'
                                    UNION ALL
                                   SELECT IM.CDAJUSTEITMAPA AS COD_AJ, IM.DSITEMMAPR AS DESCR_COMPL_AJ,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD) AS COD_ITEM,
                                          SUM(DECODE(DT.IDSUBSTSOLIDST,'S',IT.VRBASECALCIT,DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0))) AS VL_BC_ICMS,
                                          DECODE(DT.IDSUBSTSOLIDST,'S',IT.VRALIQITEM,DECODE(IT.CDFISCALIT,'1',IT.VRALIQITEM,0)) AS ALIQ_ICMS,
                                          CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E'
                                               THEN DECODE(DT.IDSUBSTSOLIDST,'S',IT.VRADICALIQFECP,DECODE(IT.CDFISCALIT,'1',IT.VRADICALIQFECP,0))
                                               ELSE 0 END AS ALIQ_FECP,
                                          SUM(ROUND(DECODE(DT.IDSUBSTSOLIDST,'S',DT.VRIMPOSTOIT,
                                                                                 IT.VRIMPOSTOIT + CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E'
                                                                                                       THEN NVL(IT.VRADICFECP,0) ELSE 0 END),2)) AS VL_ICMS,
                                          SUM(ROUND(IM.VROUTROSITMAPR,2)) AS VL_OUTROS, IM.IDTIPOITMAPR, '' AS CDCFOPPADRAO, MAX(IP.CDPRODNCMTRANS) AS CDCLASFISC,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO) AS CDPRODUTO
                                     FROM ITMAPARECUP IM, IMPOSTO IM, ITEMPROIMPOS IT, DETITMAPARECUP DT, PRODUTO PA, PRODUTO PR, ITEMPRODUTO IP
                                    WHERE IM.CDFILIAL     = C_REGC100.CDFILIAL
                                      AND IM.NRLANCTONF   = C_REGC100.NRLANCTONF
                                      AND C_REGC100.CDMODDOCFISC <> '65'
                                      AND IM.IDCOMPOEC197 = 'I'
                                      AND IM.CDFILIAL     = IT.CDFILIAL
                                      AND IM.NRLANCTONF   = IT.NRLANCTONF
                                      AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                                      AND IM.CDIMPOSTONFE IN ('00','80')
                                      AND IT.CDIMPOSTO    = DT.CDIMPOSTO
                                      AND IM.NRSEQUMAPAPU = DT.NRSEQUMAPAPU
                                      AND IM.IDTIPOITMAPR = DT.IDTIPOITMAPR
                                      AND IM.NRSEQITMAPR  = DT.NRSEQITMAPR
                                      AND IT.CDFILIAL     = DT.CDFILIAL
                                      AND IT.NRLANCTONF   = DT.NRLANCTONF
                                      AND IT.NRSEQITPROD  = DT.NRSEQITPROD
                                      AND IT.NRITPROIMPOS = DT.NRITPROIMPOS
                                      AND IT.CDFILIAL     = IP.CDFILIAL
                                      AND IT.NRLANCTONF   = IP.NRLANCTONF
                                      AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                                      AND DT.CDFILIAL     = IP.CDFILIAL
                                      AND DT.NRLANCTONF   = IP.NRLANCTONF
                                      AND DT.NRSEQITPROD  = IP.NRSEQITPROD
                                      AND PR.CDPRODUTO    = IP.CDPRODUTO
                                      AND PR.CDPRODESTO   = PA.CDPRODUTO(+)
                                      AND DT.IDTIPOITMAPR <> 'VI'
                                      AND ((IT.NRITPROIMPOS = '1' AND IT.CDFISCALIT = '1') OR (IT.NRITPROIMPOS = '3') OR (DT.IDSUBSTSOLIDST = 'S'))
                                    GROUP BY IM.CDAJUSTEITMAPA, IM.DSITEMMAPR, IT.CDFISCALIT, IT.VRALIQITEM, IT.VRADICALIQFECP, DT.IDSUBSTSOLIDST,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD), IM.IDTIPOITMAPR,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO)
                                    UNION ALL
                                   SELECT IM.CDAJUSTEITMAPA AS COD_AJ, IM.DSITEMMAPR AS DESCR_COMPL_AJ,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD) AS COD_ITEM,
                                          SUM(DT.VROUTBASE) AS VL_BC_ICMS,
                                          NVL(DT.VROUTALIQ,0) AS ALIQ_ICMS, 0 AS ALIQ_FECP,
                                          SUM(NVL(DT.VRIMPOSTOIT,0)) AS VL_ICMS,
                                          SUM(ROUND(DT.VROUTROSIMPIT,2)) AS VL_OUTROS, IM.IDTIPOITMAPR,
                                          IM.CDCFOPPADRAO, MAX(IP.CDPRODNCMTRANS) AS CDCLASFISC,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO) AS CDPRODUTO
                                     FROM ITMAPARECUP IM, DETITMAPARECUP DT, PRODUTO PA, PRODUTO PR, ITEMPRODUTO IP
                                    WHERE IM.CDFILIAL     = C_REGC100.CDFILIAL
                                      AND IM.NRLANCTONF   = C_REGC100.NRLANCTONF
                                      AND C_REGC100.CDMODDOCFISC <> '65'
                                      AND IM.IDCOMPOEC197 = 'I'
                                      AND IM.IDTIPOITMAPR = 'VI'
                                      AND IM.NRSEQUMAPAPU = DT.NRSEQUMAPAPU
                                      AND IM.IDTIPOITMAPR = DT.IDTIPOITMAPR
                                      AND IM.NRSEQITMAPR  = DT.NRSEQITMAPR
                                      AND DT.CDFILIAL     = IP.CDFILIAL
                                      AND DT.NRLANCTONF   = IP.NRLANCTONF
                                      AND DT.NRSEQITPROD  = IP.NRSEQITPROD
                                      AND PR.CDPRODUTO    = IP.CDPRODUTO
                                      AND PR.CDPRODESTO   = PA.CDPRODUTO(+)
                                    GROUP BY IM.CDAJUSTEITMAPA, IM.DSITEMMAPR,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD),
                                          DT.VROUTALIQ, IM.IDTIPOITMAPR, IM.CDCFOPPADRAO,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO)
                                    UNION ALL
                                   SELECT IM.CDAJUSTEITMAPA AS COD_AJ, IM.DSITEMMAPR AS DESCR_COMPL_AJ,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD) AS COD_ITEM,
                                          SUM(DT.VROUTBASE) AS VL_BC_ICMS,
                                          NVL(DT.VROUTALIQ,0) AS ALIQ_ICMS, 0 AS ALIQ_FECP,
                                          SUM(NVL(DT.VRIMPOSTOIT,0)) AS VL_ICMS,
                                          SUM(ROUND(DT.VROUTROSIMPIT,2)) AS VL_OUTROS, IM.IDTIPOITMAPR,
                                          IM.CDCFOPPADRAO, MAX(IP.CDCLASFISC) AS CDCLASFISC,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO) AS CDPRODUTO
                                     FROM ITMAPARECUP IM, DETITMAPARECUP DT, PRODUTO PA, PRODUTO PR, ITEMVENDA IP
                                    WHERE DT.CDFILIALVND   = C_REGC100.CDFILIAL
                                      AND DT.NRSEQVENDA    = C_REGC100.NRLANCTONF
                                      AND DT.CDCAIXA       = C_REGC100.CDCAIXA
                                      AND C_REGC100.CDMODDOCFISC = '65'
                                      AND IM.IDCOMPOEC197  = 'I'
                                      AND IM.IDTIPOITMAPR  = 'VI'
                                      AND IM.NRSEQUMAPAPU  = DT.NRSEQUMAPAPU
                                      AND IM.IDTIPOITMAPR  = DT.IDTIPOITMAPR
                                      AND IM.NRSEQITMAPR   = DT.NRSEQITMAPR
                                      AND DT.CDFILIALVND   = IP.CDFILIAL
                                      AND DT.CDCAIXA       = IP.CDCAIXA
                                      AND DT.NRSEQVENDA    = IP.NRSEQVENDA
                                      AND DT.NRSEQUITVEND  = IP.NRSEQUITVEND
                                      AND PR.CDPRODUTO     = IP.CDPRODUTO
                                      AND PR.CDPRODESTO    = PA.CDPRODUTO(+)
                                    GROUP BY IM.CDAJUSTEITMAPA, IM.DSITEMMAPR,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD),
                                          DT.VROUTALIQ, IM.IDTIPOITMAPR, IM.CDCFOPPADRAO,
                                          DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDPRODUTO,PR.CDPRODUTO),PR.CDPRODUTO))
                            ORDER BY SUBSTR(COD_AJ,8,3), IDTIPOITMAPR, CDCFOPPADRAO, CDCLASFISC, CDPRODUTO) LOOP

             BEGIN
                SELECT LPAD(NVL(IT.CDITEMVARIAVEL,'0')||DECODE(C_REGC197.CDCFOPPADRAO,'','',SUBSTR(C_REGC197.DESCR_COMPL_AJ,2,3)),6,'0') AS COD_OBS,
                       TRIM(IT.DSAJUSTE) AS TXT
                  INTO V_CODC195, V_TXTC195
                  FROM ITEMVARAJEFD IT
                 WHERE IT.SGESTADO       = SUBSTR(C_REGC197.COD_AJ,1,2)
                   AND IT.CDITEMVARIAVEL = SUBSTR(C_REGC197.COD_AJ,8,3);
             EXCEPTION
                WHEN NO_DATA_FOUND THEN
                   V_CODC195 := '';
                   V_TXTC195 := '';
             END;

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

            IF (V_PRODUTO IS NULL) AND (C_REGC197.CDPRODUTO IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3)
               VALUES
                 ('0200', V_SEQUPROD, C_REGC197.CDPRODUTO, C_REGC197.CDCLASFISC);
            END IF;
            V_PRODUTO := NULL;

             V_COD_OBS := NULL;
             SELECT MAX(CDCHAVEREG)
               INTO V_COD_OBS
               FROM TMP_REGDADOSSPED
              WHERE CDCHAVEREG = V_CODC195
                AND CDREGISTRO = '0460';

             IF (V_COD_OBS IS NULL) THEN
                V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                INSERT INTO TMP_REGDADOSSPED
                   (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
                VALUES
                   ('0460', V_SEQUPROD, NVL(NVL(V_CODC195,SUBSTR(C_REGC197.COD_AJ,8,3)),'VAZIO'), V_TXTC195);
             END IF;

             IF (V_C195 IS NULL) OR (V_C195 <> SUBSTR(C_REGC197.COD_AJ,8,3)||CASE WHEN C_REGC197.CDCFOPPADRAO IS NOT NULL THEN SUBSTR(C_REGC197.DESCR_COMPL_AJ,2,3) ELSE '' END) THEN
                INSERT INTO ARQMAGNETICO
                   (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                VALUES
                   ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C195'  || '|' || V_CODC195 || '|' || '' || '|', P_NRSEQARQMAG);
                V_CONTREGC := NVL(V_CONTREGC,0) + 1;
             END IF;

             V_REG_C197 := '|C197'                   || '|' ||
                           C_REGC197.COD_AJ          || '|' ||
                           C_REGC197.DESCR_COMPL_AJ  || '|' ||
                           C_REGC197.COD_ITEM        || '|' ||
                           C_REGC197.VL_BC_ICMS      || '|' ||
                           C_REGC197.ALIQ_ICMS       || '|' ||
                           C_REGC197.VL_ICMS         || '|' ||
                           C_REGC197.VL_OUTROS       || '|' ;
             INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
             VALUES
               ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), V_REG_C197, P_NRSEQARQMAG);
             V_CONTREGC := NVL(V_CONTREGC,0) + 1;

             V_C195 := SUBSTR(C_REGC197.COD_AJ,8,3)||CASE WHEN C_REGC197.CDCFOPPADRAO IS NOT NULL THEN SUBSTR(C_REGC197.DESCR_COMPL_AJ,2,3) ELSE '' END;
         END LOOP;
      END LOOP;
   END IF;

   IF V_CDSITUCRT = '1' THEN  /*SIMPLES NACIONAL*/
       FOR C_REGC100 IN(SELECT  '|C100'                                                              || '|' ||
                                NF.CAMPO2                                                            || '|' ||
                                (CASE WHEN ((NF.CDSERIEENTR BETWEEN '890' AND '899') AND (NF.CDMODDOCFISC = '55')) THEN '1' ELSE NF.CAMPO3 END) || '|' AS DADOS_1,
                                --DECODE(NF.IDSTATUSNFE,'D','',DECODE(NF.IDSTATUS,'C','',REPLACE(REPLACE(REPLACE(REPLACE(NF.IDORIGDESTNF||DECODE(NF.IDORIGDESTNF, 'F', NF.CDFORNECED, DECODE(NF.IDORIGDESTNF, 'C', NF.CDCLIENTE , DECODE(NF.IDENTRSAIDOP, 'E', NF.CDFILIEMIT, NF.CDFILIRECE))),'.'),'-'),'/'),' '))) || '|' ||
                                NF.CDMODDOCFISC                                                      || '|' ||
                                DECODE(NF.IDSTATUSNFE, 'D', '04', DECODE(IDSTATUS,'C','02',DECODE(IDREGESPECIAL,'S','08', DECODE(NF.IDENTRSAIDOP,'S', (CASE WHEN DTEMISSAO < P_DTINICIAL THEN DECODE(NF.CDTIPOOPER,'03','07',TIPOOPER_COMPL_S,'07','01') ELSE DECODE(NF.CDTIPOOPER,'03','06',TIPOOPER_COMPL_S,'06','00') END), (CASE WHEN ((NF.CDSERIEENTR BETWEEN '890' AND '899') AND (NF.CDMODDOCFISC = '55')) THEN '08'ELSE (CASE WHEN DTENTRSAID < P_DTINICIAL THEN DECODE(NF.CDTIPOOPER,'03','07',TIPOOPER_COMPL_E,'07','01')ELSE DECODE(NF.CDTIPOOPER,'03','06',TIPOOPER_COMPL_E,'06','00') END) END))))) ||'|'||
                                CASE WHEN LENGTH(NF.NRACESSONFE) = '44' THEN SUBSTR(NRACESSONFE,23,3) ELSE REPLACE(REPLACE(DECODE(NF.IDENTRSAIDOP, 'E', NF.CDSERIEENTR, NF.CDSERIE),'-',''),'.','') END   || '|' ||
                                DECODE(NF.IDENTRSAIDOP, 'E', LTRIM(NF.NRNOTAFISC,'0'), DECODE(NF.NRACESSONFE, '', LTRIM(REPLACE(LTRIM(NF.NRNOTAFISC, '0'),' ')),DECODE(NF.IDSTATUS,'C',LTRIM(NF.NRNOTAFISC,'0'),LTRIM(NF.NRNOTAFISCALELE, '0')) )  )|| '|' ||
                                NRACESSONFE                                                                                        || '|' ||  --chave da nota fiscal eletronica
                                DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', TO_CHAR(NF.DTEMISSAO,'ddmmyyyy')) )   || '|' AS DADOS_2,
                                REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRCONTABILNF,2), '999999999990.00')), '.', ',')                     || '|' ||
                                DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', DECODE(NVL(NF.QTDEPARC,0),0,'2',1,'0','1'))) || '|' || -- campo 13
                                DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', REPLACE(LTRIM(TO_CHAR((SELECT SUM(ROUND(VRENCARGODESC,2)) FROM ITEMPRODUTO WHERE CDFILIAL = NF.CDFILIAL AND NRLANCTONF = NF.NRLANCTONF AND NRSEQITPROD IS NOT NULL), '999999999990.00')), '.', ','))) || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', VALOR_ENCARGO_NFE(NF.CDFILIAL, NF.NRLANCTONF, V_IDDESCONTO, '03'))),2), '999999999990.00')), '.', ',') || '|' ||  -- abatimento no tributado e nao comercial desconto ICMS
                                DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRPRODUTOS,2), '999999999990.00')), '.', ','))) || '|' ||
                                DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', RPAD(NVL(NF.IDPGTOFRETE, '9'), 1 , ' ')))                                      || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', VALOR_ENCARGO_NFE(NF.CDFILIAL, NF.NRLANCTONF, V_IDFRETE,  '01'))),2), '999999999990.00')), '.', ',') || '|' ||
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', VALOR_ENCARGO_NFE(NF.CDFILIAL, NF.NRLANCTONF, V_IDSEGUR,  '02'))),2), '999999999990.00')), '.', ',') || '|' ||
                                /*REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUS, 'C', '', ABS(VALOR_IMPOSTO_NF(NF.CDFILIAL, NF.NRLANCTONF,V_CDIMPOSTRIB, 'I','1')) + ABS(VALOR_IMPOSTO_NF(NF.CDFILIAL, NF.NRLANCTONF, V_IPI, 'I','1'))),2), '999999999990.00')), '.', ',')  || '|' || --campo20*/
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', VALOR_ENCARGO_NFE(NF.CDFILIAL, NF.NRLANCTONF, V_IDOUTROS, '04'))),2), '999999999990.00')), '.', ',') || '|' || -- Campo 20 outras obrigacoes acessorias
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', NF.VRBASECALCNF)),2), '999999999990.00')), '.', ',') || '|' || -- campo 21
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', NF.VRIMPOSTONF)),2), '999999999990.00')), '.', ',') || '|' || -- campo 22
                                REPLACE(LTRIM(TO_CHAR(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS,'C','',VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF,'80','B','1'))), '999999999990.00')), '.', ',') || '|' || -- base de calc susbt. trib
                                REPLACE(LTRIM(TO_CHAR(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS,'C','',VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF,'80','I','1') + CASE WHEN V_IDSOMAFECPST = 'S' OR NF.IDENTRSAIDOP = 'E' THEN VALOR_FECP_NFE(NF.CDFILIAL, NF.NRLANCTONF,'80','I','1') ELSE 0 END)), '999999999990.00')), '.', ',') || '|' || -- valor do icms retido susbt. trib
                                REPLACE(LTRIM(TO_CHAR(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS,'C','',VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF,'10','I','1'))), '999999999990.00')), '.', ',') || '|' || -- valor do IPI
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF, '50', 'I','1'))),2), '999999999990.00')), '.', ',') || '|' || -- valor do PIS
                                REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', VALOR_IMPOSTO_NFE(NF.CDFILIAL, NF.NRLANCTONF, '60', 'I','1'))),2), '999999999990.00')), '.', ',') || '|' || -- valor do COFINS
                                DECODE(NF.IDSTATUSNFE, 'D', '', DECODE(NF.IDSTATUS, 'C', '', '0,00')) || '|' || -- pis retido por subst. trib.
                                DECODE(NF.IDSTATUSNFE, 'D', '', 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, NF.IDORIGDESTNF,
                                DECODE(NF.IDSTATUSNFE,'D','',DECODE(NF.IDSTATUS,'C','',NF.IDORIGDESTNF||DECODE(NF.IDORIGDESTNF, 'F', NF.CDFORNECED, DECODE(NF.IDORIGDESTNF, 'C', NF.CDCLIENTE , DECODE(NF.IDENTRSAIDOP, 'E', NF.CDFILIEMIT, NF.CDFILIRECE))))) AS COD_PAR,
                                DECODE(IDSTATUS,'C','02',DECODE(IDREGESPECIAL,'S','08',DECODE(NF.IDENTRSAIDOP,'S', (CASE WHEN DTEMISSAO < P_DTINICIAL THEN DECODE(NF.CDTIPOOPER,'03','07',TIPOOPER_COMPL_S,'07','01') ELSE DECODE(NF.CDTIPOOPER,'03','06',TIPOOPER_COMPL_S,'06','00') END),(CASE WHEN DTENTRSAID < P_DTINICIAL THEN DECODE(NF.CDTIPOOPER,'03','07',TIPOOPER_COMPL_E,'07','01')ELSE DECODE(NF.CDTIPOOPER,'03','06',TIPOOPER_COMPL_E,'06','00') END)))) AS SITU_DOC,
                                NF.IDSTATUSNFE, NVL(NF.IDSIMPLESNACIONAL,'N') AS IDSIMPLESNACIONAL, NF.QTDEPARC, NF.CDTIPOOPER, DECODE(NF.IDENTRSAIDOP,'S',NF.CDFILIEMIT,NF.CDFILIRECE) AS FILIAL
                        FROM (/*NF SAIDA SEM CARGA TRIBUTARIA*/
                              SELECT DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                     DECODE(N.IDENTRSAIDOP, 'E', DECODE(IDSTATUSNFE,'A','0',DECODE(IDSTATUS,'E','0','1')), '0') AS CAMPO3, NRACESSONFE,
                                     I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR,
                                     N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL, 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,
                                     N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE, DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'S') AS IDSIMPLESNACIONAL,
                                     MAX((SELECT COUNT(DISTINCT NRPARCELAREC)
                                            FROM CONTARECEBER C
                                           WHERE N.CDFILIAL = C.CDFILIAL
                                             AND N.NRLANCTONF = C.NRLANCTONF
                                             AND C.NRLANBOLREC IS NOT NULL)) AS QTDEPARC
                              FROM NOTAFISCAL N, IMPOSTONF I, IMPOSTO IP, PARAMFORNE PF, SERIENF SS, SERIEENTR SE, TIPOOPERAC T
                              WHERE N.NRSEQUMAPAPUSN  = V_NRSEQUMAPAPU
                                AND N.CDINSCESTA      = P_CDINSCESTA
                                AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                AND IP.CDIMPOSTO      = I.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') <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                AND SS.CDSERIE(+)     = N.CDSERIE
                                AND N.IDENTRSAIDOP    = 'S'
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND N.CDFILIAL        = I.CDFILIAL
                                AND N.NRLANCTONF      = I.NRLANCTONF
                                AND N.CDFORNECED      = PF.CDFORNECED(+)
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                AND N.CDMODDOCFISC IN ('01','1B','04','55')
                                AND NVL(P_GERAFORSALE,'N') = 'N'
                                AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') = 0
								        AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                AND T.CDTIPOOPER	   = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE), NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR),
                                     N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL, 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, N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE,
                                     DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'S')
                              UNION ALL
                              /*NF SAIDA COM CARGA TRIBUTARIA*/
                              SELECT DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                     DECODE(N.IDENTRSAIDOP, 'E', DECODE(IDSTATUSNFE,'A','0',DECODE(IDSTATUS,'E','0','1')), '0') AS CAMPO3, NRACESSONFE,
                                     I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR,
                                     N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL, 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(DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','B','1')))) AS VRBASECALCNF,
                                     TO_CHAR(DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1')))  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,
                                     N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE, DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'S') AS IDSIMPLESNACIONAL,
                                     MAX((SELECT COUNT(DISTINCT NRPARCELAREC)
                                            FROM CONTARECEBER C
                                           WHERE N.CDFILIAL = C.CDFILIAL
                                             AND N.NRLANCTONF = C.NRLANCTONF
                                             AND C.NRLANBOLREC IS NOT NULL)) AS QTDEPARC
                              FROM NOTAFISCAL N, IMPOSTONF I, IMPOSTO IP, PARAMFORNE PF, SERIENF SS, SERIEENTR SE, TIPOOPERAC T
                              WHERE N.NRSEQUMAPAPUSN  = V_NRSEQUMAPAPU
                                AND N.CDINSCESTA      = P_CDINSCESTA
                                AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                AND IP.CDIMPOSTO      = I.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') <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                AND SS.CDSERIE(+)     = N.CDSERIE
                                AND N.IDENTRSAIDOP    = 'S'
                                AND NVL(I.IDIMPOSTONF, '1') <> '3'
                                AND N.CDFILIAL        = I.CDFILIAL
                                AND N.NRLANCTONF      = I.NRLANCTONF
                                AND N.CDFORNECED      = PF.CDFORNECED(+)
                                AND ((N.IDSTATUS = 'A') OR (N.IDSTATUS = 'E'))
                                AND N.CDMODDOCFISC IN ('01','1B','04','55')
                                AND NVL(P_GERAFORSALE,'N') = 'N'
                                AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') > 0
                                AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                AND T.CDTIPOOPER      = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE), NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR),
                                     N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL, 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, N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE,
                                     DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'S'), VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','B','1'), VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1')
                              UNION ALL
                              /******** NOTA CANCELADA *******/
                              SELECT DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                     '0' AS CAMPO3, NRACESSONFE,
                                     N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC,NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR,
                                     '' AS NRINSJURNF, N.IDSTATUS,'' AS IDREGESPECIAL, N.DTENTRSAID, N.VRNOTAFISC, '' AS VRPRODUTOS, N.IDPGTOFRETE,
                                     N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,
                                     '' AS CDFILIEMIT, '' AS 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,
                                     NRNOTAFISCALELE,
                                     N.CDTIPOOPER, N.IDSTATUSNFE, 'N' AS IDSIMPLESNACIONAL, 0 AS QTDEPARC
                               FROM NOTAFISCAL N, SERIENF SS, SERIEENTR SE
                              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') <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                AND SS.CDSERIE(+)     = N.CDSERIE
                                AND N.IDENTRSAIDOP = 'S'
                                AND N.IDSTATUS = 'C'
                                AND N.CDMODDOCFISC IN ('01','1B','04','55')
                                AND N.DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL
                                AND N.CDINSCESTA = P_CDINSCESTA
                                AND N.CDEMPRESA = P_CDEMPRESA
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), N.CDFILIAL, N.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE), NVL(SE.CDSERIEENTRNFE,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, N.NRACESSONFE, N.CDTIPOOPER, N.IDSTATUSNFE, N.NRNOTAFISCALELE
                              UNION ALL
                              /*NF ENTRADA SEM CARGA TRIBUTARIA*/
                              SELECT DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                     DECODE(N.IDENTRSAIDOP, 'E', DECODE(IDSTATUSNFE,'A','0',DECODE(IDSTATUS,'E','0','1')), '0') AS CAMPO3, NRACESSONFE,
                                     I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR,
                                     N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL, 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,
                                     N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE, DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N') AS IDSIMPLESNACIONAL,
                                     MAX((SELECT COUNT(DISTINCT C.NRPARCELAPAG)
                                            FROM CONTAPAGAR C
                                           WHERE N.CDFILIAL   = C.CDFILIAL
                                             AND N.NRLANCTONF = C.NRLANCTONF
                                             AND C.NRLANBOLPAG IS NOT NULL)) AS QTDEPARC
                               FROM NOTAFISCAL N, IMPOSTONF I, IMPOSTO IP, PARAMFORNE PF, SERIENF SS, SERIEENTR SE, TIPOOPERAC T
                              WHERE N.NRSEQUMAPAPUSN  = V_NRSEQUMAPAPU
                                AND N.CDINSCESTA      = P_CDINSCESTA
                                AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                AND IP.CDIMPOSTO      = I.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')  <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS')  <> 'FIS')
                                AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                AND SS.CDSERIE(+)     = N.CDSERIE
                                AND N.IDENTRSAIDOP    = 'E'
                                AND PF.CDFORNECED(+)  = N.CDFORNECED
                                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','1B','04','55')
                                AND NVL(P_GERAFORSALE,'N') = 'N'
                                AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') = 0
								        AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
								        AND T.CDTIPOOPER      = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP,
                                     N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE), NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR), N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL, 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, N.NRNOTAFISCALELE, N.CDTIPOOPER, IDSTATUSNFE,
                                     DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N')
                              UNION ALL
                              /*NF ENTRADA COM CARGA TRIBUTARIA*/
                              SELECT DECODE(N.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                     DECODE(N.IDENTRSAIDOP, 'E', DECODE(IDSTATUSNFE,'A','0',DECODE(IDSTATUS,'E','0','1')), '0') AS CAMPO3, NRACESSONFE,
                                     I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP, N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR,
                                     N.NRINSJURNF, N.IDSTATUS, T.IDREGESPECIAL, 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)+DECODE(V_IDTPCARGATRIB,'O',0,CASE WHEN V_SGESTADO <> 'SP' THEN VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','B','1') ELSE 0 END))) AS VRBASECALCNF,
                                     TO_CHAR(SUM(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF, 0)+DECODE(V_IDTPCARGATRIB,'O',0,CASE WHEN V_SGESTADO <> 'SP' THEN VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') ELSE 0 END))) 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,
                                     N.NRNOTAFISCALELE, N.CDTIPOOPER, N.IDSTATUSNFE, DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N') AS IDSIMPLESNACIONAL,
                                     MAX((SELECT COUNT(DISTINCT NRPARCELAPAG)
                                            FROM CONTAPAGAR C
                                           WHERE N.CDFILIAL = C.CDFILIAL
                                             AND N.NRLANCTONF = C.NRLANCTONF
                                             AND C.NRLANBOLPAG IS NOT NULL)) AS QTDEPARC
                              FROM NOTAFISCAL N, IMPOSTONF I, IMPOSTO IP, PARAMFORNE PF, SERIENF SS, SERIEENTR SE, TIPOOPERAC T
                              WHERE N.NRSEQUMAPAPUSN  = V_NRSEQUMAPAPU
                                AND N.CDINSCESTA      = P_CDINSCESTA
                                AND IP.CDIMPOSTONFE   = P_CDIMPOSTO
                                AND IP.CDIMPOSTO      = I.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')  <> 'TMP'
                                AND NVL(N.IDSISTEMA, 'DFS')  <> 'FIS')
                                AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                AND SS.CDSERIE(+)     = N.CDSERIE
                                AND N.IDENTRSAIDOP    = 'E'
                                AND PF.CDFORNECED(+)  = N.CDFORNECED
                                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','1B','04','55')
                                AND NVL(P_GERAFORSALE,'N') = 'N'
                                AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') > 0
                                AND T.IDENTRSAIDOP    = N.IDENTRSAIDOP
                                AND T.CDTIPOOPER      = N.CDTIPOOPER
                              GROUP BY DECODE(N.IDENTRSAIDOP, 'E', '0','1'), I.CDFILIAL, I.NRLANCTONF, N.IDENTRSAIDOP,
                                     N.NRNOTAFISC, NVL(SS.CDSERIENFE,N.CDSERIE), NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR), N.NRINSJURNF, N.IDSTATUS,T.IDREGESPECIAL, 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, N.NRNOTAFISCALELE, N.CDTIPOOPER, IDSTATUSNFE,
                                     DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'N')
                              UNION
                              /*******NOTAS FISCAIS FORSALE - C100 FOS ***********/
                              SELECT DECODE(NF.IDENTRSAIDOP, 'E', '0','1') AS CAMPO2,
                                     DECODE(NF.IDENTRSAIDOP, 'E', DECODE(IDSTATUS,'E','0','1'), '0') AS CAMPO3, NRACESSONFE,
                                     NF.CDFILIAL, NF.NRLANCTONF, NF.IDENTRSAIDOP, NF.NRNOTAFISC, NF.CDSERIE, NF.CDSERIEENTR,
                                     NF.NRINSJURNF, NF.IDSTATUS,'' AS IDREGESPECIAL, NF.DTENTRSAID, NF.VRNOTAFISC, TO_CHAR(NF.VRPRODUTOS) AS VRPRODUTOS, NF.IDPGTOFRETE,
                                     NF.NRPLACAVEIC, NF.DSQTDEVOLU, NF.DSESPECVOLU, NF.DSMARCAVOLU, NF.DSNUMEVOLU,
                                     NF.CDFILIEMIT, NF.CDFILIRECE, NF.DTEMISSAO, MAX(NF.CDFISCOPERNF) AS CDFISCOPERNF, NF.CDMODDOCFISC, NF.CDCLIENTE, NF.CDFORNECED, NF.IDORIGDESTNF,
                                     NF.VRCONTABILNF AS VRCONTABILNF,
                                     NF.NRINSJURTRAN, NF.IDTPIJURTRAN, NF.VRPBRUTOVOLU, NF.VRPLIQUVOLU,
                                     TO_CHAR(VRBASECALCNF) AS VRBASECALCNF,
                                     TO_CHAR(VRIMPOSTONF),
                                     TO_CHAR(VROUTRCRED),
                                     TO_CHAR(VRISENTRIB),
                                     NF.NRNOTAFISCALELE,
                                     NF.CDTIPOOPER, '' AS IDSTATUSNFE, NF.IDSIMPLESNACIONAL, NF.QTDEPARC
                               FROM  ENDECLIE EC, ENDEFORN EF, ENDEFILI EFI,
                                (SELECT DECODE(NVL(N.IDORIGDESTNF,DECODE(N.IDENTRSAIDOP,'S','C','F')),'C',CL.NRINSJURCLIE,'F',FO.NRINSJURFORN,'L',FI.NRINSJURFILI) AS NRINSJURNF,
                                        DECODE(NVL(N.IDORIGDESTNF,DECODE(N.IDENTRSAIDOP,'S','C','F')),'C',CL.NRINSESTCLIE,'F',FO.NRINSESTFORN,'L',FI.CDINSCESTA  ) AS NRINSEST,
                                        N.IDORIGDESTNF, N.CDCLIENTE, N.CDFORNECED, N.CDFILIEMIT, N.CDFILIRECE, N.NRNOTAFISC, I.CDFISCOPERNF,
                                        N.NRLANCTONF,N.CDFILIAL, N.IDENTRSAIDOP, DECODE(N.CDMODDOCFISC,'2D','55',N.CDMODDOCFISC) AS CDMODDOCFISC,
                                        NVL(SS.CDSERIENFE,N.CDSERIE) AS CDSERIE, NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR) AS CDSERIEENTR, N.DTENTRSAID, N.DTEMISSAO,
                                        TO_CHAR(SUM(VRCONTABILNF)) AS VRCONTABILNF,
                                        DECODE(I.CDFISCALNF,'1',SUM(VRBASECALCNF),0) AS VRBASECALCNF,
                                        DECODE(I.CDFISCALNF,'2',SUM(VRCONTABILNF),'1',SUM(DECODE(I.VRPERREDBCNF,0, 0,I.VRCONTABILNF-I.VRBASECALCNF)),0) AS VRISENTRIB,
                                        DECODE(I.CDFISCALNF,'3',SUM(VRCONTABILNF),0) AS VROUTRCRED,
                                        DECODE(I.CDFISCALNF,'1',SUM(VRIMPOSTONF ),0) AS VRIMPOSTONF,
                                        SUM(N.VRPRODUTOS) AS VRPRODUTOS,
                                        N.NRINSJURTRAN,N.IDTPIJURTRAN, N.VRPBRUTOVOLU, N.VRPLIQUVOLU, N.NRNOTAFISCALELE, I.VRPERREDBCNF,N.IDSTATUS,N.IDPGTOFRETE,
                                        N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,N.VRNOTAFISC, NRACESSONFE, N.CDTIPOOPER,
                                        DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'S') AS IDSIMPLESNACIONAL,
                                        MAX((SELECT COUNT(DISTINCT NRPARCELAREC)
                                               FROM CONTARECEBER C
                                              WHERE N.CDFILIAL    = C.CDFILIAL
                                                AND N.NRLANCTONF  = C.NRLANCTONF
                                                AND C.NRLANBOLREC IS NOT NULL)) AS QTDEPARC
                                   FROM NOTAFISCAL N, IMPOSTONF I, IMPOSTO IMP, CLIENTE CL, FORNECEDOR FO, FILIAL FI, PARAMFORNE PF, SERIENF SS, SERIEENTR SE
                                  WHERE N.CDFILIAL     = I.CDFILIAL
                                    AND N.NRLANCTONF   = I.NRLANCTONF
                                    AND IMP.CDIMPOSTO  = I.CDIMPOSTO
                                    AND N.CDCLIENTE    = CL.CDCLIENTE(+)
                                    AND N.CDFORNECED   = FO.CDFORNECED(+)
                                    AND N.CDFORNECED   = PF.CDFORNECED(+)
                                    AND N.CDFILIRECE   = FI.CDFILIAL(+)
                                    AND N.CDFILIEMIT   = FI.CDFILIAL(+)
                                    AND DTEMISSAO BETWEEN P_DTINICIAL AND P_DTFINAL
                                    AND ((N.IDSTATUS   = 'A' OR N.IDSTATUS = 'E') OR (N.IDSTATUSNFE = 'A' OR N.IDSTATUSNFE = 'T'))
                                    AND N.CDINSCESTA   = P_CDINSCESTA
                                    AND IMP.CDIMPOSTONFE = P_CDIMPOSTO
                                    AND (I.IDIMPOSTONF <> '3' OR IDIMPOSTONF IS NULL)
                                    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')  <> 'TMP'
                                    AND NVL(N.IDSISTEMA, 'DFS')  <> 'FIS')
                                    AND SE.CDSERIEENTR(+) = N.CDSERIEENTR
                                    AND SS.CDEMPRESA(+)   = N.CDEMPRESA
                                    AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                    AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                    AND SS.CDSERIE(+)     = N.CDSERIE
                                    AND N.CDMODDOCFISC IN ('01','1B','04','55','2D')
                                    AND N.IDENTRSAIDOP = 'S'
                                    AND NVL(P_GERAFORSALE,'N') = 'S'
                                  GROUP BY DECODE(NVL(N.IDORIGDESTNF,DECODE(N.IDENTRSAIDOP,'S','C','F')),'C',CL.NRINSJURCLIE,'F',FO.NRINSJURFORN,'L',FI.NRINSJURFILI),
                                           DECODE(NVL(N.IDORIGDESTNF,DECODE(N.IDENTRSAIDOP,'S','C','F')),'C',CL.NRINSESTCLIE,'F',FO.NRINSESTFORN,'L',FI.CDINSCESTA),N.IDORIGDESTNF,
                                           N.CDCLIENTE, N.CDFORNECED, N.CDFILIEMIT, N.CDFILIRECE, N.NRNOTAFISC, I.CDFISCOPERNF, N.NRLANCTONF,
                                           N.CDFILIAL, N.IDENTRSAIDOP, N.CDMODDOCFISC, NVL(SS.CDSERIENFE,N.CDSERIE), NVL(SE.CDSERIEENTRNFE,N.CDSERIEENTR),
                                           N.DTENTRSAID, N.DTEMISSAO,  I.CDFISCALNF, N.NRINSJURTRAN, N.IDTPIJURTRAN, N.VRPBRUTOVOLU,
                                           N.VRPLIQUVOLU,N.NRNOTAFISCALELE,I.VRPERREDBCNF,N.IDSTATUS,IDPGTOFRETE,
                                           N.NRPLACAVEIC, N.DSQTDEVOLU, N.DSESPECVOLU, N.DSMARCAVOLU, N.DSNUMEVOLU,N.VRNOTAFISC,NRACESSONFE,N.CDTIPOOPER,DECODE(N.IDORIGDESTNF,'F',PF.IDSIMPLESNACIONAL,'S'))NF
                              WHERE NF.CDCLIENTE    = EC.CDCLIENTE(+)
                                AND NF.CDFORNECED   = EF.CDFORNECED(+)
                                AND NF.CDFILIEMIT   = EFI.CDFILIAL(+)
                                AND NF.CDFILIRECE   = EFI.CDFILIAL(+)
                                AND IDTPENDECLIE(+) = 'P'
                                AND IDTPENDEFORN(+) = 'P'
                                AND IDTPENDEFILI(+) = 'P'
                                AND NVL(P_GERAFORSALE,'N') = 'S'
                              GROUP BY DECODE(NF.IDENTRSAIDOP, 'E', '0','1'),
                                       DECODE(NF.IDENTRSAIDOP, 'E', DECODE(IDSTATUS,'E','0','1'), '0'), NRACESSONFE,
                                       NF.CDFILIAL, NF.NRLANCTONF, NF.IDENTRSAIDOP, NF.NRNOTAFISC, NF.CDSERIE, NF.CDSERIEENTR,
                                       NF.NRINSJURNF, NF.IDSTATUS, NF.DTENTRSAID, NF.VRNOTAFISC, TO_CHAR(NF.VRPRODUTOS), NF.IDPGTOFRETE,
                                       NF.NRPLACAVEIC, NF.DSQTDEVOLU, NF.DSESPECVOLU, NF.DSMARCAVOLU, NF.DSNUMEVOLU,
                                       NF.CDFILIEMIT, NF.CDFILIRECE, NF.DTEMISSAO, NF.CDMODDOCFISC, NF.CDCLIENTE, NF.CDFORNECED, NF.IDORIGDESTNF,
                                       NF.VRCONTABILNF, NF.NRINSJURTRAN, NF.IDTPIJURTRAN, NF.VRPBRUTOVOLU, NF.VRPLIQUVOLU,
                                       TO_CHAR(VRBASECALCNF), TO_CHAR(VRIMPOSTONF), TO_CHAR(VROUTRCRED), TO_CHAR(VRISENTRIB),
                                       NF.NRNOTAFISCALELE,NF.CDTIPOOPER,NF.IDSIMPLESNACIONAL, NF.QTDEPARC)NF )LOOP

           IF C_REGC100.IDSTATUS = 'C' OR C_REGC100.IDSTATUSNFE = 'D' OR C_REGC100.IDSTATUSNFE = 'I' THEN
              V_DATA := '|';
           ELSE
              IF C_REGC100.IDENTRSAIDOP = 'E' THEN
                 V_DATA := TO_CHAR(C_REGC100.DTENTRSAID,'ddmmyyyy') || '|';
              ELSE
                 V_DATA := TO_CHAR(C_REGC100.DTEMISSAO,'ddmmyyyy') || '|';
              END IF;
           END IF;
          V_VRDEBESP := 0;
          /* valor total de imposto fora do periodo - nf saida */
          V_VRFORAPER := 0;
          IF (C_REGC100.SITU_DOC IN ('01', '07')) 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
                   AND CDFISCALNF = '1'
                   AND NVL(IDIMPOSTONF,'1') <> '3';
             EXCEPTION
                WHEN NO_DATA_FOUND THEN
                   V_VRFORAPER := 0;
             END;
             V_TOTFORPER_S := NVL(V_TOTFORPER_S,0)+NVL(V_VRFORAPER,0);
          END IF;

          V_PARTICIPANTE := '';
          /* CODIGO DO PARTICIPANTE */
          SELECT MAX(CDCHAVEREG)
            INTO V_COD_PAR
            FROM TMP_REGDADOSSPED
           WHERE CDCHAVEREG = C_REGC100.COD_PAR
             AND CDREGISTRO = '0150';

          IF (V_COD_PAR IS NULL) AND (C_REGC100.COD_PAR IS NOT NULL) THEN
             V_PARTICIPANTE := RETORNA_PARTICIPANTE_SPED(C_REGC100.COD_PAR);
             IF V_PARTICIPANTE IS NULL THEN
                V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                VALUES
                  ('0150', V_SEQUPROD, C_REGC100.COD_PAR);
             END IF;
          END IF;
          V_COD_PAR := NULL;

          V_PARTICIPANTE := CASE WHEN C_REGC100.IDSTATUS = 'C' OR C_REGC100.IDSTATUSNFE = 'D' OR C_REGC100.IDSTATUSNFE = 'I' THEN  '|'
                                 ELSE REPLACE(REPLACE(REPLACE(REPLACE(NVL(V_PARTICIPANTE,C_REGC100.COD_PAR),'.'),'-'),'/'),' ')||'|' END;

          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC100.DADOS_1||V_PARTICIPANTE||C_REGC100.DADOS_2||V_DATA||C_REGC100.DADOS_3, P_NRSEQARQMAG);
          V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
          V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

          /*REGISTRO C110 - OBSERVACAO DO DOCUMENTO*/
          FOR C_REGC110 IN (SELECT '|C110'                                  ||'|'||
                                   LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0')    ||'|'||
                                   'CUPOM FISCAL REFERENCIADO - COO NUMERO '||TRIM(D.NRCOOCUPOMNF)||'|'  AS DSREGC110,
                                   '|C114'                                  ||'|'||
                                   '2D'                                     ||'|'||
                                   SUBSTR(D.NRSERIEIMPNF,1,21)              ||'|'||
                                   SUBSTR(LPAD(D.NRCOOCUPOMNF,10,'0'),5,6)  ||'|'||
                                   TO_CHAR(C_REGC100.DTEMISSAO,'DDMMYYYY')  ||'|' AS DSREGC114
                              FROM DADOSNF D, NOTAFISCAL N
                             WHERE D.NRLANCTONF  = C_REGC100.NRLANCTONF
                               AND D.CDFILIAL    = C_REGC100.CDFILIAL
                               AND D.NRECFCUPOMNF IS NOT NULL
                               AND D.NRLANCTONF  = N.NRLANCTONF
                               AND D.CDFILIAL    = N.CDFILIAL
                               AND NOT ((N.IDSTATUSNFE = 'A') OR (N.CDMODDOCFISC = '55')))LOOP

             INSERT INTO ARQMAGNETICO
                (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
              VALUES ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC110.DSREGC110 , P_NRSEQARQMAG);
                   V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
                   V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

             /*REGISTRO C114 - CUPOM FISCAL REFERENCIADO*/
             INSERT INTO ARQMAGNETICO
                (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
              VALUES ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC110.DSREGC114 , P_NRSEQARQMAG);
                   V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
                   V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
              V_SEQADIC := V_SEQADIC +1;
          END LOOP;

          /*REGISTRO C110 - OBSERVACAO DO DOCUMENTO -- DADOSADIC*/
          FOR C_REGC110 IN (SELECT TRIM(RETIRA_CARACTER_ESPECIAL(D.DSDADOADIC,'S')) AS DSREGC110
                              FROM DADOSADIC D, NOTAFISCAL N
                             WHERE D.NRLANCTONF  = C_REGC100.NRLANCTONF
                               AND D.CDFILIAL    = C_REGC100.CDFILIAL
                               AND D.NRLANCTONF  = N.NRLANCTONF
                               AND D.CDFILIAL    = N.CDFILIAL
                               AND TRIM(RETIRA_CARACTER_ESPECIAL(D.DSDADOADIC,'S')) IS NOT NULL)LOOP
             V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;

             INSERT INTO ARQMAGNETICO (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
             VALUES ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C110|'||LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0')||'|'||TRIM(SUBSTR(C_REGC110.DSREGC110, 0,499))||'|', P_NRSEQARQMAG);
             V_CONTREGC :=  NVL(V_CONTREGC,0) + 1;
             V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');

             INSERT INTO TMP_REGDADOSSPED (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
             VALUES ('0450', V_SEQUPROD, LPAD(RTRIM(TO_CHAR(V_SEQADIC)),6,'0'), TRIM(SUBSTR(C_REGC110.DSREGC110, 0, 499)));
             V_SEQUENCIAL := LPAD(RTRIM(TO_CHAR(TO_NUMBER('1'))),3,'0');
             V_SEQADIC := V_SEQADIC +1;
          END LOOP;

          /*QUANDO A OPERACAO FOR A PRAZO PARA AS ENTRADAS NOS MODELOS 1 E 1A DEVER SER GERADOS OS REGISTROS REFERENTES A FATURA*/
          IF (C_REGC100.CDMODDOCFISC IN ('01','1A')) AND (C_REGC100.QTDEPARC > 1) THEN
             /*REGISTRO C140*/
             FOR C_REGC140 IN (SELECT '|C140'                                                               ||'|'||
                                      DECODE(N.IDENTRSAIDOP,'E','1','S','0')                                ||'|'||
                                      DECODE(F.IDTPTITULOCRED,'XX','',F.IDTPTITULOCRED)                     ||'|'||
                                      DECODE(F.IDTPTITULOCRED,'99',F.DSTPTITULOCRED,'')                     ||'|'||
                                      LTRIM(N.NRNOTAFISC,'0')                                               ||'|'||
                                      LPAD(RTRIM(TO_CHAR(C_REGC100.QTDEPARC)),2,'0')                        ||'|'||
                                      REPLACE(LTRIM(TO_CHAR(SUM(C.VRORIGPAG),'999999999990.00')), '.', ',') ||'|' AS DSREG_C140
                                 FROM NOTAFISCAL N, FORNECEDOR F, CONTAPAGAR C
                                WHERE N.CDFILIAL   = C_REGC100.CDFILIAL
                                  AND N.NRLANCTONF = C_REGC100.NRLANCTONF
                                  AND N.CDFORNECED = F.CDFORNECED
                                  AND N.CDFILIAL   = C.CDFILIAL
                                  AND N.NRLANCTONF = C.NRLANCTONF
                                GROUP BY N.IDENTRSAIDOP, F.IDTPTITULOCRED, F.DSTPTITULOCRED,
                                      N.NRNOTAFISC, C_REGC100.QTDEPARC) LOOP

                INSERT INTO ARQMAGNETICO
                   (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                VALUES
                   ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC140.DSREG_C140, P_NRSEQARQMAG);
                V_CONTREGC   := NVL(V_CONTREGC,0) + 1;
             END LOOP;

             /*REGISTRO C141*/
             FOR C_REGC141 IN (SELECT '|C141'                                                               ||'|'||
                                      LPAD(RTRIM(TO_CHAR(C.NRPARCELAPAG)),2,'0')                            ||'|'||
                                      TO_CHAR(C.DTORIGVENPAG,'DDMMYYYY')                                    ||'|'||
                                      REPLACE(LTRIM(TO_CHAR(SUM(C.VRORIGPAG),'999999999990.00')), '.', ',') ||'|' AS DSREG_C141
                                 FROM CONTAPAGAR C
                                WHERE C.CDFILIAL   = C_REGC100.CDFILIAL
                                  AND C.NRLANCTONF = C_REGC100.NRLANCTONF
                                GROUP BY C.NRPARCELAPAG, C.DTORIGVENPAG
                                ORDER BY C.NRPARCELAPAG) LOOP

                INSERT INTO ARQMAGNETICO
                   (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                VALUES
                   ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC141.DSREG_C141, P_NRSEQARQMAG);
                V_CONTREGC   := NVL(V_CONTREGC,0) + 1;
             END LOOP;
          END IF;

          BEGIN
            SELECT IDATUESTTPOP
              INTO V_IDATUESTTPOP
              FROM TIPOOPERAC
             WHERE CDTIPOOPER   = C_REGC100.CDTIPOOPER
               AND IDENTRSAIDOP = C_REGC100.IDENTRSAIDOP;
          EXCEPTION
             WHEN NO_DATA_FOUND THEN
                V_IDATUESTTPOP := 'N';
          END;

          /*REGISTRO C170*/
          FOR C_REGC170 IN (SELECT COD_ITEM       || '|' ||
                                  DESCR_COMPL    || '|' ||
                                  QTD            || '|' ||
                                  UNID           || '|' ||
                                  VL_ITEM        || '|' ||
                                  VL_DESC        || '|' ||
                                  IND_MOV        || '|' ||
                                  CST_ICMS       || '|' ||
                                  CFOP           || '|' ||
                                  COD_NAT        || '|' || -- natureza da operacao campo 12
                                  VL_BC_ICMS     || '|' ||
                                  ALIQ_ICMS      || '|' ||
                                  VL_ICMS        || '|' ||
                                  VL_BC_ICMS_ST  || '|' || --16
                                  ALIQ_ST        || '|' || --17
                                  VL_ICMS_ST     || '|' || --18
                                  ''             || '|' || -- indicador de apurao do IPI CAMPO 19
                                  CST_IPI        || '|' || -- cst ipi campo 20
                                  '' 		       || '|' || -- Cdigo enquadramento IPI campo 21
                                  VL_BC_IPI      || '|' || --22
                                  ALIQ_IPI       || '|' || --23
                                  VL_IPI         || '|' || --24
                                  CST_PIS        || '|' || -- cst pis   campo 25
                                  VL_BC_PIS      || '|' ||
                                  ALIQ_PIS       || '|' ||
                                  ''  			    || '|' || -- quantidade base de calculo
                                  ''             || '|' || -- aliquota pis quantidade
                                  VL_PIS         || '|' ||
                                  CST_COFINS     || '|' || -- cst cofins campo 31
                                  VL_BC_COFINS   || '|' ||
                                  ALIQ_COFINS    || '|' ||
                                  ''    	       || '|' || -- quantidade base de calculo
                                  ''             || '|' || -- aliquota cofins quantidade
                                  VL_COFINS      || '|' ||
                                  COD_CTA        || '|' ||
                                  CASE WHEN P_VERSAO >= '013' THEN VL_ABAT_NT || '|' ELSE '' END AS ARQ_C170,  -- codigo da conta analitica contabil deb/cred
                                  CDPRODUTO, UNID, CFOP AS CDFISCOPERIT, DSCDFISOPEIT, CDCLASFISC, COD_CTA
                             FROM (/* NOTAS NAO ELETRONICAS SEM CARGA TRIBUTARIA */
                                   SELECT PR.CDARVPROD AS COD_ITEM,
                                           DECODE(C_REGC100.IDENTRSAIDOP, 'E', TRIM(RETIRA_CARACTER_ESPECIAL(IT.DSPRODEMIS, 'S'))) AS DESCR_COMPL,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '999999999990.000')), '.', ',') AS QTD,
                                           PR.SGUNIDADE  AS UNID,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(IT.VRTOTAITEM,2), '999999999990.00')), '.', ',') AS VL_ITEM,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRENCARGODESC,0),2), '999999999990.00')), '.', ',') AS VL_DESC,
                                           DECODE(NVL(V_IDATUESTTPOP,'N'),'N','1','0') AS IND_MOV,
                                           LPAD(NVL(IP.CDSITUTRIBIT,0),3,'0') AS CST_ICMS,
                                           IP.CDFISCOPERIT AS CFOP,
                                           C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER AS COD_NAT,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRBASECALCIT, 0),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRALIQITEM +CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(IP.VRADICALIQFECP,0) ELSE 0 END, 0),2), '999999999990.00')), '.', ',')   AS ALIQ_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRIMPOSTOIT+CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(IP.VRADICFECP,0) ELSE 0 END, 0),2), '999999999990.00')), '.', ',')  AS VL_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS_ST,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1') + CASE WHEN V_IDSOMAFECPST = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1') ELSE 0 END,2), '999999999990.00')), '.', ',') AS ALIQ_ST,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1') + CASE WHEN V_IDSOMAFECPST = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1') ELSE 0 END,2), '999999999990.00')), '.', ',') AS VL_ICMS_ST,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_IPI) AS CST_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_IPI,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_PIS) AS CST_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_COFINS) AS CST_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                           CONTCTBL_SPED(C_REGC100.IDENTRSAIDOP, PR.CDPRODUTO, C_REGC100.CDTIPOOPER, C_REGC100.FILIAL)  AS COD_CTA,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.IDIMPDESONERAIT,'S',NVL(IP.VRIMPOSTOIT,0),0),2), '999999999990.00')), '.', ',') AS VL_ABAT_NT,
                                           PR.CDPRODUTO, IT.DSCDFISOPEIT, IT.CDPRODNCMTRANS AS CDCLASFISC
                                   FROM ITEMPRODUTO IT, PRODUTO PR, ITEMPROIMPOS IP, IMPOSTO IM
                                   WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                                     AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                     AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                     AND IM.CDIMPOSTO     = IP.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'
                                     AND (ROUND(IT.QTITEMPROD,3) > 0 OR C_REGC100.SITU_DOC IN ('08'))
                                     AND VALOR_IMPOSTO_NFE(IP.CDFILIAL,IP.NRLANCTONF,'03','I','1') = 0
                                   UNION ALL
                                   /* NOTAS NAO ELETRONICAS COM CARGA TRIBUTARIA */
                                   SELECT PR.CDARVPROD AS COD_ITEM,
                                           DECODE(C_REGC100.IDENTRSAIDOP, 'E', TRIM(RETIRA_CARACTER_ESPECIAL(IT.DSPRODEMIS, 'S'))) AS DESCR_COMPL,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '999999999990.000')), '.', ',') AS QTD,
                                           PR.SGUNIDADE AS UNID,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(IT.VRTOTAITEM,2), '999999999990.00')), '.', ',')  AS VL_ITEM,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRENCARGODESC,0),2), '999999999990.00')), '.', ',') AS VL_DESC,
                                           DECODE(NVL(V_IDATUESTTPOP,'N'),'N','1','0') AS IND_MOV,
                                           LPAD(NVL(IP.CDSITUTRIBIT,0),3,'0') AS CST_ICMS,
                                           IP.CDFISCOPERIT AS CFOP,
                                           C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER AS COD_NAT,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRBASECALCIT, 0) + DECODE(V_IDTPCARGATRIB,'O',0,CASE WHEN V_SGESTADO <> 'SP' THEN VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'03','B','1') ELSE 0 END),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRALIQITEM +CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(IP.VRADICALIQFECP,0) ELSE 0 END,0),2), '999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRIMPOSTOIT+CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(IP.VRADICFECP,0) ELSE 0 END,0) + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(C_REGC100.IDENTRSAIDOP, 'S', VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'03','I','1'), CASE WHEN V_SGESTADO <> 'SP' THEN VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'03','I','1') ELSE 0 END)),2), '999999999990.00')), '.', ',') AS VL_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS_ST,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1') + CASE WHEN V_IDSOMAFECPST ='S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1') ELSE 0 END,2), '999999999990.00')), '.', ',') AS ALIQ_ST,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1') + CASE WHEN V_IDSOMAFECPST ='S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1') ELSE 0 END,2), '999999999990.00')), '.', ',') AS VL_ICMS_ST,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_IPI) AS CST_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_IPI,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_PIS) AS CST_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,

                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_COFINS) AS CST_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                           CONTCTBL_SPED(C_REGC100.IDENTRSAIDOP, PR.CDPRODUTO, C_REGC100.CDTIPOOPER, C_REGC100.FILIAL)  AS COD_CTA,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.IDIMPDESONERAIT,'S',NVL(IP.VRIMPOSTOIT,0),0),2), '999999999990.00')), '.', ',') AS VL_ABAT_NT,
                                           PR.CDPRODUTO, IT.DSCDFISOPEIT, IT.CDPRODNCMTRANS AS CDCLASFISC
                                   FROM ITEMPRODUTO IT, PRODUTO PR, ITEMPROIMPOS IP, IMPOSTO IM
                                   WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                                     AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                     AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                     AND IM.CDIMPOSTO     = IP.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'
                                     AND (ROUND(IT.QTITEMPROD,3) > 0 OR C_REGC100.SITU_DOC IN ('08'))
                                     AND VALOR_IMPOSTO_NFE(IP.CDFILIAL,IP.NRLANCTONF,'03','I','1') > 0
                                   UNION ALL
                                   /* NOTAS DE ENTRADA ELETRONICAS SEM CARGA TRIBUTARIA */
                                   SELECT PR.CDARVPROD AS COD_ITEM,
                                           DECODE(C_REGC100.IDENTRSAIDOP, 'E', TRIM(RETIRA_CARACTER_ESPECIAL(IT.DSPRODEMIS, 'S'))) AS DESCR_COMPL,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '999999999990.000')), '.', ',') AS QTD,
                                           PR.SGUNIDADE AS UNID,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(IT.VRTOTAITEM,2), '999999999990.00')), '.', ',') AS VL_ITEM,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRENCARGODESC,0),2), '999999999990.00')), '.', ',') AS VL_DESC,
                                           DECODE(NVL(V_IDATUESTTPOP,'N'),'N','1','0') AS IND_MOV,
                                           DECODE(C_REGC100.IDSIMPLESNACIONAL,'S',(DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),LPAD(NVL(IT.CDFISCOPERSN,0),3,'0'))),LPAD(NVL(IP.CDSITUTRIBIT,0),3,'0')) AS CST_ICMS,
                                           IP.CDFISCOPERIT AS CFOP,
                                           C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER AS COD_NAT,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRBASECALCIT, 0),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRALIQITEM+NVL(IP.VRADICALIQFECP,0),0),2), '999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRIMPOSTOIT+NVL(IP.VRADICFECP,0),0),2), '999999999990.00')), '.', ',') AS VL_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS_ST,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1') + VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_ST,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1') + VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_ICMS_ST,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_IPI) AS CST_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_IPI,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_PIS) AS CST_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_COFINS) AS CST_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                           CONTCTBL_SPED(C_REGC100.IDENTRSAIDOP, PR.CDPRODUTO, C_REGC100.CDTIPOOPER, C_REGC100.FILIAL)  AS COD_CTA,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.IDIMPDESONERAIT,'S',NVL(IP.VRIMPOSTOIT,0),0),2), '999999999990.00')), '.', ',') AS VL_ABAT_NT,
                                           PR.CDPRODUTO, IT.DSCDFISOPEIT, IT.CDPRODNCMTRANS AS CDCLASFISC
                                    FROM ITEMPRODUTO IT, PRODUTO PR, ITEMPROIMPOS IP, IMPOSTO IM
                                   WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                                     AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                     AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                     AND IM.CDIMPOSTO     = IP.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'
                                     AND C_REGC100.IDSTATUSNFE <> 'A'
                                     AND(ROUND(IT.QTITEMPROD,3) > 0 OR C_REGC100.SITU_DOC IN ('08'))
                                     AND VALOR_IMPOSTO_NFE(IP.CDFILIAL,IP.NRLANCTONF,'03','I','1') = 0
                                   UNION ALL
                                   /* NOTAS DE ENTRADA ELETRONICAS COM CARGA TRIBUTARIA */
                                   SELECT PR.CDARVPROD AS COD_ITEM,
                                           DECODE(C_REGC100.IDENTRSAIDOP, 'E', TRIM(RETIRA_CARACTER_ESPECIAL(IT.DSPRODEMIS,'S'))) AS DESCR_COMPL,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '999999999990.000')), '.', ',') AS QTD,
                                           PR.SGUNIDADE AS UNID,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(IT.VRTOTAITEM,2), '999999999990.00')), '.', ',') AS VL_ITEM,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(NVL(IT.VRENCARGODESC,0),2), '999999999990.00')), '.', ',') AS VL_DESC,
                                           DECODE(NVL(V_IDATUESTTPOP,'N'),'N','1','0') AS IND_MOV,
                                           DECODE(C_REGC100.IDSIMPLESNACIONAL,'S',(DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),LPAD(NVL(IT.CDFISCOPERSN,0),3,'0'))), LPAD(NVL(IP.CDSITUTRIBIT,0),3,'0')) AS CST_ICMS,
                                           IP.CDFISCOPERIT AS CFOP,
                                           C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER AS COD_NAT,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRBASECALCIT, 0) + DECODE(V_IDTPCARGATRIB,'O',0,CASE WHEN V_SGESTADO <> 'SP' THEN VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'03','B','1') ELSE 0 END),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRALIQITEM +NVL(IP.VRADICALIQFECP,0),0),2), '999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.CDFISCALIT, '1', IP.VRIMPOSTOIT+NVL(IP.VRADICFECP,0),0) + DECODE(V_IDTPCARGATRIB,'O',0,CASE WHEN V_SGESTADO <> 'SP' THEN VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'03','I','1') ELSE 0 END),2), '999999999990.00')), '.', ',') AS VL_ICMS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS_ST,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1') + VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_ST,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1') + VALOR_FECP_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_CDIMPOSTRIB, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_ICMS_ST,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_IPI) AS CST_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_IPI,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_IPI, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_IPI,
                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_PIS) AS CST_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_PIS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_PIS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,

                                           (SELECT MAX(CDSITUTRIBITNFE)
                                              FROM ITEMPROIMPOS N, IMPOSTO IM
                                             WHERE IP.CDFILIAL     = N.CDFILIAL
                                               AND IP.NRLANCTONF   = N.NRLANCTONF
                                               AND IP.NRSEQITPROD  = N.NRSEQITPROD
                                               AND IM.CDIMPOSTO    = N.CDIMPOSTO
                                               AND N.NRITPROIMPOS  = '1'
                                               AND IM.CDIMPOSTONFE = V_COFINS) AS CST_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'B','1'),2), '999999999990.00')), '.', ',') AS VL_BC_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'A','1'),2), '999999999990.00')), '.', ',') AS ALIQ_COFINS,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL, IP.NRLANCTONF, IP.NRSEQITPROD, V_COFINS, 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                           CONTCTBL_SPED(C_REGC100.IDENTRSAIDOP, PR.CDPRODUTO, C_REGC100.CDTIPOOPER, C_REGC100.FILIAL)  AS COD_CTA,
                                           REPLACE(LTRIM(TO_CHAR(ROUND(DECODE(IP.IDIMPDESONERAIT,'S',NVL(IP.VRIMPOSTOIT,0),0),2), '999999999990.00')), '.', ',') AS VL_ABAT_NT,
                                           PR.CDPRODUTO, IT.DSCDFISOPEIT, IT.CDPRODNCMTRANS AS CDCLASFISC
                                    FROM ITEMPRODUTO IT, PRODUTO PR, ITEMPROIMPOS IP, IMPOSTO IM
                                   WHERE IT.CDFILIAL      = C_REGC100.CDFILIAL
                                     AND IT.NRLANCTONF    = C_REGC100.NRLANCTONF
                                     AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                     AND IM.CDIMPOSTO     = IP.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'
                                     AND C_REGC100.IDSTATUSNFE <> 'A'
                                     AND (ROUND(IT.QTITEMPROD,3) > 0 OR C_REGC100.SITU_DOC IN ('08'))
                                     AND VALOR_IMPOSTO_NFE(IP.CDFILIAL,IP.NRLANCTONF,'03','I','1') > 0)) 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;

            /* CODIGO DO CFOP */
            SELECT MAX(CDCHAVEREG)
              INTO V_COD_NAT
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER
               AND CDREGISTRO = '0400';

            IF (V_COD_NAT IS NULL) AND (C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1, CDCAMPO2)
               VALUES
                 ('0400', V_SEQUPROD, C_REGC100.IDENTRSAIDOP||C_REGC100.CDTIPOOPER,C_REGC100.IDENTRSAIDOP,C_REGC100.CDTIPOOPER);
            END IF;
            V_COD_NAT := NULL;

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

            IF (V_UNID IS NULL) AND (C_REGC170.UNID IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
               VALUES
                 ('0190', V_SEQUPROD, C_REGC170.UNID);
            END IF;
            V_UNID := NULL;

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

            IF (V_PRODUTO IS NULL) AND (C_REGC170.CDPRODUTO IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                 (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3)
               VALUES
                 ('0200', V_SEQUPROD, C_REGC170.CDPRODUTO, C_REGC170.CDCLASFISC);
            END IF;
            V_PRODUTO := NULL;

            /* PLANO DE CONTAS REFERENCIAL */
            SELECT MAX(CDCHAVEREG)
              INTO V_CONTCTBL
              FROM TMP_REGDADOSSPED
             WHERE CDCHAVEREG = C_REGC170.COD_CTA
               AND CDREGISTRO = '0500';

            IF (V_CONTCTBL IS NULL) AND (C_REGC170.COD_CTA IS NOT NULL) THEN
               V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
               INSERT INTO TMP_REGDADOSSPED
                  (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
               VALUES
                  ('0500', V_SEQUPROD, C_REGC170.COD_CTA);
            END IF;
            V_CONTCTBL := NULL;
          END LOOP;

          IF P_VERSAO NOT IN ( '001', '002') THEN
             /*REGISTRO C190*/
             IF (C_REGC100.IDSIMPLESNACIONAL <> 'S') OR (C_REGC100.IDSIMPLESNACIONAL = 'S' AND C_REGC100.CDMODDOCFISC <> '55') 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(T.BC_ST,2),     '999999999990.00')), '.', ',') || '|' ||
                                          REPLACE(LTRIM(TO_CHAR(ROUND(T.IMP_ST,2),    '999999999990.00')), '.', ',') || '|' ||
                                          REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_RED_BC,2), '999999999990.00')), '.', ',') || '|' ||
                                          REPLACE(LTRIM(TO_CHAR(ROUND(VL_IPI,2),      '999999999990.00')), '.', ',') || '|' ||
                                          T.COD_OBS || '|') ARQ_C190,
                                          T.VL_ICMS, T.CFOP AS COD_NAT, T.CST_ICMS, T.ALIQ_ICMS, T.COD_OBS, T.DSCFOPLMAG
                                  FROM (/* NOTAS DE SAIDA SEM CARGA TRIBUTARIA */
                                        SELECT I.CDSITUTRIBIT AS CST_ICMS,
                                               I.CDFISCOPERIT AS CFOP,
                                               DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0),0) AS ALIQ_ICMS,
                                               SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0) VL_OPR,
                                               SUM(DECODE(I.CDFISCALIT, '1', I.VRBASECALCIT, 0)) AS VL_BC_ICMS,
                                               SUM(DECODE(I.CDFISCALIT, '1', I.VRIMPOSTOIT+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0),0)) AS VL_ICMS,
                                               NVL(SUM(DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL),0)),0) AS VL_RED_BC,
                                               SUM((SELECT NVL(SUM(VRIMPOSTOIT),0)
                                                      FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                     WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                           (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                           (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                           (IT.NRITPROIMPOS = '1'                  ) AND
                                                           (IT.CDFISCALIT   = '1'                  ) AND
                                                           (IM.CDIMPOSTONFE = '10'                 ) AND
                                                           I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                               C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + DECODE(V_IDSOMAFECPST,'S',VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1'),0)) AS IMP_ST,
                                               C.DSCFOPLMAG
                                          FROM ITEMPROIMPOS I, IMPOSTO IM, CFOPLIVROMAGTPOP C
                                         WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                           AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                           AND C_REGC100.IDENTRSAIDOP = 'S'
                                           AND I.NRITPROIMPOS  = '1'
                                           AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                           AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                           AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                           AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                           AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                           AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                           AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') = 0
                                         GROUP BY I.CDSITUTRIBIT, I.CDFISCOPERIT, DECODE(I.CDFISCALIT,'1',I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0),0),
                                               C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG
                                        UNION ALL
                                        /* NOTAS DE SAIDA COM CARGA TRIBUTARIA */
                                        SELECT I.CDSITUTRIBIT AS CST_ICMS,
                                               I.CDFISCOPERIT AS CFOP,
                                               DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0),0) AS ALIQ_ICMS,
                                               SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0) VL_OPR,
                                               DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'03','B','1')))) AS VL_BC_ICMS,
                                               SUM(DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0)+VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'03','I','1'))) AS VL_ICMS,
                                               NVL(SUM(DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL),0)),0) AS VL_RED_BC,
                                               SUM((SELECT NVL(SUM(VRIMPOSTOIT),0)
                                                      FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                     WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                           (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                           (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                           (IT.NRITPROIMPOS = '1'                  ) AND
                                                           (IT.CDFISCALIT   = '1'                  ) AND
                                                           (IM.CDIMPOSTONFE = '10'                 ) AND
                                                           I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                               C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + DECODE(V_IDSOMAFECPST,'S',VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1'),0)) AS IMP_ST,
                                               C.DSCFOPLMAG
                                          FROM ITEMPROIMPOS I, IMPOSTO IM, CFOPLIVROMAGTPOP C
                                         WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                           AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                           AND C_REGC100.IDENTRSAIDOP = 'S'
                                           AND I.NRITPROIMPOS  = '1'
                                           AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                           AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                           AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                           AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                           AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                           AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                           AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') > 0
                                         GROUP BY I.CDSITUTRIBIT, I.CDFISCOPERIT, DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0),0),
                                               C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG
                                        UNION ALL
                                        /* NOTAS DE ENTRADA SEM CARGA TRIBUTARIA */
                                        SELECT I.CDSITUTRIBIT AS CST_ICMS,
                                               I.CDFISCOPERIT AS CFOP,
                                               DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+NVL(I.VRADICALIQFECP,0),0) AS ALIQ_ICMS,
                                               SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0) VL_OPR,
                                               SUM(DECODE(I.CDFISCALIT, '1', I.VRBASECALCIT, 0)) AS VL_BC_ICMS,
                                               SUM(DECODE(I.CDFISCALIT, '1', I.VRIMPOSTOIT+NVL(I.VRADICFECP,0),0)) AS VL_ICMS,
                                               NVL(SUM(DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL),0)),0) AS VL_RED_BC,
                                               SUM((SELECT NVL(SUM(IT.VRIMPOSTOIT),0)
                                                      FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                     WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                           (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                           (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                           (IT.NRITPROIMPOS = '1'                  ) AND
                                                           (IT.CDFISCALIT   = '1'                  ) AND
                                                           (IM.CDIMPOSTONFE = '10'                 ) AND
                                                           I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                               C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1')) AS IMP_ST,
                                               C.DSCFOPLMAG
                                          FROM ITEMPROIMPOS I, IMPOSTO IM, CFOPLIVROMAGTPOP C
                                         WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                           AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                           AND C_REGC100.IDENTRSAIDOP = 'E'
                                           AND I.NRITPROIMPOS  = '1'
                                           AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                           AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                           AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                           AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                           AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                           AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                           AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') = 0
                                         GROUP BY I.CDSITUTRIBIT, I.CDFISCOPERIT, DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+NVL(I.VRADICALIQFECP,0),0),
                                               C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG
                                        UNION ALL
                                        /* NOTAS DE ENTRADA COM CARGA TRIBUTARIA */
                                        SELECT I.CDSITUTRIBIT AS CST_ICMS,
                                               I.CDFISCOPERIT AS CFOP,
                                               DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+NVL(I.VRADICALIQFECP,0),0) AS ALIQ_ICMS,
                                               SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0) VL_OPR,
                                               SUM(DECODE(CDFISCALIT,'1',I.VRBASECALCIT, 0) + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(I.CDFISCALIT, '1', CASE WHEN V_SGESTADO <> 'SP' THEN I.VRBASECALCIT ELSE 0 END, 0))) AS VL_BC_ICMS,
                                               SUM(DECODE(CDFISCALIT,'1',I.VRIMPOSTOIT,  0) + NVL(I.VRADICFECP,0) + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(I.CDFISCALIT, '1', CASE WHEN V_SGESTADO <> 'SP' THEN I.VRIMPOSTOIT  ELSE 0 END, 0))) AS VL_ICMS,
                                               NVL(SUM(DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL),0)),0) AS VL_RED_BC,
                                               SUM((SELECT NVL(SUM(VRIMPOSTOIT),0)
                                                      FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                     WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                           (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                           (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                           (IT.NRITPROIMPOS = '1'                  ) AND
                                                           (IT.CDFISCALIT   = '1'                  ) AND
                                                           (IM.CDIMPOSTONFE = '10'                 ) AND
                                                           I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                               C.CDTIPOOPER||C.CDCFOPLMAG  AS COD_OBS,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1')) AS IMP_ST,
                                               C.DSCFOPLMAG
                                          FROM ITEMPROIMPOS I, IMPOSTO IM, CFOPLIVROMAGTPOP C
                                         WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                           AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                           AND C_REGC100.IDENTRSAIDOP = 'E'
                                           AND I.NRITPROIMPOS  = '1'
                                           AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                           AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                           AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                           AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                           AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                           AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                           AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') > 0
                                         GROUP BY I.CDSITUTRIBIT, I.CDFISCOPERIT, DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+NVL(I.VRADICALIQFECP,0),0),
                                               C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG) T ) LOOP

                    V_COD_OBS := NULL;
                    SELECT MAX(CDCHAVEREG)
                      INTO V_COD_OBS
                      FROM TMP_REGDADOSSPED
                     WHERE CDCHAVEREG = C_REGC190.COD_OBS
                       AND CDREGISTRO = '0460';

                    IF (V_COD_OBS IS NULL) AND (C_REGC190.COD_OBS IS NOT NULL) THEN
                       V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                       INSERT INTO TMP_REGDADOSSPED
                          (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
                       VALUES
                          ('0460', V_SEQUPROD, C_REGC190.COD_OBS, C_REGC190.DSCFOPLMAG);
                    END IF;

                    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;

                    IF P_VERSAO >= '013' THEN
                       FOR C_REGC191 IN (SELECT '|C191'          || '|' ||
                                                REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN SUBSTR(IT.CDSITUTRIBIT,2,2) IN ('00','10','20','51','70','90') AND IM.CDIMPOSTONFE = '00'
                                                     THEN ROUND(IT.VRADICFECP,2)
                                                     ELSE 0 END),'999999999990.00')), '.', ',') || '|' ||
                                                REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN SUBSTR(IT.CDSITUTRIBIT,2,2) IN ('10','30','70','90') AND IM.CDIMPOSTONFE = '80'
                                                     THEN ROUND(IT.VRADICFECP,2)
                                                     ELSE 0 END),'999999999990.00')), '.', ',') || '|' ||
                                                REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN SUBSTR(IT.CDSITUTRIBIT,2,2) IN ('60')
                                                     THEN ROUND(IT.VRADICFECP,2)
                                                     ELSE 0 END),'999999999990.00')), '.', ',') || '|' AS ARQ_C191, IT.CDFISCOPERIT
                                           FROM ITEMPROIMPOS IT, ITEMPROIMPOS II, IMPOSTO IM
                                          WHERE IT.CDFILIAL     = C_REGC100.CDFILIAL
                                            AND IT.NRLANCTONF   = C_REGC100.NRLANCTONF
                                            AND IT.CDFISCOPERIT = C_REGC190.COD_NAT
                                            AND IT.CDSITUTRIBIT = C_REGC190.CST_ICMS
                                            AND IT.VRALIQITEM + CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(IT.VRADICALIQFECP,0) ELSE 0 END = C_REGC190.ALIQ_ICMS
                                            AND IT.NRITPROIMPOS = '1'
                                            AND IT.CDFILIAL     = II.CDFILIAL
                                            AND IT.NRLANCTONF   = II.NRLANCTONF
                                            AND IT.NRSEQITPROD  = II.NRSEQITPROD
                                            AND II.CDFISCOPERIT = C_REGC190.COD_NAT
                                            AND II.CDSITUTRIBIT = C_REGC190.CST_ICMS
                                            AND II.CDIMPOSTO    = IM.CDIMPOSTO
                                            AND IM.CDIMPOSTONFE IN ('00','80')
                                            AND II.VRADICFECP   > 0
                                          GROUP BY IT.CDFISCOPERIT) LOOP
                          INSERT INTO ARQMAGNETICO
                             (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                          VALUES
                             ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC191.ARQ_C191, P_NRSEQARQMAG);
                          V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                       END LOOP;
                    END IF;
                 END LOOP;
             END IF;

             IF (C_REGC100.IDSIMPLESNACIONAL = 'S' AND C_REGC100.CDMODDOCFISC = '55') 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(T.BC_ST,2),     '999999999990.00')), '.', ',') || '|' ||
                                          REPLACE(LTRIM(TO_CHAR(ROUND(T.IMP_ST,2),    '999999999990.00')), '.', ',') || '|' ||
                                          REPLACE(LTRIM(TO_CHAR(ROUND(T.VL_RED_BC,2), '999999999990.00')), '.', ',') || '|' ||
                                          REPLACE(LTRIM(TO_CHAR(ROUND(VL_IPI,2),      '999999999990.00')), '.', ',') || '|' ||
                                          T.COD_OBS || '|') ARQ_C190,
                                          T.VL_ICMS, T.CFOP AS COD_NAT, T.CST_ICMS, T.ALIQ_ICMS, T.COD_OBS, T.DSCFOPLMAG
                                  FROM (/* NOTAS DE SAIDA SEM CARGA TRIBUTARIA */
                                        SELECT DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),IT.CDFISCOPERSN) AS CST_ICMS,
                                               I.CDFISCOPERIT AS CFOP,
                                               DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0),0) AS ALIQ_ICMS,
                                               SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, IT.CDFISCOPERSN, C_REGC100.CDMODDOCFISC, C_REGC100.IDSIMPLESNACIONAL)),0) VL_OPR,
                                               SUM(DECODE(I.CDFISCALIT, '1', I.VRBASECALCIT, 0)) AS VL_BC_ICMS,
                                               SUM(DECODE(I.CDFISCALIT, '1', I.VRIMPOSTOIT+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0),0)) AS VL_ICMS,
                                               NVL(SUM(DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, IT.CDFISCOPERSN, C_REGC100.CDMODDOCFISC, C_REGC100.IDSIMPLESNACIONAL),0)),0) AS VL_RED_BC,
                                               SUM((SELECT NVL(SUM(VRIMPOSTOIT),0)
                                                      FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                     WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                           (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                           (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                           (IT.NRITPROIMPOS = '1'                  ) AND
                                                           (IT.CDFISCALIT   = '1'                  ) AND
                                                           (IM.CDIMPOSTONFE = '10'                 ) AND
                                                           I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                               C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + DECODE(V_IDSOMAFECPST,'S',VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1'),0)) AS IMP_ST,
                                               C.DSCFOPLMAG
                                          FROM ITEMPROIMPOS I, IMPOSTO IM, ITEMPRODUTO IT, CFOPLIVROMAGTPOP C
                                         WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                           AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                           AND C_REGC100.IDENTRSAIDOP = 'S'
                                           AND I.NRITPROIMPOS  = '1'
                                           AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                           AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                           AND I.CDFILIAL      = IT.CDFILIAL
                                           AND I.NRLANCTONF    = IT.NRLANCTONF
                                           AND I.NRSEQITPROD   = IT.NRSEQITPROD
                                           AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                           AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                           AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                           AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                           AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') = 0
                                         GROUP BY DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),IT.CDFISCOPERSN), I.CDFISCOPERIT, DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0), 0) , C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG
                                        UNION ALL
                                        /* NOTAS DE SAIDA COM CARGA TRIBUTARIA */
                                        SELECT DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),IT.CDFISCOPERSN) AS CST_ICMS,
                                               I.CDFISCOPERIT AS CFOP,
                                               DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0),0) AS ALIQ_ICMS,
                                               SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, IT.CDFISCOPERSN, C_REGC100.CDMODDOCFISC, C_REGC100.IDSIMPLESNACIONAL)),0) VL_OPR,
                                               DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'03','B','1')))) AS VL_BC_ICMS,
                                               SUM(DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0)+VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'03','I','1'))) AS VL_ICMS,
                                               NVL(SUM(DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, IT.CDFISCOPERSN, C_REGC100.CDMODDOCFISC, C_REGC100.IDSIMPLESNACIONAL),0)),0) AS VL_RED_BC,
                                               SUM((SELECT NVL(SUM(VRIMPOSTOIT),0)
                                                      FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                     WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                           (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                           (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                           (IT.NRITPROIMPOS = '1'                  ) AND
                                                           (IT.CDFISCALIT   = '1'                  ) AND
                                                           (IM.CDIMPOSTONFE = '10'                 ) AND
                                                           I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                               C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + DECODE(V_IDSOMAFECPST,'S',VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1'),0)) AS IMP_ST,
                                               C.DSCFOPLMAG
                                          FROM ITEMPROIMPOS I, IMPOSTO IM, ITEMPRODUTO IT, CFOPLIVROMAGTPOP C
                                         WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                           AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                           AND C_REGC100.IDENTRSAIDOP = 'S'
                                           AND I.NRITPROIMPOS  = '1'
                                           AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                           AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                           AND I.CDFILIAL      = IT.CDFILIAL
                                           AND I.NRLANCTONF    = IT.NRLANCTONF
                                           AND I.NRSEQITPROD   = IT.NRSEQITPROD
                                           AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                           AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                           AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                           AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                           AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') > 0
                                         GROUP BY DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),IT.CDFISCOPERSN), I.CDFISCOPERIT, DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0),0), C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG
                                        UNION ALL
                                        /* NOTAS DE ENTRADA SEM CARGA TRIBUTARIA */
                                        SELECT DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),IT.CDFISCOPERSN) AS CST_ICMS,
                                               I.CDFISCOPERIT AS CFOP,
                                               DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+NVL(I.VRADICALIQFECP,0),0) AS ALIQ_ICMS,
                                               SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, IT.CDFISCOPERSN, C_REGC100.CDMODDOCFISC, C_REGC100.IDSIMPLESNACIONAL)),0) VL_OPR,
                                               SUM(DECODE(I.CDFISCALIT, '1', I.VRBASECALCIT, 0)) AS VL_BC_ICMS,
                                               SUM(DECODE(I.CDFISCALIT, '1', I.VRIMPOSTOIT+NVL(I.VRADICFECP,0),0)) AS VL_ICMS,
                                               NVL(SUM(DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, IT.CDFISCOPERSN, C_REGC100.CDMODDOCFISC, C_REGC100.IDSIMPLESNACIONAL),0)),0) AS VL_RED_BC,
                                               SUM((SELECT NVL(SUM(VRIMPOSTOIT),0)
                                                      FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                     WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                           (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                           (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                           (IT.NRITPROIMPOS = '1'                  ) AND
                                                           (IT.CDFISCALIT   = '1'                  ) AND
                                                           (IM.CDIMPOSTONFE = '10'                 ) AND
                                                           I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                               C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1')) AS IMP_ST,
                                               C.DSCFOPLMAG
                                          FROM ITEMPROIMPOS I, IMPOSTO IM, ITEMPRODUTO IT, CFOPLIVROMAGTPOP C
                                         WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                           AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                           AND C_REGC100.IDENTRSAIDOP = 'E'
                                           AND I.NRITPROIMPOS  = '1'
                                           AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                           AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                           AND I.CDFILIAL      = IT.CDFILIAL
                                           AND I.NRLANCTONF    = IT.NRLANCTONF
                                           AND I.NRSEQITPROD   = IT.NRSEQITPROD
                                           AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                           AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                           AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                           AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                           AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') = 0
                                         GROUP BY DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),IT.CDFISCOPERSN), I.CDFISCOPERIT, DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+NVL(I.VRADICALIQFECP,0),0) , C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG
                                        UNION ALL
                                        /* NOTAS DE ENTRADA COM CARGA TRIBUTARIA */
                                        SELECT DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),IT.CDFISCOPERSN) AS CST_ICMS,
                                               I.CDFISCOPERIT AS CFOP,
                                               DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+NVL(I.VRADICALIQFECP,0),0) AS ALIQ_ICMS,
                                               SUM(I.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, IT.CDFISCOPERSN, C_REGC100.CDMODDOCFISC, C_REGC100.IDSIMPLESNACIONAL)),0) VL_OPR,
                                               SUM(DECODE(I.CDFISCALIT,'1',I.VRBASECALCIT, 0) + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(I.CDFISCALIT,'1',CASE WHEN V_SGESTADO <> 'SP' THEN I.VRBASECALCIT ELSE 0 END, 0))) AS VL_BC_ICMS,
                                               SUM(DECODE(I.CDFISCALIT,'1',I.VRIMPOSTOIT,  0) + NVL(I.VRADICFECP,0) + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(I.CDFISCALIT,'1',CASE WHEN V_SGESTADO <> 'SP' THEN I.VRIMPOSTOIT  ELSE 0 END, 0))) AS VL_ICMS,
                                               NVL(SUM(DECODE(I.CDFISCALIT,'1',VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, IT.CDFISCOPERSN, C_REGC100.CDMODDOCFISC, C_REGC100.IDSIMPLESNACIONAL),0)),0) AS VL_RED_BC,
                                               SUM((SELECT NVL(SUM(VRIMPOSTOIT),0)
                                                      FROM ITEMPROIMPOS IT, IMPOSTO IM
                                                     WHERE (IT.CDFILIAL     = I.CDFILIAL           ) AND
                                                           (IT.NRLANCTONF   = I.NRLANCTONF         ) AND
                                                           (IT.CDIMPOSTO    = IM.CDIMPOSTO         ) AND
                                                           (IT.NRITPROIMPOS = '1'                  ) AND
                                                           (IT.CDFISCALIT   = '1'                  ) AND
                                                           (IM.CDIMPOSTONFE = '10'                 ) AND
                                                           I.NRSEQITPROD = IT.NRSEQITPROD         )) AS VL_IPI,
                                               C.CDTIPOOPER||C.CDCFOPLMAG AS COD_OBS,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','B','1')) AS BC_ST,
                                               SUM(VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1') + VALOR_FECP_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'80','I','1')) AS IMP_ST,
                                               C.DSCFOPLMAG
                                          FROM ITEMPROIMPOS I, IMPOSTO IM, ITEMPRODUTO IT, CFOPLIVROMAGTPOP C
                                         WHERE I.CDFILIAL      = C_REGC100.CDFILIAL
                                           AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                           AND C_REGC100.IDENTRSAIDOP = 'E'
                                           AND I.NRITPROIMPOS  = '1'
                                           AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                           AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                           AND I.CDFILIAL      = IT.CDFILIAL
                                           AND I.NRLANCTONF    = IT.NRLANCTONF
                                           AND I.NRSEQITPROD   = IT.NRSEQITPROD
                                           AND C.IDENTRSAIDOP(+) = C_REGC100.IDENTRSAIDOP
                                           AND C.CDTIPOOPER(+)   = C_REGC100.CDTIPOOPER
                                           AND C.CDCFOPLMAG(+)   = I.CDFISCOPERIT
                                           AND C.CDLIVROMAG(+)   = P_CDLIVROMAGADIC
                                           AND VALOR_IMPOSTO_NFE(I.CDFILIAL,I.NRLANCTONF,'03','I','1') > 0
                                          GROUP BY DECODE(LENGTH(IT.CDFISCOPERSN),4,SUBSTR(IT.CDFISCOPERSN,2,3),IT.CDFISCOPERSN), I.CDFISCOPERIT, DECODE(I.CDFISCALIT, '1', I.VRALIQITEM+NVL(I.VRADICALIQFECP,0),0), C.CDTIPOOPER||C.CDCFOPLMAG, C.DSCFOPLMAG ) T ) LOOP

                     V_COD_OBS := NULL;
                    SELECT MAX(CDCHAVEREG)
                      INTO V_COD_OBS
                      FROM TMP_REGDADOSSPED
                     WHERE CDCHAVEREG = C_REGC190.COD_OBS
                       AND CDREGISTRO = '0460';

                    IF (V_COD_OBS IS NULL) AND (C_REGC190.COD_OBS IS NOT NULL) THEN
                       V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                       INSERT INTO TMP_REGDADOSSPED
                          (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
                       VALUES
                          ('0460', V_SEQUPROD, C_REGC190.COD_OBS, C_REGC190.DSCFOPLMAG);
                    END IF;

                    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;

                    IF P_VERSAO >= '013' THEN
                       FOR C_REGC191 IN (SELECT '|C191'          || '|' ||
                                                REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN DECODE(LENGTH(IP.CDFISCOPERSN),4,SUBSTR(IP.CDFISCOPERSN,2,3),IP.CDFISCOPERSN) <> '500' AND IM.CDIMPOSTONFE = '00'
                                                     THEN ROUND(IT.VRADICFECP,2)
                                                     ELSE 0 END),'999999999990.00')), '.', ',') || '|' ||
                                                REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN DECODE(LENGTH(IP.CDFISCOPERSN),4,SUBSTR(IP.CDFISCOPERSN,2,3),IP.CDFISCOPERSN) IN ('201','202','203','900') AND IM.CDIMPOSTONFE = '80'
                                                     THEN ROUND(IT.VRADICFECP,2)
                                                     ELSE 0 END),'999999999990.00')), '.', ',') || '|' ||
                                                REPLACE(LTRIM(TO_CHAR(SUM(CASE WHEN DECODE(LENGTH(IP.CDFISCOPERSN),4,SUBSTR(IP.CDFISCOPERSN,2,3),IP.CDFISCOPERSN) = '500'
                                                     THEN ROUND(IT.VRADICFECP,2)
                                                     ELSE 0 END),'999999999990.00')), '.', ',') || '|' AS ARQ_C191, IT.CDFISCOPERIT
                                           FROM ITEMPROIMPOS IT, ITEMPROIMPOS II, IMPOSTO IM, ITEMPRODUTO IP
                                          WHERE IT.CDFILIAL     = C_REGC100.CDFILIAL
                                            AND IT.NRLANCTONF   = C_REGC100.NRLANCTONF
                                            AND IT.CDFISCOPERIT = C_REGC190.COD_NAT
                                            AND IT.VRALIQITEM + CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(IT.VRADICALIQFECP,0) ELSE 0 END = C_REGC190.ALIQ_ICMS
                                            AND IT.NRITPROIMPOS = '1'
                                            AND IT.CDFILIAL     = II.CDFILIAL
                                            AND IT.NRLANCTONF   = II.NRLANCTONF
                                            AND IT.NRSEQITPROD  = II.NRSEQITPROD
                                            AND II.CDFISCOPERIT = C_REGC190.COD_NAT
                                            AND II.CDIMPOSTO    = IM.CDIMPOSTO
                                            AND IM.CDIMPOSTONFE IN ('00','80')
                                            AND II.VRADICFECP   > 0
                                            AND II.CDFILIAL     = IP.CDFILIAL
                                            AND II.NRLANCTONF   = IP.NRLANCTONF
                                            AND II.NRSEQITPROD  = IP.NRSEQITPROD
                                            AND DECODE(LENGTH(IP.CDFISCOPERSN),4,SUBSTR(IP.CDFISCOPERSN,2,3),IP.CDFISCOPERSN) = C_REGC190.CST_ICMS
                                          GROUP BY IT.CDFISCOPERIT) LOOP
                          INSERT INTO ARQMAGNETICO
                             (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                          VALUES
                             ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC191.ARQ_C191, P_NRSEQARQMAG);
                          V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                       END LOOP;
                    END IF;
                 END LOOP;
             END IF;
          END IF;
          /* geracao dos registros com diferencial  de aliquota */
          IF P_IDDIFALIQ = 'S' THEN
             SELECT MAX(SGESTADO)
               INTO V_SGESTADO
               FROM INSCRESTAD
              WHERE CDEMPRESA = P_CDEMPRESA
                AND CDINSCESTA = P_CDINSCESTA;

             IF C_REGC100.IDENTRSAIDOP = 'E' THEN

                /*BUSCA O CODIGO DE AJUSTE CADASTRADO PARA ESTA IE*/
                BEGIN
                   SELECT CDAJUSTEC195, IDGERREGC197, IDDIFEALIQUOTA
                     INTO V_CDAJUSTEC195,V_IDGERREGC197, V_IDDIFEALIQUOTA
                     FROM PARAMESTSPED
                    WHERE CDEMPRESA  = P_CDEMPRESA
                      AND CDINSCESTA = P_CDINSCESTA;
                EXCEPTION
                   WHEN NO_DATA_FOUND THEN
                      V_CDAJUSTEC195:= NULL;
                      V_IDGERREGC197:= NULL;
                END;

                /* REGISTRO C195 */                  
                FOR C_REGC195 IN (SELECT REPLACE(LTRIM(TO_CHAR(ROUND(SUM(I.VRBASECALCIT),2), '999999999990.00')), '.', ',') AS VL_BC_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND((SUM(I.VRIMPOSTOIT+CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(I.VRADICFECP,0) ELSE 0 END)/SUM(I.VRBASECALCIT))*100,2), '999999999990.00')), '.', ',') AS ALIQ_ICMS,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(SUM(I.VRIMPOSTOIT +CASE WHEN V_IDSOMAFECP = 'S' OR C_REGC100.IDENTRSAIDOP = 'E' THEN NVL(I.VRADICFECP,0) ELSE 0 END),2), '999999999990.00')), '.', ',') AS VL_ICMS,
                                         0 AS VL_OUTROS,
                                         'Diferencial de Aliquota' AS DESCR_COMPL_AJ,
                                         V_CDAJUSTEC195 AS COD_AJ
                                    FROM ITEMPROIMPOS I, IMPOSTO IM
                                   WHERE NRITPROIMPOS    = '3'
                                     AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                     AND I.CDIMPOSTO     = IM.CDIMPOSTO
                                     AND I.NRLANCTONF    = C_REGC100.NRLANCTONF
                                     AND I.CDFILIAL      = C_REGC100.CDFILIAL
                                     AND V_CDAJUSTEC195  IS NOT NULL
                                     AND V_IDGERREGC197  = 'S'
                                     AND NVL(V_IDDIFEALIQUOTA,'DA') <> 'OD'
                                    GROUP BY V_CDAJUSTEC195)LOOP
                   INSERT INTO ARQMAGNETICO
                     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                   VALUES
                     ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C195'  || '|' || '000001' || '|' || ''  || '|', P_NRSEQARQMAG);
                   V_CONTREGC := NVL(V_CONTREGC,0) + 1;

                   IF NVL(V_IDGERREGC197,'N') = 'S' THEN
                      V_REG_C197 := '|C197'                   || '|' ||
                                    C_REGC195.COD_AJ          || '|' ||
                                    C_REGC195.DESCR_COMPL_AJ  || '|' ||
                                    ''                        || '|' ||
                                    C_REGC195.VL_BC_ICMS      || '|' ||
                                    C_REGC195.ALIQ_ICMS       || '|' ||
                                    C_REGC195.VL_ICMS         || '|' ||
                                    C_REGC195.VL_OUTROS       || '|' ;
                      INSERT INTO ARQMAGNETICO
                        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                      VALUES
                        ('19'||LPAD(RTRIM(V_CONTREGC), 8, '0'), V_REG_C197, P_NRSEQARQMAG);
                      V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                   END IF;
                   SELECT MAX(CDCHAVEREG)
                     INTO V_COD_OBS
                     FROM TMP_REGDADOSSPED
                    WHERE CDCHAVEREG = '000001'
                      AND CDREGISTRO = '0460';

                   IF (V_COD_OBS IS NULL) THEN

                      V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                      INSERT INTO TMP_REGDADOSSPED
                        (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO1)
                      VALUES
                        ('0460', V_SEQUPROD, '000001', 'Diferencial de Aliquota');
                   END IF;

                END LOOP;
             END IF;
          END IF;
       END LOOP;
   END IF;


   IF V_CDSITUCRT IN ('2','3') AND P_PERFIL = 'B' THEN
      FOR C_REGC300 IN (SELECT ('|C300'    || '|' ||
                               COD_MOD     || '|' ||
                               SERIE       || '|' ||
                               COD_SUBSERIE|| '|' ||
                               NUM_DOC     || '|' ||
                               NUM_DOC     || '|' ||
                               DT_DOC      || '|' ||
                               VL_DOC      || '|' ||
                               VL_PIS      || '|' ||
                               VL_COFINS   || '|' ||
                               COD_CTA     || '|' ) AS ARQ_C300, NRLANCTONF, CDFILIAL, COD_CTA
                          FROM (SELECT N.CDMODDOCFISC  AS COD_MOD,
                                       NVL(SS.CDSERIENFE,N.CDSERIE) AS SERIE,
                                       SS.CDSUBSERIE AS COD_SUBSERIE,
                                       SUBSTR(LPAD(NRNOTAFISC,10,'0'),5,6) AS NUM_DOC,
                                       TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(N.VRNOTAFISC,2), '999999999990.00')), '.', ',') AS VL_DOC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                       MAX((SELECT MAX(CONTCTBL_SPED(N.IDENTRSAIDOP,I.CDPRODUTO,N.CDTIPOOPER,N.CDFILIEMIT))
                                              FROM ITEMPRODUTO I
                                             WHERE I.CDFILIAL   = N.CDFILIAL
                                               AND I.NRLANCTONF = N.NRLANCTONF)) AS COD_CTA,
                                       I.NRLANCTONF, I.CDFILIAL
                                  FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO IM, SERIENF SS
                                 WHERE IM.CDIMPOSTONFE   = P_CDIMPOSTO
                                   AND I.NRSEQUMAPAPU    = V_NRSEQUMAPAPU
                                   AND IM.CDIMPOSTO      = I.CDIMPOSTO
                                   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 SS.CDEMPRESA(+)   = N.CDEMPRESA
                                   AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                   AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                   AND SS.CDSERIE(+)     = N.CDSERIE
                                   AND NVL(P_GERAFORSALE,'N')  = 'N'
                                   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') <> 'TMP'
                                   AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                 GROUP BY N.CDMODDOCFISC,  NVL(SS.CDSERIENFE,N.CDSERIE),SS.CDSUBSERIE, SUBSTR(LPAD(NRNOTAFISC,10,'0'),5,6), N.DTEMISSAO,N.VRNOTAFISC,
                                       VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1'),
                                       VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1'),
                                       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;

         /* PLANO DE CONTAS REFERENCIAL */
         SELECT MAX(CDCHAVEREG)
           INTO V_CONTCTBL
           FROM TMP_REGDADOSSPED
          WHERE CDCHAVEREG = C_REGC300.COD_CTA
            AND CDREGISTRO = '0500';

         IF (V_CONTCTBL IS NULL) AND (C_REGC300.COD_CTA IS NOT NULL) THEN
            V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
            INSERT INTO TMP_REGDADOSSPED
               (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
            VALUES
               ('0500', V_SEQUPROD, C_REGC300.COD_CTA);
         END IF;
         V_CONTCTBL := NULL;

         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'          || '|' ||
                                   CDSITUTRIBIT    || '|' ||
                                   CDFISCOPERIT    || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(ROUND(IT.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICALIQFECP,0),0),2), '999999999990.00')), '.', ',')    || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(ABS(SUM(ROUND(IT.VRCONTITIMP,2))+NVL(SUM(ROUND(VALOR_REDUCAO(CDFILIAL, NRLANCTONF, CDSITUTRIBIT, NRSEQITPROD, CDFISCOPERIT, NULL, NULL, NULL),2)),0)), '999999999990.00')), '.', ',')    || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(SUM(ROUND(DECODE(CDFISCALIT,'1',IT.VRBASECALCIT,0) + DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL,IT.NRLANCTONF,IT.NRSEQITPROD,'03','B','1')),2)), '999999999990.00')), '.', ',')    || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(SUM(ROUND(DECODE(CDFISCALIT,'1',IT.VRIMPOSTOIT + DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICFECP,0),0), 0) + DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL,IT.NRLANCTONF,IT.NRSEQITPROD,'03','I','1')),2)), '999999999990.00')), '.', ',')    || '|' ||
                                   REPLACE(LTRIM(TO_CHAR(ABS(NVL(SUM(ROUND(VALOR_REDUCAO(CDFILIAL, NRLANCTONF, CDSITUTRIBIT, NRSEQITPROD, CDFISCOPERIT, NULL, NULL, NULL),2)),0)), '999999999990.00')), '.', ',')    || '|' ||
                                   ''  || '|'  ) AS ARQ_C320, IT.CDFILIAL, IT.NRLANCTONF, IT.CDSITUTRIBIT, IT.CDFISCOPERIT, IT.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICALIQFECP,0),0) AS VRALIQITEM
                             FROM ITEMPROIMPOS IT, IMPOSTO IM
                            WHERE IT.CDFILIAL     = C_REGC300.CDFILIAL
                              AND IT.NRLANCTONF   = C_REGC300.NRLANCTONF
                              AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                              AND IM.CDIMPOSTO    = IT.CDIMPOSTO
                              AND IT.NRITPROIMPOS = '1'
                            GROUP BY IT.CDSITUTRIBIT, IT.CDFISCOPERIT, IT.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICALIQFECP,0),0),
                                  IT.CDFILIAL, IT.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'         || '|' ||
                                      P.CDARVPROD      || '|' ||
                                      REPLACE(LTRIM(TO_CHAR(ROUND(SUM(IT.QTITEMPROD),3), '999999999990.000')), '.', ',')  || '|' ||
                                      P.SGUNIDADE     || '|' ||
                                      REPLACE(LTRIM(TO_CHAR(ABS(ROUND(SUM(VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0),2)), '999999999990.00')), '.', ',')    || '|' ||
                                      REPLACE(LTRIM(TO_CHAR(ROUND(SUM(IT.VRENCARGODESC),2), '999999999990.00')), '.', ',')    || '|' ||
                                      REPLACE(LTRIM(TO_CHAR(ROUND(SUM(DECODE(CDFISCALIT,'1',I.VRBASECALCIT, 0)+DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'03','B','1'))),2), '999999999990.00')), '.', ',')   || '|' ||
                                      REPLACE(LTRIM(TO_CHAR(ROUND(SUM(DECODE(CDFISCALIT,'1',I.VRIMPOSTOIT+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0),0)+DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'03','I','1'))),2), '999999999990.00')), '.', ',')    || '|' ||
                                      REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1')),2), '999999999990.00')), '.', ',')    || '|' ||
                                      REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1')),2), '999999999990.00')), '.', ',')    || '|'  ) AS ARQ_C321,
                                      P.CDPRODUTO, P.SGUNIDADE UNID, IT.CDPRODNCMTRANS AS CDCLASFISC
                                 FROM ITEMPROIMPOS I, ITEMPRODUTO IT, PRODUTO P, IMPOSTO IM
                                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 IM.CDIMPOSTONFE = P_CDIMPOSTO
                                  AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                  AND I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0) = C_REGC320.VRALIQITEM
                                  AND I.CDSITUTRIBIT  = C_REGC320.CDSITUTRIBIT
                                  AND I.CDFISCOPERIT  = C_REGC320.CDFISCOPERIT
                                  AND I.NRITPROIMPOS  = '1'
                                GROUP BY P.CDARVPROD, P.CDPRODUTO, P.SGUNIDADE, IT.CDPRODNCMTRANS) 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;

                /* CODIGO DO PRODUTO */
                SELECT MAX(CDCHAVEREG)
                  INTO V_PRODUTO
                  FROM TMP_REGDADOSSPED
                 WHERE CDCHAVEREG = C_REGC321.CDPRODUTO
                   AND CDREGISTRO = '0200';
                IF (V_PRODUTO IS NULL) AND (C_REGC321.CDPRODUTO IS NOT NULL) THEN
                   V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                   INSERT INTO TMP_REGDADOSSPED 
                     (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3)
                   VALUES 
                     ('0200', V_SEQUPROD, C_REGC321.CDPRODUTO, C_REGC321.CDCLASFISC);
                END IF; 
                V_PRODUTO := NULL;

                /* UNIDADE DE MEDIDA */
                SELECT MAX(CDCHAVEREG) 
                  INTO V_UNID 
                  FROM TMP_REGDADOSSPED 
                 WHERE CDCHAVEREG = C_REGC321.UNID 
                   AND CDREGISTRO = '0190'; 
                IF (V_UNID IS NULL) AND (C_REGC321.UNID IS NOT NULL) THEN 
                   V_SEQUPROD := NVL(V_SEQUPROD,0) + 1; 
                   INSERT INTO TMP_REGDADOSSPED 
                     (CDREGISTRO, CDCONTREGI, CDCHAVEREG) 
                   VALUES 
                     ('0190', V_SEQUPROD, C_REGC321.UNID); 
                END IF; 
                V_UNID := NULL;

                IF P_IDINFCOMPLST = 'S' THEN
                   /*REGISTRO C330: Informacoes complementares das operacoes de saida de mercadorias sujeitas a substituicao tributaria*/
                    FOR C_REGC330 IN (SELECT '|C330'                                                                                ||'|'||
                                             CASE WHEN VL_UNIT_ICMS_ST_CONV_COMPL > 0 THEN COD_MOT_COMPL
                                                  WHEN VL_UNIT_ICMS_ST_CONV_REST  > 0 THEN COD_MOT_REST
                                                  ELSE NVL(COD_MOT_COMPL,COD_MOT_REST) END                                          ||'|'||
                                             REPLACE(LTRIM(TO_CHAR(QUANT_CONV, '999999999990.000')), '.', ',')                      ||'|'||
                                             UNID                                                                                   ||'|'||
                                             REPLACE(LTRIM(TO_CHAR(VL_UNIT_CONV, '999999999990.00')), '.', ',')                     ||'|'||
                                             REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_NA_OPERACAO_CONV, '999999999990.00')), '.', ',')    ||'|'||
                                             REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_OP_CONV, '999999999990.00')), '.', ',')             ||'|'||
                                             REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_OP_ESTOQUE_CONV, '999999999990.00')), '.', ',')     ||'|'||
                                             REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_ST_ESTOQUE_CONV, '999999999990.00')), '.', ',')     ||'|'||
                                             REPLACE(LTRIM(TO_CHAR(VL_UNIT_FCP_ICMS_ST_ESTOQUE_C, '999999999990.00')), '.', ',')    ||'|'||
                                             REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_REST > 0
                                                                        THEN VL_UNIT_ICMS_ST_CONV_REST ELSE 0 END, '999999999990.000')), '.', ',')  ||'|'|| /*VL_UNIT_ICMS_ST_CONV_REST*/
                                             REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_REST > 0
                                                                        THEN (VL_UNIT_FCP_ICMS_ST_ESTOQUE_C/DECODE(VL_UNIT_ICMS_ST_ESTOQUE_CONV,0,1,VL_UNIT_ICMS_ST_ESTOQUE_CONV)) * VL_UNIT_ICMS_ST_CONV_REST
                                                                        ELSE 0 END , '999999999990.000')), '.', ',') ||'|'|| /*VL_UNIT_FCP_ST_CONV_REST*/
                                             REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_COMPL > 0
                                                                        THEN VL_UNIT_ICMS_ST_CONV_COMPL ELSE 0 END, '999999999990.000')), '.', ',') ||'|'|| /*VL_UNIT_ICMS_ST_CONV_COMPL*/
                                             REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_REST > 0
                                                                        THEN (VL_UNIT_FCP_ICMS_ST_ESTOQUE_C/DECODE(VL_UNIT_ICMS_ST_ESTOQUE_CONV,0,1,VL_UNIT_ICMS_ST_ESTOQUE_CONV)) * VL_UNIT_ICMS_ST_CONV_COMPL
                                                                        ELSE 0 END , '999999999990.000')), '.', ',') ||'|'  AS ARQ_C330 /*VL_UNIT_FCP_ST_CONV_COMP */
                                        FROM (SELECT COD_MOT_COMPL, COD_MOT_REST,
                                                     QUANT_CONV, UNID, VL_UNIT_CONV, VL_UNIT_ICMS_NA_OPERACAO_CONV,
                                                     DECODE(P_IDGERACAMPO11ST,'S',VL_UNIT_ICMS_OP_CONV,0) AS VL_UNIT_ICMS_OP_CONV,
                                                     VL_UNIT_ICMS_OP_ESTOQUE_CONV, VL_UNIT_ICMS_ST_ESTOQUE_CONV, VL_UNIT_FCP_ICMS_ST_ESTOQUE_C,
                                                     CASE WHEN SUBSTR(COD_MOT_REST,3,1) = '1' THEN VL_UNIT_ICMS_OP_ESTOQUE_CONV + VL_UNIT_ICMS_ST_ESTOQUE_CONV - VL_UNIT_ICMS_NA_OPERACAO_CONV
                                                          WHEN SUBSTR(COD_MOT_REST,3,1) = '2' THEN VL_UNIT_ICMS_OP_ESTOQUE_CONV + VL_UNIT_ICMS_ST_ESTOQUE_CONV - VL_UNIT_ICMS_OP_CONV
                                                          ELSE 0 END AS VL_UNIT_ICMS_ST_CONV_REST,
                                                     (VL_UNIT_ICMS_NA_OPERACAO_CONV - VL_UNIT_ICMS_OP_ESTOQUE_CONV - VL_UNIT_ICMS_ST_ESTOQUE_CONV) AS VL_UNIT_ICMS_ST_CONV_COMPL
                                                FROM (SELECT IP.NRSEQITPROD AS NUM_ITEM,
                                                     DECODE(P_IDCONSPRODEST,'S',NVL(PA.CDARVPROD,PR.CDARVPROD),PR.CDARVPROD) AS COD_ITEM,
                                                     IP.CDSITUTRIBIT AS CST_ICMS,
                                                     IP.CDFISCOPERIT AS CFOP,
                                                     IP.CDCOMPST AS COD_MOT_COMPL, IP.CDRESTST AS COD_MOT_REST,
                                                     ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3) AS QUANT_CONV,
                                                     DECODE(P_IDCONSPRODEST,'S',NVL(PA.SGUNIDADE,PR.SGUNIDADE),PR.SGUNIDADE) AS UNID,
                                                     ROUND(NVL(IT.VRTOTAITEM,0),2)+ROUND(NVL(VRENCARGOIMPXML,0),2) AS VL_UNIT_CONV,
                                                     ROUND((VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'05','I','1') / ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3)),2)  AS VL_UNIT_ICMS_NA_OPERACAO_CONV,
                                                     ROUND(IP.VRIMPITENTRUNIT,2) AS VL_UNIT_ICMS_OP_CONV,
                                                     '0,00' AS VL_UNIT_ICMS_OP_ESTOQUE_CONV,
                                                     '0,00' AS VL_UNIT_ICMS_ST_ESTOQUE_CONV,
                                                     '0,00' AS VL_UNIT_FCP_ICMS_ST_ESTOQUE_C
                                                FROM ITEMPRODUTO IT, PRODUTO PR, PRODUTO PA, ITEMPROIMPOS IP, IMPOSTO IM, ITMAPARECUP MR
                                               WHERE IT.CDFILIAL      = C_REGC320.CDFILIAL
                                                 AND IT.NRLANCTONF    = C_REGC320.NRLANCTONF
                                                 AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                                 AND IM.CDIMPOSTO     = IP.CDIMPOSTO
                                                 AND IT.CDFILIAL      = IP.CDFILIAL
                                                 AND IT.NRLANCTONF    = IP.NRLANCTONF
                                                 AND IT.NRSEQITPROD   = IP.NRSEQITPROD
                                                 AND IT.CDPRODUTO     = PR.CDPRODUTO
                                                 AND PR.CDPRODESTO    = PA.CDPRODUTO(+)
                                                 AND IT.CDPRODUTO     = C_REGC321.CDPRODUTO
                                                 AND IP.NRITPROIMPOS  =  '1'
                                                 AND IP.CDFILIAL      = MR.CDFILIAL(+)
                                                 AND IP.NRLANCTONF    = MR.NRLANCTONF(+)
                                                 AND V_NRSEQUMAPAPU   = MR.NRSEQUMAPAPU(+)
                                                 AND EXISTS (SELECT I.CDFILIAL, I.NRLANCTONF
                                                               FROM ITEMPROIMPOS I, IMPOSTO M
                                                              WHERE I.CDFILIAL    = IT.CDFILIAL
                                                                AND I.NRLANCTONF  = IT.NRLANCTONF
                                                                AND I.NRSEQITPROD = IT.NRSEQITPROD
                                                                AND I.CDIMPOSTO   = M.CDIMPOSTO
                                                                AND M.CDIMPOSTONFE IN ('05'))))) LOOP
                      INSERT INTO ARQMAGNETICO
                         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                      VALUES
                         ('20'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC330.ARQ_C330, P_NRSEQARQMAG);
                      V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                   END LOOP;
                END IF;
             END LOOP; 
          END LOOP; 
       END LOOP; 
   END IF;
 
   IF V_CDSITUCRT = '1' AND P_PERFIL = 'B' THEN /*SIMPLES NACIONAL*/
      FOR C_REGC300 IN (SELECT '|C300'     || '|' ||
                               COD_MOD     || '|' ||
                               SERIE       || '|' ||
                               COD_SUBSERIE|| '|' ||
                               NUM_DOC     || '|' ||
                               NUM_DOC     || '|' ||
                               DT_DOC      || '|' ||
                               VL_DOC      || '|' ||
                               VL_PIS      || '|' ||
                               VL_COFINS   || '|' ||
                               COD_CTA     || '|'  AS ARQ_C300, NRLANCTONF, CDFILIAL, COD_CTA
                          FROM (SELECT N.CDMODDOCFISC AS COD_MOD,
                                       NVL(SS.CDSERIENFE,N.CDSERIE) AS SERIE,
                                       SS.CDSUBSERIE AS COD_SUBSERIE,
                                       SUBSTR(LPAD(NRNOTAFISC,10,'0'),5,6) AS NUM_DOC,
                                       TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(N.VRNOTAFISC,2), '999999999990.00')), '.', ',') AS VL_DOC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                       MAX((SELECT MAX(CONTCTBL_SPED(N.IDENTRSAIDOP,I.CDPRODUTO,N.CDTIPOOPER,N.CDFILIEMIT))
                                              FROM ITEMPRODUTO I
                                             WHERE I.CDFILIAL   = N.CDFILIAL
                                               AND I.NRLANCTONF = N.NRLANCTONF)) AS COD_CTA, I.NRLANCTONF, I.CDFILIAL
                                  FROM IMPOSTONF I, NOTAFISCAL N, IMPOSTO IM, SERIENF SS
                                 WHERE IM.CDIMPOSTONFE = P_CDIMPOSTO
                                   AND N.NRSEQUMAPAPUSN  = V_NRSEQUMAPAPU
                                   AND N.CDINSCESTA    = P_CDINSCESTA
                                   AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                   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 SS.CDEMPRESA(+)   = N.CDEMPRESA
                                   AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                   AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                   AND SS.CDSERIE(+)     = N.CDSERIE
                                   AND NVL(P_GERAFORSALE,'N') = 'N'
                                   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') <> 'TMP'
                                   AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                 GROUP BY N.CDMODDOCFISC, NVL(SS.CDSERIENFE,N.CDSERIE),SS.CDSUBSERIE, SUBSTR(LPAD(NRNOTAFISC,10,'0'),5,6), N.DTEMISSAO,N.VRNOTAFISC,
                                       VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1'), VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1'),
                                       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;

         /* PLANO DE CONTAS REFERENCIAL */
         SELECT MAX(CDCHAVEREG)
           INTO V_CONTCTBL
           FROM TMP_REGDADOSSPED
          WHERE CDCHAVEREG = C_REGC300.COD_CTA
            AND CDREGISTRO = '0500';

         IF (V_CONTCTBL IS NULL) AND (C_REGC300.COD_CTA IS NOT NULL) THEN
            V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
            INSERT INTO TMP_REGDADOSSPED
               (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
            VALUES
               ('0500', V_SEQUPROD, C_REGC300.COD_CTA);
         END IF;
         V_CONTCTBL := NULL;

         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'         || '|' ||
                                  CDSITUTRIBIT    || '|' ||
                                  CDFISCOPERIT    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(IT.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICALIQFECP,0),0),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ABS(ROUND(SUM(IT.VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(IT.CDFILIAL, IT.NRLANCTONF, IT.CDSITUTRIBIT, IT.NRSEQITPROD, IT.CDFISCOPERIT, NULL, NULL, NULL)),0),2)), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(SUM(DECODE(IT.CDFISCALIT,'1',IT.VRBASECALCIT,0)+DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL,IT.NRLANCTONF,IT.NRSEQITPROD,'03','I','1'))),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(SUM(DECODE(IT.CDFISCALIT,'1',IT.VRIMPOSTOIT+DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICFECP,0),0),0)+DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_ITEM_NFE(IT.CDFILIAL,IT.NRLANCTONF,IT.NRSEQITPROD,'03','I','1'))),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ABS(ROUND(NVL(SUM(VALOR_REDUCAO(IT.CDFILIAL, IT.NRLANCTONF, IT.CDSITUTRIBIT, IT.NRSEQITPROD, IT.CDFISCOPERIT, NULL, NULL, NULL) ),0),2)), '999999999990.00')), '.', ',')    || '|' ||
                                  ''  || '|'  ) AS ARQ_C320, IT.CDFILIAL, IT.NRLANCTONF, IT.CDSITUTRIBIT, IT.CDFISCOPERIT, IT.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICALIQFECP,0),0) AS VRALIQITEM
                             FROM ITEMPROIMPOS IT, IMPOSTO IM
                            WHERE IT.CDFILIAL     = C_REGC300.CDFILIAL
                              AND IT.NRLANCTONF   = C_REGC300.NRLANCTONF
                              AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                              AND IM.CDIMPOSTO    = IT.CDIMPOSTO
                              AND IT.NRITPROIMPOS = '1'
                            GROUP BY IT.CDSITUTRIBIT, IT.CDFISCOPERIT, IT.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(IT.VRADICALIQFECP,0),0),
                                  IT.CDFILIAL, IT.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'         || '|' ||
                                     P.CDARVPROD      || '|' ||
                                     ROUND(SUM(IT.QTITEMPROD),3)   || '|' ||
                                     P.SGUNIDADE     || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ABS(ROUND(SUM(VRCONTITIMP)+NVL(SUM(VALOR_REDUCAO(I.CDFILIAL, I.NRLANCTONF, I.CDSITUTRIBIT, I.NRSEQITPROD, I.CDFISCOPERIT, NULL, NULL, NULL)),0),2)), '999999999990.00')), '.', ',')    || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(SUM(IT.VRENCARGODESC),2), '999999999990.00')), '.', ',')    || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(SUM(DECODE(CDFISCALIT,'1',VRBASECALCIT, 0)+DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'03','I','1'))),2), '999999999990.00')), '.', ',')   || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(SUM(DECODE(CDFISCALIT,'1',I.VRIMPOSTOIT+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0),0)+DECODE(V_IDTPCARGATRIB,'O',0,VALOR_IMPOSTO_ITEM_NFE(I.CDFILIAL,I.NRLANCTONF,I.NRSEQITPROD,'03','I','1'))),2), '999999999990.00')), '.', ',')   || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1')),2), '999999999990.00')), '.', ',')    || '|' ||
                                     REPLACE(LTRIM(TO_CHAR(ROUND(SUM(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1')),2), '999999999990.00')), '.', ',')    || '|'  ) AS ARQ_C321,
                                     P.CDPRODUTO, P.SGUNIDADE AS UNID, IT.CDPRODNCMTRANS AS CDCLASFISC
                                FROM ITEMPROIMPOS I, ITEMPRODUTO IT, PRODUTO P, IMPOSTO IM
                               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 IM.CDIMPOSTONFE = P_CDIMPOSTO
                                 AND IM.CDIMPOSTO    = I.CDIMPOSTO
                                 AND I.VRALIQITEM+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0) = C_REGC320.VRALIQITEM
                                 AND CDSITUTRIBIT    = C_REGC320.CDSITUTRIBIT
                                 AND CDFISCOPERIT    = C_REGC320.CDFISCOPERIT
                                 AND NRITPROIMPOS    = '1'
                               GROUP BY P.CDARVPROD, P.CDPRODUTO, P.SGUNIDADE, IT.CDPRODNCMTRANS) 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;

               /* CODIGO DO PRODUTO */
               SELECT MAX(CDCHAVEREG)
                 INTO V_PRODUTO
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_REGC321.CDPRODUTO
                  AND CDREGISTRO = '0200';
               IF (V_PRODUTO IS NULL) AND (C_REGC321.CDPRODUTO IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3)
                  VALUES
                    ('0200', V_SEQUPROD, C_REGC321.CDPRODUTO, C_REGC321.CDCLASFISC);
               END IF;
               V_PRODUTO := NULL;

               /* UNIDADE DE MEDIDA */
               SELECT MAX(CDCHAVEREG)
                 INTO V_UNID
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_REGC321.UNID
                  AND CDREGISTRO = '0190';
               IF (V_UNID IS NULL) AND (C_REGC321.UNID IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                  VALUES
                    ('0190', V_SEQUPROD, C_REGC321.UNID);
               END IF;
               V_UNID := NULL;
            END LOOP;
         END LOOP;
      END LOOP;
   END IF;

   IF V_CDSITUCRT IN ('2','3') AND P_PERFIL = 'A' THEN
      /*REGISTRO C350*/
      FOR C_REGC350 IN (SELECT '|C350'       || '|' ||
                               SERIE         || '|' ||
                               ''            || '|' ||
                               NUM_DOC       || '|' ||
                               DT_DOC        || '|' ||
                               CNPJ_CPF      || '|' || --CPF_CNPJ
                               VL_MERC       || '|' ||
                               VL_DOC        || '|' ||
                               VL_DESC       || '|' ||
                               VL_PIS        || '|' ||
                               VL_COFINS     || '|' ||
                               COD_CTA       || '|' AS ARQ_C350, NRLANCTONF, CDFILIAL, CDCLIENTE, CDCAIXA, COD_CTA
                          FROM (SELECT NVL(SS.CDSERIENFE,N.CDSERIE) AS SERIE,
                                       SUBSTR(LPAD(NRNOTAFISC,10,'0'),5,6) AS NUM_DOC,
                                       TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                       NVL(SUBSTR(REPLACE(REPLACE(REPLACE(REPLACE(CASE WHEN (LENGTH(N.NRINSJURNF) = 14 OR LENGTH(N.NRINSJURNF) = 11) THEN N.NRINSJURNF ELSE N.NRINSJURNFPAULI END,'.'),'-'),'/'),' '),1,14),'') AS CNPJ_CPF,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(N.VRPRODUTOS,2), '999999999990.00')), '.', ',') AS VL_MERC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(N.VRNOTAFISC,2), '999999999990.00')), '.', ',') AS VL_DOC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(SUM(T.VRITEMENCA),2), '999999999990.00')), '.', ',') AS VL_DESC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                       MAX((SELECT MAX(CONTCTBL_SPED(N.IDENTRSAIDOP,I.CDPRODUTO,N.CDTIPOOPER,N.CDFILIEMIT))
                                                     FROM ITEMPRODUTO I
                                                    WHERE I.CDFILIAL   = N.CDFILIAL
                                                      AND I.NRLANCTONF = N.NRLANCTONF)) AS COD_CTA,
                                       I.NRLANCTONF, I.CDFILIAL, N.CDCLIENTE, '' AS  CDCAIXA
                                  FROM IMPOSTONF I, NOTAFISCAL N, ENCARGO E, ITEMENCARGO T, IMPOSTO IM, SERIENF SS
                                 WHERE I.CDIMPOSTO      = IM.CDIMPOSTO
                                   AND IM.CDIMPOSTONFE  = 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 SS.CDEMPRESA(+)  = N.CDEMPRESA
                                   AND SS.CDINSCESTA(+) = N.CDINSCESTA
                                   AND SS.CDINSCMUNI(+) = N.CDINSCMUNI
                                   AND SS.CDSERIE(+)    = N.CDSERIE
                                   AND NVL(P_GERAFORSALE,'N') = 'N'
                                   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') <> 'TMP'
                                   AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                 GROUP BY NVL(SS.CDSERIENFE,N.CDSERIE), SUBSTR(LPAD(NRNOTAFISC,10,'0'),5,6), TO_CHAR(N.DTEMISSAO,'ddmmyyyy'),
                                       N.CDCLIENTE, N.VRPRODUTOS, N.VRNOTAFISC,
                                       VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1'),
                                       VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1'),
                                       I.NRLANCTONF, I.CDFILIAL, '',
                                       NVL(SUBSTR(REPLACE(REPLACE(REPLACE(REPLACE(CASE WHEN (LENGTH(N.NRINSJURNF) = 14 OR LENGTH(N.NRINSJURNF) = 11) THEN N.NRINSJURNF ELSE N.NRINSJURNFPAULI END,'.'),'-'),'/'),' '),1,14),'')
                                 UNION
                                 /*GERA C350 PARA O FORSALE - C300 FOS*/
                                SELECT 'D' AS SERIE,
                                       SUBSTR(LPAD(NRNOTAFISC,10,'0'),5,6) AS NUM_DOC,
                                       TO_CHAR(DTEMISSAO,'DDMMYYYY') AS DT_DOC,
                                       NVL(REPLACE(REPLACE(REPLACE(REPLACE(NRINSCRCONS,'.'),'-'),'/'),' '),'') AS CNPJ_CPF,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VRNOTAFISC+VRDESITVEND,2), '999999999990.00')), '.', ',') AS VL_MERC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VRNOTAFISC,2), '999999999990.00')), '.', ',')  AS VL_DOC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VRDESITVEND,2), '999999999990.00')), '.', ',') AS VL_DESC,
                                       '0,00' AS VL_PIS,
                                       '0,00' AS VL_COFINS,
                                       CONTCTBL_SPED('S','',CDTIPOOPER,CDFILIAL) AS COD_CTA,
                                       NRLANCTONF, CDFILIAL, '' CDCLIENTE, CDCAIXA
                                  FROM (SELECT VE.DTENTRVENDA AS DTEMISSAO, VE.NRSERIEVND AS CDSERIE ,VE.NRSUBSERIEVND ,VE.NRORDEMVND AS NRNOTAFISC,
                                               SUM(TRUNC(IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0)),2) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0)) AS VRNOTAFISC,
                                               SUM(VRDESITVEND) AS VRDESITVEND, PV.CDTPOPERCNTREC AS CDTIPOOPER,
                                               VE.CDFILIAL, VE.NRSEQVENDA AS NRLANCTONF, VE.CDCAIXA, VE.NRINSCRCONS
                                          FROM VENDA VE, ITEMVENDA IT, PARAVEND PV
                                         WHERE VE.CDEMPRESA     =  P_CDEMPRESA
                                           AND VE.CDFILIAL      = IT.CDFILIAL
                                           AND VE.NRSEQVENDA    = IT.NRSEQVENDA
                                           AND VE.CDCAIXA       = IT.CDCAIXA
                                           AND VE.DTENTRVENDA   BETWEEN P_DTINICIAL AND P_DTFINAL
                                           AND VE.IDSITUVENDA   = 'O'
                                           AND VE.NRORDEMVND    IS NOT NULL
                                           AND VE.NRSUBSERIEVND IS NOT NULL
                                           AND VE.NRSERIEVND    IS NOT NULL
                                           AND NVL(P_GERAFORSALE,'N') = 'S'
                                           AND VE.CDFILIAL      = PV.CDFILIAL(+)
                                         GROUP BY VE.DTENTRVENDA, VE.NRSERIEVND, VE.NRSUBSERIEVND, VE.NRORDEMVND, VE.NRSEQVENDA,
                                               VE.CDFILIAL, VE.CDCAIXA, VE.NRINSCRCONS, PV.CDTPOPERCNTREC))) 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;

         /* PLANO DE CONTAS REFERENCIAL */
         SELECT MAX(CDCHAVEREG)
           INTO V_CONTCTBL
           FROM TMP_REGDADOSSPED
          WHERE CDCHAVEREG = C_REGC350.COD_CTA
            AND CDREGISTRO = '0500';

         IF (V_CONTCTBL IS NULL) AND (C_REGC350.COD_CTA IS NOT NULL) THEN
            V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
            INSERT INTO TMP_REGDADOSSPED
               (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
            VALUES
               ('0500', V_SEQUPROD, C_REGC350.COD_CTA);
         END IF;
         V_CONTCTBL := NULL;

         V_NRSEQITEM := 0;
         FOR C_REGC370 IN(SELECT (P.CDARVPROD  || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(IT.QTITEMPROD,3), '99999999990.000')), '.', ',')  ||'|'||
                                  P.SGUNIDADE               || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND((SUM(I.VRCONTITIMP)),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND((SUM(IT.VRENCARGODESC)),2), '999999999990.00')), '.', ',')    || '|' ) AS ARQ_C370,
                                  P.CDPRODUTO, P.SGUNIDADE AS UNID, IT.CDPRODNCMTRANS AS CDCLASFISC
                            FROM ITEMPROIMPOS I, ITEMPRODUTO IT, PRODUTO P, IMPOSTO IM
                           WHERE IT.CDFILIAL      = C_REGC350.CDFILIAL
                             AND IT.NRLANCTONF    = C_REGC350.NRLANCTONF
                             AND I.CDIMPOSTO      = IM.CDIMPOSTO
                             AND I.CDFILIAL       = IT.CDFILIAL
                             AND I.NRLANCTONF     = IT.NRLANCTONF
                             AND I.NRSEQITPROD    = IT.NRSEQITPROD
                             AND IT.CDPRODUTO     = P.CDPRODUTO
                             AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                             AND NVL(P_GERAFORSALE,'N') = 'N'
                           GROUP BY SUBSTR(I.NRSEQITPROD,2,3), P.CDARVPROD, IT.QTITEMPROD, P.SGUNIDADE, P.CDPRODUTO, IT.CDPRODNCMTRANS
                           UNION
                          SELECT CDARVPROD    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(QTPRODVEND,3), '99999999990.000')), '.', ',')  ||'|'||
                                 SGUNIDADE    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_TOT_IT,2), '999999999990.00')), '.', ',')   ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VRDESITVEND,2), '999999999990.00')), '.', ',') ||'|' AS ARQ_C370,
                                 CDPRODUTO, SGUNIDADE AS UNID, CDCLASFISC
                           FROM (SELECT NRSEQUITVEND,
                                        CDARVPROD,
                                        QTPRODVEND,
                                        P.SGUNIDADE,
                                        (IV.VRUNITVEND+NVL(IV.VRUNITVENDCL,0)+NVL(IV.VRACRITVEND,0)) AS VR_TOT_IT,
                                        VRDESITVEND, P.CDPRODUTO, IV.CDCLASFISC
                                   FROM ITEMVENDA IV, PRODUTO P
                                  WHERE IV.CDFILIAL = C_REGC350.CDFILIAL
                                    AND IV.CDCAIXA = C_REGC350.CDCAIXA
                                    AND IV.NRSEQVENDA =  C_REGC350.NRLANCTONF
                                    AND NVL(P_GERAFORSALE,'N') = 'S'
                                    AND IV.CDPRODUTO = P.CDPRODUTO)NF) LOOP

               V_NRSEQITEM := NVL(V_NRSEQITEM,0)+1;
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES
                 ('24'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C370|'||LPAD(V_NRSEQITEM,3,'0')||'|'||C_REGC370.ARQ_C370, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;
 
               /* CODIGO DO PRODUTO */
               SELECT MAX(CDCHAVEREG)
                 INTO V_PRODUTO
                 FROM TMP_REGDADOSSPED
                WHERE CDCHAVEREG = C_REGC370.CDPRODUTO
                  AND CDREGISTRO = '0200';

               IF (V_PRODUTO IS NULL) AND (C_REGC370.CDPRODUTO IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1; 
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3)
                  VALUES
                    ('0200', V_SEQUPROD, C_REGC370.CDPRODUTO, C_REGC370.CDCLASFISC);
               END IF;
               V_PRODUTO := NULL;

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

               IF (V_UNID IS NULL) AND (C_REGC370.UNID IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                  VALUES
                    ('0190', V_SEQUPROD, C_REGC370.UNID);
               END IF;
               V_UNID := NULL;

             IF P_IDINFCOMPLST = 'S' THEN
                  /*REGISTRO C380: Informacoes complementares das operacoes de saida de mercadorias sujeitas a substituicao tributaria*/
                  FOR C_REGC380 IN (SELECT '|C380'                                                                                ||'|'||
                                           CASE WHEN VL_UNIT_ICMS_ST_CONV_COMPL > 0 THEN COD_MOT_COMPL
                                                WHEN VL_UNIT_ICMS_ST_CONV_REST  > 0 THEN COD_MOT_REST
                                                ELSE NVL(COD_MOT_COMPL,COD_MOT_REST) END                                          ||'|'||
                                           REPLACE(LTRIM(TO_CHAR(QUANT_CONV, '999999999990.000')), '.', ',')                      ||'|'||
                                           UNID                                                                                   ||'|'||
                                           REPLACE(LTRIM(TO_CHAR(VL_UNIT_CONV, '999999999990.00')), '.', ',')                     ||'|'||
                                           REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_NA_OPERACAO_CONV, '999999999990.00')), '.', ',')    ||'|'||
                                           REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_OP_CONV, '999999999990.00')), '.', ',')             ||'|'||
                                           REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_OP_ESTOQUE_CONV, '999999999990.00')), '.', ',')     ||'|'||
                                           REPLACE(LTRIM(TO_CHAR(VL_UNIT_ICMS_ST_ESTOQUE_CONV, '999999999990.00')), '.', ',')     ||'|'||
                                           REPLACE(LTRIM(TO_CHAR(VL_UNIT_FCP_ICMS_ST_ESTOQUE_C, '999999999990.00')), '.', ',')    ||'|'||
                                           REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_REST > 0
                                                                      THEN VL_UNIT_ICMS_ST_CONV_REST ELSE 0 END, '999999999990.000')), '.', ',')  ||'|'|| /*VL_UNIT_ICMS_ST_CONV_REST*/
                                           REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_REST > 0
                                                                      THEN (VL_UNIT_FCP_ICMS_ST_ESTOQUE_C/DECODE(VL_UNIT_ICMS_ST_ESTOQUE_CONV,0,1,VL_UNIT_ICMS_ST_ESTOQUE_CONV)) * VL_UNIT_ICMS_ST_CONV_REST
                                                                      ELSE 0 END , '999999999990.000')), '.', ',') ||'|'|| /*VL_UNIT_FCP_ST_CONV_REST*/
                                           REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_COMPL > 0
                                                                      THEN VL_UNIT_ICMS_ST_CONV_COMPL ELSE 0 END, '999999999990.000')), '.', ',') ||'|'|| /*VL_UNIT_ICMS_ST_CONV_COMPL*/
                                           REPLACE(LTRIM(TO_CHAR(CASE WHEN VL_UNIT_ICMS_ST_CONV_REST > 0
                                                                      THEN (VL_UNIT_FCP_ICMS_ST_ESTOQUE_C/DECODE(VL_UNIT_ICMS_ST_ESTOQUE_CONV,0,1,VL_UNIT_ICMS_ST_ESTOQUE_CONV)) * VL_UNIT_ICMS_ST_CONV_COMPL
                                                                      ELSE 0 END , '999999999990.000')), '.', ',') ||'|'||  /*VL_UNIT_FCP_ST_CONV_COMP */
                                           CST_ICMS           ||'|'||
                                           CFOP               ||'|' AS ARQ_C380
                                      FROM (SELECT COD_MOT_COMPL, COD_MOT_REST, CST_ICMS, CFOP,
                                                   QUANT_CONV, UNID, VL_UNIT_CONV, VL_UNIT_ICMS_NA_OPERACAO_CONV,
                                                   DECODE(P_IDGERACAMPO11ST,'S',VL_UNIT_ICMS_OP_CONV,0) AS VL_UNIT_ICMS_OP_CONV,
                                                   VL_UNIT_ICMS_OP_ESTOQUE_CONV, VL_UNIT_ICMS_ST_ESTOQUE_CONV, VL_UNIT_FCP_ICMS_ST_ESTOQUE_C,
                                                   CASE WHEN SUBSTR(COD_MOT_REST,3,1) = '1' THEN VL_UNIT_ICMS_OP_ESTOQUE_CONV + VL_UNIT_ICMS_ST_ESTOQUE_CONV - VL_UNIT_ICMS_NA_OPERACAO_CONV
                                                        WHEN SUBSTR(COD_MOT_REST,3,1) = '2' THEN VL_UNIT_ICMS_OP_ESTOQUE_CONV + VL_UNIT_ICMS_ST_ESTOQUE_CONV - VL_UNIT_ICMS_OP_CONV
                                                        ELSE 0 END AS VL_UNIT_ICMS_ST_CONV_REST,
                                                   (VL_UNIT_ICMS_NA_OPERACAO_CONV - VL_UNIT_ICMS_OP_ESTOQUE_CONV - VL_UNIT_ICMS_ST_ESTOQUE_CONV) AS VL_UNIT_ICMS_ST_CONV_COMPL
                                              FROM (SELECT IP.CDSITUTRIBIT AS CST_ICMS,
                                                           IP.CDFISCOPERIT AS CFOP,
                                                           IP.CDCOMPST AS COD_MOT_COMPL, IP.CDRESTST AS COD_MOT_REST,
                                                           ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3) AS QUANT_CONV,
                                                           DECODE(P_IDCONSPRODEST,'S',NVL(PA.SGUNIDADE,PR.SGUNIDADE),PR.SGUNIDADE) AS UNID,
                                                           ROUND(NVL(IT.VRTOTAITEM,0),2)+ROUND(NVL(VRENCARGOIMPXML,0),2) AS VL_UNIT_CONV,
                                                           ROUND((VALOR_IMPOSTO_ITEM_NFE(IP.CDFILIAL,IP.NRLANCTONF,IP.NRSEQITPROD,'05','I','1') / ROUND(IT.QTITEMPROD*NVL(PR.VRFATOCONV,1),3)),2)  AS VL_UNIT_ICMS_NA_OPERACAO_CONV,
                                                           ROUND(IP.VRIMPITENTRUNIT,2) AS VL_UNIT_ICMS_OP_CONV,
                                                           '0,00' AS VL_UNIT_ICMS_OP_ESTOQUE_CONV,
                                                           '0,00' AS VL_UNIT_ICMS_ST_ESTOQUE_CONV,
                                                           '0,00' AS VL_UNIT_FCP_ICMS_ST_ESTOQUE_C
                                                      FROM ITEMPRODUTO IT, PRODUTO PR, PRODUTO PA, ITEMPROIMPOS IP, IMPOSTO IM, ITMAPARECUP MR
                                                     WHERE IT.CDFILIAL      = C_REGC350.CDFILIAL
                                                       AND IT.NRLANCTONF    = C_REGC350.NRLANCTONF
                                                       AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                                                       AND IM.CDIMPOSTO     = IP.CDIMPOSTO
                                                       AND IT.CDFILIAL      = IP.CDFILIAL
                                                       AND IT.NRLANCTONF    = IP.NRLANCTONF
                                                       AND IT.NRSEQITPROD   = IP.NRSEQITPROD
                                                       AND IT.CDPRODUTO     = PR.CDPRODUTO
                                                       AND PR.CDPRODESTO    = PA.CDPRODUTO(+)
                                                       AND IT.CDPRODUTO     = C_REGC370.CDPRODUTO
                                                       AND IP.NRITPROIMPOS  =  '1'
                                                       AND IP.CDFILIAL      = MR.CDFILIAL(+)
                                                       AND IP.NRLANCTONF    = MR.NRLANCTONF(+)
                                                       AND V_NRSEQUMAPAPU   = MR.NRSEQUMAPAPU(+)
                                                       AND EXISTS (SELECT I.CDFILIAL, I.NRLANCTONF
                                                                     FROM ITEMPROIMPOS I, IMPOSTO M
                                                                    WHERE I.CDFILIAL    = IT.CDFILIAL
                                                                      AND I.NRLANCTONF  = IT.NRLANCTONF
                                                                      AND I.NRSEQITPROD = IT.NRSEQITPROD
                                                                      AND I.CDIMPOSTO   = M.CDIMPOSTO
                                                                      AND M.CDIMPOSTONFE IN ('05'))))) LOOP
                    INSERT INTO ARQMAGNETICO
                       (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
                    VALUES
                       ('20'||LPAD(RTRIM(V_CONTREGC), 8, '0'), C_REGC380.ARQ_C380, P_NRSEQARQMAG);
                    V_CONTREGC := NVL(V_CONTREGC,0) + 1;
                 END LOOP;
              END IF;
         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 + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(NF.VRBASECARGA,0))),2), '999999999990.00')), '.', ',')  || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRIMPOSTONF  + DECODE(V_IDTPCARGATRIB,'O',0,NVL(NF.VRIMPCARGA, 0)),2), '999999999990.00')), '.', ',')  || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRREDBCALC,2),   '999999999990.00')), '.', ',')  || '|' ||
                                  ''  || '|'  ) AS ARQ_C390, CDFISCOPERNF
                            FROM (SELECT NVL(CDSITUTRIBNF,'000') AS CDSITUTRIBNF,
                                         CDFISCOPERNF,
                                         VRALIQNF+DECODE(V_IDSOMAFECP,'S',NVL(VRADICFECP,0),0) AS VRALIQNF,
                                         SUM(ROUND(VRCONTABILNF,2)) AS VRCONTABILNF,
                                         SUM((SELECT SUM(ROUND(VRBASECALCNF,2))
                                                FROM IMPOSTONF IP
                                               WHERE IP.CDFILIAL     = I.CDFILIAL
                                                 AND IP.NRLANCTONF   = I.NRLANCTONF
                                                 AND IP.CDFISCOPERNF = I.CDFISCOPERNF
                                                 AND IP.CDIMPOSTO    = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                         SUM(ROUND(VRBASECALCNF,2)) AS VRBASECALCNF,
                                         SUM(ROUND(VRIMPCARGATRIBI,2)) AS VRIMPCARGA,
                                         SUM(ROUND(VRIMPOSTONF+DECODE(V_IDSOMAFECP,'S',NVL(VRADICFECP,0),0),2)) AS VRIMPOSTONF,
                                         SUM(ROUND(DECODE(IDIMPOSTONF,'2',VRCONTABILNF,'0'),2)) AS VRREDBCALC
                                    FROM IMPOSTONF I, IMPOSTO IM
                                   WHERE I.CDFILIAL      = C_REGC350.CDFILIAL
                                     AND I.NRLANCTONF    = C_REGC350.NRLANCTONF
                                     AND I.CDIMPOSTO     = IM.CDIMPOSTO
                                     AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                     AND IDIMPOSTONF     <> '3'
                                     AND NVL(P_GERAFORSALE,'N') = 'N' 
                                   GROUP BY NVL(CDSITUTRIBNF,'000'), CDFISCOPERNF, VRALIQNF+DECODE(V_IDSOMAFECP,'S',NVL(VRADICFECP,0),0))NF
                           UNION
                          /*GERA REGISTRO C390 PARA O FOS - C390 FOS*/
                          SELECT '|C390'      ||'|'||
                                 CDSITUTRIBIT ||'|'||
                                 CDFISCOPERIT ||'|'||
                                 VRALIQITEM   ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_OPER,2), '999999999990.00')), '.', ',')    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_BC_ICMS,2), '999999999990.00')), '.', ',') ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_ICMS,2), '999999999990.00')), '.', ',')    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_RED,2), '999999999990.00')), '.', ',')     ||'|'||
                                 COD_OB       ||'|' AS ARQ_C390, CDFISCOPERIT AS CDFISCOPERNF
                            FROM (SELECT (NVL(LPAD(IVI.CDCSTPRODI,3,'0'), CASE WHEN IP.IDTPIMPOSFIS = 'T' THEN '000'
                                                                               WHEN IP.IDTPIMPOSFIS = 'I' THEN '040'
                                                                               WHEN IP.IDTPIMPOSFIS = 'F' THEN '060'
                                                                               WHEN IP.IDTPIMPOSFIS = 'N' THEN '041' END)) AS CDSITUTRIBIT,
                                         NVL(IVI.CDCSTPRODPC,'5102')  AS CDFISCOPERIT,
                                         REPLACE(LTRIM(TO_CHAR(ROUND(IVI.VRPEALPRODIT,2), '999999999990.00')), '.', ',') AS VRALIQITEM,
                                         (CASE WHEN IP.CDIMPOSTONFE = P_CDIMPOSTO
                                               THEN SUM(TRUNC(IV.QTPRODVEND * (IV.VRUNITVEND+NVL(IV.VRUNITVENDCL,0)),2) - NVL(IV.VRDESITVEND,0) + NVL(IV.VRACRITVEND,0))
                                               ELSE 0 END) AS VR_OPER,
                                         (CASE WHEN IP.IDTPIMPOSFIS = 'T' AND IP.CDIMPOSTONFE = P_CDIMPOSTO
                                               THEN SUM(TRUNC(IV.QTPRODVEND * (IV.VRUNITVEND+NVL(IV.VRUNITVENDCL,0)),2) - NVL(IV.VRDESITVEND,0) + NVL(IV.VRACRITVEND,0))
                                               ELSE 0 END) AS VR_BC_ICMS,
                                         (CASE WHEN IP.IDTPIMPOSFIS = 'T' AND IP.CDIMPOSTONFE = P_CDIMPOSTO
                                               THEN SUM(IVI.VRIMPOPRODIT)
                                               ELSE 0 END) AS VR_ICMS,
                                         0 AS VR_RED,'' AS COD_OB, IP.CDIMPOSTONFE
                                    FROM ITEMVENDA IV, ITVENDAIMPOS IVI, IMPOSTO IP
                                   WHERE IV.CDFILIAL     = IVI.CDFILIAL
                                     AND IV.CDCAIXA      = IVI.CDCAIXA
                                     AND IV.NRSEQUITVEND = IVI.NRSEQUITVEND
                                     AND IV.NRSEQVENDA   = IVI.NRSEQVENDA
                                     AND IV.CDFILIAL     = C_REGC350.CDFILIAL
                                     AND IV.CDCAIXA      = C_REGC350.CDCAIXA
                                     AND IV.NRSEQVENDA   = C_REGC350.NRLANCTONF
                                     AND NVL(P_GERAFORSALE,'N') = 'S'
                                     AND IP.CDIMPOSTO    = IVI.CDIMPOSTO
                                   GROUP BY IVI.CDCSTPRODI, IVI.CDCSTPRODPC, IP.IDTPIMPOSFIS, IVI.VRPEALPRODIT, IP.CDIMPOSTONFE)NF
                           WHERE NF.CDIMPOSTONFE = P_CDIMPOSTO) 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;
   END IF;

   IF V_CDSITUCRT = '1' AND P_PERFIL = 'A' THEN  /*SIMPLES NACIONAL*/
      /*REGISTRO C350*/
      FOR C_REGC350 IN (SELECT '|C350'     || '|' ||
                               SERIE       || '|' ||
                               ''          || '|' ||
                               NUM_DOC     || '|' ||
                               DT_DOC      || '|' ||
                               CNPJ_CPF    || '|' ||
                               VL_MERC     || '|' ||
                               VL_DOC      || '|' ||
                               VL_DESC     || '|' ||
                               VL_PIS      || '|' ||
                               VL_COFINS   || '|' ||
                               COD_CTA     || '|' AS ARQ_C350, NRLANCTONF, CDFILIAL, CDCLIENTE, CDCAIXA, COD_CTA
                          FROM (SELECT NVL(SS.CDSERIENFE,N.CDSERIE) AS SERIE,
                                       LTRIM(NRNOTAFISC, '0') AS NUM_DOC,
                                       TO_CHAR(N.DTEMISSAO,'ddmmyyyy') AS DT_DOC,
                                       NVL(SUBSTR(REPLACE(REPLACE(REPLACE(REPLACE(N.NRINSJURNF,'.'),'-'),'/'),' '),1,14),'') AS CNPJ_CPF,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(N.VRPRODUTOS,2), '999999999990.00')), '.', ',') AS VL_MERC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(N.VRNOTAFISC,2), '999999999990.00')), '.', ',') AS VL_DOC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(SUM(T.VRITEMENCA),2), '999999999990.00')), '.', ',') AS VL_DESC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1'),2), '999999999990.00')), '.', ',') AS VL_PIS,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1'),2), '999999999990.00')), '.', ',') AS VL_COFINS,
                                       MAX((SELECT MAX(CONTCTBL_SPED(N.IDENTRSAIDOP,I.CDPRODUTO,N.CDTIPOOPER,N.CDFILIEMIT))
                                                     FROM ITEMPRODUTO I
                                                    WHERE I.CDFILIAL   = N.CDFILIAL
                                                      AND I.NRLANCTONF = N.NRLANCTONF)) AS COD_CTA,
                                       I.NRLANCTONF, I.CDFILIAL, N.CDCLIENTE, '' AS  CDCAIXA
                                  FROM IMPOSTONF I, NOTAFISCAL N, ENCARGO E, ITEMENCARGO T, IMPOSTO IM, SERIENF SS
                                 WHERE I.CDIMPOSTO = IM.CDIMPOSTO
                                   AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                   AND N.NRSEQUMAPAPUSN = V_NRSEQUMAPAPU
                                   AND N.CDINSCESTA = P_CDINSCESTA
                                   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 SS.CDEMPRESA(+)   = N.CDEMPRESA
                                   AND SS.CDINSCESTA(+)  = N.CDINSCESTA
                                   AND SS.CDINSCMUNI(+)  = N.CDINSCMUNI
                                   AND SS.CDSERIE(+)     = N.CDSERIE
                                   AND NVL(P_GERAFORSALE,'N') = 'N'
                                   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') <> 'TMP'
                                   AND NVL(N.IDSISTEMA, 'DFS') <> 'FIS')
                                 GROUP BY NVL(SS.CDSERIENFE,N.CDSERIE), LTRIM(NRNOTAFISC, '0'),TO_CHAR(N.DTEMISSAO,'ddmmyyyy'),
                                          NVL(SUBSTR(REPLACE(REPLACE(REPLACE(REPLACE(N.NRINSJURNF,'.'),'-'),'/'),' '),1,14),''),
                                          N.CDCLIENTE, N.VRPRODUTOS, N.VRNOTAFISC,
                                          VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '50', 'I','1'),
                                          VALOR_IMPOSTO_NFE(I.CDFILIAL, I.NRLANCTONF, '60', 'I','1'),
                                          I.NRLANCTONF, I.CDFILIAL, ''
                                 UNION
                                /*GERA C350 PARA O FORSALE - C300 FOS*/
                                SELECT 'D' AS SERIE,
                                       SUBSTR(LPAD(NRNOTAFISC,10,'0'),5,6) AS NUM_DOC,
                                       TO_CHAR(DTEMISSAO,'DDMMYYYY') AS DT_DOC,
                                       NVL(REPLACE(REPLACE(REPLACE(REPLACE(NRINSCRCONS,'.'),'-'),'/'),' '),'') AS CNPJ_CPF,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VRNOTAFISC+VRDESITVEND,2), '999999999990.00')), '.', ',') AS VL_MERC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VRNOTAFISC, 2), '999999999990.00')), '.', ',') AS VL_DOC,
                                       REPLACE(LTRIM(TO_CHAR(ROUND(VRDESITVEND,2), '999999999990.00')), '.', ',') AS VL_DESC,
                                       '0,00' AS VL_PIS,
                                       '0,00' AS VL_COFINS,
                                       CONTCTBL_SPED('S','',CDTIPOOPER,CDFILIAL) AS COD_CTA,
                                       NRLANCTONF, CDFILIAL, '' CDCLIENTE ,CDCAIXA
                                  FROM (SELECT VE.DTENTRVENDA AS DTEMISSAO, VE.NRSERIEVND AS CDSERIE ,VE.NRSUBSERIEVND ,VE.NRORDEMVND AS NRNOTAFISC,
                                               SUM(TRUNC(IT.QTPRODVEND * (IT.VRUNITVEND+NVL(IT.VRUNITVENDCL,0)),2) - NVL(IT.VRDESITVEND,0) + NVL(IT.VRACRITVEND,0)) AS VRNOTAFISC,
                                               SUM(VRDESITVEND) AS VRDESITVEND, PV.CDTPOPERCNTREC AS CDTIPOOPER,
                                               VE.CDFILIAL, VE.NRSEQVENDA AS NRLANCTONF, VE.CDCAIXA, VE.NRINSCRCONS
                                          FROM VENDA VE, ITEMVENDA IT, PARAVEND PV
                                         WHERE VE.CDEMPRESA   =  P_CDEMPRESA
                                           AND VE.CDFILIAL    = IT.CDFILIAL
                                           AND VE.NRSEQVENDA  = IT.NRSEQVENDA
                                           AND VE.CDCAIXA     = IT.CDCAIXA
                                           AND VE.DTENTRVENDA BETWEEN P_DTINICIAL AND P_DTFINAL
                                           AND VE.IDSITUVENDA = 'O'
                                           AND VE.NRORDEMVND  IS NOT NULL
                                           AND NVL(P_GERAFORSALE,'N') = 'S'
                                           AND VE.CDFILIAL    = PV.CDFILIAL(+)
                                         GROUP BY VE.DTENTRVENDA, VE.NRSERIEVND,VE.NRSUBSERIEVND, VE.NRORDEMVND, VE.NRSEQVENDA,
                                               VE.CDFILIAL, VE.CDCAIXA, VE.NRINSCRCONS, PV.CDTPOPERCNTREC))) 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;

         /* PLANO DE CONTAS REFERENCIAL */
         SELECT MAX(CDCHAVEREG)
           INTO V_CONTCTBL
           FROM TMP_REGDADOSSPED
          WHERE CDCHAVEREG = C_REGC350.COD_CTA
            AND CDREGISTRO = '0500';

         IF (V_CONTCTBL IS NULL) AND (C_REGC350.COD_CTA IS NOT NULL) THEN
            V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
            INSERT INTO TMP_REGDADOSSPED
               (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
            VALUES
               ('0500', V_SEQUPROD, C_REGC350.COD_CTA);
         END IF;
         V_CONTCTBL := NULL;

         V_NRSEQITEM := 0;
         FOR C_REGC370 IN(SELECT (P.CDARVPROD  || '|' ||
                                  IT.QTITEMPROD             || '|' ||
                                  P.SGUNIDADE               || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND((SUM(I.VRCONTITIMP)),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND((SUM(IT.VRENCARGODESC)),2), '999999999990.00')), '.', ',')    || '|' ) AS ARQ_C370,
                                  P.CDPRODUTO, P.SGUNIDADE AS UNID, IT.CDPRODNCMTRANS AS CDCLASFISC
                            FROM ITEMPROIMPOS I, ITEMPRODUTO IT, PRODUTO P, IMPOSTO IM
                           WHERE IT.CDFILIAL      = C_REGC350.CDFILIAL
                             AND IT.NRLANCTONF    = C_REGC350.NRLANCTONF
                             AND I.CDIMPOSTO      = IM.CDIMPOSTO
                             AND I.CDFILIAL       = IT.CDFILIAL
                             AND I.NRLANCTONF     = IT.NRLANCTONF
                             AND I.NRSEQITPROD    = IT.NRSEQITPROD
                             AND IT.CDPRODUTO     = P.CDPRODUTO
                             AND IM.CDIMPOSTONFE  = P_CDIMPOSTO
                             AND NVL(P_GERAFORSALE,'N') = 'N'
                           GROUP BY SUBSTR(I.NRSEQITPROD,2,3), P.CDARVPROD, IT.QTITEMPROD, P.SGUNIDADE, P.CDPRODUTO, IT.CDPRODNCMTRANS
                           UNION
                          SELECT CDARVPROD    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(QTPRODVEND, 3), '99999999990.000')), '.', ',') ||'|'||
                                 SGUNIDADE    ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VR_TOT_IT,  2), '999999999990.00')), '.', ',') ||'|'||
                                 REPLACE(LTRIM(TO_CHAR(ROUND(VRDESITVEND,2), '999999999990.00')), '.', ',') ||'|' AS ARQ_C370,
                                 CDPRODUTO, SGUNIDADE AS UNID, CDCLASFISC
                           FROM (SELECT NRSEQUITVEND,
                                        P.CDARVPROD,
                                        QTPRODVEND,
                                        P.SGUNIDADE,
                                        (IV.VRUNITVEND+NVL(IV.VRUNITVENDCL,0)+NVL(IV.VRACRITVEND,0))  AS VR_TOT_IT,
                                        VRDESITVEND, P.CDPRODUTO, IV.CDCLASFISC
                                   FROM ITEMVENDA IV, PRODUTO P
                                  WHERE IV.CDFILIAL   = C_REGC350.CDFILIAL
                                    AND IV.CDCAIXA    = C_REGC350.CDCAIXA
                                    AND IV.NRSEQVENDA =  C_REGC350.NRLANCTONF
                                    AND NVL(P_GERAFORSALE,'N') = 'S'
                                    AND IV.CDPRODUTO  = P.CDPRODUTO)NF) LOOP

               V_NRSEQITEM := NVL(V_NRSEQITEM,0)+1;
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('24'||LPAD(RTRIM(V_CONTREGC), 8, '0'), '|C370|'||LPAD(V_NRSEQITEM,3,'0')||'|'||C_REGC370.ARQ_C370, P_NRSEQARQMAG);
               V_CONTREGC := NVL(V_CONTREGC,0) + 1;

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

               IF (V_PRODUTO IS NULL) AND (C_REGC370.CDPRODUTO IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG, CDCAMPO3)
                  VALUES
                    ('0200', V_SEQUPROD, C_REGC370.CDPRODUTO, C_REGC370.CDCLASFISC);
               END IF;
               V_PRODUTO := NULL;

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

               IF (V_UNID IS NULL) AND (C_REGC370.UNID IS NOT NULL) THEN
                  V_SEQUPROD := NVL(V_SEQUPROD,0) + 1;
                  INSERT INTO TMP_REGDADOSSPED
                    (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
                  VALUES
                    ('0190', V_SEQUPROD, C_REGC370.UNID);
               END IF;
               V_UNID := NULL;
         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 + DECODE(V_IDTPCARGATRIB,'O',0,DECODE(V_SGESTADO,'SP',0,NVL(NF.VRBASECARGA,0))),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRIMPOSTONF  + DECODE(V_IDTPCARGATRIB,'O',0,NVL(NF.VRIMPCARGA,0)),2), '999999999990.00')), '.', ',')    || '|' ||
                                  REPLACE(LTRIM(TO_CHAR(ROUND(NF.VRREDBCALC,2), '999999999990.00')), '.', ',')    || '|' ||
                                  ''  || '|'  ) AS ARQ_C390, CDFISCOPERNF
                          FROM(SELECT NVL(I.CDSITUTRIBNF,'000') AS CDSITUTRIBNF,
                                      I.CDFISCOPERNF,
                                      I.VRALIQNF+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0) AS VRALIQNF,
                                      SUM(I.VRCONTABILNF) AS VRCONTABILNF,
                                      SUM(I.VRBASECALCNF) AS VRBASECALCNF,
                                      SUM((SELECT SUM(IP.VRBASECALCNF)
                                             FROM IMPOSTONF IP
                                            WHERE IP.CDFILIAL     = I.CDFILIAL
                                              AND IP.NRLANCTONF   = I.NRLANCTONF
                                              AND IP.CDFISCOPERNF = I.CDFISCOPERNF
                                              AND IP.CDIMPOSTO    = IM.CDIMPCARGATRIB)) AS VRBASECARGA,
                                      SUM(I.VRIMPOSTONF+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICFECP,0),0)) AS VRIMPOSTONF,
                                      SUM(I.VRIMPCARGATRIBI) AS VRIMPCARGA,
                                      SUM(DECODE(I.IDIMPOSTONF, '2', I.VRCONTABILNF, '0')) AS VRREDBCALC
                                 FROM IMPOSTONF I, IMPOSTO IM
                                WHERE I.CDFILIAL      = C_REGC350.CDFILIAL
                                  AND I.NRLANCTONF    = C_REGC350.NRLANCTONF
                                  AND I.CDIMPOSTO     = IM.CDIMPOSTO
                                  AND IM.CDIMPOSTONFE = P_CDIMPOSTO
                                  AND IDIMPOSTONF <> '3'
                                  AND NVL(P_GERAFORSALE,'N') = 'N'
                                GROUP BY NVL(CDSITUTRIBNF,'000'), CDFISCOPERNF, VRALIQNF+DECODE(V_IDSOMAFECP,'S',NVL(I.VRADICALIQFECP,0),0)) NF
                          UNION
                          /*GERA REGISTRO C390 PARA O FOS - C390 FOS*/
