CREATE OR REPLACE FUNCTION GIAM_TO_SEGMENTO_M(
   P_DTINICIO      IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFIM         IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_CDEMPRESA     IN MAPAAPURAC.CDEMPRESA%TYPE,
   P_CDINSCESTA    IN MAPAAPURAC.CDINSCESTA%TYPE,
   P_NRSEQARQMAG   IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_LIVRO         IN LIVROMAGNETICO.CDLIVROMAG%TYPE,
   P_IDRETIFIC     IN NOTAFISCAL.CDTIPOOPER%TYPE,
   P_DTINIMUNIATU  IN NOTAFISCAL.DTENTRSAID%TYPE) RETURN NUMBER
AS
   V_CONTREG NUMBER;
BEGIN
   FOR C_REG_M IN (SELECT 'M'                            ||
                          LPAD(CDINSCESTA,11,'0')        ||
                          TO_CHAR(DTFIMPERIODO,'MMYYYY') ||
                          P_IDRETIFIC                    ||
                          MUNICIPIO                      ||
                          DOMICILIOFISC                  ||
                          REPLACE(LTRIM(TO_CHAR(NVL(VRSAIDAS,0),  '000000000000.00')),'.') ||
                          REPLACE(LTRIM(TO_CHAR(NVL(VRENTRADAS,0),'000000000000.00')),'.') AS SEG_M
                     FROM (SELECT M.CDINSCESTA, M.DTFIMPERIODO,
                                  LPAD(NVL(M.CDMUNICIBGE,E.CDMUNICIPIO),14,'0') AS MUNICIPIO,
                                  SUM(ROUND(DECODE(L.CDLAYOUTLMAG,'001',I.VRCONTABILNF,0),2)) AS VRSAIDAS,
                                  SUM(ROUND(DECODE(L.CDLAYOUTLMAG,'002',I.VRCONTABILNF,0),2)) AS VRENTRADAS,
                                  CASE WHEN N.DTENTRSAID < P_DTINIMUNIATU THEN 'B' ELSE 'A' END AS DOMICILIOFISC
                             FROM MAPAAPURAC M, IMPOSTONF I, NOTAFISCAL N, ENDEFILI E, MUNICIPIO M, CFOPLIVROMAG L
                            WHERE M.CDEMPRESA    = P_CDEMPRESA
                              AND M.CDINSCESTA   = P_CDINSCESTA
                              AND M.DTINIPERIODO = P_DTINICIO
                              AND M.DTFIMPERIODO = P_DTFIM
                              AND M.CDIMPOSTO    = '00'
                              AND M.NRSEQUMAPAPU = I.NRSEQUMAPAPU
                              AND I.CDFILIAL     = N.CDFILIAL
                              AND I.NRLANCTONF   = N.NRLANCTONF
                              AND DECODE(N.IDENTRSAIDOP,'E',N.CDFILIRECE,N.CDFILIEMIT) = E.CDFILIAL
                              AND E.IDTPENDEFILI = 'P'
                              AND E.SGESTADO     = M.SGESTADO
                              AND E.CDPAIS       = M.CDPAIS
                              AND E.CDMUNICIPIO  = M.CDMUNICIPIO
                              AND I.CDFISCOPERNF = L.CDCFOPLMAG
                              AND L.CDLIVROMAG   = P_LIVRO
                            GROUP BY M.CDINSCESTA, M.DTFIMPERIODO, NVL(M.CDMUNICIBGE,E.CDMUNICIPIO),
                                  CASE WHEN N.DTENTRSAID < P_DTINIMUNIATU THEN 'B' ELSE 'A' END)) LOOP

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

      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         ('13'||LPAD(RTRIM(V_CONTREG), 8, '0'), C_REG_M.SEG_M, P_NRSEQARQMAG);

   END LOOP;
   RETURN(NVL(V_CONTREG,0));
END;