public OrderViewModel UpdateOrderStatus(OrderViewModel item)
        {
            if (item == null)
                return null;

            var record = db.Orders.Where(x => x.OrderId == item.OrderId).FirstOrDefault();
            if (record == null)
                return null;

            if (item.ConfirmationCode == record.ConfirmationCode)
            {
                record.Status = "Delivered";
                record.ConfirmationCode = null;
                db.SaveChanges();
            }
            return record;
        }
        public ActionResult UpdateOrderStatus(OrderViewModel item)
        {
            try
            {
                if (User.IsInRole("Supplier"))
                {
                    string userId = User.Identity.GetUserId();
                    if (String.IsNullOrWhiteSpace(userId))
                        return RedirectToAction("Home", "Error404");

                    var record = _repo.UpdateOrderStatus(item);
                    if (record == null || record.SupplierUserId != userId)
                        return RedirectToAction("Home", "Error404");

                    return View(record);
                }
                return RedirectToAction("Login", "Account");
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString());
            }
        }
        public ActionResult Create(List<CartViewModel> items)
        {
            string userId = User.Identity.GetUserId();

            // To make it work with the session variable just use:
            //var carList = Session["Cart"] as List<CartViewModel>;
            //Comment the code in the DummyProducts region below to make it work

            #region DummyProducts

            var cartList = new List<CartViewModel>()
            {
                new CartViewModel()
                {
                    SupplierId=2,
                    ModelId=1,
                    UserId= userId,
                    ModelName="Sony Xperia M",
                    UnitPrice=8000,
                    SubTotal=16000,
                    GrandTotal=16500,
                    Quantity=2
                },
                 new CartViewModel()
                {
                    SupplierId=2,
                    ModelId=1,
                    UserId= userId,
                    ModelName="Xperia T3",
                    UnitPrice=10939,
                    SubTotal=10939,
                    GrandTotal=11000,
                    Quantity=1
                }
            };

            #endregion DummyProducts

            if (cartList == null)
            {
                return Content("CartList is null");
            }
            if (cartList != null)
            {
                foreach (var item in cartList)
                {
                    var newOrder = new OrderViewModel()
                    {
                        DtCreated = DateTime.UtcNow,
                        ExpectedDeliveryDate = DateTime.UtcNow,
                        RealDeliveryDate = DateTime.UtcNow,
                        Status = "Active",
                        Total = item.SubTotal,
                        GrandTotal = item.GrandTotal,
                        SupplierId = item.SupplierId,
                        CustomerId = (from c in db.Customers
                                      where c.Id == item.UserId
                                      select c.CustomerId).FirstOrDefault(),
                        ModelId = item.ModelId,
                        CustomerUserId = item.UserId,
                        SupplierUserId = (from s in db.Suppliers
                                          where s.SupplierId == item.SupplierId
                                          select s.Id).FirstOrDefault()
                    };
                    db.Orders.Add(newOrder);
                    db.SaveChanges();

                    string supplierEmail = (from s in db.Suppliers
                                            where s.SupplierId == newOrder.SupplierId
                                            select s.Email).FirstOrDefault();

                    string customerEmail = (from c in db.Customers
                                            where c.CustomerId == newOrder.CustomerId
                                            select c.Email).FirstOrDefault();

                    SendNotification(supplierEmail, "Supplier");
                    SendNotification(customerEmail, "Customer");
                }
                return RedirectToAction("CustomerRetrieveOrders", "Customer");
            }

            return RedirectToAction("Login", "Acccount");
        }
        public ActionResult ReviewOrder(List<CartViewModel> orders)
        {
            if (User.IsInRole("Customer"))
            {
                var orderList = Session["Order"] as List<CartViewModel>;
                var deliveryAddress = Session["DeliveryAddress"] as DeliveryAddressViewModel;
                string userId = User.Identity.GetUserId();
                var orderItems = new List<OrderViewModel>();
                if (orderList != null)
                {
                    int newOrderId = 0;
                    foreach (var item in orderList)
                    {
                        var newOrder = new OrderViewModel()
                        {
                            DtCreated = DateTime.UtcNow,
                            ExpectedDeliveryDate = DateTime.UtcNow,
                            RealDeliveryDate = DateTime.UtcNow,
                            Status = "Active",
                            Quantity = item.Quantity,
                            UnitPrice = item.UnitPrice,
                            Total = item.SubTotal,
                            GrandTotal = item.GrandTotal,
                            SupplierId = item.SupplierId,
                            CustomerId = (from c in db.Customers
                                          where c.Id == userId
                                          select c.CustomerId).FirstOrDefault(),
                            ModelId = item.ModelId,
                            CustomerUserId = userId,
                            ConfirmationCode = CodeGenerator(),
                            SupplierUserId = (from s in db.Suppliers
                                              where s.SupplierId == item.SupplierId
                                              select s.Id).FirstOrDefault(),
                            Notification = "Supplier"
                        };
                        orderItems.Add(newOrder);
                        db.Orders.Add(newOrder);
                        db.SaveChanges();

                        db.Entry(newOrder).GetDatabaseValues();
                        newOrderId = newOrder.OrderId;

                        var newOrderDelivery = new DeliveryViewModel()
                        {
                            OrderId = newOrderId,
                            Street = deliveryAddress.Street,
                            City = deliveryAddress.City,
                            Zipcode = deliveryAddress.Zipcode,
                            ExpectedDeliveryDate = DateTime.UtcNow,
                            ActualDeliveryDate = DateTime.UtcNow,
                            DateCreated = DateTime.UtcNow,
                            Status = "Processing"
                        };
                        db.Deliveries.Add(newOrderDelivery);
                        db.SaveChanges();

                        GeneratePayment(newOrder.OrderId);
                        string supplierEmail = (from s in db.Suppliers
                                                where s.SupplierId == newOrder.SupplierId
                                                select s.Email).FirstOrDefault();

                        string customerEmail = (from c in db.Customers
                                                where c.CustomerId == newOrder.CustomerId
                                                select c.Email).FirstOrDefault();

                        new Task(() => { SendNotification(supplierEmail, "Supplier"); }).Start();
                        new Task(() => { SendNotification(customerEmail, "Customer"); }).Start();
                    }

                    Session.Remove("Order");
                    Session.Remove("Cart");
                    Session.Remove("DeliveryAddress");

                    //return RedirectToAction("Invoice", "Order", orderItems);
                    //return View("../Order/Invoice", orderItems);
                    return RedirectToAction("CustomerGetOrder", "Customer", new { id = newOrderId, message = ManageMessageId.AddOrderSuccess });
                }
            }
            return View("../Home/Error404");
        }