Search in sources :

Example 21 with AccountApiException

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);
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) HashMap(java.util.HashMap) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) BlockingState(org.killbill.billing.entitlement.api.BlockingState) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) AccountApiException(org.killbill.billing.account.api.AccountApiException) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 22 with AccountApiException

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);
}
Also used : Account(org.killbill.billing.account.api.Account) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) AccountApiException(org.killbill.billing.account.api.AccountApiException) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) BigDecimal(java.math.BigDecimal)

Example 23 with AccountApiException

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;
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) AccountApiException(org.killbill.billing.account.api.AccountApiException) ArrayList(java.util.ArrayList) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal)

Example 24 with AccountApiException

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);
    }
}
Also used : AccountApiException(org.killbill.billing.account.api.AccountApiException) Period(org.joda.time.Period) OverdueException(org.killbill.billing.overdue.config.api.OverdueException) OverdueChangeInternalEvent(org.killbill.billing.events.OverdueChangeInternalEvent) BlockingApiException(org.killbill.billing.entitlement.api.BlockingApiException) OverdueState(org.killbill.billing.overdue.api.OverdueState) TagApiException(org.killbill.billing.util.api.TagApiException) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) BlockingApiException(org.killbill.billing.entitlement.api.BlockingApiException) AccountApiException(org.killbill.billing.account.api.AccountApiException) EmailApiException(org.killbill.billing.util.email.EmailApiException) OverdueException(org.killbill.billing.overdue.config.api.OverdueException) MustacheException(com.samskivert.mustache.MustacheException) IOException(java.io.IOException) OverdueApiException(org.killbill.billing.overdue.api.OverdueApiException)

Example 25 with AccountApiException

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);
    }
}
Also used : AccountApiException(org.killbill.billing.account.api.AccountApiException) OverdueException(org.killbill.billing.overdue.config.api.OverdueException) Tag(org.killbill.billing.util.tag.Tag) UUID(java.util.UUID)

Aggregations

AccountApiException (org.killbill.billing.account.api.AccountApiException)36 UUID (java.util.UUID)17 Account (org.killbill.billing.account.api.Account)16 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)12 DefaultAccount (org.killbill.billing.account.api.DefaultAccount)10 ArrayList (java.util.ArrayList)7 ImmutableAccountData (org.killbill.billing.account.api.ImmutableAccountData)7 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)7 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)6 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)6 CallContext (org.killbill.billing.util.callcontext.CallContext)6 BigDecimal (java.math.BigDecimal)5 HashMap (java.util.HashMap)5 AccountModelDao (org.killbill.billing.account.dao.AccountModelDao)5 Invoice (org.killbill.billing.invoice.api.Invoice)5 AllowConcurrentEvents (com.google.common.eventbus.AllowConcurrentEvents)4 Subscribe (com.google.common.eventbus.Subscribe)4 LocalDate (org.joda.time.LocalDate)4 DefaultMutableAccountData (org.killbill.billing.account.api.DefaultMutableAccountData)4 PaymentMethod (org.killbill.billing.payment.api.PaymentMethod)4