@Override public UUID apply(final BillingEvent input) { return input.getSubscription().getId(); } });
@Override public boolean apply(final BillingEvent input) { return input.getSubscription().getBundleId().equals(dryRunArguments.getBundleId()); } }), new Function<BillingEvent, UUID>() {
protected Hashtable<UUID, List<SubscriptionBase>> createBundleSubscriptionMap(final SortedSet<BillingEvent> billingEvents) { final Hashtable<UUID, List<SubscriptionBase>> result = new Hashtable<UUID, List<SubscriptionBase>>(); for (final BillingEvent event : billingEvents) { final UUID bundleId = event.getSubscription().getBundleId(); List<SubscriptionBase> subs = result.get(bundleId); if (subs == null) { subs = new ArrayList<SubscriptionBase>(); result.put(bundleId, subs); } if (!result.get(bundleId).contains(event.getSubscription())) { subs.add(event.getSubscription()); } } return result; }
@Override public UUID apply(final BillingEvent billingEvent) { return billingEvent.getSubscription().getId(); } }));
public UUID getBundleId() { return billingEvents.get(0).getSubscription().getBundleId(); }
public UUID getSubscriptionId() { return billingEvents.get(0).getSubscription().getId(); }
protected SortedSet<BillingEvent> filter(final SortedSet<BillingEvent> billingEvents, final SubscriptionBase subscription) { final SortedSet<BillingEvent> result = new TreeSet<BillingEvent>(); for (final BillingEvent event : billingEvents) { if (event.getSubscription().getId().equals(subscription.getId())) { result.add(event); } } return result; }
@Override public boolean apply(final RawUsage input) { return input.getSubscriptionId().equals(subscriptionBillingEvents.get(0).getSubscription().getId()); } }));
private Map<UUID, DateTime> getNextTransitionsForSubscriptions(final BillingEventSet billingEvents) { final DateTime now = clock.getUTCNow(); final Map<UUID, DateTime> result = new HashMap<UUID, DateTime>(); for (final BillingEvent evt : billingEvents) { final UUID subscriptionId = evt.getSubscription().getId(); final DateTime evtEffectiveDate = evt.getEffectiveDate(); if (evtEffectiveDate.compareTo(now) <= 0) { continue; } final DateTime nextUpcomingPerSubscriptionDate = result.get(subscriptionId); if (nextUpcomingPerSubscriptionDate == null || nextUpcomingPerSubscriptionDate.compareTo(evtEffectiveDate) > 0) { result.put(subscriptionId, evtEffectiveDate); } } return result; }
private void processRecurringBillingEvents(final UUID invoiceId, final UUID accountId, final BillingEventSet events, final LocalDate targetDate, final Currency currency, final List<InvoiceItem> proposedItems, final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDate, final InternalCallContext internalCallContext) throws InvoiceApiException { if (events.isEmpty()) { return; } // Pretty-print the generated invoice items from the junction events final InvoiceItemGeneratorLogger invoiceItemGeneratorLogger = new InvoiceItemGeneratorLogger(invoiceId, accountId, "recurring", log); final Iterator<BillingEvent> eventIt = events.iterator(); BillingEvent nextEvent = eventIt.next(); while (eventIt.hasNext()) { final BillingEvent thisEvent = nextEvent; nextEvent = eventIt.next(); if (!events.getSubscriptionIdsWithAutoInvoiceOff(). contains(thisEvent.getSubscription().getId())) { // don't consider events for subscriptions that have auto_invoice_off final BillingEvent adjustedNextEvent = (thisEvent.getSubscription().getId() == nextEvent.getSubscription().getId()) ? nextEvent : null; final List<InvoiceItem> newProposedItems = processRecurringEvent(invoiceId, accountId, thisEvent, adjustedNextEvent, targetDate, currency, invoiceItemGeneratorLogger, thisEvent.getPlan().getRecurringBillingMode(), perSubscriptionFutureNotificationDate, internalCallContext); proposedItems.addAll(newProposedItems); } } final List<InvoiceItem> newProposedItems = processRecurringEvent(invoiceId, accountId, nextEvent, null, targetDate, currency, invoiceItemGeneratorLogger, nextEvent.getPlan().getRecurringBillingMode(), perSubscriptionFutureNotificationDate, internalCallContext); proposedItems.addAll(newProposedItems); invoiceItemGeneratorLogger.logItems(); }
protected BillingEvent createNewDisableEvent(final DateTime disabledDurationStart, final BillingEvent previousEvent, final Catalog catalog) throws CatalogApiException { final int billCycleDay = previousEvent.getBillCycleDayLocal(); final SubscriptionBase subscription = previousEvent.getSubscription(); final DateTime effectiveDate = disabledDurationStart; final PlanPhase planPhase = previousEvent.getPlanPhase(); final Plan plan = previousEvent.getPlan(); // Make sure to set the fixed price to null and the billing period to NO_BILLING_PERIOD, // which makes invoice disregard this event final BigDecimal fixedPrice = null; final BillingPeriod billingPeriod = BillingPeriod.NO_BILLING_PERIOD; final Currency currency = previousEvent.getCurrency(); final String description = ""; final SubscriptionBaseTransitionType type = SubscriptionBaseTransitionType.START_BILLING_DISABLED; final Long totalOrdering = globaltotalOrder.getAndIncrement(); return new DefaultBillingEvent(subscription, effectiveDate, true, plan, planPhase, fixedPrice, currency, billingPeriod, billCycleDay, description, totalOrdering, type, catalog, true); }
@VisibleForTesting boolean isSameDayAndSameSubscription(final InvoiceItem prevComputedFixedItem, final BillingEvent currentBillingEvent, final InternalCallContext internalCallContext) { final LocalDate curLocalEffectiveDate = internalCallContext.toLocalDate(currentBillingEvent.getEffectiveDate()); if (prevComputedFixedItem != null && /* If we have computed a previous item */ prevComputedFixedItem.getStartDate().compareTo(curLocalEffectiveDate) == 0 && /* The current billing event happens at the same date */ prevComputedFixedItem.getSubscriptionId().compareTo(currentBillingEvent.getSubscription().getId()) == 0 /* The current billing event happens for the same subscription */) { return true; } else { return false; } }
protected BillingEvent createMockBillingEvent(final int bcd, 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.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 Product product = Mockito.mock(Product.class); Mockito.when(product.getName()).thenReturn(productName); final Plan plan = Mockito.mock(Plan.class); Mockito.when(plan.getName()).thenReturn(planName); Mockito.when(plan.getProduct()).thenReturn(product); 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; }
protected BillingEvent createNewReenableEvent(final DateTime odEventTime, final BillingEvent previousEvent, final Catalog catalog, final InternalTenantContext context) throws CatalogApiException { // All fields are populated with the event state from before the blocking period, for invoice to resume invoicing final int billCycleDay = previousEvent.getBillCycleDayLocal(); final SubscriptionBase subscription = previousEvent.getSubscription(); final DateTime effectiveDate = odEventTime; final PlanPhase planPhase = previousEvent.getPlanPhase(); final BigDecimal fixedPrice = previousEvent.getFixedPrice(); final Plan plan = previousEvent.getPlan(); final Currency currency = previousEvent.getCurrency(); final String description = ""; final BillingPeriod billingPeriod = previousEvent.getBillingPeriod(); final SubscriptionBaseTransitionType type = SubscriptionBaseTransitionType.END_BILLING_DISABLED; final Long totalOrdering = globaltotalOrder.getAndIncrement(); return new DefaultBillingEvent(subscription, effectiveDate, true, plan, planPhase, fixedPrice, currency, billingPeriod, billCycleDay, description, totalOrdering, type, catalog, false); }
final UUID subscriptionId = event.getSubscription().getId(); if (curSubscriptionId != null && !curSubscriptionId.equals(subscriptionId)) { final SubscriptionUsageInArrear subscriptionUsageInArrear = new SubscriptionUsageInArrear(account.getId(), invoiceId, curEvents, rawUsgRes.getRawUsage(), rawUsgRes.getExistingTrackingIds(), targetDate, rawUsgRes.getRawUsageStartDate(), usageDetailMode, internalCallContext);
private InvoiceItem generateFixedPriceItem(final UUID invoiceId, final UUID accountId, final BillingEvent thisEvent, final LocalDate targetDate, final Currency currency, final InvoiceItemGeneratorLogger invoiceItemGeneratorLogger, final InternalCallContext internalCallContext) throws InvoiceApiException { final LocalDate roundedStartDate = internalCallContext.toLocalDate(thisEvent.getEffectiveDate()); if (roundedStartDate.isAfter(targetDate)) { return null; } else { final BigDecimal fixedPrice = thisEvent.getFixedPrice(); if (fixedPrice != null) { final FixedPriceInvoiceItem fixedPriceInvoiceItem = new FixedPriceInvoiceItem(invoiceId, accountId, thisEvent.getSubscription().getBundleId(), thisEvent.getSubscription().getId(), thisEvent.getPlan().getProduct().getName(), thisEvent.getPlan().getName(), thisEvent.getPlanPhase().getName(), roundedStartDate, fixedPrice, currency); // For debugging purposes invoiceItemGeneratorLogger.append(thisEvent, fixedPriceInvoiceItem); return fixedPriceInvoiceItem; } else { return null; } } }
@Override public int compareTo(final BillingEvent e1) { if (!getSubscription().getId().equals(e1.getSubscription().getId())) { // First order by subscription return getSubscription().getId().compareTo(e1.getSubscription().getId()); } else { // subscriptions are the same if (!getEffectiveDate().equals(e1.getEffectiveDate())) { // Secondly order by date return getEffectiveDate().compareTo(e1.getEffectiveDate()); } else { // dates and subscriptions are the same return getTotalOrdering().compareTo(e1.getTotalOrdering()); } } } };
@Override public int compareTo(final BillingEvent e1) { if (!getSubscription().getId().equals(e1.getSubscription().getId())) { // First order by subscription return getSubscription().getId().compareTo(e1.getSubscription().getId()); } else { // subscriptions are the same if (!getEffectiveDate().equals(e1.getEffectiveDate())) { // Secondly order by date
final RecurringInvoiceItem recurringItem = new RecurringInvoiceItem(invoiceId, accountId, thisEvent.getSubscription().getBundleId(), thisEvent.getSubscription().getId(), thisEvent.getPlan().getProduct().getName(), thisEvent.getPlan().getName(), updatePerSubscriptionNextNotificationDate(thisEvent.getSubscription().getId(), itemDataWithNextBillingCycleDate.getNextBillingCycleDate(), items, billingMode, perSubscriptionFutureNotificationDate);