CREATE OR REPLACE FUNCTION ARQUIVO_DACON_R01(
   P_DTINICIO               IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFIM                  IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DEMONSTRATIVO          IN VARCHAR2,
   P_CDEMPRESA              IN EMPRESA.CDEMPRESA%TYPE,
   P_NRSEQARQMAG            IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_ENTREGA                IN VARCHAR2,
   P_SITESPECIAL            IN VARCHAR2,
   P_DESENQUADRA            IN VARCHAR2,
   P_QUALIFICACAO           IN VARCHAR2,
   P_ENTIDADE               IN VARCHAR2,
   P_INCLUSAO               IN VARCHAR2,
   P_DTEVENTO               IN DATE,
   P_PISPASEP               IN VARCHAR2,
   P_APCRED                 IN VARCHAR2,
   PISCOFALIQDIFCONTR       IN VARCHAR2,
   PISCOFALIQDIFSITTRIB     IN VARCHAR2,
   PISCOFALIQUNPRODCONTR    IN VARCHAR2,
   PISCOFALIQUNPRODSITTRIB  IN VARCHAR2,
   P_ADCONTR                IN VARCHAR2,
   P_DIFCONTR               IN VARCHAR2,
   P_CREDTRANSF             IN VARCHAR2,
   P_CREDDIF                IN VARCHAR2,
   P_DESCMES                IN VARCHAR2,
   P_METDET                 IN VARCHAR2) RETURN NUMBER

AS
V_CONTREG        NUMBER;

BEGIN

 /*------------------------------HEADER---------------------------------*/

   FOR C_REG_HEADER IN (SELECT (RPAD('DACONMS',8,' ')                                                                       ||
                                 '0000'                                                                                     ||
                                 TO_CHAR(P_DTINICIO,'YYYY')                                                                 ||
                                 '0000'                                                                                     ||
                                 P_DEMONSTRATIVO                                                                            ||
                                 LPAD(REPLACE(REPLACE(REPLACE(REPLACE(E.NRINSJUREMPR,'.',''),'-',''),'/',''),' ',''),14,0)  ||
                                 '2'                                                                                        ||
                                 '250'                                                                                      ||
                                 RPAD(E.NMRAZSOCEMPR,60,' ')                                                                ||
                                 RPAD(SGESTADO,2,' ')                                                                       ||
                                 '0000000000'                                                                               ||
                                 LPAD(' ',250,' ')                                                                          ||
                                 '000'                                                                                      ||
                                 '0000000000'                                                                               ) AS HEADER
                          FROM EMPRESA E
                         WHERE E.CDEMPRESA = P_CDEMPRESA) LOOP

      V_CONTREG := NVL(V_CONTREG,0) + 1;

      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         ('00'||LPAD(RTRIM(V_CONTREG), 8, '0'), C_REG_HEADER.HEADER, P_NRSEQARQMAG);

   END LOOP;

   /*------------------------------R01---------------------------------*/

   FOR C_REG_R01 IN (SELECT (RPAD('R01',4,' ')                                                                          ||
                             LPAD(REPLACE(REPLACE(REPLACE(REPLACE(E.NRINSJUREMPR,'.',''),'-',''),'/',''),' ',''),14,0)  ||
                             P_ENTREGA                                                                                  ||
                             TO_CHAR(P_DTINICIO,'YYYYMM')                                                               ||
                             TO_CHAR(P_DTINICIO,'YYYYMMDD')                                                             ||
                             TO_CHAR(P_DTFIM,'YYYYMMDD')                                                                ||
                             P_DEMONSTRATIVO                                                                            ||
                             P_SITESPECIAL                                                                              ||
                             CASE WHEN P_SITESPECIAL <> '00' THEN TO_CHAR(P_DTEVENTO,'YYYYMMDD') ELSE '00000000' END    ||
                             P_DESENQUADRA                                                                              ||
                             CASE WHEN P_DESENQUADRA = '1' THEN TO_CHAR(P_DTEVENTO,'YYYYMMDD') ELSE '00000000' END      ||
                             '0000000000'                                                                               ||
                             P_QUALIFICACAO                                                                             ||
                             P_ENTIDADE                                                                                 ||
                             P_INCLUSAO                                                                                 ||
                             CASE WHEN P_INCLUSAO = '1' THEN TO_CHAR(P_DTEVENTO,'YYYYMMDD') ELSE '00000000' END         ||
                             P_PISPASEP                                                                                 ||
                             DECODE(P_PISPASEP,'3','0',P_APCRED)                                                        ||
                             PISCOFALIQDIFCONTR                                                                         ||
                             PISCOFALIQDIFSITTRIB                                                                       ||
                             PISCOFALIQUNPRODCONTR                                                                      ||
                             PISCOFALIQUNPRODSITTRIB                                                                    ||
                             P_ADCONTR                                                                                  ||
                             P_DIFCONTR                                                                                 ||
                             P_CREDTRANSF                                                                               ||
                             P_CREDDIF                                                                                  ||
                             P_DESCMES                                                                                  ||
                             (CASE WHEN ((P_PISPASEP = '3') OR (P_PISPASEP = '0')) THEN ' ' ELSE P_METDET END) ||
                              '0000000000'
                                                                                                                         ) AS REG_R01
                       FROM EMPRESA E
                      WHERE E.CDEMPRESA = P_CDEMPRESA) LOOP
      V_CONTREG := NVL(V_CONTREG,0) + 1;

      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         ('01'||LPAD(RTRIM(V_CONTREG), 8, '0'), C_REG_R01.REG_R01, P_NRSEQARQMAG);
   END LOOP;

   RETURN(V_CONTREG);

END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_DACON_R01','5.60.000'); END;
/