Search in sources :

Example 1 with BCDEventData

use of org.killbill.billing.subscription.events.bcd.BCDEventData 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 2 with BCDEventData

use of org.killbill.billing.subscription.events.bcd.BCDEventData 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)

Aggregations

ArrayList (java.util.ArrayList)2 TimedPhase (org.killbill.billing.subscription.alignment.TimedPhase)2 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)2 BCDEventBuilder (org.killbill.billing.subscription.events.bcd.BCDEventBuilder)2 BCDEventData (org.killbill.billing.subscription.events.bcd.BCDEventData)2 PhaseEvent (org.killbill.billing.subscription.events.phase.PhaseEvent)2 ApiEventBuilder (org.killbill.billing.subscription.events.user.ApiEventBuilder)2 Product (org.killbill.billing.catalog.api.Product)1 ApiEventChange (org.killbill.billing.subscription.events.user.ApiEventChange)1 ApiEventCreate (org.killbill.billing.subscription.events.user.ApiEventCreate)1