/// <summary>
 /// Create a new Card object.
 /// </summary>
 /// <param name="cardID">Initial value of the CardID property.</param>
 /// <param name="cardNumber">Initial value of the CardNumber property.</param>
 /// <param name="cardType">Initial value of the CardType property.</param>
 /// <param name="cVV2Code">Initial value of the CVV2Code property.</param>
 /// <param name="expiredDate">Initial value of the ExpiredDate property.</param>
 public static Card CreateCard(global::System.Int32 cardID, global::System.String cardNumber, global::System.String cardType, global::System.String cVV2Code, global::System.DateTime expiredDate)
 {
     Card card = new Card();
     card.CardID = cardID;
     card.CardNumber = cardNumber;
     card.CardType = cardType;
     card.CVV2Code = cVV2Code;
     card.ExpiredDate = expiredDate;
     return card;
 }
        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>
 /// Deprecated Method for adding a new object to the Card EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToCard(Card card)
 {
     base.AddObject("Card", card);
 }