Esempio n. 1
0
        public int CreateOrder(Order order)
        {
            decimal orderTotal = 0;

            var cartItems = GetCartItems();

            // Iterate over the items in the cart, adding the order details for each
            foreach (var item in cartItems)
            {
                //var product = _db.Products.Find(item.ProductId);
                var product = _db.Products.Single(a => a.ProductId == item.ProductId);

                var orderDetail = new OrderDetail
                {
                    ProductId = item.ProductId,
                    OrderId = order.OrderId,
                    UnitPrice = product.Price,
                    Quantity = item.Count,
                };

                // Set the order total of the shopping cart
                orderTotal += (item.Count * product.Price);

                _db.OrderDetails.Add(orderDetail);
            }

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

            // Empty the shopping cart
            EmptyCart();

            // Return the OrderId as the confirmation number
            return order.OrderId;
        }
Esempio n. 2
0
        public async Task<ActionResult> AddressAndPayment(Order order)
        {
            var formCollection = Request.Form;

            try
            {
                if (string.Equals(formCollection.GetValues("PromoCode").FirstOrDefault(), PromoCode,
                    StringComparison.OrdinalIgnoreCase) == false)
                {
                    return View(order);
                }
                else
                {
                    order.Username = User.Identity.GetUserName();
                    order.OrderDate = DateTime.Now;

                    //Add the Order
                    db.Orders.Add(order);

                    //Process the order
                    var cart = ShoppingCart.GetCart(db, HttpContext);
                    cart.CreateOrder(order);

                    // Save all changes
                    await db.SaveChangesAsync(CancellationToken.None);

                    return RedirectToAction("Complete", new { id = order.OrderId });
                }
            }
            catch
            {
                //Invalid - redisplay with errors
                return View(order);
            }
        }
        //
        // GET: /Checkout/

        public async Task<IActionResult> AddressAndPayment()
        {
            var id = User.GetUserId();
            var user = await _db.Users.FirstOrDefaultAsync(o => o.Id == id);

            var order = new Order
            {
                Name = user.Name,
                Email = user.Email,
                Username = user.UserName
            };

            return View(order);
        }
        //
        // GET: /Checkout/

        public async Task <IActionResult> AddressAndPayment()
        {
            var id   = _userManager.GetUserId(User);
            var user = await _db.Users.FirstOrDefaultAsync(o => o.Id == id);

            user.Name     = user.Name;
            user.Email    = user.Email;
            user.UserName = user.UserName;
            var order = new Order
            {
                Name  = user.Name,
                Email = user.Email,
            };

            return(View(order));
        }
        //
        // GET: /Checkout/Complete

        public IActionResult Complete(int id)
        {
            // Validate customer owns this order
            Order order = _db.Orders.FirstOrDefault(
                o => o.OrderId == id &&
                o.Username == HttpContext.User.Identity.Name);

            if (order != null)
            {
                return(View(order));
            }
            else
            {
                return(View("Error"));
            }
        }
        public async Task <IActionResult> AddressAndPayment(Order order, string finalamount)
        {
            TempData["FinalPayment"] = finalamount;
            string temp = Regex.Replace(finalamount, "[$]", "");

            HttpContext.Session.SetString("FinalAmount", temp);
            var formCollection = await HttpContext.Request.ReadFormAsync();

            try
            {
                if (string.Equals(formCollection["PromoCode"].FirstOrDefault(), PromoCode,
                                  StringComparison.OrdinalIgnoreCase) == false)
                {
                    return(View(order));
                }
                else
                {
                    order.Username  = HttpContext.User.Identity.Name;
                    order.OrderDate = DateTime.Now;

                    //Add the Order
                    _db.Orders.Add(order);

                    //Process the order
                    var cart = ShoppingCart.GetCart(_db, HttpContext);
                    cart.CreateOrder(order);

                    // Save all changes
                    await _db.SaveChangesAsync(HttpContext.RequestAborted);

                    TempData["OrderID"] = order.OrderId;
                    return(RedirectToAction("Complete",
                                            new { id = order.OrderId }));
                }
            }
            catch
            {
                //Invalid - redisplay with errors
                return(View(order));
            }
        }
        public async Task<IActionResult> AddressAndPayment(Order order)
        {
            var formCollection = await HttpContext.Request.ReadFormAsync();

            try
            {
                if (string.Equals(formCollection["PromoCode"].FirstOrDefault(), PromoCode,
                    StringComparison.OrdinalIgnoreCase) == false)
                {
                    return View(order);
                }
                else
                {
                    order.Username = HttpContext.User.GetUserName();
                    order.OrderDate = DateTime.Now;

                    //Add the Order
                    _db.Orders.Add(order);

                    //Process the order
                    var cart = ShoppingCart.GetCart(_db, HttpContext);
                    cart.CreateOrder(order);

                    // Save all changes
                    await _db.SaveChangesAsync(HttpContext.RequestAborted);

                    return RedirectToAction("Complete",
                        new { id = order.OrderId });
                }
            }
            catch
            {
                //Invalid - redisplay with errors
                return View(order);
            }
        }
        private static void PopulateOrderHistory(IServiceProvider serviceProvider, IEnumerable <Product> products)
        {
            var random = new Random(1234);
            var recomendationCombinations = new[] {
                new{ Transactions = new [] { 1, 3, 8 }, Multiplier = 60 },
                new{ Transactions = new [] { 2, 6 }, Multiplier = 10 },
                new{ Transactions = new [] { 4, 11 }, Multiplier = 20 },
                new{ Transactions = new [] { 5, 14 }, Multiplier = 10 },
                new{ Transactions = new [] { 6, 16, 18 }, Multiplier = 20 },
                new{ Transactions = new [] { 7, 17 }, Multiplier = 25 },
                new{ Transactions = new [] { 8, 1 }, Multiplier = 5 },
                new{ Transactions = new [] { 10, 17, 9 }, Multiplier = 15 },
                new{ Transactions = new [] { 11, 5 }, Multiplier = 15 },
                new{ Transactions = new [] { 12, 8 }, Multiplier = 5 },
                new{ Transactions = new [] { 13, 15 }, Multiplier = 50 },
                new{ Transactions = new [] { 14, 15 }, Multiplier = 30 },
                new{ Transactions = new [] { 16, 18 }, Multiplier = 80 }
            };

            IConfigurationSection configuration = GetAdminRoleConfiguration(serviceProvider);
            string userName = configuration[DefaultAdminNameKey];

            using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var db = serviceScope.ServiceProvider.GetService <PartsUnlimitedContext>();

                var orders = new List <Order>();
                foreach (var combination in recomendationCombinations)
                {
                    for (int i = 0; i < combination.Multiplier; i++)
                    {
                        var order = new Order
                        {
                            Username   = userName,
                            OrderDate  = DateTime.Now,
                            Name       = $"John Smith{random.Next()}",
                            Address    = "15010 NE 36th St",
                            City       = "Redmond",
                            State      = "WA",
                            PostalCode = "98052",
                            Country    = "United States",
                            Phone      = "425-703-6214",
                            Email      = userName
                        };

                        db.Orders.Add(order);
                        decimal total = 0;
                        foreach (var id in combination.Transactions)
                        {
                            var product     = products.Single(x => x.RecommendationId == id);
                            var orderDetail = GetOrderDetail(product, order);
                            db.OrderDetails.Add(orderDetail);
                            total += orderDetail.UnitPrice;
                        }

                        order.Total = total;
                    }
                }

                db.SaveChanges();
            }
        }