Search in sources :

Example 76 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultInvoiceUserApi method getInvoiceAsHTML.

@Override
public String getInvoiceAsHTML(final UUID invoiceId, final TenantContext context) throws AccountApiException, IOException, InvoiceApiException {
    final Invoice invoice = getInvoice(invoiceId, context);
    final InternalTenantContext internalContext = internalCallContextFactory.createInternalTenantContext(invoiceId, ObjectType.INVOICE, context);
    final Account account = accountUserApi.getAccountById(invoice.getAccountId(), internalContext);
    // Check if this account has the MANUAL_PAY system tag
    boolean manualPay = false;
    final List<Tag> accountTags = tagApi.getTags(account.getId(), ObjectType.ACCOUNT, internalContext);
    for (final Tag tag : accountTags) {
        if (ControlTagType.MANUAL_PAY.getId().equals(tag.getTagDefinitionId())) {
            manualPay = true;
            break;
        }
    }
    final HtmlInvoice htmlInvoice = generator.generateInvoice(account, invoice, manualPay, internalContext);
    return htmlInvoice.getBody();
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) HtmlInvoice(org.killbill.billing.invoice.template.HtmlInvoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) HtmlInvoice(org.killbill.billing.invoice.template.HtmlInvoice) Tag(org.killbill.billing.util.tag.Tag)

Example 77 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultInvoiceUserApi method getInvoiceByNumber.

@Override
public Invoice getInvoiceByNumber(final Integer number, final TenantContext context) throws InvoiceApiException {
    // The account record id will be populated in the DAO
    final InternalTenantContext internalTenantContextWithoutAccountRecordId = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(context);
    final InvoiceModelDao invoice = dao.getByNumber(number, internalTenantContextWithoutAccountRecordId);
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(invoice.getAccountId(), context);
    return new DefaultInvoice(invoice, getCatalogSafelyForPrettyNames(internalTenantContext));
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice)

Example 78 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultInvoiceDao method transferChildCreditToParent.

@Override
public void transferChildCreditToParent(final Account childAccount, final InternalCallContext childAccountContext) throws InvoiceApiException {
    // Need to create an internalCallContext for parent account because it's needed to save the correct accountRecordId in Invoice tables.
    // Then it's used to load invoices by account.
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(childAccount.getParentAccountId(), childAccountContext);
    final InternalCallContext parentAccountContext = internalCallContextFactory.createInternalCallContext(internalTenantContext.getAccountRecordId(), childAccountContext);
    final List<Tag> parentInvoicesTags = getInvoicesTags(parentAccountContext);
    final List<Tag> childInvoicesTags = getInvoicesTags(childAccountContext);
    transactionalSqlDao.execute(false, new EntitySqlDaoTransactionWrapper<Void>() {

        @Override
        public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception {
            final InvoiceSqlDao invoiceSqlDao = entitySqlDaoWrapperFactory.become(InvoiceSqlDao.class);
            final InvoiceItemSqlDao transInvoiceItemSqlDao = entitySqlDaoWrapperFactory.become(InvoiceItemSqlDao.class);
            // create child and parent invoices
            final DateTime childCreatedDate = childAccountContext.getCreatedDate();
            final BigDecimal accountCBA = getAccountCBA(childAccount.getId(), childAccountContext);
            // create external charge to child account
            final LocalDate childInvoiceDate = childAccountContext.toLocalDate(childAccountContext.getCreatedDate());
            final Invoice invoiceForExternalCharge = new DefaultInvoice(childAccount.getId(), childInvoiceDate, childCreatedDate.toLocalDate(), childAccount.getCurrency(), InvoiceStatus.COMMITTED);
            final String chargeDescription = "Charge to move credit from child to parent account";
            final InvoiceItem externalChargeItem = new ExternalChargeInvoiceItem(UUIDs.randomUUID(), childCreatedDate, invoiceForExternalCharge.getId(), childAccount.getId(), null, chargeDescription, childCreatedDate.toLocalDate(), childCreatedDate.toLocalDate(), accountCBA, childAccount.getCurrency(), null);
            invoiceForExternalCharge.addInvoiceItem(externalChargeItem);
            // create credit to parent account
            final LocalDate parentInvoiceDate = parentAccountContext.toLocalDate(parentAccountContext.getCreatedDate());
            final Invoice invoiceForCredit = new DefaultInvoice(childAccount.getParentAccountId(), parentInvoiceDate, childCreatedDate.toLocalDate(), childAccount.getCurrency(), InvoiceStatus.COMMITTED);
            final String creditDescription = "Credit migrated from child account " + childAccount.getId();
            final InvoiceItem creditItem = new CreditAdjInvoiceItem(UUIDs.randomUUID(), childCreatedDate, invoiceForCredit.getId(), childAccount.getParentAccountId(), childCreatedDate.toLocalDate(), creditDescription, // Note! The amount is negated here!
            accountCBA.negate(), childAccount.getCurrency(), null);
            invoiceForCredit.addInvoiceItem(creditItem);
            // save invoices and invoice items
            final InvoiceModelDao childInvoice = new InvoiceModelDao(invoiceForExternalCharge);
            createAndRefresh(invoiceSqlDao, childInvoice, childAccountContext);
            final InvoiceItemModelDao childExternalChargeItem = new InvoiceItemModelDao(externalChargeItem);
            createInvoiceItemFromTransaction(transInvoiceItemSqlDao, childExternalChargeItem, childAccountContext);
            // Keep invoice up-to-date for CBA below
            childInvoice.addInvoiceItem(childExternalChargeItem);
            final InvoiceModelDao parentInvoice = new InvoiceModelDao(invoiceForCredit);
            createAndRefresh(invoiceSqlDao, parentInvoice, parentAccountContext);
            final InvoiceItemModelDao parentCreditItem = new InvoiceItemModelDao(creditItem);
            createInvoiceItemFromTransaction(transInvoiceItemSqlDao, parentCreditItem, parentAccountContext);
            // Keep invoice up-to-date for CBA below
            parentInvoice.addInvoiceItem(parentCreditItem);
            // Create Mapping relation
            final InvoiceParentChildrenSqlDao transactional = entitySqlDaoWrapperFactory.become(InvoiceParentChildrenSqlDao.class);
            final InvoiceParentChildModelDao invoiceRelation = new InvoiceParentChildModelDao(parentInvoice.getId(), childInvoice.getId(), childInvoice.getAccountId());
            createAndRefresh(transactional, invoiceRelation, parentAccountContext);
            // Add child CBA complexity and notify bus on child invoice creation
            final CBALogicWrapper childCbaWrapper = new CBALogicWrapper(childAccount.getId(), childInvoicesTags, childAccountContext, entitySqlDaoWrapperFactory);
            childCbaWrapper.runCBALogicWithNotificationEvents(ImmutableSet.of(), ImmutableSet.of(childInvoice.getId()), ImmutableList.of(childInvoice));
            notifyBusOfInvoiceCreation(entitySqlDaoWrapperFactory, childInvoice, childAccountContext);
            // Add parent CBA complexity and notify bus on child invoice creation
            final CBALogicWrapper cbaWrapper = new CBALogicWrapper(childAccount.getParentAccountId(), parentInvoicesTags, parentAccountContext, entitySqlDaoWrapperFactory);
            cbaWrapper.runCBALogicWithNotificationEvents(ImmutableSet.of(), ImmutableSet.of(parentInvoice.getId()), ImmutableList.of(parentInvoice));
            notifyBusOfInvoiceCreation(entitySqlDaoWrapperFactory, parentInvoice, parentAccountContext);
            return null;
        }
    });
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) EventBusException(org.killbill.bus.api.PersistentBus.EventBusException) EntityPersistenceException(org.killbill.billing.entity.EntityPersistenceException) BigDecimal(java.math.BigDecimal) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) EntitySqlDaoWrapperFactory(org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory) Tag(org.killbill.billing.util.tag.Tag)

Example 79 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class InvoiceDaoHelper method setParentInvoice.

private void setParentInvoice(final InvoiceModelDao invoice, final List<Tag> invoicesTags, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalTenantContext childContext) {
    final InvoiceParentChildrenSqlDao invoiceParentChildrenSqlDao = entitySqlDaoWrapperFactory.become(InvoiceParentChildrenSqlDao.class);
    final List<InvoiceParentChildModelDao> mappings = invoiceParentChildrenSqlDao.getParentChildMappingsByChildInvoiceIds(ImmutableList.of(invoice.getId().toString()), childContext);
    if (mappings.isEmpty()) {
        return;
    }
    Preconditions.checkState(mappings.size() == 1, String.format("Expected only one parent mapping for invoice %s", invoice.getId()));
    final UUID parentInvoiceId = mappings.get(0).getParentInvoiceId();
    final InvoiceSqlDao invoiceSqlDao = entitySqlDaoWrapperFactory.become(InvoiceSqlDao.class);
    final InvoiceModelDao parentInvoice = invoiceSqlDao.getById(parentInvoiceId.toString(), childContext);
    final Long parentAccountRecordId = internalCallContextFactory.getRecordIdFromObject(parentInvoice.getAccountId(), ObjectType.ACCOUNT, internalCallContextFactory.createTenantContext(childContext));
    final InternalTenantContext parentContext = internalCallContextFactory.createInternalTenantContext(childContext.getTenantRecordId(), parentAccountRecordId);
    // Note the misnomer here, populateChildren simply populates the content of these invoices (unrelated to HA)
    populateChildren(parentInvoice, invoicesTags, entitySqlDaoWrapperFactory, parentContext);
    invoice.addParentInvoice(parentInvoice);
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) UUID(java.util.UUID)

Example 80 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultSubscriptionBaseTimelineApi method getBundleTimeline.

@Override
public BundleBaseTimeline getBundleTimeline(final SubscriptionBaseBundle bundle, final TenantContext context) throws SubscriptionBaseRepairException {
    try {
        final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(bundle.getAccountId(), context);
        final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(internalTenantContext);
        final List<DefaultSubscriptionBase> subscriptions = dao.getSubscriptions(bundle.getId(), ImmutableList.<SubscriptionBaseEvent>of(), catalog, internalTenantContext);
        if (subscriptions.size() == 0) {
            throw new SubscriptionBaseRepairException(ErrorCode.SUB_NO_ACTIVE_SUBSCRIPTIONS, bundle.getId());
        }
        final List<SubscriptionBaseTimeline> repairs = createGetSubscriptionRepairList(subscriptions, Collections.<SubscriptionBaseTimeline>emptyList(), catalog, internalTenantContext);
        return createGetBundleRepair(bundle.getId(), bundle.getExternalKey(), repairs);
    } catch (CatalogApiException e) {
        throw new SubscriptionBaseRepairException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog)

Aggregations

InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)92 UUID (java.util.UUID)15 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)13 CacheLoaderArgument (org.killbill.billing.util.cache.CacheLoaderArgument)11 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)10 ArrayList (java.util.ArrayList)9 ObjectType (org.killbill.billing.ObjectType)9 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)9 TenantContext (org.killbill.billing.util.callcontext.TenantContext)9 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 DateTime (org.joda.time.DateTime)7 LocalDate (org.joda.time.LocalDate)7 Predicate (com.google.common.base.Predicate)6 AccountApiException (org.killbill.billing.account.api.AccountApiException)6 VersionedCatalog (org.killbill.billing.catalog.api.VersionedCatalog)6 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)6