Search in sources :

Example 21 with SubscriptionBaseBundle

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

the class DefaultInternalBillingApi method addBillingEventsForBundles.

private void addBillingEventsForBundles(final List<SubscriptionBaseBundle> bundles, final ImmutableAccountData account, final DryRunArguments dryRunArguments, final InternalCallContext context, final DefaultBillingEventSet result, final Set<UUID> skipSubscriptionsSet) throws AccountApiException, CatalogApiException, SubscriptionBaseApiException {
    final boolean dryRunMode = dryRunArguments != null;
    // want to tap into subscriptionBase logic, so we make up a bundleId
    if (dryRunArguments != null && dryRunArguments.getAction() == SubscriptionEventType.START_BILLING && dryRunArguments.getBundleId() == null) {
        final UUID fakeBundleId = UUIDs.randomUUID();
        final List<SubscriptionBase> subscriptions = subscriptionApi.getSubscriptionsForBundle(fakeBundleId, dryRunArguments, context);
        addBillingEventsForSubscription(account, subscriptions, null, dryRunMode, context, result, skipSubscriptionsSet);
    }
    for (final SubscriptionBaseBundle bundle : bundles) {
        final DryRunArguments dryRunArgumentsForBundle = (dryRunArguments != null && dryRunArguments.getBundleId() != null && dryRunArguments.getBundleId().equals(bundle.getId())) ? dryRunArguments : null;
        final List<SubscriptionBase> subscriptions = subscriptionApi.getSubscriptionsForBundle(bundle.getId(), dryRunArgumentsForBundle, context);
        //Check if billing is off for the bundle
        final List<Tag> bundleTags = tagApi.getTags(bundle.getId(), ObjectType.BUNDLE, context);
        boolean found_AUTO_INVOICING_OFF = is_AUTO_INVOICING_OFF(bundleTags);
        if (found_AUTO_INVOICING_OFF) {
            for (final SubscriptionBase subscription : subscriptions) {
                // billing is off so list sub ids in set to be excluded
                result.getSubscriptionIdsWithAutoInvoiceOff().add(subscription.getId());
            }
        } else {
            // billing is not off
            final SubscriptionBase baseSubscription = !subscriptions.isEmpty() ? subscriptions.get(0) : null;
            addBillingEventsForSubscription(account, subscriptions, baseSubscription, dryRunMode, context, result, skipSubscriptionsSet);
        }
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DryRunArguments(org.killbill.billing.invoice.api.DryRunArguments) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) Tag(org.killbill.billing.util.tag.Tag) UUID(java.util.UUID)

Example 22 with SubscriptionBaseBundle

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

the class TestBillingApi method beforeMethod.

@BeforeMethod(groups = "fast")
public void beforeMethod() throws Exception {
    super.beforeMethod();
    final SubscriptionBaseBundle bundle = Mockito.mock(SubscriptionBaseBundle.class);
    Mockito.when(bundle.getId()).thenReturn(bunId);
    final List<SubscriptionBaseBundle> bundles = ImmutableList.<SubscriptionBaseBundle>of(bundle);
    effectiveSubscriptionTransitions = new LinkedList<EffectiveSubscriptionInternalEvent>();
    final DateTime subscriptionStartDate = clock.getUTCNow().minusDays(3);
    subscription = new MockSubscription(subId, bunId, null, subscriptionStartDate, subscriptionStartDate);
    final List<SubscriptionBase> subscriptions = ImmutableList.<SubscriptionBase>of(subscription);
    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.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.getBillingTransitions(Mockito.<SubscriptionBase>any(), Mockito.<InternalTenantContext>any())).thenReturn(effectiveSubscriptionTransitions);
    Mockito.when(subscriptionInternalApi.getAllTransitions(Mockito.<SubscriptionBase>any(), Mockito.<InternalTenantContext>any())).thenReturn(effectiveSubscriptionTransitions);
    catalog = ((MockCatalog) catalogService.getCurrentCatalog(true, true, internalCallContext));
    // TODO The MockCatalog module returns two different things for full vs current catalog
    Mockito.when(catalogService.getFullCatalog(true, true, internalCallContext)).thenReturn(catalog);
    // Set a default alignment
    catalog.setBillingAlignment(BillingAlignment.ACCOUNT);
    // Cleanup mock daos
    ((MockBlockingStateDao) blockingStateDao).clear();
    ((MockTagDao) tagDao).clear();
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockCatalog(org.killbill.billing.catalog.MockCatalog) MockTagDao(org.killbill.billing.util.tag.dao.MockTagDao) EffectiveSubscriptionInternalEvent(org.killbill.billing.events.EffectiveSubscriptionInternalEvent) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) MockSubscription(org.killbill.billing.mock.MockSubscription) MockBlockingStateDao(org.killbill.billing.entitlement.dao.MockBlockingStateDao) DateTime(org.joda.time.DateTime) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 23 with SubscriptionBaseBundle

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

the class TestBillCycleDayCalculator method testCalculateBCDForAOWithBPCancelledBundleAligned.

@Test(groups = "fast")
public void testCalculateBCDForAOWithBPCancelledBundleAligned() throws Exception {
    final DateTimeZone accountTimeZone = DateTimeZone.UTC;
    final DateTime bpStartDateUTC = new DateTime(2012, 7, 16, 21, 0, 0, DateTimeZone.UTC);
    final int expectedBCDUTC = 16;
    // Create a Bundle associated with a subscription
    final SubscriptionBaseBundle bundle = Mockito.mock(SubscriptionBaseBundle.class);
    final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class);
    Mockito.when(subscription.getStartDate()).thenReturn(bpStartDateUTC);
    // Create a the base plan associated with that subscription
    final Plan plan = Mockito.mock(Plan.class);
    final Catalog catalog = Mockito.mock(Catalog.class);
    Mockito.when(catalog.findPlan(Mockito.anyString(), Mockito.<DateTime>any(), Mockito.<DateTime>any())).thenReturn(plan);
    Mockito.when(subscription.getLastActivePlan()).thenReturn(plan);
    Mockito.when(subscription.getDateOfFirstRecurringNonZeroCharge()).thenReturn(bpStartDateUTC);
    final ImmutableAccountData account = Mockito.mock(ImmutableAccountData.class);
    Mockito.when(account.getTimeZone()).thenReturn(accountTimeZone);
    final Integer billCycleDayLocal = BillCycleDayCalculator.calculateBcdForAlignment(new HashMap<UUID, Integer>(), subscription, subscription, BillingAlignment.BUNDLE, account.getTimeZone(), 0);
    Assert.assertEquals(billCycleDayLocal, (Integer) expectedBCDUTC);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) Plan(org.killbill.billing.catalog.api.Plan) UUID(java.util.UUID) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) Catalog(org.killbill.billing.catalog.api.Catalog) Test(org.testng.annotations.Test)

Example 24 with SubscriptionBaseBundle

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

the class TestTransfer method testTransferBPInTrialWithNoCTD.

@Test(groups = "slow")
public void testTransferBPInTrialWithNoCTD() throws Exception {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    final SubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    final DateTime evergreenPhaseDate = ((DefaultSubscriptionBase) baseSubscription).getPendingTransition().getEffectiveTransitionTime();
    // MOVE A LITTLE, STILL IN TRIAL
    clock.addDays(20);
    final DateTime beforeTransferDate = clock.getUTCNow();
    final DateTime transferRequestedDate = clock.getUTCNow();
    testListener.pushExpectedEvent(NextEvent.TRANSFER);
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    transferApi.transferBundle(bundle.getAccountId(), newAccountId, bundle.getExternalKey(), transferRequestedDate, false, false, callContext);
    assertListenerStatus();
    final DateTime afterTransferDate = clock.getUTCNow();
    // CHECK OLD BASE IS CANCEL AT THE TRANSFER DATE
    final SubscriptionBase oldBaseSubscription = subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    assertNotNull(oldBaseSubscription.getEndDate());
    testUtil.assertDateWithin(oldBaseSubscription.getEndDate(), beforeTransferDate, afterTransferDate);
    assertTrue(oldBaseSubscription.getEndDate().compareTo(transferRequestedDate) == 0);
    // CHECK NEW BUNDLE EXIST, WITH ONE SUBSCRIPTION STARTING ON TRANSFER_DATE
    final List<SubscriptionBaseBundle> bundlesForAccountAndKey = subscriptionInternalApi.getBundlesForAccountAndKey(newAccountId, bundle.getExternalKey(), internalCallContext);
    assertEquals(bundlesForAccountAndKey.size(), 1);
    final SubscriptionBaseBundle newBundle = bundlesForAccountAndKey.get(0);
    final List<SubscriptionBase> subscriptions = subscriptionInternalApi.getSubscriptionsForBundle(newBundle.getId(), null, internalCallContext);
    assertEquals(subscriptions.size(), 1);
    final SubscriptionBase newBaseSubscription = subscriptions.get(0);
    assertTrue(((DefaultSubscriptionBase) newBaseSubscription).getAlignStartDate().compareTo(((DefaultSubscriptionBase) oldBaseSubscription).getAlignStartDate()) == 0);
    // CHECK NEXT PENDING PHASE IS ALIGNED WITH OLD SUBSCRIPTION START DATE
    assertEquals(subscriptionInternalApi.getAllTransitions(newBaseSubscription, internalCallContext).size(), 2);
    assertTrue(subscriptionInternalApi.getAllTransitions(newBaseSubscription, internalCallContext).get(1).getEffectiveTransitionTime().compareTo(evergreenPhaseDate) == 0);
    final Plan newPlan = newBaseSubscription.getCurrentPlan();
    assertEquals(newPlan.getProduct().getName(), baseProduct);
    assertEquals(newBaseSubscription.getCurrentPhase().getPhaseType(), PhaseType.TRIAL);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 25 with SubscriptionBaseBundle

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

the class TestTransfer method testTransferBPNoTrialWithCTD.

@Test(groups = "slow")
public void testTransferBPNoTrialWithCTD() throws Exception {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    final SubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    // MOVE AFTER TRIAL
    testListener.pushExpectedEvent(NextEvent.PHASE);
    clock.addDays(40);
    assertListenerStatus();
    // SET CTD
    final DateTime ctd = baseSubscription.getStartDate().plusDays(30).plusMonths(1);
    subscriptionInternalApi.setChargedThroughDate(baseSubscription.getId(), ctd, internalCallContext);
    final DateTime transferRequestedDate = clock.getUTCNow();
    testListener.pushExpectedEvent(NextEvent.TRANSFER);
    transferApi.transferBundle(bundle.getAccountId(), newAccountId, bundle.getExternalKey(), transferRequestedDate, false, false, callContext);
    assertListenerStatus();
    // CHECK OLD BASE IS CANCEL AT THE TRANSFER DATE
    final SubscriptionBase oldBaseSubscription = subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
    assertNotNull(oldBaseSubscription.getFutureEndDate());
    assertTrue(oldBaseSubscription.getFutureEndDate().compareTo(ctd) == 0);
    // CHECK NEW BUNDLE EXIST, WITH ONE SUBSCRIPTION STARTING ON TRANSFER_DATE
    final List<SubscriptionBaseBundle> bundlesForAccountAndKey = subscriptionInternalApi.getBundlesForAccountAndKey(newAccountId, bundle.getExternalKey(), internalCallContext);
    assertEquals(bundlesForAccountAndKey.size(), 1);
    final SubscriptionBaseBundle newBundle = bundlesForAccountAndKey.get(0);
    final List<SubscriptionBase> subscriptions = subscriptionInternalApi.getSubscriptionsForBundle(newBundle.getId(), null, internalCallContext);
    assertEquals(subscriptions.size(), 1);
    final SubscriptionBase newBaseSubscription = subscriptions.get(0);
    assertTrue(((DefaultSubscriptionBase) newBaseSubscription).getAlignStartDate().compareTo(((DefaultSubscriptionBase) baseSubscription).getAlignStartDate()) == 0);
    // CHECK ONLY ONE PHASE EXISTS
    assertEquals(subscriptionInternalApi.getAllTransitions(newBaseSubscription, internalCallContext).size(), 1);
    Plan newPlan = newBaseSubscription.getCurrentPlan();
    assertEquals(newPlan.getProduct().getName(), baseProduct);
    assertEquals(newBaseSubscription.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN);
    // MAKE A PLAN CHANGE IMM
    clock.addDays(5);
    final String newBaseProduct1 = "Assault-Rifle";
    final BillingPeriod newBaseTerm1 = BillingPeriod.ANNUAL;
    testListener.pushExpectedEvent(NextEvent.CHANGE);
    newBaseSubscription.changePlan(new PlanSpecifier(newBaseProduct1, newBaseTerm1, basePriceList), null, callContext);
    assertListenerStatus();
    newPlan = newBaseSubscription.getCurrentPlan();
    assertEquals(newPlan.getProduct().getName(), newBaseProduct1);
    assertEquals(newBaseSubscription.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN);
    // SET CTD AND MAKE CHANGE EOT
    clock.addDays(2);
    final DateTime newCtd = newBaseSubscription.getStartDate().plusYears(1);
    subscriptionInternalApi.setChargedThroughDate(newBaseSubscription.getId(), newCtd, internalCallContext);
    final SubscriptionBase newBaseSubscriptionWithCtd = subscriptionInternalApi.getSubscriptionFromId(newBaseSubscription.getId(), internalCallContext);
    final String newBaseProduct2 = "Pistol";
    final BillingPeriod newBaseTerm2 = BillingPeriod.ANNUAL;
    newBaseSubscriptionWithCtd.changePlan(new PlanSpecifier(newBaseProduct2, newBaseTerm2, basePriceList), null, callContext);
    newPlan = newBaseSubscriptionWithCtd.getCurrentPlan();
    assertEquals(newPlan.getProduct().getName(), newBaseProduct1);
    assertEquals(newBaseSubscriptionWithCtd.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN);
    assertNotNull(newBaseSubscriptionWithCtd.getPendingTransition());
    assertEquals(newBaseSubscriptionWithCtd.getPendingTransition().getEffectiveTransitionTime(), newCtd);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) PlanSpecifier(org.killbill.billing.catalog.api.PlanSpecifier) Test(org.testng.annotations.Test)

Aggregations

SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)26 DateTime (org.joda.time.DateTime)17 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)16 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)10 UUID (java.util.UUID)9 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)9 ArrayList (java.util.ArrayList)8 Test (org.testng.annotations.Test)8 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)7 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)7 Plan (org.killbill.billing.catalog.api.Plan)7 ImmutableAccountData (org.killbill.billing.account.api.ImmutableAccountData)6 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)6 LinkedList (java.util.LinkedList)4 AccountApiException (org.killbill.billing.account.api.AccountApiException)4 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)4 WithEntitlementPlugin (org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin)4 EntitlementContext (org.killbill.billing.entitlement.plugin.api.EntitlementContext)4 DefaultSubscriptionBaseBundle (org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseBundle)4 ImmutableList (com.google.common.collect.ImmutableList)3