use of org.killbill.billing.account.api.AccountApiException 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);
}
use of org.killbill.billing.account.api.AccountApiException in project killbill by killbill.
the class DefaultInvoiceUserApi method transferChildCreditToParent.
@Override
public void transferChildCreditToParent(final UUID childAccountId, final CallContext context) throws InvoiceApiException {
final Account childAccount;
final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(childAccountId, ObjectType.ACCOUNT, context);
try {
childAccount = accountUserApi.getAccountById(childAccountId, internalCallContext);
} catch (AccountApiException e) {
throw new InvoiceApiException(e);
}
if (childAccount.getParentAccountId() == null) {
throw new InvoiceApiException(ErrorCode.ACCOUNT_DOES_NOT_HAVE_PARENT_ACCOUNT, childAccountId);
}
final BigDecimal accountCBA = getAccountCBA(childAccountId, context);
if (accountCBA.compareTo(BigDecimal.ZERO) <= 0) {
throw new InvoiceApiException(ErrorCode.CHILD_ACCOUNT_MISSING_CREDIT, childAccountId);
}
dao.transferChildCreditToParent(childAccount, internalCallContext);
}
use of org.killbill.billing.account.api.AccountApiException in project killbill by killbill.
the class InvoiceTestUtils method createAndPersistInvoice.
public static Invoice createAndPersistInvoice(final TestInvoiceHelper testInvoiceHelper, final InvoiceDao invoiceDao, final Clock clock, final Iterable<BigDecimal> amounts, final Currency currency, final InternalCallContext internalCallContext) throws EntityPersistenceException {
final Invoice invoice = Mockito.mock(Invoice.class);
final UUID invoiceId = UUID.randomUUID();
final UUID accountId;
try {
final Account account = testInvoiceHelper.createAccount(internalCallContext.toCallContext(null));
accountId = account.getId();
} catch (final AccountApiException e) {
Assert.fail(e.getMessage());
return null;
}
Mockito.when(invoice.getId()).thenReturn(invoiceId);
Mockito.when(invoice.getAccountId()).thenReturn(accountId);
Mockito.when(invoice.getInvoiceDate()).thenReturn(clock.getUTCToday());
Mockito.when(invoice.getTargetDate()).thenReturn(clock.getUTCToday());
Mockito.when(invoice.getCurrency()).thenReturn(currency);
Mockito.when(invoice.isMigrationInvoice()).thenReturn(false);
Mockito.when(invoice.getStatus()).thenReturn(InvoiceStatus.COMMITTED);
final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
final List<InvoiceItemModelDao> invoiceModelItems = new ArrayList<InvoiceItemModelDao>();
for (final BigDecimal amount : amounts) {
final InvoiceItem invoiceItem = createInvoiceItem(clock, invoiceId, accountId, amount, currency);
invoiceModelItems.add(new InvoiceItemModelDao(invoiceItem));
invoiceItems.add(invoiceItem);
}
Mockito.when(invoice.getInvoiceItems()).thenReturn(invoiceItems);
final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(invoice);
invoiceModelDao.addInvoiceItems(invoiceModelItems);
invoiceDao.createInvoices(ImmutableList.<InvoiceModelDao>of(invoiceModelDao), internalCallContext);
return invoice;
}
use of org.killbill.billing.account.api.AccountApiException 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.account.api.AccountApiException 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