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

AS
V_CONTREG04        NUMBER;

BEGIN

 /*----------------------------------TIPO R04A-------------------------------------*/
   IF (PISCOFALIQDIFCONTR = '1') THEN
      FOR C_REG_R04A IN (SELECT DISTINCT (RPAD('R04A',4,' ')                                    ||
                                 LPAD(P.CDARVPROD,9,'0')                                        ||
                                 '0000000000'                                 ) AS REG_R04A
                           FROM ITEMPROIMPOS IT, ITEMPRODUTO IP,PRODUTO P,IMPOSTO IM,
                               (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                  FROM IMPOSTONF I, MAPAAPURAC M
                                 WHERE M.CDIMPOSTO     = '50'
                                   AND M.CDEMPRESA     = P_CDEMPRESA
                                   AND M.DTINIPERIODO  >= P_DTINICIO
                                   AND M.DTFIMPERIODO  <= P_DTFIM
                                   AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU) NOTA
                          WHERE IT.CDFILIAL     = NOTA.CDFILIAL
                            AND IT.NRLANCTONF   = NOTA.NRLANCTONF
                            AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                            AND IM.CDIMPOSTONFE = '50'
                            AND IT.CDFILIAL     = IP.CDFILIAL
                            AND IT.NRLANCTONF   = IP.NRLANCTONF
                            AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                            AND IP.CDPRODUTO    = P.CDPRODUTO
                            AND IT.CDSITUTRIBITNFE = '02') LOOP

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

         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('04'||LPAD(RTRIM(V_CONTREG04), 8, '0'), C_REG_R04A.REG_R04A, P_NRSEQARQMAG);

      END LOOP;
   END IF;

 /*----------------------------------TIPO R04B-------------------------------------*/
   IF (PISCOFALIQDIFSITTRIB = '1') THEN
      FOR C_REG_R04B IN (SELECT DISTINCT ('R04B'                              ||
                                 LPAD(P.CDARVPROD,9,'0')                      ||
                                 '0000000000'                                 ) AS REG_R04B
                           FROM ITEMPROIMPOS IT, ITEMPRODUTO IP,PRODUTO P,IMPOSTO IM,
                               (SELECT DISTINCT I.CDFILIAL, I.NRLANCTONF
                                  FROM IMPOSTONF I, MAPAAPURAC M
                                 WHERE M.CDIMPOSTO     = '50'
                                   AND M.CDEMPRESA     = P_CDEMPRESA
                                   AND M.DTINIPERIODO  >= P_DTINICIO
                                   AND M.DTFIMPERIODO  <= P_DTFIM
                                   AND M.NRSEQUMAPAPU  = I.NRSEQUMAPAPU) NOTA
                          WHERE IT.CDFILIAL     = NOTA.CDFILIAL
                            AND IT.NRLANCTONF   = NOTA.NRLANCTONF
                            AND IT.CDIMPOSTO    = IM.CDIMPOSTO
                            AND IM.CDIMPOSTONFE = '50'
                            AND IT.CDFILIAL     = IP.CDFILIAL
                            AND IT.NRLANCTONF   = IP.NRLANCTONF
                            AND IT.NRSEQITPROD  = IP.NRSEQITPROD
                            AND IP.CDPRODUTO    = P.CDPRODUTO
                            AND IT.CDSITUTRIBITNFE = '05') LOOP

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

         INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
            ('05'||LPAD(RTRIM(V_CONTREG04), 8, '0'), C_REG_R04B.REG_R04B, P_NRSEQARQMAG);

      END LOOP;
   END IF;



   RETURN(V_CONTREG04);

END;
/