create or replace
FUNCTION           ARQUIVO_10_DIEF_MA ( 
   P_CDEMPRESA   IN EMPRESA.CDEMPRESA%TYPE, 
   P_CDINSCESTA  IN NOTAFISCAL.CDINSCESTA%TYPE, 
   P_DTINICIAL   IN NOTAFISCAL.DTENTRSAID%TYPE, 
   P_DTFINAL     IN NOTAFISCAL.DTENTRSAID%TYPE, 
   P_VERSAO      IN NOTAFISCAL.IDSTATUS%TYPE, 
   P_FINALIDADE  IN NOTAFISCAL.IDSTATUS%TYPE, 
   P_SEQMAGNET   IN VARCHAR) 
RETURN NUMBER 
AS 
   V_CONTADOR   NUMBER; 
BEGIN 
   V_CONTADOR := 0; 
   FOR C_REGISTRO10 IN (SELECT SUBSTR( 
                           '10'|| 
                           RPAD(SUBSTR(REPLACE(REPLACE(REPLACE(REPLACE(IE.NRINSJURIEST,'.',''),',',''),'-',''),'/',''),1,14),14,' ')|| 
                           RPAD(REPLACE(REPLACE(REPLACE(REPLACE(DECODE(SUBSTR(UPPER(IE.CDINSCESTA),1,5),'ISENT','ISENTO',IE.CDINSCESTA),'.',''),',',''),'-',''),'/',''),14,' ')|| 
                           RPAD(EP.NMRAZSOCEMPR,35,' ')|| 
                           RPAD(MU.NMMUNICIPIO,30,' ')|| 
                           RPAD(IE.SGESTADO,2,' ')|| 
                           LPAD(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(NVL(IE.NRTELEIEST,'0'),' ',''),')',''),'(',''),'-',''),'.',''),10,'0')|| 
                           TO_CHAR(TO_DATE(P_DTINICIAL),'yyyymmdd')|| 
                           TO_CHAR(TO_DATE(P_DTFINAL),'yyyymmdd')|| 
                           P_VERSAO ||'3'||P_FINALIDADE,1,126) AS DSARQMAG 
                        FROM EMPRESA EP, INSCRESTAD IE, MUNICIPIO MU 
                        WHERE EP.CDEMPRESA   = P_CDEMPRESA    AND 
                              IE.CDEMPRESA   = EP.CDEMPRESA   AND 
                              IE.CDINSCESTA  = P_CDINSCESTA   AND 
                              IE.CDPAIS      = MU.CDPAIS(+)   AND 
                              IE.SGESTADO    = MU.SGESTADO(+) AND 
                              IE.CDMUNICIPIO = MU.CDMUNICIPIO(+)) LOOP 
      V_CONTADOR := V_CONTADOR + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        (LPAD(V_CONTADOR, 8, '0'), C_REGISTRO10.DSARQMAG, P_SEQMAGNET); 
   END LOOP; 
   RETURN(V_CONTADOR); 
END; 
/
-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_10_DIEF_MA','5.11.026'); END;
/

 
