Search in sources :

Example 6 with InvoiceModelDao

use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.

the class InvoiceDispatcher method processParentInvoiceForAdjustmentsWithLock.

public void processParentInvoiceForAdjustmentsWithLock(final Account account, final UUID childInvoiceId, final InternalCallContext context) throws InvoiceApiException {
    final InvoiceModelDao childInvoiceModelDao = invoiceDao.getById(childInvoiceId, context);
    final InvoiceModelDao parentInvoiceModelDao = childInvoiceModelDao.getParentInvoice();
    if (parentInvoiceModelDao == null) {
        throw new InvoiceApiException(ErrorCode.INVOICE_MISSING_PARENT_INVOICE, childInvoiceModelDao.getId());
    } else if (InvoiceModelDaoHelper.getBalance(parentInvoiceModelDao).compareTo(BigDecimal.ZERO) == 0) {
        // ignore item adjustments for paid invoices.
        return;
    }
    final Long parentAccountRecordId = internalCallContextFactory.getRecordIdFromObject(account.getParentAccountId(), ObjectType.ACCOUNT, buildTenantContext(context));
    final InternalCallContext parentContext = internalCallContextFactory.createInternalCallContext(parentAccountRecordId, context);
    final String description = "Adjustment for account ".concat(account.getExternalKey());
    // find PARENT_SUMMARY invoice item for this child account
    final InvoiceItemModelDao parentSummaryInvoiceItem = Iterables.find(parentInvoiceModelDao.getInvoiceItems(), new Predicate<InvoiceItemModelDao>() {

        @Override
        public boolean apply(@Nullable final InvoiceItemModelDao input) {
            return input.getType().equals(InvoiceItemType.PARENT_SUMMARY) && input.getChildAccountId().equals(childInvoiceModelDao.getAccountId());
        }
    });
    final Iterable<InvoiceItemModelDao> childAdjustments = Iterables.filter(childInvoiceModelDao.getInvoiceItems(), new Predicate<InvoiceItemModelDao>() {

        @Override
        public boolean apply(@Nullable final InvoiceItemModelDao input) {
            return input.getType().equals(InvoiceItemType.ITEM_ADJ);
        }
    });
    // find last ITEM_ADJ invoice added in child invoice
    final InvoiceItemModelDao lastChildInvoiceItemAdjustment = Collections.max(Lists.newArrayList(childAdjustments), new Comparator<InvoiceItemModelDao>() {

        @Override
        public int compare(InvoiceItemModelDao o1, InvoiceItemModelDao o2) {
            return o1.getCreatedDate().compareTo(o2.getCreatedDate());
        }
    });
    final BigDecimal childInvoiceAdjustmentAmount = lastChildInvoiceItemAdjustment.getAmount();
    if (parentInvoiceModelDao.getStatus().equals(InvoiceStatus.COMMITTED)) {
        ItemAdjInvoiceItem adj = new ItemAdjInvoiceItem(UUIDs.randomUUID(), lastChildInvoiceItemAdjustment.getCreatedDate(), parentSummaryInvoiceItem.getInvoiceId(), parentSummaryInvoiceItem.getAccountId(), lastChildInvoiceItemAdjustment.getStartDate(), description, childInvoiceAdjustmentAmount, parentInvoiceModelDao.getCurrency(), parentSummaryInvoiceItem.getId());
        parentInvoiceModelDao.addInvoiceItem(new InvoiceItemModelDao(adj));
        invoiceDao.createInvoices(ImmutableList.<InvoiceModelDao>of(parentInvoiceModelDao), parentContext);
        return;
    }
    // update item amount
    final BigDecimal newParentInvoiceItemAmount = childInvoiceAdjustmentAmount.add(parentSummaryInvoiceItem.getAmount());
    invoiceDao.updateInvoiceItemAmount(parentSummaryInvoiceItem.getId(), newParentInvoiceItemAmount, parentContext);
}
Also used : InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) BigDecimal(java.math.BigDecimal)

Example 7 with InvoiceModelDao

use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.

the class InvoiceDispatcher method computeCBAOnExistingInvoice.

private InvoiceItem computeCBAOnExistingInvoice(final Invoice invoice, final InternalCallContext context) throws InvoiceApiException {
    // Transformation to Invoice -> InvoiceModelDao
    final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(invoice);
    final List<InvoiceItemModelDao> invoiceItemModelDaos = ImmutableList.copyOf(Collections2.transform(invoice.getInvoiceItems(), new Function<InvoiceItem, InvoiceItemModelDao>() {

        @Override
        public InvoiceItemModelDao apply(final InvoiceItem input) {
            return new InvoiceItemModelDao(input);
        }
    }));
    invoiceModelDao.addInvoiceItems(invoiceItemModelDaos);
    final InvoiceItemModelDao cbaItem = invoiceDao.doCBAComplexity(invoiceModelDao, context);
    return cbaItem != null ? InvoiceItemFactory.fromModelDao(cbaItem) : null;
}
Also used : Function(com.google.common.base.Function) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao)

Example 8 with InvoiceModelDao

use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.

the class DefaultInvoiceUserApi method createMigrationInvoice.

@Override
public UUID createMigrationInvoice(final UUID accountId, final LocalDate targetDate, final Iterable<InvoiceItem> items, final CallContext context) {
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(accountId, context);
    final LocalDate invoiceDate = internalCallContext.toLocalDate(internalCallContext.getCreatedDate());
    final InvoiceModelDao migrationInvoice = new InvoiceModelDao(accountId, invoiceDate, targetDate, items.iterator().next().getCurrency(), true);
    final List<InvoiceItemModelDao> itemModelDaos = ImmutableList.copyOf(Iterables.transform(items, new Function<InvoiceItem, InvoiceItemModelDao>() {

        @Override
        public InvoiceItemModelDao apply(final InvoiceItem input) {
            return new InvoiceItemModelDao(internalCallContext.getCreatedDate(), input.getInvoiceItemType(), migrationInvoice.getId(), accountId, input.getBundleId(), input.getSubscriptionId(), input.getDescription(), input.getPlanName(), input.getPhaseName(), input.getUsageName(), input.getStartDate(), input.getEndDate(), input.getAmount(), input.getRate(), input.getCurrency(), input.getLinkedItemId());
        }
    }));
    migrationInvoice.addInvoiceItems(itemModelDaos);
    dao.createInvoices(ImmutableList.<InvoiceModelDao>of(migrationInvoice), internalCallContext);
    return migrationInvoice.getId();
}
Also used : Function(com.google.common.base.Function) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) LocalDate(org.joda.time.LocalDate)

Example 9 with InvoiceModelDao

use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.

the class TestInvoiceDispatcher method testDryRunInvoice.

@Test(groups = "slow")
public void testDryRunInvoice() throws InvoiceApiException, AccountApiException, CatalogApiException, SubscriptionBaseApiException {
    final UUID accountId = account.getId();
    final BillingEventSet events = new MockBillingEventSet();
    final Plan plan = MockPlan.createBicycleNoTrialEvergreen1USD();
    final PlanPhase planPhase = MockPlanPhase.create1USDMonthlyEvergreen();
    final DateTime effectiveDate = clock.getUTCNow().minusDays(1);
    final Currency currency = Currency.USD;
    final BigDecimal fixedPrice = null;
    events.add(invoiceUtil.createMockBillingEvent(account, subscription, effectiveDate, plan, planPhase, fixedPrice, BigDecimal.ONE, currency, BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "", 1L, SubscriptionBaseTransitionType.CREATE));
    Mockito.when(billingApi.getBillingEventsForAccountAndUpdateAccountBCD(Mockito.<UUID>any(), Mockito.<DryRunArguments>any(), Mockito.<InternalCallContext>any())).thenReturn(events);
    final LocalDate target = internalCallContext.toLocalDate(effectiveDate);
    final InvoiceNotifier invoiceNotifier = new NullInvoiceNotifier();
    final InvoiceDispatcher dispatcher = new InvoiceDispatcher(generator, accountApi, billingApi, subscriptionApi, invoiceDao, internalCallContextFactory, invoiceNotifier, invoicePluginDispatcher, locker, busService.getBus(), null, invoiceConfig, clock, parkedAccountsManager);
    Invoice invoice = dispatcher.processAccountFromNotificationOrBusEvent(accountId, target, new DryRunFutureDateArguments(), context);
    Assert.assertNotNull(invoice);
    List<InvoiceModelDao> invoices = invoiceDao.getInvoicesByAccount(context);
    Assert.assertEquals(invoices.size(), 0);
    // Try it again to double check
    invoice = dispatcher.processAccountFromNotificationOrBusEvent(accountId, target, new DryRunFutureDateArguments(), context);
    Assert.assertNotNull(invoice);
    invoices = invoiceDao.getInvoicesByAccount(context);
    Assert.assertEquals(invoices.size(), 0);
    // This time no dry run
    invoice = dispatcher.processAccountFromNotificationOrBusEvent(accountId, target, null, context);
    Assert.assertNotNull(invoice);
    invoices = invoiceDao.getInvoicesByAccount(context);
    Assert.assertEquals(invoices.size(), 1);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) NullInvoiceNotifier(org.killbill.billing.invoice.notification.NullInvoiceNotifier) InvoiceNotifier(org.killbill.billing.invoice.api.InvoiceNotifier) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) NullInvoiceNotifier(org.killbill.billing.invoice.notification.NullInvoiceNotifier) BillingEventSet(org.killbill.billing.junction.BillingEventSet) Currency(org.killbill.billing.catalog.api.Currency) DryRunFutureDateArguments(org.killbill.billing.invoice.TestInvoiceHelper.DryRunFutureDateArguments) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 10 with InvoiceModelDao

use of org.killbill.billing.invoice.dao.InvoiceModelDao in project killbill by killbill.

the class DefaultInvoiceUserApi method getInvoiceByPayment.

@Override
public Invoice getInvoiceByPayment(final UUID paymentId, final TenantContext context) throws InvoiceApiException {
    final InternalTenantContext tenantContext = internalCallContextFactory.createInternalTenantContext(paymentId, ObjectType.PAYMENT, context);
    final UUID invoiceId = dao.getInvoiceIdByPaymentId(paymentId, tenantContext);
    if (invoiceId == null) {
        throw new InvoiceApiException(ErrorCode.INVOICE_NOT_FOUND, paymentId);
    }
    final InvoiceModelDao invoiceModelDao = dao.getById(invoiceId, tenantContext);
    return new DefaultInvoice(invoiceModelDao);
}
Also used : InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice)

Aggregations

InvoiceModelDao (org.killbill.billing.invoice.dao.InvoiceModelDao)18 InvoiceItemModelDao (org.killbill.billing.invoice.dao.InvoiceItemModelDao)10 Invoice (org.killbill.billing.invoice.api.Invoice)9 BigDecimal (java.math.BigDecimal)8 UUID (java.util.UUID)7 LocalDate (org.joda.time.LocalDate)7 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)7 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)6 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)5 Test (org.testng.annotations.Test)5 Currency (org.killbill.billing.catalog.api.Currency)4 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)4 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)4 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)4 Function (com.google.common.base.Function)3 ArrayList (java.util.ArrayList)3 DateTime (org.joda.time.DateTime)3 AccountApiException (org.killbill.billing.account.api.AccountApiException)3 MockPlan (org.killbill.billing.catalog.MockPlan)3 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)3