CREATE OR REPLACE FUNCTION ARQUIVO_DACON_R11(
   P_DTINICIO               IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFIM                  IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_CDEMPRESA              IN EMPRESA.CDEMPRESA%TYPE,
   P_NRSEQARQMAG            IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_DIFCONTR               IN VARCHAR2) RETURN NUMBER

AS
V_CONTREG        NUMBER;

BEGIN

 /*------------------------------R11---------------------------------*/

   FOR C_REG_R11 IN (SELECT (RPAD('R11',4,' ')                                                                                                          ||
                             TO_CHAR(c.DTDIFDACON,'MM')                                                                                                 ||
                             TO_CHAR(c.DTDIFDACON,'YYYY')                                                                                               ||
                             '000'                                                                                                                      ||
                             LPAD(' ',14,' ')                                                                                                           ||
                             LPAD(' ',2,' ')                                                                                                            ||
                             LPAD(' ',8,' ')                                                                                                            ||
                             '1'                                                                                                                        || 
                             LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(VRDIFDACON,0)),'000000000000.00'),'.')),'14','0')                                       ||
                             LPAD('0',14,'0')                                                                                                           ||
                             DECODE ('1','9',LPAD('0',14,'0'),LPAD(LTRIM(REPLACE(TO_CHAR(SUM(NVL(VRCREDDACON,0)),'000000000000.00'),'.')),'14','0'))    ||
                             LPAD('0',14,'0')                                                                                                           ||
                             LPAD('0',14,'0')                                                                                                           ||
                             LPAD('0',14,'0')                                                                                                           ||
                             LPAD('0',14,'0')                                                                                                           ||
                             LPAD('0',14,'0')                                                                                                           ||
                             LPAD('0',14,'0')                                                                                                           ||
                             LPAD('0',10,'0')                                                                                                           ) AS R11
                          FROM CTRLDIFDACON C
                         WHERE C.CDEMPRESA     = P_CDEMPRESA
                           AND C.DTREFDIFDACON = P_DTINICIO
                           AND C.CDFICHADACON  = 'F11'
                           AND C.CDIMPDACON    = '50'
                       GROUP BY TO_CHAR(c.DTDIFDACON,'MM'),TO_CHAR(c.DTDIFDACON,'YYYY')) LOOP

      V_CONTREG := NVL(V_CONTREG,0) + 1;

      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         ('121'||LPAD(RTRIM(V_CONTREG), 7, '0'), C_REG_R11.R11, P_NRSEQARQMAG);

   END LOOP;


   RETURN(V_CONTREG);

END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_DACON_R11','5.60.000'); END;
/