Search in sources :

Example 1 with InvoiceFormatter

use of org.killbill.billing.invoice.api.formatters.InvoiceFormatter in project killbill by killbill.

the class TestDefaultInvoiceFormatter method testIgnoreZeroAdjustments.

@Test(groups = "fast")
public void testIgnoreZeroAdjustments() throws Exception {
    // Scenario: single item with payment
    // * $10 item
    // * $-10 CBA
    // * $10 CBA
    final FixedPriceInvoiceItem fixedItem = new FixedPriceInvoiceItem(UUID.randomUUID(), UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), new LocalDate(), BigDecimal.TEN, Currency.USD);
    final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem = new CreditBalanceAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), fixedItem.getAmount(), fixedItem.getCurrency());
    final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem2 = new CreditBalanceAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), fixedItem.getAmount().negate(), fixedItem.getCurrency());
    final Invoice invoice = new DefaultInvoice(fixedItem.getInvoiceId(), fixedItem.getAccountId(), null, new LocalDate(), new LocalDate(), Currency.USD, false, InvoiceStatus.COMMITTED);
    invoice.addInvoiceItem(fixedItem);
    invoice.addInvoiceItem(creditBalanceAdjInvoiceItem);
    invoice.addInvoiceItem(creditBalanceAdjInvoiceItem2);
    // Check the scenario
    Assert.assertEquals(invoice.getBalance().doubleValue(), 10.00);
    Assert.assertEquals(invoice.getCreditedAmount().doubleValue(), 0.00);
    // Verify the merge
    final InvoiceFormatter formatter = new DefaultInvoiceFormatter(config, invoice, Locale.US, null, resourceBundleFactory, internalCallContext);
    final List<InvoiceItem> invoiceItems = formatter.getInvoiceItems();
    Assert.assertEquals(invoiceItems.size(), 1);
    Assert.assertEquals(invoiceItems.get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
    Assert.assertEquals(invoiceItems.get(0).getAmount().doubleValue(), 10.00);
}
Also used : InvoiceFormatter(org.killbill.billing.invoice.api.formatters.InvoiceFormatter) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) LocalDate(org.joda.time.LocalDate) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 2 with InvoiceFormatter

use of org.killbill.billing.invoice.api.formatters.InvoiceFormatter 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) {
        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 3 with InvoiceFormatter

use of org.killbill.billing.invoice.api.formatters.InvoiceFormatter in project killbill by killbill.

the class TestDefaultInvoiceFormatter method testMergeItems.

@Test(groups = "fast")
public void testMergeItems() throws Exception {
    // Scenario: single item with payment
    // * $10 item
    // Then, a repair occur:
    // * $-10 repair
    // * $10 generated CBA due to the repair (assume previous payment)
    // Then, the invoice is adjusted for $1:
    // * $-1 credit adjustment
    // * $1 generated CBA due to the credit adjustment
    final FixedPriceInvoiceItem fixedItem = new FixedPriceInvoiceItem(UUID.randomUUID(), UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), new LocalDate(), BigDecimal.TEN, Currency.USD);
    final RepairAdjInvoiceItem repairAdjInvoiceItem = new RepairAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), fixedItem.getEndDate(), fixedItem.getAmount().negate(), fixedItem.getCurrency(), fixedItem.getId());
    final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem = new CreditBalanceAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), fixedItem.getAmount(), fixedItem.getCurrency());
    final CreditAdjInvoiceItem creditAdjInvoiceItem = new CreditAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), null, BigDecimal.ONE.negate(), fixedItem.getCurrency());
    final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem2 = new CreditBalanceAdjInvoiceItem(fixedItem.getInvoiceId(), fixedItem.getAccountId(), fixedItem.getStartDate(), creditAdjInvoiceItem.getAmount().negate(), fixedItem.getCurrency());
    final DefaultInvoice invoice = new DefaultInvoice(fixedItem.getInvoiceId(), fixedItem.getAccountId(), null, new LocalDate(), new LocalDate(), Currency.USD, false, InvoiceStatus.COMMITTED);
    invoice.addInvoiceItem(fixedItem);
    invoice.addInvoiceItem(repairAdjInvoiceItem);
    invoice.addInvoiceItem(creditBalanceAdjInvoiceItem);
    invoice.addInvoiceItem(creditAdjInvoiceItem);
    invoice.addInvoiceItem(creditBalanceAdjInvoiceItem2);
    invoice.addPayment(new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice.getId(), clock.getUTCNow(), BigDecimal.TEN, Currency.USD, Currency.USD, null, true));
    invoice.addPayment(new DefaultInvoicePayment(InvoicePaymentType.REFUND, UUID.randomUUID(), invoice.getId(), clock.getUTCNow(), BigDecimal.ONE.negate(), Currency.USD, Currency.USD, null, true));
    // Check the scenario
    Assert.assertEquals(invoice.getBalance().doubleValue(), 1.00);
    Assert.assertEquals(invoice.getCreditedAmount().doubleValue(), 11.00);
    Assert.assertEquals(invoice.getRefundedAmount().doubleValue(), -1.00);
    // Verify the merge
    final InvoiceFormatter formatter = new DefaultInvoiceFormatter(config, invoice, Locale.US, null, resourceBundleFactory, internalCallContext);
    final List<InvoiceItem> invoiceItems = formatter.getInvoiceItems();
    Assert.assertEquals(invoiceItems.size(), 4);
    Assert.assertEquals(invoiceItems.get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
    Assert.assertEquals(invoiceItems.get(0).getAmount().doubleValue(), 10.00);
    Assert.assertEquals(invoiceItems.get(1).getInvoiceItemType(), InvoiceItemType.REPAIR_ADJ);
    Assert.assertEquals(invoiceItems.get(1).getAmount().doubleValue(), -10.00);
    Assert.assertEquals(invoiceItems.get(2).getInvoiceItemType(), InvoiceItemType.CBA_ADJ);
    Assert.assertEquals(invoiceItems.get(2).getAmount().doubleValue(), 11.00);
    Assert.assertEquals(invoiceItems.get(3).getInvoiceItemType(), InvoiceItemType.CREDIT_ADJ);
    Assert.assertEquals(invoiceItems.get(3).getAmount().doubleValue(), -1.00);
}
Also used : InvoiceFormatter(org.killbill.billing.invoice.api.formatters.InvoiceFormatter) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) LocalDate(org.joda.time.LocalDate) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Test(org.testng.annotations.Test)

Aggregations

InvoiceFormatter (org.killbill.billing.invoice.api.formatters.InvoiceFormatter)3 LocalDate (org.joda.time.LocalDate)2 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)2 CreditAdjInvoiceItem (org.killbill.billing.invoice.model.CreditAdjInvoiceItem)2 CreditBalanceAdjInvoiceItem (org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem)2 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)2 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)2 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)2 Test (org.testng.annotations.Test)2 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 ResourceBundle (java.util.ResourceBundle)1 Invoice (org.killbill.billing.invoice.api.Invoice)1 DefaultInvoicePayment (org.killbill.billing.invoice.model.DefaultInvoicePayment)1 DefaultInvoiceTranslator (org.killbill.billing.invoice.template.translator.DefaultInvoiceTranslator)1