Search in sources :

Example 6 with TieredBlockPriceOverride

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

the class DefaultPriceOverride method getResolvedTierOverrides.

public List<TierPriceOverride> getResolvedTierOverrides(Tier[] tiers, List<TierPriceOverride> tierPriceOverrides) throws CatalogApiException {
    List<TierPriceOverride> resolvedTierOverrides = new ArrayList<TierPriceOverride>();
    for (final Tier curTier : tiers) {
        final TierPriceOverride curOverride = Iterables.tryFind(tierPriceOverrides, new Predicate<TierPriceOverride>() {

            @Override
            public boolean apply(final TierPriceOverride input) {
                if (input.getTieredBlockPriceOverrides() != null) {
                    for (TieredBlockPriceOverride blockPriceOverride : input.getTieredBlockPriceOverrides()) {
                        String unitName = blockPriceOverride.getUnitName();
                        Double max = blockPriceOverride.getMax();
                        Double size = blockPriceOverride.getSize();
                        for (int i = 0; i < curTier.getTieredBlocks().length; i++) {
                            TieredBlock curTieredBlock = curTier.getTieredBlocks()[i];
                            if (unitName.equals(curTieredBlock.getUnit().getName()) && Double.compare(size, curTieredBlock.getSize()) == 0 && Double.compare(max, curTieredBlock.getMax()) == 0) {
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
        }).orNull();
        if (curOverride != null) {
            List<TieredBlockPriceOverride> tieredBlockPriceOverrides = getResolvedTieredBlockPriceOverrides(curTier.getTieredBlocks(), curOverride.getTieredBlockPriceOverrides());
            resolvedTierOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides));
        } else {
            resolvedTierOverrides.add(null);
        }
    }
    return resolvedTierOverrides;
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) DefaultTieredBlockPriceOverride(org.killbill.billing.catalog.DefaultTieredBlockPriceOverride) TieredBlock(org.killbill.billing.catalog.api.TieredBlock) Tier(org.killbill.billing.catalog.api.Tier) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) ArrayList(java.util.ArrayList) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) Predicate(com.google.common.base.Predicate)

Example 7 with TieredBlockPriceOverride

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

the class DefaultOverriddenPlanCache method getTierPriceOverrides.

List<TierPriceOverride> getTierPriceOverrides(Usage curUsage, CatalogOverrideUsageDefinitionModelDao overriddenUsage, final InternalTenantContext context) {
    final List<TierPriceOverride> tierPriceOverrides = new ArrayList<TierPriceOverride>();
    final List<CatalogOverrideTierDefinitionModelDao> tierDefs = overrideDao.getOverriddenUsageTiers(overriddenUsage.getRecordId(), context);
    for (int i = 0; i < curUsage.getTiers().length; i++) {
        final Tier curTier = curUsage.getTiers()[i];
        final TieredBlock[] curTieredBlocks = curTier.getTieredBlocks();
        final CatalogOverrideTierDefinitionModelDao overriddenTier = Iterables.tryFind(tierDefs, new Predicate<CatalogOverrideTierDefinitionModelDao>() {

            @Override
            public boolean apply(final CatalogOverrideTierDefinitionModelDao input) {
                final List<CatalogOverrideBlockDefinitionModelDao> blockDefs = overrideDao.getOverriddenTierBlocks(input.getRecordId(), context);
                for (CatalogOverrideBlockDefinitionModelDao blockDef : blockDefs) {
                    String unitName = blockDef.getParentUnitName();
                    Double max = blockDef.getMax();
                    Double size = blockDef.getSize();
                    for (TieredBlock curTieredBlock : curTieredBlocks) {
                        if (unitName.equals(curTieredBlock.getUnit().getName()) && Double.compare(size, curTieredBlock.getSize()) == 0 && Double.compare(max, curTieredBlock.getMax()) == 0) {
                            return true;
                        }
                    }
                }
                return false;
            }
        }).orNull();
        if (overriddenTier != null) {
            List<TieredBlockPriceOverride> tieredBlockPriceOverrides = getTieredBlockPriceOverrides(curTier, overriddenTier, context);
            tierPriceOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides));
        }
    }
    return tierPriceOverrides;
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) DefaultTieredBlockPriceOverride(org.killbill.billing.catalog.DefaultTieredBlockPriceOverride) Tier(org.killbill.billing.catalog.api.Tier) ArrayList(java.util.ArrayList) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride) CatalogOverrideTierDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverrideTierDefinitionModelDao) Predicate(com.google.common.base.Predicate) TieredBlock(org.killbill.billing.catalog.api.TieredBlock) CatalogOverrideBlockDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverrideBlockDefinitionModelDao) DefaultTierPriceOverride(org.killbill.billing.catalog.DefaultTierPriceOverride)

Example 8 with TieredBlockPriceOverride

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

the class DefaultCatalogOverrideDao method getOrCreateOverrideTierDefinitionFromTransaction.

private CatalogOverrideTierDefinitionModelDao getOrCreateOverrideTierDefinitionFromTransaction(final Tier parentTier, final TierPriceOverride tierPriceOverride, Currency currency, final DateTime catalogEffectiveDate, final Handle inTransactionHandle, final InternalCallContext context) {
    final List<TieredBlockPriceOverride> resolvedTierBlockOverrides = tierPriceOverride.getTieredBlockPriceOverrides();
    final CatalogOverrideBlockDefinitionModelDao[] overrideBlockDefinitionModelDaos = new CatalogOverrideBlockDefinitionModelDao[resolvedTierBlockOverrides.size()];
    for (int i = 0; i < resolvedTierBlockOverrides.size(); i++) {
        final TieredBlockPriceOverride curOverride = resolvedTierBlockOverrides.get(i);
        if (curOverride != null) {
            final CatalogOverrideBlockDefinitionModelDao createdOverrideBlockDefinitionModelDao = getOrCreateOverriddenBlockDefinitionFromTransaction(curOverride, catalogEffectiveDate, currency.name(), inTransactionHandle, context);
            overrideBlockDefinitionModelDaos[i] = createdOverrideBlockDefinitionModelDao;
        }
    }
    final CatalogOverrideTierDefinitionSqlDao sqlDao = inTransactionHandle.attach(CatalogOverrideTierDefinitionSqlDao.class);
    final Long targetTierDefinitionRecordId = getOverrideTierDefinitionFromTransaction(overrideBlockDefinitionModelDaos, inTransactionHandle, context);
    if (targetTierDefinitionRecordId != null) {
        return sqlDao.getByRecordId(targetTierDefinitionRecordId, context);
    }
    final CatalogOverrideTierDefinitionModelDao inputTierDef = new CatalogOverrideTierDefinitionModelDao(currency.name(), null, null, catalogEffectiveDate);
    final Long recordId = sqlDao.create(inputTierDef, context);
    final CatalogOverrideTierDefinitionModelDao resultTierDef = sqlDao.getByRecordId(recordId, context);
    for (short i = 0; i < overrideBlockDefinitionModelDaos.length; i++) {
        if (overrideBlockDefinitionModelDaos[i] != null) {
            createCatalogOverrideTierBlockFromTransaction(i, overrideBlockDefinitionModelDaos[i], resultTierDef, inTransactionHandle, context);
        }
    }
    return resultTierDef;
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride)

Example 9 with TieredBlockPriceOverride

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

the class SubscriptionResourceHelpers method buildUsagePrices.

private static void buildUsagePrices(final Currency currency, final PhasePriceJson input, final Collection<UsagePriceOverride> usagePrices) {
    for (final UsagePriceJson usageOverrideJson : input.getUsagePrices()) {
        final List<TierPriceOverride> tierPriceOverrides = new LinkedList<TierPriceOverride>();
        for (final TierPriceJson tierPriceJson : usageOverrideJson.getTierPrices()) {
            final List<TieredBlockPriceOverride> blockPriceOverrides = new LinkedList<TieredBlockPriceOverride>();
            for (final BlockPriceJson block : tierPriceJson.getBlockPrices()) {
                blockPriceOverrides.add(new TieredBlockPriceOverride() {

                    @Override
                    public String getUnitName() {
                        return block.getUnitName();
                    }

                    @Override
                    public Double getSize() {
                        return block.getSize();
                    }

                    @Override
                    public BigDecimal getPrice() {
                        return block.getPrice();
                    }

                    @Override
                    public Currency getCurrency() {
                        return currency;
                    }

                    @Override
                    public Double getMax() {
                        return block.getMax();
                    }
                });
            }
            tierPriceOverrides.add(new TierPriceOverride() {

                @Override
                public List<TieredBlockPriceOverride> getTieredBlockPriceOverrides() {
                    return blockPriceOverrides;
                }
            });
        }
        usagePrices.add(new UsagePriceOverride() {

            @Override
            public String getName() {
                return usageOverrideJson.getUsageName();
            }

            @Override
            public UsageType getUsageType() {
                return usageOverrideJson.getUsageType();
            }

            @Override
            public List<TierPriceOverride> getTierPriceOverrides() {
                return tierPriceOverrides;
            }
        });
    }
}
Also used : TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) TierPriceJson(org.killbill.billing.jaxrs.json.TierPriceJson) UsageType(org.killbill.billing.catalog.api.UsageType) LinkedList(java.util.LinkedList) BigDecimal(java.math.BigDecimal) UsagePriceJson(org.killbill.billing.jaxrs.json.UsagePriceJson) Currency(org.killbill.billing.catalog.api.Currency) List(java.util.List) LinkedList(java.util.LinkedList) BlockPriceJson(org.killbill.billing.jaxrs.json.BlockPriceJson) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) TierPriceOverride(org.killbill.billing.catalog.api.TierPriceOverride) UsagePriceOverride(org.killbill.billing.catalog.api.UsagePriceOverride) TieredBlockPriceOverride(org.killbill.billing.catalog.api.TieredBlockPriceOverride)

Aggregations

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