Ejemplo n.º 1
0
        public RemessaCnab240 GerarRemessa(HeaderRemessaCnab240 header, HeaderLoteRemessaCnab240 headerLote,
                                           List <Boleto> boletos, TrailerLoteRemessaCnab240 trailerLote,
                                           TrailerRemessaCnab240 trailer)
        {
            var objReturn = new RemessaCnab240();

            objReturn.Header = header;
            objReturn.Lotes  = new List <LoteRemessaCnab240>();

            var ultimoLoteAdicionado = objReturn.AdicionarLote(headerLote, trailerLote);

            // Usado para identificar com número único e sequencial cada boleto (registro) dentro do lote.
            var contador = 1;
            var reg1     = 1;
            var reg2     = 2;

            foreach (var boletoAddRemessa in boletos)
            {
                objReturn.AdicionarBoletoAoLote(ultimoLoteAdicionado, boletoAddRemessa, contador, reg1, reg2);
                contador++;
                reg1 = reg1 + 2;
                reg2 = reg2 + 2;
            }

            objReturn.Trailer = trailer;

            return(objReturn);
        }
        public string EscreverTrailer(TrailerRemessaCnab240 infoTrailer)
        {
            if (infoTrailer.QtdRegistrosArquivo == 0)
            {
                throw new Exception("Não foi informada a quantidade de registros do arquivo.");
            }

            var trailer = new string(' ', 240);

            try
            {
                trailer = trailer.PreencherValorNaLinha(1, 3, "756");
                trailer = trailer.PreencherValorNaLinha(4, 7, "9999");
                trailer = trailer.PreencherValorNaLinha(8, 8, "9");
                trailer = trailer.PreencherValorNaLinha(9, 17, string.Empty.PadLeft(9, ' '));
                trailer = trailer.PreencherValorNaLinha(18, 23, infoTrailer.QtdLotesArquivo.ToString().PadLeft(6, '0'));
                trailer = trailer.PreencherValorNaLinha(24, 29, infoTrailer.QtdRegistrosArquivo.ToString().PadLeft(6, '0'));
                trailer = trailer.PreencherValorNaLinha(30, 35, string.Empty.PadLeft(6, '0'));
                trailer = trailer.PreencherValorNaLinha(36, 240, string.Empty.PadLeft(205, ' '));

                return(trailer);
            }
            catch (Exception e)
            {
                throw new Exception(String.Format("<BoletoBr>{0}Falha na geração do TRAILER do arquivo de REMESSA.",
                                                  Environment.NewLine), e);
            }
        }