public ActionResult AnalyzeOrders(int orderNumber)
 {
     ViewBag.OrderDisabled = true;
     OrderID = orderNumber;
     var model = new ViewModel();
     InitializeViewModel(orderNumber, model);
     return View(model);
 }
        public ActionResult AnalyzeOrders(ViewModel model)
        {
            if (model.Action.Equals("Cancel"))
                return RedirectToAction("AnalyzeOrders", new { orderNumber = OrderID });

            if (model.Action.Equals("Save"))
            {
                if (!model.OrderInfo.IsOrdered && model.OrderInfo.IsDelivered)
                {
                    ModelState.AddModelError("CheckBoxError", @"Unsupport CheckBox selection !!! Please try again !");
                    ViewBag.OrderDisabled = true;
                }
                if (model.OrderInfo.DeliveryDate == null)
                {
                    ModelState.AddModelError("NullDate", @"Delivery Date field is empty or incorrect !!! Please select date !");
                    ViewBag.OrderDisabled = true;
                }
                else
                {
                    DateTime dt1 = database.Orders.Single(m => m.OrderID == OrderID).OrderingDate;
                    var dt2 = (DateTime)model.OrderInfo.DeliveryDate;
                    if (DateTime.Compare(dt2, dt1) < 0)
                    {
                        ModelState.AddModelError("IncorrectDate", @"Delivery Date is incorrect !!! Please try again !");
                        ViewBag.OrderDisabled = true;
                    }
                    else ViewBag.OrderDisabled = false;
                }
            }
            else
                if (model.Action.Equals("Order"))
                {
                    ViewBag.OrderDisabled = false;
                    Orders order = database.Orders.FirstOrDefault(ord => ord.OrderID == OrderID);
                    order.DeliveryDate = Convert.ToDateTime(model.OrderInfo.DeliveryDate);
                    order.IsGift = model.OrderInfo.IsGift;
                    if (model.OrderInfo.IsOrdered && model.OrderInfo.IsDelivered)
                        order.Status = "Delivered";
                    else
                        if (model.OrderInfo.IsOrdered)
                            order.Status = "Ordered";
                    database.SaveChanges();
                }
            InitializeViewModel(OrderID, model);
            return View(model);
        }
        private void InitializeViewModel(int id, ViewModel model)
        {
            var query1 = (from item in database.Items
                          join order in database.ItemsOrder
                          on item.ItemID equals order.ItemInfoID
                          where order.OrderID == id
                          select new OrderedItemsInfo()
                          {
                              ItemNumber = item.ItemID,
                              ItemName = item.ItemName,
                              ItemDescription = item.ItemDescriprion,
                              Dimension = order.Dimension,
                              Price = item.Price,
                              Quantity = order.Quantity,
                              PricePerLine = item.Price * order.Quantity
                          }
                         );

            int itemsCounter = 0;
            var dt = new DimensionType();
            List<OrderedItemsInfo> tmp = query1.ToList();
            foreach (OrderedItemsInfo info in tmp)
            {
                itemsCounter += dt.GetDimension(info.Dimension) * info.Quantity;
                info.PricePerLine = info.PricePerLine * dt.GetDimension(info.Dimension);
            }
            model.OrderedItems = tmp;

            var query2 = (from user in database.Users
                          join order in database.Orders
                          on user.UserID equals order.UserID
                          join rank in database.Rank
                          on user.RankType equals rank.RankID
                          where order.OrderID == id
                          select new TotalOrderInfo()
                          {
                              CustomerName = user.UserFName,
                              CustomerType = rank.RankName,
                              OrderNumber = order.OrderID,
                              TotalPrice = order.TotalPrice,
                              TotalNumberOfItems = itemsCounter,
                              Assignee = order.Assignee,
                              DateOfOrdering = order.OrderingDate,
                              PreferableDeliveryDate = order.PreferableDeliveryDate,
                              DeliveryDate = order.DeliveryDate,
                              IsGift = order.IsGift,
                              IsOrdered = order.Status.Equals("Ordered") || order.Status.Equals("Delivered"),
                              IsDelivered = order.Status.Equals("Delivered")
                          }
                         );
            model.OrderInfo = query2.First();
        }