Search in sources :

Example 1 with SubscriptionBillingEvent

use of org.killbill.billing.subscription.api.user.SubscriptionBillingEvent in project killbill by killbill.

the class DefaultInternalBillingApi method addBillingEventsForSubscription.

private void addBillingEventsForSubscription(final ImmutableAccountData account, @Nullable final List<SubscriptionBase> subscriptions, final SubscriptionBase baseSubscription, final int currentAccountBCD, final InternalCallContext context, final DefaultBillingEventSet result, final Set<UUID> skipSubscriptionsSet, final VersionedCatalog catalog) throws SubscriptionBaseApiException, CatalogApiException {
    if (subscriptions == null) {
        return;
    }
    final Map<UUID, Integer> bcdCache = new HashMap<UUID, Integer>();
    for (final SubscriptionBase subscription : subscriptions) {
        // TODO Can we batch those ?
        final List<SubscriptionBillingEvent> billingTransitions = subscriptionApi.getSubscriptionBillingEvents(catalog, subscription, context);
        if (billingTransitions.isEmpty() || (billingTransitions.get(0).getType() != SubscriptionBaseTransitionType.CREATE && billingTransitions.get(0).getType() != SubscriptionBaseTransitionType.TRANSFER)) {
            log.warn("Skipping billing events for subscription " + subscription.getId() + ": Does not start with a valid CREATE transition");
            skipSubscriptionsSet.add(subscription.getId());
            return;
        }
        Integer overridenBCD = null;
        int bcdLocal = 0;
        BillingAlignment alignment = null;
        for (final SubscriptionBillingEvent transition : billingTransitions) {
            if (transition.getType() != SubscriptionBaseTransitionType.CANCEL) {
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier(transition.getPlan().getName(), transition.getPlanPhase().getPhaseType());
                alignment = subscription.getBillingAlignment(spec, transition.getEffectiveDate(), catalog);
                // 
                // A BCD_CHANGE transition defines a new billCycleDayLocal for the subscription and this overrides whatever computation
                // occurs below (which is based on billing alignment policy). Also multiple of those BCD_CHANGE transitions could occur,
                // to define different intervals with different billing cycle days.
                // 
                overridenBCD = transition.getBcdLocal() != null ? transition.getBcdLocal() : overridenBCD;
                bcdLocal = overridenBCD != null ? overridenBCD : calculateBcdForTransition(alignment, bcdCache, baseSubscription, subscription, currentAccountBCD, context);
            }
            final BillingEvent event = new DefaultBillingEvent(transition, subscription, bcdLocal, alignment, account.getCurrency());
            result.add(event);
        }
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) HashMap(java.util.HashMap) BillingAlignment(org.killbill.billing.catalog.api.BillingAlignment) SubscriptionBillingEvent(org.killbill.billing.subscription.api.user.SubscriptionBillingEvent) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) SubscriptionBillingEvent(org.killbill.billing.subscription.api.user.SubscriptionBillingEvent)

Example 2 with SubscriptionBillingEvent

use of org.killbill.billing.subscription.api.user.SubscriptionBillingEvent in project killbill by killbill.

the class TestBillingApi method beforeMethod.

@BeforeMethod(groups = "fast")
public void beforeMethod() throws Exception {
    if (hasFailed()) {
        return;
    }
    super.beforeMethod();
    final SubscriptionBaseBundle bundle = Mockito.mock(SubscriptionBaseBundle.class);
    Mockito.when(bundle.getId()).thenReturn(bunId);
    final List<SubscriptionBaseBundle> bundles = ImmutableList.<SubscriptionBaseBundle>of(bundle);
    billingTransitions = new LinkedList<SubscriptionBillingEvent>();
    effectiveSubscriptionTransitions = new LinkedList<EffectiveSubscriptionInternalEvent>();
    final DateTime subscriptionStartDate = clock.getUTCNow().minusDays(3);
    subscription = new MockSubscription(subId, bunId, null, null, subscriptionStartDate, subscriptionStartDate);
    final List<SubscriptionBase> subscriptions = ImmutableList.<SubscriptionBase>of(subscription);
    // Mockito.when(subscription.getBillingAlignment(Mockito.<PlanPhaseSpecifier>any(), Mockito.<DateTime>any(), Mockito.<Catalog>any())).thenReturn(BillingAlignment.ACCOUNT);
    Mockito.when(subscriptionInternalApi.getBundlesForAccount(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(bundles);
    Mockito.when(subscriptionInternalApi.getSubscriptionsForBundle(Mockito.<UUID>any(), Mockito.<DryRunArguments>any(), Mockito.<InternalTenantContext>any())).thenReturn(subscriptions);
    Mockito.when(subscriptionInternalApi.getSubscriptionsForAccount(Mockito.<VersionedCatalog>any(), Mockito.<LocalDate>any(), Mockito.<InternalTenantContext>any())).thenReturn(ImmutableMap.<UUID, List<SubscriptionBase>>builder().put(bunId, subscriptions).build());
    Mockito.when(subscriptionInternalApi.getSubscriptionFromId(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(subscription);
    Mockito.when(subscriptionInternalApi.getBundleFromId(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(bundle);
    Mockito.when(subscriptionInternalApi.getBaseSubscription(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(subscription);
    Mockito.when(subscriptionInternalApi.getSubscriptionBillingEvents(Mockito.<VersionedCatalog>any(), Mockito.<SubscriptionBase>any(), Mockito.<InternalTenantContext>any())).thenReturn(billingTransitions);
    Mockito.when(subscriptionInternalApi.getAllTransitions(Mockito.<SubscriptionBase>any(), Mockito.<InternalTenantContext>any())).thenReturn(effectiveSubscriptionTransitions);
    final VersionedCatalog versionedCatalog = catalogService.getFullCatalog(true, true, internalCallContext);
    catalog = (MockCatalog) Iterables.getLast(versionedCatalog.getVersions());
    Mockito.when(catalogService.getFullCatalog(true, true, internalCallContext)).thenReturn(versionedCatalog);
    // Set a default alignment
    ((MockSubscription) subscription).setBillingAlignment(BillingAlignment.ACCOUNT);
    // Cleanup mock daos
    ((MockBlockingStateDao) blockingStateDao).clear();
    ((MockTagDao) tagDao).clear();
}
Also used : EffectiveSubscriptionInternalEvent(org.killbill.billing.events.EffectiveSubscriptionInternalEvent) MockSubscription(org.killbill.billing.mock.MockSubscription) MockBlockingStateDao(org.killbill.billing.entitlement.dao.MockBlockingStateDao) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) MockTagDao(org.killbill.billing.util.tag.dao.MockTagDao) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) SubscriptionBillingEvent(org.killbill.billing.subscription.api.user.SubscriptionBillingEvent) DefaultSubscriptionBillingEvent(org.killbill.billing.subscription.api.user.DefaultSubscriptionBillingEvent) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)2 SubscriptionBillingEvent (org.killbill.billing.subscription.api.user.SubscriptionBillingEvent)2 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 DateTime (org.joda.time.DateTime)1 BillingAlignment (org.killbill.billing.catalog.api.BillingAlignment)1 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)1 VersionedCatalog (org.killbill.billing.catalog.api.VersionedCatalog)1 MockBlockingStateDao (org.killbill.billing.entitlement.dao.MockBlockingStateDao)1 EffectiveSubscriptionInternalEvent (org.killbill.billing.events.EffectiveSubscriptionInternalEvent)1 BillingEvent (org.killbill.billing.junction.BillingEvent)1 MockSubscription (org.killbill.billing.mock.MockSubscription)1 DefaultSubscriptionBillingEvent (org.killbill.billing.subscription.api.user.DefaultSubscriptionBillingEvent)1 SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)1 MockTagDao (org.killbill.billing.util.tag.dao.MockTagDao)1 BeforeMethod (org.testng.annotations.BeforeMethod)1