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

246 lines
9.2 KiB
Java

/*
* 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.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.fornecedor.Tipo;
import com.rjconsultores.ws.totvs.service.TotvsService;
/**
*
* @author Administrador
*/
@Service("puntoVentaService")
public class PuntoVentaServiceImpl implements PuntoVentaService {
private static Logger log = LoggerFactory.getLogger(PuntoVentaServiceImpl.class);
@Autowired
private PuntoVentaDAO puntoVentaDAO;
@Autowired
private ConstanteService constanteService;
@Autowired
private UsuarioUbicacionDAO usuarioUbicacionDAO;
// FIXME : Remover esse método de quem está 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);
}
// for (PtovtaEstoque forma : entidad.getPtovtaEstoqueList()) {
// 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 void integracionTotvs(PuntoVenta puntoVenta) throws IntegracionException {
if (ApplicationProperties.getInstance().integracionTotvs()) {
String nomeEmpresa = puntoVenta.getRazonSocial();
String nomeFilial = puntoVenta.getNumDoCPuntoVenta();
String endpointFornecedor = getEndpointFornecedor();
String endpointCliente = getEndpointCliente();
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().getCiudadId().toString();
String est = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCiudad().getEstado().getCveestado();
String codpais = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCiudad().getEstado().getPais().getPaisId().toString();
String cep = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCodpostal();
String pais = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCiudad().getEstado().getPais().getNombpais();
String tel = puntoVenta.getNumtelefonouno();
String ddd = null;
try {
String[] telefone = puntoVenta.getNumtelefonouno() == null ? null : puntoVenta.getNumtelefonouno().split("\\)");
ddd = puntoVenta.getNumtelefonouno() == null ? null : telefone[0].replaceAll("\\(", "");
tel = puntoVenta.getNumtelefonouno() == 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.getNombpuntoventa();
String tipo = Tipo.F.name();
String tpessoa = puntoVenta.getNumDoCPuntoVenta();
String obs = puntoVenta.getComentarios();
String conta = puntoVenta.getAgenciaId() == null ? null : puntoVenta.getAgenciaId().getNumconta() + "-" + puntoVenta.getAgenciaId().getDigito();
String cnae = "";
String inscrm = "";
String naturez = "";
String simpnac = "";
String contrib = "";
String entid = "";
String fator = "";
try {
TotvsService.cadastrarFornecedor(endpointFornecedor, nomeEmpresa, nomeFilial, bairro, cep,
cgc, cnae, codmun, codpais, complemen, conta, ddd, email, endereco, est, inscr,
inscrm, naturez, nome, nreduz, simpnac, tel, tipo, tpessoa);
} catch (Exception e) {
log.error("", e);
throw new IntegracionException("integracion.totvs");
}
try {
TotvsService.cadastrarCliente(endpointCliente, bairro, cep, cgc, codmun, codpais, complemen, conta,
contrib, ddd, email, nomeEmpresa, endereco, entid, fator, fax, inscr, inscrm, naturez, nome,
nreduz, obs, pais, tpessoa, tel, tipo, est);
} catch (Exception e) {
log.error("", e);
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();
}
}