Search in sources :

Example 16 with ApiEventBuilder

use of org.killbill.billing.subscription.events.user.ApiEventBuilder in project killbill by killbill.

the class DefaultSubscriptionBaseTransferApi method createEvent.

private SubscriptionBaseEvent createEvent(final boolean firstEvent, final ExistingEvent existingEvent, final DefaultSubscriptionBase subscription, final DateTime transferDate, final InternalTenantContext context) throws CatalogApiException {
    SubscriptionBaseEvent newEvent = null;
    final DateTime effectiveDate = existingEvent.getEffectiveDate().isBefore(transferDate) ? transferDate : existingEvent.getEffectiveDate();
    final PlanPhaseSpecifier spec = existingEvent.getPlanPhaseSpecifier();
    if (spec == null || existingEvent.getPlanPhaseName() == null) {
        // Ignore cancellations - we assume that transferred subscriptions should always be active
        return null;
    }
    final ApiEventBuilder apiBuilder = new ApiEventBuilder().setSubscriptionId(subscription.getId()).setEventPlan(existingEvent.getPlanName()).setEventPlanPhase(existingEvent.getPlanPhaseName()).setEventPriceList(spec.getPriceListName()).setEffectiveDate(effectiveDate).setFromDisk(true);
    switch(existingEvent.getSubscriptionTransitionType()) {
        case TRANSFER:
        case CREATE:
            newEvent = new ApiEventTransfer(apiBuilder);
            break;
        // Should we even keep future change events; product question really
        case CHANGE:
            newEvent = firstEvent ? new ApiEventTransfer(apiBuilder) : new ApiEventChange(apiBuilder);
            break;
        case PHASE:
            newEvent = firstEvent ? new ApiEventTransfer(apiBuilder) : PhaseEventData.createNextPhaseEvent(subscription.getId(), existingEvent.getPlanPhaseName(), effectiveDate);
            break;
        case CANCEL:
            break;
        default:
            throw new SubscriptionBaseError(String.format("Unexpected transitionType %s", existingEvent.getSubscriptionTransitionType()));
    }
    return newEvent;
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) ApiEventTransfer(org.killbill.billing.subscription.events.user.ApiEventTransfer) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) DateTime(org.joda.time.DateTime) ApiEventChange(org.killbill.billing.subscription.events.user.ApiEventChange)

Example 17 with ApiEventBuilder

use of org.killbill.billing.subscription.events.user.ApiEventBuilder in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method getEventsOnChangePlan.

private List<SubscriptionBaseEvent> getEventsOnChangePlan(final DefaultSubscriptionBase subscription, final Plan newPlan, final String newPriceList, final DateTime effectiveDate, final boolean addCancellationAddOnForEventsIfRequired, final Collection<DefaultSubscriptionBase> addOnSubscriptionsToBeCancelled, final Collection<SubscriptionBaseEvent> addOnCancelEvents, final PhaseType initialPhaseType, final Integer bcd, final SubscriptionCatalog catalog, final InternalTenantContext internalTenantContext) throws CatalogApiException, SubscriptionBaseApiException {
    final TimedPhase currentTimedPhase = planAligner.getCurrentTimedPhaseOnChange(subscription, newPlan, effectiveDate, initialPhaseType, catalog, internalTenantContext);
    validateSubscriptionStateForChangePlan(subscription, effectiveDate);
    final SubscriptionBaseEvent changeEvent = new ApiEventChange(new ApiEventBuilder().setSubscriptionId(subscription.getId()).setEventPlan(newPlan.getName()).setEventPlanPhase(currentTimedPhase.getPhase().getName()).setEventPriceList(newPriceList).setEffectiveDate(effectiveDate).setFromDisk(true));
    final TimedPhase nextTimedPhase = planAligner.getNextTimedPhaseOnChange(subscription, newPlan, effectiveDate, initialPhaseType, catalog, internalTenantContext);
    final PhaseEvent nextPhaseEvent = (nextTimedPhase != null) ? PhaseEventData.createNextPhaseEvent(subscription.getId(), nextTimedPhase.getPhase().getName(), nextTimedPhase.getStartPhase()) : null;
    final List<SubscriptionBaseEvent> changeEvents = new ArrayList<SubscriptionBaseEvent>();
    // Only add the PHASE if it does not coincide with the CHANGE, if not this is 'just' a CHANGE.
    changeEvents.add(changeEvent);
    if (bcd != null) {
        final SubscriptionBaseEvent bcdEvent = new BCDEventData(new BCDEventBuilder().setSubscriptionId(subscription.getId()).setEffectiveDate(effectiveDate).setActive(true).setBillCycleDayLocal(bcd));
        changeEvents.add(bcdEvent);
    }
    if (nextPhaseEvent != null && !nextPhaseEvent.getEffectiveDate().equals(changeEvent.getEffectiveDate())) {
        changeEvents.add(nextPhaseEvent);
    }
    if (subscription.getCategory() == ProductCategory.BASE && addCancellationAddOnForEventsIfRequired) {
        final Product currentBaseProduct = changeEvent.getEffectiveDate().compareTo(clock.getUTCNow()) <= 0 ? newPlan.getProduct() : subscription.getCurrentOrPendingPlan().getProduct();
        addOnSubscriptionsToBeCancelled.addAll(addCancellationAddOnForEventsIfRequired(addOnCancelEvents, currentBaseProduct, subscription.getBundleId(), effectiveDate, catalog, internalTenantContext));
    }
    return changeEvents;
}
Also used : BCDEventData(org.killbill.billing.subscription.events.bcd.BCDEventData) PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) TimedPhase(org.killbill.billing.subscription.alignment.TimedPhase) ArrayList(java.util.ArrayList) BCDEventBuilder(org.killbill.billing.subscription.events.bcd.BCDEventBuilder) Product(org.killbill.billing.catalog.api.Product) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) ApiEventChange(org.killbill.billing.subscription.events.user.ApiEventChange)

Example 18 with ApiEventBuilder

use of org.killbill.billing.subscription.events.user.ApiEventBuilder in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method getEventsOnCancelPlan.

@Override
public List<SubscriptionBaseEvent> getEventsOnCancelPlan(final DefaultSubscriptionBase subscription, final DateTime effectiveDate, final boolean addCancellationAddOnForEventsIfRequired, final SubscriptionCatalog fullCatalog, final InternalTenantContext internalTenantContext) throws CatalogApiException {
    final List<SubscriptionBaseEvent> cancelEvents = new ArrayList<SubscriptionBaseEvent>();
    final SubscriptionBaseEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder().setSubscriptionId(subscription.getId()).setEffectiveDate(effectiveDate).setFromDisk(true));
    cancelEvents.add(cancelEvent);
    if (subscription.getCategory() == ProductCategory.BASE && addCancellationAddOnForEventsIfRequired) {
        final Product currentBaseProduct = cancelEvent.getEffectiveDate().compareTo(clock.getUTCNow()) <= 0 ? null : subscription.getCurrentPlan().getProduct();
        addCancellationAddOnForEventsIfRequired(cancelEvents, currentBaseProduct, subscription.getBundleId(), effectiveDate, fullCatalog, internalTenantContext);
    }
    return cancelEvents;
}
Also used : ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) ArrayList(java.util.ArrayList) Product(org.killbill.billing.catalog.api.Product) ApiEventCancel(org.killbill.billing.subscription.events.user.ApiEventCancel) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 19 with ApiEventBuilder

use of org.killbill.billing.subscription.events.user.ApiEventBuilder in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method getEventsOnCreation.

@Override
public List<SubscriptionBaseEvent> getEventsOnCreation(final UUID subscriptionId, final DateTime alignStartDate, final DateTime bundleStartDate, final Plan plan, final PhaseType initialPhase, final String realPriceList, final DateTime effectiveDate, final Integer bcd, final SubscriptionCatalog catalog, final InternalTenantContext internalTenantContext) throws CatalogApiException, SubscriptionBaseApiException {
    final TimedPhase[] curAndNextPhases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, initialPhase, realPriceList, effectiveDate, catalog, internalTenantContext);
    final ApiEventBuilder createBuilder = new ApiEventBuilder().setSubscriptionId(subscriptionId).setEventPlan(plan.getName()).setEventPlanPhase(curAndNextPhases[0].getPhase().getName()).setEventPriceList(realPriceList).setEffectiveDate(effectiveDate).setFromDisk(true);
    final SubscriptionBaseEvent creationEvent = new ApiEventCreate(createBuilder);
    final TimedPhase nextTimedPhase = curAndNextPhases[1];
    final PhaseEvent nextPhaseEvent = (nextTimedPhase != null) ? PhaseEventData.createNextPhaseEvent(subscriptionId, nextTimedPhase.getPhase().getName(), nextTimedPhase.getStartPhase()) : null;
    final List<SubscriptionBaseEvent> events = new ArrayList<SubscriptionBaseEvent>();
    events.add(creationEvent);
    if (bcd != null) {
        final SubscriptionBaseEvent bcdEvent = new BCDEventData(new BCDEventBuilder().setSubscriptionId(subscriptionId).setEffectiveDate(effectiveDate).setActive(true).setBillCycleDayLocal(bcd));
        events.add(bcdEvent);
    }
    if (nextPhaseEvent != null) {
        events.add(nextPhaseEvent);
    }
    return events;
}
Also used : ApiEventCreate(org.killbill.billing.subscription.events.user.ApiEventCreate) BCDEventData(org.killbill.billing.subscription.events.bcd.BCDEventData) PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) TimedPhase(org.killbill.billing.subscription.alignment.TimedPhase) ArrayList(java.util.ArrayList) BCDEventBuilder(org.killbill.billing.subscription.events.bcd.BCDEventBuilder) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 20 with ApiEventBuilder

use of org.killbill.billing.subscription.events.user.ApiEventBuilder in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method addCancellationAddOnForEventsIfRequired.

private List<DefaultSubscriptionBase> addCancellationAddOnForEventsIfRequired(final Collection<SubscriptionBaseEvent> events, final Product baseProduct, final UUID bundleId, final DateTime effectiveDate, final SubscriptionCatalog catalog, final InternalTenantContext internalTenantContext) throws CatalogApiException {
    final List<DefaultSubscriptionBase> subscriptionsToBeCancelled = new ArrayList<DefaultSubscriptionBase>();
    final List<DefaultSubscriptionBase> subscriptions = dao.getSubscriptions(bundleId, ImmutableList.<SubscriptionBaseEvent>of(), catalog, internalTenantContext);
    for (final SubscriptionBase subscription : subscriptions) {
        final DefaultSubscriptionBase cur = (DefaultSubscriptionBase) subscription;
        if (cur.getState() == EntitlementState.CANCELLED || cur.getCategory() != ProductCategory.ADD_ON) {
            continue;
        }
        final Plan addonCurrentPlan = cur.getCurrentPlan();
        if (baseProduct == null || addonUtils.isAddonIncluded(baseProduct, addonCurrentPlan) || !addonUtils.isAddonAvailable(baseProduct, addonCurrentPlan)) {
            // 
            // Perform AO cancellation using the effectiveDate of the BP
            // 
            final SubscriptionBaseEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder().setSubscriptionId(cur.getId()).setEffectiveDate(effectiveDate).setFromDisk(true));
            subscriptionsToBeCancelled.add(cur);
            events.add(cancelEvent);
        }
    }
    return subscriptionsToBeCancelled;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) ArrayList(java.util.ArrayList) ApiEventCancel(org.killbill.billing.subscription.events.user.ApiEventCancel) Plan(org.killbill.billing.catalog.api.Plan) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Aggregations

ApiEventBuilder (org.killbill.billing.subscription.events.user.ApiEventBuilder)27 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)26 ArrayList (java.util.ArrayList)13 DateTime (org.joda.time.DateTime)13 ApiEventCancel (org.killbill.billing.subscription.events.user.ApiEventCancel)12 ApiEventCreate (org.killbill.billing.subscription.events.user.ApiEventCreate)11 LinkedList (java.util.LinkedList)10 ApiEventChange (org.killbill.billing.subscription.events.user.ApiEventChange)8 PhaseEventBuilder (org.killbill.billing.subscription.events.phase.PhaseEventBuilder)7 Test (org.testng.annotations.Test)7 UUID (java.util.UUID)6 TimedPhase (org.killbill.billing.subscription.alignment.TimedPhase)6 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)6 PhaseEvent (org.killbill.billing.subscription.events.phase.PhaseEvent)6 Product (org.killbill.billing.catalog.api.Product)5 PhaseEventData (org.killbill.billing.subscription.events.phase.PhaseEventData)5 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)4 Plan (org.killbill.billing.catalog.api.Plan)4 SubscriptionBuilder (org.killbill.billing.subscription.api.user.SubscriptionBuilder)4 BCDEventBuilder (org.killbill.billing.subscription.events.bcd.BCDEventBuilder)4