RollBack para correção do build
parent
9e694510ce
commit
f8c4ae43b5
|
@ -0,0 +1,14 @@
|
||||||
|
package com.rjconsultores.ventaboletos.service;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
|
||||||
|
import com.rjconsultores.ventaboletos.blocos.DetalheRetorno;
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,162 @@
|
||||||
|
package com.rjconsultores.ventaboletos.service.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
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.dao.RemessaCNABBancosDAO;
|
||||||
|
import com.rjconsultores.ventaboletos.entidad.Empresa;
|
||||||
|
import com.rjconsultores.ventaboletos.entidad.FechamentoBoleto;
|
||||||
|
import com.rjconsultores.ventaboletos.enuns.BancoLayout;
|
||||||
|
import com.rjconsultores.ventaboletos.service.ImportacaoRetornoBancarioService;
|
||||||
|
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
|
||||||
|
|
||||||
|
@Service("importacaoRetornoBancarioService")
|
||||||
|
public class ImportacaoRetornoBancarioServiceImpl implements ImportacaoRetornoBancarioService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RemessaCNABBancosDAO remessaCNABBancosDAO;
|
||||||
|
|
||||||
|
private static Logger log = Logger.getLogger(ImportacaoRetornoBancarioServiceImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String lerArquivo(ByteArrayInputStream bais, Empresa empresa){
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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())) {
|
||||||
|
resultado.append("O banco cadastrado para a empresa não é o mesmo do arquivo selecionado");
|
||||||
|
return resultado.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (DetalheRetorno detalhe : retornoBancario.getTitulos()) {
|
||||||
|
try {
|
||||||
|
if( salvarRetornoBancario( detalhe, empresa.getEmpresaId(), usuarioId )) {
|
||||||
|
atualizados++;
|
||||||
|
detalhado.append("Quitado:").append(detalhe.getNossoNumero()).append(".\n");
|
||||||
|
}else {
|
||||||
|
erros++;
|
||||||
|
detalhado.append("Nao Quitado: ").append(detalhe.getNossoNumero()).append(".\n");
|
||||||
|
}
|
||||||
|
} catch (SQLException se) {
|
||||||
|
detalhado.append("Ocorreu um erro no banco de dados: ").append(detalhe.getNossoNumero());
|
||||||
|
log.error(se);
|
||||||
|
erros++;
|
||||||
|
} catch (RuntimeException re) {
|
||||||
|
detalhado.append(re.getMessage());
|
||||||
|
log.error(re);
|
||||||
|
erros++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resultado.append("Arquivo Processado ");
|
||||||
|
if(erros > 0) {
|
||||||
|
resultado.append("com ressalvas.\n");
|
||||||
|
}else {
|
||||||
|
resultado.append("com sucesso. \n");
|
||||||
|
}
|
||||||
|
resultado.append("Quitados ").append(atualizados).append(".\n");
|
||||||
|
resultado.append("Nao Quitados ").append(erros).append(".\n");
|
||||||
|
resultado.append(detalhado);
|
||||||
|
return resultado.toString();
|
||||||
|
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
resultado.append("Ocorreu um erro ao processar o arquivo enviado");
|
||||||
|
log.error(ioe);
|
||||||
|
return resultado.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
resultado.append("Ocorreu um erro ao processar o arquivo enviado");
|
||||||
|
log.error(e);
|
||||||
|
return resultado.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ArquivoRetornoItem processaRetornoBancario(ByteArrayInputStream bais, Empresa empresa, 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");
|
||||||
|
}else {
|
||||||
|
throw new RuntimeException("Retorno Bancário não implementado");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ArquivoRetornoItem geraRetornoBancarioItau(ByteArrayInputStream bais, Empresa empresa, 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
|
||||||
|
cabecalho.setCodigoBanco(linha.substring(76, 79));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(linha.startsWith("1")) { //detalhe
|
||||||
|
DetalheRetornoItau detalhe = new DetalheRetornoItau();
|
||||||
|
detalhe.setNossoNumero(linha.substring(62, 70));
|
||||||
|
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 {
|
||||||
|
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()+"\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if( boleto.getIndBoletoQuitado() !=null && boleto.getIndBoletoQuitado()) {
|
||||||
|
throw new RuntimeException("Boleto já quitado para a empresa com o nosso numero: "+detalhe.getNossoNumero()+"\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return remessaCNABBancosDAO.quitarFechamentoBoleto( boleto.getFechamentoboletoId(), usuarioId );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue