Search in sources :

Example 6 with DefaultPlanPhasePriceOverride

use of org.killbill.billing.catalog.DefaultPlanPhasePriceOverride in project killbill by killbill.

the class EhCacheOverriddenPlanCache method createOverrides.

private PlanPhasePriceOverride[] createOverrides(final Plan defaultPlan, final List<CatalogOverridePhaseDefinitionModelDao> phaseDefs) {
    final PlanPhasePriceOverride[] result = new PlanPhasePriceOverride[defaultPlan.getAllPhases().length];
    for (int i = 0; i < defaultPlan.getAllPhases().length; i++) {
        final PlanPhase curPhase = defaultPlan.getAllPhases()[i];
        final CatalogOverridePhaseDefinitionModelDao overriddenPhase = Iterables.tryFind(phaseDefs, new Predicate<CatalogOverridePhaseDefinitionModelDao>() {

            @Override
            public boolean apply(final CatalogOverridePhaseDefinitionModelDao input) {
                return input.getParentPhaseName().equals(curPhase.getName());
            }
        }).orNull();
        result[i] = (overriddenPhase != null) ? new DefaultPlanPhasePriceOverride(curPhase.getName(), Currency.valueOf(overriddenPhase.getCurrency()), overriddenPhase.getFixedPrice(), overriddenPhase.getRecurringPrice()) : null;
    }
    return result;
}
Also used : DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) CatalogOverridePhaseDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverridePhaseDefinitionModelDao) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) Predicate(com.google.common.base.Predicate)

Example 7 with DefaultPlanPhasePriceOverride

use of org.killbill.billing.catalog.DefaultPlanPhasePriceOverride in project killbill by killbill.

the class TestCatalogOverrideDao method testGetOverriddenPlanPhases.

@Test(groups = "slow")
public void testGetOverriddenPlanPhases() throws Exception {
    final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("SpyCarAdvanced.xml").toExternalForm(), StandaloneCatalog.class);
    final Plan plan = catalog.findCurrentPlan("discount-standard-monthly");
    final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
    resolvedOverrides[0] = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[0].getName(), Currency.USD, BigDecimal.TEN, BigDecimal.ONE);
    resolvedOverrides[1] = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[1].getName(), Currency.USD, BigDecimal.ONE, BigDecimal.TEN);
    resolvedOverrides[2] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, BigDecimal.ZERO, new BigDecimal("348.64"));
    final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan.getName(), new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
    final List<CatalogOverridePhaseDefinitionModelDao> phases = catalogOverrideDao.getOverriddenPlanPhases(newPlan.getRecordId(), internalCallContext);
    assertEquals(phases.size(), 3);
    for (int i = 0; i < 3; i++) {
        final CatalogOverridePhaseDefinitionModelDao curPhase = phases.get(i);
        assertEquals(curPhase.getCurrency(), resolvedOverrides[i].getCurrency().name());
        assertEquals(curPhase.getFixedPrice().compareTo(resolvedOverrides[i].getFixedPrice()), 0);
        assertEquals(curPhase.getRecurringPrice().compareTo(resolvedOverrides[i].getRecurringPrice()), 0);
        assertEquals(curPhase.getParentPhaseName(), resolvedOverrides[i].getPhaseName());
    }
}
Also used : DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Test(org.testng.annotations.Test)

Example 8 with DefaultPlanPhasePriceOverride

use of org.killbill.billing.catalog.DefaultPlanPhasePriceOverride in project killbill by killbill.

the class TestCatalogOverrideDao method testOverrideTwoOutOfThreePhases.

@Test(groups = "slow")
public void testOverrideTwoOutOfThreePhases() throws Exception {
    final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("SpyCarAdvanced.xml").toExternalForm(), StandaloneCatalog.class);
    final Plan plan = catalog.findCurrentPlan("discount-standard-monthly");
    final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
    resolvedOverrides[0] = new DefaultPlanPhasePriceOverride(plan.getAllPhases()[0].getName(), Currency.USD, BigDecimal.TEN, null);
    resolvedOverrides[1] = null;
    resolvedOverrides[2] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, new BigDecimal("348.64"));
    final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan.getName(), new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
    assertEquals(newPlan.getParentPlanName(), "discount-standard-monthly");
    assertTrue(newPlan.getIsActive());
    final List<CatalogOverridePhaseDefinitionModelDao> phases = catalogOverrideDao.getOverriddenPlanPhases(newPlan.getRecordId(), internalCallContext);
    assertEquals(phases.size(), 2);
}
Also used : DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) Plan(org.killbill.billing.catalog.api.Plan) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) Test(org.testng.annotations.Test)

Example 9 with DefaultPlanPhasePriceOverride

use of org.killbill.billing.catalog.DefaultPlanPhasePriceOverride in project killbill by killbill.

the class DefaultPriceOverride method getOrCreateOverriddenPlan.

@Override
public DefaultPlan getOrCreateOverriddenPlan(final StandaloneCatalog standaloneCatalog, final Plan parentPlan, final DateTime catalogEffectiveDate, final List<PlanPhasePriceOverride> overrides, @Nullable final InternalCallContext context) throws CatalogApiException {
    final PlanPhasePriceOverride[] resolvedOverride = new PlanPhasePriceOverride[parentPlan.getAllPhases().length];
    int index = 0;
    for (final PlanPhase curPhase : parentPlan.getAllPhases()) {
        final PlanPhasePriceOverride curOverride = Iterables.tryFind(overrides, new Predicate<PlanPhasePriceOverride>() {

            @Override
            public boolean apply(final PlanPhasePriceOverride input) {
                if (input.getPhaseName() != null) {
                    return input.getPhaseName().equals(curPhase.getName());
                }
                // If the phaseName was not passed, we infer by matching the phaseType. This obviously would not work in a case where
                // a plan is defined with multiple phases of the same type.
                final PlanPhaseSpecifier curPlanPhaseSpecifier = input.getPlanPhaseSpecifier();
                if (curPlanPhaseSpecifier.getPhaseType().equals(curPhase.getPhaseType())) {
                    return true;
                }
                return false;
            }
        }).orNull();
        resolvedOverride[index++] = curOverride != null ? new DefaultPlanPhasePriceOverride(curPhase.getName(), curOverride.getCurrency(), curOverride.getFixedPrice(), curOverride.getRecurringPrice()) : null;
    }
    for (int i = 0; i < resolvedOverride.length; i++) {
        final PlanPhasePriceOverride curOverride = resolvedOverride[i];
        if (curOverride != null) {
            final DefaultPlanPhase curPhase = (DefaultPlanPhase) parentPlan.getAllPhases()[i];
            if (curPhase.getFixed() == null && curOverride.getFixedPrice() != null) {
                final String error = String.format("There is no existing fixed price for the phase %s", curPhase.getName());
                throw new CatalogApiException(ErrorCode.CAT_INVALID_INVALID_PRICE_OVERRIDE, parentPlan.getName(), error);
            }
            if (curPhase.getRecurring() == null && curOverride.getRecurringPrice() != null) {
                final String error = String.format("There is no existing recurring price for the phase %s", curPhase.getName());
                throw new CatalogApiException(ErrorCode.CAT_INVALID_INVALID_PRICE_OVERRIDE, parentPlan.getName(), error);
            }
        }
    }
    final String planName;
    if (context != null) {
        final CatalogOverridePlanDefinitionModelDao overriddenPlan = overrideDao.getOrCreateOverridePlanDefinition(parentPlan.getName(), catalogEffectiveDate, resolvedOverride, context);
        planName = new StringBuffer(parentPlan.getName()).append("-").append(overriddenPlan.getRecordId()).toString();
    } else {
        planName = new StringBuffer(parentPlan.getName()).append("-dryrun-").append(DRY_RUN_PLAN_IDX.incrementAndGet()).toString();
    }
    final DefaultPlan result = new DefaultPlan(planName, (DefaultPlan) parentPlan, resolvedOverride);
    result.initialize(standaloneCatalog, standaloneCatalog.getCatalogURI());
    if (context == null) {
        overriddenPlanCache.addDryRunPlan(planName, result);
    }
    return result;
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) DefaultPlanPhase(org.killbill.billing.catalog.DefaultPlanPhase) Predicate(com.google.common.base.Predicate) DefaultPlan(org.killbill.billing.catalog.DefaultPlan) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) DefaultPlanPhase(org.killbill.billing.catalog.DefaultPlanPhase) CatalogOverridePlanDefinitionModelDao(org.killbill.billing.catalog.dao.CatalogOverridePlanDefinitionModelDao) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)

Example 10 with DefaultPlanPhasePriceOverride

use of org.killbill.billing.catalog.DefaultPlanPhasePriceOverride in project killbill by killbill.

the class TestWithEntilementPlugin method testCreateSubscriptionWithEntitlementPlugin.

@Test(groups = "slow")
public void testCreateSubscriptionWithEntitlementPlugin() throws Exception {
    // We take april as it has 30 days (easier to play with BCD)
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    final List<PlanPhasePriceOverride> overrides = new ArrayList<PlanPhasePriceOverride>();
    overrides.add(new DefaultPlanPhasePriceOverride("shotgun-monthly-evergreen", account.getCurrency(), null, BigDecimal.TEN));
    testEntitlementPluginApi.setPlanPhasePriceOverride(overrides);
    //
    // Create original subscription (Trial PHASE) -> $0 invoice.
    final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), new LocalDate(2012, 6, 1), InvoiceItemType.RECURRING, BigDecimal.TEN));
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 6, 1), new LocalDate(2012, 7, 1), InvoiceItemType.RECURRING, BigDecimal.TEN));
}
Also used : Account(org.killbill.billing.account.api.Account) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) Test(org.testng.annotations.Test)

Aggregations

DefaultPlanPhasePriceOverride (org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)12 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)12 BigDecimal (java.math.BigDecimal)9 Test (org.testng.annotations.Test)9 ArrayList (java.util.ArrayList)6 LocalDate (org.joda.time.LocalDate)5 Account (org.killbill.billing.account.api.Account)5 AccountData (org.killbill.billing.account.api.AccountData)5 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)5 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)5 Plan (org.killbill.billing.catalog.api.Plan)4 DateTime (org.joda.time.DateTime)3 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)3 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)3 Predicate (com.google.common.base.Predicate)2 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)2 PlanSpecifier (org.killbill.billing.catalog.api.PlanSpecifier)2 Function (com.google.common.base.Function)1 Nullable (javax.annotation.Nullable)1 DefaultPlan (org.killbill.billing.catalog.DefaultPlan)1