Search in sources :

Example 1 with TieredBlock

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;
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) DefaultTieredBlockPriceOverride(org.killbill.billing.catalog.DefaultTieredBlockPriceOverride) CatalogOverrideBlockDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverrideBlockDefinitionModelDao) TieredBlock(org.killbill.billing.catalog.api.TieredBlock) ArrayList(java.util.ArrayList) DefaultTieredBlockPriceOverride(org.killbill.billing.catalog.DefaultTieredBlockPriceOverride) Predicate(com.google.common.base.Predicate)

Example 2 with TieredBlock

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);
}
Also used : TieredBlock(org.killbill.billing.catalog.api.TieredBlock) UsageConsumableInArrearTierUnitAggregate(org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate)

Example 3 with TieredBlock

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;
}
Also used : TieredBlock(org.killbill.billing.catalog.api.TieredBlock) Tier(org.killbill.billing.catalog.api.Tier) HashSet(java.util.HashSet)

Example 4 with TieredBlock

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;
}
Also used : TieredBlock(org.killbill.billing.catalog.api.TieredBlock) Tier(org.killbill.billing.catalog.api.Tier)

Example 5 with TieredBlock

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;
}
Also used : TieredBlock(org.killbill.billing.catalog.api.TieredBlock) BigDecimal(java.math.BigDecimal) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

TieredBlock (org.killbill.billing.catalog.api.TieredBlock)8 Tier (org.killbill.billing.catalog.api.Tier)4 Predicate (com.google.common.base.Predicate)3 ArrayList (java.util.ArrayList)3 DefaultTieredBlockPriceOverride (org.killbill.billing.catalog.DefaultTieredBlockPriceOverride)3 TieredBlockPriceOverride (org.killbill.billing.catalog.api.TieredBlockPriceOverride)3 DefaultTierPriceOverride (org.killbill.billing.catalog.DefaultTierPriceOverride)2 TierPriceOverride (org.killbill.billing.catalog.api.TierPriceOverride)2 CatalogOverrideBlockDefinitionModelDao (org.killbill.billing.catalog.dao.CatalogOverrideBlockDefinitionModelDao)2 UsageConsumableInArrearTierUnitAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 BigDecimal (java.math.BigDecimal)1 HashSet (java.util.HashSet)1 CatalogOverrideTierDefinitionModelDao (org.killbill.billing.catalog.dao.CatalogOverrideTierDefinitionModelDao)1