Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/18/13 11:39:31 (10 years ago)
Author:
spimming
Message:

#1888:

  • Users now contain ContactInformation
  • Fixed formatting in invoice template
  • Fixed calculation for next invoice date
  • Worked on indentation for invoice fields
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine/BillingEngine.cs

    r9619 r9641  
    5353        try {
    5454          Trace.WriteLine("[BillingEngine] Start billing cycle");
    55           //currentDate = DateTime.Parse(DateTime.Now.ToShortDateString());
    5655          currentDate = DateTime.Now.Date;
    5756          Trace.WriteLine(string.Format("[BillingEngine] Current date: {0}", currentDate));
     
    6059          foreach (Order order in activeOrders) {
    6160            if (!order.NextBillableDay.HasValue) {
    62               // there has never been an invoice generated from this order
     61              // An invoice has never been generated from this order
    6362              order.NextBillableDay = currentDate;
    6463            }
     
    8382                invoiceLine.Product = orderLine.Product;
    8483                invoiceLine.ProductPrice = orderLine.ProductPrice;
    85                 invoiceLine.Quantity = invoiceLine.Quantity;
     84                invoiceLine.Quantity = orderLine.Quantity;
    8685                invoice.InvoiceLines.Add(invoiceLine);
    8786              }
     
    9089              usageCharges.Invoice = invoice;
    9190              usageCharges.Product = billingService.GetProductByName("Oaas Usage Charges").First();
    92               usageCharges.ProductPrice = 999.99;
     91              usageCharges.ProductPrice = -999.99;
    9392              usageCharges.Quantity = 1;
    9493              invoice.InvoiceLines.Add(usageCharges);
    9594
    9695              // Bill Post Processing: Apply Discounts or Promotions
    97               // eg. add discounts
    98               // sum up items to sub-total, calc. tax -> total current charges
     96              // add discounts, sum up items to sub-total, calc. tax -> total current charges
     97              // maybe use windows workflow foundation for this task
    9998
    10099              // Invoice Formatting Engine: Generate Invoice
     
    117116
    118117    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);
    122120      }
    123121
    124122      DateTime nextBillingDay = CalculateNextBillingDate(order);
    125123
    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 {
    127130        order.NextBillableDay = nextBillingDay;
    128131      }
     
    133136
    134137      if (order.BillingPeriod == BillingPeriod.Weekly) {
    135         nextBillingDay = nextBillingDay.AddDays(7);
     138        nextBillingDay = Next(currentDate, DayOfWeek.Sunday);
    136139      } 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;
    146145      } 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);
    152147      }
    153148
    154149      return nextBillingDay;
    155150    }
     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    }
    156159  }
    157160}
Note: See TracChangeset for help on using the changeset viewer.