private void btnModificar_Click(object sender, EventArgs e) { try { int id = (int)dataGridAlmacenes.CurrentRow.Cells["Id"].Value; Almacen a = Almacen.Select(id); ModificarAlmacen f = new ModificarAlmacen(a, this); f.ShowDialog(); } catch (Exception ex) { MessageBox.Show(this, "Se debe seleccionar un almacén", "Seleccione uno", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public Producto(int Id, Alimento Alimento, DateTime FechaEntrada, DateTime FechaCaducidad, DateTime FechaConsumirPreferente, string Proveedor, Almacen Almacen, string Ubicacion, Entidad entidad, float Cantidad) { this.Id = Id; this.Alimento = Alimento; this.FechaEntrada = FechaEntrada; this.FechaCaducidad = FechaCaducidad; this.FechaConsumirPreferente = FechaConsumirPreferente; this.Proveedor = Proveedor; this.Almacen = Almacen; this.Ubicacion = Ubicacion; this.Entidad = entidad; this.Cantidad = Cantidad; }
/// <summary> /// Devuelve un ArrayList de los productos que estan en un determinado almacen /// </summary> /// <param name="a">El alimento</param> /// <returns>La lista de Productos</returns> public static ArrayList SelectWhereAlmacenIs(Almacen a) { ArrayList productos = new ArrayList(); using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString)) { conn.Open(); string selectString = "select * from Producto where Almacen = @almacen"; using (SqlCommand selectCommand = new SqlCommand(selectString, conn)) { selectCommand.Parameters.Add("@almacen", SqlDbType.Int).Value = a.Id; SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection); while (reader.Read()) { Producto p = new Producto(); p.Id = reader.GetInt32(0); p.Alimento = Alimento.Select(reader.GetInt32(1)); p.FechaEntrada = reader.GetDateTime(2); p.FechaCaducidad = reader.GetDateTime(3); p.FechaConsumirPreferente = reader.GetDateTime(4); p.Proveedor = reader.GetString(5); p.Ubicacion = reader.GetString(6); try { p.Almacen = Almacen.Select(reader.GetInt32(7)); } catch (Exception ex) { p.Almacen = null; } try { p.Entidad = Entidad.Select(reader.GetInt32(8)); } catch (Exception ex) { p.Entidad = null; } productos.Add(p); } conn.Close(); } } return(productos); }
/// <summary> /// Devuelve un Almacen dado su Id /// </summary> /// <param name="Id">Id del almacén</param> /// <returns>El almacén</returns> public static Almacen Select(string Nombre) { Almacen a = null; using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString)) { string selectString = "select * from Almacen where Nombre = @nombre"; using (SqlCommand selectCommand = new SqlCommand(selectString, conn)) { conn.Open(); selectCommand.Parameters.Add("@nombre", SqlDbType.VarChar).Value = Nombre; SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection); while (reader.Read()) { a = new Almacen(); a.Id = reader.GetInt32(0); a.Nombre = reader.GetString(1); a.Descripcion = reader.GetString(2); } } } return(a); }
/// <summary> /// Devuelve un ArrayList con los almacenes /// </summary> /// <returns> /// Todos los almacenes /// </returns> public static ArrayList SelectAll() { ArrayList almacenes = new ArrayList(); using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString)) { conn.Open(); string selectString = "select * from Almacen"; using (SqlCommand selectCommand = new SqlCommand(selectString, conn)) { SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection); while (reader.Read()) { Almacen a = new Almacen(); a.Id = reader.GetInt32(0); a.Nombre = reader.GetString(1); a.Descripcion = reader.GetString(2); almacenes.Add(a); } } } return(almacenes); }
private void btnEliminar_Click(object sender, EventArgs e) { try { int id = (int)dataGridAlmacenes.CurrentRow.Cells["Id"].Value; Almacen a = Almacen.Select(id); if (MessageBox.Show(this, String.Format("Se va ha eliminar el almacén '{0}' ¿Está seguro?", a.Nombre), "Eliminar almacén", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.No) { return; } System.Collections.ArrayList productos = Producto.SelectWhereAlmacenIs(a); if (productos.Count > 0) { if (MessageBox.Show(this, String.Format("El almacén '{0}' tiene productos, si se elimina se eliminarán también los productos ¿Está seguro?", a.Nombre), "Eliminar almacén", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.No) { return; } } else { return; } if (a.Delete()) { MessageBox.Show(this, "Se eliminado el almacén correctamente.", "Almacén eliminado", MessageBoxButtons.OK, MessageBoxIcon.Information); cargarDataGridView(); } else { MessageBox.Show(this, "Se ha modificado el almacén correctamente.", "Almacén modificado", MessageBoxButtons.OK, MessageBoxIcon.Information); } }catch (Exception ex) { MessageBox.Show(this, "Se debe seleccionar un almacén", "Seleccione uno", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void crearProducto_Load(object sender, EventArgs e) { ArrayList alimentos = Alimento.SelectAll(); cmb_alimentos.Items.Clear(); for (int i = 0; i < alimentos.Count; i++) { Alimento a = (Alimento)alimentos[i]; cmb_alimentos.Items.Add(a.Nombre); } ArrayList almacenes = Almacen.SelectAll(); cbxAlmacen.Items.Clear(); for (int i = 0; i < almacenes.Count; i++) { Almacen a = (Almacen)almacenes[i]; cbxAlmacen.Items.Add(a.Nombre); } ArrayList entidades = Entidad.SelectAll(); cbxEntidades.Items.Clear(); for (int i = 0; i < entidades.Count; i++) { Entidad en = (Entidad)entidades[i]; if (en.Direccion != "") { cbxEntidades.Items.Add(en.Nombre + "-" + en.Direccion); } else { cbxEntidades.Items.Add(en.Nombre); } } }
private void btnAnadir_Click(object sender, EventArgs e) { if (cmb_alimentos.Text == "") { MessageBox.Show(this, "El campo Alimento es obligatorio", "Rellena los campos", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } if (!cmb_alimentos.Items.Contains(cmb_alimentos.Text)) { MessageBox.Show(this, "El Alimento no existe", "Eliga un Alimento ya creado", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } if (comboBoxOrigen.Text == "") { MessageBox.Show(this, "El Origen no existe", "Eliga un Origen ya creado", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } if (cbxAlmacen.Text == "") { MessageBox.Show(this, "El Almacen no existe", "Eliga un Almacen ya creado", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } if (cbxEntidades.Text == "") { MessageBox.Show(this, "El Proveedor no existe", "Eliga un Proveedor ya creado", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } float cant = float.Parse(txtCantidad.Text); if (cant == 0) { MessageBox.Show(this, "Seleccione una cantidad superior a 0", "Escriba una cantidad", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } if (MessageBox.Show(this, "Se va" + (cant == 1 ? "" : "n") + " a registrar " + cant + " producto" + (cant == 1 ? "" : "s") + " del alimento " + cmb_alimentos.Text + ". ¿Está seguro?", "Añadir Productos", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.No) { return; } Producto p = new Producto(); p.Alimento = Alimento.SelectWhereNombreIs(cmb_alimentos.Text); p.Almacen = Almacen.Select(cbxAlmacen.SelectedItem.ToString()); p.FechaEntrada = dateTimeFechaEn.Value; p.FechaCaducidad = dateTimeFechaCad.Value; p.FechaConsumirPreferente = dateTimeFechaPref.Value; p.Proveedor = comboBoxOrigen.SelectedItem.ToString(); try { p.Entidad = Entidad.SelectByName((cbxEntidades.SelectedItem.ToString()).Split('-')[0], (cbxEntidades.SelectedItem.ToString()).Split('-')[1]); } catch (Exception ex) { p.Entidad = Entidad.SelectByName((cbxEntidades.SelectedItem.ToString()), ""); } p.Cantidad = cant; bool ok = false; if (txtCantidad.Text != "") { ok = p.Insert(); } // Feedback! if (!ok) { MessageBox.Show(this, "Se ha producido un error insertando el producto", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { //MessageBox.Show(this, "Se añadido un nuevo producto correctamente.", "Producto añadido", MessageBoxButtons.OK, MessageBoxIcon.Information); cmb_alimentos.Text = ""; txtCantidad.Text = "0"; this.fProductos.cargarDataGridView(); } }