Search in sources :

Example 11 with ApiEventCancel

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

the class DefaultSubscriptionDao method buildBundleSubscriptions.

private List<DefaultSubscriptionBase> buildBundleSubscriptions(final List<DefaultSubscriptionBase> input, @Nullable final Multimap<UUID, SubscriptionBaseEvent> eventsForSubscription, @Nullable final Collection<SubscriptionBaseEvent> dryRunEvents, final SubscriptionCatalog catalog, final InternalTenantContext context) throws CatalogApiException {
    if (input == null || input.isEmpty()) {
        return Collections.emptyList();
    }
    // Make sure BasePlan -- if exists-- is first
    Collections.sort(input, DefaultSubscriptionInternalApi.SUBSCRIPTIONS_COMPARATOR);
    final Collection<ApiEventChange> baseChangeEvents = new LinkedList<ApiEventChange>();
    ApiEventCancel baseCancellationEvent = null;
    final List<DefaultSubscriptionBase> result = new ArrayList<DefaultSubscriptionBase>(input.size());
    for (final DefaultSubscriptionBase cur : input) {
        final List<SubscriptionBaseEvent> events = eventsForSubscription != null ? (List<SubscriptionBaseEvent>) eventsForSubscription.get(cur.getId()) : getEventsForSubscription(cur.getId(), context);
        mergeDryRunEvents(cur.getId(), events, dryRunEvents);
        DefaultSubscriptionBase reloaded = createSubscriptionForInternalUse(cur, events, catalog, context);
        switch(cur.getCategory()) {
            case BASE:
                for (final SubscriptionBaseEvent event : events) {
                    if (!event.isActive()) {
                        continue;
                    } else if (event instanceof ApiEventCancel) {
                        baseCancellationEvent = (ApiEventCancel) event;
                        break;
                    } else if (event instanceof ApiEventChange) {
                        // Need to track all changes, see https://github.com/killbill/killbill/issues/268
                        baseChangeEvents.add((ApiEventChange) event);
                    }
                }
                break;
            case ADD_ON:
                final Plan targetAddOnPlan = reloaded.getCurrentPlan();
                if (targetAddOnPlan == null || reloaded.getFutureEndDate() != null) {
                    // triggers another cancellation before?
                    break;
                }
                SubscriptionBaseEvent baseTriggerEventForAddOnCancellation = baseCancellationEvent;
                for (final ApiEventChange baseChangeEvent : baseChangeEvents) {
                    final Plan basePlan = catalog.findPlan(baseChangeEvent.getEventPlan(), baseChangeEvent.getEffectiveDate(), cur.getAlignStartDate());
                    final Product baseProduct = basePlan.getProduct();
                    if ((!addonUtils.isAddonAvailable(baseProduct, targetAddOnPlan)) || (addonUtils.isAddonIncluded(baseProduct, targetAddOnPlan))) {
                        if (baseTriggerEventForAddOnCancellation != null) {
                            if (baseTriggerEventForAddOnCancellation.getEffectiveDate().isAfter(baseChangeEvent.getEffectiveDate())) {
                                baseTriggerEventForAddOnCancellation = baseChangeEvent;
                            }
                        } else {
                            baseTriggerEventForAddOnCancellation = baseChangeEvent;
                        }
                    }
                }
                if (baseTriggerEventForAddOnCancellation != null) {
                    final SubscriptionBaseEvent addOnCancelEvent = new ApiEventCancel(new ApiEventBuilder().setSubscriptionId(reloaded.getId()).setEffectiveDate(baseTriggerEventForAddOnCancellation.getEffectiveDate()).setCreatedDate(baseTriggerEventForAddOnCancellation.getCreatedDate()).setFromDisk(false));
                    events.add(addOnCancelEvent);
                    // Finally reload subscription with full set of events
                    reloaded = createSubscriptionForInternalUse(cur, events, catalog, context);
                }
                break;
            default:
                break;
        }
        result.add(reloaded);
    }
    return result;
}
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) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) Plan(org.killbill.billing.catalog.api.Plan) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) ApiEventChange(org.killbill.billing.subscription.events.user.ApiEventChange) LinkedList(java.util.LinkedList)

Example 12 with ApiEventCancel

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

the class TestDefaultSubscriptionBase method testFutureCancelBeforePhase.

@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/897")
public void testFutureCancelBeforePhase() throws Exception {
    final DateTime startDate = new DateTime(2012, 5, 1, 0, 0, DateTimeZone.UTC);
    final DefaultSubscriptionBase subscriptionBase = new DefaultSubscriptionBase(new SubscriptionBuilder().setAlignStartDate(startDate));
    final UUID subscriptionId = UUID.randomUUID();
    final List<SubscriptionBaseEvent> inputEvents = new LinkedList<SubscriptionBaseEvent>();
    inputEvents.add(new ApiEventCreate(new ApiEventBuilder().setApiEventType(CREATE).setEventPlan("laser-scope-monthly").setEventPlanPhase("laser-scope-monthly-discount").setEventPriceList("DEFAULT").setFromDisk(true).setUuid(UUID.randomUUID()).setSubscriptionId(subscriptionId).setCreatedDate(startDate).setUpdatedDate(startDate).setEffectiveDate(startDate).setTotalOrdering(3).setActive(true)));
    inputEvents.add(new PhaseEventData(new PhaseEventBuilder().setPhaseName("laser-scope-monthly-evergreen").setUuid(UUID.randomUUID()).setSubscriptionId(subscriptionId).setCreatedDate(startDate).setUpdatedDate(startDate).setEffectiveDate(new DateTime(2012, 6, 1, 0, 0, DateTimeZone.UTC)).setTotalOrdering(4).setActive(true)));
    inputEvents.add(new ApiEventCancel(new ApiEventBuilder().setApiEventType(ApiEventType.CANCEL).setEventPlan(null).setEventPlanPhase(null).setEventPriceList(null).setFromDisk(false).setUuid(UUID.randomUUID()).setSubscriptionId(subscriptionId).setCreatedDate(startDate).setUpdatedDate(null).setEffectiveDate(new DateTime(2012, 6, 1, 0, 0, DateTimeZone.UTC)).setTotalOrdering(// In-memory event
    0).setActive(true)));
    subscriptionBase.rebuildTransitions(inputEvents, catalog);
    Assert.assertEquals(subscriptionBase.getAllTransitions().size(), 2);
    Assert.assertNull(subscriptionBase.getAllTransitions().get(0).getPreviousState());
    Assert.assertEquals(subscriptionBase.getAllTransitions().get(0).getNextState(), EntitlementState.ACTIVE);
    Assert.assertEquals(subscriptionBase.getAllTransitions().get(0).getEffectiveTransitionTime(), startDate);
    Assert.assertEquals(subscriptionBase.getAllTransitions().get(1).getPreviousState(), EntitlementState.ACTIVE);
    Assert.assertEquals(subscriptionBase.getAllTransitions().get(1).getNextState(), EntitlementState.CANCELLED);
    Assert.assertEquals(subscriptionBase.getAllTransitions().get(1).getEffectiveTransitionTime(), new DateTime(2012, 6, 1, 0, 0, DateTimeZone.UTC));
}
Also used : ApiEventCreate(org.killbill.billing.subscription.events.user.ApiEventCreate) PhaseEventData(org.killbill.billing.subscription.events.phase.PhaseEventData) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) ApiEventCancel(org.killbill.billing.subscription.events.user.ApiEventCancel) UUID(java.util.UUID) PhaseEventBuilder(org.killbill.billing.subscription.events.phase.PhaseEventBuilder) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) DateTime(org.joda.time.DateTime) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Aggregations

SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)12 ApiEventBuilder (org.killbill.billing.subscription.events.user.ApiEventBuilder)12 ApiEventCancel (org.killbill.billing.subscription.events.user.ApiEventCancel)12 LinkedList (java.util.LinkedList)7 ArrayList (java.util.ArrayList)6 DateTime (org.joda.time.DateTime)6 ApiEventCreate (org.killbill.billing.subscription.events.user.ApiEventCreate)5 UUID (java.util.UUID)4 Plan (org.killbill.billing.catalog.api.Plan)4 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)4 Test (org.testng.annotations.Test)4 Product (org.killbill.billing.catalog.api.Product)3 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)3 PhaseEventBuilder (org.killbill.billing.subscription.events.phase.PhaseEventBuilder)3 PhaseEventData (org.killbill.billing.subscription.events.phase.PhaseEventData)3 SubscriptionBuilder (org.killbill.billing.subscription.api.user.SubscriptionBuilder)2 ApiEventChange (org.killbill.billing.subscription.events.user.ApiEventChange)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)1 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)1