public ActionResult Create(CustomerOrderInfo order)
        {
            if (order.Command == "Add Item")
            {
                Orders dbOrder = new Orders();
                string curUserLogin = HttpContext.User.Identity.Name;

                dbOrder.UserID = database.Users.Single(user => user.Login.ToLower() == curUserLogin.ToLower()).UserID;
                dbOrder.CardID = null;
                dbOrder.OrderNumber = order.OrderNumber;
                dbOrder.Status = "Created";
                dbOrder.OrderingDate = DateTime.Today;
                dbOrder.PreferableDeliveryDate = order.PreferableDeliveryDate;
                dbOrder.Assignee = order.Assignee;
                dbOrder.TotalPrice = order.TotalPrice;
                dbOrder.Discount = 0;
                dbOrder.IsGift = false;
                database.Orders.AddObject(dbOrder);
                database.SaveChanges();
                int addedOrderID = database.Orders.FirstOrDefault(ord => ord.OrderNumber == order.OrderNumber).OrderID;
                return RedirectToAction("../ItemSearch/ItemSearch", new { orderID = order.OrderID });
            }
            // TODO: When items search will be ready add validation for items
            List<string> orderNumbers = database.Orders.Select<Orders, string>(order1 => order1.OrderNumber).ToList<string>();
            if (orderNumbers.Contains(order.OrderNumber))
            {
                ModelState.AddModelError("OrderNumber", "Order Number already exists in the system./nPlease re-type it or just leave it blank");
            }
            if (order.PreferableDeliveryDate != null &&
                       order.PreferableDeliveryDate < order.OrderingDate)
            {
                ModelState.AddModelError("PreferableDeliveryDate", "Preferable delivery date goes before Date of ordering./nPlease re-choose it or just leave it blank");
            }
            // Because PreferableDeliveryDate by default = "  /  /"
            if (order.PreferableDeliveryDate == null)
            {
                ModelState["PreferableDeliveryDate"].Errors.Clear();
            }
            order.DeliveryDate = null;

            // If customer just saves order, skiping Credit Card Validation
            for (int i = 4; i < ModelState.Count; i++)
            {
                ModelState.Values.ElementAt(i).Errors.Clear();
            }
            if (ModelState.IsValid)
            {
                Orders dbOrder = new Orders();
                string curUserLogin = HttpContext.User.Identity.Name;

                dbOrder.UserID = database.Users.Single(user => user.Login.ToLower() == curUserLogin.ToLower()).UserID;
                dbOrder.CardID = null;
                dbOrder.OrderNumber = order.OrderNumber;
                dbOrder.Status = "Created";
                dbOrder.OrderingDate = DateTime.Today;
                dbOrder.PreferableDeliveryDate = order.PreferableDeliveryDate;
                dbOrder.Assignee = order.Assignee;
                dbOrder.TotalPrice = order.TotalPrice;
                dbOrder.Discount = 0;
                dbOrder.IsGift = false;
                database.Orders.AddObject(dbOrder);
                database.SaveChanges();
                int addedOrderID = database.Orders.FirstOrDefault(ord => ord.OrderNumber == order.OrderNumber).OrderID;
                return RedirectToAction("Edit", new { orderID = addedOrderID });
            }
            else
            {
                List<string> assignies = new List<string>();
                foreach (Users user in database.Users.Where(user => user.Role == "Merchandiser"))
                {
                    assignies.Add(user.Login);
                }
                order.Merchandisers = new List<string>(assignies);
                order.ItemsOrder = new List<CustomerItemsInfo>();
                return View(order);
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Orders EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToOrders(Orders orders)
 {
     base.AddObject("Orders", orders);
 }
        public ActionResult Edit(CustomerOrderInfo order)
        {
            if (order.Command == "Add Item")
            {
                return RedirectToAction("../ItemSearch/ItemSearch", new { orderID = order.OrderID });
            }

            List<string> assignies = new List<string>();
            foreach (Users user in database.Users.Where(user => user.Role == "Merchandiser"))
            {
                assignies.Add(user.Login);
            }
            order.Merchandisers = assignies;
            List<string> orderNumbers = database.Orders.Select<Orders, string>(order1 => order1.OrderNumber).ToList<string>();
            if (orderNumbers.Contains(order.OrderNumber) &&
                order.OrderNumber != database.Orders.FirstOrDefault(ord => ord.OrderID == order.OrderID).OrderNumber)
            {
                ModelState.AddModelError("OrderNumber", "Order Number already exists in the system./nPlease re-type it or just leave it blank");
            }
            if (order.PreferableDeliveryDate != null &&
                       order.PreferableDeliveryDate < order.OrderingDate)
            {
                ModelState.AddModelError("PreferableDeliveryDate", "Preferable delivery date goes before Date of ordering./nPlease re-choose it or just leave it blank");
            }
            // Because PreferableDeliveryDate by default = "  /  /"
            if (order.PreferableDeliveryDate == null)
            {
                ModelState["PreferableDeliveryDate"].Errors.Clear();
            }
            order.DeliveryDate = null;
            if (order.Command == "Save")
            {
                // If customer just saves order, skiping Credit Card Validation
                for (int i = 4; i < ModelState.Count; i++)
                {
                    ModelState.Values.ElementAt(i).Errors.Clear();
                }
                if (ModelState.IsValid)
                {
                    Orders dbOrder = new Orders();
                    string curUserLogin = HttpContext.User.Identity.Name;
                    dbOrder.UserID = database.Users.Single(user => user.Login.ToLower() == curUserLogin.ToLower()).UserID;
                    dbOrder.CardID = null;
                    dbOrder.OrderNumber = order.OrderNumber;
                    dbOrder.OrderID = order.OrderID;
                    dbOrder.Status = "Created";
                    dbOrder.OrderingDate = order.OrderingDate;
                    dbOrder.PreferableDeliveryDate = order.PreferableDeliveryDate;
                    dbOrder.Assignee = order.Assignee;
                    dbOrder.TotalPrice = order.TotalPrice;
                    dbOrder.Discount = 0;
                    dbOrder.IsGift = false;
                    database.Orders.Attach(database.Orders.Single(ord => ord.OrderID == dbOrder.OrderID));
                    database.ApplyCurrentValues("Orders", dbOrder);
                    database.SaveChanges();
                    int editedOrderID = dbOrder.OrderID;
                    return View(order);
                }
                else
                {
                    return View(order);
                }
            }
            else if (order.Command == "Order")
            {
                // TODO: Delete, whent items search will be done
                ModelState.Values.ElementAt(16).Errors.Clear();

                if (order.ExpiredDate < DateTime.Today.AddDays(3))
                {
                    ModelState.AddModelError("ExpiredDate", "Order Number already exists in the system./nPlease re-type it or just leave it blank");
                }
                if (order.PreferableDeliveryDate != null &&
                         order.PreferableDeliveryDate < order.OrderingDate)
                {
                    ModelState.AddModelError("ServerValidationError", "Preferable delivery date goes before Date of ordering./nPlease re-choose it or just leave it blank");
                }
                if (ModelState.IsValid)
                {
                    Card dbCard;
                    Orders dbOrder = new Orders();
                    string curUserLogin = HttpContext.User.Identity.Name;
                    dbOrder.UserID = database.Users.Single(user => user.Login.ToLower() == curUserLogin.ToLower()).UserID;
                    dbCard = database.Card.SingleOrDefault(card => card.CardNumber == order.CardNumber);
                    if (dbCard == null)
                    {
                        dbCard = new Card();
                        dbCard.CardNumber = order.CardNumber;
                        dbCard.CardType = order.CardType;
                        dbCard.CVV2Code = order.CVV2Code;
                        dbCard.ExpiredDate = (DateTime)order.ExpiredDate;
                        dbCard.IssueNumber = order.IssueNumber;
                        dbCard.MakeDate = order.MakeDate;
                        database.Card.AddObject(dbCard);
                        database.SaveChanges();
                    }
                    dbOrder.CardID = database.Card.FirstOrDefault(card => card.CardNumber == dbCard.CardNumber).CardID;
                    dbOrder.OrderID = order.OrderID;
                    dbOrder.OrderNumber = order.OrderNumber;
                    dbOrder.Status = "Ordered";
                    dbOrder.OrderingDate = DateTime.Today;
                    dbOrder.PreferableDeliveryDate = order.PreferableDeliveryDate;
                    dbOrder.Assignee = order.Assignee;
                    dbOrder.TotalPrice = order.TotalPrice;
                    dbOrder.Discount = 0;
                    dbOrder.IsGift = false;
                    database.Orders.Attach(database.Orders.Single(ord => ord.OrderID == dbOrder.OrderID));
                    database.ApplyCurrentValues("Orders", dbOrder);
                    database.SaveChanges();
                    int editedOrderID = dbOrder.OrderID;
                    return View(order);

                }
                else
                {
                    return View(order);
                }
            }
            return View(order);
        }
 /// <summary>
 /// Create a new Orders object.
 /// </summary>
 /// <param name="orderID">Initial value of the OrderID property.</param>
 /// <param name="userID">Initial value of the UserID property.</param>
 /// <param name="orderNumber">Initial value of the OrderNumber property.</param>
 /// <param name="status">Initial value of the Status property.</param>
 /// <param name="orderingDate">Initial value of the OrderingDate property.</param>
 /// <param name="assignee">Initial value of the Assignee property.</param>
 /// <param name="totalPrice">Initial value of the TotalPrice property.</param>
 /// <param name="discount">Initial value of the Discount property.</param>
 /// <param name="isGift">Initial value of the IsGift property.</param>
 public static Orders CreateOrders(global::System.Int32 orderID, global::System.Int32 userID, global::System.String orderNumber, global::System.String status, global::System.DateTime orderingDate, global::System.String assignee, global::System.Decimal totalPrice, global::System.Double discount, global::System.Boolean isGift)
 {
     Orders orders = new Orders();
     orders.OrderID = orderID;
     orders.UserID = userID;
     orders.OrderNumber = orderNumber;
     orders.Status = status;
     orders.OrderingDate = orderingDate;
     orders.Assignee = assignee;
     orders.TotalPrice = totalPrice;
     orders.Discount = discount;
     orders.IsGift = isGift;
     return orders;
 }