Search in sources :

Example 1 with UsageDef

use of org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates.UsageDef in project killbill by killbill.

the class InvoiceDispatcher method createNextFutureNotificationDate.

private FutureAccountNotifications createNextFutureNotificationDate(final InvoiceWithMetadata invoiceWithMetadata, final InternalCallContext context) {
    final Map<UUID, List<SubscriptionNotification>> result = new HashMap<UUID, List<SubscriptionNotification>>();
    for (final UUID subscriptionId : invoiceWithMetadata.getPerSubscriptionFutureNotificationDates().keySet()) {
        final List<SubscriptionNotification> perSubscriptionNotifications = new ArrayList<SubscriptionNotification>();
        final SubscriptionFutureNotificationDates subscriptionFutureNotificationDates = invoiceWithMetadata.getPerSubscriptionFutureNotificationDates().get(subscriptionId);
        // Add next recurring date if any
        if (subscriptionFutureNotificationDates.getNextRecurringDate() != null) {
            perSubscriptionNotifications.add(new SubscriptionNotification(context.toUTCDateTime(subscriptionFutureNotificationDates.getNextRecurringDate()), true));
        }
        // Add next usage dates if any
        if (subscriptionFutureNotificationDates.getNextUsageDates() != null) {
            for (final UsageDef usageDef : subscriptionFutureNotificationDates.getNextUsageDates().keySet()) {
                final LocalDate nextNotificationDateForUsage = subscriptionFutureNotificationDates.getNextUsageDates().get(usageDef);
                final DateTime subscriptionUsageCallbackDate = nextNotificationDateForUsage != null ? context.toUTCDateTime(nextNotificationDateForUsage) : null;
                perSubscriptionNotifications.add(new SubscriptionNotification(subscriptionUsageCallbackDate, true));
            }
        }
        if (!perSubscriptionNotifications.isEmpty()) {
            result.put(subscriptionId, perSubscriptionNotifications);
        }
    }
    // If dryRunNotification is enabled we also need to fetch the upcoming PHASE dates (we add SubscriptionNotification with isForInvoiceNotificationTrigger = false)
    final boolean isInvoiceNotificationEnabled = invoiceConfig.getDryRunNotificationSchedule(context).getMillis() > 0;
    if (isInvoiceNotificationEnabled) {
        final Map<UUID, DateTime> upcomingPhasesForSubscriptions = subscriptionApi.getNextFutureEventForSubscriptions(SubscriptionBaseTransitionType.PHASE, context);
        for (final UUID cur : upcomingPhasesForSubscriptions.keySet()) {
            final DateTime curDate = upcomingPhasesForSubscriptions.get(cur);
            List<SubscriptionNotification> resultValue = result.get(cur);
            if (resultValue == null) {
                resultValue = new ArrayList<SubscriptionNotification>();
            }
            resultValue.add(new SubscriptionNotification(curDate, false));
            result.put(cur, resultValue);
        }
    }
    return new FutureAccountNotifications(result);
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SubscriptionNotification(org.killbill.billing.invoice.InvoiceDispatcher.FutureAccountNotifications.SubscriptionNotification) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) UsageDef(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates.UsageDef) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) UUID(java.util.UUID)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 UUID (java.util.UUID)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 SubscriptionNotification (org.killbill.billing.invoice.InvoiceDispatcher.FutureAccountNotifications.SubscriptionNotification)1 SubscriptionFutureNotificationDates (org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates)1 UsageDef (org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates.UsageDef)1