use of org.killbill.billing.catalog.DefaultUsagePriceOverride in project killbill by killbill.
the class DefaultPriceOverride method getResolvedUsageOverrides.
public List<UsagePriceOverride> getResolvedUsageOverrides(Usage[] usages, List<UsagePriceOverride> usagePriceOverrides) throws CatalogApiException {
List<UsagePriceOverride> resolvedUsageOverrides = new ArrayList<UsagePriceOverride>();
for (final Usage curUsage : usages) {
final UsagePriceOverride curOverride = Iterables.tryFind(usagePriceOverrides, new Predicate<UsagePriceOverride>() {
@Override
public boolean apply(final UsagePriceOverride input) {
return input.getName() != null && input.getName().equals(curUsage.getName());
}
}).orNull();
if (curOverride != null) {
List<TierPriceOverride> tierPriceOverrides = getResolvedTierOverrides(curUsage.getTiers(), curOverride.getTierPriceOverrides());
resolvedUsageOverrides.add(new DefaultUsagePriceOverride(curUsage.getName(), curUsage.getUsageType(), tierPriceOverrides));
} else {
resolvedUsageOverrides.add(null);
}
}
return resolvedUsageOverrides;
}
use of org.killbill.billing.catalog.DefaultUsagePriceOverride in project killbill by killbill.
the class DefaultOverriddenPlanCache method getUsagePriceOverrides.
List<UsagePriceOverride> getUsagePriceOverrides(PlanPhase curPhase, CatalogOverridePhaseDefinitionModelDao overriddenPhase, final InternalTenantContext context) {
final List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>();
final List<CatalogOverrideUsageDefinitionModelDao> usageDefs = overrideDao.getOverriddenPhaseUsages(overriddenPhase.getRecordId(), context);
for (int i = 0; i < curPhase.getUsages().length; i++) {
final Usage curUsage = curPhase.getUsages()[i];
final CatalogOverrideUsageDefinitionModelDao overriddenUsage = Iterables.tryFind(usageDefs, new Predicate<CatalogOverrideUsageDefinitionModelDao>() {
@Override
public boolean apply(final CatalogOverrideUsageDefinitionModelDao input) {
return input.getParentUsageName().equals(curUsage.getName());
}
}).orNull();
if (overriddenUsage != null) {
List<TierPriceOverride> tierPriceOverrides = getTierPriceOverrides(curUsage, overriddenUsage, context);
usagePriceOverrides.add(new DefaultUsagePriceOverride(overriddenUsage.getParentUsageName(), curUsage.getUsageType(), tierPriceOverrides));
}
}
return usagePriceOverrides;
}
use of org.killbill.billing.catalog.DefaultUsagePriceOverride in project killbill by killbill.
the class TestCatalogOverrideDao method testGetOverrideOneOutOfTwoTieredBlocks.
@Test(groups = "slow")
public void testGetOverrideOneOutOfTwoTieredBlocks() throws Exception {
final StandaloneCatalog catalog = getCatalog("UsageExperimental.xml");
final Plan plan = catalog.findPlan("chocolate-monthly");
final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
List<TieredBlockPriceOverride> tieredBlockPriceOverrides = new ArrayList<TieredBlockPriceOverride>();
DefaultTieredBlockPriceOverride tieredBlockPriceOverride = new DefaultTieredBlockPriceOverride("chocolate-videos", new Double("1"), new BigDecimal(0.75), Currency.USD, new Double("10000"));
tieredBlockPriceOverrides.add(tieredBlockPriceOverride);
List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
DefaultTierPriceOverride tierPriceOverride = new DefaultTierPriceOverride(tieredBlockPriceOverrides);
tierPriceOverrides.add(null);
tierPriceOverrides.add(tierPriceOverride);
List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>();
DefaultUsagePriceOverride usagePriceOverride = new DefaultUsagePriceOverride("chocolate-monthly-videos", UsageType.CONSUMABLE, tierPriceOverrides);
usagePriceOverrides.add(usagePriceOverride);
// Override chocolate-videos unit price with size = 1 and max value = 10000 from $1 to $0.75
resolvedOverrides[0] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, null, usagePriceOverrides);
final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan, new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
final List<CatalogOverridePhaseDefinitionModelDao> phases = catalogOverrideDao.getOverriddenPlanPhases(newPlan.getRecordId(), internalCallContext);
assertEquals(phases.size(), 1);
final CatalogOverridePhaseDefinitionModelDao curPhase = phases.get(0);
assertEquals(curPhase.getCurrency(), resolvedOverrides[0].getCurrency().name());
Assert.assertNull(curPhase.getFixedPrice());
Assert.assertNull(curPhase.getRecurringPrice());
assertEquals(curPhase.getParentPhaseName(), resolvedOverrides[0].getPhaseName());
final List<CatalogOverrideUsageDefinitionModelDao> usages = catalogOverrideDao.getOverriddenPhaseUsages(curPhase.getRecordId(), internalCallContext);
assertEquals(usages.size(), 1);
final CatalogOverrideUsageDefinitionModelDao curUsage = usages.get(0);
assertEquals(curUsage.getParentUsageName(), usagePriceOverride.getName());
assertEquals(curUsage.getType(), usagePriceOverride.getUsageType().toString());
final List<CatalogOverrideTierDefinitionModelDao> tiers = catalogOverrideDao.getOverriddenUsageTiers(curUsage.getRecordId(), internalCallContext);
assertEquals(tiers.size(), 1);
final CatalogOverrideTierDefinitionModelDao curTier = tiers.get(0);
final List<CatalogOverrideBlockDefinitionModelDao> tierBlocks = catalogOverrideDao.getOverriddenTierBlocks(curTier.getRecordId(), internalCallContext);
assertEquals(tierBlocks.size(), 1);
final CatalogOverrideBlockDefinitionModelDao curTieredBlock = tierBlocks.get(0);
assertEquals(curTieredBlock.getParentUnitName(), tieredBlockPriceOverride.getUnitName());
assertEquals(curTieredBlock.getPrice().compareTo(tieredBlockPriceOverride.getPrice()), 0);
assertEquals(curTieredBlock.getSize(), (double) tieredBlockPriceOverride.getSize());
assertEquals(curTieredBlock.getMax(), (double) tieredBlockPriceOverride.getMax());
}
use of org.killbill.billing.catalog.DefaultUsagePriceOverride in project killbill by killbill.
the class TestCatalogOverrideDao method testGetOverriddenPlanPhasesWithUsageOverrides.
@Test(groups = "slow")
public void testGetOverriddenPlanPhasesWithUsageOverrides() throws Exception {
final StandaloneCatalog catalog = getCatalog("SpyCarAdvanced.xml");
final Plan plan = catalog.findPlan("gas-monthly");
final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
List<TieredBlockPriceOverride> tieredBlockPriceOverrides = new ArrayList<TieredBlockPriceOverride>();
DefaultTieredBlockPriceOverride tieredBlockPriceOverride = new DefaultTieredBlockPriceOverride("gallons", new Double("1"), new BigDecimal(4), Currency.USD, new Double("100"));
tieredBlockPriceOverrides.add(tieredBlockPriceOverride);
List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
DefaultTierPriceOverride tierPriceOverride = new DefaultTierPriceOverride(tieredBlockPriceOverrides);
tierPriceOverrides.add(tierPriceOverride);
List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>();
DefaultUsagePriceOverride usagePriceOverride = new DefaultUsagePriceOverride("gas-monthly-in-arrear", UsageType.CONSUMABLE, tierPriceOverrides);
usagePriceOverrides.add(usagePriceOverride);
// Override the gallons price from $3.95 to $4 and also the recurring price from $0 to $348.64
resolvedOverrides[0] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, BigDecimal.ZERO, new BigDecimal("348.64"), usagePriceOverrides);
final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan, new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
final List<CatalogOverridePhaseDefinitionModelDao> phases = catalogOverrideDao.getOverriddenPlanPhases(newPlan.getRecordId(), internalCallContext);
assertEquals(phases.size(), 1);
final CatalogOverridePhaseDefinitionModelDao curPhase = phases.get(0);
assertEquals(curPhase.getCurrency(), resolvedOverrides[0].getCurrency().name());
assertEquals(curPhase.getFixedPrice().compareTo(resolvedOverrides[0].getFixedPrice()), 0);
assertEquals(curPhase.getRecurringPrice().compareTo(resolvedOverrides[0].getRecurringPrice()), 0);
assertEquals(curPhase.getParentPhaseName(), resolvedOverrides[0].getPhaseName());
final List<CatalogOverrideUsageDefinitionModelDao> usages = catalogOverrideDao.getOverriddenPhaseUsages(curPhase.getRecordId(), internalCallContext);
assertEquals(usages.size(), 1);
final CatalogOverrideUsageDefinitionModelDao curUsage = usages.get(0);
assertEquals(curUsage.getParentUsageName(), usagePriceOverride.getName());
assertEquals(curUsage.getType(), usagePriceOverride.getUsageType().toString());
final List<CatalogOverrideTierDefinitionModelDao> tiers = catalogOverrideDao.getOverriddenUsageTiers(curUsage.getRecordId(), internalCallContext);
assertEquals(tiers.size(), 1);
final CatalogOverrideTierDefinitionModelDao curTier = tiers.get(0);
final List<CatalogOverrideBlockDefinitionModelDao> tierBlocks = catalogOverrideDao.getOverriddenTierBlocks(curTier.getRecordId(), internalCallContext);
assertEquals(tierBlocks.size(), 1);
final CatalogOverrideBlockDefinitionModelDao curTieredBlock = tierBlocks.get(0);
assertEquals(curTieredBlock.getParentUnitName(), tieredBlockPriceOverride.getUnitName());
assertEquals(curTieredBlock.getPrice().compareTo(tieredBlockPriceOverride.getPrice()), 0);
assertEquals(curTieredBlock.getSize(), (double) tieredBlockPriceOverride.getSize());
assertEquals(curTieredBlock.getMax(), (double) tieredBlockPriceOverride.getMax());
}
Aggregations