fixes bug#22225

dev: Celio
qua:

git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@107840 d1611594-4594-4d17-8e1d-87c2c4800839
master
fabio 2021-07-15 14:57:44 +00:00
parent 6bdc1cbe5d
commit 715870cb89
9 changed files with 558 additions and 40 deletions

View File

@ -0,0 +1,14 @@
package com.rjconsultores.ventaboletos.dao;
import java.util.Date;
import java.util.List;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.FechamentoCntcorrente;
import com.rjconsultores.ventaboletos.vo.integracao.FechamentoCntCorrenteVO;
public interface SapDAO extends GenericDAO<FechamentoCntcorrente, Long> {
public List<FechamentoCntCorrenteVO> obtenerTodosParaRemessa(Empresa empresa, Date dataDe, Date dataAte, Boolean reenviar);
}

View File

@ -0,0 +1,103 @@
package com.rjconsultores.ventaboletos.dao.hibernate;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.rjconsultores.ventaboletos.dao.SapDAO;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.FechamentoCntcorrente;
import com.rjconsultores.ventaboletos.vo.integracao.FechamentoCntCorrenteVO;
@Repository("sapDAO")
public class SapHibernateDAO extends GenericHibernateDAO<FechamentoCntcorrente, Long> implements SapDAO {
@Autowired
public SapHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
setSessionFactory(factory);
}
@Override
public List<FechamentoCntCorrenteVO> obtenerTodosParaRemessa(Empresa empresa, Date dataDe, Date dataAte, Boolean reenviar) {
StringBuilder sb = new StringBuilder();
sb.append(" SELECT ");
sb.append(" fc.fechamentocntcorrente_id, ");
sb.append(" to_char(fc.fecfechamento, 'yyyy-mm-DD'), ");
sb.append(" fc.total, ");
sb.append(" es.cveestado, ");
sb.append(" pv.numpuntoventa, ");
sb.append(" pv.nombpuntoventa, ");
sb.append(" fc.empresa_id, ");
sb.append(" emp.nombempresa, ");
sb.append(" fc.indintegradosap, ");
sb.append(" to_char(fc.fecfechamento, 'yyyy'), ");
sb.append(" to_char(fc.fecfechamento, 'mm'), ");
sb.append(" CASE to_char(fc.fecfechamento, 'D') ");
sb.append(" WHEN '6' THEN to_char(fc.fecfechamento+3, 'yyyy-mm-DD') ");
sb.append(" WHEN '7' THEN to_char(fc.fecfechamento+2, 'yyyy-mm-DD') ");
sb.append(" ELSE to_char(fc.fecfechamento+1, 'yyyy-mm-DD') END as feclancamento ");
sb.append(" FROM ");
sb.append(" fechamento_cntcorrente fc ");
sb.append(" INNER JOIN punto_venta pv ON pv.puntoventa_id = fc.puntoventa_id AND pv.activo = 1 ");
sb.append(" INNER JOIN empresa emp ON emp.empresa_id = fc.empresa_id AND emp.activo = 1 ");
sb.append(" LEFT JOIN ciudad ci ON ci.ciudad_id = emp.ciudad_id AND ci.activo = 1 ");
sb.append(" LEFT JOIN plaza pl ON pl.plaza_id = ci.plaza_id AND pl.activo = 1 ");
sb.append(" LEFT JOIN estado es ON es.estado_id = ci.estado_id AND es.activo = 1 ");
sb.append(" WHERE ");
sb.append(" fc.activo = 1 ");
sb.append(" AND fc.fecfechamento BETWEEN :dataDe AND :dataAte ");
sb.append(" AND fc.EMPRESA_ID = :empresaId ");
if(!reenviar){
sb.append(" AND ( fc.indintegradosap IS NULL OR fc.indintegradosap = 0) ");
}
sb.append(" ORDER BY ");
sb.append(" pv.nombpuntoventa, fc.fecfechamento ");
Query query = getSession().createSQLQuery(sb.toString());
query.setInteger("empresaId", empresa.getEmpresaId());
if(dataDe != null && dataAte != null){
query.setDate("dataDe", dataDe);
query.setDate("dataAte", dataAte);
}
@SuppressWarnings("unchecked")
List<Object[]> list = query.list();
List<FechamentoCntCorrenteVO> retorno = new ArrayList<FechamentoCntCorrenteVO>();
for(Object[] tupla : list){
FechamentoCntCorrenteVO fcc = new FechamentoCntCorrenteVO();
fcc.setFechamentocntcorrenteId( Long.valueOf(tupla[0].toString()));
fcc.setFecfechamento( tupla[1].toString() );
fcc.setTotal( (BigDecimal)tupla[2] );
fcc.setUfEmpresa(tupla[3].toString());
fcc.setNumPuntoVenta(tupla[4].toString());
fcc.setNombpuntoventa(tupla[5].toString());
fcc.setEmpresaId( Integer.valueOf( tupla[6].toString() ) );
fcc.setNombEmpresa( tupla[7].toString());
fcc.setIntegradoSap( tupla[8]==null?false:tupla[8].toString().equals("1")?true:false );
fcc.setAnofechamento(tupla[9].toString());
fcc.setMesfechamento(tupla[10].toString());
fcc.setFeclancamento(tupla[11].toString());
retorno.add(fcc);
}
return retorno;
}
}

View File

@ -66,6 +66,9 @@ public class FechamentoCntcorrente {
@Column(name = "INDEMAILENVIADO")
private Boolean indemailenviado;
@Column(name = "INDINTEGRADOSAP")
private Boolean integradoSap;
public Long getFechamentocntcorrenteId() {
return fechamentocntcorrenteId;
}
@ -162,6 +165,14 @@ public class FechamentoCntcorrente {
this.indemailenviado = indemailenviado;
}
public Boolean getIntegradoSap() {
return integradoSap;
}
public void setIntegradoSap(Boolean integradoSap) {
this.integradoSap = integradoSap;
}
}

View File

@ -164,6 +164,7 @@ public class FechamentoParamgeral implements java.io.Serializable {
public Long getFechamentoparamgeralId() {
return fechamentoparamgeralId;
}
public void setFechamentoparamgeralId(Long fechamentoparamgeralId) {
this.fechamentoparamgeralId = fechamentoparamgeralId;
}
@ -171,6 +172,7 @@ public class FechamentoParamgeral implements java.io.Serializable {
public Empresa getEmpresa() {
return empresa;
}
public void setEmpresa(Empresa empresa) {
this.empresa = empresa;
}
@ -186,6 +188,7 @@ public class FechamentoParamgeral implements java.io.Serializable {
public String getBoletoBancoAgencia() {
return boletoBancoAgencia;
}
public void setBoletoBancoAgencia(String boletoBancoAgencia) {
this.boletoBancoAgencia = boletoBancoAgencia;
}

View File

@ -0,0 +1,6 @@
package com.rjconsultores.ventaboletos.enums;
public enum TipoEnvioRest {
GET,
POST;
}

View File

@ -0,0 +1,110 @@
package com.rjconsultores.ventaboletos.rest;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.rjconsultores.ventaboletos.enums.TipoEnvioRest;
import com.rjconsultores.ventaboletos.vo.integracao.FechamentoCntCorrenteVO;
public class IntegracaoSapRest {
private static Logger log = Logger.getLogger(IntegracaoSapRest.class);
private static IntegracaoSapRest instance;
public static synchronized IntegracaoSapRest getInstance() {
if(instance == null) {
instance = new IntegracaoSapRest();
}
return instance;
}
public JSONObject enviarIntegracaoSap( FechamentoCntCorrenteVO fechamento, String url, String credenciais ) {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = getRequest( url, TipoEnvioRest.POST, credenciais, fechamtoToJson(fechamento));
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK
&& response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
throw new IllegalStateException("Ocorreu um erro na requisição." + response.getStatusLine() !=null ? response.getStatusLine().getReasonPhrase() : "");
}
return getRetornoJSON(response);
} catch (IOException e) {
log.error(e);
} catch (JSONException e) {
log.error(e);
} catch (Exception e) {
log.error("Erro no envio/retorno da requisicao para integração SAP", e);
}
return null;
}
private JSONObject getRetornoJSON(HttpResponse response) throws IOException, JSONException {
HttpEntity entity = response.getEntity();
if (entity != null) {
String rest = EntityUtils.toString(entity);
JSONObject result = new JSONObject(rest);
JSONObject boleto = result.getJSONObject("MT_Boleto_RES");
return boleto.getJSONArray("ReturnERP").getJSONObject(0);
}
return null;
}
private String fechamtoToJson( FechamentoCntCorrenteVO fcc){
StringBuilder json = new StringBuilder();
json.append("{");
json.append(" \"ID_LEGADO\": \"010\",");
json.append(" \"AGENCIA\": {");
json.append(" \"KUNNR\": \"").append(fcc.getNumPuntoVenta()).append("\"");
json.append(" },");
json.append(" \"BOLETO\": [{");
json.append(" \"ZBUKRS\": \"").append(fcc.getEmpresaId()).append("\",");
json.append(" \"ZZFCAIXA\": \"").append(fcc.getFechamentocntcorrenteId()).append("\",");
json.append(" \"FILIAL\": {");
json.append(" \"UF\": \"").append(fcc.getUfEmpresa()).append("\",");
json.append(" },");
json.append(" \"GJAHR\": \"").append(fcc.getAnofechamento()).append("\",");
json.append(" \"MONAT\": \"").append(fcc.getMesfechamento()).append("\",");
json.append(" \"BLDAT\": \"").append(fcc.getFecfechamento()).append("\",");
json.append(" \"BUDAT\": \"").append(fcc.getFeclancamento()).append("\",");
json.append(" \"XBLNR\": \"").append(fcc.getNumPuntoVenta()).append("\",");
json.append(" \"WRBTR\": \"").append( fcc.getTotal().toString().replace(",", ".") ).append("\",");
json.append(" }]");
json.append("}");
return json.toString();
}
private HttpUriRequest getRequest(String uri, TipoEnvioRest tipoEnvio, String credenciais, String json) throws Exception {
HttpPost request = new HttpPost(uri);
StringEntity entity = new StringEntity(json);
request.addHeader("Authorization", getAuthorization(credenciais));
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setEntity(entity);
// request.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
return request;
}
private String getAuthorization( String credenciais ) {
return "Basic " + new String(Base64.encodeBase64(credenciais.getBytes()));
}
}

View File

@ -0,0 +1,16 @@
package com.rjconsultores.ventaboletos.service;
import java.util.Date;
import java.util.List;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.FechamentoCntcorrente;
import com.rjconsultores.ventaboletos.vo.integracao.FechamentoCntCorrenteVO;
public interface SapService extends GenericService<FechamentoCntcorrente, Long> {
public int remessa(List<FechamentoCntCorrenteVO> fechamentos) throws Exception ;
public List<FechamentoCntCorrenteVO> obtenerTodosParaRemessa(Empresa empresa, Date dataDe, Date dataAte, Boolean reenviar);
}

View File

@ -0,0 +1,109 @@
package com.rjconsultores.ventaboletos.service.impl;
import java.util.Date;
import java.util.List;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.rjconsultores.ventaboletos.dao.ConstanteDAO;
import com.rjconsultores.ventaboletos.dao.SapDAO;
import com.rjconsultores.ventaboletos.entidad.Constante;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.FechamentoCntcorrente;
import com.rjconsultores.ventaboletos.rest.IntegracaoSapRest;
import com.rjconsultores.ventaboletos.service.SapService;
import com.rjconsultores.ventaboletos.vo.integracao.FechamentoCntCorrenteVO;
@Service("sapService")
public class SapServiceImpl implements SapService{
private static final String INTEGRACAO_SAP_URL = "INTEGRACAO_SAP_URL";
private static final String INTEGRACAO_SAP_CREDENCIAL = "INTEGRACAO_SAP_CREDENCIAL";
@Autowired
private SapDAO sapDAO;
@Autowired
private ConstanteDAO constanteDAO;
@Override
public List<FechamentoCntcorrente> obtenerTodos() {
return sapDAO.obtenerTodos();
}
@Override
public FechamentoCntcorrente obtenerID(Long id) {
return sapDAO.obtenerID(id);
}
@Override
@Transactional
public FechamentoCntcorrente suscribir(FechamentoCntcorrente entidad) {
return sapDAO.suscribir(entidad);
}
@Override
@Transactional
public FechamentoCntcorrente actualizacion(FechamentoCntcorrente entidad) {
return sapDAO.actualizacion(entidad);
}
@Override
@Transactional
public void borrar(FechamentoCntcorrente entidad) {
sapDAO.borrar(entidad);
}
@Override
public List<FechamentoCntCorrenteVO> obtenerTodosParaRemessa(Empresa empresa, Date dataDe, Date dataAte, Boolean reenviar) {
return sapDAO.obtenerTodosParaRemessa(empresa, dataDe, dataAte, reenviar);
}
@Override
public int remessa(List<FechamentoCntCorrenteVO> fechamentos) throws Exception {
try {
int contador = 0;
IntegracaoSapRest integracaoSapRest = IntegracaoSapRest.getInstance();
String credenciais = buscaConstante(INTEGRACAO_SAP_CREDENCIAL);
String url = buscaConstante(INTEGRACAO_SAP_URL);
for (FechamentoCntCorrenteVO fechamentoCntcorrente : fechamentos) {
if( fechamentoCntcorrente.isEnviar()) {
JSONObject integrado = integracaoSapRest.enviarIntegracaoSap(fechamentoCntcorrente, url, credenciais);
if(integrado == null) {
throw new RuntimeException("Erro no envio/retorno da requisicao para integração SAP");
}else if(integrado.getString("Integration").equals("S")) {
FechamentoCntcorrente registro = obtenerID(fechamentoCntcorrente.getFechamentocntcorrenteId());
registro.setIntegradoSap(true);
suscribir(registro);
contador++;
}else if(integrado.getString("Integration").equals("E")) {
throw new RuntimeException(integrado.getString("Message"));
}
}
}
return contador;
}catch(RuntimeException re){
throw re;
}catch(Exception e){
throw e;
}
}
private String buscaConstante(String constanteString) {
Constante constante = constanteDAO.buscarPorNomeConstante(constanteString);
if( constante == null || constante.getValorconstante() == null || constante.getValorconstante().isEmpty() ) {
throw new RuntimeException("Constante "+constanteString+" não cadastrada");
}
return constante.getValorconstante();
}
}

View File

@ -0,0 +1,146 @@
package com.rjconsultores.ventaboletos.vo.integracao;
import java.math.BigDecimal;
public class FechamentoCntCorrenteVO {
public FechamentoCntCorrenteVO() {
super();
// TODO Auto-generated constructor stub
}
private Long fechamentocntcorrenteId;
private String fecfechamento;
private String feclancamento;
private String anofechamento;
private String mesfechamento;
private BigDecimal total;
private String nombpuntoventa;
private String numPuntoVenta;
private Integer empresaId;
private String nombEmpresa;
private String ufEmpresa;
private Boolean integradoSap;
private boolean enviar;
public FechamentoCntCorrenteVO(Long fechamentocntcorrenteId, String fecfechamento, String feclancamento, String anofechamento, String mesfechamento, BigDecimal total, String nombpuntoventa, String numPuntoVenta, Integer empresaId, String nombEmpresa, String ufEmpresa, Boolean integradoSap) {
super();
this.fechamentocntcorrenteId = fechamentocntcorrenteId;
this.fecfechamento = fecfechamento;
this.feclancamento = feclancamento;
this.anofechamento = anofechamento;
this.mesfechamento = mesfechamento;
this.total = total;
this.nombpuntoventa = nombpuntoventa;
this.numPuntoVenta = numPuntoVenta;
this.empresaId = empresaId;
this.nombEmpresa = nombEmpresa;
this.ufEmpresa = ufEmpresa;
this.integradoSap = integradoSap;
}
public Long getFechamentocntcorrenteId() {
return fechamentocntcorrenteId;
}
public void setFechamentocntcorrenteId(Long fechamentocntcorrenteId) {
this.fechamentocntcorrenteId = fechamentocntcorrenteId;
}
public String getFecfechamento() {
return fecfechamento;
}
public void setFecfechamento(String fecfechamento) {
this.fecfechamento = fecfechamento;
}
public BigDecimal getTotal() {
return total;
}
public void setTotal(BigDecimal total) {
this.total = total;
}
public String getNombpuntoventa() {
return nombpuntoventa;
}
public void setNombpuntoventa(String nombpuntoventa) {
this.nombpuntoventa = nombpuntoventa;
}
public String getNumPuntoVenta() {
return numPuntoVenta;
}
public void setNumPuntoVenta(String numPuntoVenta) {
this.numPuntoVenta = numPuntoVenta;
}
public Integer getEmpresaId() {
return empresaId;
}
public void setEmpresaId(Integer empresaId) {
this.empresaId = empresaId;
}
public String getNombEmpresa() {
return nombEmpresa;
}
public void setNombEmpresa(String nombEmpresa) {
this.nombEmpresa = nombEmpresa;
}
public String getUfEmpresa() {
return ufEmpresa;
}
public void setUfEmpresa(String ufEmpresa) {
this.ufEmpresa = ufEmpresa;
}
public boolean isIntegradoSap() {
return Boolean.TRUE.equals(integradoSap);
}
public void setIntegradoSap(boolean integradoSap) {
this.integradoSap = integradoSap;
}
public String getFeclancamento() {
return feclancamento;
}
public void setFeclancamento(String feclancamento) {
this.feclancamento = feclancamento;
}
public String getMesfechamento() {
return mesfechamento;
}
public void setMesfechamento(String mesfechamento) {
this.mesfechamento = mesfechamento;
}
public String getAnofechamento() {
return anofechamento;
}
public void setAnofechamento(String anofechamento) {
this.anofechamento = anofechamento;
}
public Boolean isEnviar() {
return enviar;
}
public void setEnviar(Boolean enviar) {
this.enviar = enviar;
}
}