Search in sources :

Example 1 with Usage

use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.

the class UsageInvoiceItemGenerator method extractPerSubscriptionExistingInArrearUsageItems.

private Map<UUID, List<InvoiceItem>> extractPerSubscriptionExistingInArrearUsageItems(final Map<String, Usage> knownUsage, @Nullable final List<Invoice> existingInvoices) {
    if (existingInvoices == null || existingInvoices.isEmpty()) {
        return ImmutableMap.of();
    }
    final Map<UUID, List<InvoiceItem>> result = new HashMap<UUID, List<InvoiceItem>>();
    final Iterable<InvoiceItem> usageInArrearItems = Iterables.concat(Iterables.transform(existingInvoices, new Function<Invoice, Iterable<InvoiceItem>>() {

        @Override
        public Iterable<InvoiceItem> apply(final Invoice input) {
            return Iterables.filter(input.getInvoiceItems(), new Predicate<InvoiceItem>() {

                @Override
                public boolean apply(final InvoiceItem input) {
                    if (input.getInvoiceItemType() == InvoiceItemType.USAGE) {
                        final Usage usage = knownUsage.get(input.getUsageName());
                        return usage.getBillingMode() == BillingMode.IN_ARREAR;
                    }
                    return false;
                }
            });
        }
    }));
    for (final InvoiceItem cur : usageInArrearItems) {
        List<InvoiceItem> perSubscriptionUsageItems = result.get(cur.getSubscriptionId());
        if (perSubscriptionUsageItems == null) {
            perSubscriptionUsageItems = new LinkedList<InvoiceItem>();
            result.put(cur.getSubscriptionId(), perSubscriptionUsageItems);
        }
        perSubscriptionUsageItems.add(cur);
    }
    return result;
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) HashMap(java.util.HashMap) Function(com.google.common.base.Function) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List) UUID(java.util.UUID)

Example 2 with Usage

use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.

the class SubscriptionUsageInArrear method computeInArrearUsageInterval.

@VisibleForTesting
List<ContiguousIntervalUsageInArrear> computeInArrearUsageInterval() {
    final List<ContiguousIntervalUsageInArrear> usageIntervals = Lists.newLinkedList();
    final Map<String, ContiguousIntervalUsageInArrear> inFlightInArrearUsageIntervals = new HashMap<String, ContiguousIntervalUsageInArrear>();
    final Set<String> allSeenUsage = new HashSet<String>();
    for (final BillingEvent event : subscriptionBillingEvents) {
        // Extract all in arrear /consumable usage section for that billing event.
        final List<Usage> usages = findUsageInArrearUsages(event);
        allSeenUsage.addAll(Collections2.transform(usages, new Function<Usage, String>() {

            @Override
            public String apply(final Usage input) {
                return input.getName();
            }
        }));
        // All inflight usage interval are candidates to be closed unless we see that current billing event referencing the same usage section.
        final Set<String> toBeClosed = new HashSet<String>(allSeenUsage);
        for (final Usage usage : usages) {
            // Add inflight usage interval if non existent
            ContiguousIntervalUsageInArrear existingInterval = inFlightInArrearUsageIntervals.get(usage.getName());
            if (existingInterval == null) {
                existingInterval = new ContiguousIntervalUsageInArrear(usage, accountId, invoiceId, rawSubscriptionUsage, targetDate, rawUsageStartDate, internalTenantContext);
                inFlightInArrearUsageIntervals.put(usage.getName(), existingInterval);
            }
            // Add billing event for that usage interval
            existingInterval.addBillingEvent(event);
            // Remove usage interval for toBeClosed set
            toBeClosed.remove(usage.getName());
        }
        // Build the usage interval that are no longer referenced
        for (final String usageName : toBeClosed) {
            final ContiguousIntervalUsageInArrear interval = inFlightInArrearUsageIntervals.remove(usageName);
            if (interval != null) {
                interval.addBillingEvent(event);
                usageIntervals.add(interval.build(true));
            }
        }
    }
    for (final String usageName : inFlightInArrearUsageIntervals.keySet()) {
        usageIntervals.add(inFlightInArrearUsageIntervals.get(usageName).build(false));
    }
    inFlightInArrearUsageIntervals.clear();
    return usageIntervals;
}
Also used : Function(com.google.common.base.Function) Usage(org.killbill.billing.catalog.api.Usage) RawUsage(org.killbill.billing.usage.RawUsage) HashMap(java.util.HashMap) BillingEvent(org.killbill.billing.junction.BillingEvent) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with Usage

use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.

the class TestXMLReader method testUsageConsumableInArrear.

@Test(groups = "fast")
public void testUsageConsumableInArrear() {
    try {
        final StandaloneCatalog catalog = getCatalog("UsageExperimental.xml");
        final Usage[] usages = getUsages(catalog, "consumable-in-arrear");
        assertEquals(usages.length, 1);
        final Usage usage = usages[0];
        assertEquals(usage.getName(), "consumable-in-arrear-usage1");
        assertEquals(usage.getBillingPeriod(), BillingPeriod.MONTHLY);
        assertEquals(usage.getUsageType(), UsageType.CONSUMABLE);
        assertEquals(usage.getBillingMode(), BillingMode.IN_ARREAR);
        assertEquals(usage.getLimits().length, 0);
        assertEquals(usage.getBlocks().length, 0);
        assertEquals(usage.getTiers().length, 1);
        assertEquals(usage.getTiers()[0].getTieredBlocks().length, 2);
        assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getUnit().getName(), "cell-phone-minutes");
        assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getSize(), new Double("1000"));
        assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getMax(), new Double("10000"));
        assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getPrice().getPrices().length, 1);
        assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getPrice().getPrices()[0].getCurrency(), Currency.BTC);
        assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getPrice().getPrices()[0].getValue(), new BigDecimal("0.5"));
        assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getUnit().getName(), "Mbytes");
        assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getSize(), new Double("512"));
        assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getMax(), new Double("512000"));
        assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getPrice().getPrices().length, 1);
        assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getPrice().getPrices()[0].getCurrency(), Currency.BTC);
        assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getPrice().getPrices()[0].getValue(), new BigDecimal("0.3"));
    } catch (Exception e) {
        Assert.fail(e.toString());
    }
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) BigDecimal(java.math.BigDecimal) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) Test(org.testng.annotations.Test)

Example 4 with Usage

use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.

the class TestXMLReader method getUsages.

private Usage[] getUsages(final StandaloneCatalog catalog, final String planName) throws CatalogApiException {
    final Plan plan = catalog.findPlan(planName);
    assertNotNull(plan);
    final PlanPhase phase = plan.getFinalPhase();
    assertNotNull(phase);
    final Usage[] usages = phase.getUsages();
    return usages;
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) Plan(org.killbill.billing.catalog.api.Plan)

Example 5 with Usage

use of org.killbill.billing.catalog.api.Usage in project killbill by killbill.

the class TestXMLReader method testUsageConsumableInAdvancePrepayCredit.

@Test(groups = "fast")
public void testUsageConsumableInAdvancePrepayCredit() {
    try {
        final StandaloneCatalog catalog = getCatalog("UsageExperimental.xml");
        final Usage[] usages = getUsages(catalog, "consumable-in-advance-prepay-credit-monthly");
        assertEquals(usages.length, 1);
        final Usage usage = usages[0];
        assertEquals(usage.getName(), "consumable-in-advance-prepay-credit-monthly-usage1");
        assertEquals(usage.getBillingPeriod(), BillingPeriod.MONTHLY);
        assertEquals(usage.getUsageType(), UsageType.CONSUMABLE);
        assertEquals(usage.getBillingMode(), BillingMode.IN_ADVANCE);
        assertEquals(usage.getLimits().length, 0);
        assertEquals(usage.getTiers().length, 0);
        assertEquals(usage.getBlocks().length, 1);
        assertEquals(usage.getBlocks()[0].getUnit().getName(), "cell-phone-minutes");
        assertEquals(usage.getBlocks()[0].getSize(), new Double("1000"));
        assertEquals(usage.getBlocks()[0].getPrice().getPrices().length, 1);
        assertEquals(usage.getBlocks()[0].getPrice().getPrices()[0].getCurrency(), Currency.BTC);
        assertEquals(usage.getBlocks()[0].getPrice().getPrices()[0].getValue(), new BigDecimal("0.10"));
    } catch (Exception e) {
        Assert.fail(e.toString());
    }
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) BigDecimal(java.math.BigDecimal) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) Test(org.testng.annotations.Test)

Aggregations

Usage (org.killbill.billing.catalog.api.Usage)38 Test (org.testng.annotations.Test)22 LocalDate (org.joda.time.LocalDate)21 BigDecimal (java.math.BigDecimal)18 DefaultTier (org.killbill.billing.catalog.DefaultTier)17 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)16 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)15 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)12 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)12 HashMap (java.util.HashMap)11 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)9 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)8 UsageConsumableInArrearTierUnitAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate)8 ArrayList (java.util.ArrayList)7 BillingEvent (org.killbill.billing.junction.BillingEvent)7 Predicate (com.google.common.base.Predicate)5 LinkedList (java.util.LinkedList)5 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)5 Function (com.google.common.base.Function)4 ImmutableList (com.google.common.collect.ImmutableList)4