CREATE OR REPLACE FUNCTION GIA_RS_X14(
   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 XIV - OUTROS CREDITOS - DETALHAMENTO************************************/
BEGIN
   V_QUANTIDADE:= 0;
   V_SEQUENCIA:= P_SEQUENCIA;
   V_REG_1:=P_REG_1;
   FOR C_OC 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,
                      'X14 '                                                       ||
                      '01'                                                         ||
                      SUBSTR(LPAD(IM.NRSEQITMAPR,10,'0'),8,3)                      ||
                      REPLACE(LTRIM(TO_CHAR(IM.VRITEMMAPR,'00000000000.00')),'.')  ||
                      RPAD(DECODE(SUBSTR(LPAD(IM.NRSEQITMAPR,10,'0'),8,3),'099',SUBSTR(IM.DSITEMMAPR,1,60),' '),60,' ')  AS CORPO
                  FROM ITMAPARECUP IM, INSCRESTAD IE
                  WHERE  IM.IDTIPOITMAPR = 'OC'               AND
                         IM.NRSEQUMAPAPU = P_NRSEQUMAPAPU     AND
                         IE.CDINSCESTA   = P_CDINSCESTA       AND
                         IE.CDEMPRESA    = P_CDEMPRESA        AND
                         IE.IDAPURLUCPRE = 'N')LOOP
       V_SEQUENCIA:= V_SEQUENCIA+1;
       INSERT INTO ARQMAGNETICO
          (NRORDREGARQ,DSREGARQMAG,NRSEQARQMAG)
       VALUES(LTRIM(TO_CHAR(V_SEQUENCIA,'0000000000')),C_OC.CABECALHO||LTRIM(TO_CHAR(V_SEQUENCIA,'0000'))||
           C_OC.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;
/