public ActionResult Create(AddressCheckModel model) { var customer = workContext.GetAuthenticatedCustomer(); if (customer == null) { return(RedirectToAction("SingIn", "Login")); } #region ShoppingCart var cartList = db.ShoppingCartItems.Where(x => x.CustomerId == customer.Id).ToList(); decimal?totalprice = 0; foreach (var item in cartList) { var product = db.Products.FirstOrDefault(x => x.Id == item.ProductId); var ShoppingCart = new ShoppingCartModel(); ShoppingCart.Id = item.Id; ShoppingCart.Quantity = item.Quantity; ShoppingCart.ProductId = product.Id; ShoppingCart.Price = product.Price; ShoppingCart.CustomerId = item.CustomerId; totalprice += product.Price * item.Quantity; model.ShoppingCarts.Add(ShoppingCart); } #endregion #region OrderCerate Order order = new Order(); order.CustomerId = customer.Id; order.AddressId = model.AddressId; order.ShippingMethod = model.Cargo; order.OrderStatusId = (int)OrderStatusEnum.Preparing; order.ShippingStatusId = (int)ShippingStatusEnum.NotYetShipped; order.OrderTotal = totalprice; order.Deleted = false; order.CreatedOnUtc = DateTime.Now; db.Orders.Add(order); if (db.SaveChanges() > 0) { foreach (var product in model.ShoppingCarts) { OrderItem oi = new OrderItem(); oi.OrderId = order.Id; oi.ProductId = product.ProductId; oi.Quantity = product.Quantity; oi.Price = product.Price; db.OrderItems.Add(oi); if (db.SaveChanges() > 0) { var cart = db.ShoppingCartItems.SingleOrDefault(x => x.Id == product.Id && x.CustomerId == customer.Id); if (cart != null) { db.ShoppingCartItems.Remove(cart); db.SaveChanges(); } } } } #endregion return(RedirectToAction("Index", "Home")); }
public ActionResult AddressCheck() { var customer = workContext.GetAuthenticatedCustomer(); if (customer == null) { return(RedirectToAction("SingIn", "Login")); } var cartList = db.ShoppingCartItems.Where(x => x.CustomerId == customer.Id).ToList(); AddressCheckModel model = new AddressCheckModel(); #region ShoppingCart decimal?totalprice = 0; foreach (var item in cartList) { var product = db.Products.FirstOrDefault(x => x.Id == item.ProductId); var ShoppingCart = new ShoppingCartModel(); ShoppingCart.Id = item.Id; ShoppingCart.Quantity = item.Quantity; ShoppingCart.ProductName = product.Name; ShoppingCart.ProductId = product.Id; ShoppingCart.Price = product.Price; ShoppingCart.Photo = product.Photo; ShoppingCart.CustomerId = item.CustomerId; totalprice += product.Price * item.Quantity; model.ShoppingCarts.Add(ShoppingCart); } model.TotalPrice = totalprice; model.TotalQuantity = cartList.Count; #endregion #region Address var cam = db.CustomerAddressMappings.Where(x => x.CustomerId == customer.Id).ToList(); foreach (var addressId in cam) { var address = db.Addresses.FirstOrDefault(x => x.Id == addressId.AddressId); model.AvailableAddress.Add(new SelectListItem { Text = address.FirsName, Value = address.Id.ToString() }); } #endregion #region Cargo model.AvailableCargo.Add(new SelectListItem { Text = "Aras Kargo", Value = "Aras Kargo" }); model.AvailableCargo.Add(new SelectListItem { Text = "Yurtiçi Kargo", Value = "Yurtiçi Kargo" }); model.AvailableCargo.Add(new SelectListItem { Text = "MNG Kargo", Value = "MNG Kargo" }); model.AvailableCargo.Add(new SelectListItem { Text = "UPS Kargo", Value = "UPS Kargo" }); #endregion return(View(model)); }