Search in sources :

Example 31 with ExternalChargeInvoiceItem

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);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

ExternalChargeInvoiceItem (org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)31 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)27 Test (org.testng.annotations.Test)26 BigDecimal (java.math.BigDecimal)24 UUID (java.util.UUID)24 Invoice (org.killbill.billing.invoice.api.Invoice)19 CreditAdjInvoiceItem (org.killbill.billing.invoice.model.CreditAdjInvoiceItem)19 TaxInvoiceItem (org.killbill.billing.invoice.model.TaxInvoiceItem)16 Account (org.killbill.billing.account.api.Account)15 LocalDate (org.joda.time.LocalDate)12 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)7 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)6 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)6 DateTime (org.joda.time.DateTime)5 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)5 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)4 HashMap (java.util.HashMap)3 AccountData (org.killbill.billing.account.api.AccountData)3 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)3 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)3