CREATE OR REPLACE FUNCTION ARQUIVO_55_GIM_PB(
   P_CDIMPOSTO    IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN EMPRESA.CDINSCESTA%TYPE,
   P_CDINSCESTAST IN NOTAFISCAL.CDINSCESTAST%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_SEQMAGNET    IN VARCHAR,
   P_CONTADOR     IN NUMBER) RETURN VARCHAR
AS
   V_CONTGERAL      NUMBER;
   V_CONTREG        NUMBER;
   V_CONTADOR       NUMBER;
BEGIN
   V_CONTREG  := 0;
   V_CONTADOR := P_CONTADOR;
   FOR C_REGISTRO55 IN (SELECT SUBSTR('55' ||
                                      REPLACE(REPLACE(REPLACE(I.NRINSJURIEST,'.',''),'-',''),'/','') ||
                                      RPAD(NVL(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(DECODE(SUBSTR(UPPER(M.CDINSCESTA),1,5),'ISENT','ISENTO',M.CDINSCESTA), ' ', ''),'.',''),',',''),'-',''),'/',''),'ISENTO'),14,' ')||
                                      TO_CHAR(P.DTORIGVENPAG,'yyyymmdd')||
                                      RPAD(I.SGESTADO,2,' ')||
                                      RPAD(EF.SGESTADO,2,' ')||
                                      NVL(B.CDBANCBOLP, '   ')||
                                      NVL(SUBSTR(CDAGENBOLP,2,5), '    ') ||
                                      NVL(M.NRAUTBANDOC, '                    ')||
                                      LTRIM(REPLACE(TO_CHAR(ROUND(ABS(P.VRATUAPAG),2),'00000000000.00'),'.',''))||
                                      TO_CHAR(P.DTATUAVENPAG,'yyyymmdd')||
                                      TO_CHAR(P.DTATUAVENPAG,'mmyyyy')||
                                      RPAD(NVL(SUBSTR(M.NRCONPROMER,1,22),' '),30,' '),1,126) AS DSARQUMAG

                        FROM MAPAAPURAC M, CONTAPAGAR P,  NOTAFISCAL N ,ENDEFORN EF,
                             BOLETPAGAR B, INSCRESTAD I

                        WHERE M.CDEMPRESA    = P_CDEMPRESA      AND
                              M.CDIMPOSTO    = P_CDIMPOSTO      AND
                              M.CDINSCESTA   = P_CDINSCESTA     AND
                              M.DTINIPERIODO = P_DTINICIAL      AND
                              M.DTFIMPERIODO = P_DTFINAL        AND
                              M.CDFILIAL     = P.CDFILIAL       AND
                              M.NRLANCTONF   = P.NRLANCTONF     AND
                              M.CDEMPRESA    = I.CDEMPRESA      AND
                              M.CDINSCESTA   = I.CDINSCESTA     AND
                              N.CDFILIAL     = P.CDFILIAL       AND
                              N.NRLANCTONF   = P.NRLANCTONF     AND
                              ((N.CDINSCESTAST = P_CDINSCESTAST) OR (P_CDINSCESTAST IS NULL AND N.CDINSCESTAST IS NULL)) AND
                              N.CDFORNECED   = EF.CDFORNECED(+) AND
                              P.CDFILIBPAG   = B.CDFILIAL       AND
                              P.NRLANBOLPAG  = B.NRLANBOLPAG    AND
                              NVL(EF.IDTPENDEFORN,'P') = 'P') LOOP

      V_CONTADOR := NVL(V_CONTADOR,0) + 1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('55'||LPAD(V_CONTADOR, 8, '0'), C_REGISTRO55.DSARQUMAG, P_SEQMAGNET);

      V_CONTREG := V_CONTREG + 1;
      V_CONTGERAL := NVL(V_CONTGERAL,0) + 1;
   END LOOP;
   IF V_CONTREG > 0 THEN
      RETURN(TO_CHAR(LPAD(V_CONTGERAL,10,0))||TO_CHAR(LPAD(V_CONTADOR,10,0)));
   END IF;

   IF V_CONTREG = 0 THEN
      RETURN('0');
   END IF;
END;
/