private void cargarGrilla()
        {
            String opcion = "Sin Opción";

            try
            {
                List <Maquinaria> maquinarias = MaquinariaDAO.GetAll();

                dgv_maquinas.Rows.Clear();
                foreach (Maquinaria maq in maquinarias)
                {
                    switch (maq.estado.idEstado)
                    {
                    case 14:
                        opcion = "No disponible";
                        break;

                    case 16:
                        opcion = "Disponible";
                        break;

                    default:
                        opcion = "Sin Opción";
                        break;
                    }
                    dgv_maquinas.Rows.Add(maq.Nombre, maq.descripcion, maq.tipoMaquinaria.Nombre, maq.fechaAlta.ToShortDateString(), maq.estado.Nombre, maq.idMaquinaria, maq.tipoMaquinaria.idTipoMaquinaria, maq.estado.idEstado, opcion);
                }
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
        private void btn_eliminar_Click(object sender, EventArgs e)
        {
            string maquinaSeleccionada = dgv_maquinas.Rows[dgv_maquinas.CurrentRow.Index].Cells["nombre"].Value.ToString();

            if (MessageBox.Show("Desea eliminar: " + maquinaSeleccionada, "Atencion", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                int idMaquina = (int)dgv_maquinas.Rows[dgv_maquinas.CurrentRow.Index].Cells["idMaquinaria"].Value;

                try
                {
                    MaquinariaDAO.Delete(idMaquina);
                    MessageBox.Show("Eliminación Exitosa", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    cargarGrilla();
                }
                catch (ApplicationException ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
        }
        public void cargaCombos()
        {
            if (txt_inicio.Text != "  :" && txt_fin.Text != "  :")
            {
                try
                {
                    List <Maquinaria> maq = MaquinariaDAO.GetAllDisponibles((int)cmb_productos.SelectedValue, dtp_creacion_OT.Value.Date, Convert.ToDateTime(txt_inicio.Text), Convert.ToDateTime(txt_fin.Text));
                    maq.Add(new Maquinaria()
                    {
                        idMaquinaria = -1, Nombre = "<<Seleccione>>"
                    });

                    cmb_maquinaria.DataSource    = maq;
                    cmb_maquinaria.DisplayMember = "nombre";
                    cmb_maquinaria.ValueMember   = "idMaquinaria";
                    cmb_maquinaria.SelectedValue = -1;

                    List <Empleado> empl = EmpleadoDAO.GetAllDisponible(dtp_creacion_OT.Value.Date, Convert.ToDateTime(txt_inicio.Text), Convert.ToDateTime(txt_fin.Text));

                    empl.Add(new Empleado()
                    {
                        idEmpleado = -1, Nombre = "<<Seleccione>>"
                    });
                    cmb_empleado.DataSource    = empl;
                    cmb_empleado.DisplayMember = "nombre";
                    cmb_empleado.ValueMember   = "idEmpleado";
                    cmb_empleado.SelectedValue = -1;
                }
                catch (FormatException ex)
                {
                    MessageBox.Show("Error en el formato de las horas", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
                cmb_empleado.Enabled   = true;
                cmb_maquinaria.Enabled = true;
            }
            else
            {
                MessageBox.Show("Complete las 2 Horas", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
        }
        private void btn_aplicar_filtro_Click(object sender, EventArgs e)
        {
            Maquinaria maq = new Maquinaria();

            if (txt_nombre.Text != "")
            {
                maq.Nombre = txt_nombre.Text;
            }
            if (txt_descrip.Text != "")
            {
                maq.descripcion = txt_descrip.Text;
            }
            if (cmb_estado_maq.SelectedValue != (object)0)
            {
                maq.estado = new Estado()
                {
                    idEstado = Convert.ToInt32(cmb_estado_maq.SelectedValue)
                };
            }
            if (cmb_tipo_maq.SelectedValue != (object)0)
            {
                maq.tipoMaquinaria = new TipoMaquinaria()
                {
                    idTipoMaquinaria = (int)cmb_tipo_maq.SelectedValue
                };
            }


            List <Maquinaria> maquinas = null;

            try
            {
                maquinas = MaquinariaDAO.GetByFiltro(maq);
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            cargarGrillaFiltrada(maquinas);
        }
        public Boolean verificarExistenciaMaquinaria(string nombreMaquina)
        {
            Boolean resul = false;

            try
            {
                List <Maquinaria> maquinas = MaquinariaDAO.GetAll();

                foreach (Maquinaria maq in maquinas)
                {
                    if (maq.Nombre.Equals(nombreMaquina))
                    {
                        resul = true;
                    }
                }
            }
            catch (ApplicationException ex)
            {
                throw new ApplicationException(ex.Message);
            }


            return(resul);
        }
        private void dgv_maquinas_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_maquinas.CurrentCell is DataGridViewButtonCell)
            {
                if ((int)dgv_maquinas.CurrentRow.Cells["idestado"].Value == 14)
                {
                    int idMaquinaria = (int)dgv_maquinas.CurrentRow.Cells["idmaquinaria"].Value;

                    try
                    {
                        MaquinariaDAO.UpdateEstado(idMaquinaria, 16);
                        MessageBox.Show("Maquina no Disponible", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    }
                    catch (ApplicationException ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
                if ((int)dgv_maquinas.CurrentRow.Cells["idestado"].Value == 16)
                {
                    int idMaquinaria = (int)dgv_maquinas.CurrentRow.Cells["idmaquinaria"].Value;


                    try
                    {
                        MaquinariaDAO.UpdateEstado(idMaquinaria, 14);
                        MessageBox.Show("Maquina Disponible", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    }
                    catch (ApplicationException ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
                cargarGrilla();
            }
        }
        public void cargaCombos()
        {
            if (txt_inicio.Text != "  :" && txt_fin.Text != "  :")
            {
                try
                {
                    List <Maquinaria> maq = MaquinariaDAO.GetAllDisponibles(prodInt.idProducto, dtp_creacion_OT.Value.Date, Convert.ToDateTime(txt_inicio.Text), Convert.ToDateTime(txt_fin.Text));
                    maq.Add(new Maquinaria()
                    {
                        idMaquinaria = -1, Nombre = "<<Seleccione>>"
                    });

                    foreach (OrdenDeTrabajo ot in ordenesHijas)
                    {
                        if ((ot.horaInicio >= Convert.ToDateTime(txt_inicio.Text) && ot.horaInicio <= Convert.ToDateTime(txt_fin.Text)) || (ot.horaFin >= Convert.ToDateTime(txt_inicio.Text) && ot.horaFin <= Convert.ToDateTime(txt_fin.Text)))
                        {
                            Maquinaria aux = null;
                            foreach (Maquinaria ma in maq)
                            {
                                if (ma.idMaquinaria == ot.maquinaria.idMaquinaria)
                                {
                                    aux = ma;
                                }
                            }
                            if (aux != null)
                            {
                                maq.Remove(aux);
                            }
                        }
                    }


                    cmb_maquinaria.DataSource    = maq;
                    cmb_maquinaria.DisplayMember = "nombre";
                    cmb_maquinaria.ValueMember   = "idMaquinaria";
                    cmb_maquinaria.SelectedValue = -1;

                    List <Empleado> empl = EmpleadoDAO.GetAllDisponible(dtp_creacion_OT.Value.Date, Convert.ToDateTime(txt_inicio.Text), Convert.ToDateTime(txt_fin.Text));

                    empl.Add(new Empleado()
                    {
                        idEmpleado = -1, Nombre = "<<Seleccione>>"
                    });

                    foreach (OrdenDeTrabajo ot in ordenesHijas)
                    {
                        if ((ot.horaInicio >= Convert.ToDateTime(txt_inicio.Text) && ot.horaInicio <= Convert.ToDateTime(txt_fin.Text)) || (ot.horaFin >= Convert.ToDateTime(txt_inicio.Text) && ot.horaFin <= Convert.ToDateTime(txt_fin.Text)))
                        {
                            Empleado aux = null;
                            foreach (Empleado em in empl)
                            {
                                if (em.idEmpleado == ot.empleado.idEmpleado)
                                {
                                    aux = em;
                                }
                            }
                            if (aux != null)
                            {
                                empl.Remove(aux);
                            }
                        }
                    }



                    cmb_empleado.DataSource    = empl;
                    cmb_empleado.DisplayMember = "nombre";
                    cmb_empleado.ValueMember   = "idEmpleado";
                    cmb_empleado.SelectedValue = -1;
                }
                catch (FormatException ex)
                {
                    MessageBox.Show("Error en el formato de las horas", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }


                cmb_empleado.Enabled   = true;
                cmb_maquinaria.Enabled = true;
            }
            else
            {
                MessageBox.Show("Complete las 2 Horas", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
        }
        private void btn_guardar_Click(object sender, EventArgs e)
        {
            if (_estado == estados.nuevo && validarCampos() == true)
            {
                Maquinaria     maq = new Maquinaria();
                TipoMaquinaria tm  = new TipoMaquinaria();
                tm.idTipoMaquinaria = Convert.ToInt32(cmb_tipo_maq.SelectedValue);
                maq.Nombre          = txt_nombre.Text;
                maq.descripcion     = txt_descripcion.Text;
                maq.fechaAlta       = dtp_fechaAlta.Value;
                maq.tipoMaquinaria  = tm;


                try
                {
                    MaquinariaDAO.Insert(maq);
                    MessageBox.Show("Registrado con Exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    limpiarCampos();
                    btn_guardar.Enabled = false;
                }
                catch (ApplicationException ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            else
            {
                if (_estado == estados.modificar && validarCampos() == true)
                {
                    if (txt_nombre.Text != maqModificar.Nombre)
                    {
                        string nombreMaquina = txt_nombre.Text;
                        try
                        {
                            if (verificarExistenciaMaquinaria(nombreMaquina))
                            {
                                MessageBox.Show(this, "La Maquinaria ya existe", "Atencion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }
                            else
                            {
                                desbloquearCampos();
                            }
                        }
                        catch (ApplicationException ex)
                        {
                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        }
                    }

                    Maquinaria     maq = new Maquinaria();
                    TipoMaquinaria tm  = new TipoMaquinaria();
                    tm.idTipoMaquinaria = Convert.ToInt32(cmb_tipo_maq.SelectedValue);
                    maq.Nombre          = txt_nombre.Text;
                    maq.descripcion     = txt_descripcion.Text;
                    maq.fechaAlta       = dtp_fechaAlta.Value;
                    maq.tipoMaquinaria  = tm;
                    maq.idMaquinaria    = maqModificar.idMaquinaria;

                    try
                    {
                        MaquinariaDAO.Update(maq);
                        MessageBox.Show("Actualizado con Exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        //limpiarCampos();
                        //btn_guardar.Enabled = false;
                        Close();
                        Dispose();
                    }
                    catch (ApplicationException ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
            }
        }