Example #1
0
        private void button_Guardar_Click(object sender, EventArgs e)
        {
            // Guarda en variables todos los campos de entrada
            String  Marca             = comboBox_Marca.Text;
            String  Modelo            = textBox_Modelo.Text;
            String  Patente           = textBox_Patente.Text;
            String  TurnoSeleccionado = comboBox_Turno.Text;
            String  DniChofer         = textBox_Chofer.Text;
            Boolean activo            = checkBox_Habilitado.Checked;
            Boolean pudoModificar;
            Boolean checkBoxActivoDeshabilitar_Chofer = checkBoxDeshabilitar_chofer.Checked;
            Boolean existeTurnoAutomovil;
            Boolean existeChoferAutomovil;

            try
            {
                Automoviles auto = new Automoviles();

                auto.SetMarca(Marca);
                auto.SetModelo(Modelo);
                auto.SetPatente(Patente);
                auto.SetActivo(activo);

                this.SetIdChofer(DniChofer);

                if (TurnoSeleccionado != "Ninguno")
                {
                    String IDTurno = this.obtenerIdTurnoaPartirDeCombobox(comboBox_Turno.Text);
                    this.SetIdTurno(IDTurno);
                }


                existeTurnoAutomovil = mapper.ExisteEstadoTunoAutomovil(this.idAutomovil, idTurno);

                existeChoferAutomovil = mapper.ExisteChoferAutomovil(this.idAutomovil, this.idChofer);

                int idChoferViejo = mapper.obtenerIdChoferApartirDelDNI(dniChoferAutomovil);
                int idChoferNuevo = mapper.obtenerIdChoferApartirDelDNI(DniChofer);

                if (existeTurnoAutomovil)
                {
                    this.pudoActualizarTurnoAutomovil = mapper.ActualizarEstadoTurnoAutomovil(this.idAutomovil, idTurno, 1);
                }
                else
                {
                    if (TurnoSeleccionado != "Ninguno")
                    {
                        this.pudoActualizarTurnoAutomovil = mapper.AgregarTurnoAutomovil(this.idAutomovil, idTurno);
                    }
                    else
                    {
                        pudoActualizarTurnoAutomovil = true;
                    }
                }


                if (idChoferNuevo != idChoferViejo)
                {
                    if (existeChoferAutomovil)
                    {
                        //deshabilitar el chofer dni viejo
                        mapper.ActualizarEstadoChoferAutomovil(this.idAutomovil, idChoferViejo, 0);
                        //habilitar el chofer dni nuevo
                        this.pudoActualizarChoferAutomovil = mapper.ActualizarEstadoChoferAutomovil(this.idAutomovil, idChoferNuevo, 1);
                    }
                    else
                    {
                        this.pudoActualizarChoferAutomovil = mapper.AsignarChoferaAutomovil(this.idAutomovil, idChoferNuevo);//aca llamo al procedure pr_agregar_chofer_a_automovil
                        if (pudoActualizarChoferAutomovil)
                        {
                            mapper.ActualizarEstadoChoferAutomovil(this.idAutomovil, idChoferViejo, 0);
                        }
                    }
                }
                else
                {
                    Boolean existeChoferAutomovilActivo;
                    existeChoferAutomovilActivo = mapper.ExisteChoferAutomovilActivo(this.idChofer, this.idAutomovil, 1);//Esto quiere decir que ya existe el chofer en la tabla choferporAuto con la columna auto_chofer_estado en 1


                    if (checkBoxActivoDeshabilitar_Chofer)
                    {
                        //deshabilitar  chofer
                        this.pudoActualizarChoferAutomovil = mapper.ActualizarEstadoChoferAutomovil(this.idAutomovil, idChoferViejo, 0);
                    }
                    else
                    {
                        if (existeChoferAutomovil)
                        {
                            if (existeChoferAutomovilActivo)
                            {
                                throw new ExisteChoferAutomovilHabilitadoException("Ya existe un Automovil activo para este Chofer");//Esto quiere decir que ya existe el chofer en la tabla choferporAuto con la columna auto_chofer_estado en 1
                            }
                            else
                            {
                                this.pudoActualizarChoferAutomovil = mapper.ActualizarEstadoChoferAutomovil(this.idAutomovil, idChoferViejo, 1);
                            }
                        }
                    }
                }
                pudoModificar = mapper.Modificar(idAutomovil, auto);

                if (pudoModificar && pudoActualizarTurnoAutomovil && pudoActualizarChoferAutomovil)
                {
                    MessageBox.Show("Automovil modificado correctamente");
                }

                this.Close();
            }
            catch (CampoVacioException exception)
            {
                MessageBox.Show("Falta completar campo : " + exception.Message);
                return;
            }
            catch (FormatoInvalidoException exception)
            {
                MessageBox.Show("Datos mal ingresados en: " + exception.Message);
                return;
            }

            catch (ChoferInexistenteException exceptionChoferNoexite)
            {
                MessageBox.Show(exceptionChoferNoexite.Message);
                return;
            }
            catch (ExisteChoferAutomovilHabilitadoException exceptionChoferAutoActivoexiste)
            {
                MessageBox.Show(exceptionChoferAutoActivoexiste.Message, "Coche activo ya asignado", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            catch (SqlException error)
            {
                switch (error.Number)
                {
                case 51005: MessageBox.Show(error.Message, "Coche activo ya asignado", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }
        private void button_Guardar_Click(object sender, EventArgs e)
        {
            String Marca     = comboBox_Marca.Text;
            String Modelo    = textBox_Modelo.Text;
            String Patente   = textBox_Patente.Text;
            String DniChofer = combo_Chofer.Text;



            try
            {
                Automoviles auto = new Automoviles();

                auto.SetMarca(Marca);
                auto.SetModelo(Modelo);
                auto.SetPatente(Patente);
                auto.SetActivo(true);
                String IDTurno = obtenerIdTurnoaPartirDeCombobox(comboBox_Turno.Text);
                this.SetIdTurno(IDTurno);
                this.SetIdChofer(DniChofer);
                idAuto = mapper.CrearAutomoviles(auto);


                if (idAuto != 0)
                {
                    choferAutoAgregado = mapper.AsignarChoferaAutomovil(this.idAuto, this.idChofer);
                    turnoAutoAgregado  = mapper.AsignarTurnoaAutomovil(this.idAuto, this.idTurno);
                }
                if (idAuto > 0 && this.choferAutoAgregado == true && this.turnoAutoAgregado == true)
                {
                    MessageBox.Show("Automovil agregado correctamente");
                }
            }
            catch (CampoVacioException exceptionCampoVacio)
            {
                MessageBox.Show("Falta completar campo: " + exceptionCampoVacio.Message);
                return;
            }
            catch (FormatoInvalidoException exceptionFormato)
            {
                MessageBox.Show("Los datos fueron mal ingresados en: " + exceptionFormato.Message);
                return;
            }
            catch (ChoferInexistenteException exceptionChofer)
            {
                MessageBox.Show(exceptionChofer.Message);
                return;
            }
            catch (SqlException error)
            {
                switch (error.Number)
                {
                case 51005: MessageBox.Show(error.Message + " para agregarlo ingresarlo desmarcar el casillero de Habilitado", "Coche activo ya asignado", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    mapper.EliminarAutoFisicamenteDelaBase(idAuto, "Auto");    //esto es para eliminar el auto que se agrego arriba por que a pesar de que falle al agregar AutoChofer al al auto lo agrega
                    return;

                case 2627: MessageBox.Show("La patente ya se encuentra registrada", "Patente Invalida", MessageBoxButtons.OK, MessageBoxIcon.Error);     //Violacion de restriccion UNIQUE
                    return;
                }
            }

            VolverAlMenu();
        }