[9619] | 1 |
|
---|
| 2 | using System;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using System.Configuration;
|
---|
| 5 | using System.Diagnostics;
|
---|
| 6 | using System.Linq;
|
---|
| 7 | using System.Threading;
|
---|
| 8 | using HeuristicLab.Services.Optimization.Billing.Business;
|
---|
| 9 | using HeuristicLab.Services.Optimization.Billing.Interfaces;
|
---|
| 10 | using HeuristicLab.Services.Optimization.Billing.Model;
|
---|
| 11 | namespace HeuristicLab.Services.Optimization.Billing.BillingEngine {
|
---|
| 12 | public class BillingEngine {
|
---|
| 13 | private bool stop;
|
---|
| 14 | private AutoResetEvent waitHandle;
|
---|
| 15 | private TimeSpan interval;
|
---|
| 16 | private DateTime currentDate;
|
---|
| 17 |
|
---|
| 18 | private IOptimizationBilling billingService;
|
---|
| 19 | public IOptimizationBilling BillingService {
|
---|
| 20 | get {
|
---|
| 21 | if (billingService == null) {
|
---|
| 22 | billingService = new BillingService();
|
---|
| 23 | }
|
---|
| 24 | return billingService;
|
---|
| 25 | }
|
---|
| 26 | set { billingService = value; }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | private IInvoiceFormattingEngine invoiceFormattingEngine;
|
---|
| 30 | public IInvoiceFormattingEngine InvoiceFormattingEngine {
|
---|
| 31 | get {
|
---|
| 32 | if (invoiceFormattingEngine == null) {
|
---|
| 33 | invoiceFormattingEngine = new PlainTextInvoiceFormattingEngine();
|
---|
| 34 | }
|
---|
| 35 | return invoiceFormattingEngine;
|
---|
| 36 | }
|
---|
| 37 | set { invoiceFormattingEngine = value; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public BillingEngine() {
|
---|
| 41 | stop = false;
|
---|
| 42 | interval = TimeSpan.Parse(ConfigurationManager.AppSettings["BillingEngineInterval"]);
|
---|
| 43 | waitHandle = new AutoResetEvent(true);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public void StopBillingEngine() {
|
---|
| 47 | stop = true;
|
---|
| 48 | waitHandle.Set();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public void Run() {
|
---|
| 52 | while (!stop) {
|
---|
| 53 | try {
|
---|
| 54 | Trace.WriteLine("[BillingEngine] Start billing cycle");
|
---|
| 55 | currentDate = DateTime.Now.Date;
|
---|
| 56 | Trace.WriteLine(string.Format("[BillingEngine] Current date: {0}", currentDate));
|
---|
| 57 | IList<Order> activeOrders = BillingService.GetOrdersByState(OrderState.Active);
|
---|
| 58 | Trace.WriteLine(string.Format("[BillingEngine] Processing {0} active orders.", activeOrders.Count));
|
---|
| 59 | foreach (Order order in activeOrders) {
|
---|
| 60 | if (!order.NextBillableDay.HasValue) {
|
---|
[9641] | 61 | // An invoice has never been generated from this order
|
---|
[9619] | 62 | order.NextBillableDay = currentDate;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | if (order.NextBillableDay >= currentDate) {
|
---|
| 66 | // TODO: Embed everything in a transaction
|
---|
| 67 |
|
---|
| 68 | // Collect Usage Data
|
---|
| 69 | //IList<UsageRecord> usageRecrods = BillingService.GetUsageRecords(order.User.Name, order.LastBillableDay.Value, currentDate);
|
---|
| 70 |
|
---|
| 71 | // Collect Invoice Data
|
---|
| 72 | Invoice invoice = new Invoice();
|
---|
| 73 | invoice.Order = order;
|
---|
| 74 | invoice.User = order.User;
|
---|
| 75 | invoice.Due = currentDate.AddMonths(1);
|
---|
| 76 | invoice.InvoiceDate = currentDate;
|
---|
| 77 | invoice.Status = InvoiceStatus.Open;
|
---|
| 78 | invoice.InvoiceLines = new List<InvoiceLine>();
|
---|
| 79 | foreach (OrderLine orderLine in order.OrderLines) {
|
---|
| 80 | InvoiceLine invoiceLine = new InvoiceLine();
|
---|
| 81 | invoiceLine.Invoice = invoice;
|
---|
| 82 | invoiceLine.Product = orderLine.Product;
|
---|
| 83 | invoiceLine.ProductPrice = orderLine.ProductPrice;
|
---|
[9641] | 84 | invoiceLine.Quantity = orderLine.Quantity;
|
---|
[9619] | 85 | invoice.InvoiceLines.Add(invoiceLine);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | InvoiceLine usageCharges = new InvoiceLine();
|
---|
| 89 | usageCharges.Invoice = invoice;
|
---|
| 90 | usageCharges.Product = billingService.GetProductByName("Oaas Usage Charges").First();
|
---|
[9641] | 91 | usageCharges.ProductPrice = -999.99;
|
---|
[9619] | 92 | usageCharges.Quantity = 1;
|
---|
| 93 | invoice.InvoiceLines.Add(usageCharges);
|
---|
| 94 |
|
---|
| 95 | // Bill Post Processing: Apply Discounts or Promotions
|
---|
[9641] | 96 | // add discounts, sum up items to sub-total, calc. tax -> total current charges
|
---|
| 97 | // maybe use windows workflow foundation for this task
|
---|
[9619] | 98 |
|
---|
| 99 | // Invoice Formatting Engine: Generate Invoice
|
---|
| 100 | invoice.InvoiceDocument = InvoiceFormattingEngine.Generate(invoice);
|
---|
| 101 |
|
---|
| 102 | UpdateBillingState(order);
|
---|
| 103 | billingService.UpdateOrder(order);
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | Trace.WriteLine("[BillingEngine] Finished billing cycle");
|
---|
| 108 | }
|
---|
| 109 | catch (Exception e) {
|
---|
| 110 | Trace.WriteLine(string.Format("[BillingEngine] The following exception occured: {0}", e.ToString()));
|
---|
| 111 | }
|
---|
| 112 | waitHandle.WaitOne(interval);
|
---|
| 113 | }
|
---|
| 114 | waitHandle.Close();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | private void UpdateBillingState(Order order) {
|
---|
[9641] | 118 | if (order.BillingType == BillingType.Pre) {
|
---|
| 119 | throw new Exception("This billing type is currently not supported: " + order.BillingType);
|
---|
[9619] | 120 | }
|
---|
| 121 |
|
---|
| 122 | DateTime nextBillingDay = CalculateNextBillingDate(order);
|
---|
| 123 |
|
---|
[9641] | 124 | if (order.ActiveUntil.HasValue && order.ActiveUntil == currentDate) {
|
---|
| 125 | order.State = OrderState.Finished;
|
---|
| 126 | order.NextBillableDay = null;
|
---|
| 127 | } else if (order.ActiveUntil.HasValue && order.ActiveUntil < nextBillingDay) {
|
---|
| 128 | order.NextBillableDay = order.ActiveUntil;
|
---|
| 129 | } else {
|
---|
[9619] | 130 | order.NextBillableDay = nextBillingDay;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private DateTime CalculateNextBillingDate(Order order) {
|
---|
| 135 | DateTime nextBillingDay = currentDate;
|
---|
| 136 |
|
---|
| 137 | if (order.BillingPeriod == BillingPeriod.Weekly) {
|
---|
[9641] | 138 | nextBillingDay = Next(currentDate, DayOfWeek.Sunday);
|
---|
[9619] | 139 | } else if (order.BillingPeriod == BillingPeriod.Monthly) {
|
---|
[9641] | 140 | DateTime endOfNextMonth = new DateTime(
|
---|
| 141 | currentDate.AddMonths(1).Year,
|
---|
| 142 | currentDate.AddMonths(1).Month,
|
---|
| 143 | DateTime.DaysInMonth(currentDate.AddMonths(1).Year, currentDate.AddMonths(1).Month));
|
---|
| 144 | nextBillingDay = endOfNextMonth;
|
---|
[9619] | 145 | } else if (order.BillingPeriod == BillingPeriod.Yearly) {
|
---|
[9641] | 146 | nextBillingDay = new DateTime(currentDate.AddYears(1).Year, 12, 31);
|
---|
[9619] | 147 | }
|
---|
| 148 |
|
---|
| 149 | return nextBillingDay;
|
---|
| 150 | }
|
---|
[9641] | 151 |
|
---|
| 152 | private DateTime Next(DateTime from, DayOfWeek dayOfWeek) {
|
---|
| 153 | int start = (int)from.DayOfWeek;
|
---|
| 154 | int target = (int)dayOfWeek;
|
---|
| 155 | if (target <= start)
|
---|
| 156 | target += 7;
|
---|
| 157 | return from.AddDays(target - start);
|
---|
| 158 | }
|
---|
[9619] | 159 | }
|
---|
| 160 | }
|
---|