1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Web.Mvc;
|
---|
4 | using System.Web.Security;
|
---|
5 | using HeuristicLab.Services.Optimization.Billing.Business;
|
---|
6 | using HeuristicLab.Services.Optimization.Billing.Interfaces;
|
---|
7 | using HeuristicLab.Services.Optimization.Billing.Model;
|
---|
8 | using HeuristicLab.Services.Optimization.Web.Models;
|
---|
9 | using HeuristicLab.Services.Optimization.Web.Helpers;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Services.Optimization.Web.Controllers {
|
---|
12 | [Authorize(Roles = "Web User")]
|
---|
13 | [BillingFilterActionAttribute]
|
---|
14 | public class OrderController : BaseController {
|
---|
15 | private IOptimizationBilling billing = BillingServiceProvider.Instance;
|
---|
16 |
|
---|
17 | //
|
---|
18 | // GET: /Order/
|
---|
19 |
|
---|
20 | public ActionResult Index()
|
---|
21 | {
|
---|
22 | if (RedirectToLoginIfNecessary("Order", false))
|
---|
23 | return View();
|
---|
24 | return CreateOrder(new OrderModel()); //View();
|
---|
25 | }
|
---|
26 |
|
---|
27 | public ActionResult Overview() {
|
---|
28 | if (RedirectToLoginIfNecessary("Order", false))
|
---|
29 | return View();
|
---|
30 | MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
|
---|
31 | var orders = billing.GetOrders(currentUser.UserName);
|
---|
32 | return View(new OverviewModel() { Orders = orders });
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | [HttpPost]
|
---|
37 | public ActionResult CreateOrder(OrderModel model) {
|
---|
38 | if (RedirectToLoginIfNecessary("Order", false))
|
---|
39 | return View();
|
---|
40 | Session["order"] = model;
|
---|
41 | var currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
|
---|
42 | if (billing.GetUser(currentUser.UserName) == null) {
|
---|
43 | billing.SaveUser(new User() { Name = currentUser.UserName, PaymentInformation = new List<PaymentInformation>() });
|
---|
44 | }
|
---|
45 | model.User = billing.GetUser(currentUser.UserName);
|
---|
46 | return AddProduct();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public ActionResult AddProduct() {
|
---|
50 | return View("AddProduct", new ProductsModel() { Products = billing.GetProducts() });
|
---|
51 | }
|
---|
52 |
|
---|
53 | [HttpPost]
|
---|
54 | public ActionResult AddProduct(ProductsModel product) {
|
---|
55 | var order = Session["order"] as OrderModel;
|
---|
56 | var prod = (from b in billing.GetProducts()
|
---|
57 | where b.ProductId == product.ProductId
|
---|
58 | select b).FirstOrDefault();
|
---|
59 | order.ProductQuantities[prod.ProductId] = product.Quantity;
|
---|
60 | return AddProduct();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public ActionResult SaveOrder() {
|
---|
64 | var orderModel = Session["order"] as OrderModel;
|
---|
65 | foreach (var key in orderModel.ProductQuantities.Keys) {
|
---|
66 | var product = (from p in billing.GetProducts() where p.ProductId == key select p).FirstOrDefault();
|
---|
67 | orderModel.Products[key] = product;
|
---|
68 | }
|
---|
69 | return View(orderModel);
|
---|
70 | }
|
---|
71 |
|
---|
72 | [HttpPost]
|
---|
73 | [ActionName("SaveOrder")]
|
---|
74 | public ActionResult SaveOrderPost(OrderModel orderModel) {
|
---|
75 | var orderProductModel = Session["order"] as OrderModel;
|
---|
76 | //TODO: Save order via back-end
|
---|
77 | var order = new Order() {
|
---|
78 | State = OrderState.Created,
|
---|
79 | User = new User() { Name = Membership.GetUser().UserName },
|
---|
80 | BillingType = orderModel.BillingType,
|
---|
81 | BillingPeriod = orderModel.BillingPeriod
|
---|
82 | };
|
---|
83 |
|
---|
84 | var orderLines = new List<OrderLine>();
|
---|
85 | var products = billing.GetProducts();
|
---|
86 | foreach (var key in orderProductModel.ProductQuantities.Keys) {
|
---|
87 | var product = (from p in products where p.ProductId == key select p).FirstOrDefault();
|
---|
88 | orderLines.Add(new OrderLine() {
|
---|
89 | Order = order,
|
---|
90 | Product = product,
|
---|
91 | ProductPrice = product.Price,
|
---|
92 | Quantity = orderProductModel.ProductQuantities[key]
|
---|
93 | });
|
---|
94 | }
|
---|
95 | order.OrderLines = orderLines;
|
---|
96 | billing.SaveOrder(order);
|
---|
97 | return RedirectToAction("OrderSaved");
|
---|
98 | }
|
---|
99 |
|
---|
100 | public ActionResult Payment() {
|
---|
101 | var paymentModel = new PaymentModel();
|
---|
102 | return View(paymentModel);
|
---|
103 | }
|
---|
104 |
|
---|
105 | [HttpPost]
|
---|
106 | public ActionResult SavePayment(PaymentModel model) {
|
---|
107 | var user = billing.GetUser(UserName);
|
---|
108 | var oldInfo = (from pi in user.PaymentInformation where pi.CardNumber == model.CardNumber select pi).FirstOrDefault();
|
---|
109 | if (oldInfo != null) {
|
---|
110 | oldInfo.AdditionalInformation = model.AdditionalInformation;
|
---|
111 | oldInfo.PaymentMethod = model.Method;
|
---|
112 | billing.SaveUser(user);
|
---|
113 | }
|
---|
114 | else {
|
---|
115 | user.PaymentInformation.Add(new PaymentInformation() { CardNumber = model.CardNumber, AdditionalInformation = model.AdditionalInformation, PaymentMethod = model.Method });
|
---|
116 | }
|
---|
117 |
|
---|
118 | return RedirectToAction("SaveOrder");
|
---|
119 | }
|
---|
120 |
|
---|
121 | public ActionResult OrderSaved() {
|
---|
122 | return View();
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|