/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.gui.controladores.pricing;

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

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.util.resource.Labels;
import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Image;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.entidad.Categoria;
import com.rjconsultores.ventaboletos.entidad.ClaseServicio;
import com.rjconsultores.ventaboletos.entidad.ConexionCtrl;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.EsquemaCorrida;
import com.rjconsultores.ventaboletos.entidad.Marca;
import com.rjconsultores.ventaboletos.entidad.Parada;
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.entidad.PuntoVenta;
import com.rjconsultores.ventaboletos.entidad.Ruta;
import com.rjconsultores.ventaboletos.entidad.TipoPuntoVenta;
import com.rjconsultores.ventaboletos.service.CategoriaService;
import com.rjconsultores.ventaboletos.service.ClaseServicioService;
import com.rjconsultores.ventaboletos.service.ConexionCtrlService;
import com.rjconsultores.ventaboletos.service.MarcaService;
import com.rjconsultores.ventaboletos.service.ParadaService;
import com.rjconsultores.ventaboletos.service.PricingCategoriaService;
import com.rjconsultores.ventaboletos.service.PricingClaseService;
import com.rjconsultores.ventaboletos.service.PricingFormapagoService;
import com.rjconsultores.ventaboletos.service.PricingImporteService;
import com.rjconsultores.ventaboletos.service.PricingMarcaService;
import com.rjconsultores.ventaboletos.service.PricingMercadoService;
import com.rjconsultores.ventaboletos.service.PricingPuntoVentaService;
import com.rjconsultores.ventaboletos.service.PricingRutaService;
import com.rjconsultores.ventaboletos.service.PricingService;
import com.rjconsultores.ventaboletos.service.PricingTipoPtoVtaService;
import com.rjconsultores.ventaboletos.service.PuntoVentaService;
import com.rjconsultores.ventaboletos.service.RutaService;
import com.rjconsultores.ventaboletos.service.TipoPuntoVentaService;
import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties;
import com.rjconsultores.ventaboletos.utilerias.CustomEnum;
import com.rjconsultores.ventaboletos.utilerias.MoneyHelper;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.vo.parada.ParadaVOConexionRuta;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.MyTextboxDecimal;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderConexaoPricing;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingAsiento;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingCategoria;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingClase;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingCorrida;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingDia;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingFormaPago;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingImporte;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingMarca;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingMercado;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingOcupaAntecipa;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingPuntoVenta;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingRuta;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingTipoPtoVta;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingTipoServicio;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderPricingVigencia;

/**
 * 
 * @author Rafius
 */
@Controller("editarPricingController")
@Scope("prototype")
@SuppressWarnings("unused")
public class EditarPricingController extends PricingController {

	private static final long serialVersionUID = 1L;
	private static Logger log = LogManager.getLogger(EditarPricingController.class);

	@Autowired
	private PricingService pricingService;
	@Autowired
	private PricingMarcaService pricingMarcaService;
	@Autowired
	private PricingClaseService pricingClaseService;
	@Autowired
	private PricingPuntoVentaService pricingPuntoVentaService;
	@Autowired
	private PricingTipoPtoVtaService pricingTipoPtoVtaService;
	@Autowired
	private PricingRutaService pricingRutaService;
	@Autowired
	private PricingCategoriaService pricingCategoriaService;
	@Autowired
	private PricingMercadoService pricingMercadoService;
	@Autowired
	private PricingImporteService pricingImporteService;
	@Autowired
	private PricingFormapagoService pricingFormapagoService;
	@Autowired
	private MarcaService marcaService;
	@Autowired
	private ClaseServicioService claseServicioService;
	@Autowired
	private PuntoVentaService puntoVentaService;
	@Autowired
	private TipoPuntoVentaService tipoPuntoVentaService;
	@Autowired
	private RutaService rutaService;
	@Autowired
	private CategoriaService categoriaService;
	@Autowired
	private ParadaService paradaService;
	@Autowired
	private ConexionCtrlService conexionCtrlService;

	private MyListbox pricingList;
	private MyListbox pricingVigenciaList;
	private MyListbox pricingImporteList;
	private MyListbox pricingMarcaList;
	private MyListbox pricingClaseList;
	private MyListbox pricingPuntoVentaList;
	private MyListbox pricingTipoPtoVtaList;
	private MyListbox pricingRutaList;
	private MyListbox pricingMercadoList;
	private MyListbox pricingCategoriaList;
	private MyListbox pricingDiaList;
	private MyListbox pricingCorridaList;
	private MyListbox pricingAsientoList;
	private MyListbox pricingTipoServicioList;
	private MyListbox pricingOcupaAntecipaList;
	private MyListbox pricingFormapagoList;
	private MyListbox conexaoCtrlList;
	
	private List<Empresa> lsEmpresa;
	private List<PricingVigencia> lsPricingVigencia;
	private List<PricingImporte> lsPricingImporte;
	private List<PricingClase> lsPricingClase;
	private List<PricingTipoPtoVta> lsPricingTipoPtoVta;
	private List<PricingPuntoVenta> lsPricingPuntoVenta;
	private List<PricingMarca> lsPricingMarca;
	private List<PricingMercado> lsPricingMercado;
	private List<PricingDia> lsPricingDia;
	private List<PricingCorrida> lsPricingCorrida;
	private List<PricingCategoria> lsPricingCategoria;
	private List<PricingAsiento> lsPricingAsiento;
	private List<PricingOcupaAntecipa> lsPricingOcupaAntecipa;
	private List<PricingRuta> lsPricingRuta;
	private List<PricingTipoServicio> lsPricingTipoServicio;
	private List<PricingFormapago> lsPricingFormapago;
	private List<ConexionCtrl> lsConexoesCtrlList;
	private List<ParadaVOConexionRuta> conexoesPricingList;
	private List<ParadaVOConexionRuta> conexoesPricingListApresentacao;
	private List<Parada> paradasConexao;
	private List<PricingConexao> lsPricingConexao;
	
	private Radio rdTransSi;
	private Radio rdTransNo;
	private Radio rdExibirVendaSi;
	private Radio rdExibirVendaNo;
	private Radio rdCanceSi;
	private Radio rdCanceNo;
	private Radio rdReserSi;
	private Radio rdReserNo;
	private Radio radioImp;
	private Radio radioPor;
	
	private MyTextboxDecimal descuentoporcredondo;
	private MyTextboxDecimal descuentoporcsen;
	
	private Button btnSalvar;
	private Button btnCopiar;
	private Button btnNovoImporte;
	private Button btnApagarImporte;
	private Button btnModificarImporte;
	private Button btnApagar;
	private Button btnInativar;
	private Button btnAtivar;
	private Button btnNovoVigencia;
	private Button btnApagarVigencia;
	private Button btnModificarVigencia;
	private Button btnNovoMarca;
	private Button btnApagarMarca;
	private Button btnModificarMarca;
	private Button btnNovoClase;
	private Button btnApagarClase;
	private Button btnModificarClase;
	private Button btnNovoPuntoVenta;
	private Button btnApagarPuntoVenta;
	private Button btnModificarPuntoVenta;
	private Button btnNovoRuta;
	private Button btnApagarRuta;
	private Button btnModificarRuta;
	private Button btnNovoTipoPtoVta;
	private Button btnApagarTipoPtoVta;
	private Button btnModificarTipoPtoVta;
	private Button btnNovoMercado;
	private Button btnApagarMercado;
	private Button btnModificarMercado;
	private Button btnNovoDia;
	private Button btnApagarDia;
	private Button btnNovoCorrida;
	private Button btnApagarCorrida;
	private Button btnNovoAsiento;
	private Button btnApagarAsiento;
	private Button btnModificarAsiento;
	private Button btnNovoOcupaAntecipa;
	private Button btnApagarOcupaAntecipa;
	private Button btnModificarOcupaAntecipa;
	private Button btnNovoTipoServicio;
	private Button btnApagarTipoServicio;
	private Button btnModificarTipoServicio;
	private Button btnNovoFormaPago;
	private Button btnApagarFormaPago;
	private Button btnNovoCategoria;
	private Button btnApagarCategoria;
	private Button btnModificarCategoria;
	private Button btnModificarFormaPago;
	
	private Textbox nombrePricing;
	
	private Intbox itxCantboleto;
	private Intbox cantdiasanticipacion;
	
	private Image imgPricingDividirIdaEVolta;
	
	private Checkbox chkIndSomenteAssentoSugerido;
	private Checkbox chkIndPricingDividirIdaEVolta;
	private Checkbox chkIndSafer;
	private Checkbox chkExibePopupPricingPol;
	
	private Combobox cmbConexaoCtrl;
	private Combobox cmbIndGeneraFeriadoFechaVenta;
	private Combobox cmbIndGeneraFeriadoFechaViaje;
	private Combobox cmbEmpresa;
	
	private Radio rdOcupacaoTrechoSim;
	private Radio rdOcupacaoTrechoNao;
	
	private Pricing pricing;
	private boolean salvo = false;
	private Boolean gravarNovo = Boolean.TRUE;
	private Integer buscarPricingInativosAtivos;
	
	
	private void clearCombobox(Combobox combobox) {
		int size = combobox.getItemCount();

		for (int i = size - 1; i >= 0; i--) {
			combobox.removeItemAt(i);
		}
	}

	private void popularCombobox(Combobox cmb) throws Exception {
		clearCombobox(cmb);
		for (EsquemaCorrida.GerarFeriado p : EsquemaCorrida.GerarFeriado.values()) {
			Comboitem comboItem = new Comboitem(p.descricao());
			comboItem.setValue(p.valor());
			comboItem.setParent(cmb);
		}

		cmb.setSelectedIndex(0);
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		lsEmpresa = UsuarioLogado.getUsuarioLogado().getEmpresa();

		pricing = (Pricing) Executions.getCurrent().getArg().get("pricing");
		pricingList = (MyListbox) Executions.getCurrent().getArg().get("pricingList");
		buscarPricingInativosAtivos = (Integer) Executions.getCurrent().getArg().get("buscarPricingInativosAtivos");
		gravarNovo = (Boolean) Executions.getCurrent().getArg().get("gravarNovo");

		super.doAfterCompose(comp);
		
		imgPricingDividirIdaEVolta.setTooltiptext(Labels.getLabel("editarPricingController.indPricingDividirIdaEVolta.ajuda", new Object[] {"\n"}));
		
		pricingCategoriaList.setDisabled(Boolean.TRUE);
		btnNovoCategoria.setDisabled(Boolean.TRUE);
		btnApagarCategoria.setDisabled(Boolean.TRUE);
		btnModificarCategoria.setDisabled(Boolean.TRUE);

		popularCombobox(cmbIndGeneraFeriadoFechaVenta);
		popularCombobox(cmbIndGeneraFeriadoFechaViaje);

		if (pricing.getIndGeneraFeriadoVenta() != null) {
			if (pricing.getIndGeneraFeriadoVenta().equals("S")) {
				cmbIndGeneraFeriadoFechaVenta.setSelectedIndex(0);
			} else if (pricing.getIndGeneraFeriadoVenta().equals("F")) {
				cmbIndGeneraFeriadoFechaVenta.setSelectedIndex(1);
			} else if (pricing.getIndGeneraFeriadoVenta().equals("N")) {
				cmbIndGeneraFeriadoFechaVenta.setSelectedIndex(2);
			}
		}

		if (pricing.getIndGeneraFeriadoViaje() != null) {
			if (pricing.getIndGeneraFeriadoViaje().equals("S")) {
				cmbIndGeneraFeriadoFechaViaje.setSelectedIndex(0);
			} else if (pricing.getIndGeneraFeriadoViaje().equals("F")) {
				cmbIndGeneraFeriadoFechaViaje.setSelectedIndex(1);
			} else if (pricing.getIndGeneraFeriadoViaje().equals("N")) {
				cmbIndGeneraFeriadoFechaViaje.setSelectedIndex(2);
			}
		}

		// Vigencia
		pricingVigenciaList.setItemRenderer(new RenderPricingVigencia());
		lsPricingVigencia = new ArrayList<PricingVigencia>();
		pricingVigenciaList.setData(lsPricingVigencia);

		// Importe
		pricingImporteList.setItemRenderer(new RenderPricingImporte());
		lsPricingImporte = new ArrayList<PricingImporte>();
		pricingImporteList.setData(lsPricingImporte);

		// Marca
		pricingMarcaList.setItemRenderer(new RenderPricingMarca());
		lsPricingMarca = new ArrayList<PricingMarca>();
		pricingMarcaList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingMarca pMarca = (PricingMarca) pricingMarcaList.getSelected();
				verPricingMarca(pMarca);
			}
		});

		// Clase
		pricingClaseList.setItemRenderer(new RenderPricingClase());
		lsPricingClase = new ArrayList<PricingClase>();
		pricingClaseList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingClase pClase = (PricingClase) pricingClaseList.getSelected();
				verPricingClase(pClase);
			}
		});

		// Punto Venta
		pricingPuntoVentaList.setItemRenderer(new RenderPricingPuntoVenta());
		lsPricingPuntoVenta = new ArrayList<PricingPuntoVenta>();
		pricingPuntoVentaList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingPuntoVenta pPuntoVenta = (PricingPuntoVenta) pricingPuntoVentaList.getSelected();
				verPricingPuntoVenta(pPuntoVenta);
			}
		});

		// Canal Venta
		pricingTipoPtoVtaList.setItemRenderer(new RenderPricingTipoPtoVta());
		lsPricingTipoPtoVta = new ArrayList<PricingTipoPtoVta>();
		pricingTipoPtoVtaList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingTipoPtoVta pTipoPtoVta = (PricingTipoPtoVta) pricingTipoPtoVtaList.getSelected();
				verPricingTipoPtoVta(pTipoPtoVta);
			}
		});

		// Rutas
		pricingRutaList.setItemRenderer(new RenderPricingRuta());
		lsPricingRuta = new ArrayList<PricingRuta>();
		pricingRutaList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingRuta pRuta = (PricingRuta) pricingRutaList.getSelected();
				verPricingRuta(pRuta);
			}
		});

		// Categoria
		pricingCategoriaList.setItemRenderer(new RenderPricingCategoria());
		lsPricingCategoria = new ArrayList<PricingCategoria>();

		// Mercado
		pricingMercadoList.setItemRenderer(new RenderPricingMercado());
		lsPricingMercado = new ArrayList<PricingMercado>();
		pricingMercadoList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingMercado pMercado = (PricingMercado) pricingMercadoList.getSelected();
				verPricingMercado(pMercado);
			}
		});

		// Dia
		pricingDiaList.setItemRenderer(new RenderPricingDia());
		lsPricingDia = new ArrayList<PricingDia>();
		pricingDiaList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingDia pDia = (PricingDia) pricingDiaList.getSelected();
				verPricingDia(pDia);
			}
		});

		// Corrida
		pricingCorridaList.setItemRenderer(new RenderPricingCorrida());
		lsPricingCorrida = new ArrayList<PricingCorrida>();

		// Asiento
		pricingAsientoList.setItemRenderer(new RenderPricingAsiento());
		lsPricingAsiento = new ArrayList<PricingAsiento>();
		pricingAsientoList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingAsiento pAsiento = (PricingAsiento) pricingAsientoList.getSelected();
				verPricingAsiento(pAsiento);
			}
		});

		// OcupaAntecipa
		pricingOcupaAntecipaList.setItemRenderer(new RenderPricingOcupaAntecipa());
		lsPricingOcupaAntecipa = new ArrayList<PricingOcupaAntecipa>();
		pricingOcupaAntecipaList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingOcupaAntecipa pOcupaAntecipa = (PricingOcupaAntecipa) pricingOcupaAntecipaList.getSelected();
				verPricingOcupaAntecipa(pOcupaAntecipa);
			}
		});

		// TipoServicio
		pricingTipoServicioList.setItemRenderer(new RenderPricingTipoServicio());
		lsPricingTipoServicio = new ArrayList<PricingTipoServicio>();
		pricingTipoServicioList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingTipoServicio pTipoServicio = (PricingTipoServicio) pricingTipoServicioList.getSelected();
				verPricingTipoServicio(pTipoServicio);
			}
		});

		// FormaPago
		pricingFormapagoList.setItemRenderer(new RenderPricingFormaPago());
		lsPricingFormapago = new ArrayList<PricingFormapago>();
		pricingFormapagoList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				PricingFormapago pFormapago = (PricingFormapago) pricingFormapagoList.getSelected();
				verPricingFormapago(pFormapago);
			}
		});
		
		// Tipo Pasajero - Categoria
		if (ApplicationProperties.getInstance().isCustomHabilitado(CustomEnum.HABILITAR_PRICING_CATEGORIA.getDescricao())) {
			pricingCategoriaList.setDisabled(Boolean.FALSE);
			btnNovoCategoria.setDisabled(Boolean.FALSE);
			btnApagarCategoria.setDisabled(Boolean.FALSE);
			btnModificarCategoria.setDisabled(Boolean.FALSE);
		} 
		
		lsPricingConexao = new ArrayList<>();
		
		popularComboConexoesCtrl();
		
		if (pricing.getPricingId() != null) {
			pricing = pricingService.obtenerID(pricing.getPricingId());

			lsPricingVigencia = pricing.getPricingVigenciaList();
			pricingVigenciaList.setData(lsPricingVigencia);
			if (lsPricingVigencia.size() > 0) {
				pricingVigenciaList.setSelectedIndex(0);
			}

			lsPricingImporte = pricing.getPricingImporteList();
			pricingImporteList.setData(lsPricingImporte);
			if (lsPricingImporte.size() > 0) {
				pricingImporteList.setSelectedIndex(0);
			}

			lsPricingMarca = pricing.getPricingMarcaList();
			pricingMarcaList.setData(lsPricingMarca);
			if (lsPricingMarca.size() > 0) {
				pricingMarcaList.setSelectedIndex(0);
			}

			lsPricingClase = pricing.getPricingClaseList();
			pricingClaseList.setData(lsPricingClase);
			if (lsPricingClase.size() > 0) {
				pricingClaseList.setSelectedIndex(0);
			}

			lsPricingOcupaAntecipa = pricing.getPricingOcupaAntecipaList();
			pricingOcupaAntecipaList.setData(lsPricingOcupaAntecipa);
			if (lsPricingOcupaAntecipa.size() > 0) {
				pricingOcupaAntecipaList.setSelectedIndex(0);
			}

			lsPricingTipoServicio = pricing.getPricingTipoServicioList();
			pricingTipoServicioList.setData(lsPricingTipoServicio);
			if (lsPricingTipoServicio.size() > 0) {
				pricingTipoServicioList.setSelectedIndex(0);
			}

			lsPricingFormapago = pricing.getPricingFormapagoList();
			pricingFormapagoList.setData(lsPricingFormapago);
			if (lsPricingFormapago.size() > 0) {
				pricingFormapagoList.setSelectedIndex(0);
			}

			lsPricingPuntoVenta = pricing.getPricingPuntoventaList();
			pricingPuntoVentaList.setData(lsPricingPuntoVenta);
			if (lsPricingPuntoVenta.size() > 0) {
				pricingPuntoVentaList.setSelectedIndex(0);
			}

			lsPricingCategoria = pricing.getPricingCategoriaList();
			pricingCategoriaList.setData(lsPricingCategoria);
			if (lsPricingCategoria.size() > 0) {
				pricingCategoriaList.setSelectedIndex(0);
			}

			lsPricingRuta = pricing.getPricingRutaList();
			pricingRutaList.setData(lsPricingRuta);
			if (lsPricingRuta.size() > 0) {
				pricingRutaList.setSelectedIndex(0);
			}

			lsPricingMercado = pricing.getPricingMercadoList();
			pricingMercadoList.setData(lsPricingMercado);

			lsPricingTipoPtoVta = pricing.getPricingTipoptovtaList();
			pricingTipoPtoVtaList.setData(lsPricingTipoPtoVta);
			if (lsPricingTipoPtoVta.size() > 0) {
				pricingTipoPtoVtaList.setSelectedIndex(0);
			}

			lsPricingCorrida = pricing.getPricingCorridaList();
			pricingCorridaList.setData(lsPricingCorrida);

			lsPricingDia = pricing.getPricingDiaList();
			pricingDiaList.setData(lsPricingDia);

			lsPricingAsiento = pricing.getPricingAsientoList();
			pricingAsientoList.setData(lsPricingAsiento);

			lsPricingOcupaAntecipa = pricing.getPricingOcupaAntecipaList();
			pricingOcupaAntecipaList.setData(lsPricingOcupaAntecipa);

			lsPricingTipoServicio = pricing.getPricingTipoServicioList();
			pricingTipoServicioList.setData(lsPricingTipoServicio);

			lsPricingFormapago = pricing.getPricingFormapagoList();
			pricingFormapagoList.setData(lsPricingFormapago);
			
			lsPricingConexao = pricing.getPricingConexaoList();
			
			carregarConexoesCadastradas();

			if (pricing.getIndtransferible() == Boolean.FALSE) {
				rdTransNo.setChecked(true);
			}
			if (pricing.getIndcancelable() == Boolean.FALSE) {
				rdCanceNo.setChecked(true);
			}
			if (pricing.getIndreservable() == Boolean.FALSE) {
				rdReserNo.setChecked(true);
			}
			boolean isDescPorcentagem = isDescPorcentagem();
			
			if (isDescPorcentagem) {
				radioPor.setChecked(true);
			}
			
			rdOcupacaoTrechoSim.setChecked(pricing.getIndOcupacaoPorTrecho());
			rdOcupacaoTrechoNao.setChecked(!pricing.getIndOcupacaoPorTrecho());
			chkIndSomenteAssentoSugerido.setChecked(pricing.getIndSomenteAssentoSugerido());
			chkIndPricingDividirIdaEVolta.setChecked(pricing.getIndPricingDividirIdaEVolta());
			
			if (pricing.getIndSafer() != null) {
				chkIndSafer.setChecked(pricing.getIndSafer());
			}		
			
			if (!isDescPorcentagem) {
				radioImp.setChecked(true);
				onClick$radioImp(null);
			} else {

				btnNovoImporte.setVisible(false);
				btnApagarImporte.setVisible(false);
				btnModificarImporte.setVisible(false);
			}

			// BUG-1202 não permitir alterar a empresa.
			cmbEmpresa.setDisabled(true);

			salvo = true;
		} else {

			btnNovoImporte.setVisible(false);
			btnApagarImporte.setVisible(false);
			btnModificarImporte.setVisible(false);
			descuentoporcredondo.setDisabled(false);

			pricingService.suscribir(pricing);
			// Marca - Preenchendo com o Objeto TODOS caso exista.
			Short id = -1;
			PricingMarca pm = new PricingMarca();
			Marca m = marcaService.obtenerID(id);
			if (m != null) {
				pm.setMarca(m);
				pm.setPricing(pricing);
				pm.setActivo(Pricing.ATIVO);
				pm.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pm.setFecmodif(Calendar.getInstance().getTime());
				lsPricingMarca.add(pm);
				pricingMarcaList.setData(lsPricingMarca);
				pricingMarcaService.suscribir(pm);
			}

			// Tipo de Servicio
			PricingClase pts = new PricingClase();
			ClaseServicio cs = claseServicioService.obtenerID((int)id);
			if (cs != null) {
				pts.setClaseServicio(cs);
				pts.setPricing(pricing);
				pts.setActivo(Pricing.ATIVO);
				pts.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pts.setFecmodif(Calendar.getInstance().getTime());
				lsPricingClase.add(pts);
				pricingClaseList.setData(lsPricingClase);
				pricingClaseService.suscribir(pts);
			}

			// Punto de Venta
			PricingPuntoVenta ppv = new PricingPuntoVenta();
			PuntoVenta pv = puntoVentaService.obtenerID(-1);
			if (pv != null) {
				ppv.setPuntoVenta(pv);
				ppv.setPricing(pricing);
				ppv.setActivo(Pricing.ATIVO);
				ppv.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				ppv.setFecmodif(Calendar.getInstance().getTime());
				lsPricingPuntoVenta.add(ppv);
				pricingPuntoVentaList.setData(lsPricingPuntoVenta);
				pricingPuntoVentaService.suscribir(ppv);
			}
			// Punto de Venta
			PricingTipoPtoVta ptpv = new PricingTipoPtoVta();
			TipoPuntoVenta tpv = tipoPuntoVentaService.obtenerID((short) -1);
			if (tpv != null) {
				ptpv.setTipoPtovta(tpv);
				ptpv.setPricing(pricing);
				ptpv.setActivo(Pricing.ATIVO);
				ptpv.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				ptpv.setFecmodif(Calendar.getInstance().getTime());
				lsPricingTipoPtoVta.add(ptpv);
				pricingTipoPtoVtaList.setData(lsPricingTipoPtoVta);
				pricingTipoPtoVtaService.suscribir(ptpv);
			}

			// Ruta
			PricingRuta pr = new PricingRuta();
			Ruta r = rutaService.obtenerID(-1);
			if (r != null) {
				pr.setRuta(r);
				pr.setPricing(pricing);
				pr.setActivo(Pricing.ATIVO);
				pr.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pr.setFecmodif(Calendar.getInstance().getTime());
				lsPricingRuta.add(pr);
				pricingRutaList.setData(lsPricingRuta);
				pricingRutaService.suscribir(pr);
			}

			// Mercado
			PricingMercado pme = new PricingMercado();
			Parada p1 = paradaService.obtenerID(-1);
			if (p1 != null) {
				pme.setActivo(Pricing.ATIVO);
				pme.setPricing(pricing);
				pme.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pme.setFecmodif(Calendar.getInstance().getTime());
				pme.setOrigen(p1);
				pme.setDestino(p1);

				lsPricingMercado.add(pme);
				pricingMercadoList.setData(lsPricingMercado);
				pricingMercadoService.suscribir(pme);
			}
			
			incluirCategoria(categoriaService.obtenerID(1));

			incluirCategoriasPadroes();

			btnApagar.setVisible(Boolean.FALSE);
			btnInativar.setVisible(Boolean.FALSE);
			btnAtivar.setVisible(Boolean.FALSE);
		}
	}

	private boolean isDescPorcentagem() {
		return (pricing.getDescuentoporcentaje() != null || pricing.getDescuentoporcredondo() != null)
										&& (lsPricingImporte == null || lsPricingImporte.isEmpty() || lsPricingImporte.size() < 0);
	}

	private void incluirCategoria(Categoria categoria) {
		if (categoria != null && verificarCategoriaNaoAdicionada(categoria)) {
			PricingCategoria pc = new PricingCategoria();
			pc.setCategoria(categoria);
			pc.setPricing(pricing);
			pc.setActivo(Pricing.ATIVO);
			pc.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			pc.setFecmodif(Calendar.getInstance().getTime());
			
			if(lsPricingCategoria == null) {
				lsPricingCategoria = new ArrayList<PricingCategoria>();
			}
			lsPricingCategoria.add(pc);
			pricingCategoriaList.setData(lsPricingCategoria);
			pricingCategoriaService.suscribir(pc);
		}
	}

	/** Valida se a categoria não foi adicionada */
	private boolean verificarCategoriaNaoAdicionada(Categoria categoria) {
		if(lsPricingCategoria == null || lsPricingCategoria.isEmpty()) {
			return true;
		}
		for (PricingCategoria pricingCategoria : lsPricingCategoria) {
			if(pricingCategoria.getCategoria() != null && pricingCategoria.getCategoria().equals(categoria)) {
				return false;
			}
		}
		return true;
	}

	@Override
	protected void incluirCategoriaNormal(Categoria categoriaNormal) {
		incluirCategoria(categoriaNormal);
	}

	@Override
	protected void incluirCategoriaPet(Categoria categoriaPET) {
		incluirCategoria(categoriaPET);
	}

	public void onClick$radioImp(Event ev) {
		btnNovoImporte.setVisible(true);
		btnApagarImporte.setVisible(true);
		btnModificarImporte.setVisible(true);

		descuentoporcsen.setConstraint("");
		descuentoporcsen.setDisabled(true);
		descuentoporcsen.setValue(null);
		pricing.setDescuentoporcentaje(null);

		descuentoporcredondo.setConstraint("");
		descuentoporcredondo.setValue(null);
		descuentoporcredondo.setDisabled(true);
		pricing.setDescuentoporcredondo(null);
	}

	public void onClick$radioPor(Event ev) {
		btnNovoImporte.setVisible(false);
		btnApagarImporte.setVisible(false);
		btnModificarImporte.setVisible(false);

		descuentoporcsen.setDisabled(false);
		descuentoporcredondo.setDisabled(false);

		Object[] listPImporte = pricingImporteList.getData();
		if (listPImporte != null) {
			for (Object o : listPImporte) {
				PricingImporte pImporte = (PricingImporte) o;
				lsPricingImporte.remove(pImporte);

				pImporte.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pImporte.setFecmodif(Calendar.getInstance().getTime());
				pImporte.setActivo(Pricing.INATIVO);

				lsPricingImporte.add(pImporte);
				pricingImporteList.removeItem(pImporte);
			}

			pricingImporteList.clearSelection();
			pricing.setPricingImporteList(lsPricingImporte);
		}
	}

	private boolean validarNome() throws InterruptedException {
		try {
			nombrePricing.getValue();
		} catch (WrongValueException e) {

			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.nomPricingVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.INFORMATION);

			return false;
		}

		return true;
	}

	private boolean validarObligatorios() throws InterruptedException {
		try {
			nombrePricing.getValue();
		} catch (WrongValueException e) {

			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.nomPricingVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);

			return false;
		}

		try {
			if (pricing.getEmpresa() == null) {
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.empresaVazio"),
						Labels.getLabel("editarPricingController.window.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);

				return false;
			}
			cmbEmpresa.getValue();
		} catch (WrongValueException e) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.empresaVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);

			return false;
		}
		
		if(chkIndPricingDividirIdaEVolta.isChecked() &&
		  (pricing.getDescuentoporcentaje() == null || !MoneyHelper.isIgual(BigDecimal.ZERO, pricing.getDescuentoporcentaje()))) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.indPricingDividirIdaEVolta.msg.erro"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);			
			return false;			
		}

		try {
			itxCantboleto.getValue();
		} catch (WrongValueException e) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.cantAsientosCorridasVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}

		if (pricingVigenciaList.getModel().getSize() == 0) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.vigenciasVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}

		try {
			cantdiasanticipacion.getValue();
		} catch (WrongValueException e) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.cantDiasAntecipacionVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}

		if (pricingMarcaList.getModel().getSize() == 0) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.marcaVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}
		if (pricingClaseList.getModel().getSize() == 0) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.claseVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}
		if (pricingPuntoVentaList.getModel().getSize() == 0) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.puntoVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}
		
		if (pricingRutaList.getModel().getSize() == 0) {		
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.rutaVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}
				
		//Quando existe pricing conexao permite cadastrar linha e trecho todas
		if ( (lsPricingConexao == null || lsPricingConexao.isEmpty()) && verificarContemTodasRuta() && verificarContemTodas()) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.noRutaTodasYTrechosTodos"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}
		if (pricingTipoPtoVtaList.getModel().getSize() == 0) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.tipoPtoVtaVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}

		if (pricingCategoriaList.getModel().getSize() == 0) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.categoriaVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}

		if (pricingMercadoList.getModel().getSize() == 0 && verificarContemTodasRuta()) {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.mercadoVazio"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
			return false;
		}

		return true;
	}

	private boolean verificarContemTodasRuta() {
		Integer rutaIdTodas = -1;	
		for(int i = 0; i < pricingRutaList.getModel().getSize(); i++ ){
			PricingRuta princingRuta = (PricingRuta) pricingRutaList.getModel().getElementAt(i);
			if(princingRuta.getRuta().getRutaId() == rutaIdTodas){
				return true;
			}					
		}		
		return false;
	}

	private Boolean verificarContemTodas() {
		String TODAS = "TODAS";		
		for(int i = 0; i < pricingMercadoList.getModel().getSize(); i++ ){
			PricingMercado princingMercado  = (PricingMercado) pricingMercadoList.getModel().getElementAt(i);
			if(princingMercado.getOrigen().getDescparada().equals(TODAS) || princingMercado.getDestino().getDescparada().equals(TODAS)){
				return true;
			}					
		}		
		return false;
	}

	@SuppressWarnings("unchecked")
	public void onClick$btnSalvar(Event ev) throws InterruptedException {
		if (!validarObligatorios()) {
			return;
		}
		
		if(!pricingCorridaList.getListData().isEmpty()){
			pricing.setPricingCorridaList((List<PricingCorrida>)pricingCorridaList.getListData());
		}

		BigDecimal vlrdescuentoporcsen = descuentoporcsen.getValueDecimal();
		BigDecimal vlrdescuentoporcredondo = descuentoporcredondo.getValueDecimal();

		if (cmbIndGeneraFeriadoFechaVenta.getSelectedItem() != null) {
			pricing.setIndGeneraFeriadoVenta((String) cmbIndGeneraFeriadoFechaVenta.getSelectedItem().getValue());
		}

		if (cmbIndGeneraFeriadoFechaViaje.getSelectedItem() != null) {
			pricing.setIndGeneraFeriadoViaje((String) cmbIndGeneraFeriadoFechaViaje.getSelectedItem().getValue());
		}
		chkExibePopupPricingPol.getValue();
		pricing.setIndExibePopupPricingPol(chkExibePopupPricingPol.isChecked());

		try {
			try {
				if (radioPor.isChecked()) {

					if ((vlrdescuentoporcsen != null) && (vlrdescuentoporcsen.intValue() < -100 || vlrdescuentoporcsen.intValue() >= 1000)) {
						Messagebox.show(
								Labels.getLabel("editarPricingController.msgSencillo.title"),
								Labels.getLabel("editarPricingController.window.title"),
								Messagebox.OK, Messagebox.INFORMATION);
						return;
					}

					if ((vlrdescuentoporcredondo != null) && (vlrdescuentoporcredondo.intValue() < -100 || vlrdescuentoporcredondo.intValue() >= 1000)) {
						Messagebox.show(
								Labels.getLabel("editarPricingController.msgRedondo.title"),
								Labels.getLabel("editarPricingController.window.title"),
								Messagebox.OK, Messagebox.INFORMATION);
						return;

					}
				}
			} catch (Exception ex) {
				log.error("PrincigController - descuentoporcsen.getValue() // descuentoporcredondo.getValue() : " + ex);
				Messagebox.show(
						Labels.getLabel("MSG.Error"),
						Labels.getLabel("editarPricingController.window.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);

			}

			pricing.setEmpresa((Empresa) cmbEmpresa.getSelectedItem().getValue());

			pricing.setIndtransferible(rdTransSi.isChecked());
			
			pricing.setIndcancelable(rdCanceSi.isChecked());
			
			pricing.setIndreservable(rdReserSi.isChecked());
			
			pricing.setExibeVenda(rdExibirVendaSi.isChecked());
			
			pricing.setIndOcupacaoPorTrecho(rdOcupacaoTrechoSim.isChecked());
			
			pricing.setIndSomenteAssentoSugerido(chkIndSomenteAssentoSugerido.isChecked());
			pricing.setIndSafer(chkIndSafer.isChecked());
			
			pricing.setIndPricingDividirIdaEVolta(chkIndPricingDividirIdaEVolta.isChecked());
			
			if(lsPricingConexao != null && !lsPricingConexao.isEmpty()) {
				
				Boolean existePricingAtivo = isExistePricingAtivo(lsPricingConexao);
				
				List<PricingOcupaAntecipa> listOcupaAntecipa = (List<PricingOcupaAntecipa>) pricingOcupaAntecipaList.getModel();
				
				if(existePricingAtivo) {
					if(listOcupaAntecipa == null || listOcupaAntecipa.isEmpty()) {
						Messagebox.show( Labels.getLabel("editarConexionController.msgConexaoPreencherOcupacao.value"), 
								Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
						return;
					}
					
					for(PricingOcupaAntecipa poa : listOcupaAntecipa) {
						if(poa.getPorcentaje() != null && BigDecimal.ZERO.compareTo(poa.getPorcentaje()) < 0) {
							Messagebox.show( Labels.getLabel("editarConexionController.msgConexaoOcupacaoPorcentagem.value"), 
									Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
							return;
						}
					}
				}
				
				pricing.setPricingConexaoList(lsPricingConexao);
				
				limparPesquisaPricingConexao(pricing.getPricingId());
			}
			
			if (gravarNovo) {
				List<Pricing> lsPricing =
						pricingService.buscarPorNombre(pricing.getNombPricing());

				if (lsPricing.size() == 0) {
					pricingService.actualizacion(pricing);
					gravarNovo = false;
					pricingList.addItemNovo(pricing);

					Messagebox.show(
							Labels.getLabel("editarPricingController.MSG.suscribirOK"),
							Labels.getLabel("editarPricingController.window.title"),
							Messagebox.OK, Messagebox.INFORMATION);

					salvo = true;

				} else {
					Messagebox.show(
							Labels.getLabel("MSG.Registro.Existe"),
							Labels.getLabel("editarPricingController.window.title"),
							Messagebox.OK, Messagebox.EXCLAMATION);
				}
			} else {
				
				if(!lsPricingOcupaAntecipa.isEmpty()){
					pricing.setPricingOcupaAntecipaList(new ArrayList<PricingOcupaAntecipa>((List<PricingOcupaAntecipa>) pricingOcupaAntecipaList.getModel()));			
				}
				
				pricingService.actualizacion(pricing);
				pricingList.updateItem(pricing);

				if (buscarPricingInativosAtivos.equals(Pricing.INATIVO)) {
					pricingService.inativar(pricing);
				}

				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.suscribirOK"),
						Labels.getLabel("editarPricingController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);

				salvo = true;

			}

			// Se for por porcentage eu desativo todos os importes
			if (radioPor.isChecked()) {
				pricing = pricingService.obtenerID(pricing.getPricingId());
				for (PricingImporte pi : pricing.getPricingImporteList()) {
					pricingImporteService.borrar(pi);
				}
			}
		} catch (Exception ex) {
			log.error("Error salvar pricing : " + ex);
			Messagebox.show(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	private void limparPesquisaPricingConexao(Integer pricingId) {
		try {
			if(pricingId != null) {
				conexionCtrlService.limparPesquisaPricingConexao(pricingId);
			}
		} catch (Exception e) {
			log.info("Erro ao remover pesquisa do pricing de conexao, erro: ", e);
		}
	}

	private Boolean isExistePricingAtivo(List<PricingConexao> lsPricingConexao2) {
		
		for(PricingConexao p : lsPricingConexao2) {
			if(p.getActivo().equals(Pricing.ATIVO)) {
				return true;
			}
		}
		
		return false;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void onClick$btnCopiar(Event ev) throws InterruptedException {

		Map args = new HashMap();
		args.put("pricing", pricing);

		openWindow("/gui/pricing/copiarPricing.zul",
				Labels.getLabel("copiarPricingController.window.title"), args, MODAL);

	}

	// O tratamento para deletar o Pricing (made by Monteiro) quando sair sem
	// salvar foi mudado para o evento onClose da janela.
	public void onClick$btnFechar(Event ev) {
		onClose$winEditarPricing(null);
	}

	public void onClose$winEditarPricing(Event ev) {
		if (!salvo) {
			if (pricing.getPricingId() != null) {
				pricingService.borrarFisico(pricing);
			}
		}

		closeWindow();
	}

	public void onClick$btnApagar(Event ev) throws InterruptedException {
		try {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPergunta"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			if (resp == Messagebox.YES) {
				pricingService.borrar(pricing);
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.borrarOK"),
						Labels.getLabel("editarPricingController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);

				pricingList.removeItem(pricing);

				closeWindow();
			}
		} catch (Exception ex) {
			log.error("Erro apagar pricing : " + ex);
			Messagebox.show(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnModificarVigencia(Event ev) {
		PricingVigencia pVigencia = (PricingVigencia) pricingVigenciaList.getSelected();

		if (pVigencia != null) {
			verPricingVigencia(pVigencia);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.windowVigencia.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarVigencia(Event ev) throws Exception {
		List<Object> listPVigencia = pricingVigenciaList.getSelectedsItens();
		if (listPVigencia != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaVigencia"),
					Labels.getLabel("editarPricingController.windowVigencia.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				for (Object o : listPVigencia) {

					pricing = pricingService.obtenerID(pricing.getPricingId());
					lsPricingVigencia = pricing.getPricingVigenciaList();

					PricingVigencia pVigencia = (PricingVigencia) o;
					lsPricingVigencia.remove(pVigencia);

					pVigencia.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					pVigencia.setFecmodif(Calendar.getInstance().getTime());
					pVigencia.setActivo(Pricing.EXCLUIDO);

					lsPricingVigencia.add(pVigencia);
					pricingVigenciaList.removeItem(pVigencia);

					pricing.setPricingVigenciaList(lsPricingVigencia);

					pricingService.actualizacion(pricing);
					gravarNovo = false;
					lsPricingVigencia.clear();
				}
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoVigencia(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}
		PricingVigencia pVigencia = new PricingVigencia();
		pVigencia.setPricing(pricing);

		verPricingVigencia(pVigencia);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingVigencia(PricingVigencia pVigencia) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingVigencia", pVigencia);
		args.put("pricing", pricing);
		args.put("pricingVigenciaList", pricingVigenciaList);

		openWindow("/gui/pricing/editarPricingVigencia.zul",
				Labels.getLabel("editarPricingController.vigencia.value"), args, MODAL);
	}

	public void onClick$btnApagarImporte(Event ev) throws Exception {
		List<Object> listPImporte = pricingImporteList.getSelectedsItens();
		if (listPImporte != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaImporte"),
					Labels.getLabel("editarPricingController.windowImporte.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				for (Object o : listPImporte) {
					PricingImporte pImporte = (PricingImporte) o;
					lsPricingImporte.remove(pImporte);

					pImporte.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					pImporte.setFecmodif(Calendar.getInstance().getTime());
					pImporte.setActivo(Pricing.EXCLUIDO);

					lsPricingImporte.add(pImporte);
					pricingImporteList.removeItem(pImporte);
				}

				pricingImporteList.clearSelection();
				pricing.setPricingImporteList(lsPricingImporte);
				pricingService.actualizacion(pricing);
				gravarNovo = false;
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.windowImporte.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoImporte(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingImporte pImporte = new PricingImporte();
		pImporte.setPricing(pricing);
		verPricingImporte(pImporte);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingImporte(PricingImporte pImporte) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingImporte", pImporte);
		args.put("pricingImporteList", pricingImporteList);

		openWindow("/gui/pricing/editarPricingImporte.zul",
				Labels.getLabel("editarPricingController.windowImporte.title"), args, MODAL);
	}

	public void onClick$btnModificarImporte(Event ev) throws InterruptedException {
		PricingImporte pImporte = (PricingImporte) pricingImporteList.getSelected();

		if (pImporte != null) {
			verPricingImporte(pImporte);
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.windowImporte.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnApagarMarca(Event ev) throws Exception {
		List<Object> listPMarca = pricingMarcaList.getSelectedsItens();
		if (listPMarca != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaMarca"),
					Labels.getLabel("editarPricingController.windowMarca.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				for (Object o : listPMarca) {
					PricingMarca pMarca = (PricingMarca) o;

					lsPricingMarca.remove(pMarca);

					pMarca.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					pMarca.setFecmodif(Calendar.getInstance().getTime());
					pMarca.setActivo(Pricing.EXCLUIDO);

					lsPricingMarca.add(pMarca);
					pricingMarcaList.removeItem(pMarca);
					pricingMarcaList.clearSelection();

					pricing.setPricingMarcaList(lsPricingMarca);
					pricingService.actualizacion(pricing);
					gravarNovo = false;
				}
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoMarca(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingMarca pMarca = new PricingMarca();
		pMarca.setPricing(pricing);
		verPricingMarca(pMarca);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingMarca(PricingMarca pMarca) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingMarca", pMarca);
		args.put("pricingMarcaList", pricingMarcaList);

		openWindow("/gui/pricing/editarPricingMarca.zul",
				Labels.getLabel("editarPricingController.windowMarca.title"), args, MODAL);
	}

	public void onClick$btnModificarMarca(Event ev) {
		PricingMarca pMarca = (PricingMarca) pricingMarcaList.getSelected();

		if (pMarca != null) {
			verPricingMarca(pMarca);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarClase(Event ev) throws Exception {
		List<Object> listPClase = pricingClaseList.getSelectedsItens();

		if (listPClase != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaClase"),
					Labels.getLabel("editarPricingController.windowClase.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				for (Object o : listPClase) {

					PricingClase pClase = (PricingClase) o;

					lsPricingClase.remove(pClase);

					pClase.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					pClase.setFecmodif(Calendar.getInstance().getTime());
					pClase.setActivo(Pricing.EXCLUIDO);

					lsPricingClase.add(pClase);
					pricingClaseList.removeItem(pClase);
					pricingClaseList.clearSelection();

					pricing.setPricingClaseList(lsPricingClase);
					pricingService.actualizacion(pricing);
					gravarNovo = false;
				}
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoClase(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingClase pClase = new PricingClase();
		pClase.setPricing(pricing);
		verPricingClase(pClase);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingClase(PricingClase pClase) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingClase", pClase);
		args.put("pricingClaseList", pricingClaseList);

		openWindow("/gui/pricing/editarPricingClase.zul",
				Labels.getLabel("editarPricingController.windowTipoServicio.title"), args, MODAL);
	}

	public void onClick$btnModificarClase(Event ev) {
		PricingClase pClase = (PricingClase) pricingClaseList.getSelected();

		if (pClase != null) {
			verPricingClase(pClase);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarPuntoVenta(Event ev) throws Exception {
		List<Object> listPuntoVenta = pricingPuntoVentaList.getSelectedsItens();
		if (listPuntoVenta != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaPuntoVenta"),
					Labels.getLabel("editarPricingController.windowPuntoVenta.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {

				for (Object o : listPuntoVenta) {

					PricingPuntoVenta pPuntoVenta = (PricingPuntoVenta) o;
					lsPricingPuntoVenta.remove(pPuntoVenta);

					pPuntoVenta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					pPuntoVenta.setFecmodif(Calendar.getInstance().getTime());
					pPuntoVenta.setActivo(Pricing.EXCLUIDO);

					lsPricingPuntoVenta.add(pPuntoVenta);
					pricingPuntoVentaList.removeItem(pPuntoVenta);
					pricingPuntoVentaList.clearSelection();

					pricing.setPricingPuntoventaList(lsPricingPuntoVenta);
					pricingService.actualizacion(pricing);
					gravarNovo = false;
				}
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoPuntoVenta(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingPuntoVenta pPuntoVenta = new PricingPuntoVenta();
		pPuntoVenta.setPricing(pricing);
		verPricingPuntoVenta(pPuntoVenta);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingPuntoVenta(PricingPuntoVenta pPuntoVenta) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingPuntoVenta", pPuntoVenta);
		args.put("pricingPuntoVentaList", pricingPuntoVentaList);

		openWindow("/gui/pricing/editarPricingPuntoVenta.zul",
				Labels.getLabel("editarPricingController.windowPuntoVenta.title"), args, MODAL);
	}

	public void onClick$btnModificarPuntoVenta(Event ev) {
		PricingPuntoVenta pPuntoVenta = (PricingPuntoVenta) pricingPuntoVentaList.getSelected();

		if (pPuntoVenta != null) {
			verPricingPuntoVenta(pPuntoVenta);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarTipoPtoVta(Event ev) throws Exception {
		List<Object> listTipoPtoVta = pricingTipoPtoVtaList.getSelectedsItens();
		if (listTipoPtoVta != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaTipoPtoVta"),
					Labels.getLabel("editarPricingController.windowTipoPtoVta.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				for (Object r : listTipoPtoVta) {
					PricingTipoPtoVta pTipoPtoVta = (PricingTipoPtoVta) r;

					lsPricingTipoPtoVta.remove(pTipoPtoVta);
					pTipoPtoVta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					pTipoPtoVta.setFecmodif(Calendar.getInstance().getTime());
					pTipoPtoVta.setActivo(Pricing.EXCLUIDO);

					lsPricingTipoPtoVta.add(pTipoPtoVta);
					pricingTipoPtoVtaList.removeItem(pTipoPtoVta);
					pricingTipoPtoVtaList.clearSelection();

					pricing.setPricingTipoptovtaList(lsPricingTipoPtoVta);
					pricingService.actualizacion(pricing);
					gravarNovo = false;
				}
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoTipoPtoVta(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingTipoPtoVta pTipoPtoVta = new PricingTipoPtoVta();
		pTipoPtoVta.setPricing(pricing);
		verPricingTipoPtoVta(pTipoPtoVta);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingTipoPtoVta(PricingTipoPtoVta pTipoPtoVta) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingTipoPtoVta", pTipoPtoVta);
		args.put("pricingTipoPtoVtaList", pricingTipoPtoVtaList);

		openWindow("/gui/pricing/editarPricingTipoPtoVta.zul",
				Labels.getLabel("editarPricingController.windowTipoPtoVta.title"), args, MODAL);
	}

	public void onClick$btnModificarTipoPtoVta(Event ev) {
		PricingTipoPtoVta pTipoPtoVta = (PricingTipoPtoVta) pricingTipoPtoVtaList.getSelected();

		if (pTipoPtoVta != null) {
			verPricingTipoPtoVta(pTipoPtoVta);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarCategoria(Event ev) throws Exception {
		List<Object> listPCategoria = pricingCategoriaList.getSelectedsItens();
		if (listPCategoria != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaCategoria"),
					Labels.getLabel("editarPricingController.windowCategoria.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				for (Object o : listPCategoria) {
					PricingCategoria pCategoria = (PricingCategoria) o;
					lsPricingCategoria.remove(pCategoria);

					pCategoria.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					pCategoria.setFecmodif(Calendar.getInstance().getTime());
					pCategoria.setActivo(Pricing.EXCLUIDO);

					lsPricingCategoria.add(pCategoria);
					pricingCategoriaList.removeItem(pCategoria);
					pricingCategoriaList.clearSelection();

					pricing.setPricingCategoriaList(lsPricingCategoria);
					pricingService.actualizacion(pricing);
					gravarNovo = false;
				}
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoCategoria(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingCategoria pCategoria = new PricingCategoria();
		pCategoria.setPricing(pricing);
		verPricingCategoria(pCategoria);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingCategoria(PricingCategoria pCategoria) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingCategoria", pCategoria);
		args.put("pricingCategoriaList", pricingCategoriaList);

		openWindow("/gui/pricing/editarPricingCategoria.zul",
				Labels.getLabel("editarPricingController.windowCategoria.title"), args, MODAL);
	}

	public void onClick$btnModificarCategoria(Event ev) {
		PricingCategoria pCategoria = (PricingCategoria) pricingCategoriaList.getSelected();

		if (pCategoria != null) {
			verPricingCategoria(pCategoria);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarRuta(Event ev) throws Exception {
		List<Object> listRuta = pricingRutaList.getSelectedsItens();
		if (listRuta != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaRuta"),
					Labels.getLabel("editarPricingController.windowRuta.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				for (Object r : listRuta) {
					PricingRuta pRuta = (PricingRuta) r;

					lsPricingRuta.remove(pRuta);

					pRuta.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					pRuta.setFecmodif(Calendar.getInstance().getTime());
					pRuta.setActivo(Pricing.EXCLUIDO);

					lsPricingRuta.add(pRuta);
					pricingRutaList.removeItem(pRuta);

				}
				pricingRutaList.clearSelection();
				pricing.setPricingRutaList(lsPricingRuta);
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoRuta(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingRuta pRuta = new PricingRuta();
		pRuta.setPricing(pricing);
		verPricingRuta(pRuta);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingRuta(PricingRuta pRuta) throws InterruptedException {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

			if(pricing.getEmpresa() != null){
				Map args = new HashMap();
				args.put("pricingRuta", pRuta);
				args.put("pricingRutaList", pricingRutaList);
		
				openWindow("/gui/pricing/editarPricingRuta.zul",
						Labels.getLabel("editarPricingController.windowRuta.title"), args, MODAL);

			}else{
				Messagebox.show(
						Labels.getLabel("editarPuntoVentaController.MSG.empresa"),
						Labels.getLabel("editarPricingController.windowRuta.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
	}

	public void onClick$btnModificarRuta(Event ev) throws InterruptedException {
		PricingRuta pRuta = (PricingRuta) pricingRutaList.getSelected();

		if (pRuta != null) {
			verPricingRuta(pRuta);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarMercado(Event ev) throws Exception {
		PricingMercado pMercado = (PricingMercado) pricingMercadoList.getSelected();
		if (pMercado != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaMercado"),
					Labels.getLabel("editarPricingController.windowMercado.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				lsPricingMercado.remove(pMercado);

				pMercado.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pMercado.setFecmodif(Calendar.getInstance().getTime());
				pMercado.setActivo(Pricing.EXCLUIDO);

				lsPricingMercado.add(pMercado);
				pricingMercadoList.removeItem(pMercado);
				pricingMercadoList.clearSelection();

				pricing.setPricingMercadoList(lsPricingMercado);
				pricingService.actualizacion(pricing);
				gravarNovo = false;
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoMercado(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingMercado pMercado = new PricingMercado();
		pMercado.setPricing(pricing);

		verPricingMercado(pMercado);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingMercado(PricingMercado pMercado) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingMercado", pMercado);
		args.put("pricingMercadoList", pricingMercadoList);

		args.put("lsPricingConexao", lsPricingConexao);
		args.put("pricingRutaList", pricingRutaList);

		openWindowPosi("/gui/pricing/editarPricingMercado.zul",
				Labels.getLabel("editarPricingController.windowMercado.title"), args, MODAL, "center,top");
	}

	public void onClick$btnModificarMercado(Event ev) {
		PricingMercado pMercado = (PricingMercado) pricingMercadoList.getSelected();

		if (pMercado != null) {
			verPricingMercado(pMercado);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarCorrida(Event ev) throws Exception {
		PricingCorrida pCorrida = (PricingCorrida) pricingCorridaList.getSelected();
		if (pCorrida != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaCorrida"),
					Labels.getLabel("editarPricingController.windowCorrida.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			if (resp == Messagebox.YES) {
				pCorrida.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pCorrida.setFecmodif(Calendar.getInstance().getTime());
				pCorrida.setActivo(Pricing.EXCLUIDO);

				pricing.setPricingCorridaList(lsPricingCorrida);
				pricingService.actualizacion(pricing);
				gravarNovo = false;

				lsPricingCorrida.remove(pCorrida);
				pricingCorridaList.removeItem(pCorrida);
				pricingCorridaList.clearSelection();
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoCorrida(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		verPricingCorrida();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingCorrida() {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		List<PricingCorrida> lsCorridas = (List<PricingCorrida>) pricingCorridaList.getListData(); //pricing.getPricingCorridaList();
		// Hibernate.initialize(pricing.getPricingCorridaList()); não funcionou. Tive que carregar a lista numa variavel e colocar no map.
		
		Map args = new HashMap();
		args.put("pricing", pricing);
		args.put("lsCorridas", lsCorridas);
		args.put("pricingCorridaList", pricingCorridaList);

		openWindow("/gui/pricing/editarPricingCorrida.zul",
				Labels.getLabel("editarPricingController.windowCorrida.title"), args, MODAL);
	}

	public void onClick$btnModificarCorrida(Event ev) {
		PricingCorrida pCorrida = (PricingCorrida) pricingCorridaList.getSelected();

		if (pCorrida != null) {
			verPricingCorrida();
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("editarPricingController.window.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarDia(Event ev) throws Exception {
		PricingDia pDia = (PricingDia) pricingDiaList.getSelected();
		if (pDia != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaDia"),
					Labels.getLabel("editarPricingController.windowDia.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				lsPricingDia.remove(pDia);

				pDia.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pDia.setFecmodif(Calendar.getInstance().getTime());
				pDia.setActivo(Pricing.EXCLUIDO);

				lsPricingDia.add(pDia);
				pricingDiaList.removeItem(pDia);
				pricingDiaList.clearSelection();

				pricing.setPricingDiaList(lsPricingDia);
				pricingService.actualizacion(pricing);
				gravarNovo = false;
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoDia(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingDia pDia = new PricingDia();
		pDia.setPricing(pricing);
		verPricingDia(pDia);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingDia(PricingDia pDia) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingDia", pDia);
		args.put("pricingDiaList", pricingDiaList);

		openWindow("/gui/pricing/editarPricingDia.zul",
				Labels.getLabel("editarPricingController.windowDia.title"), args, MODAL);
	}

	public void onClick$btnModificarDia(Event ev) {
		PricingDia pDia = (PricingDia) pricingDiaList.getSelected();

		if (pDia != null) {
			verPricingDia(pDia);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarOcupaAntecipa(Event ev) throws Exception {
		PricingOcupaAntecipa pOcupaAntecipa = (PricingOcupaAntecipa) pricingOcupaAntecipaList.getSelected();

		if (pOcupaAntecipa != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaOcupacion"),
					Labels.getLabel("editarPricingController.windowOcupacion.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				lsPricingOcupaAntecipa.remove(pOcupaAntecipa);

				pOcupaAntecipa.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pOcupaAntecipa.setFecmodif(Calendar.getInstance().getTime());
				pOcupaAntecipa.setActivo(Pricing.EXCLUIDO);

				lsPricingOcupaAntecipa.add(pOcupaAntecipa);
				pricingOcupaAntecipaList.removeItem(pOcupaAntecipa);
				pricingOcupaAntecipaList.clearSelection();

				pricing.setPricingOcupaAntecipaList(lsPricingOcupaAntecipa);
				pricingService.actualizacion(pricing);
				gravarNovo = false;
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoOcupaAntecipa(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingOcupaAntecipa pOcupaAntecipa = new PricingOcupaAntecipa();
		pOcupaAntecipa.setPricing(pricing);
		verPricingOcupaAntecipa(pOcupaAntecipa);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingOcupaAntecipa(PricingOcupaAntecipa pOcupaAntecipa) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingOcupaAntecipa", pOcupaAntecipa);
		args.put("pricingOcupaAntecipaList", pricingOcupaAntecipaList);

		openWindow("/gui/pricing/editarPricingOcupaAntecipa.zul",
				Labels.getLabel("editarPricingController.windowOcupacion.title"), args, MODAL);
		
		
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingOcupaAntecipaAutomatico() {

		Map args = new HashMap();
		args.put("pricing", pricing);
        args.put("clasePricingOcupaAntecipaList", (List<PricingOcupaAntecipa>)pricingOcupaAntecipaList.getListData());
        args.put("pricingOcupaAntecipaList", pricingOcupaAntecipaList);
        
        openWindow("/gui/pricing/editarPricingOcupaAntecipaAutomatico.zul",
				Labels.getLabel("editarPricingAutomaticoController.windowOcupacion.title"), args, MODAL);
        
        
	}
	
	public void onClick$btnModificarOcupaAntecipa(Event ev) {
		PricingOcupaAntecipa pOcupaAntecipa = (PricingOcupaAntecipa) pricingOcupaAntecipaList.getSelected();

		if (pOcupaAntecipa != null) {
			verPricingOcupaAntecipa(pOcupaAntecipa);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}
	
	public void onClick$btnOcupaAntecipaAutomatico(Event ev) {
		verPricingOcupaAntecipaAutomatico();
	}

	public void onClick$btnApagarAsiento(Event ev) throws Exception {
		PricingAsiento pAsiento = (PricingAsiento) pricingAsientoList.getSelected();

		if (pAsiento != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaAsiento"),
					Labels.getLabel("editarPricingController.windowAsiento.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				lsPricingAsiento.remove(pAsiento);

				pAsiento.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pAsiento.setFecmodif(Calendar.getInstance().getTime());
				pAsiento.setActivo(Pricing.EXCLUIDO);

				lsPricingAsiento.add(pAsiento);
				pricingAsientoList.removeItem(pAsiento);
				pricingAsientoList.clearSelection();

				pricing.setPricingAsientoList(lsPricingAsiento);
				pricingService.actualizacion(pricing);
				gravarNovo = false;
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoAsiento(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingAsiento pAsiento = new PricingAsiento();
		pAsiento.setPricing(pricing);
		verPricingAsiento(pAsiento);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingAsiento(PricingAsiento pAsiento) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingAsiento", pAsiento);
		args.put("pricingAsientoList", pricingAsientoList);

		openWindow("/gui/pricing/editarPricingAsiento.zul",
				Labels.getLabel("editarPricingController.windowAsiento.title"), args, MODAL);
	}

	public void onClick$btnModificarAsiento(Event ev) {
		PricingAsiento pAsiento = (PricingAsiento) pricingAsientoList.getSelected();

		if (pAsiento != null) {
			verPricingAsiento(pAsiento);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	public void onClick$btnApagarTipoServicio(Event ev) throws Exception {
		PricingTipoServicio pTipoServicio = (PricingTipoServicio) pricingTipoServicioList.getSelected();

		if (pTipoServicio != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaTipoServicio"),
					Labels.getLabel("editarPricingController.windowTipoServicio.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				lsPricingTipoServicio.remove(pTipoServicio);

				pTipoServicio.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pTipoServicio.setFecmodif(Calendar.getInstance().getTime());
				pTipoServicio.setActivo(Pricing.EXCLUIDO);

				lsPricingTipoServicio.add(pTipoServicio);
				pricingTipoServicioList.removeItem(pTipoServicio);
				pricingTipoServicioList.clearSelection();

				pricing.setPricingTipoServicioList(lsPricingTipoServicio);
				pricingService.actualizacion(pricing);
				gravarNovo = false;
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoTipoServicio(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingTipoServicio pTipoServicio = new PricingTipoServicio();
		pTipoServicio.setPricing(pricing);
		verPricingTipoServicio(pTipoServicio);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingTipoServicio(PricingTipoServicio pTipoServicio) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingTipoServicio", pTipoServicio);
		args.put("pricingTipoServicioList", pricingTipoServicioList);

		openWindow("/gui/pricing/editarPricingTipoServicio.zul",
				Labels.getLabel("editarPricingController.windowTipoServicio.title"), args, MODAL);
	}

	public void onClick$btnModificarTipoServicio(Event ev) {
		PricingTipoServicio pTipoServicio = (PricingTipoServicio) pricingTipoServicioList.getSelected();

		if (pTipoServicio != null) {
			verPricingTipoServicio(pTipoServicio);
		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				log.error(ex.getMessage());
			}
		}
	}

	public void onClick$btnApagarFormaPago(Event ev) throws Exception {
		PricingFormapago pFormapago = (PricingFormapago) pricingFormapagoList.getSelected();

		if (pFormapago != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaFormaPago"),
					Labels.getLabel("editarPricingController.windowFormaPago.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
			if (resp == Messagebox.YES) {
				lsPricingFormapago.remove(pFormapago);

				pFormapago.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pFormapago.setFecmodif(Calendar.getInstance().getTime());
				pFormapago.setActivo(Pricing.EXCLUIDO);

				lsPricingFormapago.add(pFormapago);
				pricingFormapagoList.removeItem(pFormapago);

				pricingFormapagoList.clearSelection();

				pricing.setPricingFormapagoList(lsPricingFormapago);
				pricingService.actualizacion(pricing);

				gravarNovo = false;
				
				lsPricingFormapago = pricing.getPricingFormapagoList();
				pricingFormapagoList.setData(lsPricingFormapago);

			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"),
					Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnNovoFormaPago(Event ev) throws InterruptedException {
		if (!validarNome()) {
			return;
		}

		PricingFormapago pFormapago = new PricingFormapago();
		pFormapago.setPricing(pricing);
		verPricingFormapago(pFormapago);
	}

	public void onClick$btnModificarFormaPago(Event ev) {
		PricingFormapago pFormapago = (PricingFormapago) pricingFormapagoList.getSelected();

		if (pFormapago != null) {
			verPricingFormapago(pFormapago);

		} else {
			try {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			} catch (InterruptedException ex) {
				LogManager.getLogger(EditarPricingController.class).log(Level.FATAL, "", ex);
			}
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verPricingFormapago(PricingFormapago pFormapago) {
		if (pricing.getPricingId() == null) {
			pricingService.suscribir(pricing);
		}

		Map args = new HashMap();
		args.put("pricingFormapago", pFormapago);
		args.put("pricingFormapagoList", pricingFormapagoList);

		openWindow("/gui/pricing/editarPricingFormapago.zul",
				Labels.getLabel("editarPricingController.windowFormaPago.title"), args, MODAL);
	}

	public void onClick$btnInativar(Event ev) throws InterruptedException {
		try {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.inativarPergunta"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			if (resp == Messagebox.YES) {
				pricingService.inativar(pricing);
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.inativarOK"),
						Labels.getLabel("editarPricingController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);

				pricingList.removeItem(pricing);

				closeWindow();
			}
		} catch (Exception ex) {
			log.error("Erro inativar pricing : " + ex);
			Messagebox.show(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnAtivar(Event ev) throws InterruptedException {
		try {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.ativarPergunta"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			if (resp == Messagebox.YES) {
				pricingService.ativar(pricing);
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.ativarOK"),
						Labels.getLabel("editarPricingController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);

				ativarInativarComponentes(Boolean.FALSE);

				pricingList.removeItem(pricing);
				closeWindow();
			}
		} catch (Exception ex) {
			log.error("Erro ativar pricing : " + ex);
			Messagebox.show(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	private void ativarInativarComponentes(Boolean desativar) {
		pricingVigenciaList.setDisabled(desativar);
		pricingImporteList.setDisabled(desativar);
		pricingMarcaList.setDisabled(desativar);
		pricingClaseList.setDisabled(desativar);
		pricingPuntoVentaList.setDisabled(desativar);
		pricingTipoPtoVtaList.setDisabled(desativar);
		pricingRutaList.setDisabled(desativar);
		pricingMercadoList.setDisabled(desativar);
		pricingDiaList.setDisabled(desativar);
		pricingCorridaList.setDisabled(desativar);
		pricingAsientoList.setDisabled(desativar);
		pricingOcupaAntecipaList.setDisabled(desativar);
		pricingTipoServicioList.setDisabled(desativar);
		pricingFormapagoList.setDisabled(desativar);
		btnNovoVigencia.setDisabled(desativar);
		btnApagarVigencia.setDisabled(desativar);
		btnModificarVigencia.setDisabled(desativar);
		btnNovoImporte.setDisabled(desativar);
		btnApagarImporte.setDisabled(desativar);
		btnModificarImporte.setDisabled(desativar);
		btnNovoMarca.setDisabled(desativar);
		btnApagarMarca.setDisabled(desativar);
		btnModificarMarca.setDisabled(desativar);
		btnNovoClase.setDisabled(desativar);
		btnApagarClase.setDisabled(desativar);
		btnModificarClase.setDisabled(desativar);
		btnNovoPuntoVenta.setDisabled(desativar);
		btnApagarPuntoVenta.setDisabled(desativar);
		btnModificarPuntoVenta.setDisabled(desativar);
		btnNovoRuta.setDisabled(desativar);
		btnApagarRuta.setDisabled(desativar);
		btnModificarRuta.setDisabled(desativar);
		btnNovoTipoPtoVta.setDisabled(desativar);
		btnApagarTipoPtoVta.setDisabled(desativar);
		btnModificarTipoPtoVta.setDisabled(desativar);
		btnNovoMercado.setDisabled(desativar);
		btnApagarMercado.setDisabled(desativar);
		btnModificarMercado.setDisabled(desativar);
		btnNovoDia.setDisabled(desativar);
		btnApagarDia.setDisabled(desativar);
		btnNovoCorrida.setDisabled(desativar);
		btnApagarCorrida.setDisabled(desativar);
		btnNovoAsiento.setDisabled(desativar);
		btnApagarAsiento.setDisabled(desativar);
		btnModificarAsiento.setDisabled(desativar);
		btnNovoOcupaAntecipa.setDisabled(desativar);
		btnApagarOcupaAntecipa.setDisabled(desativar);
		btnModificarOcupaAntecipa.setDisabled(desativar);
		btnNovoTipoServicio.setDisabled(desativar);
		btnApagarTipoServicio.setDisabled(desativar);
		btnModificarTipoServicio.setDisabled(desativar);
		btnNovoFormaPago.setDisabled(desativar);
		btnApagarFormaPago.setDisabled(desativar);
		btnModificarFormaPago.setDisabled(desativar);
		
		btnCopiar.setDisabled(desativar);
		btnSalvar.setDisabled(desativar);
	}

	public List<Empresa> getLsEmpresa() {
		return lsEmpresa;
	}

	public void setLsEmpresa(List<Empresa> lsEmpresa) {
		this.lsEmpresa = lsEmpresa;
	}

	public Pricing getPricing() {
		return pricing;
	}

	public void setPricing(Pricing pricing) {
		this.pricing = pricing;
	}

	public MyListbox getPricingList() {
		return pricingList;
	}

	public void setPricingList(MyListbox pricingList) {
		this.pricingList = pricingList;
	}

	public PricingService getPricingService() {
		return pricingService;
	}

	public void setPricingService(PricingService pricingService) {
		this.pricingService = pricingService;
	}

	public List<PricingVigencia> getLsPricingVigencia() {
		return lsPricingVigencia;
	}

	public void setLsPricingVigencia(List<PricingVigencia> lsPricingVigencia) {
		this.lsPricingVigencia = lsPricingVigencia;
	}

	public Combobox getCmbEmpresa() {
		return cmbEmpresa;
	}

	public void setCmbEmpresa(Combobox cmbEmpresa) {
		this.cmbEmpresa = cmbEmpresa;
	}

	public List<PricingAsiento> getLsPricingAsiento() {
		return lsPricingAsiento;
	}

	public void setLsPricingAsiento(List<PricingAsiento> lsPricingAsiento) {
		this.lsPricingAsiento = lsPricingAsiento;
	}

	public List<PricingCategoria> getLsPricingCategoria() {
		return lsPricingCategoria;
	}

	public void setLsPricingCategoria(List<PricingCategoria> lsPricingCategoria) {
		this.lsPricingCategoria = lsPricingCategoria;
	}

	public List<PricingClase> getLsPricingClase() {
		return lsPricingClase;
	}

	public void setLsPricingClase(List<PricingClase> lsPricingClase) {
		this.lsPricingClase = lsPricingClase;
	}

	public List<PricingDia> getLsPricingDia() {
		return lsPricingDia;
	}

	public void setLsPricingDia(List<PricingDia> lsPricingDia) {
		this.lsPricingDia = lsPricingDia;
	}

	public List<PricingImporte> getLsPricingImporte() {
		return lsPricingImporte;
	}

	public void setLsPricingImporte(List<PricingImporte> lsPricingImporte) {
		this.lsPricingImporte = lsPricingImporte;
	}

	public List<PricingMarca> getLsPricingMarca() {
		return lsPricingMarca;
	}

	public void setLsPricingMarca(List<PricingMarca> lsPricingMarca) {
		this.lsPricingMarca = lsPricingMarca;
	}

	public List<PricingMercado> getLsPricingMercado() {
		return lsPricingMercado;
	}

	public void setLsPricingMercado(List<PricingMercado> lsPricingMercado) {
		this.lsPricingMercado = lsPricingMercado;
	}

	public List<PricingRuta> getLsPricingRuta() {
		return lsPricingRuta;
	}

	public void setLsPricingRuta(List<PricingRuta> lsPricingRuta) {
		this.lsPricingRuta = lsPricingRuta;
	}

	public List<PricingOcupaAntecipa> getLsPricingOcupaAntecipa() {
		return lsPricingOcupaAntecipa;
	}

	public void setLsPricingOcupaAntecipa(List<PricingOcupaAntecipa> lsPricingOcupaAntecipa) {
		this.lsPricingOcupaAntecipa = lsPricingOcupaAntecipa;
	}

	public MyListbox getPricingImporteList() {
		return pricingImporteList;
	}

	public void setPricingImporteList(MyListbox pricingImporteList) {
		this.pricingImporteList = pricingImporteList;
	}

	public MyListbox getPricingVigenciaList() {
		return pricingVigenciaList;
	}

	public void setPricingVigenciaList(MyListbox pricingVigenciaList) {
		this.pricingVigenciaList = pricingVigenciaList;
	}

	public Radio getRdCanceNo() {
		return rdCanceNo;
	}

	public void setRdCanceNo(Radio rdCanceNo) {
		this.rdCanceNo = rdCanceNo;
	}

	public Radio getRdCanceSi() {
		return rdCanceSi;
	}

	public void setRdCanceSi(Radio rdCanceSi) {
		this.rdCanceSi = rdCanceSi;
	}

	public Radio getRdReserNo() {
		return rdReserNo;
	}

	public void setRdReserNo(Radio rdReserNo) {
		this.rdReserNo = rdReserNo;
	}

	public Radio getRdReserSi() {
		return rdReserSi;
	}

	public void setRdReserSi(Radio rdReserSi) {
		this.rdReserSi = rdReserSi;
	}

	public Radio getRdTransNo() {
		return rdTransNo;
	}

	public void setRdTransNo(Radio rdTransNo) {
		this.rdTransNo = rdTransNo;
	}

	public Radio getRdTransSi() {
		return rdTransSi;
	}

	public void setRdTransSi(Radio rdTransSi) {
		this.rdTransSi = rdTransSi;
	}

	public Button getBtnApagar() {
		return btnApagar;
	}

	public void setBtnApagar(Button btnApagar) {
		this.btnApagar = btnApagar;
	}

	public Button getBtnApagarImporte() {
		return btnApagarImporte;
	}

	public void setBtnApagarImporte(Button btnApagarImporte) {
		this.btnApagarImporte = btnApagarImporte;
	}

	public Button getBtnModificarImporte() {
		return btnModificarImporte;
	}

	public void setBtnModificarImporte(Button btnModificarImporte) {
		this.btnModificarImporte = btnModificarImporte;
	}

	public Button getBtnNovoImporte() {
		return btnNovoImporte;
	}

	public void setBtnNovoImporte(Button btnNovoImporte) {
		this.btnNovoImporte = btnNovoImporte;
	}

	public Radio getRadioImp() {
		return radioImp;
	}

	public void setRadioImp(Radio radioImp) {
		this.radioImp = radioImp;
	}

	public Radio getRadioPor() {
		return radioPor;
	}

	public void setRadioPor(Radio radioPor) {
		this.radioPor = radioPor;
	}

	public Radio getRdExibirVendaSi() {
		return rdExibirVendaSi;
	}

	public void setRdExibirVendaSi(Radio rdExibirVendaSi) {
		this.rdExibirVendaSi = rdExibirVendaSi;
	}

	public Radio getRdExibirVendaNo() {
		return rdExibirVendaNo;
	}

	public void setRdExibirVendaNo(Radio rdExibirVendaNo) {
		this.rdExibirVendaNo = rdExibirVendaNo;
	}

	public boolean isFormaPagoPricingInativo(){
		return ApplicationProperties.getInstance().isCustomHabilitado(CustomEnum.IS_FORMAPAGO_PRICING_INATIVO.getDescricao(), "1");
	}

	public Radio getRdOcupacaoTrechoSim() {
		return rdOcupacaoTrechoSim;
	}

	public void setRdOcupacaoTrechoSim(Radio rdOcupacaoTrechoSim) {
		this.rdOcupacaoTrechoSim = rdOcupacaoTrechoSim;
	}

	public Radio getRdOcupacaoTrechoNao() {
		return rdOcupacaoTrechoNao;
	}

	public void setRdOcupacaoTrechoNao(Radio rdOcupacaoTrechoNao) {
		this.rdOcupacaoTrechoNao = rdOcupacaoTrechoNao;
	}
	
	private void popularComboConexoesCtrl() {
		lsConexoesCtrlList = new ArrayList<>();
		lsConexoesCtrlList = conexionCtrlService.buscarTodasConexionesCtrlPricing();
		
		paradasConexao = new ArrayList<>();
		
		List<String> combinacaoParadas = new ArrayList<>();
		
		clearCombobox(cmbConexaoCtrl);
		for (ConexionCtrl p :lsConexoesCtrlList) {
			Parada paradaOrigem = paradaService.obtenerID(p.getOrigenId());
			Parada paradaDestino = paradaService.obtenerID(p.getDestinoId());
			
			if(!paradasConexao.contains(paradaOrigem)) {
				paradasConexao.add(paradaOrigem);
			} 
			
			if(!paradasConexao.contains(paradaDestino)) {
				paradasConexao.add(paradaDestino);
			} 
			
			String combinacao = p.getOrigenId() + "-" + p.getDestinoId();
			
			if(combinacaoParadas.contains(combinacao)) {
				continue;
			}
			
			combinacaoParadas.add(combinacao);
			
			Comboitem comboItem = new Comboitem(paradaOrigem.getDescparada() + " -> " + paradaDestino.getDescparada());
			comboItem.setValue(p);
			comboItem.setParent(cmbConexaoCtrl);
		}

		cmbConexaoCtrl.setSelectedIndex(-1);
	}
	
	public void onClick$btnNovaConexaoCtrl(Event ev) throws Exception {
		try {
			if (!validarNome()) {
				return;
			}
			
			if(conexoesPricingList == null) {
				conexoesPricingList = new ArrayList<>();
			}
			
			ConexionCtrl coCtrl =  (ConexionCtrl) (cmbConexaoCtrl.getSelectedItem() != null ? cmbConexaoCtrl.getSelectedItem().getValue() : null);
			
			if(coCtrl == null) {
				Messagebox.show(Labels.getLabel("editarConexionController.msgConexaoErro.value"), 
						Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.INFORMATION);
				return;
			} else {
				List<ParadaVOConexionRuta> novasConexoes = conexionCtrlService.carregarConexoesCadastradas(coCtrl.getOrigenId(), coCtrl.getDestinoId(), null);
				
				if(isConexaoJaAdicionada(novasConexoes)) {
					Messagebox.show(Labels.getLabel("editarConexionController.msgConexaoJaAdicionada.value"),
							Labels.getLabel("editarPricingController.window.title"), Messagebox.OK, Messagebox.INFORMATION);
					return;
				}
				
				criarPricingConexao(novasConexoes);
				
				conexoesPricingList.addAll(novasConexoes);
				
				conexoesPricingListApresentacao = buscarDadosApresentacao(conexoesPricingList);
				
				conexaoCtrlList.setData(conexoesPricingListApresentacao);
				conexaoCtrlList.setItemRenderer(new RenderConexaoPricing());
			}
		} catch (Exception e) {
			log.error("Erro ao adicionar nova conexao, erro: ", e);
		}
	}

	private List<ParadaVOConexionRuta> buscarDadosApresentacao(List<ParadaVOConexionRuta> conexoesPricingList2) {
		try {
			List<ParadaVOConexionRuta> listaApresentacao = new ArrayList<>();
			List<String> origemDestino = new ArrayList<>();
			
			for(ParadaVOConexionRuta p : conexoesPricingList2) {
				String parada = p.getParadaOrigem().getDescparada() + "-" + p.getParadaDestino().getDescparada();
				
				if(!origemDestino.contains(parada)) {
					origemDestino.add(parada);
					listaApresentacao.add(p);
				}
			}
			
			return listaApresentacao;
		} catch (Exception e) {
			log.error("Erro ao buscar dados de Apresentacao: ", e);
		}
		return null;
	}

	private void criarPricingConexao(List<ParadaVOConexionRuta> novasConexoes) {
		
		for(ParadaVOConexionRuta nConex : novasConexoes) {
			PricingConexao prConexao = new PricingConexao();
			prConexao.setActivo(Pricing.ATIVO);
			
			ConexionCtrl coCtrl = new ConexionCtrl();
			coCtrl.setConexionctrlId(nConex.getConexionCtrl().getConexionctrlId());
			prConexao.setConexionCtrl(coCtrl);
			
			prConexao.setFecmodif(Calendar.getInstance().getTime());
			prConexao.setPricing(pricing);
			prConexao.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
			
			if(!conexaoJaAdicionada(prConexao)) {
				lsPricingConexao.add(prConexao);
			}
			
			nConex.setPricingConexao(prConexao);
		}
	}
	
	private boolean conexaoJaAdicionada(PricingConexao prCon) {
		for(PricingConexao conex : lsPricingConexao) {
			if(prCon.getConexionCtrl().getConexionctrlId() != null 
					&& prCon.getConexionCtrl().getConexionctrlId().equals(conex.getConexionCtrl().getConexionctrlId()) ){
				return true;
			}
		}
		
		return false;
	}

	private void carregarConexoesCadastradas() {
		if (lsPricingConexao.size() > 0) {
			
			if(conexoesPricingList == null) {
				conexoesPricingList = new ArrayList<>();
			}
			
			List<ParadaVOConexionRuta> novasConexoes = new ArrayList<>();
			
			for(PricingConexao p : lsPricingConexao) {
				List<ParadaVOConexionRuta> aux = conexionCtrlService.carregarConexoesCadastradas(p.getConexionCtrl().getOrigenId(), p.getConexionCtrl().getDestinoId(),
						p.getConexionCtrl().getConexionctrlId());
				
				for(ParadaVOConexionRuta nConex : aux) {
					nConex.setPricingConexao(p);
				}
				
				novasConexoes.addAll(aux);
			}
			
			conexoesPricingList.addAll(novasConexoes);
			
			conexoesPricingListApresentacao = buscarDadosApresentacao(conexoesPricingList);
			
			conexaoCtrlList.setData(conexoesPricingListApresentacao);
			conexaoCtrlList.setItemRenderer(new RenderConexaoPricing());
		}
		
	}
	
	public void onClick$btnApagarConexaoCtrl(Event ev) throws Exception {
		ParadaVOConexionRuta conexao = (ParadaVOConexionRuta) conexaoCtrlList.getSelected();

		if (conexao != null ) {
			
			PricingConexao prConexaoSelecionado = conexao.getPricingConexao();
			
			List<PricingConexao> prConexaoARemover = new ArrayList<>();
			
			List<PricingConexao> todos = new ArrayList<>();
			todos.addAll(lsPricingConexao);
			
			//Limpa o pricing, pois o remove nao funciona pq o equals nao leva em consideracao somente origem e destino
			lsPricingConexao.clear();
			
			for(PricingConexao prConexao : todos) {
				
				if(isMesmaOrigemDestino(prConexao, prConexaoSelecionado)) {

					if(prConexao.getPricingConexaoId() != null) {
						prConexao.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
						prConexao.setFecmodif(Calendar.getInstance().getTime());
						prConexao.setActivo(Pricing.EXCLUIDO);
						//Adiciona os que ja estao no banco de dados
						lsPricingConexao.add(prConexao);
					} else {
						prConexaoARemover.add(prConexao);
					}
				} else {
					//Adiciona os que nao sao iguais ao selecionado
					lsPricingConexao.add(prConexao);
				}
			}
			
			List<ParadaVOConexionRuta> listaAAdicionar = new ArrayList<>();
			
			for(ParadaVOConexionRuta pAdiciona : conexoesPricingList) {
				if(!pAdiciona.getParadaOrigem().getParadaId().equals(conexao.getParadaOrigem().getParadaId()) || 
						!pAdiciona.getParadaDestino().getParadaId().equals(conexao.getParadaDestino().getParadaId())	) {
					listaAAdicionar.add(pAdiciona);
				}
			}
			
			conexoesPricingList.clear();
			conexoesPricingList.addAll(listaAAdicionar);
			
			conexaoCtrlList.clear();
			
			conexoesPricingListApresentacao = buscarDadosApresentacao(conexoesPricingList);
			conexaoCtrlList.setData(conexoesPricingListApresentacao);
		}
	}

	private boolean isMesmaOrigemDestino(PricingConexao prConexao, PricingConexao prConexaoSelecionado) {
		return prConexao.getConexionCtrl() != null && prConexao.getConexionCtrl().isOrigemDestinoIgual(prConexaoSelecionado.getConexionCtrl().getOrigenId(), prConexaoSelecionado.getConexionCtrl().getDestinoId());
	}

	private boolean isConexaoJaAdicionada(List<ParadaVOConexionRuta> novasConexoes) {
		
		for(ParadaVOConexionRuta p : novasConexoes) {
			for(ParadaVOConexionRuta pAdd : conexoesPricingList) {
				if(pAdd.getConexionCtrl().getOrigenId().equals(p.getConexionCtrl().getOrigenId()) && pAdd.getConexionCtrl().getDestinoId().equals(p.getConexionCtrl().getDestinoId())) {
					return true;
				}
			}
		}
		
		return false;
	}
}
