package com.rjconsultores.ventaboletos.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rjconsultores.ventaboletos.dao.GP_PricingDAO;
import com.rjconsultores.ventaboletos.entidad.Categoria;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.GP_Pricing;
import com.rjconsultores.ventaboletos.entidad.GP_PricingAsiento;
import com.rjconsultores.ventaboletos.entidad.GP_PricingCategoria;
import com.rjconsultores.ventaboletos.entidad.GP_PricingClase;
import com.rjconsultores.ventaboletos.entidad.GP_PricingCorrida;
import com.rjconsultores.ventaboletos.entidad.GP_PricingDia;
import com.rjconsultores.ventaboletos.entidad.GP_PricingFormapago;
import com.rjconsultores.ventaboletos.entidad.GP_PricingImporte;
import com.rjconsultores.ventaboletos.entidad.GP_PricingMarca;
import com.rjconsultores.ventaboletos.entidad.GP_PricingMercado;
import com.rjconsultores.ventaboletos.entidad.GP_PricingOcupaAntecipa;
import com.rjconsultores.ventaboletos.entidad.GP_PricingPuntoVenta;
import com.rjconsultores.ventaboletos.entidad.GP_PricingRuta;
import com.rjconsultores.ventaboletos.entidad.GP_PricingTipoPtoVta;
import com.rjconsultores.ventaboletos.entidad.GP_PricingTipoServicio;
import com.rjconsultores.ventaboletos.entidad.GP_PricingVigencia;
import com.rjconsultores.ventaboletos.service.CategoriaService;
import com.rjconsultores.ventaboletos.service.GP_PricingHistoricoService;
import com.rjconsultores.ventaboletos.service.GP_PricingService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;

@Service("gp_pricingService")
public class GP_PricingServiceImpl implements GP_PricingService {

	@Autowired
	private GP_PricingDAO pricingDAO;
	
	@Autowired
	private GP_PricingHistoricoService pricingHistoricoService;
	
	@Autowired
    private CategoriaService categoriaService;

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

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

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

		return pricingDAO.suscribir(entidad);
	}

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

		return pricingDAO.actualizacion(entidad);
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		pricingDAO.actualizacion(entidad);
		
		pricingHistoricoService.registarExclusao(entidad, ativarInativarExcluir == GP_Pricing.ATIVO);
	}

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

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

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

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

	public List<GP_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<GP_Pricing> buscarPorNombre(String nombPricing) {
		return pricingDAO.buscarPorNombre(nombPricing);
	}

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

		GP_Pricing pricing = this.obtenerID(pricingId);
		GP_Pricing clonePricing = new GP_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 = suscribir(clonePricing);

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

		inativar(clonePricing);
		
		pricingHistoricoService.compararESalvar(null, clonePricing);

		return clonePricing.getPricingId() != null;
	}
	
	@Transactional
	public GP_Pricing clonarGP_Pricing(Integer pricingId, String nomePricing, boolean incluirPadraoPricingTipoPassagemPET, 
			boolean isPricingAtivo) {

		GP_Pricing pricing = this.obtenerID(pricingId);
		GP_Pricing clonePricing = new GP_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 = suscribir(clonePricing);

		percorrerPricingAtivoParaClonar(pricing, clonePricing, incluirPadraoPricingTipoPassagemPET);
		actualizacion(clonePricing);
		
		if(!isPricingAtivo){
			inativar(clonePricing);
		}	

		return clonePricing;
	}

	private void percorrerPricingAtivoParaClonar(GP_Pricing pricing, GP_Pricing clonePricing, boolean incluirPadraoPricingTipoPassagemPET) {
		Integer ativar = GP_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));
	}

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

			GP_PricingFormapago clonePricingFormapago = new GP_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<GP_PricingTipoServicio> obtemPricingTipoServicioList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingTipoServicio> pricingTipoServicioList = new ArrayList<GP_PricingTipoServicio>();
		for (GP_PricingTipoServicio pricingTipoServicio : pricing.getPricingTipoServicioList()) {

			GP_PricingTipoServicio clonePricingTipoServicio = new GP_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<GP_PricingVigencia> obtemPricingVigenciaList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingVigencia> pricingVigenciaList = new ArrayList<GP_PricingVigencia>();
		for (GP_PricingVigencia pricingVigencia : pricing.getPricingVigenciaList()) {

			GP_PricingVigencia clonePricingVigencia = new GP_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<GP_PricingTipoPtoVta> obtemPricingTipoPtoVtaList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingTipoPtoVta> pricingTipoPtoVtaList = new ArrayList<GP_PricingTipoPtoVta>();
		for (GP_PricingTipoPtoVta pricingTipoPtoVta : pricing.getPricingTipoptovtaList()) {

			GP_PricingTipoPtoVta clonePricingTipoPtoVta = new GP_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<GP_PricingRuta> obtemPricingRutaList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingRuta> pricingRutaList = new ArrayList<GP_PricingRuta>();
		for (GP_PricingRuta pricingRuta : pricing.getPricingRutaList()) {

			GP_PricingRuta clonePricingRuta = new GP_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<GP_PricingPuntoVenta> obtemPricingPuntoVentaList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingPuntoVenta> pricingPuntoVentaList = new ArrayList<GP_PricingPuntoVenta>();
		for (GP_PricingPuntoVenta pricingPuntoVenta : pricing.getPricingPuntoventaList()) {

			GP_PricingPuntoVenta clonePricingPuntoVenta = new GP_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<GP_PricingOcupaAntecipa> obtemPricingOcupaAntecipaList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingOcupaAntecipa> pricingOcupaAntecipaList = new ArrayList<GP_PricingOcupaAntecipa>();
		for (GP_PricingOcupaAntecipa pricingOcupaAntecipa : pricing.getPricingOcupaAntecipaList()) {

			GP_PricingOcupaAntecipa clonePricingOcupaAntecipa = new GP_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<GP_PricingMercado> obtemPricingMercadoList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingMercado> pricingMercadoList = new ArrayList<GP_PricingMercado>();
		for (GP_PricingMercado pricingMercado : pricing.getPricingMercadoList()) {

			GP_PricingMercado clonePricingMercado = new GP_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<GP_PricingMarca> obtemPricingMarcaList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingMarca> pricingMarcaList = new ArrayList<GP_PricingMarca>();
		for (GP_PricingMarca pricingMarca : pricing.getPricingMarcaList()) {

			GP_PricingMarca clonePricingMarca = new GP_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<GP_PricingImporte> obtemPricingImporteList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingImporte> pricingImporteList = new ArrayList<GP_PricingImporte>();
		for (GP_PricingImporte pricingImporte : pricing.getPricingImporteList()) {

			GP_PricingImporte clonePricingImporte = new GP_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<GP_PricingDia> obtemPricingDiaList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingDia> pricingDiaList = new ArrayList<GP_PricingDia>();
		for (GP_PricingDia pricingDia : pricing.getPricingDiaList()) {

			GP_PricingDia clonePricingDia = new GP_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<GP_PricingCorrida> obtemPricingCorridaList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingCorrida> pricingCorridaList = new ArrayList<GP_PricingCorrida>();
		for (GP_PricingCorrida pricingCorrida : pricing.getPricingCorridaList()) {

			GP_PricingCorrida clonePricingCorrida = new GP_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<GP_PricingAsiento> obtemPricingAsientoList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingAsiento> pricingAsientoList = new ArrayList<GP_PricingAsiento>();
		for (GP_PricingAsiento pricingAsiento : pricing.getPricingAsientoList()) {

			GP_PricingAsiento clonePricingAsiento = new GP_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<GP_PricingClase> obtemPricingClaseList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar) {
		List<GP_PricingClase> pricingClaseList = new ArrayList<GP_PricingClase>();
		for (GP_PricingClase pricingClase : pricing.getPricingClaseList()) {

			GP_PricingClase clonePricingClase = new GP_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<GP_PricingCategoria> obtemPricingCategoriaList(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar, boolean incluirPadraoPricingTipoPassagemPET) {
		List<GP_PricingCategoria> pricingCategoriaList = new ArrayList<GP_PricingCategoria>();
		for (GP_PricingCategoria pricingCategoria : pricing.getPricingCategoriaList()) {
			setCategoria(clonePricing, ativar, pricingCategoriaList, pricingCategoria.getCategoria());
		}
		
		if (incluirPadraoPricingTipoPassagemPET) {
			configuraInclusaoPadraoTipoPassagemPET(pricing, clonePricing, ativar, pricingCategoriaList);
		}
		
		return pricingCategoriaList;
	}

	protected void setCategoria(GP_Pricing clonePricing, Integer ativar, List<GP_PricingCategoria> pricingCategoriaList, Categoria categoria) {
		GP_PricingCategoria clonePricingCategoria = new GP_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(GP_Pricing pricing, GP_Pricing clonePricing, Integer ativar, List<GP_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<GP_PricingCategoria> categoriaList, Categoria categoria) {
		for (GP_PricingCategoria pricingCategoria : categoriaList) {
			if (pricingCategoria.getCategoria().equals(categoria)) {
				return true;
			}
		}
		return false;
	}
	
}
