/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.PricingDAO;
import com.rjconsultores.ventaboletos.entidad.Categoria;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingAsiento;
import com.rjconsultores.ventaboletos.entidad.PricingCategoria;
import com.rjconsultores.ventaboletos.entidad.PricingClase;
import com.rjconsultores.ventaboletos.entidad.PricingConexao;
import com.rjconsultores.ventaboletos.entidad.PricingCorrida;
import com.rjconsultores.ventaboletos.entidad.PricingDia;
import com.rjconsultores.ventaboletos.entidad.PricingFormapago;
import com.rjconsultores.ventaboletos.entidad.PricingImporte;
import com.rjconsultores.ventaboletos.entidad.PricingMarca;
import com.rjconsultores.ventaboletos.entidad.PricingMercado;
import com.rjconsultores.ventaboletos.entidad.PricingOcupaAntecipa;
import com.rjconsultores.ventaboletos.entidad.PricingPuntoVenta;
import com.rjconsultores.ventaboletos.entidad.PricingRuta;
import com.rjconsultores.ventaboletos.entidad.PricingTipoPtoVta;
import com.rjconsultores.ventaboletos.entidad.PricingTipoServicio;
import com.rjconsultores.ventaboletos.entidad.PricingVigencia;
import com.rjconsultores.ventaboletos.service.CategoriaService;
import com.rjconsultores.ventaboletos.service.PricingService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;

/**
 * 
 * @author Rafius
 */
@Service("pricingService")
public class PricingServiceImpl implements PricingService {
	
	private static final Logger log = LogManager.getLogger(PricingServiceImpl.class);

	@Autowired
	private PricingDAO pricingDAO;
	
	@Autowired
    private CategoriaService categoriaService;

	public List<Pricing> obtenerTodos() {
		return pricingDAO.obtenerTodos();
	}

	public Pricing obtenerID(Integer id) {
		return pricingDAO.obtenerID(id);
	}

	@Transactional
	public Pricing suscribir(Pricing entidad) {
		entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
		entidad.setFecmodif(Calendar.getInstance().getTime());
		entidad.setActivo(Pricing.ATIVO);

		return pricingDAO.suscribir(entidad);
	}

	@Transactional
	public Pricing actualizacion(Pricing entidad) {
		entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
		entidad.setFecmodif(Calendar.getInstance().getTime());
		entidad.setActivo(Pricing.ATIVO);

		return pricingDAO.actualizacion(entidad);
	}

	private void ativarInativarExcluirPricing(Pricing entidad, Integer ativarInativarExcluir) {
		entidad = obtenerID(entidad.getPricingId());

		for (PricingAsiento pricingAsiento : entidad.getPricingAsientoList()) {
			pricingAsiento.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingAsiento.setFecmodif(Calendar.getInstance().getTime());
			pricingAsiento.setActivo(ativarInativarExcluir);
		}

		for (PricingCategoria pricingCategoria : entidad.getPricingCategoriaList()) {
			pricingCategoria.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingCategoria.setFecmodif(Calendar.getInstance().getTime());
			pricingCategoria.setActivo(ativarInativarExcluir);
		}

		for (PricingClase pricingClase : entidad.getPricingClaseList()) {
			pricingClase.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingClase.setFecmodif(Calendar.getInstance().getTime());
			pricingClase.setActivo(ativarInativarExcluir);
		}

		for (PricingCorrida pricingCorrida : entidad.getPricingCorridaList()) {
			pricingCorrida.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingCorrida.setFecmodif(Calendar.getInstance().getTime());
			pricingCorrida.setActivo(ativarInativarExcluir);
		}

		for (PricingDia pricingDia : entidad.getPricingDiaList()) {
			pricingDia.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingDia.setFecmodif(Calendar.getInstance().getTime());
			pricingDia.setActivo(ativarInativarExcluir);
		}

		for (PricingImporte pricingImporte : entidad.getPricingImporteList()) {
			pricingImporte.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingImporte.setFecmodif(Calendar.getInstance().getTime());
			pricingImporte.setActivo(ativarInativarExcluir);
		}

		for (PricingMarca pricingMarca : entidad.getPricingMarcaList()) {
			pricingMarca.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingMarca.setFecmodif(Calendar.getInstance().getTime());
			pricingMarca.setActivo(ativarInativarExcluir);
		}

		for (PricingMercado pricingMercado : entidad.getPricingMercadoList()) {
			pricingMercado.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingMercado.setFecmodif(Calendar.getInstance().getTime());
			pricingMercado.setActivo(ativarInativarExcluir);
		}

		for (PricingOcupaAntecipa pricingOcupaAntecipa : entidad.getPricingOcupaAntecipaList()) {
			pricingOcupaAntecipa.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingOcupaAntecipa.setFecmodif(Calendar.getInstance().getTime());
			pricingOcupaAntecipa.setActivo(ativarInativarExcluir);
		}

		for (PricingPuntoVenta pricingPuntoVenta : entidad.getPricingPuntoventaList()) {
			pricingPuntoVenta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingPuntoVenta.setFecmodif(Calendar.getInstance().getTime());
			pricingPuntoVenta.setActivo(ativarInativarExcluir);
		}

		for (PricingRuta pricingRuta : entidad.getPricingRutaList()) {
			pricingRuta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingRuta.setFecmodif(Calendar.getInstance().getTime());
			pricingRuta.setActivo(ativarInativarExcluir);
		}

		for (PricingTipoPtoVta pricingTipoPtoVta : entidad.getPricingTipoptovtaList()) {
			pricingTipoPtoVta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingTipoPtoVta.setFecmodif(Calendar.getInstance().getTime());
			pricingTipoPtoVta.setActivo(ativarInativarExcluir);
		}

		for (PricingVigencia pricingVigencia : entidad.getPricingVigenciaList()) {
			pricingVigencia.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingVigencia.setFecmodif(Calendar.getInstance().getTime());
			pricingVigencia.setActivo(ativarInativarExcluir);
		}

		for (PricingTipoServicio pricingTipoServicio : entidad.getPricingTipoServicioList()) {
			pricingTipoServicio.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingTipoServicio.setFecmodif(Calendar.getInstance().getTime());
			pricingTipoServicio.setActivo(ativarInativarExcluir);
		}

		for (PricingFormapago pricingFormapago : entidad.getPricingFormapagoList()) {
			pricingFormapago.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingFormapago.setFecmodif(Calendar.getInstance().getTime());
			pricingFormapago.setActivo(ativarInativarExcluir);
		}
		
		for (PricingConexao pricingConexao : entidad.getPricingConexaoList()) {
			pricingConexao.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pricingConexao.setFecmodif(Calendar.getInstance().getTime());
			pricingConexao.setActivo(ativarInativarExcluir);
		}

		entidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
		entidad.setFecmodif(Calendar.getInstance().getTime());
		entidad.setActivo(ativarInativarExcluir);

		pricingDAO.actualizacion(entidad);
	}

	@Transactional
	public void borrar(Pricing entidad) {
		ativarInativarExcluirPricing(entidad, Pricing.EXCLUIDO);
	}

	@Transactional
	public void inativar(Pricing entidad) {
		ativarInativarExcluirPricing(entidad, Pricing.INATIVO);
	}

	@Transactional
	public void ativar(Pricing entidad) {
		ativarInativarExcluirPricing(entidad, Pricing.ATIVO);
	}

	@Transactional
	public void borrarFisico(Pricing entidad) {
		borrar(entidad);
	}

	public List<Pricing> buscar(String nombPricing, Empresa empresa, Short cantboleto,
			Integer cantdiasanticipacion, BigDecimal descuentoporcentaje, BigDecimal descuentoporcredondo) {
		return pricingDAO.buscar(nombPricing, empresa, cantboleto, cantdiasanticipacion, descuentoporcentaje, descuentoporcredondo);
	}

	public List<Pricing> buscarPorNombre(String nombPricing) {
		return pricingDAO.buscarPorNombre(nombPricing);
	}

	@Transactional
	public Boolean clonarPricing(Integer pricingId, String nomePricing, boolean incluirPadraoPricingTipoPassagemPET) {

		Pricing pricing = this.obtenerID(pricingId);
		Pricing clonePricing = new Pricing();

		clonePricing.setNombPricing(nomePricing);

		clonePricing.setCantboleto(pricing.getCantboleto());
		clonePricing.setCantdiasanticipacion(pricing.getCantdiasanticipacion());
		clonePricing.setDescuentoporcentaje(pricing.getDescuentoporcentaje());
		clonePricing.setDescuentoporcredondo(pricing.getDescuentoporcredondo());
		clonePricing.setEmpresa(pricing.getEmpresa());
		clonePricing.setIndcancelable(pricing.getIndcancelable());
		clonePricing.setIndGeneraFeriadoVenta(pricing.getIndGeneraFeriadoVenta());
		clonePricing.setIndGeneraFeriadoViaje(pricing.getIndGeneraFeriadoViaje());
		clonePricing.setIndtransferible(pricing.getIndtransferible());
		clonePricing.setIndreservable(pricing.getIndreservable());

		clonePricing = suscribir(clonePricing);

		percorrerPricingAtivoParaClonar(pricing, clonePricing, incluirPadraoPricingTipoPassagemPET);
		actualizacion(clonePricing);

		inativar(clonePricing);

		return clonePricing.getPricingId() != null;
	}

	private void percorrerPricingAtivoParaClonar(Pricing pricing, Pricing clonePricing, boolean incluirPadraoPricingTipoPassagemPET) {
		Integer ativar = Pricing.ATIVO;
		clonePricing.setPricingAsientoList(obtemPricingAsientoList(pricing, clonePricing, ativar));
		clonePricing.setPricingCategoriaList(obtemPricingCategoriaList(pricing, clonePricing, ativar, incluirPadraoPricingTipoPassagemPET));
		clonePricing.setPricingClaseList(obtemPricingClaseList(pricing, clonePricing, ativar));
		clonePricing.setPricingCorridaList(obtemPricingCorridaList(pricing, clonePricing, ativar));
		clonePricing.setPricingDiaList(obtemPricingDiaList(pricing, clonePricing, ativar));
		clonePricing.setPricingImporteList(obtemPricingImporteList(pricing, clonePricing, ativar));
		clonePricing.setPricingMarcaList(obtemPricingMarcaList(pricing, clonePricing, ativar));
		clonePricing.setPricingMercadoList(obtemPricingMercadoList(pricing, clonePricing, ativar));
		clonePricing.setPricingOcupaAntecipaList(obtemPricingOcupaAntecipaList(pricing, clonePricing, ativar));
		clonePricing.setPricingPuntoventaList(obtemPricingPuntoVentaList(pricing, clonePricing, ativar));
		clonePricing.setPricingRutaList(obtemPricingRutaList(pricing, clonePricing, ativar));
		clonePricing.setPricingTipoptovtaList(obtemPricingTipoPtoVtaList(pricing, clonePricing, ativar));
		clonePricing.setPricingVigenciaList(obtemPricingVigenciaList(pricing, clonePricing, ativar));
		clonePricing.setPricingTipoServicioList(obtemPricingTipoServicioList(pricing, clonePricing, ativar));
		clonePricing.setPricingFormapagoList(obtemPricingFormapagoList(pricing, clonePricing, ativar));
		clonePricing.setPricingConexaoList(obtemPricingConexaoList(pricing, clonePricing, ativar));
	}
	
	private List<PricingConexao> obtemPricingConexaoList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingConexao> pricingConexaoList = new ArrayList<PricingConexao>();
		for (PricingConexao pricingConexao : pricing.getPricingConexaoList()) {
			
			PricingConexao clonePricingConexao = new PricingConexao();
			clonePricingConexao.setActivo(Pricing.ATIVO);
			clonePricingConexao.setConexionCtrl(pricingConexao.getConexionCtrl());
			clonePricingConexao.setFecmodif(Calendar.getInstance().getTime());
			clonePricingConexao.setPricing(pricing);
			clonePricingConexao.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());

			pricingConexaoList.add(clonePricingConexao);
		}
		return pricingConexaoList;
	}

	private List<PricingFormapago> obtemPricingFormapagoList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingFormapago> pricingFormapagoList = new ArrayList<PricingFormapago>();
		for (PricingFormapago pricingFormapago : pricing.getPricingFormapagoList()) {

			PricingFormapago clonePricingFormapago = new PricingFormapago();
			clonePricingFormapago.setIndexcepcion(pricingFormapago.getIndexcepcion());
			clonePricingFormapago.setFormaPago(pricingFormapago.getFormaPago());

			clonePricingFormapago.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingFormapago.setFecmodif(Calendar.getInstance().getTime());
			clonePricingFormapago.setActivo(ativar);
			clonePricingFormapago.setPricing(clonePricing);
			pricingFormapagoList.add(clonePricingFormapago);
		}
		return pricingFormapagoList;
	}

	private List<PricingTipoServicio> obtemPricingTipoServicioList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingTipoServicio> pricingTipoServicioList = new ArrayList<PricingTipoServicio>();
		for (PricingTipoServicio pricingTipoServicio : pricing.getPricingTipoServicioList()) {

			PricingTipoServicio clonePricingTipoServicio = new PricingTipoServicio();
			clonePricingTipoServicio.setTipoServicio(pricingTipoServicio.getTipoServicio());

			clonePricingTipoServicio.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingTipoServicio.setFecmodif(Calendar.getInstance().getTime());
			clonePricingTipoServicio.setActivo(ativar);
			clonePricingTipoServicio.setPricing(clonePricing);
			pricingTipoServicioList.add(clonePricingTipoServicio);
		}
		return pricingTipoServicioList;
	}

	private List<PricingVigencia> obtemPricingVigenciaList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingVigencia> pricingVigenciaList = new ArrayList<PricingVigencia>();
		for (PricingVigencia pricingVigencia : pricing.getPricingVigenciaList()) {

			PricingVigencia clonePricingVigencia = new PricingVigencia();

			clonePricingVigencia.setFecfinventa(pricingVigencia.getFecfinventa());
			clonePricingVigencia.setFecinicioventa(pricingVigencia.getFecinicioventa());
			clonePricingVigencia.setFecinicioviaje(pricingVigencia.getFecinicioviaje());
			clonePricingVigencia.setFecfinviaje(pricingVigencia.getFecfinviaje());

			clonePricingVigencia.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingVigencia.setFecmodif(Calendar.getInstance().getTime());
			clonePricingVigencia.setActivo(ativar);
			clonePricingVigencia.setPricing(clonePricing);
			pricingVigenciaList.add(clonePricingVigencia);
		}
		return pricingVigenciaList;
	}

	private List<PricingTipoPtoVta> obtemPricingTipoPtoVtaList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingTipoPtoVta> pricingTipoPtoVtaList = new ArrayList<PricingTipoPtoVta>();
		for (PricingTipoPtoVta pricingTipoPtoVta : pricing.getPricingTipoptovtaList()) {

			PricingTipoPtoVta clonePricingTipoPtoVta = new PricingTipoPtoVta();

			clonePricingTipoPtoVta.setTipoPtovta(pricingTipoPtoVta.getTipoPtovta());

			clonePricingTipoPtoVta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingTipoPtoVta.setFecmodif(Calendar.getInstance().getTime());
			clonePricingTipoPtoVta.setActivo(ativar);
			clonePricingTipoPtoVta.setPricing(clonePricing);
			pricingTipoPtoVtaList.add(clonePricingTipoPtoVta);
		}
		return pricingTipoPtoVtaList;
	}

	private List<PricingRuta> obtemPricingRutaList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingRuta> pricingRutaList = new ArrayList<PricingRuta>();
		for (PricingRuta pricingRuta : pricing.getPricingRutaList()) {

			PricingRuta clonePricingRuta = new PricingRuta();

			clonePricingRuta.setRuta(pricingRuta.getRuta());

			clonePricingRuta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingRuta.setFecmodif(Calendar.getInstance().getTime());
			clonePricingRuta.setActivo(ativar);
			clonePricingRuta.setPricing(clonePricing);
			pricingRutaList.add(clonePricingRuta);
		}
		return pricingRutaList;
	}

	private List<PricingPuntoVenta> obtemPricingPuntoVentaList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingPuntoVenta> pricingPuntoVentaList = new ArrayList<PricingPuntoVenta>();
		for (PricingPuntoVenta pricingPuntoVenta : pricing.getPricingPuntoventaList()) {

			PricingPuntoVenta clonePricingPuntoVenta = new PricingPuntoVenta();

			clonePricingPuntoVenta.setPuntoVenta(pricingPuntoVenta.getPuntoVenta());

			clonePricingPuntoVenta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingPuntoVenta.setFecmodif(Calendar.getInstance().getTime());
			clonePricingPuntoVenta.setActivo(ativar);
			clonePricingPuntoVenta.setPricing(clonePricing);
			pricingPuntoVentaList.add(clonePricingPuntoVenta);
		}
		return pricingPuntoVentaList;
	}

	private List<PricingOcupaAntecipa> obtemPricingOcupaAntecipaList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingOcupaAntecipa> pricingOcupaAntecipaList = new ArrayList<PricingOcupaAntecipa>();
		for (PricingOcupaAntecipa pricingOcupaAntecipa : pricing.getPricingOcupaAntecipaList()) {

			PricingOcupaAntecipa clonePricingOcupaAntecipa = new PricingOcupaAntecipa();

			clonePricingOcupaAntecipa.setCantasientosmin(pricingOcupaAntecipa.getCantasientosmin());
			clonePricingOcupaAntecipa.setCantasientosmax(pricingOcupaAntecipa.getCantasientosmax());
			clonePricingOcupaAntecipa.setCantdiasmin(pricingOcupaAntecipa.getCantdiasmin());
			clonePricingOcupaAntecipa.setCantdiasmax(pricingOcupaAntecipa.getCantdiasmax());
			clonePricingOcupaAntecipa.setImporte(pricingOcupaAntecipa.getImporte());
			clonePricingOcupaAntecipa.setPorcentaje(pricingOcupaAntecipa.getPorcentaje());
			clonePricingOcupaAntecipa.setOcupacioninicial(pricingOcupaAntecipa.getOcupacioninicial());
			clonePricingOcupaAntecipa.setOcupacionfinal(pricingOcupaAntecipa.getOcupacionfinal());

			clonePricingOcupaAntecipa.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingOcupaAntecipa.setFecmodif(Calendar.getInstance().getTime());
			clonePricingOcupaAntecipa.setActivo(ativar);
			clonePricingOcupaAntecipa.setPricing(clonePricing);
			pricingOcupaAntecipaList.add(clonePricingOcupaAntecipa);
		}
		return pricingOcupaAntecipaList;
	}

	private List<PricingMercado> obtemPricingMercadoList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingMercado> pricingMercadoList = new ArrayList<PricingMercado>();
		for (PricingMercado pricingMercado : pricing.getPricingMercadoList()) {

			PricingMercado clonePricingMercado = new PricingMercado();

			clonePricingMercado.setDestino(pricingMercado.getDestino());
			clonePricingMercado.setOrigen(pricingMercado.getOrigen());

			clonePricingMercado.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingMercado.setFecmodif(Calendar.getInstance().getTime());
			clonePricingMercado.setActivo(ativar);
			clonePricingMercado.setPricing(clonePricing);
			pricingMercadoList.add(clonePricingMercado);
		}
		return pricingMercadoList;
	}

	private List<PricingMarca> obtemPricingMarcaList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingMarca> pricingMarcaList = new ArrayList<PricingMarca>();
		for (PricingMarca pricingMarca : pricing.getPricingMarcaList()) {

			PricingMarca clonePricingMarca = new PricingMarca();

			clonePricingMarca.setMarca(pricingMarca.getMarca());

			clonePricingMarca.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingMarca.setFecmodif(Calendar.getInstance().getTime());
			clonePricingMarca.setActivo(ativar);
			clonePricingMarca.setPricing(clonePricing);
			pricingMarcaList.add(clonePricingMarca);
		}
		return pricingMarcaList;
	}

	private List<PricingImporte> obtemPricingImporteList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingImporte> pricingImporteList = new ArrayList<PricingImporte>();
		for (PricingImporte pricingImporte : pricing.getPricingImporteList()) {

			PricingImporte clonePricingImporte = new PricingImporte();

			clonePricingImporte.setImporte(pricingImporte.getImporte());
			clonePricingImporte.setMoneda(pricingImporte.getMoneda());

			clonePricingImporte.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingImporte.setFecmodif(Calendar.getInstance().getTime());
			clonePricingImporte.setActivo(ativar);
			clonePricingImporte.setPricing(clonePricing);
			pricingImporteList.add(clonePricingImporte);
		}
		return pricingImporteList;
	}

	private List<PricingDia> obtemPricingDiaList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingDia> pricingDiaList = new ArrayList<PricingDia>();
		for (PricingDia pricingDia : pricing.getPricingDiaList()) {

			PricingDia clonePricingDia = new PricingDia();

			clonePricingDia.setHorariofin(pricingDia.getHorariofin());
			clonePricingDia.setHorarioinicio(pricingDia.getHorarioinicio());
			clonePricingDia.setInddomingo(pricingDia.getInddomingo());
			clonePricingDia.setIndjueves(pricingDia.getIndjueves());
			clonePricingDia.setIndlunes(pricingDia.getIndlunes());
			clonePricingDia.setIndmartes(pricingDia.getIndmartes());
			clonePricingDia.setIndmiercoles(pricingDia.getIndmiercoles());
			clonePricingDia.setIndsabado(pricingDia.getIndsabado());
			clonePricingDia.setIndviernes(pricingDia.getIndviernes());
			clonePricingDia.setIndfecventa(pricingDia.getIndfecventa());
			clonePricingDia.setIndfecviaje(pricingDia.getIndfecviaje());

			clonePricingDia.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingDia.setFecmodif(Calendar.getInstance().getTime());
			clonePricingDia.setActivo(ativar);
			clonePricingDia.setPricing(clonePricing);
			pricingDiaList.add(clonePricingDia);
		}
		return pricingDiaList;
	}

	private List<PricingCorrida> obtemPricingCorridaList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingCorrida> pricingCorridaList = new ArrayList<PricingCorrida>();
		for (PricingCorrida pricingCorrida : pricing.getPricingCorridaList()) {

			PricingCorrida clonePricingCorrida = new PricingCorrida();

			clonePricingCorrida.setCorridaCtrl(pricingCorrida.getCorridaCtrl());

			clonePricingCorrida.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingCorrida.setFecmodif(Calendar.getInstance().getTime());
			clonePricingCorrida.setActivo(ativar);
			clonePricingCorrida.setPricing(clonePricing);
			pricingCorridaList.add(clonePricingCorrida);
		}
		return pricingCorridaList;
	}

	private List<PricingAsiento> obtemPricingAsientoList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingAsiento> pricingAsientoList = new ArrayList<PricingAsiento>();
		for (PricingAsiento pricingAsiento : pricing.getPricingAsientoList()) {

			PricingAsiento clonePricingAsiento = new PricingAsiento();

			clonePricingAsiento.setNombImagen(pricingAsiento.getNombImagen());
			clonePricingAsiento.setNumeasiento(pricingAsiento.getNumeasiento());
			clonePricingAsiento.setPorcentaje(pricingAsiento.getPorcentaje());

			clonePricingAsiento.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingAsiento.setFecmodif(Calendar.getInstance().getTime());
			clonePricingAsiento.setActivo(ativar);
			clonePricingAsiento.setPricing(clonePricing);
			pricingAsientoList.add(clonePricingAsiento);
		}
		return pricingAsientoList;
	}

	private List<PricingClase> obtemPricingClaseList(Pricing pricing, Pricing clonePricing, Integer ativar) {
		List<PricingClase> pricingClaseList = new ArrayList<PricingClase>();
		for (PricingClase pricingClase : pricing.getPricingClaseList()) {

			PricingClase clonePricingClase = new PricingClase();

			clonePricingClase.setClaseServicio(pricingClase.getClaseServicio());

			clonePricingClase.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			clonePricingClase.setFecmodif(Calendar.getInstance().getTime());
			clonePricingClase.setActivo(ativar);
			clonePricingClase.setPricing(clonePricing);
			pricingClaseList.add(clonePricingClase);
		}
		return pricingClaseList;
	}

	private List<PricingCategoria> obtemPricingCategoriaList(Pricing pricing, Pricing clonePricing, Integer ativar, boolean incluirPadraoPricingTipoPassagemPET) {
		List<PricingCategoria> pricingCategoriaList = new ArrayList<PricingCategoria>();
		for (PricingCategoria pricingCategoria : pricing.getPricingCategoriaList()) {
			setCategoria(clonePricing, ativar, pricingCategoriaList, pricingCategoria.getCategoria());
		}
		
		if (incluirPadraoPricingTipoPassagemPET) {
			configuraInclusaoPadraoTipoPassagemPET(pricing, clonePricing, ativar, pricingCategoriaList);
		}
		
		return pricingCategoriaList;
	}

	protected void setCategoria(Pricing clonePricing, Integer ativar, List<PricingCategoria> pricingCategoriaList, Categoria categoria) {
		PricingCategoria clonePricingCategoria = new PricingCategoria();
		clonePricingCategoria.setCategoria(categoria);
		clonePricingCategoria.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
		clonePricingCategoria.setFecmodif(Calendar.getInstance().getTime());
		clonePricingCategoria.setActivo(ativar);
		clonePricingCategoria.setPricing(clonePricing);
		pricingCategoriaList.add(clonePricingCategoria);
	}
	
	private void configuraInclusaoPadraoTipoPassagemPET(Pricing pricing, Pricing clonePricing, Integer ativar, List<PricingCategoria> pricingCategoriaList) {
		Categoria categoriaNormal = categoriaService.buscarUmaCategoria("NORMAL");
		if (!possuiCategoriaNaLista(pricing.getPricingCategoriaList(), categoriaNormal)) {
			setCategoria(clonePricing, ativar, pricingCategoriaList, categoriaNormal);
		}
		Categoria categoriaPET = categoriaService.buscarUmaCategoria("PET");
		if (!possuiCategoriaNaLista(pricing.getPricingCategoriaList(), categoriaPET)) {
			setCategoria(clonePricing, ativar, pricingCategoriaList, categoriaPET);
		}
		
	}

	private boolean possuiCategoriaNaLista(List<PricingCategoria> categoriaList, Categoria categoria) {
		for (PricingCategoria pricingCategoria : categoriaList) {
			if (pricingCategoria.getCategoria().equals(categoria)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public List<Pricing> buscar(Integer pricingId, String nomepricing, Integer empresaId, Date dataViagemInicio, Date dataViagemFim, Integer origemId, Integer destinoId) {
		return pricingDAO.buscar(pricingId, nomepricing, empresaId, dataViagemInicio, dataViagemFim, origemId, destinoId);
	}
	
	@Override
	@Transactional
	public void excluirPricing(Integer diasFimViagem) {
		try {
			List<Integer> pricings = pricingDAO.buscar(diasFimViagem);
			for (Integer pricingId : pricings) {
				Pricing pricing = pricingDAO.obtenerID(pricingId);
				borrar(pricing);
				log.info(String.format("Pricing [%d] excluido via WS", pricingId));
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
	
}
