Search in sources :

Example 36 with DefaultPrice

use of org.killbill.billing.catalog.DefaultPrice in project killbill by killbill.

the class TestInvoiceDao method testMixedModeInvoicePersistence.

@Test(groups = "slow")
public void testMixedModeInvoicePersistence() throws InvoiceApiException, CatalogApiException, EntityPersistenceException {
    final Currency currency = Currency.USD;
    final DefaultPrice zeroPrice = new DefaultPrice(BigDecimal.ZERO, Currency.USD);
    final MockInternationalPrice fixedPrice = new MockInternationalPrice(zeroPrice);
    final MockPlanPhase phase1 = new MockPlanPhase(null, fixedPrice);
    final BigDecimal cheapAmount = new BigDecimal("24.95");
    final DefaultPrice cheapPrice = new DefaultPrice(cheapAmount, Currency.USD);
    final MockInternationalPrice recurringPrice = new MockInternationalPrice(cheapPrice);
    final MockPlanPhase phase2 = new MockPlanPhase(recurringPrice, null);
    final MockPlan plan = new MockPlan();
    final SubscriptionBase subscription = getZombieSubscription();
    final DateTime effectiveDate1 = invoiceUtil.buildDate(2011, 1, 1).toDateTimeAtStartOfDay();
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, effectiveDate1, plan, phase1, fixedPrice.getPrice(currency), null, currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "testEvent1", 1L, SubscriptionBaseTransitionType.CREATE);
    final BillingEventSet events = new MockBillingEventSet();
    events.add(event1);
    final DateTime effectiveDate2 = effectiveDate1.plusDays(30);
    final BillingEvent event2 = invoiceUtil.createMockBillingEvent(null, subscription, effectiveDate2, plan, phase2, null, recurringPrice.getPrice(currency), currency, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "testEvent2", 2L, SubscriptionBaseTransitionType.CHANGE);
    events.add(event2);
    final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(), null, new LocalDate(effectiveDate2), Currency.USD, null, context);
    final Invoice invoice = invoiceWithMetadata.getInvoice();
    assertNotNull(invoice);
    assertEquals(invoice.getNumberOfItems(), 2);
    assertEquals(invoice.getBalance().compareTo(cheapAmount), 0);
    invoiceUtil.createInvoice(invoice, context);
    final InvoiceModelDao savedInvoice = invoiceDao.getById(invoice.getId(), context);
    assertNotNull(savedInvoice);
    assertEquals(savedInvoice.getInvoiceItems().size(), 2);
    assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(savedInvoice).compareTo(cheapAmount), 0);
}
Also used : AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) Currency(org.killbill.billing.catalog.api.Currency) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) Test(org.testng.annotations.Test)

Example 37 with DefaultPrice

use of org.killbill.billing.catalog.DefaultPrice in project killbill by killbill.

the class TestInvoiceDao method testInvoiceGenerationForImmediateChanges.

/*
     *
     * this test verifies that immediate changes give the correct results
     *
     */
@Test(groups = "slow")
public void testInvoiceGenerationForImmediateChanges() throws InvoiceApiException, CatalogApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final List<Invoice> invoiceList = new ArrayList<Invoice>();
    final LocalDate targetDate = new LocalDate(2011, 2, 16);
    final Currency currency = Currency.USD;
    // generate first invoice
    final DefaultPrice price1 = new DefaultPrice(TEN, Currency.USD);
    final MockInternationalPrice recurringPrice = new MockInternationalPrice(price1);
    final MockPlanPhase phase1 = new MockPlanPhase(recurringPrice, null, BillingPeriod.MONTHLY, PhaseType.TRIAL);
    final MockPlan plan1 = new MockPlan(phase1);
    final SubscriptionBase subscription = getZombieSubscription();
    final DateTime effectiveDate1 = new DateTime(2011, 2, 1, 0, 0, 0);
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, effectiveDate1, plan1, phase1, null, recurringPrice.getPrice(currency), currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "testEvent1", 1L, SubscriptionBaseTransitionType.CREATE);
    final BillingEventSet events = new MockBillingEventSet();
    events.add(event1);
    final InvoiceWithMetadata invoiceWithMetadata1 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoiceList), null, targetDate, Currency.USD, null, context);
    final Invoice invoice1 = invoiceWithMetadata1.getInvoice();
    assertEquals(invoice1.getBalance(), KillBillMoney.of(TEN, invoice1.getCurrency()));
    invoiceList.add(invoice1);
    // generate second invoice
    final DefaultPrice price2 = new DefaultPrice(TWENTY, Currency.USD);
    final MockInternationalPrice recurringPrice2 = new MockInternationalPrice(price2);
    final MockPlanPhase phase2 = new MockPlanPhase(recurringPrice, null, BillingPeriod.MONTHLY, PhaseType.TRIAL);
    final MockPlan plan2 = new MockPlan(phase2);
    final DateTime effectiveDate2 = new DateTime(2011, 2, 15, 0, 0, 0);
    final BillingEvent event2 = invoiceUtil.createMockBillingEvent(null, subscription, effectiveDate2, plan2, phase2, null, recurringPrice2.getPrice(currency), currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "testEvent2", 2L, SubscriptionBaseTransitionType.CREATE);
    events.add(event2);
    // second invoice should be for one half (14/28 days) the difference between the rate plans
    // this is a temporary state, since it actually contains an adjusting item that properly belong to invoice 1
    final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoiceList), null, targetDate, Currency.USD, null, context);
    final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
    assertEquals(invoice2.getBalance(), KillBillMoney.of(FIVE, invoice2.getCurrency()));
    invoiceList.add(invoice2);
    invoiceUtil.createInvoice(invoice1, context);
    invoiceUtil.createInvoice(invoice2, context);
    final InvoiceModelDao savedInvoice1 = invoiceDao.getById(invoice1.getId(), context);
    assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(savedInvoice1), KillBillMoney.of(TEN, savedInvoice1.getCurrency()));
    final InvoiceModelDao savedInvoice2 = invoiceDao.getById(invoice2.getId(), context);
    assertEquals(InvoiceModelDaoHelper.getRawBalanceForRegularInvoice(savedInvoice2), KillBillMoney.of(FIVE, savedInvoice2.getCurrency()));
}
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) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) Currency(org.killbill.billing.catalog.api.Currency) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) Test(org.testng.annotations.Test)

Example 38 with DefaultPrice

use of org.killbill.billing.catalog.DefaultPrice in project killbill by killbill.

the class TestFixedAndRecurringInvoiceItemGeneratorWithOptimization method testRecurringInAdvance.

@Test(groups = "fast")
public void testRecurringInAdvance() throws InvoiceApiException {
    final DateTime startDate = new DateTime("2020-01-01");
    final BillingEventSet events = new MockBillingEventSet();
    final BigDecimal amount = BigDecimal.TEN;
    final MockInternationalPrice price = new MockInternationalPrice(new DefaultPrice(amount, account.getCurrency()));
    final Plan plan = new MockPlan("my-plan");
    final PlanPhase planPhase = new MockPlanPhase(price, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, startDate, plan, planPhase, null, amount, account.getCurrency(), planPhase.getRecurring().getBillingPeriod(), 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
    events.add(event);
    // Only look for older invoices 1 month from NOW (we assume targetDate = NOW as time pass normally)
    final Period maxInvoiceLimit = new Period("P1m");
    // Initial invoicing (targetDate1  = startDate)
    // There is no existing invoice
    // => Should generate the item
    // 2020-01-01
    final LocalDate targetDate1 = startDate.toLocalDate();
    final LocalDate cuttoffDate1 = targetDate1.minus(maxInvoiceLimit);
    final AccountInvoices existingInvoices1 = new AccountInvoicesExp(cuttoffDate1, null, ImmutableList.of());
    final InvoiceGeneratorResult result1 = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices1, targetDate1, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), null, internalCallContext);
    assertEquals(result1.getItems().size(), 1);
    assertEquals(result1.getItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(result1.getItems().get(0).getStartDate().compareTo(new LocalDate("2020-01-01")), 0);
    assertEquals(result1.getItems().get(0).getEndDate().compareTo(new LocalDate("2020-02-01")), 0);
    // One month after invoicing, optimization does not kick-in yet
    // There is an existing invoice
    // => Should not regenerate the item, but should correctly generate the next RECURRING item
    // 2020-02-01
    final LocalDate targetDate2 = startDate.toLocalDate().plusMonths(1);
    final LocalDate cuttoffDate2 = targetDate2.minus(maxInvoiceLimit);
    final Invoice invoice2 = new DefaultInvoice(account.getId(), clock.getUTCToday(), targetDate2, account.getCurrency());
    invoice2.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), startDate, invoice2.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), null, event.getPlan().getName(), event.getPlanPhase().getName(), null, startDate.toLocalDate(), startDate.plusMonths(1).toLocalDate(), amount, amount, account.getCurrency()));
    final AccountInvoices existingInvoices2 = new AccountInvoicesExp(cuttoffDate2, null, ImmutableList.of(invoice2));
    final InvoiceGeneratorResult result2 = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices2, targetDate2, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), null, internalCallContext);
    assertEquals(result2.getItems().size(), 1);
    assertEquals(result2.getItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(result2.getItems().get(0).getStartDate().compareTo(new LocalDate("2020-02-01")), 0);
    assertEquals(result2.getItems().get(0).getEndDate().compareTo(new LocalDate("2020-03-01")), 0);
    // Two month after invoicing, optimization **does** kick-in
    // There are 2 existing invoices but the first one is ignored (optimization)
    // => Should not regenerate the item
    // 2020-03-01
    final LocalDate targetDate3 = startDate.toLocalDate().plusMonths(2);
    final LocalDate cuttoffDate3 = targetDate3.minus(maxInvoiceLimit);
    final Invoice invoice3 = new DefaultInvoice(account.getId(), clock.getUTCToday(), targetDate3, account.getCurrency());
    invoice3.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), startDate.plusMonths(1), invoice3.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), null, event.getPlan().getName(), event.getPlanPhase().getName(), null, startDate.plusMonths(1).toLocalDate(), startDate.plusMonths(2).toLocalDate(), amount, amount, account.getCurrency()));
    final AccountInvoices existingInvoices3 = new AccountInvoicesExp(cuttoffDate3, null, ImmutableList.of(invoice3));
    final InvoiceGeneratorResult result3 = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices3, targetDate3, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), null, internalCallContext);
    assertEquals(result3.getItems().size(), 1);
    assertEquals(result3.getItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(result3.getItems().get(0).getStartDate().compareTo(new LocalDate("2020-03-01")), 0);
    assertEquals(result3.getItems().get(0).getEndDate().compareTo(new LocalDate("2020-04-01")), 0);
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) Period(org.joda.time.Period) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) InvoiceGeneratorResult(org.killbill.billing.invoice.generator.InvoiceItemGenerator.InvoiceGeneratorResult) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) AccountInvoicesExp(org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) 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) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Aggregations

DefaultPrice (org.killbill.billing.catalog.DefaultPrice)38 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)34 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)34 MockPlan (org.killbill.billing.catalog.MockPlan)33 Test (org.testng.annotations.Test)32 LocalDate (org.joda.time.LocalDate)30 BillingEvent (org.killbill.billing.junction.BillingEvent)30 Plan (org.killbill.billing.catalog.api.Plan)28 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)28 BigDecimal (java.math.BigDecimal)27 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)26 BillingEventSet (org.killbill.billing.junction.BillingEventSet)25 Invoice (org.killbill.billing.invoice.api.Invoice)23 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)23 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)22 AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)22 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)19 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)19 DefaultBillingEvent (org.killbill.billing.junction.plumbing.billing.DefaultBillingEvent)19 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)18