use of org.killbill.billing.junction.BillingEventSet in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testCancelEOTWithFullItemAdjustment.
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/654")
public void testCancelEOTWithFullItemAdjustment() throws CatalogApiException, InvoiceApiException {
final BigDecimal rate = new BigDecimal("39.95");
final BillingEventSet events = new MockBillingEventSet();
final SubscriptionBase sub = createSubscription();
final LocalDate startDate = invoiceUtil.buildDate(2016, 10, 9);
final LocalDate endDate = invoiceUtil.buildDate(2016, 11, 9);
final Plan plan = new MockPlan();
final PlanPhase phase = createMockMonthlyPlanPhase(rate);
final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, 9);
events.add(event);
final LocalDate targetDate = invoiceUtil.buildDate(2016, 10, 9);
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.getBalance(), KillBillMoney.of(rate, invoice.getCurrency()));
assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), sub.getId());
assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), startDate);
assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), endDate);
// Cancel EOT and Add the item adjustment
final BillingEvent event2 = invoiceUtil.createMockBillingEvent(account, sub, endDate.toDateTimeAtStartOfDay(), null, phase, ZERO, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 9, BillingMode.IN_ADVANCE, "Cancel", 2L, SubscriptionBaseTransitionType.CANCEL);
events.add(event2);
final InvoiceItem itemAdj = new ItemAdjInvoiceItem(invoice.getInvoiceItems().get(0), new LocalDate(2016, 10, 12), rate.negate(), Currency.USD);
invoice.addInvoiceItem(itemAdj);
final List<Invoice> existingInvoices = new ArrayList<Invoice>();
existingInvoices.add(invoice);
final InvoiceWithMetadata invoiceWithMetadata2 = generator.generateInvoice(account, events, existingInvoices, targetDate, Currency.USD, internalCallContext);
final Invoice invoice2 = invoiceWithMetadata2.getInvoice();
assertNull(invoice2);
}
use of org.killbill.billing.junction.BillingEventSet in project killbill by killbill.
the class TestInvoiceHelper method generateRegularInvoice.
public UUID generateRegularInvoice(final Account account, final LocalDate targetDate, final CallContext callContext) throws Exception {
final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class);
Mockito.when(subscription.getId()).thenReturn(UUID.randomUUID());
Mockito.when(subscription.getBundleId()).thenReturn(new UUID(0L, 0L));
final BillingEventSet events = new MockBillingEventSet();
final Plan plan = MockPlan.createBicycleNoTrialEvergreen1USD();
final PlanPhase planPhase = MockPlanPhase.create1USDMonthlyEvergreen();
final DateTime effectiveDate = new DateTime().minusDays(1);
final Currency currency = Currency.USD;
final BigDecimal fixedPrice = null;
events.add(createMockBillingEvent(account, subscription, effectiveDate, plan, planPhase, fixedPrice, BigDecimal.ONE, currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "", 1L, SubscriptionBaseTransitionType.CREATE));
Mockito.when(billingApi.getBillingEventsForAccountAndUpdateAccountBCD(Mockito.<UUID>any(), Mockito.<DryRunArguments>any(), Mockito.<InternalCallContext>any())).thenReturn(events);
final InvoiceNotifier invoiceNotifier = new NullInvoiceNotifier();
final InvoiceDispatcher dispatcher = new InvoiceDispatcher(generator, accountApi, billingApi, subscriptionApi, invoiceDao, internalCallContextFactory, invoiceNotifier, invoicePluginDispatcher, locker, busService.getBus(), null, invoiceConfig, clock, parkedAccountsManager);
Invoice invoice = dispatcher.processAccountFromNotificationOrBusEvent(account.getId(), targetDate, new DryRunFutureDateArguments(), internalCallContext);
Assert.assertNotNull(invoice);
final InternalCallContext context = internalCallContextFactory.createInternalCallContext(account.getId(), callContext);
List<InvoiceModelDao> invoices = invoiceDao.getInvoicesByAccount(context);
Assert.assertEquals(invoices.size(), 0);
invoice = dispatcher.processAccountFromNotificationOrBusEvent(account.getId(), targetDate, null, context);
Assert.assertNotNull(invoice);
invoices = invoiceDao.getInvoicesByAccount(context);
Assert.assertEquals(invoices.size(), 1);
return invoice.getId();
}
use of org.killbill.billing.junction.BillingEventSet in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testWithSingleMonthlyEvent.
@Test(groups = "fast")
public void testWithSingleMonthlyEvent() throws InvoiceApiException, CatalogApiException {
final BillingEventSet events = new MockBillingEventSet();
final SubscriptionBase sub = createSubscription();
final LocalDate startDate = invoiceUtil.buildDate(2011, 9, 1);
final Plan plan = new MockPlan();
final BigDecimal rate1 = TEN;
final PlanPhase phase = createMockMonthlyPlanPhase(rate1);
final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, 1);
events.add(event);
final LocalDate targetDate = invoiceUtil.buildDate(2011, 10, 3);
final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, targetDate, Currency.USD, internalCallContext);
final Invoice invoice = invoiceWithMetadata.getInvoice();
assertNotNull(invoice);
assertEquals(invoice.getNumberOfItems(), 2);
assertEquals(invoice.getBalance(), KillBillMoney.of(TWENTY, invoice.getCurrency()));
assertEquals(invoice.getInvoiceItems().get(0).getSubscriptionId(), sub.getId());
assertEquals(invoice.getInvoiceItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
assertEquals(invoice.getInvoiceItems().get(0).getStartDate(), new LocalDate(2011, 9, 1));
assertEquals(invoice.getInvoiceItems().get(0).getEndDate(), new LocalDate(2011, 10, 1));
assertEquals(invoice.getInvoiceItems().get(1).getInvoiceItemType(), InvoiceItemType.RECURRING);
assertEquals(invoice.getInvoiceItems().get(1).getStartDate(), new LocalDate(2011, 10, 1));
assertEquals(invoice.getInvoiceItems().get(1).getEndDate(), new LocalDate(2011, 11, 1));
}
use of org.killbill.billing.junction.BillingEventSet in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testWithSingleMonthlyEventWithLeadingProRation.
@Test(groups = "fast")
public void testWithSingleMonthlyEventWithLeadingProRation() throws InvoiceApiException, CatalogApiException {
final BillingEventSet events = new MockBillingEventSet();
final SubscriptionBase sub = createSubscription();
final LocalDate startDate = invoiceUtil.buildDate(2011, 9, 1);
final Plan plan = new MockPlan();
final BigDecimal rate = TEN;
final PlanPhase phase = createMockMonthlyPlanPhase(rate);
final BillingEvent event = createBillingEvent(sub.getId(), sub.getBundleId(), startDate, plan, phase, 15);
events.add(event);
final LocalDate targetDate = invoiceUtil.buildDate(2011, 10, 3);
final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, targetDate, Currency.USD, internalCallContext);
final Invoice invoice = invoiceWithMetadata.getInvoice();
assertNotNull(invoice);
assertEquals(invoice.getNumberOfItems(), 2);
final BigDecimal expectedNumberOfBillingCycles;
expectedNumberOfBillingCycles = ONE.add(FOURTEEN.divide(THIRTY_ONE, KillBillMoney.ROUNDING_METHOD));
final BigDecimal expectedAmount = KillBillMoney.of(expectedNumberOfBillingCycles.multiply(rate), invoice.getCurrency());
assertEquals(invoice.getBalance(), expectedAmount);
}
use of org.killbill.billing.junction.BillingEventSet 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);
}
Aggregations