CREATE OR REPLACE FUNCTION ARQUIVO_DAC_INICIO(
   P_CDEMPRESA   IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA  IN NOTAFISCAL.CDINSCESTA%TYPE,
   P_DTINICIAL   IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL     IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_VERSAO      IN NOTAFISCAL.IDSTATUS%TYPE,
   P_RELEASE     IN VARCHAR,
   P_SBRELEASE   IN VARCHAR,
   P_TPDEC       IN VARCHAR,
   P_PDEC        IN VARCHAR,
   P_INDMOV      IN VARCHAR,
   P_INDENC      IN VARCHAR,
   P_SEQMAGNET   IN VARCHAR)
RETURN NUMBER
AS
   V_REGISTRO_00    VARCHAR2(500);
   V_REGISTRO_01    VARCHAR2(500);
   V_REGISTRO_10    VARCHAR2(500);
   V_REGNULO_10     VARCHAR2(500);
   V_CONTADOR       NUMBER;
   V_CONTGERAL      NUMBER;
   V_QDATAFINAL     VARCHAR2(6);
   V_NRCPFREPRES    VARCHAR2(500);
   V_NMREPRES       VARCHAR2(500);
BEGIN 
   V_CONTADOR  := 0; 
   V_CONTGERAL := 0; 
   IF (P_PDEC = 'Q') THEN 
      V_QDATAFINAL := TO_CHAR(P_DTFINAL,'YYYYMM'); 
   ELSE 
      V_QDATAFINAL :=  TO_CHAR(P_DTINICIAL,'YYYYMM'); 
   END IF;
   V_REGISTRO_00 := '00'
                    ||'DAC2200'
                    ||RPAD(' ',6,' ')
                    ||SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(P_CDINSCESTA, ' '),'.',''),'-',''),'ISENT','ISENTO'),9,' '),1,9)
                    ||P_TPDEC
                    ||P_PDEC
                    ||V_QDATAFINAL
                    ||RPAD(' ',10,' ');
   V_CONTADOR := NVL(V_CONTADOR,0)+1;
   INSERT INTO ARQMAGNETICO
       (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
     VALUES
       (LPAD(V_CONTADOR, 10, '0'),V_REGISTRO_00,P_SEQMAGNET);
   V_CONTGERAL := V_CONTGERAL + 1;

   /* dados do representante legal da empresa */
   SELECT MAX(R.NRCPFREPRES), MAX(R.NMREPRES)
     INTO V_NRCPFREPRES, V_NMREPRES
     FROM REPRESEMPRESA E, REPRESLEGAL R
    WHERE E.CDEMPRESA = P_CDEMPRESA
      AND E.IDATIVO = 'S'
      AND E.CDREPRES = R.CDREPRES;

   SELECT SUBSTR(RPAD(MAX(NVL(C.DSENDINTERN,' ')),60,' '),1,60)
          ||RPAD(MAX(NVL(C.DSENDINTERN,' ')),60,' ')
          ||RPAD(MAX(NVL(C.DSENDINTERN,' ')),60,' ')
          ||RPAD(MAX(NVL(C.NRTELCONTATO, ' ')),15,' ')
          ||RPAD(MAX(NVL(I.NRTELEIEST, ' ')),15,' ')
     INTO V_REGISTRO_01
     FROM CONTADOR C,INSCRESTAD I,EMPRESA E
    WHERE E.CDEMPRESA   = P_CDEMPRESA
      AND E.CDCONTADOR2 = C.CDCONTADOR2
      AND E.CDEMPRESA = I.CDEMPRESA
      AND I.CDINSCESTA  = P_CDINSCESTA;
      IF (V_REGISTRO_01 IS NULL) THEN
         V_REGISTRO_01 := LPAD(' ',210,' ');
      END IF;
      V_CONTADOR := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
        (LPAD(V_CONTADOR, 10, '0'),'01'||V_REGISTRO_01,P_SEQMAGNET);
   BEGIN
      SELECT SUBSTR(LPAD(REPLACE(REPLACE(REPLACE(NVL(I.CDINSCESTA, 0),'.',''),'-',''),'ISENT','ISENTO'),9,0),1,9)
             ||LPAD(SUBSTR(REPLACE(REPLACE(REPLACE(NVL(I.NRINSJURIEST,' '),'/',''),'.',''),'-',''),1,14),14,' ')
             ||RPAD(SUBSTR(I.NMREPRINSEST,1,40),40,' ')
             ||TO_CHAR(P_DTINICIAL,'YYYYMM')
             ||TO_CHAR(P_DTFINAL,'YYYYMM')
             ||TO_CHAR(P_DTINICIAL,'YYYYMMDD')
             ||TO_CHAR(P_DTFINAL,'YYYYMMDD')
             ||P_INDMOV
             ||'00'
             ||'01'
             ||SUBSTR(RPAD(NVL(REPLACE(C.NRCRCCONT2,'1SP','SP'),' '),10,' '),1,10)
             ||SUBSTR(LPAD(NVL(C.NRINSJURCONT,0),11,0),1,11)
             ||SUBSTR(RPAD(NVL(C.NMCONTADOR,' '),40,' '),1,40)
             ||SUBSTR(LPAD(NVL(V_NRCPFREPRES,0),11,0),1,11) --15
             ||SUBSTR(RPAD(NVL(V_NMREPRES,' '),40,' '),1,40) --16
             ||P_INDENC
        INTO V_REGISTRO_10
        FROM INSCRESTAD I, CONTADOR C
       WHERE I.CDEMPRESA   = P_CDEMPRESA
         AND I.CDINSCESTA  = P_CDINSCESTA
         AND I.CDCONTADOR2 = C.CDCONTADOR2(+);
   EXCEPTION 
      WHEN NO_DATA_FOUND THEN 
         V_REGISTRO_10 := LPAD(0,9,0)||LPAD(' ',66,' ')||TO_CHAR(P_DTINICIAL,'YYYYMM')|| 
                          TO_CHAR(P_DTFINAL,'YYYYMM')||P_INDMOV||RPAD(' ',2,' ')||'01'|| 
                          LPAD(' ',113,' '); 
   END;
      V_CONTADOR := NVL(V_CONTADOR,0)+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
        VALUES
        (LPAD(V_CONTADOR, 10, '0'),'10'||V_REGISTRO_10,P_SEQMAGNET);
        V_CONTGERAL := V_CONTGERAL + 1;
   RETURN(LPAD(V_CONTADOR,10,0));
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_DAC_INICIO','5.75.000'); END;
/

