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