public bool Eliminar(int id) { bool paso = false; Contexto db = new Contexto(); ProductosControllers controllers = new ProductosControllers(); try { Ventas ventas = Buscar(id); if (ventas != null) { foreach (var item in ventas.VentasDetalles) { var temp = controllers.Buscar(item.ProductoId); temp.Cantidad += item.Cantidad; controllers.Guardar(temp); } db.Entry(ventas).State = EntityState.Deleted; paso = db.SaveChanges() > 0; } } catch (Exception) { throw; } return(paso); }
private bool Modificar(Ventas ventas) { bool paso = false; Contexto db = new Contexto(); ProductosControllers controllers = new ProductosControllers(); try { Ventas anterior = Buscar(ventas.VentaId); foreach (var item in anterior.VentasDetalles) { var temp = controllers.Buscar(item.ProductoId); temp.Cantidad += item.Cantidad; controllers.Guardar(temp); } foreach (var item in ventas.VentasDetalles) { if (item.Id == 0) { db.Entry(item).State = EntityState.Added; } } foreach (var item in anterior.VentasDetalles) { if (!ventas.VentasDetalles.Any(A => A.Id == item.Id)) { db.Entry(item).State = EntityState.Deleted; } } db.Entry(ventas).State = EntityState.Modified; paso = db.SaveChanges() > 0; //Modificar los productos cuando se modifique la venta; foreach (var item in ventas.VentasDetalles) { var temp = controllers.Buscar(item.ProductoId); temp.Cantidad -= item.Cantidad; controllers.Guardar(temp); } } catch (Exception) { throw; } return(paso); }
private bool Insertar(Ventas ventas) { bool paso = false; Contexto db = new Contexto(); ProductosControllers controllers = new ProductosControllers(); try { foreach (var item in ventas.VentasDetalles) { Productos temp = controllers.Buscar(item.ProductoId); temp.Cantidad -= item.Cantidad; controllers.Guardar(temp); } db.Ventas.Add(ventas); paso = db.SaveChanges() > 0; } catch (Exception) { throw; } return(paso); }