|
|
@ -5,6 +5,7 @@ import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
@ -20,9 +21,12 @@ import com.rjconsultores.ventaboletos.blocos.safra.DetalheRetornoSafra;
|
|
|
|
import com.rjconsultores.ventaboletos.dao.RemessaCNABBancosDAO;
|
|
|
|
import com.rjconsultores.ventaboletos.dao.RemessaCNABBancosDAO;
|
|
|
|
import com.rjconsultores.ventaboletos.entidad.Empresa;
|
|
|
|
import com.rjconsultores.ventaboletos.entidad.Empresa;
|
|
|
|
import com.rjconsultores.ventaboletos.entidad.FechamentoBoleto;
|
|
|
|
import com.rjconsultores.ventaboletos.entidad.FechamentoBoleto;
|
|
|
|
|
|
|
|
import com.rjconsultores.ventaboletos.entidad.FechamentoParamgeral;
|
|
|
|
import com.rjconsultores.ventaboletos.enuns.BancoLayout;
|
|
|
|
import com.rjconsultores.ventaboletos.enuns.BancoLayout;
|
|
|
|
|
|
|
|
import com.rjconsultores.ventaboletos.service.FechamentoParamgeralService;
|
|
|
|
import com.rjconsultores.ventaboletos.service.ImportacaoRetornoBancarioService;
|
|
|
|
import com.rjconsultores.ventaboletos.service.ImportacaoRetornoBancarioService;
|
|
|
|
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
|
|
|
|
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
|
|
|
|
|
|
|
|
import com.rjconsultores.ventaboletos.utils.NossoNumeroUtils;
|
|
|
|
|
|
|
|
|
|
|
|
@Service("importacaoRetornoBancarioService")
|
|
|
|
@Service("importacaoRetornoBancarioService")
|
|
|
|
public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBancarioService {
|
|
|
|
public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBancarioService {
|
|
|
@ -33,6 +37,10 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private RemessaCNABBancosDAO remessaCNABBancosDAO;
|
|
|
|
private RemessaCNABBancosDAO remessaCNABBancosDAO;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private FechamentoParamgeralService fechamentoParamgeralService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Logger log = LogManager.getLogger(ImportacaoRetornoBancarioServiceImpl.class);
|
|
|
|
private static final Logger log = LogManager.getLogger(ImportacaoRetornoBancarioServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -65,17 +73,19 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
|
|
|
|
if (!detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_NORMAL)
|
|
|
|
if (!detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_NORMAL)
|
|
|
|
&& !detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_EM_CARTORIO)) {
|
|
|
|
&& !detalhe.getCodigoOcorrencia().equals(LIQUIDACAO_EM_CARTORIO)) {
|
|
|
|
erros++;
|
|
|
|
erros++;
|
|
|
|
detalhado.append("Registro sem ocorrencia de quitação: ").append(detalhe.getNossoNumero()).append(".\n");
|
|
|
|
detalhado.append("Registro sem ocorrencia de quitação: ").append(detalhe.getNossoNumero())
|
|
|
|
|
|
|
|
.append(".\n");
|
|
|
|
} else
|
|
|
|
} else
|
|
|
|
if( salvarRetornoBancario( detalhe, empresa.getEmpresaId(), usuarioId )) {
|
|
|
|
|
|
|
|
atualizados++;
|
|
|
|
atualizados++;
|
|
|
|
|
|
|
|
if (salvarRetornoBancario(detalhe, empresa.getEmpresaId(), usuarioId)) {
|
|
|
|
detalhado.append("Quitado:").append(detalhe.getNossoNumero()).append(".\n");
|
|
|
|
detalhado.append("Quitado:").append(detalhe.getNossoNumero()).append(".\n");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
erros++;
|
|
|
|
erros++;
|
|
|
|
detalhado.append("Nao Quitado: ").append(detalhe.getNossoNumero()).append(".\n");
|
|
|
|
detalhado.append("Nao Quitado: ").append(detalhe.getNossoNumero()).append(".\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException se) {
|
|
|
|
} 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);
|
|
|
|
log.error(se);
|
|
|
|
erros++;
|
|
|
|
erros++;
|
|
|
|
} catch (RuntimeException re) {
|
|
|
|
} catch (RuntimeException re) {
|
|
|
@ -111,7 +121,8 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ArquivoRetornoItem processaRetornoBancario(ByteArrayInputStream bais, BancoLayout banco) throws IOException{
|
|
|
|
private ArquivoRetornoItem processaRetornoBancario(ByteArrayInputStream bais, BancoLayout banco)
|
|
|
|
|
|
|
|
throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
if (BancoLayout.ITAU_400_Envio.equals(banco)) {
|
|
|
|
if (BancoLayout.ITAU_400_Envio.equals(banco)) {
|
|
|
|
return geraRetornoBancarioItau(bais, banco);
|
|
|
|
return geraRetornoBancarioItau(bais, banco);
|
|
|
@ -122,7 +133,8 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ArquivoRetornoItem geraRetornoBancarioItau(ByteArrayInputStream bais, BancoLayout banco) throws IOException {
|
|
|
|
private ArquivoRetornoItem geraRetornoBancarioItau(ByteArrayInputStream bais, BancoLayout banco)
|
|
|
|
|
|
|
|
throws IOException {
|
|
|
|
String linha = null;
|
|
|
|
String linha = null;
|
|
|
|
ArquivoRetornoItem arquivo = new ArquivoRetornoItem();
|
|
|
|
ArquivoRetornoItem arquivo = new ArquivoRetornoItem();
|
|
|
|
CabecalhoRetorno cabecalho = new CabecalhoRetorno();
|
|
|
|
CabecalhoRetorno cabecalho = new CabecalhoRetorno();
|
|
|
@ -196,20 +208,28 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean salvarRetornoBancario(DetalheRetorno detalhe, Integer empresaId, Integer usuarioId) throws SQLException {
|
|
|
|
public boolean salvarRetornoBancario(DetalheRetorno detalhe, Integer empresaId, Integer usuarioId)
|
|
|
|
|
|
|
|
throws SQLException {
|
|
|
|
|
|
|
|
|
|
|
|
if (detalhe instanceof DetalheRetornoSafra) {
|
|
|
|
if (detalhe instanceof DetalheRetornoSafra) {
|
|
|
|
return salvarRetornoBancario(detalhe, usuarioId);
|
|
|
|
return salvarRetornoBancario(detalhe, usuarioId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FechamentoBoleto boleto = remessaCNABBancosDAO.obtenerFechamentoBoletoPorNossoNumero( detalhe.getNossoNumero(), empresaId );
|
|
|
|
FechamentoBoleto boleto = remessaCNABBancosDAO.obtenerFechamentoBoletoPorNossoNumero(detalhe.getNossoNumero(),
|
|
|
|
|
|
|
|
empresaId);
|
|
|
|
|
|
|
|
|
|
|
|
if (boleto == null) {
|
|
|
|
if (boleto == null) {
|
|
|
|
throw new RuntimeException("Fechamento não encontrado para a empresa com o nosso numero: "+detalhe.getNossoNumero());
|
|
|
|
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()) {
|
|
|
|
if (boleto.getIndBoletoQuitado() != null && boleto.getIndBoletoQuitado()) {
|
|
|
|
throw new RuntimeException("Boleto já quitado para a empresa com o nosso numero: "+detalhe.getNossoNumero());
|
|
|
|
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);
|
|
|
@ -224,16 +244,46 @@ public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (boleto.getNossonumero() != null && !boleto.getNossonumero().equals(detalhe.getNossoNumero())) {
|
|
|
|
if (boleto.getNossonumero() != null && !boleto.getNossonumero().equals(detalhe.getNossoNumero())) {
|
|
|
|
throw new RuntimeException("Fechamento encontrado o nosso numero inconsistente: "+detalhe.getNossoNumero());
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
|
|
|
"Fechamento encontrado o nosso numero inconsistente: " + detalhe.getNossoNumero());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (boleto.getIndBoletoQuitado() != null && boleto.getIndBoletoQuitado()) {
|
|
|
|
if (boleto.getIndBoletoQuitado() != null && boleto.getIndBoletoQuitado()) {
|
|
|
|
throw new RuntimeException("Boleto já quitado para a empresa com o nosso numero: "+detalhe.getNossoNumero());
|
|
|
|
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());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|