use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.
the class DefaultSubscriptionInternalApi method getDefaultBillCycleDayLocal.
@Override
public int getDefaultBillCycleDayLocal(final Map<UUID, Integer> bcdCache, final SubscriptionBase subscription, final SubscriptionBase baseSubscription, final PlanPhaseSpecifier planPhaseSpecifier, final DateTimeZone accountTimeZone, final int accountBillCycleDayLocal, final DateTime effectiveDate, final InternalTenantContext context) throws SubscriptionBaseApiException {
try {
final Catalog catalog = catalogService.getFullCatalog(true, true, context);
final BillingAlignment alignment = catalog.billingAlignment(planPhaseSpecifier, effectiveDate);
return BillCycleDayCalculator.calculateBcdForAlignment(bcdCache, subscription, baseSubscription, alignment, accountTimeZone, accountBillCycleDayLocal);
} catch (final CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
}
use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.
the class DefaultSubscriptionBaseApiService method cancelWithPolicyNoValidation.
@Override
public boolean cancelWithPolicyNoValidation(final Iterable<DefaultSubscriptionBase> subscriptions, final BillingActionPolicy policy, final DateTimeZone accountTimeZone, final int accountBillCycleDayLocal, final InternalCallContext context) throws SubscriptionBaseApiException {
final Map<DefaultSubscriptionBase, DateTime> subscriptionsWithEffectiveDate = new HashMap<DefaultSubscriptionBase, DateTime>();
final DateTime now = clock.getUTCNow();
try {
for (final DefaultSubscriptionBase subscription : subscriptions) {
final BillingAlignment billingAlignment = (subscription.getState() == EntitlementState.PENDING ? null : catalogService.getFullCatalog(true, true, context).billingAlignment(new PlanPhaseSpecifier(subscription.getLastActivePlan().getName(), subscription.getLastActivePhase().getPhaseType()), clock.getUTCNow()));
final DateTime effectiveDate = subscription.getPlanChangeEffectiveDate(policy, billingAlignment, accountTimeZone, accountBillCycleDayLocal, context);
subscriptionsWithEffectiveDate.put(subscription, effectiveDate);
}
} catch (final CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
return doCancelPlan(subscriptionsWithEffectiveDate, now, context);
}
use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.
the class DefaultInternalBillingApi method addBillingEventsForSubscription.
private void addBillingEventsForSubscription(final ImmutableAccountData account, @Nullable final List<SubscriptionBase> subscriptions, final SubscriptionBase baseSubscription, final int currentAccountBCD, final InternalCallContext context, final DefaultBillingEventSet result, final Set<UUID> skipSubscriptionsSet, final VersionedCatalog catalog) throws SubscriptionBaseApiException, CatalogApiException {
if (subscriptions == null) {
return;
}
final Map<UUID, Integer> bcdCache = new HashMap<UUID, Integer>();
for (final SubscriptionBase subscription : subscriptions) {
// TODO Can we batch those ?
final List<SubscriptionBillingEvent> billingTransitions = subscriptionApi.getSubscriptionBillingEvents(catalog, subscription, context);
if (billingTransitions.isEmpty() || (billingTransitions.get(0).getType() != SubscriptionBaseTransitionType.CREATE && billingTransitions.get(0).getType() != SubscriptionBaseTransitionType.TRANSFER)) {
log.warn("Skipping billing events for subscription " + subscription.getId() + ": Does not start with a valid CREATE transition");
skipSubscriptionsSet.add(subscription.getId());
return;
}
Integer overridenBCD = null;
int bcdLocal = 0;
BillingAlignment alignment = null;
for (final SubscriptionBillingEvent transition : billingTransitions) {
if (transition.getType() != SubscriptionBaseTransitionType.CANCEL) {
final PlanPhaseSpecifier spec = new PlanPhaseSpecifier(transition.getPlan().getName(), transition.getPlanPhase().getPhaseType());
alignment = subscription.getBillingAlignment(spec, transition.getEffectiveDate(), catalog);
//
// A BCD_CHANGE transition defines a new billCycleDayLocal for the subscription and this overrides whatever computation
// occurs below (which is based on billing alignment policy). Also multiple of those BCD_CHANGE transitions could occur,
// to define different intervals with different billing cycle days.
//
overridenBCD = transition.getBcdLocal() != null ? transition.getBcdLocal() : overridenBCD;
bcdLocal = overridenBCD != null ? overridenBCD : calculateBcdForTransition(alignment, bcdCache, baseSubscription, subscription, currentAccountBCD, context);
}
final BillingEvent event = new DefaultBillingEvent(transition, subscription, bcdLocal, alignment, account.getCurrency());
result.add(event);
}
}
}
use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.
the class DefaultSubscriptionBase method getBillingAlignment.
@Override
public BillingAlignment getBillingAlignment(final PlanPhaseSpecifier spec, final DateTime transitionTime, final VersionedCatalog publicCatalog) throws SubscriptionBaseApiException {
try {
final SubscriptionCatalog catalog = DefaultSubscriptionCatalogApi.wrapCatalog(publicCatalog, clock);
final SubscriptionBaseTransition transition = (getState() == EntitlementState.PENDING) ? getPendingTransition() : getLastTransitionForCurrentPlan();
final BillingAlignment alignment = catalog.billingAlignment(spec, transitionTime, transition.getEffectiveTransitionTime());
return alignment;
} catch (final CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
}
use of org.killbill.billing.catalog.api.BillingAlignment in project killbill by killbill.
the class EventsStreamBuilder method buildForEntitlement.
private EventsStream buildForEntitlement(final ImmutableAccountData account, final SubscriptionBaseBundle bundle, @Nullable final SubscriptionBase baseSubscription, final SubscriptionBase subscription, final Collection<SubscriptionBase> allSubscriptionsForBundle, final Collection<BlockingState> blockingStates, final int accountBCD, final Map<UUID, Integer> bcdCache, final VersionedCatalog catalog, final InternalTenantContext internalTenantContext) throws EntitlementApiException {
try {
Integer defaultAlignmentDay = null;
final BillingAlignment alignment = subscription.getBillingAlignment(createPlanPhaseSpecifier(subscription), clock.getUTCNow(), catalog);
if (alignment != BillingAlignment.ACCOUNT || accountBCD != 0) {
defaultAlignmentDay = BillCycleDayCalculator.calculateBcdForAlignment(bcdCache, subscription, baseSubscription, alignment, internalTenantContext, accountBCD);
}
return new DefaultEventsStream(account, bundle, blockingStates, checker, baseSubscription, subscription, allSubscriptionsForBundle, defaultAlignmentDay, internalTenantContext, clock.getUTCNow());
} catch (final SubscriptionBaseApiException e) {
throw new EntitlementApiException(e);
}
}
Aggregations