Search in sources :

Example 11 with PhaseType

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

the class DefaultSubscriptionBaseTimeline method toExistingEvents.

private List<ExistingEvent> toExistingEvents(final SubscriptionCatalog catalog, final ProductCategory category, final List<SubscriptionBaseEvent> events) throws CatalogApiException {
    final List<ExistingEvent> result = new LinkedList<SubscriptionBaseTimeline.ExistingEvent>();
    Plan prevPlan = null;
    String prevProductName = null;
    String prevPriceListName = null;
    PhaseType prevPhaseType = null;
    DateTime startDate = null;
    for (final SubscriptionBaseEvent cur : events) {
        if (!cur.isActive()) {
            continue;
        }
        startDate = (startDate == null) ? cur.getEffectiveDate() : startDate;
        String productName = null;
        String priceListName = null;
        PhaseType phaseType = null;
        Plan plan = null;
        String planPhaseName = null;
        Integer billCycleDayLocal = null;
        ApiEventType apiType = null;
        switch(cur.getType()) {
            case PHASE:
                final PhaseEvent phaseEV = (PhaseEvent) cur;
                planPhaseName = phaseEV.getPhase();
                // A PHASE event always occurs within the same plan (and is never the first event)
                phaseType = prevPlan != null ? prevPlan.findPhase(phaseEV.getPhase()).getPhaseType() : null;
                plan = prevPlan;
                productName = prevProductName;
                priceListName = prevPriceListName;
                break;
            case BCD_UPDATE:
                final BCDEvent bcdEvent = (BCDEvent) cur;
                billCycleDayLocal = bcdEvent.getBillCycleDayLocal();
                break;
            case API_USER:
                final ApiEvent userEV = (ApiEvent) cur;
                apiType = userEV.getApiEventType();
                planPhaseName = userEV.getEventPlanPhase();
                plan = (userEV.getEventPlan() != null) ? catalog.findPlan(userEV.getEventPlan(), cur.getEffectiveDate(), startDate) : null;
                phaseType = (plan != null && userEV.getEventPlanPhase() != null) ? plan.findPhase(userEV.getEventPlanPhase()).getPhaseType() : prevPhaseType;
                productName = (plan != null) ? plan.getProduct().getName() : prevProductName;
                priceListName = (userEV.getPriceList() != null) ? userEV.getPriceList() : prevPriceListName;
                break;
        }
        final SubscriptionBaseTransitionType transitionType = SubscriptionBaseTransitionData.toSubscriptionTransitionType(cur.getType(), apiType);
        final String planNameWithClosure = plan != null ? plan.getName() : null;
        final String planPhaseNameWithClosure = planPhaseName;
        final Integer billCycleDayLocalWithClosure = billCycleDayLocal;
        final PlanPhaseSpecifier spec = new PlanPhaseSpecifier(planNameWithClosure, phaseType);
        result.add(new ExistingEvent() {

            @Override
            public SubscriptionBaseTransitionType getSubscriptionTransitionType() {
                return transitionType;
            }

            @Override
            public ProductCategory getProductCategory() {
                return category;
            }

            @Override
            public PlanPhaseSpecifier getPlanPhaseSpecifier() {
                return spec;
            }

            @Override
            public UUID getEventId() {
                return cur.getId();
            }

            @Override
            public DateTime getEffectiveDate() {
                return cur.getEffectiveDate();
            }

            @Override
            public String getPlanName() {
                return planNameWithClosure;
            }

            @Override
            public String getPlanPhaseName() {
                return planPhaseNameWithClosure;
            }

            @Override
            public Integer getBillCycleDayLocal() {
                return billCycleDayLocalWithClosure;
            }
        });
        prevPlan = plan;
        prevProductName = productName;
        prevPriceListName = priceListName;
        prevPhaseType = phaseType;
    }
    sortExistingEvent(result);
    return result;
}
Also used : ApiEventType(org.killbill.billing.subscription.events.user.ApiEventType) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) SubscriptionBaseTransitionType(org.killbill.billing.subscription.api.SubscriptionBaseTransitionType) BCDEvent(org.killbill.billing.subscription.events.bcd.BCDEvent) Plan(org.killbill.billing.catalog.api.Plan) LinkedList(java.util.LinkedList) DateTime(org.joda.time.DateTime) ApiEvent(org.killbill.billing.subscription.events.user.ApiEvent) PhaseType(org.killbill.billing.catalog.api.PhaseType) ProductCategory(org.killbill.billing.catalog.api.ProductCategory) UUID(java.util.UUID) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 12 with PhaseType

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

the class DefaultSubscriptionBase method getDateOfFirstRecurringNonZeroCharge.

@Override
public DateTime getDateOfFirstRecurringNonZeroCharge() {
    final Plan initialPlan = !transitions.isEmpty() ? transitions.get(0).getNextPlan() : null;
    final PlanPhase initialPhase = !transitions.isEmpty() ? transitions.get(0).getNextPhase() : null;
    final PhaseType initialPhaseType = initialPhase != null ? initialPhase.getPhaseType() : null;
    return initialPlan.dateOfFirstRecurringNonZeroCharge(getStartDate(), initialPhaseType);
}
Also used : PhaseType(org.killbill.billing.catalog.api.PhaseType) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Plan(org.killbill.billing.catalog.api.Plan)

Example 13 with PhaseType

use of org.killbill.billing.catalog.api.PhaseType 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)

Aggregations

PhaseType (org.killbill.billing.catalog.api.PhaseType)13 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)9 DateTime (org.joda.time.DateTime)6 Plan (org.killbill.billing.catalog.api.Plan)5 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)4 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 UUID (java.util.UUID)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)2 Catalog (org.killbill.billing.catalog.api.Catalog)2 Currency (org.killbill.billing.catalog.api.Currency)2 PlanAlignmentChange (org.killbill.billing.catalog.api.PlanAlignmentChange)2 PlanPhasePriceOverridesWithCallContext (org.killbill.billing.catalog.api.PlanPhasePriceOverridesWithCallContext)2 PlanSpecifier (org.killbill.billing.catalog.api.PlanSpecifier)2 ProductCategory (org.killbill.billing.catalog.api.ProductCategory)2 SubscriptionBaseTransitionType (org.killbill.billing.subscription.api.SubscriptionBaseTransitionType)2 DefaultPlanPhasePriceOverridesWithCallContext (org.killbill.billing.subscription.api.svcs.DefaultPlanPhasePriceOverridesWithCallContext)2 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)2 BCDEvent (org.killbill.billing.subscription.events.bcd.BCDEvent)2