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);
}
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;
}
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);
}
Aggregations