Search in sources :

Example 26 with BillingPeriod

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;
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) InvoiceDateUtils.calculateProRationBeforeFirstBillingPeriod(org.killbill.billing.invoice.generator.InvoiceDateUtils.calculateProRationBeforeFirstBillingPeriod) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ArrayList(java.util.ArrayList) RecurringInvoiceItemData(org.killbill.billing.invoice.model.RecurringInvoiceItemData) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) InvalidDateSequenceException(org.killbill.billing.invoice.model.InvalidDateSequenceException) RecurringInvoiceItemDataWithNextBillingCycleDate(org.killbill.billing.invoice.model.RecurringInvoiceItemDataWithNextBillingCycleDate) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) BillingMode(org.killbill.billing.catalog.api.BillingMode) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 27 with BillingPeriod

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);
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) Currency(org.killbill.billing.catalog.api.Currency) AtomicLong(java.util.concurrent.atomic.AtomicLong) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) SubscriptionBaseTransitionType(org.killbill.billing.subscription.api.SubscriptionBaseTransitionType) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal)

Example 28 with BillingPeriod

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);
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SubscriptionBaseTransitionType(org.killbill.billing.subscription.api.SubscriptionBaseTransitionType) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Currency(org.killbill.billing.catalog.api.Currency) AtomicLong(java.util.concurrent.atomic.AtomicLong) PlanPhase(org.killbill.billing.catalog.api.PlanPhase)

Example 29 with BillingPeriod

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();
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) InternationalPrice(org.killbill.billing.catalog.api.InternationalPrice) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) Recurring(org.killbill.billing.catalog.api.Recurring) MockPlan(org.killbill.billing.catalog.MockPlan) Currency(org.killbill.billing.catalog.api.Currency) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PlanPhase(org.killbill.billing.catalog.api.PlanPhase)

Example 30 with BillingPeriod

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);
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Aggregations

BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)111 Test (org.testng.annotations.Test)91 LocalDate (org.joda.time.LocalDate)61 DateTime (org.joda.time.DateTime)59 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)45 BigDecimal (java.math.BigDecimal)44 Account (org.killbill.billing.account.api.Account)44 Invoice (org.killbill.billing.invoice.api.Invoice)40 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)31 Plan (org.killbill.billing.catalog.api.Plan)20 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)20 Interval (org.joda.time.Interval)17 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)17 UUID (java.util.UUID)15 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)15 ArrayList (java.util.ArrayList)14 Duration (org.killbill.billing.catalog.api.Duration)10 Entitlement (org.killbill.billing.entitlement.api.Entitlement)10 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)10 HashMap (java.util.HashMap)9