public static bool Guardar(CotizarArticulos articulo) { bool paso = false; Contexto contexto = new Contexto(); try { if (contexto.Cotizar.Add(articulo) != null) { foreach (var item in articulo.Detalle) { contexto.Articulos.Find(item.ArticuloId).CantCotizada += item.Cantidad; } contexto.SaveChanges(); paso = true; } } catch (Exception) { throw; } finally { contexto.Dispose(); } return(paso); }
public static bool Eliminar(int id) { bool paso = false; Contexto contexto = new Contexto(); try { CotizarArticulos articulo = contexto.Cotizar.Find(id); foreach (var item in articulo.Detalle) { var cotizar = contexto.Articulos.Find(item.ArticuloId).CantCotizada -= item.Cantidad; } contexto.Cotizar.Remove(articulo); if (contexto.SaveChanges() > 0) { paso = true; } } catch (Exception) { throw; } finally { contexto.Dispose(); } return(paso); }
public static CotizarArticulos Buscar(int id) { Contexto contexto = new Contexto(); CotizarArticulos articulo = new CotizarArticulos(); try { articulo = contexto.Cotizar.Find(id); articulo.Detalle.Count(); foreach (var item in articulo.Detalle) { string d = item.Articulo.Descripcion; string n = item.Persona.Nombres; } } catch (Exception) { throw; } finally { contexto.Dispose(); } return(articulo); }
private void Buscar_button_Click(object sender, EventArgs e) { int id = Convert.ToInt32(Id_numericUpDown.Value); CotizarArticulos articulo = BLL.CotizarArticulosBLL.Buscar(id); if (articulo != null) { LlenaCampos(articulo); } else { MessageBox.Show("No hay resultados!!", "Busqueda Fallida!!", MessageBoxButtons.OK, MessageBoxIcon.Stop); } }
//llenar los campos del registro private void LlenaCampos(CotizarArticulos cotizar) { Id_numericUpDown.Value = cotizar.CotizarId; FechaCotizacion_dateTimePicker.Value = cotizar.Fecha; Total_numericUpDown.Value = cotizar.Total; Comentarios_textBox.Text = cotizar.Comentarios; DetalleDataGridView.DataSource = cotizar.Detalle; DetalleDataGridView.Columns["Id"].Visible = false; DetalleDataGridView.Columns["CotizarId"].Visible = false; DetalleDataGridView.Columns["ArticuloId"].Visible = false; DetalleDataGridView.Columns["PersonaId"].Visible = false; }
public static bool Modificar(CotizarArticulos articulo) { bool paso = false; Contexto contexto = new Contexto(); try { var cotizacionAnt = BLL.CotizarArticulosBLL.Buscar(articulo.CotizarId); foreach (var item in cotizacionAnt.Detalle) { contexto.Articulos.Find(item.ArticuloId).CantCotizada -= item.Cantidad; if (!articulo.Detalle.ToList().Exists(a => a.Id == item.Id)) { contexto.Articulos.Find(item.ArticuloId).CantCotizada -= item.Cantidad; item.Articulo = null; contexto.Entry(item).State = EntityState.Deleted; } } foreach (var item in articulo.Detalle) { contexto.Articulos.Find(item.ArticuloId).CantCotizada += item.Cantidad; var estado = item.Id > 0 ? EntityState.Modified : EntityState.Added; contexto.Entry(item).State = estado; } contexto.Entry(articulo).State = EntityState.Modified; if (contexto.SaveChanges() > 0) { paso = true; } } catch (Exception) { throw; } finally { contexto.Dispose(); } return(paso); }
//llenar la entidad con los datos private CotizarArticulos LlenaClase() { CotizarArticulos cotizar = new CotizarArticulos(); cotizar.CotizarId = Convert.ToInt32(Id_numericUpDown.Value); cotizar.Fecha = FechaCotizacion_dateTimePicker.Value.Date; cotizar.Total = Total_numericUpDown.Value; cotizar.Comentarios = Comentarios_textBox.Text; foreach (DataGridViewRow item in DetalleDataGridView.Rows) { cotizar.AgregarDetalle( ToInt(item.Cells["Id"].Value), ToInt(item.Cells["CotizarId"].Value), ToInt(item.Cells["ArticuloId"].Value), ToInt(item.Cells["PersonaId"].Value), ToInt(item.Cells["Cantidad"].Value), ToInt(item.Cells["Precio"].Value), ToInt(item.Cells["Importe"].Value) ); } return(cotizar); }