Search in sources :

Example 1 with TierPriceOverride

use of org.killbill.billing.catalog.api.TierPriceOverride 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;
}
Also used : Usage(org.killbill.billing.catalog.api.Usage) DefaultUsagePriceOverride(org.killbill.billing.catalog.DefaultUsagePriceOverride) ArrayList(java.util.ArrayList) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) DefaultUsagePriceOverride(org.killbill.billing.catalog.DefaultUsagePriceOverride) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) Predicate(com.google.common.base.Predicate)

Example 2 with TierPriceOverride

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

the class TestDefaultPriceOverride method testOverrideOneOutOfTwoTieredBlockPrices.

@Test(groups = "slow")
public void testOverrideOneOutOfTwoTieredBlockPrices() throws Exception {
    final StandaloneCatalog catalog = getCatalog("UsageExperimental.xml");
    final Plan plan = catalog.findPlan("chocolate-monthly");
    final List<PlanPhasePriceOverride> overrides = new ArrayList<PlanPhasePriceOverride>();
    final List<TieredBlockPriceOverride> tieredBlockPriceOverrides = new ArrayList<TieredBlockPriceOverride>();
    tieredBlockPriceOverrides.add(new DefaultTieredBlockPriceOverride("chocolate-videos", new Double("1"), new BigDecimal("0.75"), Currency.USD, new Double("10000")));
    final List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
    tierPriceOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides));
    final List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>();
    usagePriceOverrides.add(new DefaultUsagePriceOverride("chocolate-monthly-videos", UsageType.CONSUMABLE, tierPriceOverrides));
    final PlanPhasePriceOverride phase = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, null, usagePriceOverrides);
    overrides.add(phase);
    // Overriding only the tieredblockprice for unit - 'chocolate-videos' with size = 1 and max = 10000 from $1 to $0.75
    final DefaultPlan overriddenPlan = priceOverride.getOrCreateOverriddenPlan(catalog, plan, new DateTime(catalog.getEffectiveDate()), overrides, internalCallContext);
    final String[] parts = priceOverridePattern.getPlanParts(overriddenPlan.getName());
    assertEquals(parts[0], plan.getName());
    assertEquals(overriddenPlan.getProduct().getName(), plan.getProduct().getName());
    assertEquals(overriddenPlan.getRecurringBillingPeriod(), plan.getRecurringBillingPeriod());
    if (plan.getEffectiveDateForExistingSubscriptions() != null) {
        assertEquals(overriddenPlan.getEffectiveDateForExistingSubscriptions().compareTo(plan.getEffectiveDateForExistingSubscriptions()), 0);
    }
    assertNotEquals(overriddenPlan.getFinalPhase().getName(), plan.getFinalPhase().getName());
    for (int i = 0; i < overriddenPlan.getFinalPhase().getUsages().length; i++) {
        final DefaultUsage initialUsage = (DefaultUsage) plan.getFinalPhase().getUsages()[i];
        final DefaultUsage newUsage = (DefaultUsage) overriddenPlan.getFinalPhase().getUsages()[i];
        assertEquals(newUsage.getName(), initialUsage.getName());
        assertEquals(newUsage.getUsageType(), initialUsage.getUsageType());
        assertEquals(newUsage.getBillingPeriod(), initialUsage.getBillingPeriod());
        assertEquals(newUsage.getBillingPeriod(), initialUsage.getBillingPeriod());
        assertEquals(newUsage.getTiers().length, initialUsage.getTiers().length);
        for (int j = 0; j < newUsage.getTiers().length; j++) {
            final DefaultTier initialTier = (DefaultTier) initialUsage.getTiers()[j];
            final DefaultTier newTier = (DefaultTier) newUsage.getTiers()[j];
            assertEquals(newTier.getTieredBlocks().length, initialTier.getTieredBlocks().length);
            for (int k = 0; k < newTier.getTieredBlocks().length; k++) {
                final DefaultTieredBlock initialTieredBlock = (DefaultTieredBlock) initialTier.getTieredBlocks()[k];
                final DefaultTieredBlock newTieredBlock = (DefaultTieredBlock) newTier.getTieredBlocks()[k];
                final TieredBlockPriceOverride override = Iterables.tryFind(tieredBlockPriceOverrides, new Predicate<TieredBlockPriceOverride>() {

                    @Override
                    public boolean apply(final TieredBlockPriceOverride input) {
                        return input.getUnitName().equals(initialTieredBlock.getUnit().getName()) && Double.compare(input.getSize(), initialTieredBlock.getSize()) == 0 && Double.compare(input.getMax(), initialTieredBlock.getMax()) == 0;
                    }
                }).orNull();
                assertEquals(newTieredBlock.getUnit().getName(), initialTieredBlock.getUnit().getName());
                assertEquals(newTieredBlock.getMax(), initialTieredBlock.getMax());
                assertEquals(newTieredBlock.getSize(), initialTieredBlock.getSize());
                assertTieredBlockInternationalPrice(newTieredBlock.getPrice(), initialTieredBlock.getPrice(), override);
            }
        }
    }
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) ArrayList(java.util.ArrayList) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) DateTime(org.joda.time.DateTime) Predicate(com.google.common.base.Predicate) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 3 with TierPriceOverride

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

the class TestDefaultPriceOverride method testOverrideTwoOutOfTwoTieredBlockPrices.

@Test(groups = "slow")
public void testOverrideTwoOutOfTwoTieredBlockPrices() throws Exception {
    final StandaloneCatalog catalog = getCatalog("UsageExperimental.xml");
    final Plan plan = catalog.findPlan("chocolate-monthly");
    final List<PlanPhasePriceOverride> overrides = new ArrayList<PlanPhasePriceOverride>();
    final List<TieredBlockPriceOverride> tieredBlockPriceOverrides1 = new ArrayList<TieredBlockPriceOverride>();
    tieredBlockPriceOverrides1.add(new DefaultTieredBlockPriceOverride("chocolate-videos", new Double("1"), new BigDecimal("1.5"), Currency.USD, new Double("5")));
    final List<TieredBlockPriceOverride> tieredBlockPriceOverrides2 = new ArrayList<TieredBlockPriceOverride>();
    tieredBlockPriceOverrides2.add(new DefaultTieredBlockPriceOverride("chocolate-videos", new Double("1"), new BigDecimal("0.75"), Currency.USD, new Double("10000")));
    final List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
    tierPriceOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides1));
    tierPriceOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides2));
    final List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>();
    usagePriceOverrides.add(new DefaultUsagePriceOverride("chocolate-monthly-videos", UsageType.CONSUMABLE, tierPriceOverrides));
    final PlanPhasePriceOverride phase = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, new BigDecimal("35"), usagePriceOverrides);
    overrides.add(phase);
    /* Overriding phase recurring price from $30 to $35, tieredblockprice from $2 to $1.5 for unit- 'chocolate-videos' with size = 1 and max = 5 and
         also overriding tieredblockprice from $1 to $0.75 for unit - 'chocolate-videos' with size = 1 and max = 10000 */
    final DefaultPlan overriddenPlan = priceOverride.getOrCreateOverriddenPlan(catalog, plan, new DateTime(catalog.getEffectiveDate()), overrides, internalCallContext);
    final String[] parts = priceOverridePattern.getPlanParts(overriddenPlan.getName());
    assertEquals(parts[0], plan.getName());
    assertEquals(overriddenPlan.getProduct().getName(), plan.getProduct().getName());
    assertEquals(overriddenPlan.getRecurringBillingPeriod(), plan.getRecurringBillingPeriod());
    if (plan.getEffectiveDateForExistingSubscriptions() != null) {
        assertEquals(overriddenPlan.getEffectiveDateForExistingSubscriptions().compareTo(plan.getEffectiveDateForExistingSubscriptions()), 0);
    }
    assertNotEquals(overriddenPlan.getFinalPhase().getName(), plan.getFinalPhase().getName());
    final DefaultPlanPhase initialPhase = (DefaultPlanPhase) plan.getFinalPhase();
    final DefaultPlanPhase newPhase = (DefaultPlanPhase) overriddenPlan.getFinalPhase();
    final PlanPhasePriceOverride override = Iterables.tryFind(overrides, new Predicate<PlanPhasePriceOverride>() {

        @Override
        public boolean apply(final PlanPhasePriceOverride input) {
            return input.getPhaseName().equals(initialPhase.getName());
        }
    }).orNull();
    assertNotEquals(newPhase.getName(), initialPhase.getName());
    assertEquals(newPhase.getName(), overriddenPlan.getName() + "-" + initialPhase.getName().split("-")[initialPhase.getName().split("-").length - 1]);
    assertEquals(newPhase.getDuration(), initialPhase.getDuration());
    assertEquals(newPhase.getPhaseType(), initialPhase.getPhaseType());
    assertEquals(newPhase.getUsages().length, initialPhase.getUsages().length);
    if (initialPhase.getFixed() != null) {
        assertEquals(newPhase.getFixed().getType(), initialPhase.getFixed().getType());
        assertInternationalPrice(newPhase.getFixed().getPrice(), initialPhase.getFixed().getPrice(), override, true);
    }
    if (initialPhase.getRecurring() != null) {
        assertInternationalPrice(newPhase.getRecurring().getRecurringPrice(), initialPhase.getRecurring().getRecurringPrice(), override, false);
    }
    for (int i = 0; i < overriddenPlan.getFinalPhase().getUsages().length; i++) {
        final DefaultUsage initialUsage = (DefaultUsage) plan.getFinalPhase().getUsages()[i];
        final DefaultUsage newUsage = (DefaultUsage) overriddenPlan.getFinalPhase().getUsages()[i];
        assertEquals(newUsage.getName(), initialUsage.getName());
        assertEquals(newUsage.getUsageType(), initialUsage.getUsageType());
        assertEquals(newUsage.getBillingPeriod(), initialUsage.getBillingPeriod());
        assertEquals(newUsage.getBillingPeriod(), initialUsage.getBillingPeriod());
        assertEquals(newUsage.getTiers().length, initialUsage.getTiers().length);
        for (int j = 0; j < newUsage.getTiers().length; j++) {
            final DefaultTier initialTier = (DefaultTier) initialUsage.getTiers()[j];
            final DefaultTier newTier = (DefaultTier) newUsage.getTiers()[j];
            assertEquals(newTier.getTieredBlocks().length, initialTier.getTieredBlocks().length);
            for (int k = 0; k < newTier.getTieredBlocks().length; k++) {
                final DefaultTieredBlock initialTieredBlock = (DefaultTieredBlock) initialTier.getTieredBlocks()[k];
                final DefaultTieredBlock newTieredBlock = (DefaultTieredBlock) newTier.getTieredBlocks()[k];
                List<TieredBlockPriceOverride> tieredBlockPriceOverrides = new ArrayList<TieredBlockPriceOverride>();
                tieredBlockPriceOverrides.addAll(tieredBlockPriceOverrides1);
                tieredBlockPriceOverrides.addAll(tieredBlockPriceOverrides2);
                final TieredBlockPriceOverride tieredBlockPriceOverride = Iterables.tryFind(tieredBlockPriceOverrides, new Predicate<TieredBlockPriceOverride>() {

                    @Override
                    public boolean apply(final TieredBlockPriceOverride input) {
                        return input.getUnitName().equals(initialTieredBlock.getUnit().getName()) && Double.compare(input.getSize(), initialTieredBlock.getSize()) == 0 && Double.compare(input.getMax(), initialTieredBlock.getMax()) == 0;
                    }
                }).orNull();
                assertEquals(newTieredBlock.getUnit().getName(), initialTieredBlock.getUnit().getName());
                assertEquals(newTieredBlock.getMax(), initialTieredBlock.getMax());
                assertEquals(newTieredBlock.getSize(), initialTieredBlock.getSize());
                assertTieredBlockInternationalPrice(newTieredBlock.getPrice(), initialTieredBlock.getPrice(), tieredBlockPriceOverride);
            }
        }
    }
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) ArrayList(java.util.ArrayList) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) DateTime(org.joda.time.DateTime) Predicate(com.google.common.base.Predicate) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with TierPriceOverride

use of org.killbill.billing.catalog.api.TierPriceOverride 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;
}
Also used : CatalogOverrideUsageDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverrideUsageDefinitionModelDao) Usage(org.killbill.billing.catalog.api.Usage) DefaultUsagePriceOverride(org.killbill.billing.catalog.DefaultUsagePriceOverride) ArrayList(java.util.ArrayList) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) DefaultUsagePriceOverride(org.killbill.billing.catalog.DefaultUsagePriceOverride) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) Predicate(com.google.common.base.Predicate)

Example 5 with TierPriceOverride

use of org.killbill.billing.catalog.api.TierPriceOverride 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)

Aggregations

TierPriceOverride (org.killbill.billing.catalog.api.TierPriceOverride)10 ArrayList (java.util.ArrayList)8 TieredBlockPriceOverride (org.killbill.billing.catalog.api.TieredBlockPriceOverride)7 UsagePriceOverride (org.killbill.billing.catalog.api.UsagePriceOverride)7 Predicate (com.google.common.base.Predicate)6 DefaultTierPriceOverride (org.killbill.billing.catalog.DefaultTierPriceOverride)6 BigDecimal (java.math.BigDecimal)5 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)5 DateTime (org.joda.time.DateTime)4 DefaultTieredBlockPriceOverride (org.killbill.billing.catalog.DefaultTieredBlockPriceOverride)4 DefaultUsagePriceOverride (org.killbill.billing.catalog.DefaultUsagePriceOverride)4 Plan (org.killbill.billing.catalog.api.Plan)4 Test (org.testng.annotations.Test)4 Tier (org.killbill.billing.catalog.api.Tier)3 DefaultPlanPhasePriceOverride (org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)2 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)2 TieredBlock (org.killbill.billing.catalog.api.TieredBlock)2 Usage (org.killbill.billing.catalog.api.Usage)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1