Implementação do retorno SAFRA feat bug#AL-3377

master
fabio.fbarreto 2023-12-27 15:52:20 -03:00
parent 912c7f078e
commit d2d6573a81
5 changed files with 153 additions and 19 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.rjconsultores</groupId>
<artifactId>ModelWeb</artifactId>
<version>1.28.0</version>
<version>1.29.0</version>
<distributionManagement>
<repository>
@ -176,7 +176,7 @@
<dependency>
<groupId>br.com.rjconsultores</groupId>
<artifactId>GeneradorBoletosCNAB</artifactId>
<version>1.5.0</version>
<version>1.6.0</version>
</dependency>
<dependency>

View File

@ -39,4 +39,8 @@ public interface RemessaCNABBancosDAO extends GenericDAO<FechamentoBoleto, Long>
public boolean quitarFechamentoBoleto(Long fechamentoboletoId, Integer usuarioId, DetalheRetorno detalhe) throws SQLException;
public FechamentoBoleto obtenerFechamentoBoletoPorId(Integer idBoletoFechamento);
public boolean quitarFechamentoBoleto(DetalheRetorno detalhe, Integer usuarioId) throws SQLException;
}

View File

@ -1233,7 +1233,7 @@ public class RemessaCNABBancosHibernateDAO extends GenericHibernateDAO<Fechament
DetalheObrigatorioDadosPagadorBancoBrasil pagadorBancoBrasil = new DetalheObrigatorioDadosPagadorBancoBrasil();
pagadorBancoBrasil.setTipoInscricaoSacado(Integer.parseInt(TipoInscricaoPagador.CNPJ.getId()));
String ptoVenda = tupla[21].toString();
pagadorBancoBrasil.setNomeSacado(ptoVenda.toString());
pagadorBancoBrasil.setNomeSacado(ptoVenda);
String cep = "";
try{
cep = StringUtils.right(ZERO_ESQUERDA + tupla[24].toString(), 8);
@ -1979,10 +1979,78 @@ public class RemessaCNABBancosHibernateDAO extends GenericHibernateDAO<Fechament
return qtd==1;
}
@Override
public boolean quitarFechamentoBoleto(DetalheRetorno detalhe,Integer usuarioId) throws SQLException {
@SuppressWarnings("deprecation")
Connection con = getSession().connection();
Statement stmt = con.createStatement();
con.setAutoCommit(false);
StringBuilder qry = new StringBuilder();
qry.append(" update FECHAMENTO_BOLETO set INDBOLETOQUITADO = 1, ");
qry.append(" FECMODIF = SYSDATE, ");
qry.append(" USUARIO_ID_QUITA = ").append(usuarioId);
qry.append(" , FECBAIXA = TO_DATE('");
qry.append( detalhe.getDataBaixa());
qry.append("', 'DDMMYY')");
if( detalhe.getValorJuros() != null) {
qry.append(" , VALOR_JUROS = ").append(detalhe.getValorJuros());
}else {
qry.append(" , VALOR_JUROS = 0 ");
}
qry.append(" where FECHAMENTOBOLETO_ID = ").append(detalhe.getIdBoletoFechamento());
qry.append(" AND ACTIVO = 1 ");
int qtd = stmt.executeUpdate(qry.toString());
con.commit();
stmt.close();
con.close();
return qtd==1;
}
private void zerarDesconto(DetalheObrigatorioDadosTitulosBancoBrasil titulo) {
titulo.setCodigoDesconto(0);
titulo.setDataDesconto(null);
titulo.setDescontoConcedido(BigDecimal.ZERO);
}
@Override
public FechamentoBoleto obtenerFechamentoBoletoPorId(Integer idBoletoFechamento) {
StringBuilder sb = new StringBuilder();
sb.append(" SELECT ");
sb.append(" fb.FECHAMENTOBOLETO_ID AS fechamentoboletoId, ");
sb.append(" fb.nossonumero, ");
sb.append(" fb.FECDOCUMENTO, ");
sb.append(" fb.FECVENCIMENTO, ");
sb.append(" fb.VALORDOCUMENTO, ");
sb.append(" fb.STATUS as status, ");
sb.append(" fb.REMESSA_ID as remessaid, ");
sb.append(" fb.indboletoquitado as indBoletoQuitado ");
sb.append(" FROM ");
sb.append(" FECHAMENTO_BOLETO fb ");
sb.append(" INNER JOIN FECHAMENTO_CNTCORRENTE fc ");
sb.append(" ON fb.FECHAMENTOCNTCORRENTE_ID = fc.FECHAMENTOCNTCORRENTE_ID ");
sb.append(" AND fc.ACTIVO = 1 ");
sb.append(" WHERE fb.activo = 1 ");
sb.append(" AND fb.FECHAMENTOBOLETO_ID = :idFechamentoBoleto ");
Query query = getSession().createSQLQuery(sb.toString())
.addScalar("fechamentoboletoId", LongType.INSTANCE)
.addScalar("nossonumero", StringType.INSTANCE)
.addScalar("status", StringType.INSTANCE)
.addScalar("remessaId", LongType.INSTANCE)
.addScalar("indBoletoQuitado", BooleanType.INSTANCE)
.setResultTransformer(new AliasToBeanResultTransformer(FechamentoBoleto.class));
query.setInteger("idFechamentoBoleto", idBoletoFechamento);
FechamentoBoleto retorno = (FechamentoBoleto)query.uniqueResult();
return retorno;
}
}

View File

@ -2,6 +2,7 @@ package com.rjconsultores.ventaboletos.service;
import java.io.ByteArrayInputStream;
import java.sql.SQLException;
import com.rjconsultores.ventaboletos.blocos.DetalheRetorno;
import com.rjconsultores.ventaboletos.entidad.Empresa;
@ -9,6 +10,8 @@ import com.rjconsultores.ventaboletos.entidad.Empresa;
public interface ImportacaoRetornoBancarioService {
public String lerArquivo(ByteArrayInputStream bais, Empresa empresa);
public boolean salvarRetornoBancario(DetalheRetorno detalhe, Integer empresaId, Integer usuarioId) throws Exception;
public boolean salvarRetornoBancario(DetalheRetorno detalhe, Integer empresaId, Integer usuarioId) throws SQLException;
public boolean salvarRetornoBancario(DetalheRetorno detalhe, Integer usuarioId) throws SQLException;
}

View File

@ -8,7 +8,6 @@ import java.math.BigDecimal;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -16,6 +15,7 @@ import com.rjconsultores.ventaboletos.ArquivoRetornoItem;
import com.rjconsultores.ventaboletos.blocos.CabecalhoRetorno;
import com.rjconsultores.ventaboletos.blocos.DetalheRetorno;
import com.rjconsultores.ventaboletos.blocos.itau.DetalheRetornoItau;
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;
@ -39,14 +39,13 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
StringBuilder resultado = new StringBuilder();
StringBuilder detalhado = new StringBuilder();
Integer usuarioId = UsuarioLogado.getUsuarioLogado().getUsuarioId();
Integer atualizados = 0;
Integer erros = 0;
try {
BancoLayout banco = BancoLayout.getInstanceByCodBanco(remessaCNABBancosDAO.findBanco(empresa));
ArquivoRetornoItem retornoBancario = processaRetornoBancario(bais, empresa, banco);
ArquivoRetornoItem retornoBancario = processaRetornoBancario(bais, banco);
if( retornoBancario == null ) {
resultado.append("O banco cadastrado para a empresa não tem Implementação de retorno bancário");
@ -59,6 +58,8 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
}
for (DetalheRetorno detalhe : retornoBancario.getTitulos()) {
Integer usuarioId = UsuarioLogado.getUsuarioLogado().getUsuarioId();
try {
if( !detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_NORMAL)
&& !detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_EM_CARTORIO) ) {
@ -108,22 +109,18 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
}
}
private ArquivoRetornoItem processaRetornoBancario(ByteArrayInputStream bais, Empresa empresa, BancoLayout banco) throws IOException{
private ArquivoRetornoItem processaRetornoBancario(ByteArrayInputStream bais, BancoLayout banco) throws IOException{
if(BancoLayout.ITAU_400_Envio.equals(banco)){
return geraRetornoBancarioItau(bais, empresa, banco);
}else if(BancoLayout.BB_240_Envio.equals(banco)){
throw new RuntimeException("Retorno Bancário não implementado");
}else if(BancoLayout.BRADESCO_400_Envio.equals(banco)){
throw new RuntimeException("Retorno Bancário não implementado");
}else if(BancoLayout.SANTANDER_400_Envio.equals(banco)){
throw new RuntimeException("Retorno Bancário não implementado");
return geraRetornoBancarioItau(bais, banco);
}else if(BancoLayout.SAFRA_400_Envio.equals(banco)){
return geraRetornoBancarioSafra(bais);
}else {
throw new RuntimeException("Retorno Bancário não implementado");
return null;
}
}
private ArquivoRetornoItem geraRetornoBancarioItau(ByteArrayInputStream bais, Empresa empresa, BancoLayout banco) throws IOException {
private ArquivoRetornoItem geraRetornoBancarioItau(ByteArrayInputStream bais, BancoLayout banco) throws IOException {
String linha = null;
ArquivoRetornoItem arquivo = new ArquivoRetornoItem();
CabecalhoRetorno cabecalho = new CabecalhoRetorno();
@ -159,8 +156,50 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
return arquivo;
}
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
cabecalho.setCodigoBanco(linha.substring(76, 79));
continue;
}
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.setDataBaixa(linha.substring(295, 301));
arquivo.addTitulo(detalhe);
continue;
}
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 ) {
return salvarRetornoBancario(detalhe, usuarioId);
}
FechamentoBoleto boleto = remessaCNABBancosDAO.obtenerFechamentoBoletoPorNossoNumero( detalhe.getNossoNumero(), empresaId );
if(boleto == null) {
@ -172,7 +211,27 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
}
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());
}
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());
}
return remessaCNABBancosDAO.quitarFechamentoBoleto( detalhe, usuarioId );
}
}