Search in sources :

Example 61 with BillingEvent

use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.

the class BlockingCalculator method eventsToRemove.

protected SortedSet<BillingEvent> eventsToRemove(final List<DisabledDuration> disabledDuration, final SortedSet<BillingEvent> billingEvents, final SubscriptionBase subscription) {
    final SortedSet<BillingEvent> result = new TreeSet<BillingEvent>();
    final SortedSet<BillingEvent> filteredBillingEvents = filter(billingEvents, subscription);
    for (final DisabledDuration duration : disabledDuration) {
        for (final BillingEvent event : filteredBillingEvents) {
            if (duration.getEnd() == null || event.getEffectiveDate().isBefore(duration.getEnd())) {
                if (event.getEffectiveDate().isAfter(duration.getStart())) {
                    //between the pair
                    result.add(event);
                }
            } else {
                //after the last event of the pair no need to keep checking
                break;
            }
        }
    }
    return result;
}
Also used : TreeSet(java.util.TreeSet) BillingEvent(org.killbill.billing.junction.BillingEvent)

Example 62 with BillingEvent

use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.

the class BlockingCalculator method insertBlockingEvents.

/**
     * Given a set of billing events, add corresponding blocking (overdue) billing events.
     *
     * @param billingEvents the original list of billing events to update (without overdue events)
     */
public boolean insertBlockingEvents(final SortedSet<BillingEvent> billingEvents, final Set<UUID> skippedSubscriptions, final InternalTenantContext context) throws CatalogApiException {
    if (billingEvents.size() <= 0) {
        return false;
    }
    final Hashtable<UUID, List<SubscriptionBase>> bundleMap = createBundleSubscriptionMap(billingEvents);
    final SortedSet<BillingEvent> billingEventsToAdd = new TreeSet<BillingEvent>();
    final SortedSet<BillingEvent> billingEventsToRemove = new TreeSet<BillingEvent>();
    final List<BlockingState> blockingEvents = blockingApi.getBlockingAllForAccount(context);
    final Iterable<BlockingState> accountBlockingEvents = Iterables.filter(blockingEvents, new Predicate<BlockingState>() {

        @Override
        public boolean apply(final BlockingState input) {
            return BlockingStateType.ACCOUNT == input.getType();
        }
    });
    final Map<UUID, List<BlockingState>> perBundleBlockingEvents = getPerTypeBlockingEvents(BlockingStateType.SUBSCRIPTION_BUNDLE, blockingEvents);
    final Map<UUID, List<BlockingState>> perSubscriptionBlockingEvents = getPerTypeBlockingEvents(BlockingStateType.SUBSCRIPTION, blockingEvents);
    for (final UUID bundleId : bundleMap.keySet()) {
        final List<BlockingState> bundleBlockingEvents = perBundleBlockingEvents.get(bundleId) != null ? perBundleBlockingEvents.get(bundleId) : ImmutableList.<BlockingState>of();
        for (final SubscriptionBase subscription : bundleMap.get(bundleId)) {
            // Avoid inserting additional events for subscriptions that don't even have a START event
            if (skippedSubscriptions.contains(subscription.getId())) {
                continue;
            }
            final List<BlockingState> subscriptionBlockingEvents = perSubscriptionBlockingEvents.get(subscription.getId()) != null ? perSubscriptionBlockingEvents.get(subscription.getId()) : ImmutableList.<BlockingState>of();
            final List<BlockingState> aggregateSubscriptionBlockingEvents = getAggregateBlockingEventsPerSubscription(subscription.getEndDate(), subscriptionBlockingEvents, bundleBlockingEvents, accountBlockingEvents);
            final List<DisabledDuration> accountBlockingDurations = createBlockingDurations(aggregateSubscriptionBlockingEvents);
            billingEventsToAdd.addAll(createNewEvents(accountBlockingDurations, billingEvents, subscription, context));
            billingEventsToRemove.addAll(eventsToRemove(accountBlockingDurations, billingEvents, subscription));
        }
    }
    for (final BillingEvent eventToAdd : billingEventsToAdd) {
        billingEvents.add(eventToAdd);
    }
    for (final BillingEvent eventToRemove : billingEventsToRemove) {
        billingEvents.remove(eventToRemove);
    }
    return !(billingEventsToAdd.isEmpty() && billingEventsToRemove.isEmpty());
}
Also used : BlockingState(org.killbill.billing.entitlement.api.BlockingState) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID)

Example 63 with BillingEvent

use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.

the class DefaultBillingEventSet method getUsages.

@Override
public Map<String, Usage> getUsages() {
    final Iterable<Usage> allUsages = Iterables.concat(Iterables.transform(this, new Function<BillingEvent, List<Usage>>() {

        @Override
        public List<Usage> apply(final BillingEvent input) {
            return input.getUsages();
        }
    }));
    if (!allUsages.iterator().hasNext()) {
        return Collections.emptyMap();
    }
    final Map<String, Usage> result = new HashMap<String, Usage>();
    for (Usage cur : Sets.<Usage>newHashSet(allUsages)) {
        result.put(cur.getName(), cur);
    }
    return result;
}
Also used : Function(com.google.common.base.Function) Usage(org.killbill.billing.catalog.api.Usage) HashMap(java.util.HashMap) BillingEvent(org.killbill.billing.junction.BillingEvent)

Example 64 with BillingEvent

use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.

the class TestDefaultBillingEvent method testEventOrderingMix.

@Test(groups = "fast")
public void testEventOrderingMix() {
    final BillingEvent event0 = createEvent(subscription(ID_ZERO), new DateTime("2012-01-01T00:02:04.000Z"), SubscriptionBaseTransitionType.CREATE);
    final BillingEvent event1 = createEvent(subscription(ID_ZERO), new DateTime("2012-01-02T00:02:04.000Z"), SubscriptionBaseTransitionType.CHANGE);
    final BillingEvent event2 = createEvent(subscription(ID_ONE), new DateTime("2012-01-01T00:02:04.000Z"), SubscriptionBaseTransitionType.CANCEL);
    final SortedSet<BillingEvent> set = new TreeSet<BillingEvent>();
    set.add(event2);
    set.add(event1);
    set.add(event0);
    final Iterator<BillingEvent> it = set.iterator();
    Assert.assertEquals(event0, it.next());
    Assert.assertEquals(event1, it.next());
    Assert.assertEquals(event2, it.next());
}
Also used : TreeSet(java.util.TreeSet) BillingEvent(org.killbill.billing.junction.BillingEvent) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 65 with BillingEvent

use of org.killbill.billing.junction.BillingEvent in project killbill by killbill.

the class TestDefaultBillingEvent method testEventOrderingSubscription.

@Test(groups = "fast")
public void testEventOrderingSubscription() {
    final BillingEvent event0 = createEvent(subscription(ID_ZERO), new DateTime("2012-01-31T00:02:04.000Z"), SubscriptionBaseTransitionType.CREATE);
    final BillingEvent event1 = createEvent(subscription(ID_ONE), new DateTime("2012-01-31T00:02:04.000Z"), SubscriptionBaseTransitionType.CREATE);
    final BillingEvent event2 = createEvent(subscription(ID_TWO), new DateTime("2012-01-31T00:02:04.000Z"), SubscriptionBaseTransitionType.CREATE);
    final SortedSet<BillingEvent> set = new TreeSet<BillingEvent>();
    set.add(event2);
    set.add(event1);
    set.add(event0);
    final Iterator<BillingEvent> it = set.iterator();
    Assert.assertEquals(event0, it.next());
    Assert.assertEquals(event1, it.next());
    Assert.assertEquals(event2, it.next());
}
Also used : TreeSet(java.util.TreeSet) BillingEvent(org.killbill.billing.junction.BillingEvent) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

BillingEvent (org.killbill.billing.junction.BillingEvent)98 Test (org.testng.annotations.Test)82 DateTime (org.joda.time.DateTime)52 LocalDate (org.joda.time.LocalDate)49 Plan (org.killbill.billing.catalog.api.Plan)42 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)41 MockPlan (org.killbill.billing.catalog.MockPlan)40 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)40 BigDecimal (java.math.BigDecimal)36 ArrayList (java.util.ArrayList)36 UUID (java.util.UUID)35 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)35 BillingEventSet (org.killbill.billing.junction.BillingEventSet)33 Invoice (org.killbill.billing.invoice.api.Invoice)32 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)32 TreeSet (java.util.TreeSet)30 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)27 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)26 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)25 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)25