use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testMultiplePlansWithUtterChaos.
// TODO: modify this test to keep a running total of expected invoice amount over time
@Test(groups = "fast")
public void testMultiplePlansWithUtterChaos() throws InvoiceApiException, CatalogApiException {
// plan 1: change of phase from trial to discount followed by immediate cancellation; (covers phase change, cancel, pro-ration)
// plan 2: single plan that moves from trial to discount to evergreen; BCD = 10 (covers phase change)
// plan 3: change of term from monthly (BCD = 20) to annual (BCD = 31; immediate)
// plan 4: change of plan, effective EOT, BCD = 7 (covers change of plan)
// plan 5: addon to plan 2, with bill cycle alignment to plan; immediate cancellation
final UUID accountId = UUID.randomUUID();
final UUID bundleId = UUID.randomUUID();
final UUID subscriptionId1 = UUID.randomUUID();
final UUID subscriptionId2 = UUID.randomUUID();
final UUID subscriptionId3 = UUID.randomUUID();
final UUID subscriptionId4 = UUID.randomUUID();
final UUID subscriptionId5 = UUID.randomUUID();
final Plan plan1 = new MockPlan("Change from trial to discount with immediate cancellation");
final PlanPhase plan1Phase1 = createMockMonthlyPlanPhase(EIGHT, PhaseType.TRIAL);
final PlanPhase plan1Phase2 = createMockMonthlyPlanPhase(TWELVE, PhaseType.DISCOUNT);
final PlanPhase plan1Phase3 = createMockMonthlyPlanPhase();
final LocalDate plan1StartDate = invoiceUtil.buildDate(2011, 1, 5);
final LocalDate plan1PhaseChangeDate = invoiceUtil.buildDate(2011, 4, 5);
final LocalDate plan1CancelDate = invoiceUtil.buildDate(2011, 4, 29);
final Plan plan2 = new MockPlan("Change phase from trial to discount to evergreen");
final PlanPhase plan2Phase1 = createMockMonthlyPlanPhase(TWENTY, PhaseType.TRIAL);
final PlanPhase plan2Phase2 = createMockMonthlyPlanPhase(THIRTY, PhaseType.DISCOUNT);
final PlanPhase plan2Phase3 = createMockMonthlyPlanPhase(FORTY, PhaseType.EVERGREEN);
final LocalDate plan2StartDate = invoiceUtil.buildDate(2011, 3, 10);
final LocalDate plan2PhaseChangeToDiscountDate = invoiceUtil.buildDate(2011, 6, 10);
final LocalDate plan2PhaseChangeToEvergreenDate = invoiceUtil.buildDate(2011, 9, 10);
final Plan plan3 = new MockPlan("Upgrade with immediate change, BCD = 31");
final PlanPhase plan3Phase1 = createMockMonthlyPlanPhase(TEN, PhaseType.EVERGREEN);
final PlanPhase plan3Phase2 = createMockAnnualPlanPhase(ONE_HUNDRED, PhaseType.EVERGREEN);
final LocalDate plan3StartDate = invoiceUtil.buildDate(2011, 5, 20);
final LocalDate plan3UpgradeToAnnualDate = invoiceUtil.buildDate(2011, 7, 31);
final Plan plan4a = new MockPlan("Plan change effective EOT; plan 1");
final Plan plan4b = new MockPlan("Plan change effective EOT; plan 2");
final PlanPhase plan4aPhase1 = createMockMonthlyPlanPhase(FIFTEEN);
final PlanPhase plan4bPhase1 = createMockMonthlyPlanPhase(TWENTY_FOUR);
final LocalDate plan4StartDate = invoiceUtil.buildDate(2011, 6, 7);
final LocalDate plan4ChangeOfPlanDate = invoiceUtil.buildDate(2011, 8, 7);
final Plan plan5 = new MockPlan("Add-on");
final PlanPhase plan5Phase1 = createMockMonthlyPlanPhase(TWENTY);
final PlanPhase plan5Phase2 = createMockMonthlyPlanPhase();
final LocalDate plan5StartDate = invoiceUtil.buildDate(2011, 6, 21);
final LocalDate plan5CancelDate = invoiceUtil.buildDate(2011, 10, 7);
BigDecimal expectedAmount;
final List<Invoice> invoices = new ArrayList<Invoice>();
final BillingEventSet events = new MockBillingEventSet();
// on 1/5/2011, create SubscriptionBase 1 (trial)
events.add(createBillingEvent(subscriptionId1, bundleId, plan1StartDate, plan1, plan1Phase1, 5));
expectedAmount = EIGHT;
testInvoiceGeneration(accountId, events, invoices, plan1StartDate, 1, expectedAmount);
// on 2/5/2011, invoice SubscriptionBase 1 (trial)
expectedAmount = EIGHT;
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 2, 5), 1, expectedAmount);
// on 3/5/2011, invoice SubscriptionBase 1 (trial)
expectedAmount = EIGHT;
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 3, 5), 1, expectedAmount);
// on 3/10/2011, create SubscriptionBase 2 (trial)
events.add(createBillingEvent(subscriptionId2, bundleId, plan2StartDate, plan2, plan2Phase1, 10));
expectedAmount = TWENTY;
testInvoiceGeneration(accountId, events, invoices, plan2StartDate, 1, expectedAmount);
// on 4/5/2011, invoice SubscriptionBase 1 (discount)
events.add(createBillingEvent(subscriptionId1, bundleId, plan1PhaseChangeDate, plan1, plan1Phase2, 5));
expectedAmount = TWELVE;
testInvoiceGeneration(accountId, events, invoices, plan1PhaseChangeDate, 1, expectedAmount);
// on 4/10/2011, invoice SubscriptionBase 2 (trial)
expectedAmount = TWENTY;
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 4, 10), 1, expectedAmount);
// on 4/29/2011, cancel SubscriptionBase 1
events.add(createBillingEvent(subscriptionId1, bundleId, plan1CancelDate, plan1, plan1Phase3, 5));
expectedAmount = new BigDecimal("-2.40");
testInvoiceGeneration(accountId, events, invoices, plan1CancelDate, 1, expectedAmount);
// on 5/10/2011, invoice SubscriptionBase 2 (trial)
expectedAmount = TWENTY;
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 5, 10), 1, expectedAmount);
// on 5/20/2011, create SubscriptionBase 3 (monthly)
events.add(createBillingEvent(subscriptionId3, bundleId, plan3StartDate, plan3, plan3Phase1, 20));
expectedAmount = TEN;
testInvoiceGeneration(accountId, events, invoices, plan3StartDate, 1, expectedAmount);
// on 6/7/2011, create SubscriptionBase 4
events.add(createBillingEvent(subscriptionId4, bundleId, plan4StartDate, plan4a, plan4aPhase1, 7));
expectedAmount = FIFTEEN;
testInvoiceGeneration(accountId, events, invoices, plan4StartDate, 1, expectedAmount);
// on 6/10/2011, invoice SubscriptionBase 2 (discount)
events.add(createBillingEvent(subscriptionId2, bundleId, plan2PhaseChangeToDiscountDate, plan2, plan2Phase2, 10));
expectedAmount = THIRTY;
testInvoiceGeneration(accountId, events, invoices, plan2PhaseChangeToDiscountDate, 1, expectedAmount);
// on 6/20/2011, invoice SubscriptionBase 3 (monthly)
expectedAmount = TEN;
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 6, 20), 1, expectedAmount);
// on 6/21/2011, create add-on (subscription 5)
events.add(createBillingEvent(subscriptionId5, bundleId, plan5StartDate, plan5, plan5Phase1, 10));
expectedAmount = TWENTY.multiply(NINETEEN).divide(THIRTY, KillBillMoney.ROUNDING_METHOD);
testInvoiceGeneration(accountId, events, invoices, plan5StartDate, 1, expectedAmount);
// on 7/7/2011, invoice SubscriptionBase 4 (plan 1)
expectedAmount = FIFTEEN;
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 7, 7), 1, expectedAmount);
// on 7/10/2011, invoice SubscriptionBase 2 (discount), invoice SubscriptionBase 5
expectedAmount = THIRTY.add(TWENTY);
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 7, 10), 2, expectedAmount);
// on 7/20/2011, invoice SubscriptionBase 3 (monthly)
expectedAmount = TEN;
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 7, 20), 1, expectedAmount);
// on 7/31/2011, convert SubscriptionBase 3 to annual
events.add(createBillingEvent(subscriptionId3, bundleId, plan3UpgradeToAnnualDate, plan3, plan3Phase2, 31));
testInvoiceGeneration(accountId, events, invoices, plan3UpgradeToAnnualDate, 2, new BigDecimal("93.55"));
// on 8/7/2011, invoice SubscriptionBase 4 (plan 2)
events.add(createBillingEvent(subscriptionId4, bundleId, plan4ChangeOfPlanDate, plan4b, plan4bPhase1, 7));
expectedAmount = TWENTY_FOUR;
testInvoiceGeneration(accountId, events, invoices, plan4ChangeOfPlanDate, 1, expectedAmount);
// on 8/10/2011, invoice plan 2 (discount), invoice SubscriptionBase 5
expectedAmount = THIRTY.add(TWENTY);
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 8, 10), 2, expectedAmount);
// on 9/7/2011, invoice SubscriptionBase 4 (plan 2)
expectedAmount = TWENTY_FOUR;
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 9, 7), 1, expectedAmount);
// on 9/10/2011, invoice plan 2 (evergreen), invoice SubscriptionBase 5
events.add(createBillingEvent(subscriptionId2, bundleId, plan2PhaseChangeToEvergreenDate, plan2, plan2Phase3, 10));
expectedAmount = FORTY.add(TWENTY);
testInvoiceGeneration(accountId, events, invoices, plan2PhaseChangeToEvergreenDate, 2, expectedAmount);
// on 10/7/2011, invoice SubscriptionBase 4 (plan 2), cancel SubscriptionBase 5
events.add(createBillingEvent(subscriptionId5, bundleId, plan5CancelDate, plan5, plan5Phase2, 10));
testInvoiceGeneration(accountId, events, invoices, plan5CancelDate, 2, new BigDecimal("22.00"));
// on 10/10/2011, invoice plan 2 (evergreen)
expectedAmount = FORTY;
testInvoiceGeneration(accountId, events, invoices, invoiceUtil.buildDate(2011, 10, 10), 1, expectedAmount);
}
use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testOverlappingItems.
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testOverlappingItems() throws InvoiceApiException {
final LocalDate startDate = new LocalDate("2016-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.toDateTimeAtStartOfDay(), plan, planPhase, null, amount, account.getCurrency(), BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event);
// Simulate a previous mis-bill: existing item is for [2016-01-01,2016-01-30], proposed will be for [2016-01-01,2016-02-01]
final List<Invoice> existingInvoices = new LinkedList<Invoice>();
final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate, account.getCurrency());
invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), startDate, startDate.plusDays(29), amount, amount, account.getCurrency()));
existingInvoices.add(invoice);
// We will repair the wrong item and generate the correct recurring item
final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
assertEquals(generatedItems.size(), 2);
assertTrue(generatedItems.get(0) instanceof RecurringInvoiceItem);
assertEquals(generatedItems.get(0).getStartDate(), new LocalDate("2016-01-01"));
assertEquals(generatedItems.get(0).getEndDate(), new LocalDate("2016-02-01"));
assertEquals(generatedItems.get(0).getAmount().compareTo(amount), 0);
assertTrue(generatedItems.get(1) instanceof RepairAdjInvoiceItem);
assertEquals(generatedItems.get(1).getAmount().compareTo(amount.negate()), 0);
assertEquals(generatedItems.get(1).getLinkedItemId(), invoice.getInvoiceItems().get(0).getId());
}
use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testOverlappingItemsWithTooManyRepairs.
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testOverlappingItemsWithTooManyRepairs() throws InvoiceApiException {
final LocalDate startDate = new LocalDate("2016-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.toDateTimeAtStartOfDay(), plan, planPhase, null, amount, account.getCurrency(), BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event);
// Simulate a previous mis-bill: existing item is for [2016-01-01,2016-01-30], proposed will be for [2016-01-01,2016-02-01]
final List<Invoice> existingInvoices = new LinkedList<Invoice>();
final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate, account.getCurrency());
invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), startDate, startDate.plusDays(29), amount, amount, account.getCurrency()));
// But the system has already repaired it
invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, startDate.plusDays(29), // Note! The amount will not matter
BigDecimal.ONE.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
// Twice!
invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, startDate.plusDays(29), // Note! The amount will not matter
BigDecimal.ONE.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
existingInvoices.add(invoice);
try {
final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
assertTrue(e.getCause().getMessage().startsWith("Too many repairs"));
}
}
use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testSimpleWithSingleDiscountEvent.
@Test(groups = "fast")
public void testSimpleWithSingleDiscountEvent() throws Exception {
final UUID accountId = UUID.randomUUID();
final SubscriptionBase sub = createSubscription();
final Plan plan = new MockPlan("Plan with a single discount phase");
final PlanPhase phaseEvergreen = createMockMonthlyPlanPhase(EIGHT, PhaseType.DISCOUNT);
final int bcdLocal = 16;
final LocalDate startDate = invoiceUtil.buildDate(2012, 7, 16);
final BillingEventSet events = new MockBillingEventSet();
events.add(createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phaseEvergreen, bcdLocal));
// Set a target date of today (start date)
final LocalDate targetDate = startDate;
final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, targetDate, Currency.USD, internalCallContext);
final Invoice invoice = invoiceWithMetadata.getInvoice();
assertNotNull(invoice);
assertEquals(invoice.getNumberOfItems(), 1);
assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), invoiceUtil.buildDate(2012, 7, 16));
assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), invoiceUtil.buildDate(2012, 8, 16));
}
use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testBuggyBillingEventsDoNotImpactInvoicing.
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testBuggyBillingEventsDoNotImpactInvoicing() throws InvoiceApiException, CatalogApiException {
final UUID accountId = UUID.randomUUID();
final UUID bundleId = UUID.randomUUID();
final UUID subscriptionId1 = UUID.randomUUID();
final BillingEventSet events = new MockBillingEventSet();
final List<Invoice> invoices = new ArrayList<Invoice>();
Invoice invoice;
final Plan plan1 = new MockPlan("plan1");
final PlanPhase plan1Phase1 = createMockMonthlyPlanPhase(null, EIGHT, PhaseType.TRIAL);
final PlanPhase plan1Phase2 = createMockMonthlyPlanPhase(TWELVE, PhaseType.EVERGREEN);
final LocalDate plan1StartDate = invoiceUtil.buildDate(2011, 1, 5);
final LocalDate plan1PhaseChangeDate = invoiceUtil.buildDate(2011, 2, 5);
// To simulate a bug, duplicate the billing events
for (int i = 0; i < 10; i++) {
events.add(createBillingEvent(subscriptionId1, bundleId, plan1StartDate, plan1, plan1Phase1, 5));
events.add(createBillingEvent(subscriptionId1, bundleId, plan1PhaseChangeDate, plan1, plan1Phase2, 5));
}
assertEquals(events.size(), 20);
// Fix for https://github.com/killbill/killbill/issues/467 will prevent duplicate fixed items
testInvoiceGeneration(accountId, events, invoices, plan1StartDate, 1, EIGHT);
invoice = invoices.get(0);
assertEquals(invoice.getInvoiceItems().size(), 1);
assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), subscriptionId1);
assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.FIXED);
assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2011, 1, 5));
assertNull(invoice.getInvoiceItems().get(0).getEndDate());
assertEquals(invoice.getInvoiceItems().get(0).getAmount().compareTo(EIGHT), 0);
// Intermediate billing intervals associated with recurring items will be less than a day, so only one recurring item will be generated
testInvoiceGeneration(accountId, events, invoices, plan1PhaseChangeDate, 1, TWELVE);
invoice = invoices.get(1);
assertEquals(invoice.getInvoiceItems().size(), 1);
assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), subscriptionId1);
assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2011, 2, 5));
assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), new LocalDate(2011, 3, 5));
assertEquals(invoice.getInvoiceItems().get(0).getAmount().compareTo(TWELVE), 0);
}
Aggregations