Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine/PlainTextInvoiceFormattingEngine.cs @ 16375

Last change on this file since 16375 was 10013, checked in by spimming, 11 years ago

#1888:

  • enabled transactions
  • enabled tracing output
  • set correct invoice data
  • saving invoices
File size: 5.7 KB
Line 
1using System;
2using System.Drawing;
3using System.IO;
4using System.Linq;
5using System.Text.RegularExpressions;
6using System.Windows.Forms;
7using HeuristicLab.Services.Optimization.Billing.Interfaces;
8using HeuristicLab.Services.Optimization.Billing.Model;
9
10namespace HeuristicLab.Services.Optimization.Billing.BillingEngine {
11  public enum PlainTextInvoiceFormat {
12    TXT,
13    RTF,
14    PDF
15  }
16
17  public class PlainTextInvoiceFormattingEngine : IInvoiceFormattingEngine {
18    private const string defaultTemplatePath = @"BillingEngine\InvoiceTemplate.txt";
19    private string template;
20    private string invoiceText;
21    private PlainTextInvoiceFormat invoiceFormat;
22    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    }
33
34    public PlainTextInvoiceFormattingEngine()
35      : this(defaultTemplatePath, PlainTextInvoiceFormat.RTF) {
36
37    }
38
39    public PlainTextInvoiceFormattingEngine(string templatePath, PlainTextInvoiceFormat format) {
40      if (!File.Exists(templatePath)) {
41        throw new FileNotFoundException("Cannot find template file", templatePath);
42      }
43      template = File.ReadAllText(templatePath);
44
45      if (format == PlainTextInvoiceFormat.PDF) {
46        throw new Exception("PDF invoice format is currently not supported");
47      }
48      invoiceFormat = format;
49    }
50
51    public string Generate(Invoice invoice) {
52      invoiceText = string.Copy(template);
53
54      invoiceText = invoiceText.Replace("{INVOICE_ID}", invoice.InvoiceId.ToString());
55
56      invoiceText = invoiceText.Replace("{ORGANIZATION_NAME}", invoice.User.ContactInformation.OrganizationName);
57      invoiceText = invoiceText.Replace("{FIRSTNAME}", invoice.User.ContactInformation.FirstName);
58      invoiceText = invoiceText.Replace("{LASTNAME}", invoice.User.ContactInformation.LastName);
59      invoiceText = invoiceText.Replace("{STREET}", invoice.User.ContactInformation.Street);
60      invoiceText = invoiceText.Replace("{POSTAL_CODE}", invoice.User.ContactInformation.PostalCode);
61      invoiceText = invoiceText.Replace("{CITY}", invoice.User.ContactInformation.City);
62
63      invoiceText = invoiceText.Replace("{INVOICE_DATE}", invoice.InvoiceDate.ToString("dd.MM.yyyy"));
64      invoiceText = invoiceText.Replace("{ORDER_ID}", invoice.OrderId.ToString());
65      invoiceText = invoiceText.Replace("{USER_NAME}", invoice.User.Name);
66      invoiceText = invoiceText.Replace("{PAYMENT_TERMS}", "2% 10, net 30 days");
67      invoiceText = invoiceText.Replace("{PAYMENT_DUE}", invoice.Due.Date.ToString("dd.MM.yyyy"));
68
69      int itemNo = invoice.InvoiceLines.Count;
70      int first = invoiceText.IndexOf("#BEGIN");
71      int last = invoiceText.LastIndexOf("#END");
72      string orderLineTextTemplate = invoiceText.Substring(first, last + 6 - first);
73      invoiceText = invoiceText.Remove(first, last + 6 - first);
74      foreach (InvoiceLine invoiceLine in invoice.InvoiceLines.Reverse()) {
75        string invoiceLineText = string.Copy(orderLineTextTemplate);
76
77        invoiceLineText = invoiceLineText.Replace("{ITEM}", (itemNo).ToString().PadRight(5));
78        itemNo--;
79        invoiceLineText = invoiceLineText.Replace("{PRODUCT_NAME}", invoiceLine.Product.Name.PadRight(39));
80        invoiceLineText = invoiceLineText.Replace("{QUANTITY}", invoiceLine.Quantity.ToString().PadRight(9));
81        invoiceLineText = invoiceLineText.Replace("{PRODUCT_PRICE}", invoiceLine.ProductPrice.ToString("C2").PadLeft(10));
82        double totalPrice = invoiceLine.Quantity * invoiceLine.ProductPrice;
83        invoiceLineText = invoiceLineText.Replace("{PRODUCT_TOTAL}", totalPrice.ToString("C2").PadLeft(11));
84
85        invoiceLineText = invoiceLineText.Remove(invoiceLineText.IndexOf("#BEGIN"), 7);
86        invoiceLineText = invoiceLineText.Remove(invoiceLineText.IndexOf("#END"), 4);
87
88        invoiceText = invoiceText.Insert(first, invoiceLineText);
89      }
90
91      invoiceText = invoiceText.Replace("{INVOICE_SUB_TOTAL}", string.Format("{0:C2}", invoice.Total - invoice.Tax).PadLeft(18));
92      invoiceText = invoiceText.Replace("{INVOICE_TAX}", invoice.Tax.ToString("C2").PadLeft(12));
93      invoiceText = invoiceText.Replace("{INVOICE_TOTAL}", invoice.Total.ToString("C2").PadLeft(14));
94
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
99      if (invoiceFormat == PlainTextInvoiceFormat.RTF) {
100        invoiceOutputPath = string.Format("{0}_{1}_{2}.rtf", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), customerName);
101
102        RichTextBox richTextBox = new RichTextBox();
103        richTextBox.Text = invoiceText;
104        richTextBox.SelectAll();
105        richTextBox.SelectionFont = new Font("Consolas", 10, FontStyle.Regular);
106
107        //File.WriteAllText(invoiceOutputPath, richTextBox.Rtf);
108        InvoiceDao.SaveDocument(invoiceOutputPath, richTextBox.Rtf);
109      } else if (invoiceFormat == PlainTextInvoiceFormat.TXT) {
110        invoiceOutputPath = string.Format("{0}_{1}_{2}.txt", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), customerName);
111
112        //File.WriteAllText(invoiceOutputPath, invoiceText);
113        InvoiceDao.SaveDocument(invoiceOutputPath, invoiceText);
114      } else if (invoiceFormat == PlainTextInvoiceFormat.PDF) {
115        throw new Exception("PDF invoice format is currently not supported");
116      }
117
118      return invoiceOutputPath;
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.