public IActionResult BuySCArticles() { var userName = User.Identity.Name; var userId = context.Users.Single(u => u.UserName == userName).Id; var carItems = (from a in context.ShoppingCars join b in context.ShoppingLists on a.ShoppingListId equals b.ShoppingListId join c in context.ShoppingList_Articles on b.ShoppingListId equals c.ShoppingListId where b.IsMainList == true && a.UserId == userId select new { c.Article, c.Amount }).ToList(); if (carItems.Count == 0) { return(RedirectToAction("Index", "ShoppingCart")); } var buyViewModel = new BuySCArticleViewModel { CustomerId = userId, Total = 0 }; List <ShoppingListArticuleViewModel> articlesToBuy = new List <ShoppingListArticuleViewModel>(); foreach (var item in carItems) { var ad = context.Adds.Include(a => a.User).SingleOrDefault(b => b.ArticleId == item.Article.ArticleId); ShoppingListArticuleViewModel slArticle = new ShoppingListArticuleViewModel { ArticleId = item.Article.ArticleId, Name = item.Article.Name, Price = item.Article.Price }; if (ad.Amount >= item.Amount)//si hay disponible la ctdad necesaria compralo todo { slArticle.Amount = item.Amount; articlesToBuy.Add(slArticle); buyViewModel.Total += slArticle.TotalPrice; } else//sino comprar todo lo que queda y reportar que no se pudo obtener todo, a no ser que se haya agotado. { if (ad.Amount > 0) { slArticle.Amount = ad.Amount; articlesToBuy.Add(slArticle); } buyViewModel.Incomplete = true; buyViewModel.Total += slArticle.TotalPrice; } } buyViewModel.SCArticles = articlesToBuy; return(View(buyViewModel)); }
public IActionResult EffectSCBuy(BuySCArticleViewModel bvm) { if (!ModelState.IsValid) { BuySCArticleViewModel newBvm = new BuySCArticleViewModel { CustomerId = bvm.CustomerId, Total = bvm.Total, Incomplete = bvm.Incomplete, Address = bvm.Address, SCArticles = bvm.SCArticles }; return(View("BuySCArticles", newBvm)); } var date = DateTime.Now; foreach (var article in bvm.SCArticles) { var newOrder = new Order {//poner un booleano o algo que represente que la entrega fue de shopping car, por tanto el cobro por esta es en general CustomerId = bvm.CustomerId, ArticleId = article.ArticleId, Date = date, Amount = article.Amount, Address = bvm.Address, ShippingCharge = bvm.ShippingCharge, SharedOrder = true }; context.Orders.Add(newOrder); context.SaveChanges(); } var totalCharge = bvm.Total + bvm.ShippingCharge; return(RedirectToAction("GetPaymentMethod", "PaymentGateway", new { userId = bvm.CustomerId, articleId = -1, orderDateTicks = date.Ticks, charge = totalCharge, orderType = 1 })); }