Search in sources :

Example 1 with InvoiceParentChildModelDao

use of org.killbill.billing.invoice.dao.InvoiceParentChildModelDao 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)

Aggregations

BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 LocalDate (org.joda.time.LocalDate)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 Invoice (org.killbill.billing.invoice.api.Invoice)1 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)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 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)1 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)1 ParentInvoiceItem (org.killbill.billing.invoice.model.ParentInvoiceItem)1