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;
}
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);
}
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);
}
Aggregations