AdmMono/src/com/rjconsultores/ventaboletos/service/impl/TarifaEmbarcadaServiceImpl....

177 lines
6.8 KiB
Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.rjconsultores.ventaboletos.service.impl;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.rjconsultores.ventaboletos.dao.TarifaEmbarcadaDAO;
import com.rjconsultores.ventaboletos.entidad.ClaseServicio;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.Marca;
import com.rjconsultores.ventaboletos.entidad.Moneda;
import com.rjconsultores.ventaboletos.entidad.Parada;
import com.rjconsultores.ventaboletos.entidad.Plaza;
import com.rjconsultores.ventaboletos.entidad.Ruta;
import com.rjconsultores.ventaboletos.entidad.TarifaEmbarcada;
import com.rjconsultores.ventaboletos.entidad.TarifaEmbarcadaHist;
import com.rjconsultores.ventaboletos.entidad.TarifaMinima;
import com.rjconsultores.ventaboletos.entidad.Tramo;
import com.rjconsultores.ventaboletos.entidad.Via;
import com.rjconsultores.ventaboletos.entidad.VigenciaTarifa;
import com.rjconsultores.ventaboletos.exception.BusinessException;
import com.rjconsultores.ventaboletos.service.TarifaEmbarcadaHistService;
import com.rjconsultores.ventaboletos.service.TarifaEmbarcadaService;
import com.rjconsultores.ventaboletos.service.TarifaMinimaService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
@Service("tarifaEmbarcadaService")
public class TarifaEmbarcadaServiceImpl implements TarifaEmbarcadaService {
private static final Logger log = Logger.getLogger(TarifaEmbarcadaService.class);
@Autowired
private TarifaEmbarcadaDAO tarifaEmbarcadaDAO;
@Autowired
private TarifaEmbarcadaHistService tarifaEmbarcadaHistService;
@Autowired
private TarifaMinimaService tarifaMinimaService;
public List<TarifaEmbarcada> obtenerTodos() {
return tarifaEmbarcadaDAO.obtenerTodos();
}
@Transactional(readOnly = true)
public TarifaEmbarcada obtenerID(Integer id) {
return tarifaEmbarcadaDAO.obtenerID(id);
}
@Transactional
public TarifaEmbarcada suscribir(TarifaEmbarcada entidad) {
entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.setFecmodif(Calendar.getInstance().getTime());
entidad.setActivo(Boolean.TRUE);
if (entidad.getPrecio() != null) {
entidad.setPrecio(entidad.getPrecio().setScale(2, RoundingMode.HALF_UP));
}
if (entidad.getPreciooriginal() != null) {
entidad.setPreciooriginal(entidad.getPreciooriginal().setScale(2, RoundingMode.HALF_UP));
}
if (entidad.getPrecioredabierto() != null) {
entidad.setPrecioredabierto(entidad.getPrecioredabierto().setScale(2, RoundingMode.HALF_UP));
}
cadastrarTarifaEmbarcadaHistorico(entidad);
return tarifaEmbarcadaDAO.suscribir(entidad);
}
@Transactional
public TarifaEmbarcada actualizacion(TarifaEmbarcada entidad) {
entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.setFecmodif(Calendar.getInstance().getTime());
entidad.setActivo(Boolean.TRUE);
if (entidad.getPrecio() != null) {
entidad.setPrecio(entidad.getPrecio().setScale(2, RoundingMode.HALF_UP));
}
if (entidad.getPreciooriginal() != null) {
entidad.setPreciooriginal(entidad.getPreciooriginal().setScale(2, RoundingMode.HALF_UP));
}
if (entidad.getPrecioredabierto() != null) {
entidad.setPrecioredabierto(entidad.getPrecioredabierto().setScale(2, RoundingMode.HALF_UP));
}
cadastrarTarifaEmbarcadaHistorico(entidad);
tarifaEmbarcadaDAO.updateTarifa(entidad);
return entidad;
}
@Transactional
public void borrar(TarifaEmbarcada entidad) {
entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
entidad.setFecmodif(Calendar.getInstance().getTime());
entidad.setActivo(Boolean.FALSE);
cadastrarTarifaEmbarcadaHistorico(entidad);
tarifaEmbarcadaDAO.actualizacion(entidad);
}
private void cadastrarTarifaEmbarcadaHistorico(TarifaEmbarcada entidad) {
TarifaEmbarcadaHist th = new TarifaEmbarcadaHist();
th.setActivo(entidad.getActivo());
th.setClaseServicio(entidad.getClaseServicio());
th.setFecmodif(entidad.getFecmodif());
th.setMarca(entidad.getMarca());
th.setMoneda(entidad.getMoneda());
th.setPrecio(entidad.getPrecio());
th.setPreciooriginal(entidad.getPreciooriginal());
th.setStatustarifa(entidad.getStatustarifa());
th.setUsuarioId(entidad.getUsuarioId());
th.setVigenciaTarifa(entidad.getVigenciaTarifa());
th = tarifaEmbarcadaHistService.suscribir(th);
}
@Override
public void excluirTodasTarifas(Moneda moneda, Marca marca, ClaseServicio claseServicio, VigenciaTarifa vigenciaTarifa, Parada origem, Parada destino, Ruta ruta, Via via, Boolean vende) {
tarifaEmbarcadaDAO.excluirTodasTarifas(moneda, marca, claseServicio, vigenciaTarifa, origem, destino, ruta, via, vende);
}
@Override
public Boolean podeAlterarTarifaMinima(TarifaEmbarcada tarifa, Marca marca, Parada origem,
Parada destino, ClaseServicio claseServicio, Plaza plaza, Moneda moneda) {
Boolean podeAlterarTarifaMinima = Boolean.TRUE;
List<TarifaMinima> lsTarifaMinima = tarifaMinimaService.pesquisarEspecifico(marca, origem, destino, claseServicio, moneda,null);
if (lsTarifaMinima.isEmpty()) {
lsTarifaMinima = tarifaMinimaService.pesquisarOrigemDestinoTodos(marca, origem, destino, claseServicio, moneda,null);
}
log.debug("Moneda : " + moneda.getDescmoneda());
log.debug("preço : " + tarifa.getPrecio());
for (TarifaMinima tm : lsTarifaMinima) {
if (tarifa.getPrecio().compareTo(tm.getImportetarifa()) == -1) {
podeAlterarTarifaMinima = Boolean.FALSE;
log.debug("Tarifa Minima ID: " + tm.getTarifaminimaId());
log.debug("Tarifa Minima Origen : " + tm.getOrigem().getDescparada());
log.debug("Tarifa Minima Destino: " + tm.getDestino().getDescparada());
log.debug("Tarifa Minima Marca : " + tm.getMarca().getDescmarca());
log.debug("Tarifa Minima Clase : " + tm.getClaseServicio().getDescclase());
log.debug("Tarifa Minima Moneda : " + tm.getMoneda().getDescmoneda());
log.debug("Tarifa Minima : " + tm.getImportetarifa() + " Valor Alterado: " + tarifa.getPrecio());
}
}
return podeAlterarTarifaMinima;
}
@Override
public void copiarTarifas(VigenciaTarifa vigenciaTarifaOrigem, VigenciaTarifa vigenciaTarifaDestino, Empresa empresa, boolean excluirTarifasDestino) throws BusinessException {
tarifaEmbarcadaDAO.copiarTarifas(vigenciaTarifaOrigem, vigenciaTarifaDestino, empresa, excluirTarifasDestino);
}
public TarifaEmbarcada buscar(Tramo tramo, Marca marca, ClaseServicio claseServicio,
VigenciaTarifa vigenciaTarifa, Moneda moneda, Ruta ruta) {
return tarifaEmbarcadaDAO.buscar(tramo, marca, claseServicio, vigenciaTarifa, moneda, ruta);
}
}