fixes bug#24059
dev: Celio qua: git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@110844 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
61754ad127
commit
85f1d256ca
|
@ -1,5 +1,6 @@
|
|||
package com.rjconsultores.ventaboletos.dao;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -30,4 +31,8 @@ public interface RemessaCNABBancosDAO extends GenericDAO<FechamentoBoleto, Long>
|
|||
public List<Empresa> getEmpresas();
|
||||
|
||||
public Boolean atualizaRemessa(Empresa empresa, ArquivoRemessa arquivoRemessa) throws Exception;
|
||||
|
||||
public FechamentoBoleto obtenerFechamentoBoletoPorNossoNumero(String nossoNumero, Integer empresaId);
|
||||
|
||||
public boolean quitarFechamentoBoleto(Long fechamentoboletoId, Integer usuarioId) throws SQLException;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.rjconsultores.ventaboletos.dao.hibernate;
|
|||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.text.DateFormat;
|
||||
import java.text.DecimalFormat;
|
||||
|
@ -15,6 +16,11 @@ import org.apache.commons.lang.StringUtils;
|
|||
import org.apache.log4j.Logger;
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.transform.AliasToBeanResultTransformer;
|
||||
import org.hibernate.type.BooleanType;
|
||||
import org.hibernate.type.IntegerType;
|
||||
import org.hibernate.type.LongType;
|
||||
import org.hibernate.type.StringType;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
@ -1678,4 +1684,63 @@ public class RemessaCNABBancosHibernateDAO extends GenericHibernateDAO<Fechament
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FechamentoBoleto obtenerFechamentoBoletoPorNossoNumero(String nossoNumero, Integer empresaId) {
|
||||
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 on fb.FECHAMENTOCNTCORRENTE_ID = fc.FECHAMENTOCNTCORRENTE_ID AND fc.ACTIVO = 1 ");
|
||||
sb.append(" WHERE fb.activo = 1 ");
|
||||
sb.append(" AND fb.nossonumero = :nossoNumero ");
|
||||
sb.append(" AND fc.EMPRESA_ID = :empresaId ");
|
||||
|
||||
Query query = getSession().createSQLQuery(sb.toString())
|
||||
.addScalar("fechamentoboletoId", LongType.INSTANCE)
|
||||
.addScalar("nossonumero", StringType.INSTANCE)
|
||||
.addScalar("status", StringType.INSTANCE)
|
||||
.addScalar("remessaId", IntegerType.INSTANCE)
|
||||
.addScalar("indBoletoQuitado", BooleanType.INSTANCE)
|
||||
.setResultTransformer(new AliasToBeanResultTransformer(FechamentoBoleto.class));
|
||||
|
||||
|
||||
query.setString("nossoNumero", nossoNumero);
|
||||
query.setInteger("empresaId", empresaId);
|
||||
|
||||
FechamentoBoleto retorno = (FechamentoBoleto)query.uniqueResult();
|
||||
|
||||
return retorno;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean quitarFechamentoBoleto(Long fechamentoboletoId, 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(" where FECHAMENTOBOLETO_ID = ").append(fechamentoboletoId);
|
||||
qry.append(" AND ACTIVO = 1 ");
|
||||
|
||||
|
||||
int qtd = stmt.executeUpdate(qry.toString());
|
||||
con.commit();
|
||||
stmt.close();
|
||||
|
||||
return qtd==1;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -85,6 +85,8 @@ public class FechamentoBoleto implements java.io.Serializable{
|
|||
@Column(name = "INDBOLETOQUITADO")
|
||||
private Boolean indBoletoQuitado;
|
||||
|
||||
@Column(name = "USUARIO_ID_QUITA")
|
||||
private Integer usuarioQuitacao;
|
||||
|
||||
public Long getFechamentoboletoId() {
|
||||
return fechamentoboletoId;
|
||||
|
@ -214,4 +216,10 @@ public class FechamentoBoleto implements java.io.Serializable{
|
|||
public void setIndBoletoQuitado(Boolean indBoletoQuitado) {
|
||||
this.indBoletoQuitado = indBoletoQuitado;
|
||||
}
|
||||
public Integer getUsuarioQuitacao() {
|
||||
return usuarioQuitacao;
|
||||
}
|
||||
public void setUsuarioQuitacao(Integer usuarioQuitacao) {
|
||||
this.usuarioQuitacao = usuarioQuitacao;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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