public async Task <ActionResult> AddProduct(BudgetViewModel model)
        {
            if (ModelState.IsValid)
            {
                // quantity <= stock siempre se debe hacer
                var product = await db.Products.FindAsync(model.ProductId);

                if (product == null)
                {
                    TempData["ErrorMessage"] = "Producto no encontrado";
                    return(RedirectToAction("Edit", new { id = model.BudgetId }));
                }

                if (product.ProductStock < model.Quantity)
                {
                    TempData["ErrorMessage"] = $"No hay stock suficiente, nos quedan {product.ProductStock} unidades ";
                    return(RedirectToAction("Edit", new { id = model.BudgetId }));
                }
                var budgetProduct = new BudgetProduct
                {
                    ProductId = model.ProductId,
                    Price     = product.ProductPrice,
                    BudgetId  = model.BudgetId,
                    Quantity  = model.Quantity
                };
                db.BudgetProducts.Add(budgetProduct);
                await db.SaveChangesAsync();

                TempData["SuccessMessage"] = "Se agrego correctamente el producto";
                return(RedirectToAction("Edit", new { id = model.BudgetId }));
            }

            TempData["ErrorMessage"] = "Faltan datos requeridos";
            return(RedirectToAction("Edit", new { id = model.BudgetId }));
        }
Example #2
0
 public void GenerateContext(BudgetProduct budgetProduct)
 {
     // Geração do contexto do produto
 }