public void ClearOrderline(Orderline orderline)
 {
     if (orderline.OrderId == 0)
     {
         context.Orderlines.Remove(orderline);
         context.SaveChanges();
     }
 }
 public void SaveOrderline(Orderline orderline)
 {
     if (orderline.OrderlineId == 0)
     {
         context.Orderlines.Add(orderline);
     }
     else
     {
         context.Entry(orderline).State = EntityState.Modified;
     }
     context.SaveChanges();
 }
        public ActionResult SaveCurrentOrder(Cart cart)
        {
            var orderlineQuery = orderlineRepository.Orderlines.Where(u => u.UserId == WebSecurity.CurrentUserId).ToList();
            foreach (var orderline in orderlineQuery)
            {
                if (orderline != null)
                {
                    orderlineRepository.ClearOrderline(orderline);
                }
            }

            for(int i = 0; i<cart.Lines.Count(); i++){
                
                var line = cart.Lines.ElementAt(i);
                for (int j = 0; j < line.Quantity; j++)
                {
                    Orderline orderline = new Orderline();
                    orderline.PizzaId = line.Pizza.PizzaId;
                    orderline.OrderlinePrice = line.Pizza.Price;
                    orderline.UserId = WebSecurity.CurrentUserId;
                    orderlineRepository.SaveOrderline(orderline);
                }
                
            }
            TempData["message"] = string.Format("Order saved");

            return RedirectToAction("Index");
        }
        public ViewResult ConfirmCheckout(Cart cart, int deliveryId)
        {
            Delivery delivery = deliveryRepository.Deliveries.FirstOrDefault(d => d.DeliveryId == deliveryId);
            Order order = new Order();
            order.PriceBeforeVouchers = cart.GetTotalValue();
            order.UserId = WebSecurity.CurrentUserId;
            order.TimeSubmitted = DateTime.Now;
            
            order.DeliveryId = deliveryId;
            order.Subtotal = order.PriceBeforeVouchers + delivery.Cost;
            order.Total = order.Subtotal;
            order.Status = "Processed";
            orderRepository.SaveOrder(order);
            int orderId = orderRepository.Orders.FirstOrDefault(o => o.TimeSubmitted == order.TimeSubmitted).OrderId;
            List<Pizza> pizzas = new List<Pizza>();

            foreach(var cartLine in cart.Lines){
                for(int i=0;i<cartLine.Quantity;i++)
                {
                    Orderline orderline = new Orderline();
                    orderline.PizzaId = cartLine.Pizza.PizzaId;
                    orderline.OrderlinePrice = cartLine.Pizza.Price * cartLine.Quantity;
                    orderline.UserId = WebSecurity.CurrentUserId;
                    orderline.OrderId = order.OrderId;
                    orderlineRepository.SaveOrderline(orderline);
                    pizzas.Add(repository.Pizzas.FirstOrDefault(p => p.PizzaId == orderline.PizzaId));
                    Pizza pizza = repository.Pizzas.FirstOrDefault(p => p.PizzaId == orderline.PizzaId);
                    if (pizza.Name == "Create Your Own")
                    {
                        foreach (var topping in Session["ToppingList"] as List<Topping>)
                        {
                            PizzaToppingOrder pizzaToppingOrder = new PizzaToppingOrder();
                            pizzaToppingOrder.OrderlineId = orderline.OrderlineId;
                            pizzaToppingOrder.ToppingId = topping.ToppingId;
                            pizzaToppingOrderRepository.SavePizzaToppingOrder(pizzaToppingOrder);
                        }
                    }
                }
            }

            cart.Clear();
            ViewBag.PriceBeforeVouchers = order.PriceBeforeVouchers;
            
            ViewBag.DeliveryCost = delivery.Cost;
            ViewBag.PriceIncDelivery = order.PriceBeforeVouchers + delivery.Cost;
            ViewBag.DeliveryType = delivery.DeliveryType;
            
            var context = new UsersContext();
            var username = User.Identity.Name;
            var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);

            List<Topping> toppings = Session["ToppingList"] as List<Topping>;

            return View(new ConfirmCheckoutViewModel
            {
                User = user,
                Order = order,
                Pizzas =  pizzas, //orderlineRepository.Orderlines.Where(ol => ol.OrderId == order.OrderId).ToList()
                Toppings = toppings
            });
            //return View(user);
        }