Search in sources :

Example 21 with SubscriptionBaseError

use of org.killbill.billing.subscription.exceptions.SubscriptionBaseError in project killbill by killbill.

the class PlanAligner method getTimedPhaseOnChange.

private TimedPhase getTimedPhaseOnChange(final DateTime subscriptionStartDate, final DateTime bundleStartDate, final PlanPhase currentPhase, final Plan currentPlan, final Plan nextPlan, final DateTime effectiveDate, final DateTime catalogEffectiveDate, final DateTime lastOrCurrentChangeEffectiveDate, final PhaseType originalInitialPhase, @Nullable final PhaseType newPlanInitialPhaseType, final WhichPhase which, final SubscriptionCatalog catalog, final InternalTenantContext context) throws CatalogApiException, SubscriptionBaseApiException {
    final PlanPhaseSpecifier fromPlanPhaseSpecifier = new PlanPhaseSpecifier(currentPlan.getName(), currentPhase.getPhaseType());
    final PlanSpecifier toPlanSpecifier = new PlanSpecifier(nextPlan.getName());
    final PhaseType initialPhase;
    final DateTime planStartDate;
    final PlanAlignmentChange alignment = catalog.getPlanChangeResult(fromPlanPhaseSpecifier, toPlanSpecifier, catalogEffectiveDate).getAlignment();
    switch(alignment) {
        case START_OF_SUBSCRIPTION:
            planStartDate = subscriptionStartDate;
            initialPhase = newPlanInitialPhaseType != null ? newPlanInitialPhaseType : (isPlanContainPhaseType(nextPlan, originalInitialPhase) ? originalInitialPhase : null);
            break;
        case START_OF_BUNDLE:
            planStartDate = bundleStartDate;
            initialPhase = newPlanInitialPhaseType != null ? newPlanInitialPhaseType : (isPlanContainPhaseType(nextPlan, originalInitialPhase) ? originalInitialPhase : null);
            break;
        case CHANGE_OF_PLAN:
            planStartDate = lastOrCurrentChangeEffectiveDate;
            initialPhase = newPlanInitialPhaseType;
            break;
        case CHANGE_OF_PRICELIST:
            throw new SubscriptionBaseError(String.format("Not implemented yet %s", alignment));
        default:
            throw new SubscriptionBaseError(String.format("Unknown PlanAlignmentChange %s", alignment));
    }
    final List<TimedPhase> timedPhases = getPhaseAlignments(nextPlan, initialPhase, planStartDate);
    return getTimedPhase(timedPhases, effectiveDate, which);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) PlanAlignmentChange(org.killbill.billing.catalog.api.PlanAlignmentChange) PhaseType(org.killbill.billing.catalog.api.PhaseType) DateTime(org.joda.time.DateTime) PlanSpecifier(org.killbill.billing.catalog.api.PlanSpecifier)

Example 22 with SubscriptionBaseError

use of org.killbill.billing.subscription.exceptions.SubscriptionBaseError in project killbill by killbill.

the class PlanAligner method getPhaseAlignments.

private List<TimedPhase> getPhaseAlignments(final Plan plan, @Nullable final PhaseType initialPhase, final DateTime initialPhaseStartDate) throws SubscriptionBaseApiException {
    if (plan == null) {
        return Collections.emptyList();
    }
    final List<TimedPhase> result = new LinkedList<TimedPhase>();
    DateTime curPhaseStart = (initialPhase == null) ? initialPhaseStartDate : null;
    DateTime nextPhaseStart;
    for (final PlanPhase cur : plan.getAllPhases()) {
        // For create we can specify the phase so skip any phase until we reach initialPhase
        if (curPhaseStart == null) {
            if (initialPhase != cur.getPhaseType()) {
                continue;
            }
            curPhaseStart = initialPhaseStartDate;
        }
        result.add(new TimedPhase(cur, curPhaseStart));
        // STEPH check for duration null instead TimeUnit UNLIMITED
        if (cur.getPhaseType() != PhaseType.EVERGREEN) {
            final Duration curPhaseDuration = cur.getDuration();
            nextPhaseStart = addDuration(curPhaseStart, curPhaseDuration);
            if (nextPhaseStart == null) {
                throw new SubscriptionBaseError(String.format("Unexpected non ending UNLIMITED phase for plan %s", plan.getName()));
            }
            curPhaseStart = nextPhaseStart;
        }
    }
    if (initialPhase != null && curPhaseStart == null) {
        throw new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_BAD_PHASE, initialPhase);
    }
    return result;
}
Also used : SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Duration(org.killbill.billing.catalog.api.Duration) LinkedList(java.util.LinkedList) DateTime(org.joda.time.DateTime) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Aggregations

SubscriptionBaseError (org.killbill.billing.subscription.exceptions.SubscriptionBaseError)22 DateTime (org.joda.time.DateTime)11 Plan (org.killbill.billing.catalog.api.Plan)7 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)6 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)6 ArrayList (java.util.ArrayList)5 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)5 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)5 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)4 PlanSpecifier (org.killbill.billing.catalog.api.PlanSpecifier)4 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)4 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)4 PhaseEvent (org.killbill.billing.subscription.events.phase.PhaseEvent)4 UUID (java.util.UUID)3 Catalog (org.killbill.billing.catalog.api.Catalog)3 SubscriptionBuilder (org.killbill.billing.subscription.api.user.SubscriptionBuilder)3 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2 LocalDate (org.joda.time.LocalDate)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)2