use of org.killbill.billing.overdue.config.api.OverdueException in project killbill by killbill.
the class OverdueStateApplicator method cancelSubscriptionsIfRequired.
private void cancelSubscriptionsIfRequired(final DateTime effectiveDate, final ImmutableAccountData account, final OverdueState nextOverdueState, final InternalCallContext context) throws OverdueException {
if (nextOverdueState.getOverdueCancellationPolicy() == OverdueCancellationPolicy.NONE) {
return;
}
final CallContext callContext = internalCallContextFactory.createCallContext(context);
try {
final BillingActionPolicy actionPolicy;
switch(nextOverdueState.getOverdueCancellationPolicy()) {
case END_OF_TERM:
actionPolicy = BillingActionPolicy.END_OF_TERM;
break;
case IMMEDIATE:
actionPolicy = BillingActionPolicy.IMMEDIATE;
break;
default:
throw new IllegalStateException("Unexpected OverdueCancellationPolicy " + nextOverdueState.getOverdueCancellationPolicy());
}
final List<Entitlement> toBeCancelled = new LinkedList<Entitlement>();
computeEntitlementsToCancel(account, toBeCancelled, callContext);
try {
entitlementInternalApi.cancel(toBeCancelled, context.toLocalDate(effectiveDate), actionPolicy, ImmutableList.<PluginProperty>of(), context);
} catch (final EntitlementApiException e) {
throw new OverdueException(e);
}
} catch (final EntitlementApiException e) {
throw new OverdueException(e);
}
}
use of org.killbill.billing.overdue.config.api.OverdueException in project killbill by killbill.
the class OverdueStateApplicator method clear.
public void clear(final DateTime effectiveDate, final ImmutableAccountData account, final OverdueState previousOverdueState, final OverdueState clearState, final InternalCallContext context) throws OverdueException {
log.debug("OverdueStateApplicator:clear : time = " + effectiveDate + ", previousState = " + previousOverdueState.getName());
storeNewState(effectiveDate, account, clearState, context);
clearFutureNotification(account, context);
try {
avoid_extra_credit_by_toggling_AUTO_INVOICE_OFF(account, previousOverdueState, clearState, context);
} catch (final OverdueApiException e) {
throw new OverdueException(e);
}
final OverdueChangeInternalEvent event;
try {
event = createOverdueEvent(account, previousOverdueState.getName(), clearState.getName(), isBlockBillingTransition(previousOverdueState, clearState), isUnblockBillingTransition(previousOverdueState, clearState), context);
} catch (final BlockingApiException e) {
log.warn("Failed to create OverdueChangeInternalEvent for accountId='{}'", account.getId(), e);
return;
}
try {
bus.post(event);
} catch (final Exception e) {
log.warn("Failed to post event {}", event, e);
}
}
use of org.killbill.billing.overdue.config.api.OverdueException in project killbill by killbill.
the class OverdueStateApplicator method apply.
public void apply(final DateTime effectiveDate, final OverdueStateSet overdueStateSet, final BillingState billingState, final ImmutableAccountData account, final OverdueState previousOverdueState, final OverdueState nextOverdueState, final InternalCallContext context) throws OverdueException, OverdueApiException {
try {
if (isAccountTaggedWith_OVERDUE_ENFORCEMENT_OFF(context)) {
log.debug("OverdueStateApplicator: apply returns because account (recordId={}) is set with OVERDUE_ENFORCEMENT_OFF", context.getAccountRecordId());
return;
}
log.debug("OverdueStateApplicator: time={}, previousState={}, nextState={}, billingState={}", effectiveDate, previousOverdueState, nextOverdueState, billingState);
final OverdueState firstOverdueState = overdueStateSet.getFirstState();
final boolean conditionForNextNotfication = !nextOverdueState.isClearState() || // We did not reach the first state yet but we have an unpaid invoice
(firstOverdueState != null && billingState != null && billingState.getDateOfEarliestUnpaidInvoice() != null);
if (conditionForNextNotfication) {
final Period reevaluationInterval = getReevaluationInterval(overdueStateSet, nextOverdueState);
// If there is no configuration in the config, we assume this is because the overdue conditions are not time based and so there is nothing to retry
if (reevaluationInterval == null) {
log.debug("OverdueStateApplicator <notificationQ>: missing InitialReevaluationInterval from config, NOT inserting notification for account {}", account.getId());
} else {
log.debug("OverdueStateApplicator <notificationQ>: inserting notification for account={}, time={}", account.getId(), effectiveDate.plus(reevaluationInterval));
createFutureNotification(account, effectiveDate.plus(reevaluationInterval), context);
}
} else if (nextOverdueState.isClearState()) {
clearFutureNotification(account, context);
}
if (previousOverdueState.getName().equals(nextOverdueState.getName())) {
log.debug("OverdueStateApplicator is no-op: previousState={}, nextState={}", previousOverdueState, nextOverdueState);
return;
}
cancelSubscriptionsIfRequired(effectiveDate, account, nextOverdueState, context);
sendEmailIfRequired(account.getId(), billingState, nextOverdueState, context);
avoid_extra_credit_by_toggling_AUTO_INVOICE_OFF(account, previousOverdueState, nextOverdueState, context);
// Make sure to store the new state last here: the entitlement DAO will send a BlockingTransitionInternalEvent
// on the bus to which invoice will react. We need the latest state (including AUTO_INVOICE_OFF tag for example)
// to be present in the database first.
storeNewState(effectiveDate, account, nextOverdueState, context);
} catch (final AccountApiException e) {
throw new OverdueException(e);
}
final OverdueChangeInternalEvent event;
try {
event = createOverdueEvent(account, previousOverdueState.getName(), nextOverdueState.getName(), isBlockBillingTransition(previousOverdueState, nextOverdueState), isUnblockBillingTransition(previousOverdueState, nextOverdueState), context);
} catch (final BlockingApiException e) {
log.warn("Failed to create OverdueChangeInternalEvent for accountId='{}'", account.getId(), e);
return;
}
try {
bus.post(event);
} catch (final Exception e) {
log.warn("Failed to post event {}", event, e);
}
}
use of org.killbill.billing.overdue.config.api.OverdueException in project killbill by killbill.
the class OverdueStateApplicator method isAccountTaggedWith_OVERDUE_ENFORCEMENT_OFF.
//
// Uses callcontext information to retrieve account matching the Overduable object and check whether we should do any overdue processing
//
private boolean isAccountTaggedWith_OVERDUE_ENFORCEMENT_OFF(final InternalCallContext context) throws OverdueException {
try {
final UUID accountId = accountApi.getByRecordId(context.getAccountRecordId(), context);
final List<Tag> accountTags = tagApi.getTags(accountId, ObjectType.ACCOUNT, context);
for (final Tag cur : accountTags) {
if (cur.getTagDefinitionId().equals(ControlTagType.OVERDUE_ENFORCEMENT_OFF.getId())) {
return true;
}
}
return false;
} catch (final AccountApiException e) {
throw new OverdueException(e);
}
}
Aggregations