Ejemplo n.º 1
0
        public bool Add(VentaViewModel modelo)
        {
            bool ok = false;

            using (DbContextTransaction transaction = db.Database.BeginTransaction())
            {
                try
                {
                    //Agregar encabezado de venta
                    db.Venta.Add(new Venta
                    {
                        ClienteId  = modelo.VentaCliente.ClienteId,
                        UsuarioId  = modelo.VentaUsuario.UsuarioId,
                        VentaFecha = modelo.Fecha
                    });
                    db.SaveChanges();

                    //Obtener el codigo de la venta ingresada
                    var ventaId = db.Venta.ToList().Select(v => v.VentaId).Max();

                    //Insertar el detalle de la venta
                    foreach (var item in modelo.VentaDetalle)
                    {
                        var ventaDetalle = new VentaDetalle
                        {
                            VentaId              = ventaId,
                            ProductoId           = item.ProductoId,
                            VentaDetalleCantidad = item.Cantidad,
                            VentaDetallePrecio   = item.Precio
                        };
                        db.VentaDetalle.Add(ventaDetalle);
                        db.SaveChanges();

                        //Reducir la existencia del producto
                        var producto = db.Producto.Find(ventaDetalle.ProductoId);
                        producto.ProductoStock = producto.ProductoStock - ventaDetalle.VentaDetalleCantidad;

                        db.Entry(producto).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    transaction.Commit();
                    ok = true;
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return(ok);
                }
            }

            return(ok);
        }
Ejemplo n.º 2
0
 public bool Edit(Producto Producto)
 {
     try
     {
         db.Entry(Producto).State = EntityState.Modified;
         db.SaveChanges();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Ejemplo n.º 3
0
 public bool Edit(Cotegoria categoria)
 {
     try
     {
         db.Entry(categoria).State = EntityState.Modified;
         db.SaveChanges();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }