Search in sources :

Example 21 with ExternalChargeInvoiceItem

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

the class TestInvoiceItemDao method testExternalChargeInvoiceSqlDao.

@Test(groups = "slow")
public void testExternalChargeInvoiceSqlDao() throws Exception {
    final UUID invoiceId = UUID.randomUUID();
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final String description = UUID.randomUUID().toString();
    final LocalDate startDate = new LocalDate(2012, 4, 1);
    final LocalDate endDate = new LocalDate(2012, 5, 1);
    final InvoiceItem externalChargeInvoiceItem = new ExternalChargeInvoiceItem(invoiceId, accountId, bundleId, description, startDate, endDate, TEN, Currency.USD, null);
    invoiceUtil.createInvoiceItem(externalChargeInvoiceItem, context);
    final InvoiceItemModelDao savedItem = invoiceUtil.getInvoiceItemById(externalChargeInvoiceItem.getId(), context);
    assertSameInvoiceItem(externalChargeInvoiceItem, savedItem);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) UUID(java.util.UUID) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 22 with ExternalChargeInvoiceItem

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

the class TestInvoiceItemDao method createAndVerifyExternalCharge.

private void createAndVerifyExternalCharge(final BigDecimal amount, final Currency currency) throws EntityPersistenceException {
    final InvoiceItem externalChargeInvoiceItem = new ExternalChargeInvoiceItem(UUID.randomUUID(), account.getId(), UUID.randomUUID(), UUID.randomUUID().toString(), new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), amount, currency, null);
    invoiceUtil.createInvoiceItem(externalChargeInvoiceItem, context);
    final InvoiceItemModelDao savedItem = invoiceUtil.getInvoiceItemById(externalChargeInvoiceItem.getId(), context);
    assertSameInvoiceItem(externalChargeInvoiceItem, savedItem);
    Assert.assertEquals(externalChargeInvoiceItem.getAmount().compareTo(amount), 0);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) TaxInvoiceItem(org.killbill.billing.invoice.model.TaxInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) LocalDate(org.joda.time.LocalDate)

Example 23 with ExternalChargeInvoiceItem

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

the class TestDefaultInvoiceUserApi method testPostExternalChargeForBundle.

@Test(groups = "slow")
public void testPostExternalChargeForBundle() 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 UUID bundleId = UUID.randomUUID();
    final InvoiceItem externalCharge = new ExternalChargeInvoiceItem(null, accountId, bundleId, 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);
    Assert.assertEquals(externalChargeInvoiceItem.getBundleId(), bundleId);
}
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 24 with ExternalChargeInvoiceItem

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

the class TestDefaultInvoiceUserApi method testDeleteUserGeneratedCredit.

@Test(groups = "slow")
public void testDeleteUserGeneratedCredit() throws Exception {
    final Account account = invoiceUtil.createAccount(callContext);
    final UUID accountId = account.getId();
    // Invoice created in DRAFT state so we can add credit items on top of it
    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), false, null, callContext);
    assertEquals(items1.size(), 1);
    final Invoice invoice1 = invoiceUserApi.getInvoice(items1.get(0).getInvoiceId(), callContext);
    final InvoiceItem creditItemInput = new CreditAdjInvoiceItem(invoice1.getId(), accountId, clock.getUTCToday(), "something", new BigDecimal("200.00"), accountCurrency, null);
    final List<InvoiceItem> itemCredits = invoiceUserApi.insertCredits(accountId, clock.getUTCToday(), ImmutableList.of(creditItemInput), true, null, callContext);
    final InvoiceItem itemCredit = itemCredits.get(0);
    // No credit as invoice is still in DRAFT
    final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(accountId, callContext);
    final BigDecimal accountCBA = invoiceUserApi.getAccountCBA(accountId, callContext);
    Assert.assertEquals(accountBalance.compareTo(BigDecimal.ZERO), 0);
    Assert.assertEquals(accountCBA.compareTo(BigDecimal.ZERO), 0);
    // Create new invoice (COMMITTED)
    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);
    // Verify balance and credit only reflect invoice2
    final BigDecimal accountBalance2 = invoiceUserApi.getAccountBalance(accountId, callContext);
    final BigDecimal accountCBA2 = invoiceUserApi.getAccountCBA(accountId, callContext);
    Assert.assertEquals(accountBalance2.compareTo(new BigDecimal("50")), 0);
    Assert.assertEquals(accountCBA2.compareTo(BigDecimal.ZERO), 0);
    // Commit invoice 1
    invoiceUserApi.commitInvoice(invoice1.getId(), callContext);
    final BigDecimal accountBalance3 = invoiceUserApi.getAccountBalance(accountId, callContext);
    final BigDecimal accountCBA3 = invoiceUserApi.getAccountCBA(accountId, callContext);
    Assert.assertEquals(accountBalance3.compareTo(new BigDecimal("-50")), 0);
    Assert.assertEquals(accountCBA3.compareTo(new BigDecimal("50")), 0);
    // Void invoice1 where we initially generated the credit -> fails because there was credit generated on it
    try {
        invoiceUserApi.voidInvoice(invoice1.getId(), callContext);
        Assert.fail("Should not allow to void invoice with credit");
    } catch (final Exception ignore) {
    // No check because of  https://github.com/killbill/killbill/issues/1501
    }
    Invoice latestInvoice1 = invoiceUserApi.getInvoice(items1.get(0).getInvoiceId(), callContext);
    final InvoiceItem creditGenItem = Iterables.tryFind(latestInvoice1.getInvoiceItems(), new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem invoiceItem) {
            return InvoiceItemType.CBA_ADJ == invoiceItem.getInvoiceItemType() && invoiceItem.getAmount().compareTo(BigDecimal.ZERO) > 0;
        }
    }).get();
    // Delete the credit generation
    invoiceUserApi.deleteCBA(accountId, invoice1.getId(), creditGenItem.getId(), callContext);
    latestInvoice1 = invoiceUserApi.getInvoice(items1.get(0).getInvoiceId(), callContext);
    Assert.assertEquals(latestInvoice1.getBalance().compareTo(BigDecimal.ZERO), 0);
    Assert.assertEquals(latestInvoice1.getInvoiceItems().size(), 3);
    Assert.assertNotNull(Iterables.tryFind(latestInvoice1.getInvoiceItems(), new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem invoiceItem) {
            return InvoiceItemType.CBA_ADJ == invoiceItem.getInvoiceItemType() && invoiceItem.getAmount().compareTo(BigDecimal.ZERO) == 0;
        }
    }).orNull());
    Assert.assertNotNull(Iterables.tryFind(latestInvoice1.getInvoiceItems(), new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem invoiceItem) {
            return InvoiceItemType.CREDIT_ADJ == invoiceItem.getInvoiceItemType() && // Original credit amount - CBA gen (we just deleted)
            invoiceItem.getAmount().compareTo(new BigDecimal("-100.00")) == 0;
        }
    }).orNull());
    final BigDecimal accountBalance4 = invoiceUserApi.getAccountBalance(accountId, callContext);
    final BigDecimal accountCBA4 = invoiceUserApi.getAccountCBA(accountId, callContext);
    Assert.assertEquals(accountBalance4.compareTo(new BigDecimal("50.0")), 0);
    Assert.assertEquals(accountCBA4.compareTo(BigDecimal.ZERO), 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) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Predicate(com.google.common.base.Predicate) Test(org.testng.annotations.Test)

Example 25 with ExternalChargeInvoiceItem

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

the class TestInvoiceDao method testSimpleInvoiceRun.

@Test(groups = "slow")
public void testSimpleInvoiceRun() throws Exception {
    final UUID accountId = account.getId();
    final InvoiceModelDao invoiceForExternalCharge = new InvoiceModelDao(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD, false);
    final InvoiceItemModelDao externalCharge1 = new InvoiceItemModelDao(new ExternalChargeInvoiceItem(invoiceForExternalCharge.getId(), accountId, UUID.randomUUID(), UUID.randomUUID().toString(), clock.getUTCToday(), clock.getUTCToday(), new BigDecimal("15.0"), Currency.USD, null));
    final InvoiceItemModelDao externalCharge2 = new InvoiceItemModelDao(new ExternalChargeInvoiceItem(invoiceForExternalCharge.getId(), accountId, UUID.randomUUID(), UUID.randomUUID().toString(), clock.getUTCToday(), clock.getUTCToday(), new BigDecimal("17.0"), Currency.USD, null));
    invoiceForExternalCharge.addInvoiceItem(externalCharge1);
    invoiceForExternalCharge.addInvoiceItem(externalCharge2);
    invoiceDao.createInvoice(invoiceForExternalCharge, null, ImmutableSet.<InvoiceTrackingModelDao>of(), new FutureAccountNotifications(), new ExistingInvoiceMetadata(ImmutableList.<Invoice>of()), context);
    final Invoice invoice = invoiceUserApi.getInvoice(invoiceForExternalCharge.getId(), callContext);
    invoiceUtil.checkInvoicesEqual(invoiceForExternalCharge, invoice);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FutureAccountNotifications(org.killbill.billing.invoice.InvoiceDispatcher.FutureAccountNotifications) 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