Пример #1
0
        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);
        }
Пример #2
0
        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);
        }