Search in sources :

Example 76 with DefaultInvoice

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

the class TestInvoiceDao method testGetInvoicesBySubscriptionForRecurringItems.

@Test(groups = "slow")
public void testGetInvoicesBySubscriptionForRecurringItems() throws EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final UUID subscriptionId1 = UUID.randomUUID();
    final BigDecimal rate1 = new BigDecimal("17.0");
    final UUID subscriptionId2 = UUID.randomUUID();
    final BigDecimal rate2 = new BigDecimal("42.0");
    final UUID subscriptionId3 = UUID.randomUUID();
    final BigDecimal rate3 = new BigDecimal("3.0");
    final UUID subscriptionId4 = UUID.randomUUID();
    final BigDecimal rate4 = new BigDecimal("12.0");
    final LocalDate targetDate = new LocalDate(2011, 5, 23);
    // Create invoice 1 (subscriptions 1-4)
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate, Currency.USD);
    invoiceUtil.createInvoice(invoice1, context);
    final UUID invoiceId1 = invoice1.getId();
    LocalDate startDate = new LocalDate(2011, 3, 1);
    LocalDate endDate = startDate.plusMonths(1);
    final RecurringInvoiceItem item1 = new RecurringInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId1, "test product", "test plan", "test A", null, startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(item1, context);
    final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId2, "test product", "test plan", "test B", null, startDate, endDate, rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(item2, context);
    final RecurringInvoiceItem item3 = new RecurringInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId3, "test product", "test plan", "test C", null, startDate, endDate, rate3, rate3, Currency.USD);
    invoiceUtil.createInvoiceItem(item3, context);
    final RecurringInvoiceItem item4 = new RecurringInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId4, "test product", "test plan", "test D", null, startDate, endDate, rate4, rate4, Currency.USD);
    invoiceUtil.createInvoiceItem(item4, context);
    // Create invoice 2 (subscriptions 1-3)
    final DefaultInvoice invoice2 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate, Currency.USD);
    invoiceUtil.createInvoice(invoice2, context);
    final UUID invoiceId2 = invoice2.getId();
    startDate = endDate;
    endDate = startDate.plusMonths(1);
    final RecurringInvoiceItem item5 = new RecurringInvoiceItem(invoiceId2, accountId, bundleId, subscriptionId1, "test product", "test plan", "test phase A", null, startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(item5, context);
    final RecurringInvoiceItem item6 = new RecurringInvoiceItem(invoiceId2, accountId, bundleId, subscriptionId2, "test product", "test plan", "test phase B", null, startDate, endDate, rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(item6, context);
    final RecurringInvoiceItem item7 = new RecurringInvoiceItem(invoiceId2, accountId, bundleId, subscriptionId3, "test product", "test plan", "test phase C", null, startDate, endDate, rate3, rate3, Currency.USD);
    invoiceUtil.createInvoiceItem(item7, context);
    // Check that each subscription returns the correct number of invoices
    final List<InvoiceModelDao> items1 = invoiceDao.getInvoicesBySubscription(subscriptionId1, context);
    assertEquals(items1.size(), 2);
    final List<InvoiceModelDao> items2 = invoiceDao.getInvoicesBySubscription(subscriptionId2, context);
    assertEquals(items2.size(), 2);
    final List<InvoiceModelDao> items3 = invoiceDao.getInvoicesBySubscription(subscriptionId3, context);
    assertEquals(items3.size(), 2);
    final List<InvoiceModelDao> items4 = invoiceDao.getInvoicesBySubscription(subscriptionId4, context);
    assertEquals(items4.size(), 1);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 77 with DefaultInvoice

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

the class TestInvoiceDao method testInvoicePayment.

@Test(groups = "slow")
public void testInvoicePayment() throws InvoiceApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final Invoice invoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    final UUID invoiceId = invoice.getId();
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = new LocalDate(2010, 1, 1);
    final LocalDate endDate = new LocalDate(2010, 4, 1);
    final InvoiceItem invoiceItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "test product", "test plan", "test phase", null, startDate, endDate, new BigDecimal("21.00"), new BigDecimal("7.00"), Currency.USD);
    invoice.addInvoiceItem(invoiceItem);
    invoiceUtil.createInvoice(invoice, context);
    final InvoiceModelDao savedInvoice = invoiceDao.getById(invoiceId, context);
    assertNotNull(savedInvoice);
    assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(savedInvoice).compareTo(new BigDecimal("21.00")), 0);
    assertEquals(savedInvoice.getInvoiceItems().size(), 1);
    final BigDecimal paymentAmount = new BigDecimal("11.00");
    final UUID paymentId = UUID.randomUUID();
    final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoiceId, clock.getUTCNow().plusDays(12), paymentAmount, Currency.USD, Currency.USD, "cookie", true);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), UUID.randomUUID(), context);
    final InvoiceModelDao retrievedInvoice = invoiceDao.getById(invoiceId, context);
    assertNotNull(retrievedInvoice);
    assertEquals(retrievedInvoice.getInvoiceItems().size(), 1);
    assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(retrievedInvoice).compareTo(new BigDecimal("10.00")), 0);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Test(org.testng.annotations.Test)

Example 78 with DefaultInvoice

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

the class TestInvoiceDao method testAccountBalance.

@Test(groups = "slow")
public void testAccountBalance() throws EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate targetDate1 = new LocalDate(2011, 10, 6);
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1, Currency.USD);
    invoiceUtil.createInvoice(invoice1, context);
    final LocalDate startDate = new LocalDate(2011, 3, 1);
    final LocalDate endDate = startDate.plusMonths(1);
    final BigDecimal rate1 = new BigDecimal("17.0");
    final BigDecimal rate2 = new BigDecimal("42.0");
    final RecurringInvoiceItem item1 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test phase A", null, startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(item1, context);
    final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test product", "test plan", "test phase B", null, startDate, endDate, rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(item2, context);
    final BigDecimal payment1 = new BigDecimal("48.0");
    final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice1.getId(), new DateTime(), payment1, Currency.USD, Currency.USD, null, true);
    invoiceUtil.createPayment(payment, context);
    final BigDecimal balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(rate1.add(rate2).subtract(payment1)), 0);
}
Also used : InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Test(org.testng.annotations.Test)

Example 79 with DefaultInvoice

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

the class TestInvoiceDaoHelper method testPopulateChildrenWithParent.

@Test(groups = "slow")
public void testPopulateChildrenWithParent() throws Exception {
    final Account parentAccount = invoiceUtil.createAccount(callContext);
    final UUID childAccountId = account.getId();
    final UUID parentAccountId = parentAccount.getId();
    final InternalCallContext parentContext = internalCallContextFactory.createInternalCallContext(parentAccountId, callContext);
    final InvoiceModelDao parentInvoice = new InvoiceModelDao(parentAccountId, today, account.getCurrency(), InvoiceStatus.DRAFT, true);
    final InvoiceItem parentInvoiceItem = new ParentInvoiceItem(UUID.randomUUID(), now, parentInvoice.getId(), parentAccountId, childAccountId, BigDecimal.TEN, account.getCurrency(), "");
    parentInvoice.addInvoiceItem(new InvoiceItemModelDao(parentInvoiceItem));
    invoiceUtil.createInvoice(new DefaultInvoice(parentInvoice), parentContext);
    final UUID accountId = account.getId();
    final Invoice childInvoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    final InvoiceItem invoiceItem = new RecurringInvoiceItem(childInvoice.getId(), accountId, UUID.randomUUID(), UUID.randomUUID(), "test", "test-plan", "test-phase", null, today, today, BigDecimal.TEN, BigDecimal.TEN, Currency.USD);
    childInvoice.addInvoiceItem(invoiceItem);
    invoiceUtil.createInvoice(childInvoice, internalAccountContext);
    InvoiceParentChildModelDao invoiceRelation = new InvoiceParentChildModelDao(parentInvoice.getId(), childInvoice.getId(), childAccountId);
    invoiceDao.createParentChildInvoiceRelation(invoiceRelation, internalAccountContext);
    // //
    final List<Tag> tags = ImmutableList.of();
    final InvoiceModelDao invoice1 = getRawInvoice(childInvoice.getId(), internalAccountContext);
    populateChildrenByInvoiceId(invoice1, tags);
    final InvoiceModelDao invoice2 = getRawInvoice(childInvoice.getId(), internalAccountContext);
    populateChildrenByAccountRecordId(invoice2, tags);
    Assert.assertEquals(invoice1, invoice2);
}
Also used : Account(org.killbill.billing.account.api.Account) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) DefaultControlTag(org.killbill.billing.util.tag.DefaultControlTag) Tag(org.killbill.billing.util.tag.Tag) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 80 with DefaultInvoice

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

the class TestInvoiceDaoHelper method testPopulateChildrenWith_WRITTEN_OFF.

@Test(groups = "slow")
public void testPopulateChildrenWith_WRITTEN_OFF() throws Exception {
    final UUID accountId = account.getId();
    final Invoice inputInvoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    final InvoiceItem invoiceItem = new RecurringInvoiceItem(inputInvoice.getId(), accountId, UUID.randomUUID(), UUID.randomUUID(), "test", "test-plan", "test-phase", null, today, today, BigDecimal.TEN, BigDecimal.TEN, Currency.USD);
    inputInvoice.addInvoiceItem(invoiceItem);
    invoiceUtil.createInvoice(inputInvoice, internalAccountContext);
    final List<Tag> tags = ImmutableList.of(new DefaultControlTag(ControlTagType.WRITTEN_OFF, ObjectType.INVOICE, inputInvoice.getId(), clock.getUTCNow()));
    final InvoiceModelDao invoice1 = getRawInvoice(inputInvoice.getId(), internalAccountContext);
    populateChildrenByInvoiceId(invoice1, tags);
    final InvoiceModelDao invoice2 = getRawInvoice(inputInvoice.getId(), internalAccountContext);
    populateChildrenByAccountRecordId(invoice2, tags);
    Assert.assertEquals(invoice1, invoice2);
    Assert.assertEquals((new DefaultInvoice(invoice1)).getBalance().compareTo(BigDecimal.ZERO), 0);
    Assert.assertEquals((new DefaultInvoice(invoice2)).getBalance().compareTo(BigDecimal.ZERO), 0);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) DefaultControlTag(org.killbill.billing.util.tag.DefaultControlTag) Tag(org.killbill.billing.util.tag.Tag) DefaultControlTag(org.killbill.billing.util.tag.DefaultControlTag) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Aggregations

DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)92 Invoice (org.killbill.billing.invoice.api.Invoice)81 LocalDate (org.joda.time.LocalDate)64 Test (org.testng.annotations.Test)63 UUID (java.util.UUID)51 BigDecimal (java.math.BigDecimal)46 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)46 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)38 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)37 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)28 LinkedList (java.util.LinkedList)25 HashMap (java.util.HashMap)24 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)24 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)20 AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)20 BillingEventSet (org.killbill.billing.junction.BillingEventSet)20 MockPlan (org.killbill.billing.catalog.MockPlan)18 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)18 BillingEvent (org.killbill.billing.junction.BillingEvent)18 Plan (org.killbill.billing.catalog.api.Plan)17