CREATE OR REPLACE FUNCTION GIA_RS_X08(
   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_DAINIPER       IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_DATAFIMPER     IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_IMPPAGMES      IN IMPOSTONF.VRIMPOSTONF%TYPE,
   P_DATAVENC       IN MAPAAPURAC.DTINIPERIODO%TYPE)RETURN VARCHAR2
AS
   V_SEQUENCIA  NUMBER;
   V_REG_1      VARCHAR2(1000);

/***************************************************ANEXO VIII PAGAMENTOS ICMS NO MES*********************************/
BEGIN
   V_REG_1:=P_REG_1;
   IF P_IMPPAGMES <> '' THEN
      V_SEQUENCIA:= P_SEQUENCIA+1;
      INSERT INTO ARQMAGNETICO
               (NRORDREGARQ,DSREGARQMAG,NRSEQARQMAG)
      VALUES(LTRIM(TO_CHAR(V_SEQUENCIA,'0000000000')),'****'||
             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'))             ||
             'X08 '                                         ||
             '01'                                           ||
             LTRIM(TO_CHAR(P_DAINIPER,'DD'))                ||
             LTRIM(TO_CHAR(P_DATAFIMPER,'DD'))              ||
             P_DATAVENC                                     ||
             LPAD(REPLACE(TO_CHAR(P_IMPPAGMES),'.'),13,'0') ||
             LPAD('0',13,'0')                               ||
             LPAD(P_CDINSCESTA,10,'0'),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;
/