use of org.killbill.billing.invoice.dao.InvoiceItemModelDao in project killbill by killbill.
the class InvoiceApiHelper method dispatchToInvoicePluginsAndInsertItems.
public List<InvoiceItem> dispatchToInvoicePluginsAndInsertItems(final UUID accountId, final boolean isDryRun, final WithAccountLock withAccountLock, final CallContext context) throws InvoiceApiException {
GlobalLock lock = null;
try {
lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), accountId.toString(), invoiceConfig.getMaxGlobalLockRetries());
final Iterable<Invoice> invoicesForPlugins = withAccountLock.prepareInvoices();
final List<InvoiceModelDao> invoiceModelDaos = new LinkedList<InvoiceModelDao>();
for (final Invoice invoiceForPlugin : invoicesForPlugins) {
// Call plugin
final List<InvoiceItem> additionalInvoiceItems = invoicePluginDispatcher.getAdditionalInvoiceItems(invoiceForPlugin, isDryRun, context);
invoiceForPlugin.addInvoiceItems(additionalInvoiceItems);
// Transformation to InvoiceModelDao
final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(invoiceForPlugin);
final List<InvoiceItem> invoiceItems = invoiceForPlugin.getInvoiceItems();
final List<InvoiceItemModelDao> invoiceItemModelDaos = toInvoiceItemModelDao(invoiceItems);
invoiceModelDao.addInvoiceItems(invoiceItemModelDaos);
// Keep track of modified invoices
invoiceModelDaos.add(invoiceModelDao);
}
final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(accountId, context);
final List<InvoiceItemModelDao> createdInvoiceItems = dao.createInvoices(invoiceModelDaos, internalCallContext);
return fromInvoiceItemModelDao(createdInvoiceItems);
} catch (final LockFailedException e) {
log.warn("Failed to process invoice items for accountId='{}'", accountId.toString(), e);
return ImmutableList.<InvoiceItem>of();
} finally {
if (lock != null) {
lock.release();
}
}
}
use of org.killbill.billing.invoice.dao.InvoiceItemModelDao 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, 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);
final LocalDate today = clock.getUTCToday();
Mockito.when(invoice.getInvoiceDate()).thenReturn(today);
Mockito.when(invoice.getTargetDate()).thenReturn(today);
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), null, ImmutableSet.of(), internalCallContext);
return invoice;
}
use of org.killbill.billing.invoice.dao.InvoiceItemModelDao in project killbill by killbill.
the class TestInvoiceHelper method createInvoice.
public void createInvoice(final Invoice invoice, final InternalCallContext internalCallContext) throws EntityPersistenceException {
final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(invoice);
final List<InvoiceItemModelDao> invoiceItemModelDaos = ImmutableList.<InvoiceItemModelDao>copyOf(Collections2.transform(invoice.getInvoiceItems(), new Function<InvoiceItem, InvoiceItemModelDao>() {
@Override
public InvoiceItemModelDao apply(final InvoiceItem input) {
return new InvoiceItemModelDao(input);
}
}));
invoiceSqlDao.create(invoiceModelDao, internalCallContext);
for (final InvoiceItem invoiceItem : invoice.getInvoiceItems()) {
createInvoiceItem(invoiceItem, internalCallContext);
}
}
use of org.killbill.billing.invoice.dao.InvoiceItemModelDao in project killbill by killbill.
the class DefaultInvoiceUserApi method getInvoiceItemsByParentInvoice.
@Override
public List<InvoiceItem> getInvoiceItemsByParentInvoice(final UUID parentInvoiceId, final TenantContext context) throws InvoiceApiException {
final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(parentInvoiceId, ObjectType.INVOICE, context);
final VersionedCatalog catalog = getCatalogSafelyForPrettyNames(internalTenantContext);
return ImmutableList.copyOf(Collections2.transform(dao.getInvoiceItemsByParentInvoice(parentInvoiceId, internalTenantContext), new Function<InvoiceItemModelDao, InvoiceItem>() {
@Override
public InvoiceItem apply(final InvoiceItemModelDao input) {
return InvoiceItemFactory.fromModelDaoWithCatalog(input, catalog);
}
}));
}
use of org.killbill.billing.invoice.dao.InvoiceItemModelDao in project killbill by killbill.
the class TestIntegrationInvoiceWithRepairLogic method testWithPartialRepairAndExistingPartialTooLargeAdjustment.
@Test(groups = "slow")
public void testWithPartialRepairAndExistingPartialTooLargeAdjustment() throws Exception {
final LocalDate today = new LocalDate(2013, 7, 19);
// Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
clock.setDeltaFromReality(today.toDateTimeAtCurrentTime(DateTimeZone.UTC).getMillis() - clock.getUTCNow().getMillis());
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
final String productName = "Shotgun";
final BillingPeriod term = BillingPeriod.ANNUAL;
//
// CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
//
final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
assertNotNull(bpEntitlement);
assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext).size(), 1);
assertEquals(bpEntitlement.getSubscriptionBase().getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.ANNUAL);
// Move out of trials for interesting invoices adjustments
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
clock.addDays(30);
assertListenerStatus();
List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
assertEquals(invoices.size(), 2);
ImmutableList<ExpectedInvoiceItemCheck> toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2013, 8, 18), new LocalDate(2014, 8, 18), InvoiceItemType.RECURRING, new BigDecimal("2399.95")));
invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, toBeChecked);
clock.addMonths(6);
// Cancelation IMM -> partially repaired
busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.INVOICE);
bpEntitlement.cancelEntitlementWithPolicyOverrideBillingPolicy(EntitlementActionPolicy.IMMEDIATE, BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
// 1209.84
final InvoiceItem targetItem = invoices.get(1).getInvoiceItems().get(0);
final InvoiceItemModelDao adj = new InvoiceItemModelDao(clock.getUTCNow(), InvoiceItemType.ITEM_ADJ, targetItem.getInvoiceId(), targetItem.getAccountId(), null, null, null, targetItem.getProductName(), targetItem.getPlanName(), targetItem.getPhaseName(), targetItem.getUsageName(), targetItem.getCatalogEffectiveDate(), clock.getUTCToday(), clock.getUTCToday(), new BigDecimal("-1300.00"), null, targetItem.getCurrency(), targetItem.getId());
final InvoiceModelDao invoiceForAdj = new InvoiceModelDao(UUID.randomUUID(), clock.getUTCNow(), bpEntitlement.getAccountId(), null, clock.getUTCToday(), clock.getUTCToday(), Currency.USD, false, InvoiceStatus.COMMITTED, false);
invoiceForAdj.addInvoiceItem(adj);
busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
insertInvoiceItems(invoiceForAdj);
assertListenerStatus();
// __PARK__ account
busHandler.pushExpectedEvents(NextEvent.TAG);
try {
invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), callContext);
fail("Should not have generated an extra invoice");
} catch (final InvoiceApiException e) {
assertListenerStatus();
assertTrue(e.getCause().getMessage().startsWith("Too many repairs for invoiceItemId"));
} finally {
assertListenerStatus();
}
}
Aggregations