public ActionResult SupplierFilterOrders(int SearchString)
 {
     var userId = User.Identity.GetUserId();
     var records = db.Orders.Where(o => o.OrderId == SearchString && o.SupplierUserId == userId).ToList();
     var orderList = new List<SupplierRetrieveOrdersViewModel>();
     foreach (var item in records)
     {
         var orderItem = new SupplierRetrieveOrdersViewModel()
         {
             OrderId = item.OrderId,
             ModelId = item.ModelId,
             ModelNumber = (from m in db.Models
                            where m.ModelId == item.ModelId
                            select m.ModelNumber).FirstOrDefault(),
             CustomerName = (from c in db.Customers
                             where c.CustomerId == item.CustomerId
                             select c.Name).FirstOrDefault(),
             Status = item.Status,
             DtCreated = item.DtCreated
         };
         orderList.Add(orderItem);
     }
     ViewBag.Title = "List of your Orders";
     return View("SupplierRetrieveOrders", orderList);
 }
        public ActionResult SupplierRetrieveOrders(ManageMessageId? message)
        {
            try
            {
                Session.Remove("InitialRequest");

                string userId = User.Identity.GetUserId();
                if (userId == null)
                {
                    var initialRequest = Request.Url.AbsoluteUri.ToString();
                    if (!String.IsNullOrWhiteSpace(initialRequest))
                        Session["InitialRequest"] = initialRequest;
                    return RedirectToAction("Login", "Account");
                }

                if (User.IsInRole("Supplier"))
                {
                    var orderList = new List<SupplierRetrieveOrdersViewModel>();
                    var records = from o in db.Orders
                                  where o.SupplierUserId == userId
                                  select o;
                    foreach (var item in records)
                    {
                        var orderItem = new SupplierRetrieveOrdersViewModel()
                        {
                            OrderId = item.OrderId,
                            ModelId = item.ModelId,
                            ModelNumber = (from m in db.Models
                                           where m.ModelId == item.ModelId
                                           select m.ModelNumber).FirstOrDefault(),
                            CustomerName = (from c in db.Customers
                                            where c.CustomerId == item.CustomerId
                                            select c.Name).FirstOrDefault(),
                            Status = item.Status,
                            DtCreated = item.DtCreated
                        };
                        orderList.Add(orderItem);
                    }

                    GetSupplierReturnNotification();
                    GetSupplierNotification();
                    ViewBag.Title = "List of your Orders";

                    ViewBag.StatusMessage =
                           message == ManageMessageId.ConfirmationFailure ? "Your Confirmation code did not match with the Customer code."
                           : message == ManageMessageId.Error ? "An error has occurred."
                           : message == ManageMessageId.UpdateOrderStatus ? "You just updated the order status."
                           : message == ManageMessageId.OrderDeliveredSuccess ? "You just confirmed that your order has been delivered."
                            : "";
                    return View(orderList);
                }
                return RedirectToAction("Login", "Account");
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString());
            }
        }
        public ActionResult NewOrders()
        {
            var userId = User.Identity.GetUserId();
            var newOrders = db.Orders.Where(o => o.SupplierUserId == userId && o.Notification == "Supplier").ToList();
            if (newOrders == null)
            {
                return RedirectToAction("Index");
            }
            foreach (var item in newOrders)
            {
                item.Notification = null;
            }
            db.SaveChanges();

            var newOrderList = new List<SupplierRetrieveOrdersViewModel>();

            foreach (var item in newOrders)
            {
                var orderItem = new SupplierRetrieveOrdersViewModel()
                {
                    OrderId = item.OrderId,
                    ModelId = item.ModelId,
                    ModelNumber = (from m in db.Models
                                   where m.ModelId == item.ModelId
                                   select m.ModelNumber).FirstOrDefault(),
                    CustomerName = (from c in db.Customers
                                    where c.CustomerId == item.CustomerId
                                    select c.Name).FirstOrDefault(),
                    Status = item.Status,
                    DtCreated = item.DtCreated
                };
                newOrderList.Add(orderItem);
            }
            GetSupplierReturnNotification();
            GetSupplierNotification();
            ViewBag.Title = "List of your New Orders";
            return View("SupplierRetrieveOrders", newOrderList);
        }
        // GET: Supplier
        public ActionResult Index()
        {
            try
            {
                string userId = User.Identity.GetUserId();
                if (userId == null)
                {
                    return RedirectToAction("Login", "Account");
                }

                //Check if the "Supplier" role exists if not it returns a null value
                var role = db.Roles.SingleOrDefault(m => m.Name == "Supplier");

                if (User.IsInRole("Supplier"))
                {
                    //Runs a query to determine if the user is actually an "Supplier" if not it returns a null value
                    //var userInRole = db.Users.Where(m => m.Roles.Any(r => r.UserId == userId)).FirstOrDefault();
                    //if (userInRole != null)
                    //{
                    int cancelledOrders = db.Orders.Where(o => o.Status == "Inactive" && o.SupplierUserId == userId).ToList().Count();
                    ViewBag.CancelledOrdersCount = cancelledOrders;

                    var orderList = new List<SupplierRetrieveOrdersViewModel>();
                    var orderRecords = from o in db.Orders
                                       where o.SupplierUserId == userId
                                       select o;
                    foreach (var item in orderRecords.OrderByDescending(r => r.DtCreated).Take(10))
                    {
                        var orderItem = new SupplierRetrieveOrdersViewModel()
                        {
                            OrderId = item.OrderId,
                            ModelId = item.ModelId,
                            ModelNumber = (from m in db.Models
                                           where m.ModelId == item.ModelId
                                           select m.ModelNumber).FirstOrDefault(),
                            CustomerName = (from c in db.Customers
                                            where c.CustomerId == item.CustomerId
                                            select c.Name).FirstOrDefault(),
                            Status = item.Status,
                            DtCreated = item.DtCreated
                        };
                        orderList.Add(orderItem);
                    }

                    ViewBag.OrderTransactions = orderList;

                    var returnRecords = from r in db.Returns
                                        join o in db.Orders on r.OrderId equals o.OrderId
                                        where o.SupplierUserId == userId
                                        select r;
                    if (returnRecords == null)
                        return RedirectToAction("Index", "Error404");

                    ViewBag.ReturnTransactions = returnRecords.OrderByDescending(r => r.DtCreated).Take(10);

                    GetSupplierNotification();
                    GetSupplierReturnNotification();
                    return View();
                }
                return RedirectToAction("Login", "Account");
            }
            catch (Exception ex)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString());
            }
        }