Search in sources :

Example 26 with BlockingState

use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.

the class DefaultEventsStream method computeAddonsBlockingStatesForNextSubscriptionBaseEvent.

private Collection<BlockingState> computeAddonsBlockingStatesForNextSubscriptionBaseEvent(final DateTime effectiveDate, final boolean useBillingEffectiveDate) {
    SubscriptionBaseTransition subscriptionBaseTransitionTrigger = null;
    if (!isEntitlementFutureCancelled()) {
        // Compute the transition trigger (either subscription cancel or change)
        final Iterable<SubscriptionBaseTransition> pendingSubscriptionBaseTransitions = getPendingSubscriptionEvents(effectiveDate, SubscriptionBaseTransitionType.CHANGE, SubscriptionBaseTransitionType.CANCEL);
        if (!pendingSubscriptionBaseTransitions.iterator().hasNext()) {
            return ImmutableList.<BlockingState>of();
        }
        subscriptionBaseTransitionTrigger = pendingSubscriptionBaseTransitions.iterator().next();
    }
    final Product baseTransitionTriggerNextProduct;
    final DateTime blockingStateEffectiveDate;
    if (subscriptionBaseTransitionTrigger == null) {
        baseTransitionTriggerNextProduct = null;
        blockingStateEffectiveDate = effectiveDate;
    } else {
        baseTransitionTriggerNextProduct = (EntitlementState.CANCELLED.equals(subscriptionBaseTransitionTrigger.getNextState()) ? null : subscriptionBaseTransitionTrigger.getNextPlan().getProduct());
        blockingStateEffectiveDate = useBillingEffectiveDate ? subscriptionBaseTransitionTrigger.getEffectiveTransitionTime() : effectiveDate;
    }
    return computeAddonsBlockingStatesForSubscriptionBaseEvent(baseTransitionTriggerNextProduct, blockingStateEffectiveDate);
}
Also used : Product(org.killbill.billing.catalog.api.Product) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) SubscriptionBaseTransition(org.killbill.billing.subscription.api.user.SubscriptionBaseTransition) DateTime(org.joda.time.DateTime)

Example 27 with BlockingState

use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.

the class DefaultEventsStream method computeAddonsBlockingStatesForSubscriptionBaseEvent.

private Collection<BlockingState> computeAddonsBlockingStatesForSubscriptionBaseEvent(@Nullable final Product baseTransitionTriggerNextProduct, final DateTime blockingStateEffectiveDate) {
    if (baseSubscription == null || baseSubscription.getLastActivePlan() == null || !ProductCategory.BASE.equals(baseSubscription.getLastActivePlan().getProduct().getCategory())) {
        return ImmutableList.<BlockingState>of();
    }
    // Compute included and available addons for the new product
    final Collection<String> includedAddonsForProduct;
    final Collection<String> availableAddonsForProduct;
    if (baseTransitionTriggerNextProduct == null) {
        includedAddonsForProduct = ImmutableList.<String>of();
        availableAddonsForProduct = ImmutableList.<String>of();
    } else {
        includedAddonsForProduct = Collections2.<Product, String>transform(ImmutableSet.<Product>copyOf(baseTransitionTriggerNextProduct.getIncluded()), new Function<Product, String>() {

            @Override
            public String apply(final Product product) {
                return product.getName();
            }
        });
        availableAddonsForProduct = Collections2.<Product, String>transform(ImmutableSet.<Product>copyOf(baseTransitionTriggerNextProduct.getAvailable()), new Function<Product, String>() {

            @Override
            public String apply(final Product product) {
                return product.getName();
            }
        });
    }
    // Retrieve all add-ons to block for that base subscription
    final Collection<SubscriptionBase> futureBlockedAddons = Collections2.<SubscriptionBase>filter(allSubscriptionsForBundle, new Predicate<SubscriptionBase>() {

        @Override
        public boolean apply(final SubscriptionBase subscription) {
            final Plan lastActivePlan = subscription.getLastActivePlan();
            final boolean result = ProductCategory.ADD_ON.equals(subscription.getCategory()) && // Check the subscription started, if not we don't want it, and that way we avoid doing NPE a few lines below.
            lastActivePlan != null && // Check the entitlement for that add-on hasn't been cancelled yet
            getEntitlementCancellationEvent(subscription.getId()) == null && (// Base subscription cancelled
            baseTransitionTriggerNextProduct == null || (// Change plan - check which add-ons to cancel
            includedAddonsForProduct.contains(lastActivePlan.getProduct().getName()) || !availableAddonsForProduct.contains(subscription.getLastActivePlan().getProduct().getName())));
            return result;
        }
    });
    // Create the blocking states
    return Collections2.<SubscriptionBase, BlockingState>transform(futureBlockedAddons, new Function<SubscriptionBase, BlockingState>() {

        @Override
        public BlockingState apply(final SubscriptionBase input) {
            return new DefaultBlockingState(input.getId(), BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_CANCELLED, EntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, blockingStateEffectiveDate);
        }
    });
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) Function(com.google.common.base.Function) Product(org.killbill.billing.catalog.api.Product) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) Plan(org.killbill.billing.catalog.api.Plan) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState)

Example 28 with BlockingState

use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.

the class EntitlementUtils method setBlockingStateAndPostBlockingTransitionEvent.

public void setBlockingStateAndPostBlockingTransitionEvent(final Map<BlockingState, UUID> blockingStates, final InternalCallContext internalCallContext) {
    final ImmutableMap.Builder<BlockingState, Optional<UUID>> states = new ImmutableMap.Builder<BlockingState, Optional<UUID>>();
    for (final BlockingState blockingState : blockingStates.keySet()) {
        states.put(blockingState, Optional.<UUID>fromNullable(blockingStates.get(blockingState)));
    }
    dao.setBlockingStatesAndPostBlockingTransitionEvent(states.build(), internalCallContext);
}
Also used : Optional(com.google.common.base.Optional) BlockingState(org.killbill.billing.entitlement.api.BlockingState) UUID(java.util.UUID) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 29 with BlockingState

use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.

the class EventsStreamBuilder method buildForEntitlement.

public EventsStream buildForEntitlement(final UUID entitlementId, final InternalTenantContext internalTenantContext) throws EntitlementApiException {
    final SubscriptionBaseBundle bundle;
    final SubscriptionBase subscription;
    final List<SubscriptionBase> allSubscriptionsForBundle;
    final SubscriptionBase baseSubscription;
    try {
        subscription = subscriptionInternalApi.getSubscriptionFromId(entitlementId, internalTenantContext);
        bundle = subscriptionInternalApi.getBundleFromId(subscription.getBundleId(), internalTenantContext);
        allSubscriptionsForBundle = subscriptionInternalApi.getSubscriptionsForBundle(subscription.getBundleId(), null, internalTenantContext);
        baseSubscription = findBaseSubscription(allSubscriptionsForBundle);
    } catch (SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    }
    final ImmutableAccountData account;
    try {
        account = accountInternalApi.getImmutableAccountDataById(bundle.getAccountId(), internalTenantContext);
    } catch (AccountApiException e) {
        throw new EntitlementApiException(e);
    }
    // Retrieve the blocking states
    final List<BlockingState> blockingStatesForAccount = defaultBlockingStateDao.getBlockingAllForAccountRecordId(internalTenantContext);
    final Map<UUID, Integer> bcdCache = new HashMap<UUID, Integer>();
    return buildForEntitlement(blockingStatesForAccount, account, bundle, baseSubscription, subscription, allSubscriptionsForBundle, bcdCache, internalTenantContext);
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) HashMap(java.util.HashMap) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) BlockingState(org.killbill.billing.entitlement.api.BlockingState) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) AccountApiException(org.killbill.billing.account.api.AccountApiException) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 30 with BlockingState

use of org.killbill.billing.entitlement.api.BlockingState in project killbill by killbill.

the class TestBillingApi method testBillingEventsWithBlock.

@Test(groups = "fast")
public void testBillingEventsWithBlock() throws CatalogApiException, AccountApiException, SubscriptionBaseApiException {
    final Plan nextPlan = catalog.findPlan("3-PickupTrialEvergreen10USD", clock.getUTCNow());
    final PlanPhase nextPhase = nextPlan.getAllPhases()[1];
    final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase);
    final Account account = createAccount(32);
    final BlockingState blockingState1 = new DefaultBlockingState(bunId, BlockingStateType.SUBSCRIPTION_BUNDLE, DISABLED_BUNDLE, "test", true, true, true, now.plusDays(1));
    final BlockingState blockingState2 = new DefaultBlockingState(bunId, BlockingStateType.SUBSCRIPTION_BUNDLE, CLEAR_BUNDLE, "test", false, false, false, now.plusDays(2));
    blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableMap.<BlockingState, Optional<UUID>>of(blockingState1, Optional.<UUID>absent(), blockingState2, Optional.<UUID>absent()), internalCallContext);
    final SortedSet<BillingEvent> events = billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, internalCallContext);
    Assert.assertEquals(events.size(), 3);
    final Iterator<BillingEvent> it = events.iterator();
    checkEvent(it.next(), nextPlan, account.getBillCycleDayLocal(), subId, now, nextPhase, SubscriptionBaseTransitionType.CREATE.toString(), nextPhase.getFixed().getPrice(), nextPhase.getRecurring().getRecurringPrice());
    checkEvent(it.next(), nextPlan, account.getBillCycleDayLocal(), subId, now.plusDays(1), nextPhase, SubscriptionBaseTransitionType.START_BILLING_DISABLED.toString(), null, null);
    checkEvent(it.next(), nextPlan, account.getBillCycleDayLocal(), subId, now.plusDays(2), nextPhase, SubscriptionBaseTransitionType.END_BILLING_DISABLED.toString(), nextPhase.getFixed().getPrice(), nextPhase.getRecurring().getRecurringPrice());
}
Also used : Account(org.killbill.billing.account.api.Account) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) BillingEvent(org.killbill.billing.junction.BillingEvent) Plan(org.killbill.billing.catalog.api.Plan) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Test(org.testng.annotations.Test)

Aggregations

BlockingState (org.killbill.billing.entitlement.api.BlockingState)46 UUID (java.util.UUID)24 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)22 Test (org.testng.annotations.Test)15 DateTime (org.joda.time.DateTime)13 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)6 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)6 LocalDate (org.joda.time.LocalDate)5 ImmutableList (com.google.common.collect.ImmutableList)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 Account (org.killbill.billing.account.api.Account)4 BillingEvent (org.killbill.billing.junction.BillingEvent)4 Optional (com.google.common.base.Optional)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 TreeSet (java.util.TreeSet)3 AccountApiException (org.killbill.billing.account.api.AccountApiException)3 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)3 BlockingStateType (org.killbill.billing.entitlement.api.BlockingStateType)3