Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Web/Controllers/OrderController.cs @ 9586

Last change on this file since 9586 was 9586, checked in by fschoepp, 11 years ago

#1888:

  • Billing Component may now be deactivated by setting the BillingEnabled flag within the OaaS service configuration.
  • Added views for invoices and usage data.
  • Changed appearance of the SaveOrder view.
File size: 4.6 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using System.Web.Mvc;
4using System.Web.Security;
5using HeuristicLab.Services.Optimization.Billing.Business;
6using HeuristicLab.Services.Optimization.Billing.Interfaces;
7using HeuristicLab.Services.Optimization.Billing.Model;
8using HeuristicLab.Services.Optimization.Web.Models;
9using HeuristicLab.Services.Optimization.Web.Helpers;
10
11namespace 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}
Note: See TracBrowser for help on using the repository browser.