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