Search in sources :

Example 1 with AccountInvoicesExp

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp in project killbill by killbill.

the class TestFixedAndRecurringInvoiceItemGeneratorWithOptimization method testFixedPrice.

@Test(groups = "fast")
public void testFixedPrice() throws InvoiceApiException {
    final BillingEventSet events = new MockBillingEventSet();
    final BigDecimal fixedPriceAmount = BigDecimal.TEN;
    final MockInternationalPrice fixedPrice = new MockInternationalPrice(new DefaultPrice(fixedPriceAmount, Currency.USD));
    final Plan plan = new MockPlan("my-plan");
    final PlanPhase phase = new MockPlanPhase(null, fixedPrice, BillingPeriod.NO_BILLING_PERIOD, PhaseType.TRIAL);
    final DateTime startDate = new DateTime("2020-01-01");
    final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, startDate, plan, phase, fixedPriceAmount, null, Currency.USD, BillingPeriod.NO_BILLING_PERIOD, 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.FIXED);
    assertEquals(result1.getItems().get(0).getStartDate().compareTo(new LocalDate("2020-01-01")), 0);
    // One month after invoicing, optimization does not kick-in yet
    // There is an existing invoice
    // => Should not regenerate the item
    // 2020-02-01
    final LocalDate targetDate2 = startDate.toLocalDate().plusMonths(1);
    final LocalDate cuttoffDate2 = targetDate2.minus(maxInvoiceLimit);
    final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), targetDate2, account.getCurrency());
    invoice.addInvoiceItem(new FixedPriceInvoiceItem(UUID.randomUUID(), clock.getUTCNow(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), null, event.getPlan().getName(), event.getPlanPhase().getName(), null, "Buggy fixed item", startDate.toLocalDate(), fixedPriceAmount, account.getCurrency()));
    final AccountInvoices existingInvoices2 = new AccountInvoicesExp(cuttoffDate2, null, ImmutableList.of(invoice));
    final InvoiceGeneratorResult result2 = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices2, targetDate2, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), null, internalCallContext);
    assertEquals(result2.getItems().size(), 0);
    // Two month after invoicing, optimization *does* not kick-in
    // There is no existing invoice (optimization removed it)
    // => Should not regenerate the item
    // 2020-03-01;
    final LocalDate targetDate3 = startDate.toLocalDate().plusMonths(2);
    final LocalDate cuttoffDate3 = targetDate3.minus(maxInvoiceLimit);
    final AccountInvoices existingInvoices3 = new AccountInvoicesExp(cuttoffDate3, null, ImmutableList.of());
    final InvoiceGeneratorResult result3 = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices3, targetDate3, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), null, internalCallContext);
    assertEquals(result3.getItems().size(), 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) Period(org.joda.time.Period) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) 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) BigDecimal(java.math.BigDecimal) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) DateTime(org.joda.time.DateTime) 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)

Example 2 with AccountInvoicesExp

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp in project killbill by killbill.

the class TestFixedAndRecurringInvoiceItemGeneratorWithOptimization method testRecurringInArrear.

@Test(groups = "fast")
public void testRecurringInArrear() 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 MockPlan plan = new MockPlan("my-plan");
    plan.setRecurringBillingMode(BillingMode.IN_ARREAR);
    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_ARREAR, "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 nothing to 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(), 0);
    // Initial invoicing (targetDate2  = startDate + 1 month)
    // There is no existing invoice
    // => Should generate the item
    // 2020-02-01
    final LocalDate targetDate2 = startDate.plusMonths(1).toLocalDate();
    final LocalDate cuttoffDate2 = targetDate2.minus(maxInvoiceLimit);
    final AccountInvoices existingInvoices2 = new AccountInvoicesExp(cuttoffDate2, null, ImmutableList.of());
    final InvoiceGeneratorResult result2 = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices2, targetDate2, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), null, internalCallContext);
    assertEquals(result2.getItems().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(result2.getItems().get(0).getStartDate().compareTo(new LocalDate("2020-01-01")), 0);
    assertEquals(result2.getItems().get(0).getEndDate().compareTo(new LocalDate("2020-02-01")), 0);
    // 2020-03-01
    final LocalDate targetDate3 = startDate.plusMonths(2).toLocalDate();
    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.toLocalDate(), startDate.plusMonths(1).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().get(0).getInvoiceItemType(), InvoiceItemType.RECURRING);
    assertEquals(result3.getItems().get(0).getStartDate().compareTo(new LocalDate("2020-02-01")), 0);
    assertEquals(result3.getItems().get(0).getEndDate().compareTo(new LocalDate("2020-03-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) 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)

Example 3 with AccountInvoicesExp

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp in project killbill by killbill.

the class TestInvoiceOptimizerExp method testInArrearP0M.

@Test(groups = "fast")
public void testInArrearP0M() {
    final LocalDate startDate = new LocalDate(2021, 2, 1);
    LocalDate targetDate = new LocalDate(2021, 6, 1);
    // Proposed: invoice from 2021-2-1 -> 2021-6-1
    final List<InvoiceItem> proposedItems = new ArrayList<InvoiceItem>();
    final Invoice proposed = createInvoice(targetDate);
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, startDate, new LocalDate(2021, 3, 1)));
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, new LocalDate(2021, 3, 1), new LocalDate(2021, 4, 1)));
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, new LocalDate(2021, 4, 1), new LocalDate(2021, 5, 1)));
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, new LocalDate(2021, 5, 1), new LocalDate(2021, 6, 1)));
    // P0M
    final LocalDate cutoffDate = targetDate;
    // Existing: invoice from 2021-2-1 -> 2021-5-1
    // Existing (filtered) empty
    final List<Invoice> existing = new ArrayList<Invoice>();
    final Invoice invoice = createInvoice(cutoffDate);
    existing.add(invoice);
    final AccountInvoicesExp test = new AccountInvoicesExp(cutoffDate, null, existing);
    final DefaultBillingEventSet billingEvents = new DefaultBillingEventSet(false, false, false);
    billingEvents.add(createBillingEvent(startDate, BillingMode.IN_ARREAR, SubscriptionBaseTransitionType.CREATE));
    test.filterProposedItems(proposedItems, billingEvents, internalCallContext);
    Assert.assertEquals(proposedItems.size(), 1);
    // New proposed item
    Assert.assertEquals(proposedItems.get(0).getStartDate(), new LocalDate(2021, 5, 1));
    Assert.assertEquals(proposedItems.get(0).getEndDate(), new LocalDate(2021, 6, 1));
}
Also used : RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) ArrayList(java.util.ArrayList) DefaultBillingEventSet(org.killbill.billing.junction.plumbing.billing.DefaultBillingEventSet) LocalDate(org.joda.time.LocalDate) AccountInvoicesExp(org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp) Test(org.testng.annotations.Test)

Example 4 with AccountInvoicesExp

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp in project killbill by killbill.

the class TestInvoiceOptimizerExp method testInArrearLateP1M.

@Test(groups = "fast")
public void testInArrearLateP1M() {
    final LocalDate startDate = new LocalDate(2021, 2, 1);
    LocalDate targetDate = new LocalDate(2021, 6, 1);
    // Proposed: invoice from 2021-2-1 -> 2021-6-1
    final List<InvoiceItem> proposedItems = new ArrayList<InvoiceItem>();
    final Invoice proposed = createInvoice(targetDate);
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, startDate, new LocalDate(2021, 3, 1)));
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, new LocalDate(2021, 3, 1), new LocalDate(2021, 4, 1)));
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, new LocalDate(2021, 4, 1), new LocalDate(2021, 5, 1)));
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, new LocalDate(2021, 5, 1), new LocalDate(2021, 6, 1)));
    // P1M
    // We assume we did not bill last N period so there is no filtered
    final LocalDate cutoffDate = targetDate.minusMonths(1);
    // Existing: empty
    // Existing (filtered) empty
    final List<Invoice> existing = new ArrayList<Invoice>();
    final Invoice invoice = createInvoice(cutoffDate);
    existing.add(invoice);
    final AccountInvoicesExp test = new AccountInvoicesExp(cutoffDate, null, existing);
    final DefaultBillingEventSet billingEvents = new DefaultBillingEventSet(false, false, false);
    billingEvents.add(createBillingEvent(startDate, BillingMode.IN_ARREAR, SubscriptionBaseTransitionType.CREATE));
    // 
    // Output is similar to previous test 'testInArrearP1M', i.e we have a proposed
    // for last period and new one (but because there is no existing for last period)
    // it would remain when coming back from the tree
    // 
    test.filterProposedItems(proposedItems, billingEvents, internalCallContext);
    Assert.assertEquals(proposedItems.size(), 2);
    // Latest existing (P1M)
    Assert.assertEquals(proposedItems.get(0).getStartDate(), new LocalDate(2021, 4, 1));
    Assert.assertEquals(proposedItems.get(0).getEndDate(), new LocalDate(2021, 5, 1));
    // New proposed item
    Assert.assertEquals(proposedItems.get(1).getStartDate(), new LocalDate(2021, 5, 1));
    Assert.assertEquals(proposedItems.get(1).getEndDate(), new LocalDate(2021, 6, 1));
}
Also used : RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) ArrayList(java.util.ArrayList) DefaultBillingEventSet(org.killbill.billing.junction.plumbing.billing.DefaultBillingEventSet) LocalDate(org.joda.time.LocalDate) AccountInvoicesExp(org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp) Test(org.testng.annotations.Test)

Example 5 with AccountInvoicesExp

use of org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp in project killbill by killbill.

the class TestInvoiceOptimizerExp method testInAdvanceP0M.

@Test(groups = "fast")
public void testInAdvanceP0M() {
    final LocalDate startDate = new LocalDate(2021, 2, 1);
    LocalDate targetDate = new LocalDate(2021, 5, 1);
    // Proposed: invoice from 2021-2-1 -> 2021-6-1
    final List<InvoiceItem> proposedItems = new ArrayList<InvoiceItem>();
    final Invoice proposed = createInvoice(targetDate);
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, startDate, new LocalDate(2021, 3, 1)));
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, new LocalDate(2021, 3, 1), new LocalDate(2021, 4, 1)));
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, new LocalDate(2021, 4, 1), new LocalDate(2021, 5, 1)));
    proposedItems.add(createItem(proposed.getId(), BigDecimal.TEN, BigDecimal.TEN, new LocalDate(2021, 5, 1), new LocalDate(2021, 6, 1)));
    // P0M
    final LocalDate cutoffDate = targetDate;
    // Existing: invoice from 2021-2-1 -> 2021-5-1
    // Existing (filtered) empty
    final List<Invoice> existing = new ArrayList<Invoice>();
    final Invoice invoice = createInvoice(cutoffDate);
    existing.add(invoice);
    final DefaultBillingEventSet billingEvents = new DefaultBillingEventSet(false, false, false);
    billingEvents.add(createBillingEvent(startDate, BillingMode.IN_ADVANCE, SubscriptionBaseTransitionType.CREATE));
    final AccountInvoicesExp test = new AccountInvoicesExp(cutoffDate, null, existing);
    test.filterProposedItems(proposedItems, billingEvents, internalCallContext);
    Assert.assertEquals(proposedItems.size(), 1);
    // New proposed item
    Assert.assertEquals(proposedItems.get(0).getStartDate(), new LocalDate(2021, 5, 1));
    Assert.assertEquals(proposedItems.get(0).getEndDate(), new LocalDate(2021, 6, 1));
}
Also used : RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) ArrayList(java.util.ArrayList) DefaultBillingEventSet(org.killbill.billing.junction.plumbing.billing.DefaultBillingEventSet) LocalDate(org.joda.time.LocalDate) AccountInvoicesExp(org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp) Test(org.testng.annotations.Test)

Aggregations

LocalDate (org.joda.time.LocalDate)13 Invoice (org.killbill.billing.invoice.api.Invoice)13 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)13 AccountInvoicesExp (org.killbill.billing.invoice.optimizer.InvoiceOptimizerExp.AccountInvoicesExp)13 Test (org.testng.annotations.Test)13 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)12 ArrayList (java.util.ArrayList)10 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)10 DefaultBillingEventSet (org.killbill.billing.junction.plumbing.billing.DefaultBillingEventSet)10 BigDecimal (java.math.BigDecimal)6 UUID (java.util.UUID)3 DateTime (org.joda.time.DateTime)3 Period (org.joda.time.Period)3 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)3 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)3 MockPlan (org.killbill.billing.catalog.MockPlan)3 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)3 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)3 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)3 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)3