Search in sources :

Example 1 with ParentInvoiceItem

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);
}
Also used : DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) ArrayList(java.util.ArrayList) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceParentChildModelDao(org.killbill.billing.invoice.dao.InvoiceParentChildModelDao) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice)

Example 2 with ParentInvoiceItem

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);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 3 with ParentInvoiceItem

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>");
}
Also used : ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 4 with ParentInvoiceItem

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);
}
Also used : Account(org.killbill.billing.account.api.Account) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) DefaultControlTag(org.killbill.billing.util.tag.DefaultControlTag) Tag(org.killbill.billing.util.tag.Tag) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Aggregations

ParentInvoiceItem (org.killbill.billing.invoice.model.ParentInvoiceItem)4 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)3 Test (org.testng.annotations.Test)3 UUID (java.util.UUID)2 LocalDate (org.joda.time.LocalDate)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)2 Invoice (org.killbill.billing.invoice.api.Invoice)2 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)2 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)2 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 DateTime (org.joda.time.DateTime)1 Account (org.killbill.billing.account.api.Account)1 InvoiceItemModelDao (org.killbill.billing.invoice.dao.InvoiceItemModelDao)1 InvoiceModelDao (org.killbill.billing.invoice.dao.InvoiceModelDao)1 InvoiceParentChildModelDao (org.killbill.billing.invoice.dao.InvoiceParentChildModelDao)1 CreditAdjInvoiceItem (org.killbill.billing.invoice.model.CreditAdjInvoiceItem)1 CreditBalanceAdjInvoiceItem (org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem)1 ExternalChargeInvoiceItem (org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)1