public static bool Eliminar(int id) { Contexto context = new Contexto(); bool found = false; try { Ordenes orden = Buscar(id); if (orden != null) { foreach (OrdenesDetalle d in orden.Detalle) { Productos p = ProductosBLL.Buscar(d.ProductoId); p.Inventario -= d.Cantidad; ProductosBLL.Modificar(p); } context.Ordenes.Remove(orden); found = context.SaveChanges() > 0; } } catch { throw; } finally { context.Dispose(); } return(found); }
private static bool Insertar(Ordenes orden) { Contexto context = new Contexto(); bool found = false; try { foreach (OrdenesDetalle d in orden.Detalle) { Productos p = ProductosBLL.Buscar(d.ProductoId); p.Inventario += d.Cantidad; ProductosBLL.Modificar(p); } context.Ordenes.Add(orden); found = context.SaveChanges() > 0; } catch { throw; } finally { context.Dispose(); } return(found); }
public static bool Modificar(Ordenes orden) { Contexto context = new Contexto(); bool found = false; try { foreach (OrdenesDetalle d in Buscar(orden.OrdenId).Detalle) { Productos p = ProductosBLL.Buscar(d.ProductoId); p.Inventario -= d.Cantidad; ProductosBLL.Modificar(p); } //Agrega al inventario la nueva cantidad asignada foreach (OrdenesDetalle d in orden.Detalle) { Productos p = ProductosBLL.Buscar(d.ProductoId); p.Inventario += d.Cantidad; ProductosBLL.Modificar(p); } context.Database.ExecuteSqlRaw($"delete from OrdenesDetalle where OrdenId = {orden.OrdenId}"); foreach (var anterior in orden.Detalle) { context.Entry(anterior).State = EntityState.Added; } context.Entry(orden).State = EntityState.Modified; found = context.SaveChanges() > 0; } catch { throw; } finally { context.Dispose(); } return(found); }