Search in sources :

Example 6 with ExternalChargeInvoiceItem

use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.

the class TestWithTaxItems method testUpdateTaxItems.

@Test(groups = "slow")
public void testUpdateTaxItems() throws Exception {
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    // Create original subscription (Trial PHASE) -> $0 invoice.
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    // Add tags to keep DRAFT invoices and reuse them
    add_AUTO_INVOICING_DRAFT_Tag(account.getId(), ObjectType.ACCOUNT);
    add_AUTO_INVOICING_REUSE_DRAFT_Tag(account.getId(), ObjectType.ACCOUNT);
    // Make sure TestInvoicePluginApi will return an additional TAX item
    final UUID invoiceTaxItemId = UUID.randomUUID();
    testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(invoiceTaxItemId, null, account.getId(), null, "Tax Item", new LocalDate(2012, 4, 1), BigDecimal.ONE, account.getCurrency()));
    // Insert external charge autoCommit = false => Invoice will be in DRAFT
    invoiceUserApi.insertExternalCharges(account.getId(), clock.getUTCNow().toLocalDate(), ImmutableList.<InvoiceItem>of(new ExternalChargeInvoiceItem(null, account.getId(), null, "foo", new LocalDate(2012, 4, 1), null, new BigDecimal("33.80"), account.getCurrency(), null)), false, null, callContext);
    // Make sure TestInvoicePluginApi **update** the original TAX item
    testInvoicePluginApi.addTaxItem(new TaxInvoiceItem(invoiceTaxItemId, null, account.getId(), null, "Tax Item", new LocalDate(2012, 4, 1), new BigDecimal("12.45"), account.getCurrency()));
    // Move to Evergreen PHASE, but invoice remains in DRAFT mode
    busHandler.pushExpectedEvents(NextEvent.PHASE);
    clock.addDays(30);
    assertListenerStatus();
    final List<Invoice> accountInvoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    assertEquals(accountInvoices.size(), 2);
    // Commit invoice
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
    invoiceUserApi.commitInvoice(accountInvoices.get(1).getId(), callContext);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.EXTERNAL_CHARGE, new BigDecimal("33.80")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, new BigDecimal("29.95")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.TAX, new BigDecimal("12.45")));
}
Also used : Account(org.killbill.billing.account.api.Account) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) UUID(java.util.UUID) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 7 with ExternalChargeInvoiceItem

use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.

the class TestInvoiceDao method testExternalChargeWithCBA.

@Test(groups = "slow")
public void testExternalChargeWithCBA() throws InvoiceApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final InvoiceItemModelDao credit = createCredit(accountId, clock.getUTCToday(), new BigDecimal("20.0"), true);
    final String description = UUID.randomUUID().toString();
    final InvoiceModelDao invoiceForExternalCharge = new InvoiceModelDao(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD, false);
    final InvoiceItemModelDao externalCharge = new InvoiceItemModelDao(new ExternalChargeInvoiceItem(invoiceForExternalCharge.getId(), accountId, bundleId, description, clock.getUTCToday(), clock.getUTCToday(), new BigDecimal("15.0"), Currency.USD, null));
    invoiceForExternalCharge.addInvoiceItem(externalCharge);
    final InvoiceItemModelDao charge = invoiceDao.createInvoices(ImmutableList.<InvoiceModelDao>of(invoiceForExternalCharge), null, ImmutableSet.of(), context).get(0);
    InvoiceModelDao newInvoice = invoiceDao.getById(charge.getInvoiceId(), context);
    List<InvoiceItemModelDao> items = newInvoice.getInvoiceItems();
    // No CBA consumed yet since the credit was created on a DRAFT invoice
    assertEquals(items.size(), 1);
    assertEquals(items.get(0).getType(), InvoiceItemType.EXTERNAL_CHARGE);
    assertEquals(items.get(0).getDescription(), description);
    invoiceDao.changeInvoiceStatus(credit.getInvoiceId(), InvoiceStatus.COMMITTED, context);
    // CBA should have been consumed
    newInvoice = invoiceDao.getById(charge.getInvoiceId(), context);
    items = newInvoice.getInvoiceItems();
    assertEquals(items.size(), 2);
    for (final InvoiceItemModelDao cur : items) {
        if (cur.getId().equals(charge.getId())) {
            assertEquals(cur.getType(), InvoiceItemType.EXTERNAL_CHARGE);
            assertEquals(cur.getDescription(), description);
        } else {
            assertEquals(cur.getType(), InvoiceItemType.CBA_ADJ);
            assertTrue(cur.getAmount().compareTo(new BigDecimal("-15.00")) == 0);
        }
    }
}
Also used : UUID(java.util.UUID) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 8 with ExternalChargeInvoiceItem

use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.

the class TestDefaultInvoiceUserApi method testPostExternalChargeForBundleOnNewInvoice.

@Test(groups = "slow")
public void testPostExternalChargeForBundleOnNewInvoice() 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 UUID bundleId = UUID.randomUUID();
    final InvoiceItem externalCharge = new ExternalChargeInvoiceItem(null, accountId, bundleId, UUID.randomUUID().toString(), 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, bundleId, externalChargeAmount, externalChargeInvoiceItem);
}
Also used : Account(org.killbill.billing.account.api.Account) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) UUID(java.util.UUID) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 9 with ExternalChargeInvoiceItem

use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.

the class TestDefaultInvoiceUserApi method testVoidInvoiceWithUsedCredits.

@Test(groups = "slow")
public void testVoidInvoiceWithUsedCredits() throws Exception {
    final Account account = invoiceUtil.createAccount(callContext);
    final UUID accountId = account.getId();
    final InvoiceItem creditItem = new CreditAdjInvoiceItem(null, accountId, clock.getUTCToday(), "something", new BigDecimal("200.00"), accountCurrency, null);
    invoiceUserApi.insertCredits(accountId, clock.getUTCToday(), ImmutableList.of(creditItem), true, null, callContext);
    final InvoiceItem externalCharge1 = new ExternalChargeInvoiceItem(null, accountId, null, "description", clock.getUTCToday(), clock.getUTCToday(), new BigDecimal("100.00"), accountCurrency, null);
    final List<InvoiceItem> items1 = invoiceUserApi.insertExternalCharges(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(externalCharge1), true, null, callContext);
    assertEquals(items1.size(), 1);
    final Invoice invoice1 = invoiceUserApi.getInvoice(items1.get(0).getInvoiceId(), callContext);
    final InvoiceItem externalCharge2 = new ExternalChargeInvoiceItem(null, accountId, null, "description", clock.getUTCToday(), clock.getUTCToday(), new BigDecimal("50.00"), accountCurrency, null);
    final List<InvoiceItem> items2 = invoiceUserApi.insertExternalCharges(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(externalCharge2), true, null, callContext);
    assertEquals(items2.size(), 1);
    final Invoice invoice2 = invoiceUserApi.getInvoice(items2.get(0).getInvoiceId(), callContext);
    final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(accountId, callContext);
    final BigDecimal accountCBA = invoiceUserApi.getAccountCBA(accountId, callContext);
    Assert.assertEquals(accountBalance.compareTo(new BigDecimal("-50")), 0);
    Assert.assertEquals(accountCBA.compareTo(new BigDecimal("50")), 0);
    invoiceUserApi.voidInvoice(invoice1.getId(), callContext);
    // We verify that credit *used* on invoice1 is reclaimed, i.e the system ignores it as the invoice was voided.
    final BigDecimal accountBalance2 = invoiceUserApi.getAccountBalance(accountId, callContext);
    final BigDecimal accountCBA2 = invoiceUserApi.getAccountCBA(accountId, callContext);
    Assert.assertEquals(accountBalance2.compareTo(new BigDecimal("-150")), 0);
    Assert.assertEquals(accountCBA2.compareTo(new BigDecimal("150")), 0);
}
Also used : Account(org.killbill.billing.account.api.Account) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) 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) UUID(java.util.UUID) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 10 with ExternalChargeInvoiceItem

use of org.killbill.billing.invoice.model.ExternalChargeInvoiceItem in project killbill by killbill.

the class TestDefaultInvoiceUserApi method testOriginalAmountCharged.

@Test(groups = "slow")
public void testOriginalAmountCharged() throws Exception {
    final Account account = invoiceUtil.createAccount(callContext);
    final UUID accountId = account.getId();
    final UUID invoiceId = invoiceUtil.generateRegularInvoice(account, null, callContext);
    // Post an external charge
    final BigDecimal externalChargeAmount = BigDecimal.TEN;
    final InvoiceItem externalCharge = new ExternalChargeInvoiceItem(null, accountId, null, UUID.randomUUID().toString(), clock.getUTCToday(), null, externalChargeAmount, accountCurrency, null);
    final InvoiceItem externalChargeInvoiceItem = invoiceUserApi.insertExternalCharges(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(externalCharge), true, null, callContext).get(0);
    final Invoice newInvoice = invoiceUserApi.getInvoice(externalChargeInvoiceItem.getInvoiceId(), callContext);
    final BigDecimal newAmountCharged = newInvoice.getChargedAmount();
    Assert.assertEquals(newInvoice.getOriginalChargedAmount().compareTo(externalChargeAmount), 0);
    Assert.assertEquals(newAmountCharged.compareTo(externalChargeAmount), 0);
}
Also used : Account(org.killbill.billing.account.api.Account) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) 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) UUID(java.util.UUID) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) 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