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

182 lines
5.5 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.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.rjconsultores.ventaboletos.dao.EstacionDAO;
import com.rjconsultores.ventaboletos.entidad.Estacion;
import com.rjconsultores.ventaboletos.entidad.EstacionImpresora;
import com.rjconsultores.ventaboletos.entidad.EstacionSitef;
import com.rjconsultores.ventaboletos.entidad.PuntoVenta;
import com.rjconsultores.ventaboletos.exception.BusinessException;
import com.rjconsultores.ventaboletos.service.AutorizaFolioService;
import com.rjconsultores.ventaboletos.service.EstacionService;
import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
/**
*
* @author Administrador
*/
@Service("estacionService")
public class EstacionServiceImpl implements EstacionService {
@Autowired
private EstacionDAO estacionDAO;
@Autowired
private AutorizaFolioService autorizaFolioService;
public static final String BEMATECH_FISCAL = "BEMATECHFISCAL";
public List<Estacion> obtenerTodos() {
return estacionDAO.obtenerTodos();
}
public Estacion obtenerID(Integer id) {
return estacionDAO.obtenerID(id);
}
@Transactional(rollbackFor = BusinessException.class)
public Estacion suscribirActualizar(Estacion estacion) throws BusinessException {
validarEstoqueUpdate(estacion);
Boolean esMacDuplicado = Boolean.FALSE;
List<Estacion> lsEstacionMac = estacionDAO.buscar(estacion.getDescmac());
if (!lsEstacionMac.isEmpty()) {
for (Estacion est : lsEstacionMac) {
if (!est.getEstacionId().equals(estacion.getEstacionId())) {
esMacDuplicado = Boolean.TRUE;
}
}
}
if (esMacDuplicado) {
throw new BusinessException("estacionServiceImpl.msg.macDuplicado");
}
boolean esCajaDuplicado = false;
List<Estacion> lsEstacion = estacionDAO.buscar(estacion.getNumcaja(), estacion.getPuntoVenta());
if (!lsEstacion.isEmpty()) {
if (estacion.getEstacionId() == null) {
esCajaDuplicado = true;
} else {
esCajaDuplicado = !lsEstacion.get(0).getEstacionId().equals(estacion.getEstacionId());
}
}
if (esCajaDuplicado) {
throw new BusinessException("estacionServiceImpl.msg.cajaDuplicado");
}
estacion.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
estacion.setFecmodif(Calendar.getInstance().getTime());
estacion.setActivo(Boolean.TRUE);
String errorNoChequeFolio = null;
for (EstacionImpresora ei : estacion.getLsEstacionImpresora()) {
if (ApplicationProperties.getInstance().generarRotinaFolios())
errorNoChequeFolio = autorizaFolioService.noChequeFolioPreimpresos(ei, false);
if (validacionImpressoraFiscal(ei))
errorNoChequeFolio = autorizaFolioService.noChequeFolioPreimpresos(ei, true);
}
if (estacion.getEstacionId() == null) {
estacion = estacionDAO.suscribir(estacion);
} else {
estacion = estacionDAO.actualizacion(estacion);
}
if (StringUtils.isNotBlank(errorNoChequeFolio)) {
throw new BusinessException(errorNoChequeFolio);
}
return estacion;
}
private boolean validacionImpressoraFiscal(final EstacionImpresora ei) {
if (ei.getNombImpresora().equals(BEMATECH_FISCAL))
return true;
else
return false;
}
private void validarEstoqueUpdate(Estacion estacion) throws BusinessException {
if (estacion.getEstacionId() == null) {
return;
}
if (!ApplicationProperties.getInstance().generarRotinaFolios()) {
Estacion estacionPuntoVentaAnterior = estacionDAO.obtenerID(estacion.getEstacionId());
if (estacionPuntoVentaAnterior.getPuntoVenta() != null)
if (!estacionPuntoVentaAnterior.getPuntoVenta().equals(estacion.getPuntoVenta())) {
if (estacionDAO.temEstoque(estacionPuntoVentaAnterior.getPuntoVenta(), estacion)) {
throw new BusinessException("estacionServiceImpl.msg.hayStock");
}
}
}
}
private void validarEstoqueBorrar(Estacion estacion) throws BusinessException {
if (!ApplicationProperties.getInstance().generarRotinaFolios()) {
if (estacionDAO.temEstoque(estacion.getPuntoVenta(), estacion)) {
throw new BusinessException("estacionServiceImpl.msg.hayStock.borrar");
}
}
}
@Transactional
public void borrar(Estacion entidad) throws BusinessException {
validarEstoqueBorrar(entidad);
entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.setFecmodif(Calendar.getInstance().getTime());
entidad.setActivo(Boolean.FALSE);
if (entidad.getEstacionSitefList() != null) {
for (EstacionSitef es : entidad.getEstacionSitefList()) {
es.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
es.setFecmodif(Calendar.getInstance().getTime());
es.setActivo(Boolean.FALSE);
}
}
estacionDAO.actualizacion(entidad);
}
public List<Estacion> buscar(String descEstacion, String descMac, Long nunCaja, PuntoVenta pv) {
return estacionDAO.buscar(descEstacion, descMac, nunCaja, pv);
}
public List<Estacion> buscar(String descMac) {
return estacionDAO.buscar(descMac);
}
@Override
public Long getDecimalMAC(String mac) {
mac = mac.trim();
mac = mac.replace("-", "");
return Long.parseLong(mac, 16);
}
@Override
public List<Estacion> buscarEstaciones(PuntoVenta puntoVenta) {
List<Estacion> estaciones = estacionDAO.buscarEstaciones(puntoVenta);
return estaciones;
}
}