public static bool Eliminar(int id) { bool paso = false; Contexto contexto = new Contexto(); try { var orden = OrdenesBLL.Buscar(id); Productos producto; List <OrdenesDetalle> viejosDetalles = Buscar(orden.OrdenId).Detalle; foreach (OrdenesDetalle d in viejosDetalles) { producto = ProductosBLL.Buscar(d.ProductoId); producto.Inventario -= d.Cantidad; ProductosBLL.Guardar(producto); } if (orden != null) { contexto.Entry(orden).State = EntityState.Deleted; paso = contexto.SaveChanges() > 0; } } catch (Exception) { throw; } finally { contexto.Dispose(); } return(paso); }
private static bool Insertar(Ordenes orden) { bool paso = false; Contexto contexto = new Contexto(); try { contexto.Ordenes.Add(orden); paso = contexto.SaveChanges() > 0; Productos producto; List <OrdenesDetalle> detalle = orden.Detalle; foreach (OrdenesDetalle m in detalle) { producto = ProductosBLL.Buscar(m.ProductoId); producto.Inventario += m.Cantidad; ProductosBLL.Guardar(producto); } } catch (Exception) { throw; } finally { contexto.Dispose(); } return(paso); }
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); }
private static bool Modificar(Ordenes orden) { bool paso = false; Contexto contexto = new Contexto(); try { Productos producto; List <OrdenesDetalle> detalle = Buscar(orden.OrdenId).Detalle; foreach (OrdenesDetalle m in detalle) { producto = ProductosBLL.Buscar(m.ProductoId); producto.Inventario -= m.Cantidad; ProductosBLL.Guardar(producto); } contexto.Database.ExecuteSqlRaw($"Delete FROM OrdenesDetalle Where OrdenId={orden.OrdenId}"); foreach (var item in orden.Detalle) { contexto.Entry(item).State = EntityState.Added; } List <OrdenesDetalle> nuevo = orden.Detalle; foreach (OrdenesDetalle m in nuevo) { producto = ProductosBLL.Buscar(m.ProductoId); producto.Inventario += m.Cantidad; ProductosBLL.Guardar(producto); } contexto.Entry(orden).State = EntityState.Modified; paso = contexto.SaveChanges() > 0; } catch (Exception) { throw; } finally { contexto.Dispose(); } return(paso); }
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); }