CREATE OR REPLACE FUNCTION GIA_RS_X07(
   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_SEQUENCIA      IN NUMBER,
   P_REG_1          IN VARCHAR2,
   P_NRSEQUMAPAPU   IN MAPAAPURAC.NRSEQUMAPAPU%TYPE )RETURN VARCHAR2
AS
   V_SEQUENCIA  NUMBER;
   V_REG_1      VARCHAR2(1000);
   V_DADOS      VARCHAR2(1000);
/***************************ANEXO VII  - OUTRAS SAIDAS ST- DETALHAMENTO********************************/
BEGIN
   V_REG_1:=P_REG_1;
   V_SEQUENCIA:= P_SEQUENCIA+1;
   V_DADOS := ' ';
   BEGIN
      SELECT '****'                                                                                         ||
             P_VERSAO                                                                                       ||
             DECODE(P_VERSAO,'07','00000000','')                                                            ||/*VERSAO 07*/
             DECODE(P_VERSAO,'07',LPAD(' ',1,' '),'')                                                       ||/*VERSAO 07*/
             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'))                                                             ||
             'X07 '                                                                                         ||
             '01'                                                                                           ||
             REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(ST.VRCREDITONORMAL+ST.VRDEVMAPA+ST.VRRESSAMAPA,0),2)),'00000000000.00')),'.')||
             '0000000000000'                                                                                ||
             LPAD(' ',60,' ')                                                                               ||
             REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(ST.VRCREDITONORMAL+ST.VRDEVMAPA+ST.VRRESSAMAPA,0),2)),'00000000000.00')),'.')||
             REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(ST.VRDEBITONORMAL,0),2)),'00000000000.00')),'.')           ||
             '0000000000000'                                                                                ||
             LPAD(' ',60,' ')                                                                               ||
             REPLACE(LTRIM(TO_CHAR(SUM(ROUND(NVL(ST.VRDEBITONORMAL,0),2)),'00000000000.00')),'.')
        INTO V_DADOS
        FROM MAPAAPURAC M, MAPAAPURESTADO ST
       WHERE M.CDEMPRESA     = P_CDEMPRESA     AND
             M.CDIMPOSTO     = '80'            AND
             M.CDINSCESTA    = P_CDINSCESTA    AND
             M.DTINIPERIODO  = P_DTINIPERIODO  AND
             M.DTFIMPERIODO  = P_DTFIMPERIODO  AND
             M.NRSEQUMAPAPU  = ST.NRSEQUMAPAPU;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_DADOS:='';
         V_SEQUENCIA:= V_SEQUENCIA-1;
   END;

   IF V_DADOS <> ' ' THEN
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ,DSREGARQMAG,NRSEQARQMAG)
      VALUES(LTRIM(TO_CHAR(V_SEQUENCIA,'0000000000')),V_DADOS,P_NRSEQUENCIAL);
      V_REG_1:= V_REG_1||LPAD('1',3,'0');
   ELSE
      V_REG_1:= V_REG_1||LPAD('0',3,'0');
   END IF;
   RETURN(V_REG_1);
END;
/