CREATE OR REPLACE FUNCTION GIA_RS_X15(
   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_QUANTIDADE NUMBER;
   V_REG_1      VARCHAR2(1000);
/************************************ANEXO XV - OUTROS DEBITOS DETALHAMENTO*****************************************/
BEGIN
   V_QUANTIDADE:=0;
   V_REG_1:= P_REG_1;
   V_SEQUENCIA:=P_SEQUENCIA;
   FOR C_OD IN(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') As CABECALHO,
                      'X15 '                                                       ||
                      '01'                                                         ||
                      LPAD(IM.CODIGO,3,'0')                                   ||
                      REPLACE(LTRIM(TO_CHAR(IM.VALOR,'00000000000.00')),'.')  ||
                      RPAD(DECODE(IM.CODIGO,'099',SUBSTR(IM.ESPEC,1,60),' '),60,' ')  AS CORPO
                 FROM (SELECT '001' AS CODIGO, ROUND(SUM(VRITEMMAPR),2) AS VALOR, ' ' AS ESPEC
                         FROM ITMAPARECUP IM
                        WHERE IM.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                          AND IM.IDTIPOITMAPR = 'DA'
                        GROUP BY DSITEMMAPR
                       UNION ALL
                       SELECT '002' AS CODIGO, ROUND(SUM(VRITEMMAPR),2) AS VALOR, ' ' AS ESPEC
                         FROM ITMAPARECUP IM
                        WHERE IM.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                          AND IM.IDTIPOITMAPR = 'EC'
                        GROUP BY DSITEMMAPR
                       UNION ALL
                       SELECT '099' AS CODIGO, ROUND(SUM(VRITEMMAPR),2) AS VALOR, DSITEMMAPR AS ESPEC
                         FROM ITMAPARECUP IM
                        WHERE IM.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                          AND IM.IDTIPOITMAPR = 'OD'
                          AND IM.DSITEMMAPR <> 'CREDITO PRESUMIDO'
                        GROUP BY DSITEMMAPR)IM )LOOP
       V_SEQUENCIA:= V_SEQUENCIA+1;
       INSERT INTO ARQMAGNETICO
          (NRORDREGARQ,DSREGARQMAG,NRSEQARQMAG)
       VALUES(LTRIM(TO_CHAR(V_SEQUENCIA,'0000000000')),C_OD.CABECALHO||LTRIM(TO_CHAR(V_SEQUENCIA,'0000'))||
           C_OD.CORPO,P_NRSEQUENCIAL);
       V_QUANTIDADE:= V_QUANTIDADE+1;
   END LOOP;

   /***********CONTADOR DO ANEXO ABC*************************************************************************************/
   V_REG_1:=V_REG_1||LTRIM(TO_CHAR(V_QUANTIDADE,'000'));
   RETURN(V_REG_1);
END;
/