Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine/PlainTextInvoiceFormattingEngine.cs @ 15428

Last change on this file since 15428 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
RevLine 
[9619]1using System;
2using System.Drawing;
3using System.IO;
4using System.Linq;
[10013]5using System.Text.RegularExpressions;
[9619]6using System.Windows.Forms;
7using HeuristicLab.Services.Optimization.Billing.Interfaces;
8using HeuristicLab.Services.Optimization.Billing.Model;
9
10namespace HeuristicLab.Services.Optimization.Billing.BillingEngine {
[9641]11  public enum PlainTextInvoiceFormat {
12    TXT,
13    RTF,
14    PDF
15  }
16
17  public class PlainTextInvoiceFormattingEngine : IInvoiceFormattingEngine {
[9619]18    private const string defaultTemplatePath = @"BillingEngine\InvoiceTemplate.txt";
19    private string template;
20    private string invoiceText;
[9641]21    private PlainTextInvoiceFormat invoiceFormat;
[9619]22    private string invoiceOutputPath;
[10013]23    private IInvoiceDao invoiceDao;
[9619]24
[10013]25    private IInvoiceDao InvoiceDao {
26      get {
27        if (invoiceDao == null) {
28          invoiceDao = new FileSystemInvoiceDao();
29        }
30        return invoiceDao;
31      }
32    }
33
[9619]34    public PlainTextInvoiceFormattingEngine()
[9641]35      : this(defaultTemplatePath, PlainTextInvoiceFormat.RTF) {
[9619]36
37    }
38
[9641]39    public PlainTextInvoiceFormattingEngine(string templatePath, PlainTextInvoiceFormat format) {
[9619]40      if (!File.Exists(templatePath)) {
41        throw new FileNotFoundException("Cannot find template file", templatePath);
42      }
43      template = File.ReadAllText(templatePath);
[9641]44
45      if (format == PlainTextInvoiceFormat.PDF) {
46        throw new Exception("PDF invoice format is currently not supported");
47      }
48      invoiceFormat = format;
[9619]49    }
50
[9653]51    public string Generate(Invoice invoice) {
[9619]52      invoiceText = string.Copy(template);
53
54      invoiceText = invoiceText.Replace("{INVOICE_ID}", invoice.InvoiceId.ToString());
55
[9641]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);
[9619]62
63      invoiceText = invoiceText.Replace("{INVOICE_DATE}", invoice.InvoiceDate.ToString("dd.MM.yyyy"));
64      invoiceText = invoiceText.Replace("{ORDER_ID}", invoice.OrderId.ToString());
[9641]65      invoiceText = invoiceText.Replace("{USER_NAME}", invoice.User.Name);
[9619]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
[9641]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);
[9619]76
[9641]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));
[9619]84
[9641]85        invoiceLineText = invoiceLineText.Remove(invoiceLineText.IndexOf("#BEGIN"), 7);
86        invoiceLineText = invoiceLineText.Remove(invoiceLineText.IndexOf("#END"), 4);
[9619]87
[9641]88        invoiceText = invoiceText.Insert(first, invoiceLineText);
[9619]89      }
90
[9641]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));
[9619]94
[10013]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
[9641]99      if (invoiceFormat == PlainTextInvoiceFormat.RTF) {
[10013]100        invoiceOutputPath = string.Format("{0}_{1}_{2}.rtf", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), customerName);
[9619]101
[9641]102        RichTextBox richTextBox = new RichTextBox();
103        richTextBox.Text = invoiceText;
104        richTextBox.SelectAll();
105        richTextBox.SelectionFont = new Font("Consolas", 10, FontStyle.Regular);
[9619]106
[10013]107        //File.WriteAllText(invoiceOutputPath, richTextBox.Rtf);
108        InvoiceDao.SaveDocument(invoiceOutputPath, richTextBox.Rtf);
[9641]109      } else if (invoiceFormat == PlainTextInvoiceFormat.TXT) {
[10013]110        invoiceOutputPath = string.Format("{0}_{1}_{2}.txt", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), customerName);
[9619]111
[10013]112        //File.WriteAllText(invoiceOutputPath, invoiceText);
113        InvoiceDao.SaveDocument(invoiceOutputPath, invoiceText);
[9641]114      } else if (invoiceFormat == PlainTextInvoiceFormat.PDF) {
115        throw new Exception("PDF invoice format is currently not supported");
116      }
117
[9619]118      return invoiceOutputPath;
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.