use of org.killbill.billing.invoice.model.ParentInvoiceItem in project killbill by killbill.
the class InvoiceDispatcher method processParentInvoiceForInvoiceGenerationWithLock.
private void processParentInvoiceForInvoiceGenerationWithLock(final Account childAccount, final UUID childInvoiceId, final InternalCallContext context) throws InvoiceApiException {
log.info("Processing parent invoice for parentAccountId='{}', childInvoiceId='{}'", childAccount.getParentAccountId(), childInvoiceId);
final InvoiceModelDao childInvoiceModelDao = invoiceDao.getById(childInvoiceId, context);
final Invoice childInvoice = new DefaultInvoice(childInvoiceModelDao);
final Long parentAccountRecordId = internalCallContextFactory.getRecordIdFromObject(childAccount.getParentAccountId(), ObjectType.ACCOUNT, buildTenantContext(context));
final InternalCallContext parentContext = internalCallContextFactory.createInternalCallContext(parentAccountRecordId, context);
final BigDecimal childInvoiceAmount = InvoiceCalculatorUtils.computeChildInvoiceAmount(childInvoice.getCurrency(), childInvoice.getInvoiceItems());
InvoiceModelDao draftParentInvoice = invoiceDao.getParentDraftInvoice(childAccount.getParentAccountId(), parentContext);
final String description = childAccount.getExternalKey().concat(" summary");
if (draftParentInvoice != null) {
for (final InvoiceItemModelDao item : draftParentInvoice.getInvoiceItems()) {
if ((item.getChildAccountId() != null) && item.getChildAccountId().equals(childInvoice.getAccountId())) {
// update child item amount for existing parent invoice item
final BigDecimal newChildInvoiceAmount = childInvoiceAmount.add(item.getAmount());
log.info("Updating existing itemId='{}', oldAmount='{}', newAmount='{}' on existing DRAFT invoiceId='{}'", item.getId(), item.getAmount(), newChildInvoiceAmount, draftParentInvoice.getId());
invoiceDao.updateInvoiceItemAmount(item.getId(), newChildInvoiceAmount, parentContext);
return;
}
}
// new item when the parent invoices does not have this child item yet
final ParentInvoiceItem newParentInvoiceItem = new ParentInvoiceItem(UUID.randomUUID(), context.getCreatedDate(), draftParentInvoice.getId(), childAccount.getParentAccountId(), childAccount.getId(), childInvoiceAmount, childAccount.getCurrency(), description);
final InvoiceItemModelDao parentInvoiceItem = new InvoiceItemModelDao(newParentInvoiceItem);
draftParentInvoice.addInvoiceItem(parentInvoiceItem);
final List<InvoiceModelDao> invoices = new ArrayList<InvoiceModelDao>();
invoices.add(draftParentInvoice);
log.info("Adding new itemId='{}', amount='{}' on existing DRAFT invoiceId='{}'", parentInvoiceItem.getId(), childInvoiceAmount, draftParentInvoice.getId());
invoiceDao.createInvoices(invoices, null, ImmutableSet.of(), parentContext);
} else {
if (shouldIgnoreChildInvoice(childInvoice, childInvoiceAmount)) {
return;
}
final LocalDate invoiceDate = context.toLocalDate(context.getCreatedDate());
draftParentInvoice = new InvoiceModelDao(childAccount.getParentAccountId(), invoiceDate, childAccount.getCurrency(), InvoiceStatus.DRAFT, true);
final InvoiceItem parentInvoiceItem = new ParentInvoiceItem(UUID.randomUUID(), context.getCreatedDate(), draftParentInvoice.getId(), childAccount.getParentAccountId(), childAccount.getId(), childInvoiceAmount, childAccount.getCurrency(), description);
draftParentInvoice.addInvoiceItem(new InvoiceItemModelDao(parentInvoiceItem));
log.info("Adding new itemId='{}', amount='{}' on new DRAFT invoiceId='{}'", parentInvoiceItem.getId(), childInvoiceAmount, draftParentInvoice.getId());
invoiceDao.createInvoices(ImmutableList.<InvoiceModelDao>of(draftParentInvoice), null, ImmutableSet.of(), parentContext);
}
// save parent child invoice relation
final InvoiceParentChildModelDao invoiceRelation = new InvoiceParentChildModelDao(draftParentInvoice.getId(), childInvoiceId, childAccount.getId());
invoiceDao.createParentChildInvoiceRelation(invoiceRelation, parentContext);
}
use of org.killbill.billing.invoice.model.ParentInvoiceItem in project killbill by killbill.
the class TestInvoiceDao method testCreateParentInvoice.
@Test(groups = "slow")
public void testCreateParentInvoice() throws InvoiceApiException {
final UUID parentAccountId = UUID.randomUUID();
final UUID childAccountId = UUID.randomUUID();
final DateTime today = clock.getNow(account.getTimeZone());
InvoiceModelDao parentInvoice = new InvoiceModelDao(parentAccountId, today.toLocalDate(), account.getCurrency(), InvoiceStatus.DRAFT, true);
InvoiceItem parentInvoiceItem = new ParentInvoiceItem(UUID.randomUUID(), today, parentInvoice.getId(), parentAccountId, childAccountId, BigDecimal.TEN, account.getCurrency(), "");
parentInvoice.addInvoiceItem(new InvoiceItemModelDao(parentInvoiceItem));
invoiceDao.createInvoices(ImmutableList.<InvoiceModelDao>of(parentInvoice), null, ImmutableSet.of(), context);
final InvoiceModelDao parentDraftInvoice = invoiceDao.getParentDraftInvoice(parentAccountId, context);
assertNotNull(parentDraftInvoice);
assertEquals(parentDraftInvoice.getStatus(), InvoiceStatus.DRAFT);
assertEquals(parentDraftInvoice.getInvoiceItems().size(), 1);
}
use of org.killbill.billing.invoice.model.ParentInvoiceItem in project killbill by killbill.
the class TestDefaultInvoiceItemFormatter method testNullStartAndEndDate.
@Test(groups = "fast")
public void testNullStartAndEndDate() throws Exception {
final LocalDate startDate = new LocalDate(2012, 12, 1);
final LocalDate endDate = new LocalDate(2012, 12, 31);
final ParentInvoiceItem parentInvoiceItem = new ParentInvoiceItem(UUID.randomUUID(), null, UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), BigDecimal.TEN, Currency.USD, UUID.randomUUID().toString());
checkOutput(parentInvoiceItem, "{{#invoiceItem}}<td>{{formattedStartDate}}{{#formattedEndDate}} - {{formattedEndDate}}{{/formattedEndDate}}</td>{{/invoiceItem}}", "<td></td>");
}
use of org.killbill.billing.invoice.model.ParentInvoiceItem in project killbill by killbill.
the class TestInvoiceDaoHelper method testPopulateChildrenWithParent.
@Test(groups = "slow")
public void testPopulateChildrenWithParent() throws Exception {
final Account parentAccount = invoiceUtil.createAccount(callContext);
final UUID childAccountId = account.getId();
final UUID parentAccountId = parentAccount.getId();
final InternalCallContext parentContext = internalCallContextFactory.createInternalCallContext(parentAccountId, callContext);
final InvoiceModelDao parentInvoice = new InvoiceModelDao(parentAccountId, today, account.getCurrency(), InvoiceStatus.DRAFT, true);
final InvoiceItem parentInvoiceItem = new ParentInvoiceItem(UUID.randomUUID(), now, parentInvoice.getId(), parentAccountId, childAccountId, BigDecimal.TEN, account.getCurrency(), "");
parentInvoice.addInvoiceItem(new InvoiceItemModelDao(parentInvoiceItem));
invoiceUtil.createInvoice(new DefaultInvoice(parentInvoice), parentContext);
final UUID accountId = account.getId();
final Invoice childInvoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
final InvoiceItem invoiceItem = new RecurringInvoiceItem(childInvoice.getId(), accountId, UUID.randomUUID(), UUID.randomUUID(), "test", "test-plan", "test-phase", null, today, today, BigDecimal.TEN, BigDecimal.TEN, Currency.USD);
childInvoice.addInvoiceItem(invoiceItem);
invoiceUtil.createInvoice(childInvoice, internalAccountContext);
InvoiceParentChildModelDao invoiceRelation = new InvoiceParentChildModelDao(parentInvoice.getId(), childInvoice.getId(), childAccountId);
invoiceDao.createParentChildInvoiceRelation(invoiceRelation, internalAccountContext);
// //
final List<Tag> tags = ImmutableList.of();
final InvoiceModelDao invoice1 = getRawInvoice(childInvoice.getId(), internalAccountContext);
populateChildrenByInvoiceId(invoice1, tags);
final InvoiceModelDao invoice2 = getRawInvoice(childInvoice.getId(), internalAccountContext);
populateChildrenByAccountRecordId(invoice2, tags);
Assert.assertEquals(invoice1, invoice2);
}
Aggregations