use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class TestUsageInArrearBase method createMockBillingEvent.
protected BillingEvent createMockBillingEvent(final DateTime effectiveDate, final BillingPeriod billingPeriod, final List<Usage> usages) {
final BillingEvent result = Mockito.mock(BillingEvent.class);
Mockito.when(result.getCurrency()).thenReturn(Currency.BTC);
Mockito.when(result.getBillCycleDayLocal()).thenReturn(BCD);
Mockito.when(result.getTimeZone()).thenReturn(DateTimeZone.UTC);
Mockito.when(result.getEffectiveDate()).thenReturn(effectiveDate);
Mockito.when(result.getBillingPeriod()).thenReturn(billingPeriod);
final Account account = Mockito.mock(Account.class);
Mockito.when(account.getId()).thenReturn(accountId);
final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class);
Mockito.when(subscription.getId()).thenReturn(subscriptionId);
Mockito.when(subscription.getBundleId()).thenReturn(bundleId);
Mockito.when(result.getSubscription()).thenReturn(subscription);
final Plan plan = Mockito.mock(Plan.class);
Mockito.when(plan.getName()).thenReturn(planName);
Mockito.when(result.getPlan()).thenReturn(plan);
final PlanPhase phase = Mockito.mock(PlanPhase.class);
Mockito.when(phase.getName()).thenReturn(phaseName);
Mockito.when(result.getPlanPhase()).thenReturn(phase);
Mockito.when(result.getUsages()).thenReturn(usages);
return result;
}
use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class DefaultSubscriptionBaseTransferApi method createEvent.
private SubscriptionBaseEvent createEvent(final boolean firstEvent, final ExistingEvent existingEvent, final DefaultSubscriptionBase subscription, final DateTime transferDate, final InternalTenantContext context) throws CatalogApiException {
SubscriptionBaseEvent newEvent = null;
final Catalog catalog = catalogService.getFullCatalog(true, true, context);
final DateTime effectiveDate = existingEvent.getEffectiveDate().isBefore(transferDate) ? transferDate : existingEvent.getEffectiveDate();
final PlanPhaseSpecifier spec = existingEvent.getPlanPhaseSpecifier();
final PlanPhase currentPhase = existingEvent.getPlanPhaseName() != null ? catalog.findPhase(existingEvent.getPlanPhaseName(), effectiveDate, subscription.getAlignStartDate()) : null;
if (spec == null || currentPhase == null) {
// Ignore cancellations - we assume that transferred subscriptions should always be active
return null;
}
final ApiEventBuilder apiBuilder = new ApiEventBuilder().setSubscriptionId(subscription.getId()).setEventPlan(existingEvent.getPlanName()).setEventPlanPhase(currentPhase.getName()).setEventPriceList(spec.getPriceListName()).setEffectiveDate(effectiveDate).setFromDisk(true);
switch(existingEvent.getSubscriptionTransitionType()) {
case TRANSFER:
case CREATE:
newEvent = new ApiEventTransfer(apiBuilder);
break;
// Should we even keep future change events; product question really
case CHANGE:
newEvent = firstEvent ? new ApiEventTransfer(apiBuilder) : new ApiEventChange(apiBuilder);
break;
case PHASE:
newEvent = firstEvent ? new ApiEventTransfer(apiBuilder) : PhaseEventData.createNextPhaseEvent(subscription.getId(), currentPhase.getName(), effectiveDate);
break;
case CANCEL:
break;
default:
throw new SubscriptionBaseError(String.format("Unexpected transitionType %s", existingEvent.getSubscriptionTransitionType()));
}
return newEvent;
}
use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class DefaultSubscriptionBase method getDateOfFirstRecurringNonZeroCharge.
@Override
public DateTime getDateOfFirstRecurringNonZeroCharge() {
final Plan initialPlan = !transitions.isEmpty() ? transitions.get(0).getNextPlan() : null;
final PlanPhase initialPhase = !transitions.isEmpty() ? transitions.get(0).getNextPhase() : null;
final PhaseType initialPhaseType = initialPhase != null ? initialPhase.getPhaseType() : null;
return initialPlan.dateOfFirstRecurringNonZeroCharge(getStartDate(), initialPhaseType);
}
use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class DefaultSubscriptionBase method rebuildTransitions.
public void rebuildTransitions(final List<SubscriptionBaseEvent> inputEvents, final Catalog catalog) throws CatalogApiException {
if (inputEvents == null) {
return;
}
this.events = inputEvents;
filterOutDuplicateCancelEvents(events);
UUID nextUserToken = null;
UUID nextEventId = null;
DateTime nextCreatedDate = null;
EntitlementState nextState = null;
String nextPlanName = null;
String nextPhaseName = null;
Integer nextBillingCycleDayLocal = null;
UUID prevEventId = null;
DateTime prevCreatedDate = null;
EntitlementState previousState = null;
PriceList previousPriceList = null;
Plan previousPlan = null;
PlanPhase previousPhase = null;
Integer previousBillingCycleDayLocal = null;
transitions = new LinkedList<SubscriptionBaseTransition>();
for (final SubscriptionBaseEvent cur : inputEvents) {
if (!cur.isActive()) {
continue;
}
ApiEventType apiEventType = null;
boolean isFromDisk = true;
nextEventId = cur.getId();
nextCreatedDate = cur.getCreatedDate();
switch(cur.getType()) {
case PHASE:
final PhaseEvent phaseEV = (PhaseEvent) cur;
nextPhaseName = phaseEV.getPhase();
break;
case BCD_UPDATE:
final BCDEvent bcdEvent = (BCDEvent) cur;
nextBillingCycleDayLocal = bcdEvent.getBillCycleDayLocal();
break;
case API_USER:
final ApiEvent userEV = (ApiEvent) cur;
apiEventType = userEV.getApiEventType();
isFromDisk = userEV.isFromDisk();
switch(apiEventType) {
case TRANSFER:
case CREATE:
prevEventId = null;
prevCreatedDate = null;
previousState = null;
previousPlan = null;
previousPhase = null;
previousPriceList = null;
nextState = EntitlementState.ACTIVE;
nextPlanName = userEV.getEventPlan();
nextPhaseName = userEV.getEventPlanPhase();
break;
case CHANGE:
nextPlanName = userEV.getEventPlan();
nextPhaseName = userEV.getEventPlanPhase();
break;
case CANCEL:
nextState = EntitlementState.CANCELLED;
nextPlanName = null;
nextPhaseName = null;
break;
case UNCANCEL:
default:
throw new SubscriptionBaseError(String.format("Unexpected UserEvent type = %s", userEV.getApiEventType().toString()));
}
break;
default:
throw new SubscriptionBaseError(String.format("Unexpected Event type = %s", cur.getType()));
}
Plan nextPlan = null;
PlanPhase nextPhase = null;
PriceList nextPriceList = null;
nextPlan = (nextPlanName != null) ? catalog.findPlan(nextPlanName, cur.getEffectiveDate(), getAlignStartDate()) : null;
nextPhase = (nextPhaseName != null) ? catalog.findPhase(nextPhaseName, cur.getEffectiveDate(), getAlignStartDate()) : null;
nextPriceList = (nextPlan != null) ? catalog.findPriceListForPlan(nextPlanName, cur.getEffectiveDate(), getAlignStartDate()) : null;
final SubscriptionBaseTransitionData transition = new SubscriptionBaseTransitionData(cur.getId(), id, bundleId, bundleExternalKey, cur.getType(), apiEventType, cur.getEffectiveDate(), prevEventId, prevCreatedDate, previousState, previousPlan, previousPhase, previousPriceList, previousBillingCycleDayLocal, nextEventId, nextCreatedDate, nextState, nextPlan, nextPhase, nextPriceList, nextBillingCycleDayLocal, cur.getTotalOrdering(), cur.getCreatedDate(), nextUserToken, isFromDisk);
transitions.add(transition);
previousState = nextState;
previousPlan = nextPlan;
previousPhase = nextPhase;
previousPriceList = nextPriceList;
prevEventId = nextEventId;
prevCreatedDate = nextCreatedDate;
previousBillingCycleDayLocal = nextBillingCycleDayLocal;
}
}
use of org.killbill.billing.catalog.api.PlanPhase in project killbill by killbill.
the class TestUserApiAddOn method testAddonCreateInternal.
private void testAddonCreateInternal(final String aoProduct, final BillingPeriod aoTerm, final String aoPriceList, final PlanAlignmentCreate expAlignement) throws SubscriptionBaseApiException {
final String baseProduct = "Shotgun";
final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
// CREATE BP
final DefaultSubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
// MOVE CLOCK 14 DAYS LATER
Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(14));
clock.addDeltaFromReality(it.toDurationMillis());
// CREATE ADDON
final DateTime beforeAOCreation = clock.getUTCNow();
DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
final DateTime afterAOCreation = clock.getUTCNow();
// CHECK EVERYTHING
Plan aoCurrentPlan = aoSubscription.getCurrentPlan();
assertNotNull(aoCurrentPlan);
assertEquals(aoCurrentPlan.getProduct().getName(), aoProduct);
assertEquals(aoCurrentPlan.getProduct().getCategory(), ProductCategory.ADD_ON);
assertEquals(aoCurrentPlan.getRecurringBillingPeriod(), aoTerm);
PlanPhase aoCurrentPhase = aoSubscription.getCurrentPhase();
assertNotNull(aoCurrentPhase);
assertEquals(aoCurrentPhase.getPhaseType(), PhaseType.DISCOUNT);
testUtil.assertDateWithin(aoSubscription.getStartDate(), beforeAOCreation, afterAOCreation);
assertEquals(aoSubscription.getBundleStartDate(), baseSubscription.getBundleStartDate());
// CHECK next AO PHASE EVENT IS INDEED A MONTH AFTER BP STARTED => BUNDLE ALIGNMENT
SubscriptionBaseTransition aoPendingTranstion = aoSubscription.getPendingTransition();
if (expAlignement == PlanAlignmentCreate.START_OF_BUNDLE) {
assertEquals(aoPendingTranstion.getEffectiveTransitionTime(), baseSubscription.getStartDate().plusMonths(1));
} else {
assertEquals(aoPendingTranstion.getEffectiveTransitionTime(), aoSubscription.getStartDate().plusMonths(1));
}
// ADD TWO PHASE EVENTS (BP + AO)
testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE);
// MOVE THROUGH TIME TO GO INTO EVERGREEN
it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(33));
clock.addDeltaFromReality(it.toDurationMillis());
assertListenerStatus();
// CHECK EVERYTHING AGAIN
aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
aoCurrentPlan = aoSubscription.getCurrentPlan();
assertNotNull(aoCurrentPlan);
assertEquals(aoCurrentPlan.getProduct().getName(), aoProduct);
assertEquals(aoCurrentPlan.getProduct().getCategory(), ProductCategory.ADD_ON);
assertEquals(aoCurrentPlan.getRecurringBillingPeriod(), aoTerm);
aoCurrentPhase = aoSubscription.getCurrentPhase();
assertNotNull(aoCurrentPhase);
assertEquals(aoCurrentPhase.getPhaseType(), PhaseType.EVERGREEN);
aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
aoPendingTranstion = aoSubscription.getPendingTransition();
assertNull(aoPendingTranstion);
assertListenerStatus();
}
Aggregations