use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.
the class TestInvoiceFlagBehaviors method testWrittenOffInvoiceBeforeAccountCredit.
@Test(groups = "slow", description = "Verify invoice/account balance with a WRITTEN_OFF invoice. Verify account credit is not used against such invoice")
public void testWrittenOffInvoiceBeforeAccountCredit() throws Exception {
// Create new invoice with one charge and expect account credit to be used
final List<InvoiceItem> items = invoiceUserApi.insertExternalCharges(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(new ExternalChargeInvoiceItem(UUID.randomUUID(), clock.getUTCNow(), null, accountId, null, null, null, null, BigDecimal.TEN, accountCurrency, null)), true, null, callContext);
assertEquals(items.size(), 1);
// Check both invoice and account balance is 10.00
final UUID invoiceId = items.get(0).getInvoiceId();
final Invoice invoice0 = invoiceUserApi.getInvoice(invoiceId, callContext);
assertEquals(invoice0.getBalance().compareTo(BigDecimal.TEN), 0);
final BigDecimal accountBalance0 = invoiceUserApi.getAccountBalance(accountId, callContext);
assertEquals(accountBalance0.compareTo(BigDecimal.TEN), 0);
final BigDecimal accountCBA0 = invoiceUserApi.getAccountCBA(accountId, callContext);
assertEquals(accountCBA0.compareTo(BigDecimal.ZERO), 0);
// Tag invoice with WRITTEN_OFF and expect balance to now show as Zero
tagUserApi.addTag(invoiceId, ObjectType.INVOICE, ControlTagType.WRITTEN_OFF.getId(), callContext);
// Check both invoice and account balance is NOW 0
final Invoice invoice1 = invoiceUserApi.getInvoice(invoiceId, callContext);
assertEquals(invoice1.getBalance().compareTo(BigDecimal.ZERO), 0);
final BigDecimal accountBalance1 = invoiceUserApi.getAccountBalance(accountId, callContext);
assertEquals(accountBalance1.compareTo(BigDecimal.ZERO), 0);
final BigDecimal accountCBA1 = invoiceUserApi.getAccountCBA(accountId, callContext);
assertEquals(accountCBA1.compareTo(BigDecimal.ZERO), 0);
// Add credit on the account
final InvoiceItem inputCredit = new CreditAdjInvoiceItem(null, accountId, clock.getUTCToday(), "some description", BigDecimal.TEN, accountCurrency, null);
invoiceUserApi.insertCredits(accountId, clock.getUTCToday(), ImmutableList.of(inputCredit), true, null, callContext);
final Invoice invoice2 = invoiceUserApi.getInvoice(invoiceId, callContext);
assertEquals(invoice2.getBalance().compareTo(BigDecimal.ZERO), 0);
assertEquals(invoice2.getInvoiceItems().size(), 1);
final BigDecimal accountBalance2 = invoiceUserApi.getAccountBalance(accountId, callContext);
assertEquals(accountBalance2.compareTo(new BigDecimal("-10.00")), 0);
final BigDecimal accountCBA2 = invoiceUserApi.getAccountCBA(accountId, callContext);
assertEquals(accountCBA2.compareTo(BigDecimal.TEN), 0);
}
Aggregations