private FreeMarketResult RemoveItemFromDatabase(int itemNumber, string userId = null)
        {
            FreeMarketResult result = FreeMarketResult.NoResult;

            if (itemNumber != 0)
            {
                using (FreeMarketEntities db = new FreeMarketEntities())
                {
                    OrderDetail item = db.OrderDetails.Find(itemNumber);

                    if (item != null)
                    {
                        FreeStock(item.ProductNumber, item.SupplierNumber, (int)item.CustodianNumber, item.Quantity, item.SizeType);

                        db.OrderDetails.Remove(item);
                        db.SaveChanges();

                        AuditUser.LogAudit(8, string.Format("Order number: {0}", Order.OrderNumber), userId);

                        result = FreeMarketResult.Success;
                    }
                    else
                    {
                        result = FreeMarketResult.Failure;
                    }
                }
            }
            else
            {
                result = FreeMarketResult.Success;
            }

            return(result);
        }
        public FreeMarketObject RemoveItem(int itemNumber, int productNumber, int supplierNumber, int sizeType, string userId = null)
        {
            // If the item is in the database
            FreeMarketResult resultDatabase = RemoveItemFromDatabase(itemNumber, userId);

            // Remove the item from the Session as well
            FreeMarketResult resultSession = RemoveItemFromSession(productNumber, supplierNumber, sizeType);

            if (resultDatabase == FreeMarketResult.Success && resultSession == FreeMarketResult.Success)
            {
                Product product = new Product();

                using (FreeMarketEntities db = new FreeMarketEntities())
                {
                    product = db.Products.Find(itemNumber);
                }

                // Keep the OrderTotal in sync
                UpdateTotal();

                return(new FreeMarketObject {
                    Result = FreeMarketResult.Success, Argument = product
                });
            }
            else
            {
                UpdateTotal();

                return(new FreeMarketObject {
                    Result = FreeMarketResult.Failure, Argument = null
                });
            }
        }
        public ActionResult ModifyDeliveryDetails(ModifyDeliveryDetailsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = UserManager.FindById(User.Identity.GetUserId());
                if (user == null)
                {
                    return(View("Error"));
                }

                if (CustomerAddress.AddressExists(user.Id, model.AddressName))
                {
                    FreeMarketResult result = CustomerAddress.UpdateAddress(user.Id, model.AddressName, model.Address.AddressLine1, model.Address.AddressLine2
                                                                            , model.Address.AddressLine3, model.Address.AddressLine4, model.Address.AddressSuburb
                                                                            , model.Address.AddressCity, model.Address.AddressPostalCode);

                    if (result == FreeMarketResult.Success)
                    {
                        TempData["message"] = string.Format
                                                  ("Your {0} address has been updated.",
                                                  model.AddressName);
                    }
                    else
                    {
                        TempData["message"] = string.Format
                                                  ("Sorry, we could not process your request at this time, please try again later.");
                    }
                }
                else
                {
                    FreeMarketResult result = CustomerAddress.AddAddress(user.Id, model.AddressName, model.Address.AddressLine1, model.Address.AddressLine2
                                                                         , model.Address.AddressLine3, model.Address.AddressLine4, model.Address.AddressSuburb
                                                                         , model.Address.AddressCity, model.Address.AddressPostalCode);

                    if (result == FreeMarketResult.Success)
                    {
                        TempData["message"] = string.Format
                                                  ("Your {0} address has been added to our system.",
                                                  model.AddressName);
                    }
                    else
                    {
                        TempData["message"] = string.Format
                                                  ("Sorry, we could not process your request at this time, please try again later.");
                    }
                }

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
        private FreeMarketResult RemoveItemFromSession(int productNumber, int supplierNumber, int sizeType)
        {
            Debug.Write(string.Format("Removing Product {0} from Session...", productNumber));

            Product          product = new Product();
            FreeMarketResult result  = FreeMarketResult.NoResult;

            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                product = db.Products.Find(productNumber);

                if (product != null)
                {
                    Body.OrderDetails.RemoveAll(c => c.ProductNumber == productNumber && c.SupplierNumber == supplierNumber && c.SizeType == sizeType);
                    result = FreeMarketResult.Success;
                }
                else
                {
                    result = FreeMarketResult.Failure;
                }
            }

            return(result);
        }