public async Task <IActionResult> AccountOrderList(int?id) { if (id != null) { try { AppUser appUser = await _baselDb.AppUsers .Where(z => z.UserName == HttpContext.User.Identity.Name) .Include(z => z.BillingDetail) .SingleOrDefaultAsync(); Order order = await _baselDb.Orders .Where(z => z.AppUserId == appUser.Id) .Where(z => z.Id == id) .Include(z => z.ProductOrders) .SingleOrDefaultAsync(); if (order != null) { OrderCheckOutData orderCheckOutData = new OrderCheckOutData() { AppUser = appUser, Order = order }; return(View(orderCheckOutData)); } } catch (Exception) { } } return(RedirectToAction("AccountOrder")); }
public async Task <IActionResult> OrderCheckout(string result, string userId) { ViewBag.IsOrdered = false; if (result != null && userId != null) { if (result == "Payed") { ViewBag.Result = "Thank you. Your order has been received."; ViewBag.Color = "#7a9c59"; ViewBag.IsOrdered = true; try { if (HttpContext.User.Identity.IsAuthenticated) { AppUser appUser = await _baselDb.AppUsers .Where(x => x.UserName == HttpContext.User.Identity.Name) .Where(x => x.Id == userId) .Include(x => x.BillingDetail) .Include(x => x.Cart).ThenInclude(x => x.ProductCarts) .SingleOrDefaultAsync(); List <ProductCart> productCarts = await _baselDb.Carts .Where(x => x.AppUserId == appUser.Id) .Include(x => x.ProductCarts) .SelectMany(x => x.ProductCarts) .Include(x => x.Product) .ThenInclude(x => x.ProductLanguages) .ToListAsync(); int?cultures = GetLanguage.GetLangId(); List <ProductOrder> productOrders = new List <ProductOrder>(); foreach (ProductCart item in productCarts) { productOrders.Add(new ProductOrder() { ProductId = item.ProductId, ProductCount = item.ProductCount, ProductName = item.Product.ProductLanguages .Where(z => z.LanguageId == cultures) .Select(z => z.Name).SingleOrDefault(), ProductPrice = item.Product.Price }); } Random random = new Random(); int OrderNumber = random.Next(1, 999999); string TotalPrice = HttpContext.Session.GetString("userTotalPrice"); if (TotalPrice != null) { List <Order> orders = new List <Order>(); string[] total = TotalPrice.Split(","); decimal userTotalPrice = Convert.ToDecimal(total[0]); Order order = new Order() { AppUserId = appUser.Id, AppUser = appUser, Date = DateTime.Now, Number = OrderNumber, Status = "Full", Total = userTotalPrice, ProductOrders = productOrders }; orders.Add(order); appUser.Orders = orders; await _baselDb.Orders.AddAsync(order); OrderCheckOutData orderCheckOutData = new OrderCheckOutData() { ProductOrders = productOrders, AppUser = appUser }; foreach (ProductCart item in productCarts) { appUser.Cart.ProductCarts.Remove(item); } _baselDb.SaveChanges(); return(View(orderCheckOutData)); } } } catch (Exception exp) { ViewBag.Result = exp.Message; ViewBag.Color = "red"; ViewBag.IsOrdered = false; } } else if (result == "Low Balance") { ViewBag.Result = "There is not enough balance on your card"; ViewBag.Color = "red"; ViewBag.IsOrdered = false; } else if (result == "Not Found TotalPrice") { ViewBag.Result = "Amount not included"; ViewBag.Color = "red"; ViewBag.IsOrdered = false; } else { ViewBag.Result = "No data found"; ViewBag.Color = "red"; ViewBag.IsOrdered = false; } return(View()); } return(RedirectToAction("Error", "Home")); }