Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/01/13 13:18:19 (11 years ago)
Author:
spimming
Message:

#1888:

  • enabled transactions
  • enabled tracing output
  • set correct invoice data
  • saving invoices
Location:
branches/OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine
Files:
2 added
2 edited

Legend:

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

    r9653 r10013  
    66using System.Linq;
    77using System.Threading;
     8using System.Transactions;
    89using HeuristicLab.Services.Optimization.Billing.Business;
    910using HeuristicLab.Services.Optimization.Billing.Interfaces;
     
    5152    public void Run() {
    5253      while (!stop) {
     54        TransactionOptions options = new TransactionOptions();
     55        options.IsolationLevel = IsolationLevel.ReadUncommitted;
     56        TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, options);
    5357        try {
    5458          Trace.WriteLine("[BillingEngine] Start billing cycle");
     
    5862          Trace.WriteLine(string.Format("[BillingEngine] Processing {0} active orders.", activeOrders.Count));
    5963          foreach (Order order in activeOrders) {
     64            Trace.WriteLine(string.Format("[BillingEngine] Processing order with id: {0}", order.OrderId));
     65
    6066            if (!order.NextBillableDay.HasValue) {
    6167              // An invoice has never been generated from this order
     
    6470            }
    6571
    66             if (order.NextBillableDay >= currentDate) {
    67               // TODO: Embed everything in a transaction
    68 
     72            if (order.NextBillableDay <= currentDate) {
    6973              // Collect Usage Data
     74              Trace.WriteLine("[BillingEngine] Collecting usage data");
    7075              //IList<UsageRecord> usageRecrods = BillingService.GetUsageRecords(order.User.Name, order.LastBillableDay.Value, currentDate);
    7176
    7277              // Collect Invoice Data
     78              Trace.WriteLine("[BillingEngine] Collecting invoice data");
    7379              Invoice invoice = new Invoice();
    7480              invoice.Order = order;
     
    96102              // Bill Post Processing: Apply Discounts or Promotions
    97103              // add discounts, sum up items to sub-total, calc. tax -> total current charges
    98               // maybe use windows workflow foundation for this task
     104              // maybe use rule execution feature from the windows workflow foundation for this task
     105              Trace.WriteLine("[BillingEngine] Bill post preccessing");
     106              double subTotal = 0.0;
     107              foreach (InvoiceLine invoiceLine in invoice.InvoiceLines) {
     108                subTotal += invoiceLine.ProductPrice;
     109              }
     110              invoice.SubTotal = subTotal;
     111              invoice.Tax = invoice.SubTotal * 0.2;
     112              invoice.Total = invoice.SubTotal + invoice.Tax;
    99113
    100               // Invoice Formatting Engine: Generate Invoice
     114              // Invoice Formatting Engine: Generate Invoice
     115              Trace.WriteLine("[BillingEngine] Generating invoice document");
    101116              invoice.InvoiceDocument = InvoiceFormattingEngine.Generate(invoice);
    102117
     
    104119              billingService.UpdateOrder(order);
    105120              billingService.SaveInvoice(invoice);
     121
     122              transaction.Complete();
    106123            }
    107124          }
     
    112129          Trace.WriteLine(string.Format("[BillingEngine] The following exception occured: {0}", e.ToString()));
    113130        }
     131        finally {
     132          transaction.Dispose();
     133        }
     134
    114135        waitHandle.WaitOne(interval);
    115136      }
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine/PlainTextInvoiceFormattingEngine.cs

    r9653 r10013  
    33using System.IO;
    44using System.Linq;
     5using System.Text.RegularExpressions;
    56using System.Windows.Forms;
    67using HeuristicLab.Services.Optimization.Billing.Interfaces;
     
    2021    private PlainTextInvoiceFormat invoiceFormat;
    2122    private string invoiceOutputPath;
     23    private IInvoiceDao invoiceDao;
     24
     25    private IInvoiceDao InvoiceDao {
     26      get {
     27        if (invoiceDao == null) {
     28          invoiceDao = new FileSystemInvoiceDao();
     29        }
     30        return invoiceDao;
     31      }
     32    }
    2233
    2334    public PlainTextInvoiceFormattingEngine()
     
    8293      invoiceText = invoiceText.Replace("{INVOICE_TOTAL}", invoice.Total.ToString("C2").PadLeft(14));
    8394
     95      // remove all escape sequences and replace directory separator with a hyphen
     96      string customerName = Regex.Replace(invoice.User.ContactInformation.OrganizationName, "\\r\\n|\\[bnrt]", "");
     97      customerName = customerName.Replace("/", "-");
     98
    8499      if (invoiceFormat == PlainTextInvoiceFormat.RTF) {
    85         invoiceOutputPath = string.Format("{0}_{1}_{2}.rtf", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), "{CUSTOMER}");
     100        invoiceOutputPath = string.Format("{0}_{1}_{2}.rtf", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), customerName);
    86101
    87102        RichTextBox richTextBox = new RichTextBox();
     
    90105        richTextBox.SelectionFont = new Font("Consolas", 10, FontStyle.Regular);
    91106
    92         File.WriteAllText(invoiceOutputPath, richTextBox.Rtf);
     107        //File.WriteAllText(invoiceOutputPath, richTextBox.Rtf);
     108        InvoiceDao.SaveDocument(invoiceOutputPath, richTextBox.Rtf);
    93109      } else if (invoiceFormat == PlainTextInvoiceFormat.TXT) {
    94         invoiceOutputPath = string.Format("{0}_{1}_{2}.txt", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), "{CUSTOMER}");
     110        invoiceOutputPath = string.Format("{0}_{1}_{2}.txt", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), customerName);
    95111
    96         File.WriteAllText(invoiceOutputPath, invoiceText);
     112        //File.WriteAllText(invoiceOutputPath, invoiceText);
     113        InvoiceDao.SaveDocument(invoiceOutputPath, invoiceText);
    97114      } else if (invoiceFormat == PlainTextInvoiceFormat.PDF) {
    98115        throw new Exception("PDF invoice format is currently not supported");
Note: See TracChangeset for help on using the changeset viewer.