Search in sources :

Example 86 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.

the class DefaultInvoiceUserApi method insertCreditForInvoice.

private InvoiceItem insertCreditForInvoice(final UUID accountId, final UUID invoiceId, final BigDecimal amount, final LocalDate effectiveDate, final Currency currency, final boolean autoCommit, final String description, final CallContext context) throws InvoiceApiException {
    if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
        throw new InvoiceApiException(ErrorCode.CREDIT_AMOUNT_INVALID, amount);
    }
    final WithAccountLock withAccountLock = new WithAccountLock() {

        private InvoiceItem creditItem;

        @Override
        public List<Invoice> prepareInvoices() throws InvoiceApiException {
            final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(accountId, context);
            final LocalDate invoiceDate = internalTenantContext.toLocalDate(context.getCreatedDate());
            // Create an invoice for that credit if it doesn't exist
            final Invoice invoiceForCredit;
            if (invoiceId == null) {
                final InvoiceStatus status = autoCommit ? InvoiceStatus.COMMITTED : InvoiceStatus.DRAFT;
                invoiceForCredit = new DefaultInvoice(accountId, invoiceDate, effectiveDate, currency, status);
            } else {
                invoiceForCredit = getInvoiceAndCheckCurrency(invoiceId, currency, context);
                if (InvoiceStatus.COMMITTED.equals(invoiceForCredit.getStatus())) {
                    throw new InvoiceApiException(ErrorCode.INVOICE_ALREADY_COMMITTED, invoiceId);
                }
            }
            // Create the new credit
            creditItem = new CreditAdjInvoiceItem(UUIDs.randomUUID(), context.getCreatedDate(), invoiceForCredit.getId(), accountId, effectiveDate, description, // Note! The amount is negated here!
            amount.negate(), currency);
            invoiceForCredit.addInvoiceItem(creditItem);
            return ImmutableList.<Invoice>of(invoiceForCredit);
        }
    };
    final Collection<InvoiceItem> creditInvoiceItems = Collections2.<InvoiceItem>filter(invoiceApiHelper.dispatchToInvoicePluginsAndInsertItems(accountId, false, withAccountLock, context), new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem invoiceItem) {
            return InvoiceItemType.CREDIT_ADJ.equals(invoiceItem.getInvoiceItemType());
        }
    });
    Preconditions.checkState(creditInvoiceItems.size() == 1, "Should have created a single credit invoice item: " + creditInvoiceItems);
    return creditInvoiceItems.iterator().next();
}
Also used : InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) 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) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) WithAccountLock(org.killbill.billing.invoice.api.WithAccountLock) LocalDate(org.joda.time.LocalDate) InvoiceStatus(org.killbill.billing.invoice.api.InvoiceStatus) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice)

Example 87 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem 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 88 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.

the class InvoicePluginDispatcher method getAdditionalInvoiceItems.

//
// If we have multiple plugins there is a question of plugin ordering and also a 'product' questions to decide whether
// subsequent plugins should have access to items added by previous plugins
//
public List<InvoiceItem> getAdditionalInvoiceItems(final Invoice originalInvoice, final boolean isDryRun, final CallContext callContext) throws InvoiceApiException {
    // We clone the original invoice so plugins don't remove/add items
    final Invoice clonedInvoice = (Invoice) ((DefaultInvoice) originalInvoice).clone();
    final List<InvoiceItem> additionalInvoiceItems = new LinkedList<InvoiceItem>();
    final List<InvoicePluginApi> invoicePlugins = getInvoicePlugins();
    for (final InvoicePluginApi invoicePlugin : invoicePlugins) {
        final List<InvoiceItem> items = invoicePlugin.getAdditionalInvoiceItems(clonedInvoice, isDryRun, ImmutableList.<PluginProperty>of(), callContext);
        if (items != null) {
            for (final InvoiceItem item : items) {
                validateInvoiceItemFromPlugin(item, invoicePlugin);
                additionalInvoiceItems.add(item);
            }
        }
    }
    return additionalInvoiceItems;
}
Also used : InvoicePluginApi(org.killbill.billing.invoice.plugin.api.InvoicePluginApi) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) LinkedList(java.util.LinkedList)

Example 89 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.

the class TestInvoiceItemJsonSimple method testFromInvoiceItem.

@Test(groups = "fast")
public void testFromInvoiceItem() throws Exception {
    final InvoiceItem invoiceItem = Mockito.mock(InvoiceItem.class);
    Mockito.when(invoiceItem.getId()).thenReturn(UUID.randomUUID());
    Mockito.when(invoiceItem.getInvoiceId()).thenReturn(UUID.randomUUID());
    Mockito.when(invoiceItem.getLinkedItemId()).thenReturn(UUID.randomUUID());
    Mockito.when(invoiceItem.getAccountId()).thenReturn(UUID.randomUUID());
    Mockito.when(invoiceItem.getBundleId()).thenReturn(UUID.randomUUID());
    Mockito.when(invoiceItem.getSubscriptionId()).thenReturn(UUID.randomUUID());
    Mockito.when(invoiceItem.getPlanName()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(invoiceItem.getPhaseName()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(invoiceItem.getUsageName()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(invoiceItem.getDescription()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(invoiceItem.getStartDate()).thenReturn(clock.getUTCToday());
    Mockito.when(invoiceItem.getEndDate()).thenReturn(clock.getUTCToday());
    Mockito.when(invoiceItem.getAmount()).thenReturn(BigDecimal.TEN);
    Mockito.when(invoiceItem.getCurrency()).thenReturn(Currency.EUR);
    Mockito.when(invoiceItem.getInvoiceItemType()).thenReturn(InvoiceItemType.FIXED);
    final InvoiceItemJson invoiceItemJson = new InvoiceItemJson(invoiceItem);
    Assert.assertEquals(invoiceItemJson.getInvoiceItemId(), invoiceItem.getId().toString());
    Assert.assertEquals(invoiceItemJson.getInvoiceId(), invoiceItem.getInvoiceId().toString());
    Assert.assertEquals(invoiceItemJson.getLinkedInvoiceItemId(), invoiceItem.getLinkedItemId().toString());
    Assert.assertEquals(invoiceItemJson.getAccountId(), invoiceItem.getAccountId().toString());
    Assert.assertEquals(invoiceItemJson.getBundleId(), invoiceItem.getBundleId().toString());
    Assert.assertEquals(invoiceItemJson.getSubscriptionId(), invoiceItem.getSubscriptionId().toString());
    Assert.assertEquals(invoiceItemJson.getPlanName(), invoiceItem.getPlanName());
    Assert.assertEquals(invoiceItemJson.getPhaseName(), invoiceItem.getPhaseName());
    Assert.assertEquals(invoiceItemJson.getUsageName(), invoiceItem.getUsageName());
    Assert.assertEquals(invoiceItemJson.getDescription(), invoiceItem.getDescription());
    Assert.assertEquals(invoiceItemJson.getStartDate(), invoiceItem.getStartDate());
    Assert.assertEquals(invoiceItemJson.getEndDate(), invoiceItem.getEndDate());
    Assert.assertEquals(invoiceItemJson.getAmount(), invoiceItem.getAmount());
    Assert.assertEquals(invoiceItemJson.getCurrency(), invoiceItem.getCurrency().name());
}
Also used : InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) Test(org.testng.annotations.Test)

Example 90 with InvoiceItem

use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.

the class AuditChecker method checkInvoiceCreated.

/**
     * ********************************************  INVOICE *******************************************************
     */
public void checkInvoiceCreated(final Invoice invoice, final CallContext context) {
    final List<AuditLog> invoiceLogs = getAuditLogForInvoice(invoice, context);
    Assert.assertEquals(invoiceLogs.size(), 1);
    checkAuditLog(ChangeType.INSERT, context, invoiceLogs.get(0), invoice.getId(), InvoiceSqlDao.class, false, false);
    for (InvoiceItem cur : invoice.getInvoiceItems()) {
        final List<AuditLog> auditLogs = getAuditLogForInvoiceItem(cur, context);
        Assert.assertEquals(auditLogs.size(), 1);
        checkAuditLog(ChangeType.INSERT, context, auditLogs.get(0), cur.getId(), InvoiceItemSqlDao.class, false, false);
    }
}
Also used : InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) AuditLog(org.killbill.billing.util.audit.AuditLog)

Aggregations

InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)168 LocalDate (org.joda.time.LocalDate)118 Test (org.testng.annotations.Test)109 BigDecimal (java.math.BigDecimal)103 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)97 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)92 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)79 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)76 UUID (java.util.UUID)68 Invoice (org.killbill.billing.invoice.api.Invoice)57 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)36 ExternalChargeInvoiceItem (org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)32 BillingEvent (org.killbill.billing.junction.BillingEvent)27 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)25 ArrayList (java.util.ArrayList)23 DateTime (org.joda.time.DateTime)23 MockPlan (org.killbill.billing.catalog.MockPlan)23 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)23 Plan (org.killbill.billing.catalog.api.Plan)22 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)21