CREATE OR REPLACE FUNCTION GIAM_TO_SEGMENTO_B(
   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_IDRETIFIC     IN NOTAFISCAL.CDTIPOOPER%TYPE,
   P_DTINIMUNIATU  IN NOTAFISCAL.DTENTRSAID%TYPE) RETURN NUMBER
AS
   V_CONTREG NUMBER;
BEGIN
   FOR C_REG_B IN (SELECT 'B'                                 ||
                          LPAD(I.CDINSCESTA,11,'0')           ||
                          TO_CHAR(I.DTFIMPERIODO,'MMYYYY')    ||
                          P_IDRETIFIC                         ||
                          I.IDENTRSAIDOP                      ||
                          RPAD(NVL(I.CDFISCOPERNF,' '),4,' ') ||
                          REPLACE(LTRIM(TO_CHAR(NVL(I.VRBASECALC,0),'000000000000.00')),'.')    ||
                          REPLACE(LTRIM(TO_CHAR(NVL(I.VRISENTO,0),'000000000000.00')),'.')      ||
                          REPLACE(LTRIM(TO_CHAR(NVL(CASE WHEN NVL(I.VRCONTABILNF,0) - NVL(I.VRBASECALC,0) - NVL(I.VRISENTO,0) - NVL(I.VRIMPST,0) > 0
                                                   THEN NVL(I.VRCONTABILNF,0) - NVL(I.VRBASECALC,0) - NVL(I.VRISENTO,0) - NVL(I.VRIMPST,0)
                                                   ELSE 0 END,0),'000000000000.00')),'.') ||
                          REPLACE(LTRIM(TO_CHAR(NVL(I.VRIMPST,0),'000000000000.00')),'.')       ||
                          REPLACE(LTRIM(TO_CHAR(NVL(I.VRCONTABILNF,0),'000000000000.00')),'.')  ||
                          REPLACE(LTRIM(TO_CHAR(NVL(I.VRIMPOSTO,0),'000000000000.00')),'.')     ||
                          I.DOMICILIOFISC AS SEG_B
                     FROM (SELECT M.CDINSCESTA, M.DTFIMPERIODO,
                                  DECODE(N.IDENTRSAIDOP,'E','0','1') AS IDENTRSAIDOP,
                                  I.CDFISCOPERNF, SUM(ROUND(I.VRBASECALCNF,2)) AS VRBASECALC,
                                  SUM(ROUND(CASE WHEN I.CDFISCALNF IN ('2','3') AND I.IDIMPOSTONF <> '2'
                                                 THEN I.VRCONTABILNF ELSE 0 END,2)) AS VRISENTO,
                                  SUM(ROUND(I.VRCONTABILNF,2)) AS VRCONTABILNF, 0 AS VRIMPST,
                                  SUM(ROUND(DECODE(I.CDFISCALNF,'1',I.VRIMPOSTONF,0),2)) AS VRIMPOSTO,
                                  CASE WHEN N.DTENTRSAID < P_DTINIMUNIATU THEN 'B' ELSE 'A' END AS DOMICILIOFISC
                             FROM MAPAAPURAC M, IMPOSTONF I, NOTAFISCAL N
                            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
                            GROUP BY M.CDINSCESTA, M.DTFIMPERIODO, N.IDENTRSAIDOP, I.CDFISCOPERNF,
                                  CASE WHEN N.DTENTRSAID < P_DTINIMUNIATU THEN 'B' ELSE 'A' END)I) LOOP

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

      INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
         ('02'||LPAD(RTRIM(V_CONTREG), 8, '0'), C_REG_B.SEG_B, P_NRSEQARQMAG);

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