示例#1
0
        public async Task <ActionResult> Confirmation(string totalPrice, string paymentType, string deliveryTime, string email)
        {
            var sumTotalPrice = double.Parse(totalPrice);
            var currentUser   = await GetCurrentUserAsync();

            var userOrders = await _context.Orders.Where(x => x.User.Id == currentUser.Id).ToListAsync();

            if (!HttpContext.User.IsInRole("KeyCustomer") && userOrders.Count() > 2)
            {
                await UserMgr.AddToRoleAsync(currentUser, "KeyCustomer");
            }
            switch (deliveryTime)
            {
            case "2-5 days":
                sumTotalPrice += 60;
                break;

            case "5-10 days":
                sumTotalPrice += 40;
                break;

            case "30 days":
                sumTotalPrice += 20;
                break;
            }
            if (paymentType == "Invoice")
            {
                sumTotalPrice += 50;
            }
            if (await UserMgr.IsInRoleAsync(currentUser, "KeyCustomer"))
            {
                sumTotalPrice      *= 0.9;
                ViewBag.keyCustomer = "You recived a 10% discount since you have been a loyal customer";
            }
            order = new Order
            {
                User           = await _context.Users.FindAsync(currentUser.Id),
                PaymentOption  = paymentType,
                TotalAmount    = sumTotalPrice,
                DeliveryOption = deliveryTime,
                Confirmed      = true
            };

            orderItems = new List <OrderItem>();
            cartItems  = HttpContext.Session.GetString("cartItems");

            foreach (var id in cartItems)
            {
                if (!orderItems.Where(p => p.Product.Id == id - '0').Any())
                {
                    orderItems.Add(
                        new OrderItem
                    {
                        Order    = order,
                        Product  = await _context.Products.FindAsync(id - '0'),
                        Quantity = cartItems.Count(p => p.ToString() == id.ToString())
                    });
                }
            }
            await _context.AddRangeAsync(orderItems);

            await _context.SaveChangesAsync();

            ReciveConfirmationViaEmail(orderItems, email);

            ViewBag.totalPrice             = sumTotalPrice.ToString();
            ViewBag.paymentType            = paymentType;
            ViewBag.delivery               = deliveryTime;
            ViewBag.orderId                = order.Id;
            ViewBag.HideCurrencyConversion = true;

            HttpContext.Session.SetString("cartItems", "");

            return(View(orderItems));
        }
示例#2
0
        public async Task <ActionResult> Confirmation(string totalPrice, string paymentType, string deliveryTime, string email)
        {
            currentUser = await GetCurrentUserAsync();

            double paymentDeliveryOptTotal = 0;

            switch (deliveryTime)
            {
            case "2-5 days":
                paymentDeliveryOptTotal = 60;
                break;

            case "5-10 days":
                paymentDeliveryOptTotal = 40;
                break;

            case "30 days":
                paymentDeliveryOptTotal = 20;
                break;
            }
            if (paymentType == "Invoice")
            {
                paymentDeliveryOptTotal += 50;
            }
            order = new Order
            {
                User           = await _context.Users.FindAsync(currentUser.Id),
                PaymentOption  = paymentType,
                TotalAmount    = (double.Parse(totalPrice) + paymentDeliveryOptTotal),
                DeliveryOption = deliveryTime,
                Confirmed      = true
            };

            orderItems = new List <OrderItem>();
            cartItems  = HttpContext.Session.GetString("cartItems");

            foreach (var id in cartItems)
            {
                if (!orderItems.Where(p => p.Product.Id == id - '0').Any())
                {
                    orderItems.Add(
                        new OrderItem
                    {
                        Order    = order,
                        Product  = await _context.Products.FindAsync(id - '0'),
                        Quantity = cartItems.Count(p => p.ToString() == id.ToString())
                    });
                }
            }
            await _context.AddRangeAsync(orderItems);

            await _context.SaveChangesAsync();

            ReciveConfirmationViaEmail(orderItems, email);

            ViewBag.totalPrice             = totalPrice;
            ViewBag.paymentType            = paymentType;
            ViewBag.delivery               = deliveryTime;
            ViewBag.orderId                = order.Id;
            ViewBag.HideCurrencyConversion = true;

            HttpContext.Session.SetString("cartItems", "");

            return(View(orderItems));
        }