use of org.killbill.billing.junction.BillingEventSet 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.junction.BillingEventSet 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.junction.BillingEventSet 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.junction.BillingEventSet in project killbill by killbill.
the class TestDefaultInvoiceGenerator method testWithEmptyEventSet.
@Test(groups = "fast")
public void testWithEmptyEventSet() throws InvoiceApiException {
final BillingEventSet events = new MockBillingEventSet();
final InvoiceWithMetadata invoiceWithMetadata = generator.generateInvoice(account, events, null, clock.getUTCToday(), Currency.USD, internalCallContext);
assertNull(invoiceWithMetadata.getInvoice());
}
use of org.killbill.billing.junction.BillingEventSet 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