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;
}
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;
}
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;
}
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;
}
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");
}
Aggregations