Search in sources :

Example 1 with UsageInArrearTierUnitDetail

use of org.killbill.billing.invoice.usage.details.UsageInArrearTierUnitDetail in project killbill by killbill.

the class ContiguousIntervalCapacityUsageInArrear method computeToBeBilledCapacityInArrear.

@VisibleForTesting
UsageCapacityInArrearAggregate computeToBeBilledCapacityInArrear(final List<RolledUpUnit> roUnits) throws CatalogApiException {
    Preconditions.checkState(isBuilt.get());
    final List<Tier> tiers = getCapacityInArrearTier(usage);
    final Set<String> perUnitTypeDetailTierLevel = new HashSet<String>();
    int tierNum = 0;
    final List<UsageInArrearTierUnitDetail> toBeBilledDetails = Lists.newLinkedList();
    for (final Tier cur : tiers) {
        tierNum++;
        final BigDecimal curTierPrice = cur.getRecurringPrice().getPrice(getCurrency());
        boolean complies = true;
        // Support for $0 Usage item
        boolean allUnitAmountToZero = 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;
            } else {
                allUnitAmountToZero = ro.getAmount() > 0 ? false : allUnitAmountToZero;
                if (!perUnitTypeDetailTierLevel.contains(ro.getUnitType())) {
                    toBeBilledDetails.add(new UsageInArrearTierUnitDetail(tierNum, ro.getUnitType(), curTierPrice, ro.getAmount()));
                    perUnitTypeDetailTierLevel.add(ro.getUnitType());
                }
            }
        }
        if (complies) {
            return new UsageCapacityInArrearAggregate(toBeBilledDetails, allUnitAmountToZero ? BigDecimal.ZERO : curTierPrice);
        }
    }
    // Probably invalid catalog config
    joiner.join(roUnits);
    Preconditions.checkState(false, "Could not find tier for usage " + usage.getName() + "matching with data = " + joiner.join(roUnits));
    return null;
}
Also used : Tier(org.killbill.billing.catalog.api.Tier) UsageUtils.getCapacityInArrearTier(org.killbill.billing.invoice.usage.UsageUtils.getCapacityInArrearTier) BigDecimal(java.math.BigDecimal) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) UsageInArrearTierUnitDetail(org.killbill.billing.invoice.usage.details.UsageInArrearTierUnitDetail) Limit(org.killbill.billing.catalog.api.Limit) UsageCapacityInArrearAggregate(org.killbill.billing.invoice.usage.details.UsageCapacityInArrearAggregate) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with UsageInArrearTierUnitDetail

use of org.killbill.billing.invoice.usage.details.UsageInArrearTierUnitDetail in project killbill by killbill.

the class TestContiguousIntervalCapacityInArrear method testMultipleItemsAndTiersWithExistingItems.

@Test(groups = "fast")
public void testMultipleItemsAndTiersWithExistingItems() throws Exception {
    // let's assume we have some existing usage
    final UsageInArrearTierUnitDetail existingFooUsageTier1 = new UsageInArrearTierUnitDetail(1, "FOO", BigDecimal.ONE, 9L);
    final UsageInArrearTierUnitDetail existingBarUsageTier2 = new UsageInArrearTierUnitDetail(2, "BAR", BigDecimal.TEN, 200L);
    List<RawUsageRecord> rawUsageRecords = new ArrayList<RawUsageRecord>();
    // tier 3
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 60L, "tracking-1"));
    // tier 2
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 200L, "tracking-1"));
    final List<UsageInArrearTierUnitDetail> existingUsage = ImmutableList.of(existingFooUsageTier1, existingBarUsageTier2);
    final String existingUsageJson = objectMapper.writeValueAsString(new UsageCapacityInArrearAggregate(existingUsage, BigDecimal.TEN));
    final List<InvoiceItem> existingItems = new ArrayList<InvoiceItem>();
    final InvoiceItem ii1 = new UsageInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, productName, planName, phaseName, usageName, null, new LocalDate(2014, 03, 20), new LocalDate(2014, 04, 15), BigDecimal.TEN, null, currency, null, existingUsageJson);
    existingItems.add(ii1);
    List<InvoiceItem> result = produceInvoiceItems(rawUsageRecords, UsageDetailMode.AGGREGATE, existingItems);
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("90.00")), 0, String.format("%s != 90.0", result.get(0).getAmount()));
    UsageCapacityInArrearAggregate itemDetails = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageCapacityInArrearAggregate>() {
    });
    assertEquals(itemDetails.getAmount().compareTo(new BigDecimal("100.00")), 0);
    assertEquals(itemDetails.getTierDetails().size(), 2);
    List<UsageInArrearTierUnitDetail> itemUnitDetails = itemDetails.getTierDetails();
    // BAR item detail
    assertEquals(itemUnitDetails.get(0).getTierUnit(), "BAR");
    assertEquals(itemUnitDetails.get(0).getTier(), 2);
    assertEquals(itemUnitDetails.get(0).getQuantity().intValue(), 200);
    assertEquals(itemUnitDetails.get(0).getTierPrice().compareTo(BigDecimal.TEN), 0);
    // FOO item detail
    assertEquals(itemUnitDetails.get(1).getTierUnit(), "FOO");
    assertEquals(itemUnitDetails.get(1).getTier(), 3);
    assertEquals(itemUnitDetails.get(1).getQuantity().intValue(), 60);
    assertEquals(itemUnitDetails.get(1).getTierPrice().compareTo(new BigDecimal("100.00")), 0);
}
Also used : 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) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) UsageInArrearTierUnitDetail(org.killbill.billing.invoice.usage.details.UsageInArrearTierUnitDetail) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) UsageCapacityInArrearAggregate(org.killbill.billing.invoice.usage.details.UsageCapacityInArrearAggregate) Test(org.testng.annotations.Test)

Example 3 with UsageInArrearTierUnitDetail

use of org.killbill.billing.invoice.usage.details.UsageInArrearTierUnitDetail in project killbill by killbill.

the class TestContiguousIntervalCapacityInArrear method testMultipleItemsAndTiers.

private void testMultipleItemsAndTiers(UsageDetailMode usageDetailMode) throws Exception {
    // Case 1
    List<RawUsageRecord> rawUsageRecords = new ArrayList<RawUsageRecord>();
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 5L, "tracking-1"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 99L, "tracking-2"));
    List<InvoiceItem> result = produceInvoiceItems(rawUsageRecords, usageDetailMode, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(BigDecimal.ONE), 0, String.format("%s != 1.0", result.get(0).getAmount()));
    UsageCapacityInArrearAggregate itemDetails = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageCapacityInArrearAggregate>() {
    });
    assertEquals(itemDetails.getAmount().compareTo(BigDecimal.ONE), 0);
    assertEquals(itemDetails.getTierDetails().size(), 2);
    List<UsageInArrearTierUnitDetail> itemUnitDetails = itemDetails.getTierDetails();
    // BAR item detail
    assertEquals(itemUnitDetails.get(0).getTierUnit(), "BAR");
    assertEquals(itemUnitDetails.get(0).getTier(), 1);
    assertEquals(itemUnitDetails.get(0).getQuantity().intValue(), 99);
    assertEquals(itemUnitDetails.get(0).getTierPrice().compareTo(BigDecimal.ONE), 0);
    // FOO item detail
    assertEquals(itemUnitDetails.get(1).getTierUnit(), "FOO");
    assertEquals(itemUnitDetails.get(1).getTier(), 1);
    assertEquals(itemUnitDetails.get(1).getQuantity().intValue(), 5);
    assertEquals(itemUnitDetails.get(1).getTierPrice().compareTo(BigDecimal.ONE), 0);
    // Case 2
    rawUsageRecords = new ArrayList<RawUsageRecord>();
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 5L, "tracking-1"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 101L, "tracking-2"));
    result = produceInvoiceItems(rawUsageRecords, usageDetailMode, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(BigDecimal.TEN), 0, String.format("%s != 10.0", result.get(0).getAmount()));
    itemDetails = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageCapacityInArrearAggregate>() {
    });
    assertEquals(itemDetails.getAmount().compareTo(BigDecimal.TEN), 0);
    assertEquals(itemDetails.getTierDetails().size(), 2);
    itemUnitDetails = itemDetails.getTierDetails();
    // FOO item detail
    assertEquals(itemUnitDetails.get(0).getTierUnit(), "FOO");
    assertEquals(itemUnitDetails.get(0).getTier(), 1);
    assertEquals(itemUnitDetails.get(0).getQuantity().intValue(), 5);
    assertEquals(itemUnitDetails.get(0).getTierPrice().compareTo(BigDecimal.ONE), 0);
    // BAR item detail
    assertEquals(itemUnitDetails.get(1).getTierUnit(), "BAR");
    assertEquals(itemUnitDetails.get(1).getTier(), 2);
    assertEquals(itemUnitDetails.get(1).getQuantity().intValue(), 101);
    assertEquals(itemUnitDetails.get(1).getTierPrice().compareTo(BigDecimal.TEN), 0);
    // Case 3
    rawUsageRecords = new ArrayList<RawUsageRecord>();
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 20), "FOO", 75L, "tracking-3"));
    rawUsageRecords.add(new DefaultRawUsage(subscriptionId, new LocalDate(2014, 03, 21), "BAR", 101L, "tracking-3"));
    result = produceInvoiceItems(rawUsageRecords, usageDetailMode, ImmutableList.<InvoiceItem>of());
    assertEquals(result.size(), 1);
    assertEquals(result.get(0).getAmount().compareTo(new BigDecimal("100.0")), 0, String.format("%s != 100.0", result.get(0).getAmount()));
    itemDetails = objectMapper.readValue(result.get(0).getItemDetails(), new TypeReference<UsageCapacityInArrearAggregate>() {
    });
    assertEquals(itemDetails.getAmount().compareTo(new BigDecimal("100.0")), 0);
    assertEquals(itemDetails.getTierDetails().size(), 2);
    itemUnitDetails = itemDetails.getTierDetails();
    // BAR item detail
    assertEquals(itemUnitDetails.get(0).getTierUnit(), "BAR");
    assertEquals(itemUnitDetails.get(0).getTier(), 2);
    assertEquals(itemUnitDetails.get(0).getQuantity().intValue(), 101);
    assertEquals(itemUnitDetails.get(0).getTierPrice().compareTo(new BigDecimal("10.0")), 0);
    // FOO item detail
    assertEquals(itemUnitDetails.get(1).getTierUnit(), "FOO");
    assertEquals(itemUnitDetails.get(1).getTier(), 3);
    assertEquals(itemUnitDetails.get(1).getQuantity().intValue(), 75);
    assertEquals(itemUnitDetails.get(1).getTierPrice().compareTo(new BigDecimal("100.0")), 0);
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) UsageInvoiceItem(org.killbill.billing.invoice.model.UsageInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) UsageInArrearTierUnitDetail(org.killbill.billing.invoice.usage.details.UsageInArrearTierUnitDetail) DefaultRawUsage(org.killbill.billing.usage.api.svcs.DefaultRawUsage) TypeReference(com.fasterxml.jackson.core.type.TypeReference) UsageCapacityInArrearAggregate(org.killbill.billing.invoice.usage.details.UsageCapacityInArrearAggregate)

Aggregations

BigDecimal (java.math.BigDecimal)3 UsageCapacityInArrearAggregate (org.killbill.billing.invoice.usage.details.UsageCapacityInArrearAggregate)3 UsageInArrearTierUnitDetail (org.killbill.billing.invoice.usage.details.UsageInArrearTierUnitDetail)3 ArrayList (java.util.ArrayList)2 LocalDate (org.joda.time.LocalDate)2 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)2 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)2 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)2 RawUsageRecord (org.killbill.billing.usage.api.RawUsageRecord)2 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 HashSet (java.util.HashSet)1 Limit (org.killbill.billing.catalog.api.Limit)1 Tier (org.killbill.billing.catalog.api.Tier)1 UsageUtils.getCapacityInArrearTier (org.killbill.billing.invoice.usage.UsageUtils.getCapacityInArrearTier)1 RolledUpUnit (org.killbill.billing.usage.api.RolledUpUnit)1 Test (org.testng.annotations.Test)1