Search in sources :

Example 11 with PlanPhasePriceOverridesWithCallContext

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

the class DefaultSubscriptionBaseApiService method doChangePlan.

private void doChangePlan(final DefaultSubscriptionBase subscription, final EntitlementSpecifier spec, final DateTime effectiveDate, final CallContext context) throws SubscriptionBaseApiException, CatalogApiException {
    final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context);
    final PlanPhasePriceOverridesWithCallContext overridesWithContext = new DefaultPlanPhasePriceOverridesWithCallContext(spec.getOverrides(), context);
    final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(internalCallContext);
    final PlanPhaseSpecifier planPhaseSpecifier = spec.getPlanPhaseSpecifier();
    final StaticCatalog versionCatalog = catalog.versionForDate(effectiveDate);
    final Plan newPlan = versionCatalog.createOrFindPlan(planPhaseSpecifier, overridesWithContext);
    final PhaseType initialPhaseType = planPhaseSpecifier.getPhaseType();
    if (ProductCategory.ADD_ON.toString().equalsIgnoreCase(newPlan.getProduct().getCategory().toString())) {
        if (newPlan.getPlansAllowedInBundle() != -1 && newPlan.getPlansAllowedInBundle() > 0 && addonUtils.countExistingAddOnsWithSamePlanName(dao.getSubscriptions(subscription.getBundleId(), null, catalog, internalCallContext), newPlan.getName()) >= newPlan.getPlansAllowedInBundle()) {
            // the plan can be changed to the new value, because it has reached its limit by bundle
            throw new SubscriptionBaseApiException(ErrorCode.SUB_CHANGE_AO_MAX_PLAN_ALLOWED_BY_BUNDLE, newPlan.getName());
        }
    }
    if (newPlan.getProduct().getCategory() != subscription.getCategory()) {
        throw new SubscriptionBaseApiException(ErrorCode.SUB_CHANGE_INVALID, subscription.getId());
    }
    final List<DefaultSubscriptionBase> addOnSubscriptionsToBeCancelled = new ArrayList<DefaultSubscriptionBase>();
    final List<SubscriptionBaseEvent> addOnCancelEvents = new ArrayList<SubscriptionBaseEvent>();
    final List<SubscriptionBaseEvent> changeEvents = getEventsOnChangePlan(subscription, newPlan, newPlan.getPriceList().getName(), effectiveDate, true, addOnSubscriptionsToBeCancelled, addOnCancelEvents, initialPhaseType, spec.getBillCycleDay(), catalog, internalCallContext);
    dao.changePlan(subscription, changeEvents, addOnSubscriptionsToBeCancelled, addOnCancelEvents, catalog, internalCallContext);
    subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId(), internalCallContext), catalog);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) ArrayList(java.util.ArrayList) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) Plan(org.killbill.billing.catalog.api.Plan) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) DefaultPlanPhasePriceOverridesWithCallContext(org.killbill.billing.subscription.api.svcs.DefaultPlanPhasePriceOverridesWithCallContext) PlanPhasePriceOverridesWithCallContext(org.killbill.billing.catalog.api.PlanPhasePriceOverridesWithCallContext) DefaultPlanPhasePriceOverridesWithCallContext(org.killbill.billing.subscription.api.svcs.DefaultPlanPhasePriceOverridesWithCallContext) PhaseType(org.killbill.billing.catalog.api.PhaseType) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Aggregations

Plan (org.killbill.billing.catalog.api.Plan)11 PlanPhasePriceOverridesWithCallContext (org.killbill.billing.catalog.api.PlanPhasePriceOverridesWithCallContext)11 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)6 DateTime (org.joda.time.DateTime)5 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)5 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)5 StaticCatalog (org.killbill.billing.catalog.api.StaticCatalog)5 ArrayList (java.util.ArrayList)4 Catalog (org.killbill.billing.catalog.api.Catalog)4 EntitlementSpecifier (org.killbill.billing.entitlement.api.EntitlementSpecifier)4 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)4 SubscriptionBuilder (org.killbill.billing.subscription.api.user.SubscriptionBuilder)4 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)4 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)3 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)3 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)3 DefaultPlanPhasePriceOverridesWithCallContext (org.killbill.billing.subscription.api.svcs.DefaultPlanPhasePriceOverridesWithCallContext)3 CallContext (org.killbill.billing.util.callcontext.CallContext)3 TenantContext (org.killbill.billing.util.callcontext.TenantContext)3 UUID (java.util.UUID)2