CREATE OR REPLACE FUNCTION GIA_RS_XDC(
   P_CDEMPRESA      IN EMPRESA.CDEMPRESA%TYPE,
   P_CDIMPOSTO      IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDINSCESTA     IN INSCRESTAD.CDINSCESTA%TYPE,
   P_DTINIPERIODO   IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_DTFIMPERIODO   IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_VERSAO         IN IMPOSTO.CDIMPOSTO%TYPE,
   P_NRSEQUENCIAL   IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_ENTCOMPLE      IN NOTAFISCAL.IDDOCFISCAL%TYPE,
   P_SEQUENCIA      IN NUMBER,
   P_CODENTREGA     IN VARCHAR2)RETURN VARCHAR2
AS
   V_SEQUENCIA  NUMBER;
   V_DADOS      VARCHAR(1000);
/****************************DADOS DA EMPRESA - REGISTRO XDC*************************************************/

BEGIN

   V_SEQUENCIA:= P_SEQUENCIA;
   BEGIN
      SELECT '****'                                                                                            ||
             P_VERSAO                                                                                          ||
             DECODE(P_VERSAO,'07','00000000','')                                                               ||/*VERSAO 07*/
             DECODE(P_VERSAO,'07',LPAD(' ',1,' '),'')                                                          ||
             LTRIM(TO_CHAR(P_DTINIPERIODO,'DD'))                                                               ||
             LTRIM(TO_CHAR(P_DTFIMPERIODO,'DD'))                                                               ||
             LTRIM(TO_CHAR(P_DTFIMPERIODO,'MMYYYY'))                                                           ||
             LPAD(P_CDINSCESTA,10,'0')                                                                         ||
             LTRIM(TO_CHAR(V_SEQUENCIA,'0000'))                                                                ||
             'XDC '                                                                                            ||
             REPLACE(REPLACE(NRINSJUREMPR,'.'),'/')                                                            ||
             RPAD(SUBSTR(NMRAZSOCEMPR,1,50),50,' ')                                                            ||
             SUBSTR(LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(NRTELEIEST,'-'),'('),')'),'.'),' '),'0'),12,'0'),1,3) ||
             SUBSTR(LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(NRTELEIEST,'-'),'('),')'),'.'),' '),'0'),12,'0'),-9,9) ||
             P_ENTCOMPLE                                                                                       ||
             RPAD(NVL(P_CODENTREGA,' '),6,' ')
        INTO V_DADOS
        FROM EMPRESA E, INSCRESTAD I
       WHERE E.CDEMPRESA  = P_CDEMPRESA
         AND E.CDEMPRESA  = I.CDEMPRESA
         AND I.CDINSCESTA = P_CDINSCESTA;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
      V_DADOS := '';
      V_SEQUENCIA:= V_SEQUENCIA-1;
   END;

   IF V_DADOS IS NOT NULL THEN
      V_SEQUENCIA:= V_SEQUENCIA+1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ,DSREGARQMAG,NRSEQARQMAG)
      VALUES(LTRIM(TO_CHAR(V_SEQUENCIA,'0000000000')),V_DADOS,P_NRSEQUENCIAL);
   END IF;

  RETURN(V_SEQUENCIA);
END;
/