Search in sources :

Example 1 with ApiEventTransfer

use of org.killbill.billing.subscription.events.user.ApiEventTransfer 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 Catalog catalog = catalogService.getFullCatalog(true, true, context);
    final DateTime effectiveDate = existingEvent.getEffectiveDate().isBefore(transferDate) ? transferDate : existingEvent.getEffectiveDate();
    final PlanPhaseSpecifier spec = existingEvent.getPlanPhaseSpecifier();
    final PlanPhase currentPhase = existingEvent.getPlanPhaseName() != null ? catalog.findPhase(existingEvent.getPlanPhaseName(), effectiveDate, subscription.getAlignStartDate()) : null;
    if (spec == null || currentPhase == 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(currentPhase.getName()).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(), currentPhase.getName(), 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) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) ApiEventTransfer(org.killbill.billing.subscription.events.user.ApiEventTransfer) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) Catalog(org.killbill.billing.catalog.api.Catalog) DateTime(org.joda.time.DateTime) ApiEventChange(org.killbill.billing.subscription.events.user.ApiEventChange)

Aggregations

DateTime (org.joda.time.DateTime)1 Catalog (org.killbill.billing.catalog.api.Catalog)1 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)1 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)1 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)1 ApiEventBuilder (org.killbill.billing.subscription.events.user.ApiEventBuilder)1 ApiEventChange (org.killbill.billing.subscription.events.user.ApiEventChange)1 ApiEventTransfer (org.killbill.billing.subscription.events.user.ApiEventTransfer)1 SubscriptionBaseError (org.killbill.billing.subscription.exceptions.SubscriptionBaseError)1