use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.
the class TestDefaultInvoiceUserApi method testCommitInvoice.
@Test(groups = "slow")
public void testCommitInvoice() throws Exception {
final Account account = invoiceUtil.createAccount(callContext);
final UUID accountId = account.getId();
final UUID invoiceId = invoiceUtil.generateRegularInvoice(account, null, callContext);
// Verify the initial invoice balance
final BigDecimal invoiceBalance = invoiceUserApi.getInvoice(invoiceId, callContext).getBalance();
Assert.assertEquals(invoiceBalance.compareTo(BigDecimal.ZERO), 1);
// Verify the initial account balance
final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(accountId, callContext);
Assert.assertEquals(accountBalance, invoiceBalance);
// Adjust the invoice for the full amount
final BigDecimal creditAmount = BigDecimal.TEN;
final InvoiceItem inputCredit = new CreditAdjInvoiceItem(null, accountId, clock.getUTCToday(), "some description", creditAmount, accountCurrency, null);
final List<InvoiceItem> creditInvoiceItems = invoiceUserApi.insertCredits(accountId, clock.getUTCToday(), ImmutableList.of(inputCredit), false, null, callContext);
Assert.assertEquals(creditInvoiceItems.size(), 1);
final InvoiceItem creditInvoiceItem = creditInvoiceItems.get(0);
final UUID invoiceId2 = creditInvoiceItem.getInvoiceId();
Invoice creditInvoice = invoiceUserApi.getInvoice(invoiceId2, callContext);
Assert.assertEquals(creditInvoice.getStatus(), InvoiceStatus.DRAFT);
Assert.assertEquals(creditInvoiceItem.getInvoiceId(), creditInvoice.getId());
// Verify DRAFT invoice is not taken into consideration when computing accountBalance
final BigDecimal accountBalance2 = invoiceUserApi.getAccountBalance(accountId, callContext);
Assert.assertEquals(accountBalance2, accountBalance);
// move invoice from DRAFT to COMMITTED
invoiceUserApi.commitInvoice(creditInvoice.getId(), callContext);
creditInvoice = invoiceUserApi.getInvoice(invoiceId2, callContext);
Assert.assertEquals(creditInvoice.getStatus(), InvoiceStatus.COMMITTED);
try {
final InvoiceItem externalCharge = new ExternalChargeInvoiceItem(invoiceId2, accountId, null, "Initial external charge", clock.getUTCToday(), null, new BigDecimal("12.33"), accountCurrency, null);
invoiceUserApi.insertExternalCharges(accountId, clock.getUTCToday(), ImmutableList.of(externalCharge), true, null, callContext);
Assert.fail("Should fail to add external charge on already committed invoice");
} catch (final InvoiceApiException e) {
Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_ALREADY_COMMITTED.getCode());
}
try {
final InvoiceItem inputCreditForInvoice = new CreditAdjInvoiceItem(invoiceId2, accountId, clock.getUTCToday(), "some description", creditAmount, accountCurrency, null);
invoiceUserApi.insertCredits(accountId, clock.getUTCToday(), ImmutableList.of(inputCreditForInvoice), false, null, callContext);
Assert.fail("Should fail to add credit on already committed invoice");
} catch (final InvoiceApiException e) {
Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_ALREADY_COMMITTED.getCode());
}
}
use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.
the class TestDefaultInvoiceUserApi method testPostExternalChargeOnNewInvoice.
@Test(groups = "slow")
public void testPostExternalChargeOnNewInvoice() throws Exception {
final Account account = invoiceUtil.createAccount(callContext);
final UUID accountId = account.getId();
final UUID invoiceId = invoiceUtil.generateRegularInvoice(account, null, callContext);
// Initial account balance
final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(accountId, callContext);
// Post an external charge
final BigDecimal externalChargeAmount = BigDecimal.TEN;
final InvoiceItem externalCharge = new ExternalChargeInvoiceItem(null, accountId, null, "description", clock.getUTCToday(), clock.getUTCToday(), externalChargeAmount, accountCurrency, null);
final InvoiceItem externalChargeInvoiceItem = invoiceUserApi.insertExternalCharges(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(externalCharge), true, null, callContext).get(0);
verifyExternalChargeOnNewInvoice(accountId, invoiceId, accountBalance, null, externalChargeAmount, externalChargeInvoiceItem);
assertEquals(externalChargeInvoiceItem.getDescription(), "description");
}
use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.
the class TestDefaultInvoiceUserApi method testVoidInvoice.
@Test(groups = "slow")
public void testVoidInvoice() throws Exception {
final Account account = invoiceUtil.createAccount(callContext);
final UUID accountId = account.getId();
final UUID invoiceId = invoiceUtil.generateRegularInvoice(account, null, callContext);
// Void invoice
invoiceUserApi.voidInvoice(invoiceId, callContext);
final Invoice invoice = invoiceUserApi.getInvoice(invoiceId, callContext);
Assert.assertEquals(invoice.getStatus(), InvoiceStatus.VOID);
// Check we cannot add items on a VOIDed invoice
try {
final InvoiceItem creditItem = new CreditAdjInvoiceItem(invoiceId, accountId, clock.getUTCToday(), "something", BigDecimal.TEN, accountCurrency, null);
invoiceUserApi.insertCredits(accountId, clock.getUTCToday(), ImmutableList.of(creditItem), true, null, callContext);
Assert.fail("Should not allow to add items to a VOIDed invoice");
} catch (final Exception ignore) {
// No check because of https://github.com/killbill/killbill/issues/1501
}
try {
final InvoiceItem externalCharge = new ExternalChargeInvoiceItem(invoiceId, accountId, null, "description", clock.getUTCToday(), clock.getUTCToday(), BigDecimal.TEN, accountCurrency, null);
invoiceUserApi.insertExternalCharges(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(externalCharge), true, null, callContext).get(0);
Assert.fail("Should not allow to add items to a VOIDed invoice");
} catch (final IllegalStateException ignore) {
// No check because of https://github.com/killbill/killbill/issues/1501
}
try {
final InvoiceItem originalItem = invoice.getInvoiceItems().get(0);
invoiceUserApi.insertInvoiceItemAdjustment(accountId, invoiceId, originalItem.getId(), clock.getUTCToday(), null, null, null, callContext);
Assert.fail("Should not allow to add items to a VOIDed invoice");
} catch (final Exception ignore) {
// No check because of https://github.com/killbill/killbill/issues/1501
}
}
use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.
the class TestDefaultInvoiceUserApi method testCreditOnDraftInvoice.
@Test(groups = "slow")
public void testCreditOnDraftInvoice() throws Exception {
final Account account = invoiceUtil.createAccount(callContext);
final UUID accountId = account.getId();
// Create external charge
final BigDecimal externalChargeAmount = BigDecimal.TEN;
final InvoiceItem externalCharge = new ExternalChargeInvoiceItem(null, accountId, null, UUID.randomUUID().toString(), clock.getUTCToday(), null, externalChargeAmount, accountCurrency, null);
// Create invoice in draft status with external charge
final InvoiceItem externalChargeInvoiceItem = invoiceUserApi.insertExternalCharges(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(externalCharge), false, null, callContext).get(0);
final Invoice externalChargeInvoice = invoiceUserApi.getInvoice(externalChargeInvoiceItem.getInvoiceId(), callContext);
Assert.assertEquals(externalChargeInvoice.getChargedAmount().compareTo(externalChargeAmount), 0);
// invoice is in DRAFT status, so balance is 0
Assert.assertEquals(externalChargeInvoice.getBalance().compareTo(BigDecimal.ZERO), 0);
// Post a credit on draft invoice
final BigDecimal creditAmount = new BigDecimal(3);
final InvoiceItem credit = new CreditAdjInvoiceItem(externalChargeInvoice.getId(), accountId, clock.getUTCToday(), "Adding credit", creditAmount, accountCurrency, null);
invoiceUserApi.insertCredits(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(credit), true, null, callContext);
final Invoice updatedExternalChargeInvoice = invoiceUserApi.getInvoice(externalChargeInvoice.getId(), callContext);
// credit is adjusted in the amountCharged since invoice is in DRAFT status
Assert.assertEquals(updatedExternalChargeInvoice.getChargedAmount().compareTo(new BigDecimal(7)), 0);
// invoice is in DRAFT status, so balance is 0
Assert.assertEquals(updatedExternalChargeInvoice.getBalance().compareTo(BigDecimal.ZERO), 0);
// commit invoice
invoiceUserApi.commitInvoice(externalChargeInvoice.getId(), callContext);
final Invoice committedInvoice = invoiceUserApi.getInvoice(externalChargeInvoice.getId(), callContext);
Assert.assertEquals(committedInvoice.getChargedAmount().compareTo(new BigDecimal(7)), 0);
// invoice is committed so balance is now 7
Assert.assertEquals(committedInvoice.getBalance().compareTo(new BigDecimal(7)), 0);
}
use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.
the class TestInvoiceFlagBehaviors method testWrittenOffInvoiceWithAccountCredit.
@Test(groups = "slow", description = "Verify invoice/account balance with a WRITTEN_OFF invoice. Verify behavior when WRITTEN_OFF tag is added after credit was added to invoice")
public void testWrittenOffInvoiceWithAccountCredit() throws Exception {
// 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 BigDecimal accountBalance0 = invoiceUserApi.getAccountBalance(accountId, callContext);
assertEquals(accountBalance0.compareTo(new BigDecimal("-10.0")), 0);
final BigDecimal accountCBA0 = invoiceUserApi.getAccountCBA(accountId, callContext);
assertEquals(accountCBA0.compareTo(BigDecimal.TEN), 0);
// 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, new BigDecimal("13.5"), accountCurrency, null)), true, null, callContext);
assertEquals(items.size(), 1);
final BigDecimal accountBalance1 = invoiceUserApi.getAccountBalance(accountId, callContext);
assertEquals(accountBalance1.compareTo(new BigDecimal("3.5")), 0);
final BigDecimal accountCBA1 = invoiceUserApi.getAccountCBA(accountId, callContext);
assertEquals(accountCBA1.compareTo(BigDecimal.ZERO), 0);
// Tag invoice with WRITTEN_OFF and expect balance to now show as Zero
tagUserApi.addTag(items.get(0).getInvoiceId(), ObjectType.INVOICE, ControlTagType.WRITTEN_OFF.getId(), callContext);
final BigDecimal accountBalance2 = invoiceUserApi.getAccountBalance(accountId, callContext);
assertEquals(accountBalance2.compareTo(BigDecimal.ZERO), 0);
final BigDecimal accountCBA2 = invoiceUserApi.getAccountCBA(accountId, callContext);
assertEquals(accountCBA2.compareTo(BigDecimal.ZERO), 0);
}
Aggregations