Пример #1
0
        private void btnGenerar_Click(object sender, EventArgs e)
        {
            try
            {
                string mensaje = string.Empty;
                if (cboMatriculas.SelectedIndex == -1) { mensaje += "Debe seleccionar una aeronave." + Environment.NewLine; }
                if (cboOrigen.SelectedIndex == -1) { mensaje += "Debe seleccionar una ciudad de origen." + Environment.NewLine; }
                if (cboDestino.SelectedIndex == -1) { mensaje += "Debe seleccionar una ciudad de destino." + Environment.NewLine; }
                if (cboOrigen.SelectedIndex == cboDestino.SelectedIndex) { mensaje += "Las ciudades no pueden ser iguales." + Environment.NewLine; }
                if (dtpFechaSalida.Value < ConfiguracionGlobal.FechaSistema) { mensaje += "La fecha debe ser posterior a la fecha actual." + Environment.NewLine; }
                if ((dtpFechaLlegadaEstimada.Value < dtpFechaSalida.Value.AddHours(1))) { mensaje += "El viaje tiene que durar más de una hora." + Environment.NewLine; }
                if ((dtpFechaLlegadaEstimada.Value > dtpFechaSalida.Value.AddDays(1))) { mensaje += "El viaje tiene que durar menos de un día." + Environment.NewLine; }
                if (!string.IsNullOrEmpty(mensaje)) { throw new Exception(mensaje); }

                Aeronave a = (Aeronave)cboMatriculas.SelectedItem;
                Ciudad origen = (Ciudad)cboOrigen.SelectedItem;
                Ciudad destino = (Ciudad)cboDestino.SelectedItem;

                RutaAerea ra = new RutaAerea().obtenerRutaAereaPorCiudades(origen.idCiudad, destino.idCiudad);
                if (ra == null) { throw new Exception(string.Format("No se ha encontrado una ruta aerea de {0} a {1}.",origen.nombreCiudad,destino.nombreCiudad)); }

                List<ServicioPorRuta> serviciosRuta = new ServicioPorRuta().obtenerListaPorIdRuta(ra.idRutaAerea);
                if (!serviciosRuta.Exists(sr => sr.idTipoServicio == a.idTipoServicio))
                {
                    throw new Exception(string.Format("El servicio de la aeronave no coincide con ninguno de la ruta aerea entre {0} y {1}.",origen.nombreCiudad,destino.nombreCiudad));
                }

                if (new Aeronave().tieneViajesEntreHorarios(dtpFechaSalida.Value, dtpFechaLlegadaEstimada.Value,a.idAeronave))
                {
                    dtpFechaSalida.Value = DateTime.Now;
                    actualizarExtremosFechas();
                    throw new Exception("La aeronave seleccionada no esta disponible en ese rango horario.");
                }

                var msg = MessageBox.Show("¿Está seguro que desea generar el viaje ingresado?", "Atención", MessageBoxButtons.YesNo);
                if (msg == DialogResult.Yes)
                {
                    new Viaje().generarViaje(a.idAeronave, ra.idRutaAerea, dtpFechaSalida.Value, dtpFechaLlegadaEstimada.Value);
                    MessageBox.Show("El viaje ha sido generado correctamente.", "OK");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }