AdmMono/src/com/rjconsultores/ventaboletos/service/impl/PuntoVentaServiceImpl.java

340 lines
12 KiB
Java
Raw Blame History

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.rjconsultores.ventaboletos.service.impl;
import java.util.Calendar;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.rjconsultores.ventaboletos.dao.PuntoVentaDAO;
import com.rjconsultores.ventaboletos.dao.UsuarioUbicacionDAO;
import com.rjconsultores.ventaboletos.entidad.Constante;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.FormaPagoDet;
import com.rjconsultores.ventaboletos.entidad.Parada;
import com.rjconsultores.ventaboletos.entidad.PuntoVenta;
import com.rjconsultores.ventaboletos.entidad.Usuario;
import com.rjconsultores.ventaboletos.exception.IntegracionException;
import com.rjconsultores.ventaboletos.service.ConstanteService;
import com.rjconsultores.ventaboletos.service.PuntoVentaService;
import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ws.totvs.service.TotvsService;
import com.rjconsultores.ws.utileria.Atributos;
import com.rjconsultores.ws.utileria.Atributos.CodPaisBanco;
import com.rjconsultores.ws.utileria.Atributos.ContribuinteICMS;
import com.rjconsultores.ws.utileria.Atributos.ContribuinteSimplesNacional;
import com.rjconsultores.ws.utileria.Atributos.TipoCliente;
import com.rjconsultores.ws.utileria.Atributos.TipoEntidade;
import com.rjconsultores.ws.utileria.Atributos.TipoFornecedor;
import com.rjconsultores.ws.utileria.Atributos.TipoPessoa;
/**
*
* @author Administrador
*/
@Service("puntoVentaService")
public class PuntoVentaServiceImpl implements PuntoVentaService {
private static Logger log = LoggerFactory.getLogger(PuntoVentaServiceImpl.class);
private static final int CANAL_VENTA_PROPRIAS = 11;
private static final int CANAL_VENTA_FRANQUEADAS = 12;
private static final int CANAL_VENTA_PRESTACAO_SERVICO = 13;
private static final int CANAL_VENTA_MOTORISTA = 14;
private static final int CANAL_VENTA_COBRADOR = 15;
@Autowired
private PuntoVentaDAO puntoVentaDAO;
@Autowired
private ConstanteService constanteService;
@Autowired
private UsuarioUbicacionDAO usuarioUbicacionDAO;
// FIXME : Remover esse m<>todo de quem est<73> usando. Esse m<>todo carrega muitos dados
@Deprecated
public List<PuntoVenta> obtenerTodos() {
return puntoVentaDAO.obtenerTodos();
}
public PuntoVenta obtenerID(Integer id) {
return puntoVentaDAO.obtenerID(id);
}
@Transactional(noRollbackFor = { IntegracionException.class })
public PuntoVenta suscribir(PuntoVenta entidad) throws IntegracionException {
entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.setFecmodif(Calendar.getInstance().getTime());
entidad.setActivo(Boolean.TRUE);
entidad = puntoVentaDAO.suscribir(entidad);
integracionTotvs(entidad);
return entidad;
}
@Transactional(noRollbackFor = { IntegracionException.class })
public PuntoVenta actualizacion(PuntoVenta entidad) throws IntegracionException {
entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.setFecmodif(Calendar.getInstance().getTime());
entidad.setActivo(Boolean.TRUE);
entidad = puntoVentaDAO.actualizacion(entidad);
integracionTotvs(entidad);
return entidad;
}
@Transactional
public void borrar(PuntoVenta entidad) {
for (FormaPagoDet forma : entidad.getLsFormaPagoDet()) {
forma.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
forma.setFecmodif(Calendar.getInstance().getTime());
forma.setActivo(Boolean.FALSE);
}
if (entidad.getAgenciaId() != null) {
entidad.getAgenciaId().setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.getAgenciaId().setFecmodif(Calendar.getInstance().getTime());
entidad.getAgenciaId().setActivo(Boolean.FALSE);
}
if (entidad.getComissaoId() != null) {
entidad.getComissaoId().setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.getComissaoId().setFecmodif(Calendar.getInstance().getTime());
entidad.getComissaoId().setActivo(Boolean.FALSE);
}
if (entidad.getDiversosId() != null) {
entidad.getDiversosId().setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.getDiversosId().setFecmodif(Calendar.getInstance().getTime());
entidad.getDiversosId().setActivo(Boolean.FALSE);
}
if (entidad.getTitularId() != null) {
entidad.getTitularId().setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.getTitularId().setFecmodif(Calendar.getInstance().getTime());
entidad.getTitularId().setActivo(Boolean.FALSE);
}
entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.setFecmodif(Calendar.getInstance().getTime());
entidad.setActivo(Boolean.FALSE);
puntoVentaDAO.actualizacion(entidad);
}
public List<PuntoVenta> buscaLike(String strEstacion) {
return puntoVentaDAO.buscaLike(strEstacion);
}
public List<PuntoVenta> buscar(String nomPuntoVenta, String numPuntoVenta) {
return puntoVentaDAO.busca(nomPuntoVenta, numPuntoVenta);
}
public List<PuntoVenta> buscaPuntoVenta(String numPuntoVenta) {
return puntoVentaDAO.buscaPuntoVenta(numPuntoVenta);
}
public List<PuntoVenta> buscaPuntoVentaParada(Parada paradaId) {
return puntoVentaDAO.buscaPuntoVentaParada(paradaId);
}
public List<PuntoVenta> buscarPuntoVentaSubordinados(PuntoVenta puntoVenta) {
List<PuntoVenta> lsPuntoVentaSubordinados = puntoVentaDAO.buscarPuntoVentaSubordinados(puntoVenta);
return lsPuntoVentaSubordinados;
}
public List<PuntoVenta> buscarPuntosVentaMovimentacionBilhetes(List<Empresa> empresas) {
List<PuntoVenta> puntosVenta = puntoVentaDAO.buscarPuntosVentaMovimentacionBilhetes(empresas);
return puntosVenta;
}
public List<PuntoVenta> buscarPuntosVentaPorUsuario(Usuario usuario) {
List<PuntoVenta> puntosVenta = usuarioUbicacionDAO.buscarPuntoVentaPorUsuario(usuario);
return puntosVenta;
}
private boolean validaCanalVenta(Integer tipoptovtaId) {
boolean resp = false;
switch (tipoptovtaId) {
case CANAL_VENTA_PROPRIAS:
resp = true;
break;
case CANAL_VENTA_FRANQUEADAS:
resp = true;
break;
case CANAL_VENTA_MOTORISTA:
resp = true;
break;
case CANAL_VENTA_COBRADOR:
resp = true;
break;
default:
break;
}
return resp;
}
private void integracionTotvs(PuntoVenta puntoVenta) throws IntegracionException {
if (ApplicationProperties.getInstance().integracionTotvs()) {
boolean clienteValido = validaCanalVenta(puntoVenta.getTipoPuntoVenta().getTipoptovtaId().intValue());
boolean fornecedorValido = (puntoVenta.getTipoPuntoVenta().getTipoptovtaId() == CANAL_VENTA_PRESTACAO_SERVICO);
if (fornecedorValido)
clienteValido = true;
String endpointFornecedor = getEndpointFornecedor();
String endpointCliente = getEndpointCliente();
String xidsvp = puntoVenta.getNumPuntoVenta();
String cnpjEmpresaProtheus = Atributos.CNPJ_EMPRESA_PROTHEUS;
String nome = puntoVenta.getNombpuntoventa();
String cgc = puntoVenta.getNumDoCPuntoVenta();
String endereco = puntoVenta.getDireccioncalle() + "," + puntoVenta.getDireccionnumero() == null ? "" : puntoVenta.getDireccionnumero();
String complemen = puntoVenta.getCompl() == null ? "" : puntoVenta.getCompl();
String bairro = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getDesccolonia();
String codmun = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCiudad().getCodmunicipio().toString();
codmun = (codmun == null) ? "" : StringUtils.leftPad(codmun, 5, '0');
String est = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCiudad().getEstado().getCveestado();
String codpais = CodPaisBanco.BRASIL.getValor();
String cep = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCodpostal();
String tel = puntoVenta.getNumtelefonouno();
String ddd = null;
try {
String[] telefone = tel == null ? null : tel.split("\\)");
ddd = tel == null ? null : telefone[0].replaceAll("\\(", "");
tel = tel == null ? null : telefone[1].replaceAll("\\(", "");
} catch (Exception e) {
tel = puntoVenta.getNumtelefonouno();
}
String email = puntoVenta.getDescCorreo();
String fax = puntoVenta.getNumfax();
String inscr = puntoVenta.getNumIEPuntoVenta();
String nreduz = puntoVenta.getRazonSocial() != null ? puntoVenta.getRazonSocial() : puntoVenta.getNombpuntoventa();
String simpnac = ContribuinteSimplesNacional.NAO.getValor();
String tppessoa = TipoFornecedor.OS.name();
String tppessoaCliente = TipoCliente.F.name();
String tipoPessoa = TipoPessoa.F.name();
if (isCNPJ(cgc)) {
tipoPessoa = TipoPessoa.J.name();
}
String obs = puntoVenta.getComentarios();
obs = StringUtils.isBlank(obs) ? "sem obs." : obs;
String conta = "1110101001001"; // DEVE SER REVISADO COM A GUANABARA
String inscrm = "ISENTO"; // DEVE SER REVISADO COM A GUANABARA
String naturez = "";
String cnae = "";
String fator = "";
String agencia = puntoVenta.getAgenciaId() == null ? "" : puntoVenta.getAgenciaId().getNumagencia();
String dgverag = "";
try {
String[] agenciaComp = agencia == null ? null : agencia.split("-");
agencia = agencia == null ? null : agenciaComp[0];
dgverag = agencia == null ? null : agenciaComp[1];
} catch (Exception e) {
tel = puntoVenta.getAgenciaId().getNumagencia();
}
String banco = puntoVenta.getAgenciaId().getInstiFinanceiraId() == null ? "" : puntoVenta.getAgenciaId().getInstiFinanceiraId().getCodigo();
String ctacorr = puntoVenta.getAgenciaId() == null ? "" : puntoVenta.getAgenciaId().getNumconta();
String dgverct = puntoVenta.getAgenciaId() == null ? "" : puntoVenta.getAgenciaId().getDigito();
Exception respEx = null;
if (fornecedorValido) {
try {
TotvsService.cadastrarFornecedor(endpointFornecedor, bairro, cep, cgc, cnae, codmun, codpais, complemen, conta, ddd, email, endereco,
est, inscr, inscrm, naturez, nome, nreduz, simpnac, tel, tipoPessoa, tppessoa, cnpjEmpresaProtheus, xidsvp, agencia, banco,
dgverag, ctacorr, dgverct);
} catch (Exception e) {
log.error("", e);
respEx = e;
}
}
String contrib = ContribuinteICMS.NAO.getValor();
String entid = TipoEntidade.TRANSPORTE_PASSAGEIRO.getValor();
String pais = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCiudad().getEstado().getPais().getNombpais();
pais = "";// segundo mateus, passar em branco, segundo orienta<74><61>o do mateus da totvs
if (clienteValido) {
try {
TotvsService.cadastrarCliente(endpointCliente, bairro, cep, cgc, codmun, codpais, complemen, conta, contrib, ddd, email, cnpjEmpresaProtheus,
endereco, entid, fator, fax, inscr, inscrm, naturez, nome, nreduz, obs, pais, tppessoaCliente, tel, tipoPessoa, est, xidsvp);
} catch (Exception e) {
log.error("", e);
respEx = e;
}
if (respEx != null) {
throw new IntegracionException("integracion.totvs");
}
}
}
}
private String getEndpointFornecedor() {
Constante constante = constanteService.buscarPorNomeConstante("WS_TOTVS_FORNECEDOR_ENDPOINT");
if (constante == null) {
return "";
}
return constante.getValorconstante();
}
private String getEndpointCliente() {
Constante constante = constanteService.buscarPorNomeConstante("WS_TOTVS_CLIENTE_ENDPOINT");
if (constante == null) {
return "";
}
return constante.getValorconstante();
}
private String somenteNumeros(String s) {
if (s == null || StringUtils.isBlank(s)) {
return "";
}
s = StringUtils.trim(s);
return s.replaceAll("[^0-9]", "");
}
private boolean isCNPJ(String vlr) {
vlr = somenteNumeros(vlr);
return vlr.length() > 11;
}
}