Search in sources :

Example 1 with RolledUpUnit

use of org.killbill.billing.usage.api.RolledUpUnit in project killbill by killbill.

the class ContiguousIntervalUsageInArrear method computeMissingItemsAndNextNotificationDate.

/**
     * Compute the missing usage invoice items based on what should be billed and what has been billed ($ amount comparison).
     *
     * @param existingUsage existing on disk usage items for the subscription
     * @throws CatalogApiException
     */
public UsageInArrearItemsAndNextNotificationDate computeMissingItemsAndNextNotificationDate(final List<InvoiceItem> existingUsage) throws CatalogApiException {
    Preconditions.checkState(isBuilt.get());
    if (transitionTimes.size() < 2) {
        return new UsageInArrearItemsAndNextNotificationDate(ImmutableList.<InvoiceItem>of(), computeNextNotificationDate());
    }
    final List<InvoiceItem> result = Lists.newLinkedList();
    // We start by generating 'marker' USAGE items with $0 that will allow to correctly insert the next notification for when there is no USAGE to bill.
    // Those will be removed by the invoicing code later so as to not end up with superfluous $0 items
    LocalDate prevDate = null;
    for (final LocalDate curDate : transitionTimes) {
        if (prevDate != null) {
            final InvoiceItem item = new UsageInvoiceItem(invoiceId, accountId, getBundleId(), getSubscriptionId(), getPlanName(), getPhaseName(), usage.getName(), prevDate, curDate, BigDecimal.ZERO, getCurrency());
            result.add(item);
        }
        prevDate = curDate;
    }
    final List<RolledUpUsage> allUsage = getRolledUpUsage();
    for (final RolledUpUsage ru : allUsage) {
        BigDecimal toBeBilledUsage = BigDecimal.ZERO;
        if (usage.getUsageType() == UsageType.CAPACITY) {
            toBeBilledUsage = computeToBeBilledCapacityInArrear(ru.getRolledUpUnits());
        } else /* UsageType.CONSUMABLE */
        {
            // Compute total price amount that should be billed for that period of time (and usage section) across unitTypes.
            for (final RolledUpUnit cur : ru.getRolledUpUnits()) {
                if (!unitTypes.contains(cur.getUnitType())) {
                    log.warn("ContiguousIntervalConsumableInArrear is skipping unitType " + cur.getUnitType());
                    continue;
                }
                final BigDecimal toBeBilledForUnit = computeToBeBilledConsumableInArrear(cur);
                toBeBilledUsage = toBeBilledUsage.add(toBeBilledForUnit);
            }
        }
        // Retrieves current price amount billed for that period of time (and usage section)
        final Iterable<InvoiceItem> billedItems = getBilledItems(ru.getStart(), ru.getEnd(), existingUsage);
        final BigDecimal billedUsage = computeBilledUsage(billedItems);
        // Compare the two and add the missing piece if required.
        if (!billedItems.iterator().hasNext() || billedUsage.compareTo(toBeBilledUsage) < 0) {
            final BigDecimal amountToBill = toBeBilledUsage.subtract(billedUsage);
            if (amountToBill.compareTo(BigDecimal.ZERO) > 0) {
                final InvoiceItem item = new UsageInvoiceItem(invoiceId, accountId, getBundleId(), getSubscriptionId(), getPlanName(), getPhaseName(), usage.getName(), ru.getStart(), ru.getEnd(), amountToBill, getCurrency());
                result.add(item);
            }
        }
    }
    final LocalDate nextNotificationdate = computeNextNotificationDate();
    return new UsageInArrearItemsAndNextNotificationDate(result, nextNotificationdate);
}
Also used : UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 2 with RolledUpUnit

use of org.killbill.billing.usage.api.RolledUpUnit in project killbill by killbill.

the class DefaultUsageUserApi method getRolledUpUnits.

private List<RolledUpUnit> getRolledUpUnits(final List<RolledUpUsageModelDao> usageForSubscription) {
    final Map<String, Long> tmp = new HashMap<String, Long>();
    for (RolledUpUsageModelDao cur : usageForSubscription) {
        Long currentAmount = tmp.get(cur.getUnitType());
        Long updatedAmount = (currentAmount != null) ? currentAmount + cur.getAmount() : cur.getAmount();
        tmp.put(cur.getUnitType(), updatedAmount);
    }
    final List<RolledUpUnit> result = new ArrayList<RolledUpUnit>(tmp.size());
    for (final String unitType : tmp.keySet()) {
        result.add(new DefaultRolledUpUnit(unitType, tmp.get(unitType)));
    }
    return result;
}
Also used : RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao)

Example 3 with RolledUpUnit

use of org.killbill.billing.usage.api.RolledUpUnit in project killbill by killbill.

the class DefaultUsageUserApi method getAllUsageForSubscription.

@Override
public List<RolledUpUsage> getAllUsageForSubscription(final UUID subscriptionId, final List<LocalDate> transitionTimes, final TenantContext tenantContext) {
    final InternalTenantContext internalCallContext = internalCallContextFactory.createInternalTenantContext(subscriptionId, ObjectType.SUBSCRIPTION, tenantContext);
    List<RolledUpUsage> result = new ArrayList<RolledUpUsage>();
    LocalDate prevDate = null;
    for (LocalDate curDate : transitionTimes) {
        if (prevDate != null) {
            final List<RolledUpUsageModelDao> usageForSubscription = rolledUpUsageDao.getAllUsageForSubscription(subscriptionId, prevDate, curDate, internalCallContext);
            final List<RolledUpUnit> rolledUpAmount = getRolledUpUnits(usageForSubscription);
            result.add(new DefaultRolledUpUsage(subscriptionId, prevDate, curDate, rolledUpAmount));
        }
        prevDate = curDate;
    }
    return result;
}
Also used : RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) ArrayList(java.util.ArrayList) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao) LocalDate(org.joda.time.LocalDate)

Example 4 with RolledUpUnit

use of org.killbill.billing.usage.api.RolledUpUnit in project killbill by killbill.

the class ContiguousIntervalUsageInArrear method getRolledUpUsage.

@VisibleForTesting
List<RolledUpUsage> getRolledUpUsage() {
    final Iterator<RawUsage> rawUsageIterator = rawSubscriptionUsage.iterator();
    if (!rawUsageIterator.hasNext()) {
        return ImmutableList.of();
    }
    final List<RolledUpUsage> result = new ArrayList<RolledUpUsage>();
    //
    // Skip all items before our first transition date
    //
    // prevRawUsage keeps track of first unconsumed raw usage element
    RawUsage prevRawUsage = null;
    while (rawUsageIterator.hasNext()) {
        final RawUsage curRawUsage = rawUsageIterator.next();
        if (curRawUsage.getDate().compareTo(transitionTimes.get(0)) >= 0) {
            prevRawUsage = curRawUsage;
            break;
        }
    }
    // Optimize path where all raw usage items are outside or our transitionTimes range
    if (prevRawUsage.getDate().compareTo(transitionTimes.get(transitionTimes.size() - 1)) >= 0) {
        return ImmutableList.of();
    }
    //
    // Loop through each interval [prevDate, curDate) and consume as many rawSubscriptionUsage elements within that range
    // to create one RolledUpUsage per interval. If an interval does not have any rawSubscriptionUsage element, there will be no
    // matching RolledUpUsage for that interval, and we'll detect that in the 'computeMissingItems' logic
    //
    LocalDate prevDate = null;
    for (final LocalDate curDate : transitionTimes) {
        if (prevDate != null) {
            // Allocate new perRangeUnitToAmount for this interval and populate with rawSubscriptionUsage items
            final Map<String, Long> perRangeUnitToAmount = new HashMap<String, Long>();
            // Start consuming prevRawUsage element if it exists and falls into the range
            if (prevRawUsage != null) {
                if (prevRawUsage.getDate().compareTo(prevDate) >= 0 && prevRawUsage.getDate().compareTo(curDate) < 0) {
                    final Long currentAmount = perRangeUnitToAmount.get(prevRawUsage.getUnitType());
                    final Long updatedAmount = computeUpdatedAmount(currentAmount, prevRawUsage.getAmount());
                    perRangeUnitToAmount.put(prevRawUsage.getUnitType(), updatedAmount);
                    prevRawUsage = null;
                }
            }
            //
            if (prevRawUsage == null) {
                while (rawUsageIterator.hasNext()) {
                    final RawUsage curRawUsage = rawUsageIterator.next();
                    if (curRawUsage.getDate().compareTo(curDate) >= 0) {
                        prevRawUsage = curRawUsage;
                        break;
                    }
                    final Long currentAmount = perRangeUnitToAmount.get(curRawUsage.getUnitType());
                    final Long updatedAmount = computeUpdatedAmount(currentAmount, curRawUsage.getAmount());
                    perRangeUnitToAmount.put(curRawUsage.getUnitType(), updatedAmount);
                }
            }
            // If we did find some usage for that date range, let's populate the result
            if (!perRangeUnitToAmount.isEmpty()) {
                final List<RolledUpUnit> rolledUpUnits = new ArrayList<RolledUpUnit>(perRangeUnitToAmount.size());
                for (final String unitType : perRangeUnitToAmount.keySet()) {
                    rolledUpUnits.add(new DefaultRolledUpUnit(unitType, perRangeUnitToAmount.get(unitType)));
                }
                result.add(new DefaultRolledUpUsage(getSubscriptionId(), prevDate, curDate, rolledUpUnits));
            }
        }
        prevDate = curDate;
    }
    return result;
}
Also used : HashMap(java.util.HashMap) RawUsage(org.killbill.billing.usage.RawUsage) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with RolledUpUnit

use of org.killbill.billing.usage.api.RolledUpUnit in project killbill by killbill.

the class ContiguousIntervalUsageInArrear method computeToBeBilledCapacityInArrear.

/**
     * @param roUnits the list of rolled up units for the period
     * @return the price amount that should be billed for that period/unitType
     * @throws CatalogApiException
     */
@VisibleForTesting
BigDecimal computeToBeBilledCapacityInArrear(final List<RolledUpUnit> roUnits) throws CatalogApiException {
    Preconditions.checkState(isBuilt.get());
    final List<Tier> tiers = getCapacityInArrearTier(usage);
    for (final Tier cur : tiers) {
        boolean complies = true;
        for (final RolledUpUnit ro : roUnits) {
            final Limit tierLimit = getTierLimit(cur, ro.getUnitType());
            // Specifying a -1 value for last max tier will make the validation works
            if (tierLimit.getMax() != (double) -1 && ro.getAmount().doubleValue() > tierLimit.getMax()) {
                complies = false;
                break;
            }
        }
        if (complies) {
            return cur.getRecurringPrice().getPrice(getCurrency());
        }
    }
    // Probably invalid catalog config
    final Joiner joiner = Joiner.on(", ");
    joiner.join(roUnits);
    Preconditions.checkState(false, "Could not find tier for usage " + usage.getName() + "matching with data = " + joiner.join(roUnits));
    return null;
}
Also used : Joiner(com.google.common.base.Joiner) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) Tier(org.killbill.billing.catalog.api.Tier) UsageUtils.getCapacityInArrearTier(org.killbill.billing.invoice.usage.UsageUtils.getCapacityInArrearTier) Limit(org.killbill.billing.catalog.api.Limit) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

RolledUpUnit (org.killbill.billing.usage.api.RolledUpUnit)6 LocalDate (org.joda.time.LocalDate)4 RolledUpUsage (org.killbill.billing.usage.api.RolledUpUsage)4 ArrayList (java.util.ArrayList)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2 RawUsage (org.killbill.billing.usage.RawUsage)2 RolledUpUsageModelDao (org.killbill.billing.usage.dao.RolledUpUsageModelDao)2 Joiner (com.google.common.base.Joiner)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 DefaultLimit (org.killbill.billing.catalog.DefaultLimit)1 DefaultTier (org.killbill.billing.catalog.DefaultTier)1 DefaultUnit (org.killbill.billing.catalog.DefaultUnit)1 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)1 Limit (org.killbill.billing.catalog.api.Limit)1 Tier (org.killbill.billing.catalog.api.Tier)1 Usage (org.killbill.billing.catalog.api.Usage)1 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)1 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)1