use of org.killbill.billing.catalog.DefaultPlanPhase in project killbill by killbill.
the class StandaloneCatalogMapper method toDefaultPlanPhase.
private DefaultPlanPhase toDefaultPlanPhase(final PlanPhase input) {
final DefaultPlanPhase result = new DefaultPlanPhase();
result.setDuration(toDefaultDuration(input.getDuration()));
result.setFixed(toDefaultFixed(input.getFixed()));
result.setPhaseType(input.getPhaseType());
result.setRecurring(toDefaultRecurring(input.getRecurring()));
if (input.getUsages() != null && input.getUsages().length > 0) {
result.setUsages(toDefaultUsages(Arrays.asList(input.getUsages())));
}
return result;
}
use of org.killbill.billing.catalog.DefaultPlanPhase in project killbill by killbill.
the class TestXMLWriter method testAddPlan.
@Test(groups = "fast")
public void testAddPlan() throws Exception {
final StandaloneCatalog catalog = getCatalog("SpyCarBasic.xml");
final MutableStaticCatalog mutableCatalog = new DefaultMutableStaticCatalog(catalog);
final DefaultProduct newProduct = new DefaultProduct();
newProduct.setName("Dynamic");
newProduct.setCatagory(ProductCategory.BASE);
newProduct.initialize((StandaloneCatalog) mutableCatalog);
mutableCatalog.addProduct(newProduct);
final DefaultPlanPhase trialPhase = new DefaultPlanPhase();
trialPhase.setPhaseType(PhaseType.TRIAL);
trialPhase.setDuration(new DefaultDuration().setUnit(TimeUnit.DAYS).setNumber(14));
trialPhase.setFixed(new DefaultFixed().setFixedPrice(new DefaultInternationalPrice().setPrices(new DefaultPrice[] { new DefaultPrice().setCurrency(Currency.USD).setValue(BigDecimal.ZERO) })));
final DefaultPlanPhase evergreenPhase = new DefaultPlanPhase();
evergreenPhase.setPhaseType(PhaseType.EVERGREEN);
evergreenPhase.setDuration(new DefaultDuration().setUnit(TimeUnit.UNLIMITED));
evergreenPhase.setRecurring(new DefaultRecurring().setBillingPeriod(BillingPeriod.MONTHLY).setRecurringPrice(new DefaultInternationalPrice().setPrices(new DefaultPrice[] { new DefaultPrice().setCurrency(Currency.USD).setValue(BigDecimal.TEN) })));
final DefaultPlan newPlan = new DefaultPlan();
newPlan.setName("dynamic-monthly");
newPlan.setPriceListName(DefaultPriceListSet.DEFAULT_PRICELIST_NAME);
newPlan.setProduct(newProduct);
newPlan.setInitialPhases(new DefaultPlanPhase[] { trialPhase });
newPlan.setFinalPhase(evergreenPhase);
newPlan.setRecurringBillingMode(BillingMode.IN_ADVANCE);
newPlan.initialize((StandaloneCatalog) mutableCatalog);
mutableCatalog.addPlan(newPlan);
final String newCatalogStr = XMLWriter.writeXML((StandaloneCatalog) mutableCatalog, StandaloneCatalog.class);
final StandaloneCatalog newCatalog = XMLLoader.getObjectFromStream(new ByteArrayInputStream(newCatalogStr.getBytes(Charset.forName("UTF-8"))), StandaloneCatalog.class);
assertEquals(newCatalog.getPlans().size(), catalog.getPlans().size() + 1);
final Plan plan = newCatalog.findPlan("dynamic-monthly");
assertEquals(plan.getName(), "dynamic-monthly");
assertEquals(plan.getPriceList().getName(), DefaultPriceListSet.DEFAULT_PRICELIST_NAME);
assertEquals(plan.getProduct().getName(), "Dynamic");
assertEquals(plan.getProduct().getCategory(), ProductCategory.BASE);
assertEquals(plan.getInitialPhases().length, 1);
assertEquals(plan.getInitialPhases()[0].getName(), "dynamic-monthly-trial");
assertEquals(plan.getInitialPhases()[0].getPhaseType(), PhaseType.TRIAL);
assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices().length, 1);
assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices()[0].getCurrency(), Currency.USD);
assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices()[0].getValue(), BigDecimal.ZERO);
assertEquals(plan.getFinalPhase().getName(), "dynamic-monthly-evergreen");
assertEquals(plan.getFinalPhase().getPhaseType(), PhaseType.EVERGREEN);
assertEquals(plan.getFinalPhase().getRecurring().getBillingPeriod(), BillingPeriod.MONTHLY);
assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices().length, 1);
assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getCurrency(), Currency.USD);
assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getValue(), BigDecimal.TEN);
}
use of org.killbill.billing.catalog.DefaultPlanPhase 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();
if (curOverride != null) {
List<UsagePriceOverride> resolvedUsageOverrides = getResolvedUsageOverrides(curPhase.getUsages(), curOverride.getUsagePriceOverrides());
resolvedOverride[index++] = new DefaultPlanPhasePriceOverride(curPhase.getName(), curOverride.getCurrency(), curOverride.getFixedPrice(), curOverride.getRecurringPrice(), resolvedUsageOverrides);
} else {
resolvedOverride[index++] = 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, 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);
if (context == null) {
overriddenPlanCache.addDryRunPlan(planName, result);
}
return result;
}
Aggregations