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