use of org.killbill.billing.catalog.api.TieredBlock in project killbill by killbill.
the class DefaultOverriddenPlanCache method getTieredBlockPriceOverrides.
List<TieredBlockPriceOverride> getTieredBlockPriceOverrides(Tier curTier, CatalogOverrideTierDefinitionModelDao overriddenTier, final InternalTenantContext context) {
final List<TieredBlockPriceOverride> blockPriceOverrides = new ArrayList<TieredBlockPriceOverride>();
final List<CatalogOverrideBlockDefinitionModelDao> blockDefs = overrideDao.getOverriddenTierBlocks(overriddenTier.getRecordId(), context);
for (int i = 0; i < curTier.getTieredBlocks().length; i++) {
final TieredBlock curTieredBlock = curTier.getTieredBlocks()[i];
final CatalogOverrideBlockDefinitionModelDao overriddenTierBlock = Iterables.tryFind(blockDefs, new Predicate<CatalogOverrideBlockDefinitionModelDao>() {
@Override
public boolean apply(final CatalogOverrideBlockDefinitionModelDao input) {
return (input.getParentUnitName().equals(curTieredBlock.getUnit().getName()) && Double.compare(input.getSize(), curTieredBlock.getSize()) == 0 && Double.compare(input.getMax(), curTieredBlock.getMax()) == 0);
}
}).orNull();
if (overriddenTierBlock != null) {
blockPriceOverrides.add(new DefaultTieredBlockPriceOverride(overriddenTierBlock.getParentUnitName(), overriddenTierBlock.getSize(), overriddenTierBlock.getPrice(), Currency.valueOf(overriddenTierBlock.getCurrency()), overriddenTierBlock.getMax()));
}
}
return blockPriceOverrides;
}
use of org.killbill.billing.catalog.api.TieredBlock in project killbill by killbill.
the class ContiguousIntervalConsumableUsageInArrear method computeToBeBilledConsumableInArrearWith_TOP_TIER.
UsageConsumableInArrearTierUnitAggregate computeToBeBilledConsumableInArrearWith_TOP_TIER(final List<TieredBlock> tieredBlocks, final List<UsageConsumableInArrearTierUnitAggregate> previousUsage, final Long units) throws CatalogApiException {
Long remainingUnits = units;
// By default last last tierBlock
TieredBlock targetBlock = tieredBlocks.get(tieredBlocks.size() - 1);
int targetTierNum = tieredBlocks.size();
int tierNum = 0;
// Loop through all tier block
for (final TieredBlock tieredBlock : tieredBlocks) {
tierNum++;
final long blockTierSize = tieredBlock.getSize().longValue();
final long tmp = remainingUnits / blockTierSize + (remainingUnits % blockTierSize == 0 ? 0 : 1);
if (tmp > tieredBlock.getMax()) {
/* Includes the case where max is unlimited (-1) */
remainingUnits -= tieredBlock.getMax().longValue() * blockTierSize;
} else {
targetBlock = tieredBlock;
targetTierNum = tierNum;
break;
}
}
final long lastBlockTierSize = targetBlock.getSize().longValue();
final long nbBlocks = units / lastBlockTierSize + (units % lastBlockTierSize == 0 ? 0 : 1);
return new UsageConsumableInArrearTierUnitAggregate(targetTierNum, targetBlock.getUnit().getName(), targetBlock.getPrice().getPrice(getCurrency()), targetBlock.getSize().longValue(), nbBlocks);
}
use of org.killbill.billing.catalog.api.TieredBlock in project killbill by killbill.
the class UsageUtils method getConsumableInArrearUnitTypes.
public static Set<String> getConsumableInArrearUnitTypes(final Usage usage) {
Preconditions.checkArgument(usage.getBillingMode() == BillingMode.IN_ARREAR && usage.getUsageType() == UsageType.CONSUMABLE);
Preconditions.checkArgument(usage.getTiers().length > 0);
final Set<String> result = new HashSet<String>();
for (Tier tier : usage.getTiers()) {
for (TieredBlock tierBlock : tier.getTieredBlocks()) {
result.add(tierBlock.getUnit().getName());
}
}
return result;
}
use of org.killbill.billing.catalog.api.TieredBlock in project killbill by killbill.
the class UsageUtils method getConsumableInArrearTieredBlocks.
public static List<TieredBlock> getConsumableInArrearTieredBlocks(final Usage usage, final String unitType) {
Preconditions.checkArgument(usage.getBillingMode() == BillingMode.IN_ARREAR && usage.getUsageType() == UsageType.CONSUMABLE);
Preconditions.checkArgument(usage.getTiers().length > 0);
final List<TieredBlock> result = Lists.newLinkedList();
for (Tier tier : usage.getTiers()) {
boolean found = false;
for (TieredBlock tierBlock : tier.getTieredBlocks()) {
if (tierBlock.getUnit().getName().equals(unitType)) {
result.add(tierBlock);
found = true;
break;
}
}
// We expect this method to return an ordered list of TieredBlock, each for each tier.
Preconditions.checkState(found, "Catalog issue in usage section '%s': Missing tierBlock definition for unit '%s'", usage.getName(), unitType);
}
return result;
}
use of org.killbill.billing.catalog.api.TieredBlock in project killbill by killbill.
the class ContiguousIntervalUsageInArrear method computeToBeBilledConsumableInArrear.
/**
* @param roUnit the rolled up unit for the period
* @return the price amount that should be billed for that period/unitType
* @throws CatalogApiException
*/
@VisibleForTesting
BigDecimal computeToBeBilledConsumableInArrear(final RolledUpUnit roUnit) throws CatalogApiException {
Preconditions.checkState(isBuilt.get());
BigDecimal result = BigDecimal.ZERO;
final List<TieredBlock> tieredBlocks = getConsumableInArrearTieredBlocks(usage, roUnit.getUnitType());
int remainingUnits = roUnit.getAmount().intValue();
for (final TieredBlock tieredBlock : tieredBlocks) {
final int blockTierSize = tieredBlock.getSize().intValue();
final int tmp = remainingUnits / blockTierSize + (remainingUnits % blockTierSize == 0 ? 0 : 1);
final int nbUsedTierBlocks;
if (tmp > tieredBlock.getMax()) {
nbUsedTierBlocks = tieredBlock.getMax().intValue();
remainingUnits -= tieredBlock.getMax() * blockTierSize;
} else {
nbUsedTierBlocks = tmp;
remainingUnits = 0;
}
result = result.add(tieredBlock.getPrice().getPrice(getCurrency()).multiply(new BigDecimal(nbUsedTierBlocks)));
}
return result;
}
Aggregations