use of org.killbill.billing.invoice.model.DefaultInvoice in project killbill by killbill.
the class TestInvoiceDao method testRetrieveInvoiceItemsByParentInvoice.
@Test(groups = "slow")
public void testRetrieveInvoiceItemsByParentInvoice() throws InvoiceApiException, EntityPersistenceException {
final UUID childAccountId = account.getId();
final Invoice childInvoice = new DefaultInvoice(childAccountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
final UUID invoiceId = childInvoice.getId();
final UUID subscriptionId = UUID.randomUUID();
final UUID bundleId = UUID.randomUUID();
final LocalDate startDate = new LocalDate(2010, 1, 1);
final LocalDate endDate = new LocalDate(2010, 4, 1);
final InvoiceItem invoiceItem = new RecurringInvoiceItem(invoiceId, childAccountId, bundleId, subscriptionId, "test product", "test plan", "test phase", null, startDate, endDate, new BigDecimal("21.00"), new BigDecimal("7.00"), Currency.USD);
final InvoiceItem invoiceAdj = new ItemAdjInvoiceItem(invoiceItem, startDate, new BigDecimal("-5.00"), Currency.USD);
childInvoice.addInvoiceItem(invoiceItem);
childInvoice.addInvoiceItem(invoiceAdj);
invoiceUtil.createInvoice(childInvoice, context);
final UUID parentInvoiceId = UUID.randomUUID();
InvoiceParentChildModelDao invoiceRelation = new InvoiceParentChildModelDao(parentInvoiceId, childInvoice.getId(), childAccountId);
invoiceDao.createParentChildInvoiceRelation(invoiceRelation, context);
final List<InvoiceItemModelDao> invoiceItems = invoiceDao.getInvoiceItemsByParentInvoice(parentInvoiceId, context);
assertEquals(invoiceItems.size(), 2);
assertEquals(invoiceItems.get(0).getType(), InvoiceItemType.RECURRING);
assertEquals(invoiceItems.get(1).getType(), InvoiceItemType.ITEM_ADJ);
}
use of org.killbill.billing.invoice.model.DefaultInvoice 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);
}
Aggregations