Search in sources :

Example 36 with SubscriptionBaseEvent

use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.

the class MockSubscriptionDaoMemory method createSubscriptionsWithAddOns.

@Override
public void createSubscriptionsWithAddOns(final List<SubscriptionBaseWithAddOns> subscriptions, final Map<UUID, List<SubscriptionBaseEvent>> initialEventsMap, final InternalCallContext context) {
    synchronized (events) {
        for (final SubscriptionBaseWithAddOns subscription : subscriptions) {
            for (final SubscriptionBase subscriptionBase : subscription.getSubscriptionBaseList()) {
                final List<SubscriptionBaseEvent> initialEvents = initialEventsMap.get(subscriptionBase.getId());
                events.addAll(initialEvents);
                for (final SubscriptionBaseEvent cur : initialEvents) {
                    recordFutureNotificationFromTransaction(null, cur.getEffectiveDate(), new SubscriptionNotificationKey(cur.getId()), context);
                }
                final SubscriptionBase updatedSubscription = buildSubscription((DefaultSubscriptionBase) subscriptionBase, context);
                this.subscriptions.add(updatedSubscription);
                mockNonEntityDao.addTenantRecordIdMapping(updatedSubscription.getId(), context);
            }
        }
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionNotificationKey(org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey) DefaultSubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseWithAddOns) SubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 37 with SubscriptionBaseEvent

use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.

the class DefaultSubscriptionBase method rebuildTransitions.

public void rebuildTransitions(final List<SubscriptionBaseEvent> inputEvents, final Catalog catalog) throws CatalogApiException {
    if (inputEvents == null) {
        return;
    }
    this.events = inputEvents;
    filterOutDuplicateCancelEvents(events);
    UUID nextUserToken = null;
    UUID nextEventId = null;
    DateTime nextCreatedDate = null;
    EntitlementState nextState = null;
    String nextPlanName = null;
    String nextPhaseName = null;
    Integer nextBillingCycleDayLocal = null;
    UUID prevEventId = null;
    DateTime prevCreatedDate = null;
    EntitlementState previousState = null;
    PriceList previousPriceList = null;
    Plan previousPlan = null;
    PlanPhase previousPhase = null;
    Integer previousBillingCycleDayLocal = null;
    transitions = new LinkedList<SubscriptionBaseTransition>();
    for (final SubscriptionBaseEvent cur : inputEvents) {
        if (!cur.isActive()) {
            continue;
        }
        ApiEventType apiEventType = null;
        boolean isFromDisk = true;
        nextEventId = cur.getId();
        nextCreatedDate = cur.getCreatedDate();
        switch(cur.getType()) {
            case PHASE:
                final PhaseEvent phaseEV = (PhaseEvent) cur;
                nextPhaseName = phaseEV.getPhase();
                break;
            case BCD_UPDATE:
                final BCDEvent bcdEvent = (BCDEvent) cur;
                nextBillingCycleDayLocal = bcdEvent.getBillCycleDayLocal();
                break;
            case API_USER:
                final ApiEvent userEV = (ApiEvent) cur;
                apiEventType = userEV.getApiEventType();
                isFromDisk = userEV.isFromDisk();
                switch(apiEventType) {
                    case TRANSFER:
                    case CREATE:
                        prevEventId = null;
                        prevCreatedDate = null;
                        previousState = null;
                        previousPlan = null;
                        previousPhase = null;
                        previousPriceList = null;
                        nextState = EntitlementState.ACTIVE;
                        nextPlanName = userEV.getEventPlan();
                        nextPhaseName = userEV.getEventPlanPhase();
                        break;
                    case CHANGE:
                        nextPlanName = userEV.getEventPlan();
                        nextPhaseName = userEV.getEventPlanPhase();
                        break;
                    case CANCEL:
                        nextState = EntitlementState.CANCELLED;
                        nextPlanName = null;
                        nextPhaseName = null;
                        break;
                    case UNCANCEL:
                    default:
                        throw new SubscriptionBaseError(String.format("Unexpected UserEvent type = %s", userEV.getApiEventType().toString()));
                }
                break;
            default:
                throw new SubscriptionBaseError(String.format("Unexpected Event type = %s", cur.getType()));
        }
        Plan nextPlan = null;
        PlanPhase nextPhase = null;
        PriceList nextPriceList = null;
        nextPlan = (nextPlanName != null) ? catalog.findPlan(nextPlanName, cur.getEffectiveDate(), getAlignStartDate()) : null;
        nextPhase = (nextPhaseName != null) ? catalog.findPhase(nextPhaseName, cur.getEffectiveDate(), getAlignStartDate()) : null;
        nextPriceList = (nextPlan != null) ? catalog.findPriceListForPlan(nextPlanName, cur.getEffectiveDate(), getAlignStartDate()) : null;
        final SubscriptionBaseTransitionData transition = new SubscriptionBaseTransitionData(cur.getId(), id, bundleId, bundleExternalKey, cur.getType(), apiEventType, cur.getEffectiveDate(), prevEventId, prevCreatedDate, previousState, previousPlan, previousPhase, previousPriceList, previousBillingCycleDayLocal, nextEventId, nextCreatedDate, nextState, nextPlan, nextPhase, nextPriceList, nextBillingCycleDayLocal, cur.getTotalOrdering(), cur.getCreatedDate(), nextUserToken, isFromDisk);
        transitions.add(transition);
        previousState = nextState;
        previousPlan = nextPlan;
        previousPhase = nextPhase;
        previousPriceList = nextPriceList;
        prevEventId = nextEventId;
        prevCreatedDate = nextCreatedDate;
        previousBillingCycleDayLocal = nextBillingCycleDayLocal;
    }
}
Also used : ApiEventType(org.killbill.billing.subscription.events.user.ApiEventType) PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) BCDEvent(org.killbill.billing.subscription.events.bcd.BCDEvent) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) EntitlementState(org.killbill.billing.entitlement.api.Entitlement.EntitlementState) SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) ApiEvent(org.killbill.billing.subscription.events.user.ApiEvent) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) UUID(java.util.UUID) PriceList(org.killbill.billing.catalog.api.PriceList) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 38 with SubscriptionBaseEvent

use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method createPlansWithAddOns.

@Override
public List<SubscriptionBaseWithAddOns> createPlansWithAddOns(final UUID accountId, final Iterable<SubscriptionAndAddOnsSpecifier> subscriptionsAndAddOns, final CallContext context) throws SubscriptionBaseApiException {
    final Map<UUID, List<SubscriptionBaseEvent>> eventsMap = new HashMap<UUID, List<SubscriptionBaseEvent>>();
    final Collection<List<SubscriptionBase>> subscriptionBaseAndAddOnsList = new ArrayList<List<SubscriptionBase>>();
    final List<SubscriptionBaseWithAddOns> allSubscriptions = new ArrayList<SubscriptionBaseWithAddOns>();
    for (final SubscriptionAndAddOnsSpecifier subscriptionAndAddOns : subscriptionsAndAddOns) {
        final List<SubscriptionBase> subscriptionBaseList = new ArrayList<SubscriptionBase>();
        createEvents(subscriptionAndAddOns.getSubscriptionSpecifiers(), context, eventsMap, subscriptionBaseList);
        subscriptionBaseAndAddOnsList.add(subscriptionBaseList);
        final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = new DefaultSubscriptionBaseWithAddOns(subscriptionAndAddOns.getBundleId(), subscriptionBaseList, subscriptionAndAddOns.getEffectiveDate());
        allSubscriptions.add(subscriptionBaseWithAddOns);
    }
    final InternalCallContext internalCallContext = createCallContextFromAccountId(accountId, context);
    dao.createSubscriptionsWithAddOns(allSubscriptions, eventsMap, internalCallContext);
    for (final List<SubscriptionBase> subscriptions : subscriptionBaseAndAddOnsList) {
        final SubscriptionBase baseSubscription = findBaseSubscription(subscriptions);
        rebuildTransitions(internalCallContext, subscriptions, baseSubscription);
    }
    return allSubscriptions;
}
Also used : SubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) UUID(java.util.UUID) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 39 with SubscriptionBaseEvent

use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.

the class DefaultSubscriptionDao method cancelSubscriptionsFromTransaction.

private void cancelSubscriptionsFromTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final List<DefaultSubscriptionBase> subscriptions, final List<SubscriptionBaseEvent> cancelEvents, final InternalCallContext context) throws EntityPersistenceException {
    for (int i = 0; i < subscriptions.size(); i++) {
        final DefaultSubscriptionBase subscription = subscriptions.get(i);
        final SubscriptionBaseEvent cancelEvent = cancelEvents.get(i);
        cancelSubscriptionFromTransaction(subscription, cancelEvent, entitySqlDaoWrapperFactory, context, subscriptions.size() - i - 1);
    }
}
Also used : DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 40 with SubscriptionBaseEvent

use of org.killbill.billing.subscription.events.SubscriptionBaseEvent in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method doCancelPlan.

private boolean doCancelPlan(final Map<DefaultSubscriptionBase, DateTime> subscriptions, final DateTime now, final InternalCallContext internalCallContext) throws SubscriptionBaseApiException {
    final List<DefaultSubscriptionBase> subscriptionsToBeCancelled = new LinkedList<DefaultSubscriptionBase>();
    final List<SubscriptionBaseEvent> cancelEvents = new LinkedList<SubscriptionBaseEvent>();
    try {
        for (final DefaultSubscriptionBase subscription : subscriptions.keySet()) {
            final DateTime effectiveDate = subscriptions.get(subscription);
            validateEffectiveDate(subscription, effectiveDate);
            subscriptionsToBeCancelled.add(subscription);
            cancelEvents.addAll(getEventsOnCancelPlan(subscription, effectiveDate, now, false, internalCallContext));
            if (subscription.getCategory() == ProductCategory.BASE) {
                subscriptionsToBeCancelled.addAll(computeAddOnsToCancel(cancelEvents, null, subscription.getBundleId(), effectiveDate, internalCallContext));
            }
        }
        dao.cancelSubscriptions(subscriptionsToBeCancelled, cancelEvents, internalCallContext);
        boolean allSubscriptionsCancelled = true;
        for (final DefaultSubscriptionBase subscription : subscriptions.keySet()) {
            final Catalog fullCatalog = catalogService.getFullCatalog(true, true, internalCallContext);
            subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId(), internalCallContext), fullCatalog);
            allSubscriptionsCancelled = allSubscriptionsCancelled && (subscription.getState() == EntitlementState.CANCELLED);
        }
        return allSubscriptionsCancelled;
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) LinkedList(java.util.LinkedList) DateTime(org.joda.time.DateTime) Catalog(org.killbill.billing.catalog.api.Catalog)

Aggregations

SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)75 DateTime (org.joda.time.DateTime)31 ApiEventBuilder (org.killbill.billing.subscription.events.user.ApiEventBuilder)26 LinkedList (java.util.LinkedList)25 ArrayList (java.util.ArrayList)23 UUID (java.util.UUID)22 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)21 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)16 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)14 Test (org.testng.annotations.Test)14 Plan (org.killbill.billing.catalog.api.Plan)13 PhaseEvent (org.killbill.billing.subscription.events.phase.PhaseEvent)13 ApiEvent (org.killbill.billing.subscription.events.user.ApiEvent)13 ApiEventCancel (org.killbill.billing.subscription.events.user.ApiEventCancel)12 ApiEventCreate (org.killbill.billing.subscription.events.user.ApiEventCreate)11 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)10 SubscriptionBuilder (org.killbill.billing.subscription.api.user.SubscriptionBuilder)9 SubscriptionNotificationKey (org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey)8 ApiEventChange (org.killbill.billing.subscription.events.user.ApiEventChange)8 ImmutableList (com.google.common.collect.ImmutableList)7