Search in sources :

Example 1 with UsageInvoiceItem

use of org.killbill.billing.invoice.model.UsageInvoiceItem 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 UsageInvoiceItem

use of org.killbill.billing.invoice.model.UsageInvoiceItem in project killbill by killbill.

the class RawUsageOptimizer method getOptimizedRawUsageStartDate.

@VisibleForTesting
LocalDate getOptimizedRawUsageStartDate(final LocalDate firstEventStartDate, final LocalDate targetDate, final Iterable<InvoiceItem> existingUsageItems, final Map<String, Usage> knownUsage, final InternalCallContext internalCallContext) {
    if (!existingUsageItems.iterator().hasNext()) {
        return firstEventStartDate;
    }
    // Extract all usage billing period known in that catalog
    final Set<BillingPeriod> knownUsageBillingPeriod = ImmutableSet.copyOf(Iterables.transform(knownUsage.values(), new Function<Usage, BillingPeriod>() {

        @Nullable
        @Override
        public BillingPeriod apply(final Usage input) {
            return input.getBillingPeriod();
        }
    }));
    // Make sure all usage items are sorted by endDate
    final List<InvoiceItem> sortedUsageItems = USAGE_ITEM_ORDERING.sortedCopy(existingUsageItems);
    // Compute an array with one date per BillingPeriod:
    // If BillingPeriod is never defined in the catalog (no need to look for items), we initialize its value
    // such that it cannot be chosen
    //
    // Exclude the NO_BILLING_PERIOD
    final LocalDate[] perBillingPeriodMostRecentConsumableInArrearItemEndDate = new LocalDate[BillingPeriod.values().length - 1];
    int idx = 0;
    for (final BillingPeriod bp : BillingPeriod.values()) {
        if (bp != BillingPeriod.NO_BILLING_PERIOD) {
            final LocalDate makerDateThanCannotBeChosenAsTheMinOfAllDates = InvoiceDateUtils.advanceByNPeriods(targetDate, bp, config.getMaxRawUsagePreviousPeriod(internalCallContext));
            perBillingPeriodMostRecentConsumableInArrearItemEndDate[idx++] = (knownUsageBillingPeriod.contains(bp)) ? null : makerDateThanCannotBeChosenAsTheMinOfAllDates;
        }
    }
    final ListIterator<InvoiceItem> iterator = sortedUsageItems.listIterator(sortedUsageItems.size());
    while (iterator.hasPrevious()) {
        final InvoiceItem previous = iterator.previous();
        Preconditions.checkState(previous instanceof UsageInvoiceItem);
        final UsageInvoiceItem item = (UsageInvoiceItem) previous;
        final Usage usage = knownUsage.get(item.getUsageName());
        if (perBillingPeriodMostRecentConsumableInArrearItemEndDate[usage.getBillingPeriod().ordinal()] == null) {
            perBillingPeriodMostRecentConsumableInArrearItemEndDate[usage.getBillingPeriod().ordinal()] = item.getEndDate();
            if (!containsNullEntries(perBillingPeriodMostRecentConsumableInArrearItemEndDate)) {
                break;
            }
        }
    }
    // Extract the min from all the dates
    LocalDate targetStartDate = null;
    idx = 0;
    for (final BillingPeriod bp : BillingPeriod.values()) {
        if (bp != BillingPeriod.NO_BILLING_PERIOD) {
            final LocalDate tmp = perBillingPeriodMostRecentConsumableInArrearItemEndDate[idx];
            final LocalDate targetBillingPeriodDate = tmp != null ? InvoiceDateUtils.recedeByNPeriods(tmp, bp, config.getMaxRawUsagePreviousPeriod(internalCallContext)) : null;
            if (targetStartDate == null || (targetBillingPeriodDate != null && targetBillingPeriodDate.compareTo(targetStartDate) < 0)) {
                targetStartDate = targetBillingPeriodDate;
            }
            idx++;
        }
    }
    final LocalDate result = targetStartDate.compareTo(firstEventStartDate) > 0 ? targetStartDate : firstEventStartDate;
    return result;
}
Also used : Function(com.google.common.base.Function) Usage(org.killbill.billing.catalog.api.Usage) RawUsage(org.killbill.billing.usage.RawUsage) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) LocalDate(org.joda.time.LocalDate) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with UsageInvoiceItem

use of org.killbill.billing.invoice.model.UsageInvoiceItem in project killbill by killbill.

the class TestContiguousIntervalCapacityInArrear method testComputeMissingItems.

@Test(groups = "fast")
public void testComputeMissingItems() throws CatalogApiException {
    final LocalDate startDate = new LocalDate(2014, 03, 20);
    final LocalDate firstBCDDate = new LocalDate(2014, 04, 15);
    final LocalDate endDate = new LocalDate(2014, 05, 15);
    // 2 items for startDate - firstBCDDate
    final List<RawUsage> rawUsages = new ArrayList<RawUsage>();
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "unit", 130L));
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "unit", 271L));
    // 1 items for firstBCDDate - endDate
    rawUsages.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 04, 15), "unit", 199L));
    final DefaultUnit unit = new DefaultUnit().setName("unit");
    final DefaultLimit limit = new DefaultLimit().setUnit(unit).setMax((double) -1);
    final DefaultTier tier = createDefaultTierWithLimits(BigDecimal.TEN, limit);
    final DefaultUsage usage = createCapacityInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    final LocalDate targetDate = endDate;
    final BillingEvent event1 = createMockBillingEvent(startDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final BillingEvent event2 = createMockBillingEvent(endDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList());
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, rawUsages, targetDate, true, event1, event2);
    final List<InvoiceItem> invoiceItems = new ArrayList<InvoiceItem>();
    final InvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, firstBCDDate, BigDecimal.ONE, currency);
    invoiceItems.add(ii1);
    final InvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), firstBCDDate, endDate, BigDecimal.ONE, currency);
    invoiceItems.add(ii2);
    final UsageInArrearItemsAndNextNotificationDate usageResult = intervalConsumableInArrear.computeMissingItemsAndNextNotificationDate(invoiceItems);
    final List<InvoiceItem> rawResults = usageResult.getInvoiceItems();
    assertEquals(rawResults.size(), 4);
    final List<InvoiceItem> result = ImmutableList.copyOf(Iterables.filter(rawResults, new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getAmount().compareTo(BigDecimal.ZERO) > 0;
        }
    }));
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("9.0")), 0, String.format("%s != 9.0", result.get(0).getAmount()));
    assertEquals(result.get(0).getCurrency(), Currency.BTC);
    assertEquals(result.get(0).getAccountId(), accountId);
    assertEquals(result.get(0).getBundleId(), bundleId);
    assertEquals(result.get(0).getSubscriptionId(), subscriptionId);
    assertEquals(result.get(0).getPlanName(), planName);
    assertEquals(result.get(0).getPhaseName(), phaseName);
    assertEquals(result.get(0).getUsageName(), usage.getName());
    assertTrue(result.get(0).getStartDate().compareTo(startDate) == 0);
    assertTrue(result.get(0).getEndDate().compareTo(firstBCDDate) == 0);
    assertEquals(result.get(1).getAmount().compareTo(new BigDecimal("9.0")), 0, String.format("%s != 9.0", result.get(0).getAmount()));
    assertEquals(result.get(1).getCurrency(), Currency.BTC);
    assertEquals(result.get(1).getAccountId(), accountId);
    assertEquals(result.get(1).getBundleId(), bundleId);
    assertEquals(result.get(1).getSubscriptionId(), subscriptionId);
    assertEquals(result.get(1).getPlanName(), planName);
    assertEquals(result.get(1).getPhaseName(), phaseName);
    assertEquals(result.get(1).getUsageName(), usage.getName());
    assertTrue(result.get(1).getStartDate().compareTo(firstBCDDate) == 0);
    assertTrue(result.get(1).getEndDate().compareTo(endDate) == 0);
}
Also used : DefaultLimit(org.killbill.billing.catalog.DefaultLimit) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) ArrayList(java.util.ArrayList) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RawUsage(org.killbill.billing.usage.RawUsage) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Predicate(com.google.common.base.Predicate) UsageInArrearItemsAndNextNotificationDate(org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) DefaultUnit(org.killbill.billing.catalog.DefaultUnit) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Example 4 with UsageInvoiceItem

use of org.killbill.billing.invoice.model.UsageInvoiceItem in project killbill by killbill.

the class TestContiguousIntervalCapacityInArrear method testComputeToBeBilledUsage.

@Test(groups = "fast")
public void testComputeToBeBilledUsage() {
    final LocalDate startDate = new LocalDate(2014, 03, 20);
    final LocalDate endDate = new LocalDate(2014, 04, 20);
    final DefaultUnit unit = new DefaultUnit().setName("unit");
    final DefaultLimit limit = new DefaultLimit().setUnit(unit).setMax((double) 100);
    final DefaultTier tier = createDefaultTierWithLimits(BigDecimal.TEN, limit);
    final DefaultUsage usage = createCapacityInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    final LocalDate targetDate = startDate.plusDays(1);
    final ContiguousIntervalUsageInArrear intervalCapacityInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsage>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList()));
    final List<InvoiceItem> existingUsage = Lists.newArrayList();
    final UsageInvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii1);
    final UsageInvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii2);
    // Will be ignored as is starts one day earlier.
    final UsageInvoiceItem ii3 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate.minusDays(1), endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii3);
    // Will be ignored as it is for a different udsage section
    final UsageInvoiceItem ii4 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, "other", startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii4);
    // Will be ignored because non usage item
    final FixedPriceInvoiceItem ii5 = new FixedPriceInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, BigDecimal.TEN, currency);
    existingUsage.add(ii5);
    final Iterable<InvoiceItem> billedItems = intervalCapacityInArrear.getBilledItems(startDate, endDate, existingUsage);
    final BigDecimal result = intervalCapacityInArrear.computeBilledUsage(billedItems);
    assertEquals(result.compareTo(BigDecimal.TEN.add(BigDecimal.TEN)), 0);
}
Also used : DefaultLimit(org.killbill.billing.catalog.DefaultLimit) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) RawUsage(org.killbill.billing.usage.RawUsage) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultUnit(org.killbill.billing.catalog.DefaultUnit) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTier(org.killbill.billing.catalog.DefaultTier) Test(org.testng.annotations.Test)

Example 5 with UsageInvoiceItem

use of org.killbill.billing.invoice.model.UsageInvoiceItem in project killbill by killbill.

the class TestContiguousIntervalConsumableInArrear method testComputeToBeBilledUsage.

@Test(groups = "fast")
public void testComputeToBeBilledUsage() {
    final LocalDate startDate = new LocalDate(2014, 03, 20);
    final LocalDate endDate = new LocalDate(2014, 04, 20);
    final DefaultTieredBlock block = createDefaultTieredBlock("unit", 100, 1000, BigDecimal.ONE);
    final DefaultTier tier = createDefaultTierWithBlocks(block);
    final DefaultUsage usage = createConsumableInArrearUsage(usageName, BillingPeriod.MONTHLY, tier);
    final LocalDate targetDate = startDate.plusDays(1);
    final ContiguousIntervalUsageInArrear intervalConsumableInArrear = createContiguousIntervalConsumableInArrear(usage, ImmutableList.<RawUsage>of(), targetDate, false, createMockBillingEvent(targetDate.toDateTimeAtStartOfDay(DateTimeZone.UTC), BillingPeriod.MONTHLY, Collections.<Usage>emptyList()));
    final List<InvoiceItem> existingUsage = Lists.newArrayList();
    final UsageInvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii1);
    final UsageInvoiceItem ii2 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii2);
    // Will be ignored as is starts one day earlier.
    final UsageInvoiceItem ii3 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, usage.getName(), startDate.minusDays(1), endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii3);
    // Will be ignored as it is for a different udsage section
    final UsageInvoiceItem ii4 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, "other", startDate, endDate, BigDecimal.TEN, currency);
    existingUsage.add(ii4);
    // Will be ignored because non usage item
    final FixedPriceInvoiceItem ii5 = new FixedPriceInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, BigDecimal.TEN, currency);
    existingUsage.add(ii5);
    final BigDecimal result = intervalConsumableInArrear.computeBilledUsage(intervalConsumableInArrear.getBilledItems(startDate, endDate, existingUsage));
    assertEquals(result.compareTo(BigDecimal.TEN.add(BigDecimal.TEN)), 0);
}
Also used : DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) RawUsage(org.killbill.billing.usage.RawUsage) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) DefaultUsage(org.killbill.billing.catalog.DefaultUsage) DefaultTieredBlock(org.killbill.billing.catalog.DefaultTieredBlock) LocalDate(org.joda.time.LocalDate) DefaultTier(org.killbill.billing.catalog.DefaultTier) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

LocalDate (org.joda.time.LocalDate)6 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)6 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)6 BigDecimal (java.math.BigDecimal)5 RawUsage (org.killbill.billing.usage.RawUsage)5 DefaultTier (org.killbill.billing.catalog.DefaultTier)4 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)4 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)4 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)4 Test (org.testng.annotations.Test)4 Usage (org.killbill.billing.catalog.api.Usage)3 RolledUpUsage (org.killbill.billing.usage.api.RolledUpUsage)3 Predicate (com.google.common.base.Predicate)2 ArrayList (java.util.ArrayList)2 DefaultLimit (org.killbill.billing.catalog.DefaultLimit)2 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)2 DefaultUnit (org.killbill.billing.catalog.DefaultUnit)2 UsageInArrearItemsAndNextNotificationDate (org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.UsageInArrearItemsAndNextNotificationDate)2 BillingEvent (org.killbill.billing.junction.BillingEvent)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1