Exemplo n.º 1
0
        public RetornoArquivoDTO LerArquivo(Stream arquivo)
        {
            var boletos = new List <BoletoDTO>();
            RetornoArquivoDTO retorno;

            try
            {
                using (StreamReader arquivoRetorno = new StreamReader(arquivo, System.Text.Encoding.UTF8))
                {
                    var registro    = arquivoRetorno.ReadLine();
                    var tipoArquivo = registro.Substring(142, 1);
                    if (tipoArquivo == "1") // Remessa
                    {
                        retorno        = LerHeaderRemessa(registro);
                        registro       = arquivoRetorno.ReadLine();
                        retorno.Numero = registro.Substring(185, 6);
                    }
                    else // Retorno
                    {
                        retorno = LerHeaderRetorno(registro);
                    }
                    var boletoLido = false;
                    while (!arquivoRetorno.EndOfStream)
                    {
                        registro = arquivoRetorno.ReadLine();
                        var tipoRegistro = registro.Substring(7, 1);

                        if (tipoRegistro == "3")
                        {
                            var tipoSegmento = registro.Substring(13, 1);
                            #region Retorno
                            if (tipoSegmento == "T")
                            {
                                boletoLido = false;
                                var boleto      = LerDetalheSegmentoT(registro);
                                var boletoSalvo = boletoRepository.BuscarBoletoPorNumero(boleto.SeuNumero);
                                if (boletoSalvo != null)
                                {
                                    boletos.Add(new BoletoDTO(boletoSalvo));
                                    boletoLido = true;
                                }
                            }
                            else if (tipoSegmento == "U" && boletoLido)
                            {
                                var boleto = boletos.LastOrDefault();
                                if (boleto == null)
                                {
                                    continue;
                                }
                                LerDetalheSegmentoU(ref boleto, registro);
                            }
                            #endregion
                            #region Remessa
                            else if (tipoSegmento == "P")
                            {
                                boletoLido = false;
                                var boleto = LerDetalheSegmentoP(registro);
                                if (boleto != null)
                                {
                                    boletos.Add(boleto);
                                    boletoLido = true;
                                }
                            }
                            else if (tipoSegmento == "Q" && boletoLido)
                            {
                                var boleto = boletos.LastOrDefault();
                                if (boleto == null)
                                {
                                    continue;
                                }
                                LerDetalheSegmentoQ(ref boleto, registro);
                                var aluno = alunoRepository.BuscarAlunoPorCPF(boleto.CpfAluno);
                                if (aluno != null)
                                {
                                    boleto.Aluno = new AlunoDTO(aluno);
                                }
                            }
                            else if (tipoSegmento == "R" && boletoLido)
                            {
                                var boleto = boletos.LastOrDefault();
                                if (boleto == null)
                                {
                                    continue;
                                }
                                LerDetalheSegmentoR(ref boleto, registro);
                            }
                            #endregion
                        }
                    }
                }
                retorno.Movimentacoes = boletos;
                return(retorno);
            }
            catch (Exception ex)
            {
                throw new Exception("Erro ao ler arquivo.", ex);
            }
        }