CREATE OR REPLACE FUNCTION ARQUIVO_DACON_R03(
   P_CDEMPRESA       IN EMPRESA.CDEMPRESA%TYPE,
   P_NRSEQARQMAG     IN ARQMAGNETICO.NRSEQARQMAG%TYPE) RETURN NUMBER
AS
   V_CONTREG03          NUMBER;
   V_NMREPRES           VARCHAR2(300);
   V_NRCPFREPRES        VARCHAR2(300);
   V_NRDDDTELEREPRES1   VARCHAR2(300);
   V_NRTELEREPRES1      VARCHAR2(300);
   V_NRRAMALREPRES1     VARCHAR2(300);
   V_NRDDDFAXREPRES     VARCHAR2(300);
   V_NRFAXREPRES        VARCHAR2(300);
   V_DSEMAILREPRES      VARCHAR2(300);

BEGIN

 /*-------------------------------------R03 -----------------------------------*/

   /* dados do representante legal da empresa */
   SELECT MAX(R.NMREPRES),
          MAX(R.NRCPFREPRES),
          MAX(R.NRDDDTELEREPRES1),
          MAX(R.NRTELEREPRES1),
          MAX(R.NRRAMALREPRES1),
          MAX(R.NRDDDFAXREPRES),
          MAX(R.NRFAXREPRES),
          MAX(R.DSEMAILREPRES)
     INTO V_NMREPRES, V_NRCPFREPRES, V_NRDDDTELEREPRES1, V_NRTELEREPRES1,
          V_NRRAMALREPRES1, V_NRDDDFAXREPRES, V_NRFAXREPRES, V_DSEMAILREPRES
     FROM REPRESEMPRESA E, REPRESLEGAL R
    WHERE E.CDEMPRESA = P_CDEMPRESA
      AND E.IDATIVO = 'S'
      AND E.CDREPRES = R.CDREPRES;

   FOR C_REG_R03 IN (SELECT (RPAD('R03',4,' ')                                                                                                  ||
                             RPAD(NVL(V_NMREPRES,' '),150,' ')                                                                                  ||
                             LPAD(NVL(V_NRCPFREPRES,' '),11,'0')                                                                                ||
                             LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(V_NRDDDTELEREPRES1,'-'),'.'),'('),')'),' '),' '),4,' ')           ||
                             SUBSTR(LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(V_NRTELEREPRES1,'-'),'.'),'('),')'),' '),' '),12,' '),5,8) ||
                             LPAD(NVL(V_NRRAMALREPRES1,' '),5,' ')                                                                              ||
                             LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(V_NRDDDFAXREPRES,'-'),'.'),'('),')'),' '),' '),4,' ')             ||
                             SUBSTR(LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(V_NRFAXREPRES,'-'),'.'),'('),')'),' '),' '),12,' '),5,8)   ||
                             RPAD(NVL(V_DSEMAILREPRES,' '),115,' ')                                                                             ||
                             RPAD(NVL(NMCONTADOR,' '),150,' ')                                                                                  ||
                             LPAD(NVL(NRCPFRESPONS,' '),11,' ')                                                                                 ||
                             RPAD(NVL(NRCRCCONT2,' '),15,' ')                                                                                   ||
                             RPAD(NVL(E.SGESTADO,' '),2,' ')                                                                                    ||
                             SUBSTR(LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(NRTELCONTATO,'-'),'.'),'('),')'),' '),' '),12,' '),1,4)    ||
                             SUBSTR(LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(NRTELCONTATO,'-'),'.'),'('),')'),' '),' '),12,' '),5,8)    ||
                             LPAD(NVL(NRRAMALCONTA,' '),5,' ')                                                                                  ||
                             SUBSTR(LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(NRFAXCONTATO,'-'),'.'),'('),')'),' '),' '),12,' '),1,4)    ||
                             SUBSTR(LPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(NRFAXCONTATO,'-'),'.'),'('),')'),' '),' '),12,' '),5,8)    ||
                             RPAD(NVL(DSENDINTERN,' '),115,' ')                                                                                 ||
                             '0000000000'                                                                                                       ) AS REG_R03
                        FROM EMPRESA E, CONTADOR C
                       WHERE CDEMPRESA = P_CDEMPRESA
                         AND E.CDCONTADOR2 = C.CDCONTADOR2(+)) LOOP

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

      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         ('03'||LPAD(RTRIM(V_CONTREG03), 8, '0'), C_REG_R03.REG_R03, P_NRSEQARQMAG);

   END LOOP;

   RETURN(V_CONTREG03);

END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_DACON_R03','5.70.000'); END;
/
