public async Task <IActionResult> CheckOut()
        {
            var cart = await GetCurrentUserCartAsync();

            var customer = await GetCustomerByUserIdAsync();

            await CalculaTetoatalAsync();

            var cartDetails = _context.CartDetails
                              .Where(c => c.CartId == cart.Id)
                              .Include(c => c.Product);

            foreach (var item in cartDetails)
            {
                if (item.Quantity > item.Product.Quantity)
                {
                    @TempData["CartMessage"] = "Product " + item.Product.Name + " stock was exceeded";
                    return(RedirectToAction("index"));
                }
            }
            if (cartDetails.Count() < 1)
            {
                @TempData["CartMessage"] = "Cart is empty";
                return(RedirectToAction("index"));
            }
            if (customer.Credit < ViewBag.total)
            {
                @TempData["CartMessage"] = "Credit exceeded";
                return(RedirectToAction("index"));
            }

            Orders order = new Orders();

            order.OrderStatusId = 8;
            order.CustomerId    = customer.Id;
            DateTime date = DateTime.Now;

            order.Date        = date;
            order.isPaid      = false;
            order.DiscountVal = ViewBag.discountVal + ViewBag.discPackage;
            if (order.DiscountVal == 0)
            {
                order.DiscountVal = null;
            }
            order.TotalPrice = ViewBag.total;
            _context.Add(order);
            await _context.SaveChangesAsync();

            var id = order.Id;

            foreach (var item in cartDetails)
            {
                OrderDetails orderDetails = new OrderDetails();
                Products     product      = await _context.Products.FirstOrDefaultAsync(p => p.Id == item.ProductId);

                product.Quantity       = product.Quantity - item.Quantity;
                orderDetails.OrderId   = order.Id;
                orderDetails.ProductId = item.ProductId;
                orderDetails.Quantity  = item.Quantity;
                if (ViewBag.discountPer > 0)
                {
                    orderDetails.DiscountPer = ViewBag.discountPer;
                }
                orderDetails.Price = item.Product.FinalPrice - ViewBag.discountPer * item.Product.FinalPrice / 100;
                try
                {
                    _context.Update(product);
                }
                catch
                {
                    return(NotFound());
                }
                _context.Add(orderDetails);
                _context.Remove(item);
            }

            await _context.SaveChangesAsync();

            cart.DiscountPer = null;
            cart.DiscountVal = null;
            customer.Credit  = customer.Credit - ViewBag.total;
            try
            {
                _context.Update(cart);
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(NotFound());
            }



            return(RedirectToAction("details", "Orders", new { Id = id }));
        }