public RetornoCnab240 ProcessarRetorno()
        {
            /* Validações */
            #region Validações
            ValidaArquivoRetorno();
            #endregion

            var objRetornar = new RetornoCnab240();

            LoteRetornoCnab240    ultimoLoteIdentificado     = null;
            DetalheRetornoCnab240 ultimoRegistroIdentificado = null;

            foreach (var linhaAtual in _linhasArquivo)
            {
                /* Header de arquivo */
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "0")
                {
                    objRetornar.Header = ObterHeader(linhaAtual);
                }

                /* Header de Lote */
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "1")
                {
                    ultimoLoteIdentificado     = new LoteRetornoCnab240();
                    ultimoRegistroIdentificado = new DetalheRetornoCnab240();
                    objRetornar.Lotes.Add(ultimoLoteIdentificado);

                    ultimoLoteIdentificado.HeaderLote = ObterHeaderLote(linhaAtual);
                }
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "3")
                {
                    if (linhaAtual.ExtrairValorDaLinha(14, 14) == "T")
                    {
                        if (ultimoLoteIdentificado == null)
                        {
                            throw new Exception("Não foi encontrado header de lote para o segmento atual.");
                        }

                        //ultimoLoteIdentificado.RegistrosDetalheSegmentoT.Add(objDetalhe);
                        ultimoRegistroIdentificado.SegmentoT = ObterRegistrosDetalheT(linhaAtual);
                    }
                    if (linhaAtual.ExtrairValorDaLinha(14, 14) == "U")
                    {
                        if (ultimoLoteIdentificado == null)
                        {
                            throw new Exception("Não foi encontrado header de lote para o segmento atual.");
                        }

                        //ultimoLoteIdentificado.RegistrosDetalheSegmentoU.Add(objDetalhe);
                        ultimoRegistroIdentificado.SegmentoU = ObterRegistrosDetalheU(linhaAtual);
                    }
                    if (linhaAtual.ExtrairValorDaLinha(14, 14) == "E")
                    {
                        if (ultimoLoteIdentificado == null)
                        {
                            throw new Exception("Não foi encontrado header de lote para o segmento atual.");
                        }

                        //ultimoLoteIdentificado.RegistrosDetalheSegmentoU.Add(objDetalhe);
                        ultimoRegistroIdentificado.SegmentoE = ObterRegistrosDetalheE(linhaAtual);
                        DetalheSegmentoERetornoCnab240 detalheSegmentoE = new DetalheSegmentoERetornoCnab240();
                        detalheSegmentoE = ultimoRegistroIdentificado.SegmentoE;
                        DetalheRetornoCnab240 registro = new DetalheRetornoCnab240();
                        registro.SegmentoE = detalheSegmentoE;
                        ultimoLoteIdentificado.RegistrosDetalheSegmentos.Add(registro);
                    }
                }
                /* Trailer de Lote */
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "5")
                {
                    if (ultimoLoteIdentificado != null)
                    {
                        ultimoLoteIdentificado.TrailerLote = ObterTrailerLote(linhaAtual);
                    }
                }

                /* Trailer de arquivo */
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "9")
                {
                    objRetornar.Trailer = ObterTrailer(linhaAtual);
                }
            }

            return(objRetornar);
        }
Example #2
0
        public RetornoCnab240 ProcessarRetorno()
        {
            /* Validações */
            #region Validações
            ValidaArquivoRetorno();
            #endregion

            var objRetornar = new RetornoCnab240();

            LoteRetornoCnab240    ultimoLoteIdentificado            = null;
            DetalheRetornoCnab240 ultimoRegistroDetalheIdentificado = null;

            foreach (var linhaAtual in _linhasArquivo)
            {
                /* Header de arquivo */
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "0")
                {
                    objRetornar.Header = ObterHeader(linhaAtual);
                }

                /* Header de Lote */
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "1")
                {
                    ultimoLoteIdentificado = new LoteRetornoCnab240();

                    objRetornar.Lotes.Add(ultimoLoteIdentificado);

                    ultimoLoteIdentificado.HeaderLote = ObterHeaderLote(linhaAtual);
                }
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "3")
                {
                    if (linhaAtual.ExtrairValorDaLinha(14, 14) == "T")
                    {
                        if (ultimoLoteIdentificado == null)
                        {
                            throw new Exception("Não foi encontrado header de lote para o segmento atual.");
                        }

                        ultimoRegistroDetalheIdentificado = new DetalheRetornoCnab240();
                        ultimoLoteIdentificado.RegistrosDetalheSegmentos.Add(ultimoRegistroDetalheIdentificado);

                        var objSegmentoT = ObterRegistrosDetalheT(linhaAtual);
                        ultimoRegistroDetalheIdentificado.SegmentoT = objSegmentoT;
                    }
                    if (linhaAtual.ExtrairValorDaLinha(14, 14) == "U")
                    {
                        var objSegmentoU = ObterRegistrosDetalheU(linhaAtual);
                        if (ultimoLoteIdentificado == null)
                        {
                            throw new Exception("Não foi encontrado header de lote para o segmento atual.");
                        }
                        if (ultimoRegistroDetalheIdentificado == null)
                        {
                            throw new Exception("Não deveria ser nulo o último detalhe. Deveria ter sido criado no segmento T, anterior a este.");
                        }

                        ultimoRegistroDetalheIdentificado.SegmentoU = objSegmentoU;
                    }
                }
                /* Trailer de Lote */
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "5")
                {
                    if (ultimoLoteIdentificado != null)
                    {
                        ultimoLoteIdentificado.TrailerLote = ObterTrailerLote(linhaAtual);
                    }
                }

                /* Trailer de arquivo */
                if (linhaAtual.ExtrairValorDaLinha(8, 8) == "9")
                {
                    objRetornar.Trailer = ObterTrailer(linhaAtual);
                }
            }

            return(objRetornar);
        }