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();
}
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();
}
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;
}
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());
}
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);
}
}
Aggregations