use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class FixedAndRecurringInvoiceItemGenerator method processRecurringEvent.
// Turn a set of events into a list of invoice items. Note that the dates on the invoice items will be rounded (granularity of a day)
@VisibleForTesting
List<InvoiceItem> processRecurringEvent(final UUID invoiceId, final UUID accountId, final BillingEvent thisEvent, @Nullable final BillingEvent nextEvent, final LocalDate targetDate, final Currency currency, final InvoiceItemGeneratorLogger invoiceItemGeneratorLogger, final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDates, final InternalCallContext internalCallContext) throws InvoiceApiException {
final List<InvoiceItem> items = new ArrayList<InvoiceItem>();
final LocalDate thisEventEffectiveDate = internalCallContext.toLocalDate(thisEvent.getEffectiveDate());
if (thisEventEffectiveDate.compareTo(targetDate) > 0) {
return items;
}
// Handle recurring items
final BillingPeriod billingPeriod = thisEvent.getBillingPeriod();
if (billingPeriod != BillingPeriod.NO_BILLING_PERIOD) {
final Plan currentPlan = thisEvent.getPlan();
Preconditions.checkNotNull(currentPlan, "Unexpected null Plan name event = %s", thisEvent);
final BillingMode recurringBillingMode = currentPlan.getRecurringBillingMode();
final LocalDate startDate = thisEventEffectiveDate;
if (!startDate.isAfter(targetDate)) {
final LocalDate endDate = (nextEvent == null) ? null : internalCallContext.toLocalDate(nextEvent.getEffectiveDate());
final int billCycleDayLocal = thisEvent.getBillCycleDayLocal();
final RecurringInvoiceItemDataWithNextBillingCycleDate itemDataWithNextBillingCycleDate;
try {
itemDataWithNextBillingCycleDate = generateInvoiceItemData(startDate, endDate, targetDate, billCycleDayLocal, billingPeriod, recurringBillingMode);
} catch (final InvalidDateSequenceException e) {
throw new InvoiceApiException(ErrorCode.INVOICE_INVALID_DATE_SEQUENCE, startDate, endDate, targetDate);
}
for (final RecurringInvoiceItemData itemDatum : itemDataWithNextBillingCycleDate.getItemData()) {
final BigDecimal rate = thisEvent.getRecurringPrice();
if (rate != null) {
final BigDecimal amount = KillBillMoney.of(itemDatum.getNumberOfCycles().multiply(rate), currency);
final DateTime catalogEffectiveDate = thisEvent.getCatalogEffectiveDate() != null ? thisEvent.getCatalogEffectiveDate() : null;
final RecurringInvoiceItem recurringItem = new RecurringInvoiceItem(invoiceId, accountId, thisEvent.getBundleId(), thisEvent.getSubscriptionId(), currentPlan.getProduct().getName(), currentPlan.getName(), thisEvent.getPlanPhase().getName(), catalogEffectiveDate, itemDatum.getStartDate(), itemDatum.getEndDate(), amount, rate, currency);
items.add(recurringItem);
}
}
updatePerSubscriptionNextNotificationDate(thisEvent.getSubscriptionId(), itemDataWithNextBillingCycleDate.getNextBillingCycleDate(), items, recurringBillingMode, perSubscriptionFutureNotificationDates);
}
} else {
final SubscriptionFutureNotificationDates futureNotificationDates = perSubscriptionFutureNotificationDates.get(thisEvent.getSubscriptionId());
if (futureNotificationDates != null) {
futureNotificationDates.clearNextRecurringDate();
}
}
// For debugging purposes
invoiceItemGeneratorLogger.append(thisEvent, items);
return items;
}
use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class BlockingCalculator method createNewDisableEvent.
protected BillingEvent createNewDisableEvent(final DateTime disabledDurationStart, final BillingEvent previousEvent) {
final int billCycleDay = previousEvent.getBillCycleDayLocal();
final DateTime effectiveDate = disabledDurationStart;
final PlanPhase planPhase = previousEvent.getPlanPhase();
final Plan plan = previousEvent.getPlan();
// Make sure to set the fixed price to null and the billing period to NO_BILLING_PERIOD,
// which makes invoice disregard this event
final BigDecimal fixedPrice = null;
final BigDecimal recurringPrice = null;
final BillingPeriod billingPeriod = BillingPeriod.NO_BILLING_PERIOD;
final Currency currency = previousEvent.getCurrency();
final String description = "";
final SubscriptionBaseTransitionType type = SubscriptionBaseTransitionType.START_BILLING_DISABLED;
final Long totalOrdering = globaltotalOrder.getAndIncrement();
return new DefaultBillingEvent(previousEvent.getSubscriptionId(), previousEvent.getBundleId(), effectiveDate, plan, planPhase, fixedPrice, recurringPrice, ImmutableList.of(), currency, billingPeriod, billCycleDay, description, totalOrdering, type, true);
}
use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class BlockingCalculator method createNewReenableEvent.
protected BillingEvent createNewReenableEvent(final DateTime odEventTime, final BillingEvent previousEvent) throws CatalogApiException {
// All fields are populated with the event state from before the blocking period, for invoice to resume invoicing
final int billCycleDay = previousEvent.getBillCycleDayLocal();
final DateTime effectiveDate = odEventTime;
final PlanPhase planPhase = previousEvent.getPlanPhase();
final BigDecimal fixedPrice = previousEvent.getFixedPrice();
final BigDecimal recurringPrice = previousEvent.getRecurringPrice();
final List<Usage> usages = previousEvent.getUsages();
final Plan plan = previousEvent.getPlan();
final Currency currency = previousEvent.getCurrency();
final String description = "";
final BillingPeriod billingPeriod = previousEvent.getBillingPeriod();
final SubscriptionBaseTransitionType type = SubscriptionBaseTransitionType.END_BILLING_DISABLED;
final Long totalOrdering = globaltotalOrder.getAndIncrement();
return new DefaultBillingEvent(previousEvent.getSubscriptionId(), previousEvent.getBundleId(), effectiveDate, plan, planPhase, fixedPrice, recurringPrice, usages, currency, billingPeriod, billCycleDay, description, totalOrdering, type, false);
}
use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class TestBlockingCalculator method createRealEvent.
private BillingEvent createRealEvent(final SubscriptionBase subscription, final DateTime effectiveDate, final SubscriptionBaseTransitionType type, final Long totalOrdering) {
try {
final Integer billCycleDay = 1;
final Plan plan = new MockPlan();
final Currency currency = Currency.USD;
final String description = "";
final BillingPeriod billingPeriod = BillingPeriod.MONTHLY;
final PlanPhase planPhase = Mockito.mock(PlanPhase.class);
final InternationalPrice internationalPrice = Mockito.mock(InternationalPrice.class);
Mockito.when(internationalPrice.getPrice(Mockito.<Currency>any())).thenReturn(BigDecimal.TEN);
final Recurring recurring = Mockito.mock(Recurring.class);
Mockito.when(recurring.getRecurringPrice()).thenReturn(internationalPrice);
Mockito.when(planPhase.getRecurring()).thenReturn(recurring);
Mockito.when(planPhase.getUsages()).thenReturn(new DefaultUsage[0]);
final BigDecimal fixedPrice = BigDecimal.TEN;
final BigDecimal recurringPrice = BigDecimal.TEN;
return new DefaultBillingEvent(subscription.getId(), subscription.getBundleId(), effectiveDate, plan, planPhase, fixedPrice, recurringPrice, ImmutableList.of(), currency, billingPeriod, billCycleDay, description, totalOrdering, type, false);
} catch (final CatalogApiException e) {
Assert.fail("", e);
}
throw new IllegalStateException();
}
use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class TestRawUsageOptimizer method testGetBillingPeriodMinDate2.
@Test(groups = "fast")
public void testGetBillingPeriodMinDate2() {
// Normal scenario where we are up to date when billing in-arrear
final LocalDate today = clock.getUTCToday();
final LocalDate targetDate = today;
final Map<BillingPeriod, LocalDate> res = rawUsageOptimizer.getBillingPeriodMinDate2(ImmutableList.of(BillingPeriod.MONTHLY), targetDate);
Assert.assertEquals(res.size(), 1);
//
// We expect to return 1 BP back in time from where we are.
// Note that if there a org.killbill.invoice.readMaxRawUsagePreviousPeriod > 0 this is taken into account in the caller
// of the getBillingPeriodMinDate2 so we don't see it.
//
Assert.assertEquals(res.get(BillingPeriod.MONTHLY).compareTo(today.minusMonths(1)), 0);
}
Aggregations