Search in sources :

Example 1 with AccountItemTree

use of org.killbill.billing.invoice.tree.AccountItemTree in project killbill by killbill.

the class FixedAndRecurringInvoiceItemGenerator method generateItems.

public List<InvoiceItem> generateItems(final ImmutableAccountData account, final UUID invoiceId, final BillingEventSet eventSet, @Nullable final List<Invoice> existingInvoices, final LocalDate targetDate, final Currency targetCurrency, final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDate, final InternalCallContext internalCallContext) throws InvoiceApiException {
    final Multimap<UUID, LocalDate> createdItemsPerDayPerSubscription = LinkedListMultimap.<UUID, LocalDate>create();
    final AccountItemTree accountItemTree = new AccountItemTree(account.getId(), invoiceId);
    if (existingInvoices != null) {
        for (final Invoice invoice : existingInvoices) {
            for (final InvoiceItem item : invoice.getInvoiceItems()) {
                if (// Always include migration invoices, credits, external charges etc.
                item.getSubscriptionId() == null || !eventSet.getSubscriptionIdsWithAutoInvoiceOff().contains(item.getSubscriptionId())) {
                    //don't add items with auto_invoice_off tag
                    accountItemTree.addExistingItem(item);
                    trackInvoiceItemCreatedDay(item, createdItemsPerDayPerSubscription, internalCallContext);
                }
            }
        }
    }
    // Generate list of proposed invoice items based on billing events from junction-- proposed items are ALL items since beginning of time
    final List<InvoiceItem> proposedItems = new ArrayList<InvoiceItem>();
    processRecurringBillingEvents(invoiceId, account.getId(), eventSet, targetDate, targetCurrency, proposedItems, perSubscriptionFutureNotificationDate, existingInvoices, internalCallContext);
    processFixedBillingEvents(invoiceId, account.getId(), eventSet, targetDate, targetCurrency, proposedItems, internalCallContext);
    try {
        accountItemTree.mergeWithProposedItems(proposedItems);
    } catch (final IllegalStateException e) {
        // Proposed items have already been logged
        throw new InvoiceApiException(e, ErrorCode.UNEXPECTED_ERROR, String.format("ILLEGAL INVOICING STATE accountItemTree=%s", accountItemTree.toString()));
    }
    final List<InvoiceItem> resultingItems = accountItemTree.getResultingItemList();
    safetyBounds(resultingItems, createdItemsPerDayPerSubscription, internalCallContext);
    return resultingItems;
}
Also used : InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) AccountItemTree(org.killbill.billing.invoice.tree.AccountItemTree) Invoice(org.killbill.billing.invoice.api.Invoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ArrayList(java.util.ArrayList) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate)

Aggregations

ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 LocalDate (org.joda.time.LocalDate)1 Invoice (org.killbill.billing.invoice.api.Invoice)1 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)1 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)1 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)1 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)1 AccountItemTree (org.killbill.billing.invoice.tree.AccountItemTree)1