package com.rjconsultores.ventaboletos.web.gui.controladores.gestaopricing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
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.event.Event;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Window;

import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.rjconsultores.ventaboletos.entidad.ClaseServicio;
import com.rjconsultores.ventaboletos.entidad.Constante;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.GP_Pricing;
import com.rjconsultores.ventaboletos.entidad.GP_PricingGestao;
import com.rjconsultores.ventaboletos.entidad.Parada;
import com.rjconsultores.ventaboletos.entidad.Ruta;
import com.rjconsultores.ventaboletos.relatorios.utilitarios.PricingColorVO;
import com.rjconsultores.ventaboletos.relatorios.utilitarios.PricingGestaoAlteradoVO;
import com.rjconsultores.ventaboletos.relatorios.utilitarios.PricingGestaoVO;
import com.rjconsultores.ventaboletos.service.ClaseServicioService;
import com.rjconsultores.ventaboletos.service.ConstanteService;
import com.rjconsultores.ventaboletos.service.EmpresaService;
import com.rjconsultores.ventaboletos.service.GP_PricingGestaoService;
import com.rjconsultores.ventaboletos.service.GP_PricingService;
import com.rjconsultores.ventaboletos.service.ParadaService;
import com.rjconsultores.ventaboletos.service.RutaService;
import com.rjconsultores.ventaboletos.service.impl.GP_GestaoPricingService;
import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties;
import com.rjconsultores.ventaboletos.utilerias.CustomEnum;
import com.rjconsultores.ventaboletos.utilerias.DateUtil;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.PantallaUtileria;

@Controller("gestaoPricingController")
@Scope("prototype")
public class GestaoPricingController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private GP_PricingService pricingService;
	@Autowired
	private GP_PricingGestaoService pricingGestaoService;
	@Autowired
	private EmpresaService empresaService;
	@Autowired
	private RutaService rutaService;

	private Paging pagingPricing;
	private List<GP_Pricing> lsPricing;
	private List<Empresa> lsEmpresa;
	private List<Ruta> lsRuta;
	private Combobox cmbEmpresa;
	private Datebox fechaInicio;
	private Datebox fechaFin;
	private Datebox fechaSemana;
	private Intbox numeroSemana;
	private Combobox cmbRuta;
	private Combobox cmbOrigen;
	private Combobox cmbDestino;
	private Combobox cmbTipoServicio;
	private List<ClaseServicio> lsClaseServicio;
	@Autowired
	private ParadaService paradaService;
	@Autowired
	private ClaseServicioService claseServicioService;
	@Autowired
	private GP_GestaoPricingService gestaoService;
	@Autowired
	private ConstanteService constanteService;
	
	private Parada origem;
	private Parada destino;
	private Empresa empresa;
	private ClaseServicio classe;
	private Ruta ruta;
	
	private static Logger log = LogManager.getLogger(GestaoPricingController.class);

	@Override
	public void doAfterCompose(Component comp) throws Exception {

		lsEmpresa = UsuarioLogado.getUsuarioLogado().getEmpresa();
		lsClaseServicio = claseServicioService.obtenerTodos();

		super.doAfterCompose(comp);
		
		if (arg.get("color") == null) {
			arg.put("color", "{}");
		}

		if (arg.get("origem") != null) {
			Comboitem co = (Comboitem) arg.get("origem");
			cmbOrigen.appendChild(co);
			cmbOrigen.setSelectedItem(co);
			cmbOrigen.setSelectedIndex(0);
		}
		if (arg.get("cmbDestino") != null) {
			Comboitem co = (Comboitem) arg.get("cmbDestino");
			cmbDestino.appendChild(co);
			cmbDestino.setSelectedItem(co);
			cmbDestino.setSelectedIndex(0);
		}
		if (arg.get("cmbEmpresa") != null) {
			Comboitem co = (Comboitem) arg.get("cmbEmpresa");
			cmbEmpresa.appendChild(co);
			cmbEmpresa.setSelectedItem(co);
			cmbEmpresa.setSelectedIndex(0);
		}
		if (arg.get("cmbTipoServicio") != null) {
			Comboitem co = (Comboitem) arg.get("cmbTipoServicio");
			cmbTipoServicio.appendChild(co);
			cmbTipoServicio.setSelectedItem(co);
			cmbTipoServicio.setSelectedIndex(0);
		}
		if (arg.get("cmbRuta") != null) {
			Comboitem co = (Comboitem) arg.get("cmbRuta");
			cmbRuta.appendChild(co);
			cmbRuta.setSelectedItem(co);
			cmbRuta.setSelectedIndex(0);
		}
		if (arg.get("numeroSemana") != null) {
			numeroSemana.setValue((Integer) arg.get("numeroSemana"));
			onChange$numeroSemana(null);
		}
		
		lsPricing = new ArrayList<GP_Pricing>();

	}
	
	public void onChange$fechaSemana(Event ev) throws InterruptedException {		
		if (fechaSemana.getValue() != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(fechaSemana.getValue());
			
			numeroSemana.setValue(calendar.get(Calendar.WEEK_OF_YEAR));
			onChange$numeroSemana(ev);
			onClick$btnReturn(null);
		} 
	}
	
	public void onChange$cmbRuta(Event ev) throws InterruptedException {
		Ruta ruta = (Ruta) (cmbRuta.getSelectedItem() != null ? cmbRuta.getSelectedItem().getValue() : null);
		if (ruta != null) {
			ClaseServicio cs = claseServicioService.obtenerID(ruta.getClaseServicio().getClaseservicioId());
			lsClaseServicio.clear();
			lsClaseServicio.add(cs);
			BindingListModelList l = new BindingListModelList(lsClaseServicio, true);
			cmbTipoServicio.setModel(l);
			cmbTipoServicio.setSelectedIndex(0);
		}

	}
	
	public void onChange$numeroSemana(Event ev) throws InterruptedException {
		if (numeroSemana.getValue() != null) {
			Calendar calendar = Calendar.getInstance();
			
			calendar.setFirstDayOfWeek(Calendar.SUNDAY);
			calendar.set(Calendar.WEEK_OF_YEAR, numeroSemana.getValue());			
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			
			fechaInicio.setValue(calendar.getTime());
			fechaSemana.setValue(calendar.getTime());
						
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			
			fechaFin.setValue(calendar.getTime());
			
		} 
	}

	public void onChange$cmbEmpresa(Event ev) throws InterruptedException {
		
		Empresa empresa = (Empresa) (cmbEmpresa.getSelectedItem() != null ? cmbEmpresa.getSelectedItem().getValue() : null);
		
		if (empresa != null) {
			lsRuta = rutaService.obtenerPorEmpresa(empresa);
			lsRuta.add(rutaService.obtenerID(-1));
			BindingListModelList l = new BindingListModelList(lsRuta, true);
			cmbRuta.setModel(l);
		}
	}
	
	public void onClick$btnPesquisa(Event ev) throws InterruptedException {
		refreshLista();
		arg.put("tmpJs", arg.get("json"));
	}

	public void onClick$btnReturn(Event ev) {

		this.closeWindow();


			arg.put("origem", cmbOrigen.getSelectedItem());
			arg.put("cmbDestino", cmbDestino.getSelectedItem());
			arg.put("cmbEmpresa", cmbEmpresa.getSelectedItem());
			arg.put("cmbTipoServicio", cmbTipoServicio.getSelectedItem());
			arg.put("cmbRuta", cmbRuta.getSelectedItem());
			arg.put("numeroSemana", numeroSemana.getValue());
			
		if (ev != null) {
			if (arg.get("tmpJs") != null) {
				arg.put("json", arg.get("tmpJs"));
			}

			if (arg.get("color") != null) {
				arg.put("color", arg.get("color"));
			}
		}

		PantallaUtileria.openWindow("/gui/gestao_pricing/gestaoPricing.zul",
				Labels.getLabel("indexController.mniGestaoPricing.label"), arg, desktop);

	}

	private void refreshLista() {
		
		Empresa empresa = (Empresa) (cmbEmpresa.getSelectedItem() != null ? cmbEmpresa.getSelectedItem().getValue() : null);
		Ruta ruta = (Ruta) (cmbRuta.getSelectedItem() != null ? cmbRuta.getSelectedItem().getValue() : null);
		Parada origen = null;
		
		if (cmbOrigen.getSelectedItem() == null) {
			List<Parada> lsParadaOri = paradaService.buscar(cmbOrigen.getText().toUpperCase());
			if (!lsParadaOri.isEmpty()) {
				origen = lsParadaOri.get(0);
			}
		} else {
			origen = (Parada) cmbOrigen.getSelectedItem().getValue();
		}
		Parada destino = null;
		if (cmbDestino.getSelectedItem() == null) {
			List<Parada> lsParadaOri = paradaService.buscar(cmbDestino.getText().toUpperCase());
			if (!lsParadaOri.isEmpty()) {
				destino = lsParadaOri.get(0);
			}
		} else {
			destino = (Parada) cmbDestino.getSelectedItem().getValue();
		}
		
		if (origen == null || destino == null) {
			try {
				Messagebox.show("É necessário preencher origem e destino.", Labels
						.getLabel("busquedaPricingController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);
				return;
			} catch (InterruptedException ex) {
			}
		}
		
		ClaseServicio claseServicio = (ClaseServicio) (cmbTipoServicio.getSelectedItem() != null ? cmbTipoServicio.getSelectedItem().getValue() : null);
	
		Calendar dataInicio = Calendar.getInstance();
		Calendar dataFinal = Calendar.getInstance();
		if ((fechaInicio.getValue() != null) && (fechaFin.getValue() != null)) {
			dataInicio.setTime(fechaInicio.getValue());
			dataInicio.set(Calendar.HOUR_OF_DAY, 0);
			dataInicio.set(Calendar.MINUTE, 0);
			dataInicio.set(Calendar.SECOND, 0);
			dataInicio.set(Calendar.MILLISECOND, 0);

			dataFinal.setTime(fechaFin.getValue());
			dataFinal.set(Calendar.HOUR_OF_DAY, 23);
			dataFinal.set(Calendar.MINUTE, 59);
			dataFinal.set(Calendar.SECOND, 59);
			dataFinal.set(Calendar.MILLISECOND, 999);
		} else {
			try {
				Messagebox.show(Labels.getLabel("gestaoPricing.SelecionarDatas.msg"), Labels
						.getLabel("busquedaPricingController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);
				return;
			} catch (InterruptedException ex) {
			}
		}
		
		List<String> empresas = new ArrayList<String>();
		for (Empresa emp : UsuarioLogado.getUsuarioLogado().getEmpresa()) {
			empresas.add(emp.getEmpresaId().toString());
		}
		
		Map<String, Object> params = new LinkedHashMap<String, Object>();
		params.put("listEmpresaId", empresas.toString().replace(" ","").replace("[","").replace("]","") +",");
		params.put("unaFecCorrida", DateUtil.getStringDate(dataInicio.getTime()));
		params.put("embarcada", false);
		params.put("funcion", false);
		params.put("verificaBPe", false);
		params.put("esViajeRedondo", false);
		params.put("unPuntoVenta_id", UsuarioLogado.getUsuarioLogado().getUsuarioUbicacionPadrao().getPuntoVenta().getPuntoventaId());
		params.put("usuarioId", UsuarioLogado.getUsuarioLogado().getUsuarioId());
		params.put("unTipoVenta_id", "1");
		params.put("unaHoraCorrida", "00:01");
		params.put("origemId", origen.getParadaId());
		params.put("unOrigen_id", origen.getParadaId());
		params.put("destinoId", destino.getParadaId());
		params.put("unDestino_id", destino.getParadaId());
		
		if (empresa != null) {
			params.put("listEmpresaId", empresa.getEmpresaId());
		}

		if (claseServicio != null) {
			params.put("unaClaseServicio_id", claseServicio.getClaseservicioId());
		}
		if (ruta != null) {
			params.put("rutaId", ruta.getRutaId());
		}
		if (claseServicio != null) {
			params.put("classeId", claseServicio.getClaseservicioId());
		}
		
		params.put("dtInicio", dataInicio.getTimeInMillis());
		params.put("dtFim", dataFinal.getTimeInMillis());
		
		Constante constante = constanteService.buscarPorNomeConstante("URL_TOTALBUS_SERVIDOR");
		
		if(constante == null){
			try {
				Messagebox.show("É necessário configurar a URL do servidor.", Labels
						.getLabel("busquedaPricingController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);
				return;
			} catch (InterruptedException ex) {
			}
			
		}
		
		String json = retornaJs(constante.getValorconstante() + "/VentaBoletosServidor/Boleto/VerificarPricing.do?", params);
		
		log.info(json);

		try {
			JsonParser parser = new JsonParser();
			parser.parse(json);

		} catch (Exception e) {
			log.error("", e);
			try {
				Messagebox.show(Labels.getLabel("MSG.ningunRegistro"), Labels
						.getLabel("busquedaPricingController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);
			} catch (InterruptedException e1) {

			}
			return;
		}

		Gson gson = new Gson();
		List<PricingGestaoVO> listPricingGestaoVO = new ArrayList<PricingGestaoVO>();
		List<PricingColorVO> listPricingColorVO = new ArrayList<PricingColorVO>();
		
		List<GP_Pricing> pricings = pricingService.obtenerTodos();
		
		for (GP_Pricing gp_Pricing : pricings) {
			if (gp_Pricing.getIndSobreposicao() != null && !gp_Pricing.getIndSobreposicao() && StringUtils.isNotBlank(gp_Pricing.getNombPricing())) {
				PricingColorVO vo = new PricingColorVO();
				vo.setId(gp_Pricing.getNombPricing());
				vo.setIdBanco(gp_Pricing.getPricingId().toString());
				vo.setColor("white");
				vo.setBackgroundColor(gp_Pricing.getColor());
				listPricingColorVO.add(vo);
			}

		}
		
		do {
			PricingColorVO vo = new PricingColorVO();
			vo.setId("Vazio");
			vo.setColor("white");
			vo.setBackgroundColor("white");
			listPricingColorVO.add(vo);
		} while (listPricingColorVO.size() < 15);
		
		if (json.equals("[]")) {
			do {
				PricingGestaoVO servico = new PricingGestaoVO();
				servico.setServico("");
				servico.setDomingo("");
				servico.setSegunda("");
				servico.setTerca("");
				servico.setQuarta("");
				servico.setQuinta("");
				servico.setSexta("");
				servico.setSabado("");
				listPricingGestaoVO.add(servico);
			} while (listPricingGestaoVO.size() < 15);

			json = gson.toJson(listPricingGestaoVO);
		}

		String color = gson.toJson(listPricingColorVO);

		arg.put("origem", cmbOrigen.getSelectedItem());
		arg.put("cmbDestino", cmbDestino.getSelectedItem());
		arg.put("cmbEmpresa", cmbEmpresa.getSelectedItem());
		arg.put("cmbTipoServicio", cmbTipoServicio.getSelectedItem());
		arg.put("cmbRuta",  cmbRuta.getSelectedItem());
		arg.put("numeroSemana", numeroSemana.getValue());

		this.closeWindow();
		
		arg.put("json", json);
		arg.put("color", color);
		
		PantallaUtileria.openWindow("/gui/gestao_pricing/gestaoPricing.zul",
                Labels.getLabel("indexController.mniGestaoPricing.label"), arg, desktop);

	}


	public void onCreateWindow(Event evt) {

		Window window = (Window) evt.getTarget();

		if (window.getId().equals("winGestaoPricing")) {
			window.addEventListener("onNotifyServer", this);
			window.addEventListener("onLoad", this);
		} 
	}

	@Override
	public void onEvent(Event evt) throws Exception {
		super.onEvent(evt);
		if (evt.getName().equals("onNotifyServer")) {
			serverNotified(evt);
		}
	}

	private void serverNotified(Event evt) {

		try {
			int resp = Messagebox.show(
					"Salvar os cenários inseridos ?",
					Labels.getLabel("editarPricingController.window.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			if (resp == Messagebox.YES) {

				Gson gson = new Gson();
				Type typePricingGestaoVO = new TypeToken<ArrayList<PricingGestaoVO>>() {
				}.getType();
				Type typePricingColorVO = new TypeToken<ArrayList<PricingColorVO>>() {
				}.getType();

				String json = (String) arg.get("json");
				String cores = (String) arg.get("color");
				List<PricingGestaoVO> pricingGestaoVOOld = gson.fromJson(json, typePricingGestaoVO);
				List<PricingColorVO> pricingCores = gson.fromJson(cores, typePricingColorVO);
				Object[] data = (Object[]) evt.getData();
				List<PricingGestaoAlteradoVO> listPricingGestaoVO = new ArrayList<PricingGestaoAlteradoVO>();
				Date inicio = DateUtil.normalizar(fechaInicio.getValue());
				for (int i = 1; i < data.length; i++) {
					PricingGestaoVO pricingOld = pricingGestaoVOOld.get(i);
					PricingGestaoVO pricingGestaoVO = gson.fromJson(data[i].toString(), PricingGestaoVO.class);

					if (!pricingOld.getDomingo().equals(pricingGestaoVO.getDomingo())) {
						PricingGestaoAlteradoVO alterado = new PricingGestaoAlteradoVO();
						alterado.setPricing(pricingGestaoVO.getDomingo());
						alterado.setServico(pricingGestaoVO.getServico());
						alterado.setData(inicio);
						listPricingGestaoVO.add(alterado);
					}
					if (!pricingOld.getSegunda().equals(pricingGestaoVO.getSegunda())) {
						PricingGestaoAlteradoVO alterado = new PricingGestaoAlteradoVO();
						alterado.setPricing(pricingGestaoVO.getSegunda());
						alterado.setServico(pricingGestaoVO.getServico());
						alterado.setData(DateUtil.somarDias(inicio, 1));
						listPricingGestaoVO.add(alterado);
					}
					if (!pricingOld.getTerca().equals(pricingGestaoVO.getTerca())) {
						PricingGestaoAlteradoVO alterado = new PricingGestaoAlteradoVO();
						alterado.setPricing(pricingGestaoVO.getTerca());
						alterado.setServico(pricingGestaoVO.getServico());
						alterado.setData(DateUtil.somarDias(inicio, 2));
						listPricingGestaoVO.add(alterado);
					}
					if (!pricingOld.getQuarta().equals(pricingGestaoVO.getQuarta())) {
						PricingGestaoAlteradoVO alterado = new PricingGestaoAlteradoVO();
						alterado.setPricing(pricingGestaoVO.getQuarta());
						alterado.setServico(pricingGestaoVO.getServico());
						alterado.setData(DateUtil.somarDias(inicio, 3));
						listPricingGestaoVO.add(alterado);
					}
					if (!pricingOld.getQuinta().equals(pricingGestaoVO.getQuinta())) {
						PricingGestaoAlteradoVO alterado = new PricingGestaoAlteradoVO();
						alterado.setPricing(pricingGestaoVO.getQuinta());
						alterado.setServico(pricingGestaoVO.getServico());
						alterado.setData(DateUtil.somarDias(inicio, 4));
						listPricingGestaoVO.add(alterado);
					}
					if (!pricingOld.getSexta().equals(pricingGestaoVO.getSexta())) {
						PricingGestaoAlteradoVO alterado = new PricingGestaoAlteradoVO();
						alterado.setPricing(pricingGestaoVO.getSexta());
						alterado.setServico(pricingGestaoVO.getServico());
						alterado.setData(DateUtil.somarDias(inicio, 5));
						listPricingGestaoVO.add(alterado);
					}
					if (!pricingOld.getSabado().equals(pricingGestaoVO.getSabado())) {
						PricingGestaoAlteradoVO alterado = new PricingGestaoAlteradoVO();
						alterado.setPricing(pricingGestaoVO.getSabado());
						alterado.setServico(pricingGestaoVO.getServico());
						alterado.setData(DateUtil.somarDias(inicio, 6));
						listPricingGestaoVO.add(alterado);
					}

				}

				for (PricingGestaoAlteradoVO pricingGestaoAlteradoVO : listPricingGestaoVO) {

					for (PricingColorVO cor : pricingCores) {
						if (cor != null && cor.getId() != null && cor.getId().equals(pricingGestaoAlteradoVO.getPricing())) {
							pricingGestaoAlteradoVO.setId(cor.getIdBanco());
						}
					}
				}

				for (PricingGestaoAlteradoVO pricingGestaoAlteradoVO : listPricingGestaoVO) {

					boolean isPricingAtivo = true;

					GP_Pricing pricingOld = pricingService.obtenerID(Integer.valueOf(pricingGestaoAlteradoVO.getId()));
					GP_Pricing pricingNew = pricingService.clonarGP_Pricing(Integer.valueOf(pricingGestaoAlteradoVO.getId()), pricingOld.getNombPricing() , ApplicationProperties.getInstance().isCustomHabilitado(CustomEnum.USA_PADRAO_PRICING_TIPO_PASSAGEM_PET.getDescricao()), isPricingAtivo);
					pricingNew.setColor(pricingOld.getColor());
					pricingNew.setIndGestao(true);
					
					GP_PricingGestao gestao = new GP_PricingGestao();
					gestao.setCorridaId(Integer.valueOf(pricingGestaoAlteradoVO.getServico()));
					gestao.setFeccorrida(DateUtil.normalizarToFecha(pricingGestaoAlteradoVO.getData()));
					gestao.setFecmodif(Calendar.getInstance().getTime());
					gestao.setPricing(pricingNew);
					gestao.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					
					pricingGestaoService.suscribir(gestao);

					pricingService.actualizacion(pricingNew);
				}

				Messagebox.show(
						"Os cenários foram adicionados com sucesso.",
						Labels.getLabel("editarPricingController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);
				
				arg.put("tmpJs", null);

			}
		} catch (Exception e) {
			log.error("", e);
		}

	}
	
	
	public String retornaJs(String URL, Map<String, Object> params) {
		try {

			StringBuilder postData = new StringBuilder();
			for (Map.Entry<String, Object> param : params.entrySet()) {
				if (postData.length() != 0)
					postData.append('&');
				postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
				postData.append('=');
				postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
			}
	        
			URL url = new URL(URL + postData.toString());
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Accept", "application/json");

			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			StringBuilder retorno = new StringBuilder();
			while ((output = br.readLine()) != null) {
				retorno.append(output);
				log.info(output);
			}

			conn.disconnect();

			return retorno.toString();

		} catch (MalformedURLException e) {

			log.error("", e);

		} catch (IOException e) {

			log.error("", e);

		}
		return "{}";
	}

	public List<ClaseServicio> getLsClaseServicio() {
		return lsClaseServicio;
	}

	public void setLsClaseServicio(List<ClaseServicio> lsClaseServicio) {
		this.lsClaseServicio = lsClaseServicio;
	}

	public ClaseServicioService getClaseServicioService() {
		return claseServicioService;
	}

	public void setClaseServicioService(ClaseServicioService claseServicioService) {
		this.claseServicioService = claseServicioService;
	}
	
	public List<GP_Pricing> getLsPricing() {
		return lsPricing;
	}

	public void setLsPricing(List<GP_Pricing> lsPricing) {
		this.lsPricing = lsPricing;
	}

	public Paging getPagingPricing() {
		return pagingPricing;
	}

	public void setPagingPricing(Paging pagingPricing) {
		this.pagingPricing = pagingPricing;
	}

	public GP_PricingService getPricingService() {
		return pricingService;
	}

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

	public EmpresaService getEmpresaService() {
		return empresaService;
	}

	public void setEmpresaService(EmpresaService empresaService) {
		this.empresaService = empresaService;
	}

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

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

	public Datebox getFechaInicio() {
		return fechaInicio;
	}

	public void setFechaInicio(Datebox fechaInicio) {
		this.fechaInicio = fechaInicio;
	}

	public Datebox getFechaFin() {
		return fechaFin;
	}

	public void setFechaFin(Datebox fechaFin) {
		this.fechaFin = fechaFin;
	}

	public RutaService getRutaService() {
		return rutaService;
	}

	public void setRutaService(RutaService rutaService) {
		this.rutaService = rutaService;
	}

	public List<Ruta> getLsRuta() {
		return lsRuta;
	}

	public void setLsRuta(List<Ruta> lsRuta) {
		this.lsRuta = lsRuta;
	}

	public Combobox getCmbRuta() {
		return cmbRuta;
	}

	public void setCmbRuta(Combobox cmbRuta) {
		this.cmbRuta = cmbRuta;
	}

	public Datebox getFechaSemana() {
		return fechaSemana;
	}

	public void setFechaSemana(Datebox fechaSemana) {
		this.fechaSemana = fechaSemana;
	}

	public Intbox getNumeroSemana() {
		return numeroSemana;
	}

	public void setNumeroSemana(Intbox numeroSemana) {
		this.numeroSemana = numeroSemana;
	}

	public Parada getOrigem() {
		return origem;
	}

	public void setOrigem(Parada origem) {
		this.origem = origem;
	}

	public Parada getDestino() {
		return destino;
	}

	public void setDestino(Parada destino) {
		this.destino = destino;
	}

	public Empresa getEmpresa() {
		return empresa;
	}

	public void setEmpresa(Empresa empresa) {
		this.empresa = empresa;
	}

	public ClaseServicio getClasse() {
		return classe;
	}

	public void setClasse(ClaseServicio classe) {
		this.classe = classe;
	}

	public Ruta getRuta() {
		return ruta;
	}

	public void setRuta(Ruta ruta) {
		this.ruta = ruta;
	}
	
}
