1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using HeuristicLab.Services.Optimization.Web.Models;
|
---|
7 | using HeuristicLab.Services.Optimization.Billing.Model;
|
---|
8 | using HeuristicLab.Services.Optimization.Billing.Interfaces;
|
---|
9 | using HeuristicLab.Services.Optimization.Billing;
|
---|
10 | using System.Web.Security;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Services.Optimization.Web.Controllers
|
---|
13 | {
|
---|
14 | public class OrderController : Controller
|
---|
15 | {
|
---|
16 | private IOptimizationBilling billing = new MockupOptimizationBilling();
|
---|
17 |
|
---|
18 | //
|
---|
19 | // GET: /Order/
|
---|
20 |
|
---|
21 | public ActionResult Index()
|
---|
22 | {
|
---|
23 | return CreateOrder(new OrderModel()); //View();
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | [HttpPost]
|
---|
28 | public ActionResult CreateOrder(OrderModel model) {
|
---|
29 | Session["order"] = model;
|
---|
30 | return AddProduct();
|
---|
31 | }
|
---|
32 |
|
---|
33 | public ActionResult AddProduct() {
|
---|
34 | return View("AddProduct", new ProductsModel() { Products = billing.GetProducts() });
|
---|
35 | }
|
---|
36 |
|
---|
37 | [HttpPost]
|
---|
38 | public ActionResult AddProduct(ProductsModel product) {
|
---|
39 | var order = Session["order"] as OrderModel;
|
---|
40 | var prod = (from b in billing.GetProducts()
|
---|
41 | where b.Id == product.ProductId
|
---|
42 | select b).FirstOrDefault();
|
---|
43 | order.ProductQuantities[prod.Id] = product.Quantity;
|
---|
44 | return AddProduct();
|
---|
45 | }
|
---|
46 |
|
---|
47 | public ActionResult SaveOrder() {
|
---|
48 | var orderModel = Session["order"] as OrderModel;
|
---|
49 | foreach (var key in orderModel.ProductQuantities.Keys) {
|
---|
50 | var product = (from p in billing.GetProducts() where p.Id == key select p).FirstOrDefault();
|
---|
51 | orderModel.Products[key] = product;
|
---|
52 | }
|
---|
53 | return View(orderModel);
|
---|
54 | }
|
---|
55 |
|
---|
56 | [HttpPost]
|
---|
57 | [ActionName("SaveOrder")]
|
---|
58 | public ActionResult SaveOrderPost() {
|
---|
59 | var orderModel = Session["order"] as OrderModel;
|
---|
60 | //TODO: Save order via back-end
|
---|
61 | var order = new Order() {
|
---|
62 | State = OrderState.Created,
|
---|
63 | User = new User() { Name = Membership.GetUser().UserName },
|
---|
64 | BillingType = orderModel.BillingType,
|
---|
65 | BillingPeriod = orderModel.BillingPeriod
|
---|
66 | };
|
---|
67 |
|
---|
68 | var orderLines = new List<OrderLine>();
|
---|
69 | var products = billing.GetProducts();
|
---|
70 | foreach (var key in orderModel.ProductQuantities.Keys) {
|
---|
71 | var product = (from p in products where p.Id == key select p).FirstOrDefault();
|
---|
72 | orderLines.Add(new OrderLine() {
|
---|
73 | Order = order,
|
---|
74 | Product = product,
|
---|
75 | ProductPrice = product.Price,
|
---|
76 | Quantity = orderModel.ProductQuantities[key]
|
---|
77 | });
|
---|
78 | }
|
---|
79 | order.OrderLines = orderLines;
|
---|
80 | billing.SaveOrder(order);
|
---|
81 | return RedirectToAction("OrderSaved");
|
---|
82 | }
|
---|
83 |
|
---|
84 | public ActionResult OrderSaved() {
|
---|
85 | return View();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|