Search in sources :

Example 1 with DefaultInvoiceTranslator

use of org.killbill.billing.invoice.template.translator.DefaultInvoiceTranslator in project killbill by killbill.

the class HtmlInvoiceGenerator method generateInvoice.

public HtmlInvoice generateInvoice(final Account account, @Nullable final Invoice invoice, final boolean manualPay, final InternalTenantContext context) throws IOException {
    // Don't do anything if the invoice is null
    if (invoice == null || invoice.getNumberOfItems() == 0) {
        return null;
    }
    final String accountLocale = Strings.emptyToNull(account.getLocale());
    final Locale locale = accountLocale == null ? Locale.getDefault() : LocaleUtils.toLocale(accountLocale);
    final HtmlInvoice invoiceData = new HtmlInvoice();
    final Map<String, Object> data = new HashMap<String, Object>();
    final ResourceBundle invoiceBundle = accountLocale != null ? bundleFactory.createBundle(LocaleUtils.toLocale(accountLocale), config.getInvoiceTemplateBundlePath(), ResourceBundleType.INVOICE_TRANSLATION, context) : null;
    final ResourceBundle defaultInvoiceBundle = bundleFactory.createBundle(Locale.getDefault(), config.getInvoiceTemplateBundlePath(), ResourceBundleType.INVOICE_TRANSLATION, context);
    final DefaultInvoiceTranslator invoiceTranslator = new DefaultInvoiceTranslator(invoiceBundle, defaultInvoiceBundle);
    data.put("text", invoiceTranslator);
    data.put("account", account);
    final InvoiceFormatter formattedInvoice = factory.createInvoiceFormatter(config, invoice, locale, currencyConversionApi, bundleFactory, context);
    data.put("invoice", formattedInvoice);
    invoiceData.setSubject(invoiceTranslator.getInvoiceEmailSubject());
    final String templateText = getTemplateText(locale, manualPay, context);
    invoiceData.setBody(templateEngine.executeTemplateText(templateText, data));
    return invoiceData;
}
Also used : Locale(java.util.Locale) InvoiceFormatter(org.killbill.billing.invoice.api.formatters.InvoiceFormatter) HashMap(java.util.HashMap) DefaultInvoiceTranslator(org.killbill.billing.invoice.template.translator.DefaultInvoiceTranslator) ResourceBundle(java.util.ResourceBundle)

Example 2 with DefaultInvoiceTranslator

use of org.killbill.billing.invoice.template.translator.DefaultInvoiceTranslator in project killbill by killbill.

the class TestDefaultInvoiceFormatter method testForDisplay.

// No Assertion, just to print the html
@Test(groups = "fast")
public void testForDisplay() throws Exception {
    final FixedPriceInvoiceItem fixedItemBRL = new FixedPriceInvoiceItem(UUID.randomUUID(), UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString(), null, new LocalDate(), new BigDecimal("1499.95"), Currency.BRL);
    final Invoice invoice = new DefaultInvoice(UUID.randomUUID(), UUID.randomUUID(), new Integer(234), new LocalDate(), new LocalDate(), Currency.BRL, false, InvoiceStatus.COMMITTED);
    invoice.addInvoiceItem(fixedItemBRL);
    final String template = "<html>\n" + "    <head>\n" + "        <style type=\"text/css\">\n" + "            #header td { width: 250px; }\n" + "            #header .company_info { width: 450px; }\n" + "            #header .label { text-align: right; font-weight: bold; }\n" + "            #header .label_value { padding-left: 10px; }\n" + "\n" + "            #invoice_items { margin-top: 50px; }\n" + "            #invoice_items th { border-bottom: solid 2px black; }\n" + "            #invoice_items td { width: 250px; }\n" + "            #invoice_items td.amount { width: 125px; }\n" + "            #invoice_items td.network { width: 350px; }\n" + "            #invoice_items .amount { text-align: right; }\n" + "            #invoice_items .label { text-align: right; font-weight: bold; }\n" + "        </style>\n" + "    </head>\n" + "    <body>\n" + "        <table id=\"header\">\n" + "            <tr>\n" + "                <td class=\"company_info\"/>\n" + "                <td />\n" + "                <td><h1>{{text.invoiceTitle}}</h1></td>\n" + "            </tr>\n" + "            <tr>\n" + "                <td colspan=\"3\"><img src=\"http://static.foo.com/www/0/main/gfx/front/logo.png\"/></td>\n" + "            </tr>\n" + "            <tr>\n" + "                <td class=\"company_info\" />\n" + "                <td class=\"label\">{{text.invoiceDate}}</td>\n" + "                <td class=\"label_value\">{{invoice.formattedInvoiceDate}}</td>\n" + "            </tr>\n" + "            <tr>\n" + "                <td class=\"company_info\" />\n" + "                <td class=\"label\">{{text.invoiceNumber}}</td>\n" + "                <td class=\"label_value\">{{invoice.invoiceNumber}}</td>\n" + "            </tr>\n" + "            <tr>\n" + "                <td class=\"company_info\">{{text.companyCountry}}</td>\n" + "                <td colspan=\"2\" />\n" + "            </tr>\n" + "            <tr>\n" + "                <td class=\"company_info\">{{text.companyUrl}}</td>\n" + "                <td colspan=\"2\" />\n" + "            </tr>\n" + "        </table>\n" + "\n" + "        <table id=\"invoice_items\">\n" + "            <tr>\n" + "                <th class=\"network\">{{text.invoiceItemBundleName}}</td>\n" + "                <th>{{text.invoiceItemDescription}}</td>\n" + "                <th>{{text.invoiceItemServicePeriod}}</td>\n" + "                <th>{{text.invoiceItemAmount}}</td>\n" + "            </tr>\n" + "            {{#invoice.invoiceItems}}\n" + "            <tr>\n" + "                <td class=\"network\">{{description}}</td>\n" + "                <td>{{planName}}</td>\n" + "                <td>{{formattedStartDate}}{{#formattedEndDate}} - {{formattedEndDate}}{{/formattedEndDate}}</td>\n" + "                <td class=\"amount\">{{formattedAmount}}</td>\n" + "            </tr>\n" + "            {{/invoice.invoiceItems}}\n" + "            <tr>\n" + "                <td colspan=\"4\" height=\"50px\"></td>\n" + "            </tr>\n" + "            <tr>\n" + "                <td colspan=\"2\" />\n" + "                <td class=\"label\">{{text.invoiceAmount}}</td>\n" + "                <td class=\"amount\"><strong>{{invoice.formattedChargedAmount}}</strong></td>\n" + "            </tr>\n" + "            <tr>\n" + "                <td colspan=\"2\" />\n" + "                <td class=\"label\">{{text.invoiceAmountPaid}}" + "                    {{#invoice.processedCurrency}}" + " (*)" + "                    {{/invoice.processedCurrency}}\n" + "                </td>\n" + "                <td class=\"amount\"><strong>{{invoice.formattedPaidAmount}}</strong></td>\n" + "            </tr>\n" + "            <tr>\n" + "                <td colspan=\"2\" />\n" + "                <td class=\"label\">{{text.invoiceBalance}}</td>\n" + "                <td class=\"amount\"><strong>{{invoice.formattedBalance}}</strong></td>\n" + "            </tr>\n" + "        </table>\n" + "        {{#invoice.processedCurrency}}" + "        {{text.processedPaymentCurrency}} {{invoice.processedCurrency}}." + "        {{#invoice.processedPaymentRate}}\n" + " {{text.processedPaymentRate}} {{invoice.processedPaymentRate}}.\n" + "        {{/invoice.processedPaymentRate}}" + "        {{/invoice.processedCurrency}}" + "    </body>\n" + "</html>\n";
    final Map<String, Object> data = new HashMap<String, Object>();
    final String bundlePath = "org/killbill/billing/util/template/translation/InvoiceTranslation";
    final ResourceBundle bundle = resourceBundleFactory.createBundle(Locale.US, bundlePath, ResourceBundleType.INVOICE_TRANSLATION, internalCallContext);
    final DefaultInvoiceTranslator translator = new DefaultInvoiceTranslator(bundle, null);
    data.put("text", translator);
    data.put("invoice", new DefaultInvoiceFormatter(config, invoice, Locale.US, currencyConversionApi, resourceBundleFactory, internalCallContext));
    final String formattedText = templateEngine.executeTemplateText(template, data);
    System.out.println(formattedText);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) HashMap(java.util.HashMap) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultInvoiceTranslator(org.killbill.billing.invoice.template.translator.DefaultInvoiceTranslator) ResourceBundle(java.util.ResourceBundle) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Aggregations

HashMap (java.util.HashMap)2 ResourceBundle (java.util.ResourceBundle)2 DefaultInvoiceTranslator (org.killbill.billing.invoice.template.translator.DefaultInvoiceTranslator)2 BigDecimal (java.math.BigDecimal)1 Locale (java.util.Locale)1 LocalDate (org.joda.time.LocalDate)1 Invoice (org.killbill.billing.invoice.api.Invoice)1 InvoiceFormatter (org.killbill.billing.invoice.api.formatters.InvoiceFormatter)1 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)1 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)1 Test (org.testng.annotations.Test)1