Search in sources :

Example 1 with Tier

use of org.killbill.billing.catalog.api.Tier in project killbill by killbill.

the class DefaultCatalogOverrideDao method getOrCreateOverrideUsageDefinitionFromTransaction.

private CatalogOverrideUsageDefinitionModelDao getOrCreateOverrideUsageDefinitionFromTransaction(final Usage parentUsage, Currency currency, final DateTime catalogEffectiveDate, final UsagePriceOverride override, final Handle inTransactionHandle, final InternalCallContext context) {
    final List<TierPriceOverride> resolvedTierOverrides = override.getTierPriceOverrides();
    final CatalogOverrideTierDefinitionModelDao[] overrideTierDefinitionModelDaos = new CatalogOverrideTierDefinitionModelDao[resolvedTierOverrides.size()];
    for (int i = 0; i < resolvedTierOverrides.size(); i++) {
        final TierPriceOverride curOverride = resolvedTierOverrides.get(i);
        if (curOverride != null) {
            Tier parentTier = parentUsage.getTiers()[i];
            final CatalogOverrideTierDefinitionModelDao createdOverrideTierDefinitionModelDao = getOrCreateOverrideTierDefinitionFromTransaction(parentTier, curOverride, currency, catalogEffectiveDate, inTransactionHandle, context);
            overrideTierDefinitionModelDaos[i] = createdOverrideTierDefinitionModelDao;
        }
    }
    final CatalogOverrideUsageDefinitionSqlDao sqlDao = inTransactionHandle.attach(CatalogOverrideUsageDefinitionSqlDao.class);
    final List<Long> targetUsageDefinitionRecordIds = getOverrideUsageDefinitionFromTransaction(overrideTierDefinitionModelDaos, inTransactionHandle, context);
    List<CatalogOverrideUsageDefinitionModelDao> results = sqlDao.getByAttributes(parentUsage.getName(), context);
    for (CatalogOverrideUsageDefinitionModelDao usage : results) {
        if (targetUsageDefinitionRecordIds != null && targetUsageDefinitionRecordIds.contains(usage.getRecordId())) {
            return usage;
        }
    }
    final CatalogOverrideUsageDefinitionModelDao inputUsageDef = new CatalogOverrideUsageDefinitionModelDao(parentUsage.getName(), parentUsage.getUsageType().name(), currency.name(), null, null, catalogEffectiveDate);
    final Long recordId = sqlDao.create(inputUsageDef, context);
    final CatalogOverrideUsageDefinitionModelDao resultUsageDef = sqlDao.getByRecordId(recordId, context);
    for (short i = 0; i < overrideTierDefinitionModelDaos.length; i++) {
        if (overrideTierDefinitionModelDaos[i] != null) {
            createCatalogOverrideUsageTierFromTransaction(i, overrideTierDefinitionModelDaos[i], resultUsageDef, inTransactionHandle, context);
        }
    }
    return resultUsageDef;
}
Also used : Tier(org.killbill.billing.catalog.api.Tier) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride)

Example 2 with Tier

use of org.killbill.billing.catalog.api.Tier 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 3 with Tier

use of org.killbill.billing.catalog.api.Tier 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 4 with Tier

use of org.killbill.billing.catalog.api.Tier 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 5 with Tier

use of org.killbill.billing.catalog.api.Tier in project killbill by killbill.

the class ContiguousIntervalUsageInArrear method computeToBeBilledCapacityInArrear.

/**
     * @param roUnits the list of rolled up units for the period
     * @return the price amount that should be billed for that period/unitType
     * @throws CatalogApiException
     */
@VisibleForTesting
BigDecimal computeToBeBilledCapacityInArrear(final List<RolledUpUnit> roUnits) throws CatalogApiException {
    Preconditions.checkState(isBuilt.get());
    final List<Tier> tiers = getCapacityInArrearTier(usage);
    for (final Tier cur : tiers) {
        boolean complies = 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;
                break;
            }
        }
        if (complies) {
            return cur.getRecurringPrice().getPrice(getCurrency());
        }
    }
    // Probably invalid catalog config
    final Joiner joiner = Joiner.on(", ");
    joiner.join(roUnits);
    Preconditions.checkState(false, "Could not find tier for usage " + usage.getName() + "matching with data = " + joiner.join(roUnits));
    return null;
}
Also used : Joiner(com.google.common.base.Joiner) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) Tier(org.killbill.billing.catalog.api.Tier) UsageUtils.getCapacityInArrearTier(org.killbill.billing.invoice.usage.UsageUtils.getCapacityInArrearTier) Limit(org.killbill.billing.catalog.api.Limit) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

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