public async Task<ActionResult> SaldoDisponible(FormCollection values)
        {
            var order = new tb_factura();
            var asociado = db.tb_asociado.FirstOrDefault(a => a.correo_electronico == User.Identity.Name);
            var cart = ShoppingCart.GetCart(this.HttpContext);
            //var previousOrder = db.tb_factura.FirstOrDefault(x => x.usuario == User.Identity.Name);
            var compra = new ProcesarCompra();
            compra.asociado = asociado;
            compra.montoCompra = cart.GetTotal();
            compra.saldo = (decimal)asociado.monto_ahorro - cart.GetTotal();
            try
            {
                order.cliente_asociado = asociado.id_asociado;
                order.id_direccion = 1;
                order.id_estado = 1;
                order.costo_total = cart.GetTotal();
                order.fecha = DateTime.Now;
                order.usuario = asociado.correo_electronico;

                //Guardar Orden
                db.tb_factura.Add(order);

                asociado.monto_ahorro = (decimal)(asociado.monto_ahorro - order.costo_total);
                await db.SaveChangesAsync();
                //Procesar la orden
                order = cart.CreateOrder(order);


                return RedirectToAction("Completar",
                    new { id = order.id_factura });

            }
            catch(Exception e)
            {
                //Invalid - redisplay with errors
                compra.Message = "Hubo un error en la compra: " + e.Message;
                return View(compra);
            }
        }
 public async Task<ActionResult> Edit(tb_factura order)
 {
     if (ModelState.IsValid)
     {
         db.Entry(order).State = EntityState.Modified;
         await db.SaveChangesAsync();
         return RedirectToAction("Index");
     }
     return View(order);
 }
Ejemplo n.º 3
0
        public tb_factura CreateOrder(tb_factura order)
        {
            decimal orderTotal = 0;
            order.tb_detalle = new List<tb_detalle>();

            var cartItems = GetCartItems();
            // Iterate over the items in the cart, 
            // adding the order details for each
            foreach (var item in cartItems)
            {
                var orderDetail = new tb_detalle
                {
                    id_producto = item.id_producto,
                    id_factura = order.id_factura,
                    costo_total = item.tb_producto.costo,
                    cantidad = item.cantidad
                };
                // Set the order total of the shopping cart
                orderTotal += (item.cantidad * item.tb_producto.costo);
                order.tb_detalle.Add(orderDetail);
                db.tb_detalle.Add(orderDetail);

            }

            // Set the order's total to the orderTotal count
            order.costo_total = orderTotal;

            // Save the order
            db.SaveChanges();
            // Empty the shopping cart
            EmptyCart();
            // Return the OrderId as the confirmation number
            return order;
        }
        public async Task<ActionResult> Create(tb_factura order)
        {
            if (ModelState.IsValid)
            {
                db.tb_factura.Add(order);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(order);
        }