1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.IO;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Windows.Forms;
|
---|
6 | using HeuristicLab.Services.Optimization.Billing.Interfaces;
|
---|
7 | using HeuristicLab.Services.Optimization.Billing.Model;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Services.Optimization.Billing.BillingEngine {
|
---|
10 | public enum PlainTextInvoiceFormat {
|
---|
11 | TXT,
|
---|
12 | RTF,
|
---|
13 | PDF
|
---|
14 | }
|
---|
15 |
|
---|
16 | public class PlainTextInvoiceFormattingEngine : IInvoiceFormattingEngine {
|
---|
17 | private const string defaultTemplatePath = @"BillingEngine\InvoiceTemplate.txt";
|
---|
18 | private string template;
|
---|
19 | private string invoiceText;
|
---|
20 | private PlainTextInvoiceFormat invoiceFormat;
|
---|
21 | private string invoiceOutputPath;
|
---|
22 |
|
---|
23 | public PlainTextInvoiceFormattingEngine()
|
---|
24 | : this(defaultTemplatePath, PlainTextInvoiceFormat.RTF) {
|
---|
25 |
|
---|
26 | }
|
---|
27 |
|
---|
28 | public PlainTextInvoiceFormattingEngine(string templatePath, PlainTextInvoiceFormat format) {
|
---|
29 | if (!File.Exists(templatePath)) {
|
---|
30 | throw new FileNotFoundException("Cannot find template file", templatePath);
|
---|
31 | }
|
---|
32 | template = File.ReadAllText(templatePath);
|
---|
33 |
|
---|
34 | if (format == PlainTextInvoiceFormat.PDF) {
|
---|
35 | throw new Exception("PDF invoice format is currently not supported");
|
---|
36 | }
|
---|
37 | invoiceFormat = format;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public string Generate(Invoice invoice) {
|
---|
41 | invoiceText = string.Copy(template);
|
---|
42 |
|
---|
43 | invoiceText = invoiceText.Replace("{INVOICE_ID}", invoice.InvoiceId.ToString());
|
---|
44 |
|
---|
45 | invoiceText = invoiceText.Replace("{ORGANIZATION_NAME}", invoice.User.ContactInformation.OrganizationName);
|
---|
46 | invoiceText = invoiceText.Replace("{FIRSTNAME}", invoice.User.ContactInformation.FirstName);
|
---|
47 | invoiceText = invoiceText.Replace("{LASTNAME}", invoice.User.ContactInformation.LastName);
|
---|
48 | invoiceText = invoiceText.Replace("{STREET}", invoice.User.ContactInformation.Street);
|
---|
49 | invoiceText = invoiceText.Replace("{POSTAL_CODE}", invoice.User.ContactInformation.PostalCode);
|
---|
50 | invoiceText = invoiceText.Replace("{CITY}", invoice.User.ContactInformation.City);
|
---|
51 |
|
---|
52 | invoiceText = invoiceText.Replace("{INVOICE_DATE}", invoice.InvoiceDate.ToString("dd.MM.yyyy"));
|
---|
53 | invoiceText = invoiceText.Replace("{ORDER_ID}", invoice.OrderId.ToString());
|
---|
54 | invoiceText = invoiceText.Replace("{USER_NAME}", invoice.User.Name);
|
---|
55 | invoiceText = invoiceText.Replace("{PAYMENT_TERMS}", "2% 10, net 30 days");
|
---|
56 | invoiceText = invoiceText.Replace("{PAYMENT_DUE}", invoice.Due.Date.ToString("dd.MM.yyyy"));
|
---|
57 |
|
---|
58 | int itemNo = invoice.InvoiceLines.Count;
|
---|
59 | int first = invoiceText.IndexOf("#BEGIN");
|
---|
60 | int last = invoiceText.LastIndexOf("#END");
|
---|
61 | string orderLineTextTemplate = invoiceText.Substring(first, last + 6 - first);
|
---|
62 | invoiceText = invoiceText.Remove(first, last + 6 - first);
|
---|
63 | foreach (InvoiceLine invoiceLine in invoice.InvoiceLines.Reverse()) {
|
---|
64 | string invoiceLineText = string.Copy(orderLineTextTemplate);
|
---|
65 |
|
---|
66 | invoiceLineText = invoiceLineText.Replace("{ITEM}", (itemNo).ToString().PadRight(5));
|
---|
67 | itemNo--;
|
---|
68 | invoiceLineText = invoiceLineText.Replace("{PRODUCT_NAME}", invoiceLine.Product.Name.PadRight(39));
|
---|
69 | invoiceLineText = invoiceLineText.Replace("{QUANTITY}", invoiceLine.Quantity.ToString().PadRight(9));
|
---|
70 | invoiceLineText = invoiceLineText.Replace("{PRODUCT_PRICE}", invoiceLine.ProductPrice.ToString("C2").PadLeft(10));
|
---|
71 | double totalPrice = invoiceLine.Quantity * invoiceLine.ProductPrice;
|
---|
72 | invoiceLineText = invoiceLineText.Replace("{PRODUCT_TOTAL}", totalPrice.ToString("C2").PadLeft(11));
|
---|
73 |
|
---|
74 | invoiceLineText = invoiceLineText.Remove(invoiceLineText.IndexOf("#BEGIN"), 7);
|
---|
75 | invoiceLineText = invoiceLineText.Remove(invoiceLineText.IndexOf("#END"), 4);
|
---|
76 |
|
---|
77 | invoiceText = invoiceText.Insert(first, invoiceLineText);
|
---|
78 | }
|
---|
79 |
|
---|
80 | invoiceText = invoiceText.Replace("{INVOICE_SUB_TOTAL}", string.Format("{0:C2}", invoice.Total - invoice.Tax).PadLeft(18));
|
---|
81 | invoiceText = invoiceText.Replace("{INVOICE_TAX}", invoice.Tax.ToString("C2").PadLeft(12));
|
---|
82 | invoiceText = invoiceText.Replace("{INVOICE_TOTAL}", invoice.Total.ToString("C2").PadLeft(14));
|
---|
83 |
|
---|
84 | if (invoiceFormat == PlainTextInvoiceFormat.RTF) {
|
---|
85 | invoiceOutputPath = string.Format("{0}_{1}_{2}.rtf", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), "{CUSTOMER}");
|
---|
86 |
|
---|
87 | RichTextBox richTextBox = new RichTextBox();
|
---|
88 | richTextBox.Text = invoiceText;
|
---|
89 | richTextBox.SelectAll();
|
---|
90 | richTextBox.SelectionFont = new Font("Consolas", 10, FontStyle.Regular);
|
---|
91 |
|
---|
92 | File.WriteAllText(invoiceOutputPath, richTextBox.Rtf);
|
---|
93 | } else if (invoiceFormat == PlainTextInvoiceFormat.TXT) {
|
---|
94 | invoiceOutputPath = string.Format("{0}_{1}_{2}.txt", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), "{CUSTOMER}");
|
---|
95 |
|
---|
96 | File.WriteAllText(invoiceOutputPath, invoiceText);
|
---|
97 | } else if (invoiceFormat == PlainTextInvoiceFormat.PDF) {
|
---|
98 | throw new Exception("PDF invoice format is currently not supported");
|
---|
99 | }
|
---|
100 |
|
---|
101 | return invoiceOutputPath;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|