Skip to content

Commit

Permalink
invoice: Implement filtering to opnly return items matching specific …
Browse files Browse the repository at this point in the history
…dryRun subscriptionId.

See #401
This is an extension for 879d013
  • Loading branch information
sbrossie committed Oct 5, 2015
1 parent 879d013 commit 25ab392
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Expand Up @@ -189,7 +189,6 @@ public void testDryRunWithNoTargetDateAndMultipleNonAlignedSubscriptions() throw
// We test first the next expected invoice for a specific subscription: We can see the targetDate is 2015-2-14 and not 2015-2-1
final DryRunArguments dryRunWIthSubscription = new TestDryRunArguments(DryRunType.UPCOMING_INVOICE, null, null, null, null, null, null, subscriptionMonthly.getId(), null, null, null);
expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 2, 14), new LocalDate(2015, 3, 14), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 2, 1), new LocalDate(2016, 2, 1), InvoiceItemType.RECURRING, new BigDecimal("2399.95")));
dryRunInvoice = invoiceUserApi.triggerInvoiceGeneration(account.getId(), null, dryRunWIthSubscription, callContext);
assertEquals(dryRunInvoice.getTargetDate(), new LocalDate(2015, 2, 14));
invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, callContext, expectedInvoices);
Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -235,12 +236,14 @@ private Invoice processAccountWithLock(final UUID accountId, @Nullable final Dat
if (billingEvents.isEmpty()) {
return null;
}
final Iterable<UUID> filteredSubscriptionIdsForDryRun = getFilteredSubscriptionIdsForDryRun(dryRunArguments, billingEvents);
final List<DateTime> candidateDateTimes = (inputTargetDateTime != null) ?
ImmutableList.of(inputTargetDateTime) :
getUpcomingInvoiceCandidateDates(getFilteredSubscriptionIds(dryRunArguments, billingEvents), context);
getUpcomingInvoiceCandidateDates(filteredSubscriptionIdsForDryRun, context);
for (final DateTime curTargetDateTime : candidateDateTimes) {
final Invoice invoice = processAccountWithLockAndInputTargetDate(accountId, curTargetDateTime, billingEvents, isDryRun, context);
if (invoice != null) {
filterInvoiceItemsForDryRun(filteredSubscriptionIdsForDryRun, invoice);
return invoice;
}
}
Expand All @@ -251,9 +254,23 @@ private Invoice processAccountWithLock(final UUID accountId, @Nullable final Dat
}
}

private void filterInvoiceItemsForDryRun(final Iterable<UUID> filteredSubscriptionIdsForDryRun, final Invoice invoice) {
if (!filteredSubscriptionIdsForDryRun.iterator().hasNext()) {
return;
}

final Iterator<InvoiceItem> it = invoice.getInvoiceItems().iterator();
while (it.hasNext()) {
final InvoiceItem cur = it.next();
if (!Iterables.contains(filteredSubscriptionIdsForDryRun, cur.getSubscriptionId())) {
it.remove();
}
}
}

private Iterable<UUID> getFilteredSubscriptionIds(final DryRunArguments dryRunArguments, final BillingEventSet billingEvents) {
if (!dryRunArguments.getDryRunType().equals(DryRunType.UPCOMING_INVOICE) ||
private Iterable<UUID> getFilteredSubscriptionIdsForDryRun(@Nullable final DryRunArguments dryRunArguments, final BillingEventSet billingEvents) {
if (dryRunArguments == null ||
!dryRunArguments.getDryRunType().equals(DryRunType.UPCOMING_INVOICE) ||
(dryRunArguments.getSubscriptionId() == null && dryRunArguments.getBundleId() == null)) {
return ImmutableList.<UUID>of();
}
Expand Down

0 comments on commit 25ab392

Please sign in to comment.