Search in sources :

Example 1 with BillingAlignment

use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.

the class DefaultSubscriptionInternalApi method getDefaultBillCycleDayLocal.

@Override
public int getDefaultBillCycleDayLocal(final Map<UUID, Integer> bcdCache, final SubscriptionBase subscription, final SubscriptionBase baseSubscription, final PlanPhaseSpecifier planPhaseSpecifier, final DateTimeZone accountTimeZone, final int accountBillCycleDayLocal, final DateTime effectiveDate, final InternalTenantContext context) throws SubscriptionBaseApiException {
    try {
        final Catalog catalog = catalogService.getFullCatalog(true, true, context);
        final BillingAlignment alignment = catalog.billingAlignment(planPhaseSpecifier, effectiveDate);
        return BillCycleDayCalculator.calculateBcdForAlignment(bcdCache, subscription, baseSubscription, alignment, accountTimeZone, accountBillCycleDayLocal);
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) BillingAlignment(org.killbill.billing.catalog.api.BillingAlignment) Catalog(org.killbill.billing.catalog.api.Catalog) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 2 with BillingAlignment

use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method cancelWithPolicyNoValidation.

@Override
public boolean cancelWithPolicyNoValidation(final Iterable<DefaultSubscriptionBase> subscriptions, final BillingActionPolicy policy, final DateTimeZone accountTimeZone, final int accountBillCycleDayLocal, final InternalCallContext context) throws SubscriptionBaseApiException {
    final Map<DefaultSubscriptionBase, DateTime> subscriptionsWithEffectiveDate = new HashMap<DefaultSubscriptionBase, DateTime>();
    final DateTime now = clock.getUTCNow();
    try {
        for (final DefaultSubscriptionBase subscription : subscriptions) {
            final BillingAlignment billingAlignment = (subscription.getState() == EntitlementState.PENDING ? null : catalogService.getFullCatalog(true, true, context).billingAlignment(new PlanPhaseSpecifier(subscription.getLastActivePlan().getName(), subscription.getLastActivePhase().getPhaseType()), clock.getUTCNow()));
            final DateTime effectiveDate = subscription.getPlanChangeEffectiveDate(policy, billingAlignment, accountTimeZone, accountBillCycleDayLocal, context);
            subscriptionsWithEffectiveDate.put(subscription, effectiveDate);
        }
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
    return doCancelPlan(subscriptionsWithEffectiveDate, now, context);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) HashMap(java.util.HashMap) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) BillingAlignment(org.killbill.billing.catalog.api.BillingAlignment) DateTime(org.joda.time.DateTime) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 3 with BillingAlignment

use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.

the class DefaultInternalBillingApi method addBillingEventsForSubscription.

private void addBillingEventsForSubscription(final ImmutableAccountData account, @Nullable final List<SubscriptionBase> subscriptions, final SubscriptionBase baseSubscription, final int currentAccountBCD, final InternalCallContext context, final DefaultBillingEventSet result, final Set<UUID> skipSubscriptionsSet, final VersionedCatalog catalog) throws SubscriptionBaseApiException, CatalogApiException {
    if (subscriptions == null) {
        return;
    }
    final Map<UUID, Integer> bcdCache = new HashMap<UUID, Integer>();
    for (final SubscriptionBase subscription : subscriptions) {
        // TODO Can we batch those ?
        final List<SubscriptionBillingEvent> billingTransitions = subscriptionApi.getSubscriptionBillingEvents(catalog, subscription, context);
        if (billingTransitions.isEmpty() || (billingTransitions.get(0).getType() != SubscriptionBaseTransitionType.CREATE && billingTransitions.get(0).getType() != SubscriptionBaseTransitionType.TRANSFER)) {
            log.warn("Skipping billing events for subscription " + subscription.getId() + ": Does not start with a valid CREATE transition");
            skipSubscriptionsSet.add(subscription.getId());
            return;
        }
        Integer overridenBCD = null;
        int bcdLocal = 0;
        BillingAlignment alignment = null;
        for (final SubscriptionBillingEvent transition : billingTransitions) {
            if (transition.getType() != SubscriptionBaseTransitionType.CANCEL) {
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier(transition.getPlan().getName(), transition.getPlanPhase().getPhaseType());
                alignment = subscription.getBillingAlignment(spec, transition.getEffectiveDate(), catalog);
                // 
                // A BCD_CHANGE transition defines a new billCycleDayLocal for the subscription and this overrides whatever computation
                // occurs below (which is based on billing alignment policy). Also multiple of those BCD_CHANGE transitions could occur,
                // to define different intervals with different billing cycle days.
                // 
                overridenBCD = transition.getBcdLocal() != null ? transition.getBcdLocal() : overridenBCD;
                bcdLocal = overridenBCD != null ? overridenBCD : calculateBcdForTransition(alignment, bcdCache, baseSubscription, subscription, currentAccountBCD, context);
            }
            final BillingEvent event = new DefaultBillingEvent(transition, subscription, bcdLocal, alignment, account.getCurrency());
            result.add(event);
        }
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) HashMap(java.util.HashMap) BillingAlignment(org.killbill.billing.catalog.api.BillingAlignment) SubscriptionBillingEvent(org.killbill.billing.subscription.api.user.SubscriptionBillingEvent) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) SubscriptionBillingEvent(org.killbill.billing.subscription.api.user.SubscriptionBillingEvent)

Example 4 with BillingAlignment

use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.

the class DefaultSubscriptionBase method getBillingAlignment.

@Override
public BillingAlignment getBillingAlignment(final PlanPhaseSpecifier spec, final DateTime transitionTime, final VersionedCatalog publicCatalog) throws SubscriptionBaseApiException {
    try {
        final SubscriptionCatalog catalog = DefaultSubscriptionCatalogApi.wrapCatalog(publicCatalog, clock);
        final SubscriptionBaseTransition transition = (getState() == EntitlementState.PENDING) ? getPendingTransition() : getLastTransitionForCurrentPlan();
        final BillingAlignment alignment = catalog.billingAlignment(spec, transitionTime, transition.getEffectiveTransitionTime());
        return alignment;
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) BillingAlignment(org.killbill.billing.catalog.api.BillingAlignment) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog)

Example 5 with BillingAlignment

use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.

the class EventsStreamBuilder method buildForEntitlement.

private EventsStream buildForEntitlement(final ImmutableAccountData account, final SubscriptionBaseBundle bundle, @Nullable final SubscriptionBase baseSubscription, final SubscriptionBase subscription, final Collection<SubscriptionBase> allSubscriptionsForBundle, final Collection<BlockingState> blockingStates, final int accountBCD, final Map<UUID, Integer> bcdCache, final VersionedCatalog catalog, final InternalTenantContext internalTenantContext) throws EntitlementApiException {
    try {
        Integer defaultAlignmentDay = null;
        final BillingAlignment alignment = subscription.getBillingAlignment(createPlanPhaseSpecifier(subscription), clock.getUTCNow(), catalog);
        if (alignment != BillingAlignment.ACCOUNT || accountBCD != 0) {
            defaultAlignmentDay = BillCycleDayCalculator.calculateBcdForAlignment(bcdCache, subscription, baseSubscription, alignment, internalTenantContext, accountBCD);
        }
        return new DefaultEventsStream(account, bundle, blockingStates, checker, baseSubscription, subscription, allSubscriptionsForBundle, defaultAlignmentDay, internalTenantContext, clock.getUTCNow());
    } catch (final SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    }
}
Also used : BillingAlignment(org.killbill.billing.catalog.api.BillingAlignment) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Aggregations

BillingAlignment (org.killbill.billing.catalog.api.BillingAlignment)6 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)4 HashMap (java.util.HashMap)3 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)3 DateTime (org.joda.time.DateTime)2 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)2 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)2 UUID (java.util.UUID)1 AccountApiException (org.killbill.billing.account.api.AccountApiException)1 Catalog (org.killbill.billing.catalog.api.Catalog)1 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)1 BillingEvent (org.killbill.billing.junction.BillingEvent)1 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)1 SubscriptionBillingEvent (org.killbill.billing.subscription.api.user.SubscriptionBillingEvent)1 SubscriptionCatalog (org.killbill.billing.subscription.catalog.SubscriptionCatalog)1