From a882c3e9475a4120b93583fea755daaffa542798 Mon Sep 17 00:00:00 2001 From: "gleison.cruz" Date: Fri, 30 Aug 2024 10:20:08 -0300 Subject: [PATCH] =?UTF-8?q?fixes=20bug#AL-4854=20Corre=C3=A7=C3=A3o=20do?= =?UTF-8?q?=20Digito=20verficador=20itau?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../dao/FechamentoParamgeralDAO.java | 1 + .../FechamentoParamgeralHibernateDAO.java | 12 + .../service/FechamentoParamgeralService.java | 2 +- .../impl/FechamentoParamgeralServiceImpl.java | 10 + .../ImportacaoRetornoBancarioServiceImpl.java | 232 +++++++++++------- 6 files changed, 166 insertions(+), 93 deletions(-) diff --git a/pom.xml b/pom.xml index 6eb5324c6..151bffaf2 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 br.com.rjconsultores ModelWeb - 1.102.0 + 1.102.1 diff --git a/src/com/rjconsultores/ventaboletos/dao/FechamentoParamgeralDAO.java b/src/com/rjconsultores/ventaboletos/dao/FechamentoParamgeralDAO.java index e4fa0e2cc..637905ea6 100644 --- a/src/com/rjconsultores/ventaboletos/dao/FechamentoParamgeralDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/FechamentoParamgeralDAO.java @@ -8,5 +8,6 @@ public interface FechamentoParamgeralDAO extends GenericDAO buscaParametrosPorEmpresas(List empresasId); public List buscaParametrosPorEmpresa(Integer empresasId); + public FechamentoParamgeral buscaParametrosPorEmpresasBanco(Integer empresaId, String boletoBancoCod); } diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/FechamentoParamgeralHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/FechamentoParamgeralHibernateDAO.java index c1c234501..5ac6816d6 100644 --- a/src/com/rjconsultores/ventaboletos/dao/hibernate/FechamentoParamgeralHibernateDAO.java +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/FechamentoParamgeralHibernateDAO.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Repository; import com.rjconsultores.ventaboletos.dao.FechamentoParamgeralDAO; import com.rjconsultores.ventaboletos.entidad.FechamentoParamgeral; +import com.rjconsultores.ventaboletos.entidad.FechamentoParamptovta; import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; @Repository("fechamentoParamgeralDAO") @@ -60,4 +61,15 @@ public class FechamentoParamgeralHibernateDAO extends GenericHibernateDAO buscaParametrosPorEmpresas(List empresasId); public List buscaParametrosPorEmpresa(Integer empresasId); public FechamentoParamgeral suscribirOrActualizacion(FechamentoParamgeral fechamentoParamgeral); - + public FechamentoParamgeral buscaParametrosPorEmpresasBanco(Integer empresasId, String boletoBancoCod); } diff --git a/src/com/rjconsultores/ventaboletos/service/impl/FechamentoParamgeralServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/FechamentoParamgeralServiceImpl.java index 6a69097be..28df9c276 100644 --- a/src/com/rjconsultores/ventaboletos/service/impl/FechamentoParamgeralServiceImpl.java +++ b/src/com/rjconsultores/ventaboletos/service/impl/FechamentoParamgeralServiceImpl.java @@ -69,4 +69,14 @@ public class FechamentoParamgeralServiceImpl implements FechamentoParamgeralServ } return null; } + + @Override + public FechamentoParamgeral buscaParametrosPorEmpresasBanco(Integer empresaId, String boletoBancoCod) { + return fechamentoParamgeralDAO.buscaParametrosPorEmpresasBanco(empresaId, boletoBancoCod); + } + + + + + } diff --git a/src/com/rjconsultores/ventaboletos/service/impl/ImportacaoRetornoBancarioServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/ImportacaoRetornoBancarioServiceImpl.java index c742100fc..022ea8407 100644 --- a/src/com/rjconsultores/ventaboletos/service/impl/ImportacaoRetornoBancarioServiceImpl.java +++ b/src/com/rjconsultores/ventaboletos/service/impl/ImportacaoRetornoBancarioServiceImpl.java @@ -5,6 +5,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigDecimal; +import java.math.BigInteger; import java.sql.SQLException; import org.apache.logging.log4j.LogManager; @@ -20,62 +21,71 @@ import com.rjconsultores.ventaboletos.blocos.safra.DetalheRetornoSafra; import com.rjconsultores.ventaboletos.dao.RemessaCNABBancosDAO; import com.rjconsultores.ventaboletos.entidad.Empresa; import com.rjconsultores.ventaboletos.entidad.FechamentoBoleto; +import com.rjconsultores.ventaboletos.entidad.FechamentoParamgeral; import com.rjconsultores.ventaboletos.enuns.BancoLayout; +import com.rjconsultores.ventaboletos.service.FechamentoParamgeralService; import com.rjconsultores.ventaboletos.service.ImportacaoRetornoBancarioService; import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; +import com.rjconsultores.ventaboletos.utils.NossoNumeroUtils; @Service("importacaoRetornoBancarioService") public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBancarioService { - - private static final String LIQUIDACAO_NORMAL = "06"; - private static final String LIQUIDACAO_EM_CARTORIO = "08"; - + + private static final String LIQUIDACAO_NORMAL = "06"; + private static final String LIQUIDACAO_EM_CARTORIO = "08"; + @Autowired private RemessaCNABBancosDAO remessaCNABBancosDAO; + @Autowired + private FechamentoParamgeralService fechamentoParamgeralService; + + private static final Logger log = LogManager.getLogger(ImportacaoRetornoBancarioServiceImpl.class); @Override - public String lerArquivo(ByteArrayInputStream bais, Empresa empresa){ - + public String lerArquivo(ByteArrayInputStream bais, Empresa empresa) { + StringBuilder resultado = new StringBuilder(); - StringBuilder detalhado = new StringBuilder(); + StringBuilder detalhado = new StringBuilder(); Integer atualizados = 0; Integer erros = 0; try { - BancoLayout banco = BancoLayout.getInstanceByCodBanco(remessaCNABBancosDAO.findBanco(empresa)); + BancoLayout banco = BancoLayout.getInstanceByCodBanco(remessaCNABBancosDAO.findBanco(empresa)); ArquivoRetornoItem retornoBancario = processaRetornoBancario(bais, banco); - - if( retornoBancario == null ) { + + if (retornoBancario == null) { resultado.append("O banco cadastrado para a empresa não tem Implementação de retorno bancário"); return resultado.toString(); } - - if(!banco.getCodBanco().equals(retornoBancario.getCabecalhoRetorno().getCodigoBanco())) { + + if (!banco.getCodBanco().equals(retornoBancario.getCabecalhoRetorno().getCodigoBanco())) { resultado.append("O banco cadastrado para a empresa não é o mesmo do arquivo selecionado"); return resultado.toString(); } - + for (DetalheRetorno detalhe : retornoBancario.getTitulos()) { Integer usuarioId = UsuarioLogado.getUsuarioLogado().getUsuarioId(); - + try { - if( !detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_NORMAL) - && !detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_EM_CARTORIO) ) { + if (!detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_NORMAL) + && !detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_EM_CARTORIO)) { erros++; - detalhado.append("Registro sem ocorrencia de quitação: ").append(detalhe.getNossoNumero()).append(".\n"); - }else - if( salvarRetornoBancario( detalhe, empresa.getEmpresaId(), usuarioId )) { + detalhado.append("Registro sem ocorrencia de quitação: ").append(detalhe.getNossoNumero()) + .append(".\n"); + } else atualizados++; + if (salvarRetornoBancario(detalhe, empresa.getEmpresaId(), usuarioId)) { detalhado.append("Quitado:").append(detalhe.getNossoNumero()).append(".\n"); - }else { + } else { erros++; - detalhado.append("Nao Quitado: ").append(detalhe.getNossoNumero()).append(".\n"); + detalhado.append("Nao Quitado: ").append(detalhe.getNossoNumero()).append(".\n"); } } catch (SQLException se) { - detalhado.append("Ocorreu um erro no banco de dados: ").append(detalhe.getNossoNumero()).append(".\n"); + detalhado.append("Ocorreu um erro no banco de dados: ").append(detalhe.getNossoNumero()) + .append(".\n"); log.error(se); erros++; } catch (RuntimeException re) { @@ -84,11 +94,11 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa erros++; } } - + resultado.append("Arquivo Processado "); - if(erros > 0) { + if (erros > 0) { resultado.append("com ressalvas.\n"); - }else { + } else { resultado.append("com sucesso. \n"); } resultado.append("Quitados ").append(atualizados).append(".\n"); @@ -101,139 +111,179 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa log.error(ioe); return resultado.toString(); } catch (RuntimeException re) { - resultado.append( re.getMessage() ); + resultado.append(re.getMessage()); log.error(re); - return resultado.toString(); + return resultado.toString(); } catch (Exception e) { resultado.append("Ocorreu um erro ao processar o arquivo enviado"); log.error(e); - return resultado.toString(); + return resultado.toString(); } } - - private ArquivoRetornoItem processaRetornoBancario(ByteArrayInputStream bais, BancoLayout banco) throws IOException{ - - if(BancoLayout.ITAU_400_Envio.equals(banco)){ + + private ArquivoRetornoItem processaRetornoBancario(ByteArrayInputStream bais, BancoLayout banco) + throws IOException { + + if (BancoLayout.ITAU_400_Envio.equals(banco)) { return geraRetornoBancarioItau(bais, banco); - }else if(BancoLayout.SAFRA_400_Envio.equals(banco)){ + } else if (BancoLayout.SAFRA_400_Envio.equals(banco)) { return geraRetornoBancarioSafra(bais); - }else { + } else { return null; } } - private ArquivoRetornoItem geraRetornoBancarioItau(ByteArrayInputStream bais, BancoLayout banco) throws IOException { + private ArquivoRetornoItem geraRetornoBancarioItau(ByteArrayInputStream bais, BancoLayout banco) + throws IOException { String linha = null; ArquivoRetornoItem arquivo = new ArquivoRetornoItem(); CabecalhoRetorno cabecalho = new CabecalhoRetorno(); - + BufferedReader leitor = new BufferedReader(new InputStreamReader(bais)); while ((linha = leitor.readLine()) != null) { - - if( linha.startsWith("0")) { //cabecalho + + if (linha.startsWith("0")) { // cabecalho cabecalho.setCodigoBanco(linha.substring(76, 79)); continue; } - - if(linha.startsWith("1")) { //detalhe + + if (linha.startsWith("1")) { // detalhe DetalheRetornoItau detalhe = new DetalheRetornoItau(); detalhe.setCodigoOcorrencia(linha.substring(108, 110)); - detalhe.setValorJuros( new BigDecimal(linha.substring(266, 279)).divide(new BigDecimal(100)) ); - detalhe.setNossoNumero(linha.substring(85, 93)+"-"+linha.substring(93, 94)); + detalhe.setValorJuros(new BigDecimal(linha.substring(266, 279)).divide(new BigDecimal(100))); + detalhe.setNossoNumero(linha.substring(85, 93) + "-" + linha.substring(93, 94)); detalhe.setDataBaixa(linha.substring(110, 116)); arquivo.addTitulo(detalhe); continue; } - - if(linha.startsWith("9")) { //rodape - - } + + if (linha.startsWith("9")) { // rodape + + } } leitor.close(); - + arquivo.setCabecalhoRetorno(cabecalho); - + return arquivo; } - - private ArquivoRetornoItem geraRetornoBancarioSafra(ByteArrayInputStream bais) throws IOException { + + private ArquivoRetornoItem geraRetornoBancarioSafra(ByteArrayInputStream bais) throws IOException { String linha = null; ArquivoRetornoItem arquivo = new ArquivoRetornoItem(); CabecalhoRetorno cabecalho = new CabecalhoRetorno(); - + BufferedReader leitor = new BufferedReader(new InputStreamReader(bais)); while ((linha = leitor.readLine()) != null) { - - if( linha.startsWith("0")) { //cabecalho + + if (linha.startsWith("0")) { // cabecalho cabecalho.setCodigoBanco(linha.substring(76, 79)); continue; } - - if(linha.startsWith("1")) { //detalhe + + if (linha.startsWith("1")) { // detalhe DetalheRetornoSafra detalhe = new DetalheRetornoSafra(); detalhe.setIdBoletoFechamento(Integer.valueOf(linha.substring(116, 126))); detalhe.setCodigoOcorrencia(linha.substring(108, 110)); - detalhe.setValorJuros( new BigDecimal(linha.substring(267, 279)).divide(new BigDecimal(100)) ); - detalhe.setNossoNumero(linha.substring(61, 70)+"-"+linha.substring(70, 71)); + detalhe.setValorJuros(new BigDecimal(linha.substring(267, 279)).divide(new BigDecimal(100))); + detalhe.setNossoNumero(linha.substring(61, 70) + "-" + linha.substring(70, 71)); detalhe.setDataBaixa(linha.substring(295, 301)); arquivo.addTitulo(detalhe); continue; } - - if(linha.startsWith("9")) { //rodape - - } + + if (linha.startsWith("9")) { // rodape + + } } leitor.close(); - + arquivo.setCabecalhoRetorno(cabecalho); - + return arquivo; } @Override - public boolean salvarRetornoBancario(DetalheRetorno detalhe, Integer empresaId, Integer usuarioId) throws SQLException { - - if(detalhe instanceof DetalheRetornoSafra ) { + public boolean salvarRetornoBancario(DetalheRetorno detalhe, Integer empresaId, Integer usuarioId) + throws SQLException { + + if (detalhe instanceof DetalheRetornoSafra) { return salvarRetornoBancario(detalhe, usuarioId); } - - FechamentoBoleto boleto = remessaCNABBancosDAO.obtenerFechamentoBoletoPorNossoNumero( detalhe.getNossoNumero(), empresaId ); - - if(boleto == null) { - throw new RuntimeException("Fechamento não encontrado para a empresa com o nosso numero: "+detalhe.getNossoNumero()); + + FechamentoBoleto boleto = remessaCNABBancosDAO.obtenerFechamentoBoletoPorNossoNumero(detalhe.getNossoNumero(), + empresaId); + + if (boleto == null) { + if (detalhe instanceof DetalheRetornoItau) { + return validaDacItau(detalhe, empresaId, usuarioId); + } else { + throw new RuntimeException( + "Fechamento não encontrado para a empresa com o nosso numero: " + detalhe.getNossoNumero()); + } } - - if( boleto.getIndBoletoQuitado() !=null && boleto.getIndBoletoQuitado()) { - throw new RuntimeException("Boleto já quitado para a empresa com o nosso numero: "+detalhe.getNossoNumero()); + + if (boleto.getIndBoletoQuitado() != null && boleto.getIndBoletoQuitado()) { + throw new RuntimeException( + "Boleto já quitado para a empresa com o nosso numero: " + detalhe.getNossoNumero()); } - - return remessaCNABBancosDAO.quitarFechamentoBoleto( boleto.getFechamentoboletoId(), usuarioId, detalhe ); + + return remessaCNABBancosDAO.quitarFechamentoBoleto(boleto.getFechamentoboletoId(), usuarioId, detalhe); } - + @Override - public boolean salvarRetornoBancario(DetalheRetorno detalhe, Integer usuarioId) throws SQLException { - FechamentoBoleto boleto = remessaCNABBancosDAO.obtenerFechamentoBoletoPorId( detalhe.getIdBoletoFechamento() ); - - if(boleto == null) { - throw new RuntimeException("Fechamento não encontrado com o ID: "+detalhe.getIdBoletoFechamento()); + public boolean salvarRetornoBancario(DetalheRetorno detalhe, Integer usuarioId) throws SQLException { + FechamentoBoleto boleto = remessaCNABBancosDAO.obtenerFechamentoBoletoPorId(detalhe.getIdBoletoFechamento()); + + if (boleto == null) { + throw new RuntimeException("Fechamento não encontrado com o ID: " + detalhe.getIdBoletoFechamento()); } - - if(boleto.getNossonumero() !=null && !boleto.getNossonumero().equals(detalhe.getNossoNumero())) { - throw new RuntimeException("Fechamento encontrado o nosso numero inconsistente: "+detalhe.getNossoNumero()); + + if (boleto.getNossonumero() != null && !boleto.getNossonumero().equals(detalhe.getNossoNumero())) { + throw new RuntimeException( + "Fechamento encontrado o nosso numero inconsistente: " + detalhe.getNossoNumero()); } - - if( boleto.getIndBoletoQuitado() !=null && boleto.getIndBoletoQuitado()) { - throw new RuntimeException("Boleto já quitado para a empresa com o nosso numero: "+detalhe.getNossoNumero()); + + if (boleto.getIndBoletoQuitado() != null && boleto.getIndBoletoQuitado()) { + throw new RuntimeException( + "Boleto já quitado para a empresa com o nosso numero: " + detalhe.getNossoNumero()); } - - return remessaCNABBancosDAO.quitarFechamentoBoleto( detalhe, usuarioId ); + + return remessaCNABBancosDAO.quitarFechamentoBoleto(detalhe, usuarioId); } - + public boolean validaDacItau(DetalheRetorno detalhe, Integer empresaId, Integer usuarioId) throws SQLException { + + FechamentoParamgeral dadosContaItau = fechamentoParamgeralService.buscaParametrosPorEmpresasBanco(empresaId, "341"); + BigInteger nossoNumero = new BigInteger(detalhe.getNossoNumero().substring(0, 7)); + if (dadosContaItau != null) { + + String dacItau = NossoNumeroUtils.dacItau( + Integer.valueOf(dadosContaItau.getBoletoBancoAgencia()), + Integer.valueOf(dadosContaItau.getBoletoBancoConta()), + Integer.valueOf(dadosContaItau.getBoletoBancoCarteira()), + nossoNumero); + + detalhe.setNossoNumero(detalhe.getNossoNumero().substring(0, 7) + "-" + dacItau); + + FechamentoBoleto boleto = remessaCNABBancosDAO.obtenerFechamentoBoletoPorNossoNumero(detalhe.getNossoNumero(), + empresaId); + + if (boleto == null) { + throw new RuntimeException("Fechamento não encontrado para a empresa com o nosso numero: " + detalhe.getNossoNumero()); + } + + if (boleto.getIndBoletoQuitado() != null && boleto.getIndBoletoQuitado()) { + throw new RuntimeException( + "Boleto já quitado para a empresa com o nosso numero: " + detalhe.getNossoNumero()); + } + return remessaCNABBancosDAO.quitarFechamentoBoleto(boleto.getFechamentoboletoId(), usuarioId, detalhe); + }else + throw new RuntimeException("Dados da conta não encontrados : " + detalhe.getIdBoletoFechamento()); + } }