Search in sources :

Example 86 with DefaultInvoice

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

the class TestInvoiceDao method testGetInvoicesBySubscriptionForRecurringAndFixedItems.

@Test(groups = "slow")
public void testGetInvoicesBySubscriptionForRecurringAndFixedItems() 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 recurringItem1 = new RecurringInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId1, "test product", "test plan", "test A", null, startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(recurringItem1, context);
    final RecurringInvoiceItem recurringItem2 = new RecurringInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId2, "test product", "test plan", "test B", null, startDate, endDate, rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(recurringItem2, context);
    final RecurringInvoiceItem recurringItem3 = new RecurringInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId3, "test product", "test plan", "test C", null, startDate, endDate, rate3, rate3, Currency.USD);
    invoiceUtil.createInvoiceItem(recurringItem3, context);
    final RecurringInvoiceItem recurringItem4 = new RecurringInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId4, "test product", "test plan", "test D", null, startDate, endDate, rate4, rate4, Currency.USD);
    invoiceUtil.createInvoiceItem(recurringItem4, context);
    final FixedPriceInvoiceItem fixedItem1 = new FixedPriceInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId1, "test product", "test plan", "test A", null, startDate, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(fixedItem1, context);
    final FixedPriceInvoiceItem fixedItem2 = new FixedPriceInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId2, "test product", "test plan", "test B", null, startDate, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(fixedItem2, context);
    final FixedPriceInvoiceItem fixedItem3 = new FixedPriceInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId3, "test product", "test plan", "test C", null, startDate, rate3, Currency.USD);
    invoiceUtil.createInvoiceItem(fixedItem3, context);
    final FixedPriceInvoiceItem fixedItem4 = new FixedPriceInvoiceItem(invoiceId1, accountId, bundleId, subscriptionId4, "test product", "test plan", "test D", null, startDate, rate4, Currency.USD);
    invoiceUtil.createInvoiceItem(fixedItem4, 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 recurringItem5 = new RecurringInvoiceItem(invoiceId2, accountId, bundleId, subscriptionId1, "test product", "test plan", "test phase A", null, startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(recurringItem5, context);
    final RecurringInvoiceItem recurringItem6 = new RecurringInvoiceItem(invoiceId2, accountId, bundleId, subscriptionId2, "test product", "test plan", "test phase B", null, startDate, endDate, rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(recurringItem6, context);
    final RecurringInvoiceItem recurringItem7 = new RecurringInvoiceItem(invoiceId2, accountId, bundleId, subscriptionId3, "test product", "test plan", "test phase C", null, startDate, endDate, rate3, rate3, Currency.USD);
    invoiceUtil.createInvoiceItem(recurringItem7, context);
    final FixedPriceInvoiceItem fixedItem5 = new FixedPriceInvoiceItem(invoiceId2, accountId, bundleId, subscriptionId1, "test product", "test plan", "test phase A", null, startDate, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(fixedItem5, context);
    final FixedPriceInvoiceItem fixedItem6 = new FixedPriceInvoiceItem(invoiceId2, accountId, bundleId, subscriptionId2, "test product", "test plan", "test phase B", null, startDate, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(fixedItem6, context);
    final FixedPriceInvoiceItem fixedItem7 = new FixedPriceInvoiceItem(invoiceId2, accountId, bundleId, subscriptionId3, "test product", "test plan", "test phase C", null, startDate, rate3, Currency.USD);
    invoiceUtil.createInvoiceItem(fixedItem7, context);
    // check that each subscription returns the correct number of invoices
    final List<InvoiceModelDao> items1 = invoiceDao.getInvoicesBySubscription(subscriptionId1, context);
    assertEquals(items1.size(), 4);
    final List<InvoiceModelDao> items2 = invoiceDao.getInvoicesBySubscription(subscriptionId2, context);
    assertEquals(items2.size(), 4);
    final List<InvoiceModelDao> items3 = invoiceDao.getInvoicesBySubscription(subscriptionId3, context);
    assertEquals(items3.size(), 4);
    final List<InvoiceModelDao> items4 = invoiceDao.getInvoicesBySubscription(subscriptionId4, context);
    assertEquals(items4.size(), 2);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) 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 87 with DefaultInvoice

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

the class TestInvoiceDao method testCreationAndRetrievalByAccount.

@Test(groups = "slow")
public void testCreationAndRetrievalByAccount() throws EntityPersistenceException {
    final UUID accountId = account.getId();
    final Invoice invoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    final LocalDate invoiceDate = invoice.getInvoiceDate();
    invoiceUtil.createInvoice(invoice, context);
    final List<InvoiceModelDao> invoices = invoiceDao.getInvoicesByAccount(false, context);
    assertNotNull(invoices);
    assertEquals(invoices.size(), 1);
    final InvoiceModelDao thisInvoice = invoices.get(0);
    assertEquals(invoice.getAccountId(), accountId);
    assertTrue(thisInvoice.getInvoiceDate().compareTo(invoiceDate) == 0);
    assertEquals(thisInvoice.getCurrency(), Currency.USD);
    assertEquals(thisInvoice.getInvoiceItems().size(), 0);
    assertTrue(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(thisInvoice).compareTo(BigDecimal.ZERO) == 0);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 88 with DefaultInvoice

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

the class TestInvoiceDao method testSimple.

@Test(groups = "slow")
public void testSimple() throws Exception {
    final UUID accountId = account.getId();
    final Invoice invoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    invoiceUtil.createInvoice(invoice, context);
    final InvoiceModelDao retrievedInvoice = invoiceDao.getById(invoice.getId(), context);
    invoiceUtil.checkInvoicesEqual(retrievedInvoice, invoice);
    invoiceUtil.checkInvoicesEqual(invoiceDao.getByNumber(retrievedInvoice.getInvoiceNumber(), context), invoice);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 89 with DefaultInvoice

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

the class TestInvoiceDao method testInvoiceNumber.

@Test(groups = "slow")
public void testInvoiceNumber() throws InvoiceApiException, EntityPersistenceException {
    final Currency currency = Currency.USD;
    final DateTime targetDate1 = clock.getUTCNow().plusMonths(1);
    final DateTime targetDate2 = clock.getUTCNow().plusMonths(2);
    final SubscriptionBase subscription = getZombieSubscription();
    final Product product = Mockito.mock(Product.class);
    Mockito.when(product.getName()).thenReturn("product");
    final Plan plan = Mockito.mock(Plan.class);
    Mockito.when(plan.getName()).thenReturn("plan");
    Mockito.when(plan.getProduct()).thenReturn(product);
    Mockito.when(plan.getRecurringBillingMode()).thenReturn(BillingMode.IN_ADVANCE);
    final PlanPhase phase1 = Mockito.mock(PlanPhase.class);
    Mockito.when(phase1.getName()).thenReturn("plan-phase1");
    final PlanPhase phase2 = Mockito.mock(PlanPhase.class);
    Mockito.when(phase2.getName()).thenReturn("plan-phase2");
    final BillingEventSet events = new MockBillingEventSet();
    final List<Invoice> invoices = new ArrayList<Invoice>();
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, targetDate1, plan, phase1, null, TEN, currency, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "testEvent1", 1L, SubscriptionBaseTransitionType.CHANGE);
    events.add(event1);
    InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoices), null, new LocalDate(targetDate1), Currency.USD, null, context);
    Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    invoices.add(invoice1);
    invoiceUtil.createInvoice(invoice1, context);
    invoice1 = new DefaultInvoice(invoiceDao.getById(invoice1.getId(), context));
    assertNotNull(invoice1.getInvoiceNumber());
    final BillingEvent event2 = invoiceUtil.createMockBillingEvent(null, subscription, targetDate1, plan, phase2, null, TWENTY, currency, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "testEvent2", 2L, SubscriptionBaseTransitionType.CHANGE);
    events.add(event2);
    InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoices), null, new LocalDate(targetDate2), Currency.USD, null, context);
    Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    invoiceUtil.createInvoice(invoice2, context);
    invoice2 = new DefaultInvoice(invoiceDao.getById(invoice2.getId(), context));
    assertNotNull(invoice2.getInvoiceNumber());
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) ArrayList(java.util.ArrayList) Product(org.killbill.billing.catalog.api.Product) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) Currency(org.killbill.billing.catalog.api.Currency) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 90 with DefaultInvoice

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

the class TestInvoiceDao method testCantDeleteSystemGeneratedCredit.

@Test(groups = "slow")
public void testCantDeleteSystemGeneratedCredit() throws Exception {
    final UUID accountId = account.getId();
    // Create invoice 1
    // Scenario:
    // * $-10 repair
    // * $10 generated CBA
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    final RepairAdjInvoiceItem repairAdjInvoiceItem = new RepairAdjInvoiceItem(invoice1.getId(), invoice1.getAccountId(), invoice1.getInvoiceDate(), invoice1.getInvoiceDate(), BigDecimal.TEN.negate(), invoice1.getCurrency(), UUID.randomUUID());
    final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem1 = new CreditBalanceAdjInvoiceItem(invoice1.getId(), invoice1.getAccountId(), invoice1.getInvoiceDate(), repairAdjInvoiceItem.getAmount().negate(), invoice1.getCurrency());
    invoiceUtil.createInvoice(invoice1, context);
    invoiceUtil.createInvoiceItem(repairAdjInvoiceItem, context);
    invoiceUtil.createInvoiceItem(creditBalanceAdjInvoiceItem1, context);
    // Verify scenario
    Assert.assertEquals(invoiceDao.getAccountCBA(accountId, context).doubleValue(), 10.00);
    invoiceUtil.verifyInvoice(invoice1.getId(), 0.00, 10.00, context);
    // Delete the CBA on invoice 1
    try {
        invoiceDao.deleteCBA(accountId, invoice1.getId(), creditBalanceAdjInvoiceItem1.getId(), context);
        Assert.fail();
    } catch (IllegalStateException e) {
        Assert.assertEquals(e.getMessage(), "Cannot delete system generated credit");
    }
    // Verify the result
    Assert.assertEquals(invoiceDao.getAccountCBA(accountId, context).doubleValue(), 10.00);
    invoiceUtil.verifyInvoice(invoice1.getId(), 0.00, 10.00, context);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) 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