using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using System.Web.Security; using HeuristicLab.Services.Optimization.Billing.Business; using HeuristicLab.Services.Optimization.Billing.Interfaces; using HeuristicLab.Services.Optimization.Billing.Model; using HeuristicLab.Services.Optimization.Web.Helpers; using HeuristicLab.Services.Optimization.Web.Models; namespace HeuristicLab.Services.Optimization.Web.Controllers { [Authorize(Roles = "Web User")] [BillingFilterActionAttribute] public class OrderController : BaseController { private IOptimizationBilling billing = BillingServiceProvider.Instance; // // GET: /Order/ public ActionResult Index() { if (RedirectToLoginIfNecessary("Order", false)) return View(); return CreateOrder(new OrderModel()); //View(); } public ActionResult Overview() { if (RedirectToLoginIfNecessary("Order", false)) return View(); MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */); var orders = billing.GetOrders(currentUser.UserName); return View(new OverviewModel() { Orders = orders }); } [HttpPost] public ActionResult CreateOrder(OrderModel model) { if (RedirectToLoginIfNecessary("Order", false)) return View(); Session["order"] = model; var currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */); if (billing.GetUser(currentUser.UserName) == null) { billing.SaveUser(new User() { Name = currentUser.UserName, PaymentInformation = new List() }); } model.User = billing.GetUser(currentUser.UserName); return AddProduct(); } public ActionResult AddProduct() { return View("AddProduct", new ProductsModel() { Products = billing.GetProducts() }); } [HttpPost] public ActionResult AddProduct(ProductsModel product) { var order = Session["order"] as OrderModel; var prod = (from b in billing.GetProducts() where b.ProductId == product.ProductId select b).FirstOrDefault(); order.ProductQuantities[prod.ProductId] = product.Quantity; return AddProduct(); } public ActionResult SaveOrder() { var orderModel = Session["order"] as OrderModel; foreach (var key in orderModel.ProductQuantities.Keys) { var product = (from p in billing.GetProducts() where p.ProductId == key select p).FirstOrDefault(); orderModel.Products[key] = product; } return View(orderModel); } [HttpPost] [ActionName("SaveOrder")] public ActionResult SaveOrderPost(OrderModel orderModel) { var user = billing.GetUser(UserName); var orderProductModel = Session["order"] as OrderModel; //TODO: Save order via back-end var order = new Order() { State = OrderState.Created, User = user, BillingType = orderModel.BillingType, BillingPeriod = orderModel.BillingPeriod }; var orderLines = new List(); var products = billing.GetProducts(); foreach (var key in orderProductModel.ProductQuantities.Keys) { var product = (from p in products where p.ProductId == key select p).FirstOrDefault(); orderLines.Add(new OrderLine() { Order = order, Product = product, ProductPrice = product.Price, Quantity = orderProductModel.ProductQuantities[key] }); } order.OrderLines = orderLines; billing.SaveOrder(order); return RedirectToAction("OrderSaved"); } public ActionResult Payment() { var paymentModel = new PaymentModel(); return View(paymentModel); } [HttpPost] public ActionResult SavePayment(PaymentModel model) { var user = billing.GetUser(UserName); var oldInfo = (from pi in user.PaymentInformation where pi.CardNumber == model.CardNumber select pi).FirstOrDefault(); if (oldInfo != null) { oldInfo.AdditionalInformation = model.AdditionalInformation; oldInfo.PaymentMethod = model.Method; oldInfo.EntityState = State.Modified; } else { user.PaymentInformation.Add(new PaymentInformation() { CardNumber = model.CardNumber, AdditionalInformation = model.AdditionalInformation, PaymentMethod = model.Method }); user.EntityState = State.Modified; } billing.SaveUser(user); return RedirectToAction("SaveOrder"); } public ActionResult OrderSaved() { return View(); } public ActionResult ContactInformation() { var user = billing.GetUser(UserName); var userCi = user.ContactInformation; ContactInformationModel contactInformationModel = null; if (userCi == null) { contactInformationModel = new ContactInformationModel(); } else { contactInformationModel = new ContactInformationModel() { ContactInformationId = userCi.ContactInformationId, FirstName = userCi.FirstName, LastName = userCi.LastName, Email = userCi.Email, OrganizationName = userCi.OrganizationName, Street = userCi.Street, PostalCode = userCi.PostalCode, City = userCi.City, State = userCi.State }; } return View(contactInformationModel); } [HttpPost] public ActionResult SaveContactInformation(ContactInformationModel model) { var user = billing.GetUser(UserName); var userCi = user.ContactInformation; if (userCi != null) { userCi.City = model.City; userCi.Email = model.Email; userCi.FirstName = model.FirstName; userCi.LastName = model.LastName; userCi.OrganizationName = model.OrganizationName; userCi.PostalCode = model.PostalCode; userCi.State = model.State; userCi.Street = model.Street; userCi.EntityState = State.Modified; } else { user.ContactInformation = new ContactInformation() { City = model.City, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, OrganizationName = model.OrganizationName, PostalCode = model.PostalCode, State = model.State, Street = model.Street }; user.EntityState = State.Modified; } billing.SaveUser(user); return RedirectToAction("SaveOrder"); } } }