Changeset 9641 for branches/OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine/BillingEngine.cs
- Timestamp:
- 06/18/13 11:39:31 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine/BillingEngine.cs
r9619 r9641 53 53 try { 54 54 Trace.WriteLine("[BillingEngine] Start billing cycle"); 55 //currentDate = DateTime.Parse(DateTime.Now.ToShortDateString());56 55 currentDate = DateTime.Now.Date; 57 56 Trace.WriteLine(string.Format("[BillingEngine] Current date: {0}", currentDate)); … … 60 59 foreach (Order order in activeOrders) { 61 60 if (!order.NextBillableDay.HasValue) { 62 // there has never been an invoicegenerated from this order61 // An invoice has never been generated from this order 63 62 order.NextBillableDay = currentDate; 64 63 } … … 83 82 invoiceLine.Product = orderLine.Product; 84 83 invoiceLine.ProductPrice = orderLine.ProductPrice; 85 invoiceLine.Quantity = invoiceLine.Quantity;84 invoiceLine.Quantity = orderLine.Quantity; 86 85 invoice.InvoiceLines.Add(invoiceLine); 87 86 } … … 90 89 usageCharges.Invoice = invoice; 91 90 usageCharges.Product = billingService.GetProductByName("Oaas Usage Charges").First(); 92 usageCharges.ProductPrice = 999.99;91 usageCharges.ProductPrice = -999.99; 93 92 usageCharges.Quantity = 1; 94 93 invoice.InvoiceLines.Add(usageCharges); 95 94 96 95 // Bill Post Processing: Apply Discounts or Promotions 97 // eg. add discounts98 // sum up items to sub-total, calc. tax -> total current charges96 // add discounts, sum up items to sub-total, calc. tax -> total current charges 97 // maybe use windows workflow foundation for this task 99 98 100 99 // Invoice Formatting Engine: Generate Invoice … … 117 116 118 117 private void UpdateBillingState(Order order) { 119 if (order.ActiveUntil != null && order.ActiveUntil == order.NextBillableDay) { 120 order.NextBillableDay = null; 121 order.State = OrderState.Finished; 118 if (order.BillingType == BillingType.Pre) { 119 throw new Exception("This billing type is currently not supported: " + order.BillingType); 122 120 } 123 121 124 122 DateTime nextBillingDay = CalculateNextBillingDate(order); 125 123 126 if (order.ActiveUntil != null && order.ActiveUntil < nextBillingDay) { 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 { 127 130 order.NextBillableDay = nextBillingDay; 128 131 } … … 133 136 134 137 if (order.BillingPeriod == BillingPeriod.Weekly) { 135 nextBillingDay = nextBillingDay.AddDays(7);138 nextBillingDay = Next(currentDate, DayOfWeek.Sunday); 136 139 } else if (order.BillingPeriod == BillingPeriod.Monthly) { 137 if (order.BillingType == BillingType.Pre) { 138 nextBillingDay = nextBillingDay.AddDays(DateTime.DaysInMonth(nextBillingDay.Year, nextBillingDay.Month) + 1); 139 } else if (order.BillingType == BillingType.Post) { 140 if (nextBillingDay.Month == 12) { 141 nextBillingDay = nextBillingDay.AddDays(DateTime.DaysInMonth(nextBillingDay.Year + 1, 1) + 1); 142 } else { 143 nextBillingDay = nextBillingDay.AddDays(DateTime.DaysInMonth(nextBillingDay.Year, nextBillingDay.Month + 1) + 1); 144 } 145 } 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; 146 145 } else if (order.BillingPeriod == BillingPeriod.Yearly) { 147 if (order.BillingType == BillingType.Pre) { 148 nextBillingDay = new DateTime(DateTime.Now.Year + 1, 1, 1); 149 } else if (order.BillingType == BillingType.Post) { 150 nextBillingDay = new DateTime(DateTime.Now.Year + 1, 12, 31); 151 } 146 nextBillingDay = new DateTime(currentDate.AddYears(1).Year, 12, 31); 152 147 } 153 148 154 149 return nextBillingDay; 155 150 } 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 } 156 159 } 157 160 }
Note: See TracChangeset
for help on using the changeset viewer.