Search in sources :

Example 11 with SubscriptionFutureNotificationDates

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

the class TestInvoiceWithMetadata method testWith$0RecurringItem.

@Test(groups = "fast")
public void testWith$0RecurringItem() {
    final LocalDate invoiceDate = new LocalDate(2016, 11, 15);
    final DefaultInvoice originalInvoice = new DefaultInvoice(account.getId(), invoiceDate, account.getCurrency());
    final Plan plan = new MockPlan("my-plan");
    final MockInternationalPrice price = new MockInternationalPrice(new DefaultPrice(BigDecimal.TEN, account.getCurrency()));
    final PlanPhase planPhase = new MockPlanPhase(price, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
    final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, invoiceDate.toDateTimeAtStartOfDay(), plan, planPhase, null, BigDecimal.ZERO, account.getCurrency(), planPhase.getRecurring().getBillingPeriod(), 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
    final InvoiceItem invoiceItem = new RecurringInvoiceItem(UUID.randomUUID(), invoiceDate.toDateTimeAtStartOfDay(), originalInvoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), invoiceDate, invoiceDate.plusMonths(1), BigDecimal.ZERO, BigDecimal.ZERO, account.getCurrency());
    originalInvoice.addInvoiceItem(invoiceItem);
    final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDates = new HashMap<UUID, SubscriptionFutureNotificationDates>();
    final SubscriptionFutureNotificationDates subscriptionFutureNotificationDates = new SubscriptionFutureNotificationDates(BillingMode.IN_ADVANCE);
    subscriptionFutureNotificationDates.updateNextRecurringDateIfRequired(invoiceDate.plusMonths(1));
    perSubscriptionFutureNotificationDates.put(subscription.getId(), subscriptionFutureNotificationDates);
    final InvoiceWithMetadata invoiceWithMetadata = new InvoiceWithMetadata(originalInvoice, perSubscriptionFutureNotificationDates);
    // We generate an invoice with one item, invoicing for $0
    final Invoice resultingInvoice = invoiceWithMetadata.getInvoice();
    Assert.assertNotNull(resultingInvoice);
    Assert.assertEquals(resultingInvoice.getInvoiceItems().size(), 1);
    Assert.assertEquals(resultingInvoice.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
    final Map<UUID, InvoiceWithMetadata.SubscriptionFutureNotificationDates> dateMap = invoiceWithMetadata.getPerSubscriptionFutureNotificationDates();
    final InvoiceWithMetadata.SubscriptionFutureNotificationDates futureNotificationDates = dateMap.get(subscription.getId());
    // We verify that we generated the future notification for a month ahead
    Assert.assertNotNull(futureNotificationDates.getNextRecurringDate());
    Assert.assertEquals(futureNotificationDates.getNextRecurringDate().compareTo(invoiceDate.plusMonths(1)), 0);
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) HashMap(java.util.HashMap) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) LocalDate(org.joda.time.LocalDate) MockInternationalPrice(org.killbill.billing.catalog.MockInternationalPrice) SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) MockPlan(org.killbill.billing.catalog.MockPlan) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) DefaultPrice(org.killbill.billing.catalog.DefaultPrice) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 12 with SubscriptionFutureNotificationDates

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

the class DefaultInvoiceGenerator method generateInvoice.

/*
     * adjusts target date to the maximum invoice target date, if future invoices exist
     */
@Override
public InvoiceWithMetadata generateInvoice(final ImmutableAccountData account, @Nullable final BillingEventSet events, @Nullable final List<Invoice> existingInvoices, final LocalDate targetDate, final Currency targetCurrency, final InternalCallContext context) throws InvoiceApiException {
    if ((events == null) || (events.size() == 0) || events.isAccountAutoInvoiceOff()) {
        return new InvoiceWithMetadata(null, ImmutableMap.<UUID, SubscriptionFutureNotificationDates>of());
    }
    validateTargetDate(targetDate, context);
    final LocalDate adjustedTargetDate = adjustTargetDate(existingInvoices, targetDate);
    final LocalDate invoiceDate = context.toLocalDate(context.getCreatedDate());
    final DefaultInvoice invoice = new DefaultInvoice(account.getId(), invoiceDate, adjustedTargetDate, targetCurrency);
    final UUID invoiceId = invoice.getId();
    final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDates = new HashMap<UUID, SubscriptionFutureNotificationDates>();
    final List<InvoiceItem> fixedAndRecurringItems = recurringInvoiceItemGenerator.generateItems(account, invoiceId, events, existingInvoices, adjustedTargetDate, targetCurrency, perSubscriptionFutureNotificationDates, context);
    invoice.addInvoiceItems(fixedAndRecurringItems);
    final List<InvoiceItem> usageItems = usageInvoiceItemGenerator.generateItems(account, invoiceId, events, existingInvoices, adjustedTargetDate, targetCurrency, perSubscriptionFutureNotificationDates, context);
    invoice.addInvoiceItems(usageItems);
    return new InvoiceWithMetadata(invoice.getInvoiceItems().isEmpty() ? null : invoice, perSubscriptionFutureNotificationDates);
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) HashMap(java.util.HashMap) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice)

Example 13 with SubscriptionFutureNotificationDates

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

the class FixedAndRecurringInvoiceItemGenerator method updatePerSubscriptionNextNotificationDate.

private void updatePerSubscriptionNextNotificationDate(final UUID subscriptionId, final LocalDate nextBillingCycleDate, final List<InvoiceItem> newProposedItems, final BillingMode billingMode, final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDates) {
    LocalDate nextNotificationDate = null;
    switch(billingMode) {
        case IN_ADVANCE:
            for (final InvoiceItem item : newProposedItems) {
                if ((item.getEndDate() != null) && (item.getAmount() == null || item.getAmount().compareTo(BigDecimal.ZERO) >= 0)) {
                    if (nextNotificationDate == null) {
                        nextNotificationDate = item.getEndDate();
                    } else {
                        nextNotificationDate = nextNotificationDate.compareTo(item.getEndDate()) > 0 ? nextNotificationDate : item.getEndDate();
                    }
                }
            }
            break;
        case IN_ARREAR:
            nextNotificationDate = nextBillingCycleDate;
            break;
        default:
            throw new IllegalStateException("Unrecognized billing mode " + billingMode);
    }
    if (nextNotificationDate != null) {
        SubscriptionFutureNotificationDates subscriptionFutureNotificationDates = perSubscriptionFutureNotificationDates.get(subscriptionId);
        if (subscriptionFutureNotificationDates == null) {
            subscriptionFutureNotificationDates = new SubscriptionFutureNotificationDates(billingMode);
            perSubscriptionFutureNotificationDates.put(subscriptionId, subscriptionFutureNotificationDates);
        }
        subscriptionFutureNotificationDates.updateNextRecurringDateIfRequired(nextNotificationDate);
    }
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) LocalDate(org.joda.time.LocalDate)

Example 14 with SubscriptionFutureNotificationDates

use of org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates 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)

Example 15 with SubscriptionFutureNotificationDates

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

the class UsageInvoiceItemGenerator method updatePerSubscriptionNextNotificationUsageDate.

private void updatePerSubscriptionNextNotificationUsageDate(final UUID subscriptionId, final Map<String, LocalDate> nextBillingCycleDates, final BillingMode usageBillingMode, final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDates) {
    if (usageBillingMode == BillingMode.IN_ADVANCE) {
        throw new IllegalStateException("Not implemented Yet)");
    }
    SubscriptionFutureNotificationDates subscriptionFutureNotificationDates = perSubscriptionFutureNotificationDates.get(subscriptionId);
    if (subscriptionFutureNotificationDates == null) {
        subscriptionFutureNotificationDates = new SubscriptionFutureNotificationDates(null);
        perSubscriptionFutureNotificationDates.put(subscriptionId, subscriptionFutureNotificationDates);
    }
    for (final String usageName : nextBillingCycleDates.keySet()) {
        subscriptionFutureNotificationDates.updateNextUsageDateIfRequired(usageName, usageBillingMode, nextBillingCycleDates.get(usageName));
    }
}
Also used : SubscriptionFutureNotificationDates(org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates)

Aggregations

SubscriptionFutureNotificationDates (org.killbill.billing.invoice.generator.InvoiceWithMetadata.SubscriptionFutureNotificationDates)19 LocalDate (org.joda.time.LocalDate)18 UUID (java.util.UUID)17 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)17 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)16 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)16 LinkedList (java.util.LinkedList)15 Invoice (org.killbill.billing.invoice.api.Invoice)15 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)15 Test (org.testng.annotations.Test)15 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)14 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)14 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)14 BillingEventSet (org.killbill.billing.junction.BillingEventSet)14 BigDecimal (java.math.BigDecimal)12 DefaultPrice (org.killbill.billing.catalog.DefaultPrice)12 MockInternationalPrice (org.killbill.billing.catalog.MockInternationalPrice)12 MockPlan (org.killbill.billing.catalog.MockPlan)12 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)12 Plan (org.killbill.billing.catalog.api.Plan)12