Search in sources :

Example 66 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class TestUsageInArrearBase method createMockBillingEvent.

protected BillingEvent createMockBillingEvent(final DateTime effectiveDate, final BillingPeriod billingPeriod, final List<Usage> usages) {
    final BillingEvent result = Mockito.mock(BillingEvent.class);
    Mockito.when(result.getCurrency()).thenReturn(Currency.BTC);
    Mockito.when(result.getBillCycleDayLocal()).thenReturn(BCD);
    Mockito.when(result.getTimeZone()).thenReturn(DateTimeZone.UTC);
    Mockito.when(result.getEffectiveDate()).thenReturn(effectiveDate);
    Mockito.when(result.getBillingPeriod()).thenReturn(billingPeriod);
    final Account account = Mockito.mock(Account.class);
    Mockito.when(account.getId()).thenReturn(accountId);
    final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class);
    Mockito.when(subscription.getId()).thenReturn(subscriptionId);
    Mockito.when(subscription.getBundleId()).thenReturn(bundleId);
    Mockito.when(result.getSubscription()).thenReturn(subscription);
    final Plan plan = Mockito.mock(Plan.class);
    Mockito.when(plan.getName()).thenReturn(planName);
    Mockito.when(result.getPlan()).thenReturn(plan);
    final PlanPhase phase = Mockito.mock(PlanPhase.class);
    Mockito.when(phase.getName()).thenReturn(phaseName);
    Mockito.when(result.getPlanPhase()).thenReturn(phase);
    Mockito.when(result.getUsages()).thenReturn(usages);
    return result;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) Account(org.killbill.billing.account.api.Account) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) Plan(org.killbill.billing.catalog.api.Plan)

Example 67 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase 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 68 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class DefaultSubscriptionDao method getSubscriptionsFromAccountId.

private Map<UUID, List<SubscriptionBase>> getSubscriptionsFromAccountId(final InternalTenantContext context) {
    final List<SubscriptionBase> allSubscriptions = transactionalSqlDao.execute(new EntitySqlDaoTransactionWrapper<List<SubscriptionBase>>() {

        @Override
        public List<SubscriptionBase> inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
            final List<SubscriptionBundleModelDao> bundleModels = entitySqlDaoWrapperFactory.become(BundleSqlDao.class).getByAccountRecordId(context);
            final List<SubscriptionModelDao> subscriptionModels = entitySqlDaoWrapperFactory.become(SubscriptionSqlDao.class).getByAccountRecordId(context);
            return new ArrayList<SubscriptionBase>(Collections2.transform(subscriptionModels, new Function<SubscriptionModelDao, SubscriptionBase>() {

                @Override
                public SubscriptionBase apply(final SubscriptionModelDao input) {
                    final SubscriptionBundleModelDao bundleModel = Iterables.find(bundleModels, new Predicate<SubscriptionBundleModelDao>() {

                        @Override
                        public boolean apply(final SubscriptionBundleModelDao bundleInput) {
                            return bundleInput.getId().equals(input.getBundleId());
                        }
                    });
                    return SubscriptionModelDao.toSubscription(input, bundleModel.getExternalKey());
                }
            }));
        }
    });
    final Map<UUID, List<SubscriptionBase>> result = new HashMap<UUID, List<SubscriptionBase>>();
    for (final SubscriptionBase subscriptionBase : allSubscriptions) {
        if (result.get(subscriptionBase.getBundleId()) == null) {
            result.put(subscriptionBase.getBundleId(), new LinkedList<SubscriptionBase>());
        }
        result.get(subscriptionBase.getBundleId()).add(subscriptionBase);
    }
    return result;
}
Also used : SubscriptionModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionModelDao) HashMap(java.util.HashMap) SubscriptionBundleModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionBundleModelDao) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) SubscriptionApiException(org.killbill.billing.entitlement.api.SubscriptionApiException) IOException(java.io.IOException) EventBusException(org.killbill.bus.api.PersistentBus.EventBusException) EntityPersistenceException(org.killbill.billing.entity.EntityPersistenceException) Predicate(com.google.common.base.Predicate) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) EntitySqlDaoWrapperFactory(org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) UUID(java.util.UUID)

Example 69 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method addCancellationAddOnForEventsIfRequired.

private List<DefaultSubscriptionBase> addCancellationAddOnForEventsIfRequired(final Collection<SubscriptionBaseEvent> events, final CatalogEntity baseProduct, final UUID bundleId, final DateTime effectiveDate, final InternalTenantContext internalTenantContext) throws CatalogApiException {
    final List<DefaultSubscriptionBase> subscriptionsToBeCancelled = new ArrayList<DefaultSubscriptionBase>();
    final List<SubscriptionBase> subscriptions = dao.getSubscriptions(bundleId, ImmutableList.<SubscriptionBaseEvent>of(), internalTenantContext);
    for (final SubscriptionBase subscription : subscriptions) {
        final DefaultSubscriptionBase cur = (DefaultSubscriptionBase) subscription;
        if (cur.getState() == EntitlementState.CANCELLED || cur.getCategory() != ProductCategory.ADD_ON) {
            continue;
        }
        final Plan addonCurrentPlan = cur.getCurrentPlan();
        if (baseProduct == null || addonUtils.isAddonIncludedFromProdName(baseProduct.getName(), addonCurrentPlan, effectiveDate, internalTenantContext) || !addonUtils.isAddonAvailableFromProdName(baseProduct.getName(), addonCurrentPlan, effectiveDate, internalTenantContext)) {
            //
            // Perform AO cancellation using the effectiveDate of the BP
            //
            final SubscriptionBaseEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder().setSubscriptionId(cur.getId()).setEffectiveDate(effectiveDate).setFromDisk(true));
            subscriptionsToBeCancelled.add(cur);
            events.add(cancelEvent);
        }
    }
    return subscriptionsToBeCancelled;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) ArrayList(java.util.ArrayList) ApiEventCancel(org.killbill.billing.subscription.events.user.ApiEventCancel) Plan(org.killbill.billing.catalog.api.Plan) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 70 with SubscriptionBase

use of org.killbill.billing.subscription.api.SubscriptionBase in project killbill by killbill.

the class DefaultSubscriptionDao method buildSubscription.

private SubscriptionBase buildSubscription(final SubscriptionBase input, final InternalTenantContext context) throws CatalogApiException {
    if (input == null) {
        return null;
    }
    final List<SubscriptionBase> bundleInput = new ArrayList<SubscriptionBase>();
    if (input.getCategory() == ProductCategory.ADD_ON) {
        final SubscriptionBase baseSubscription = getBaseSubscription(input.getBundleId(), false, context);
        if (baseSubscription == null) {
            return null;
        }
        bundleInput.add(baseSubscription);
        bundleInput.add(input);
    } else {
        bundleInput.add(input);
    }
    final List<SubscriptionBase> reloadedSubscriptions = buildBundleSubscriptions(bundleInput, null, null, context);
    for (final SubscriptionBase cur : reloadedSubscriptions) {
        if (cur.getId().equals(input.getId())) {
            return cur;
        }
    }
    throw new SubscriptionBaseError("Unexpected code path in buildSubscription");
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) ArrayList(java.util.ArrayList)

Aggregations

SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)120 Test (org.testng.annotations.Test)47 UUID (java.util.UUID)46 DateTime (org.joda.time.DateTime)40 Plan (org.killbill.billing.catalog.api.Plan)37 ArrayList (java.util.ArrayList)35 LocalDate (org.joda.time.LocalDate)33 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)32 BillingEvent (org.killbill.billing.junction.BillingEvent)30 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)26 MockPlan (org.killbill.billing.catalog.MockPlan)24 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)24 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)23 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)23 BigDecimal (java.math.BigDecimal)22 LinkedList (java.util.LinkedList)21 Invoice (org.killbill.billing.invoice.api.Invoice)21 AccountInvoices (org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices)21 BillingEventSet (org.killbill.billing.junction.BillingEventSet)21 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)20