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