Permalink
Browse files

jaxrs: Add ability to filter invoices using a start date. See #651

  • Loading branch information...
sbrossie committed Jun 20, 2018
1 parent 0c5c3bf commit 42c1cdfe2c76b3470f52948a1ce3923dba6ec383
@@ -138,6 +138,7 @@
import org.killbill.notificationq.api.NotificationQueueService;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
@@ -710,20 +711,32 @@ public Response rebalanceExistingCBAOnAccount(@PathParam("accountId") final UUID
@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid account id supplied"),
@ApiResponse(code = 404, message = "Account not found")})
public Response getInvoicesForAccount(@PathParam("accountId") final UUID accountId,
@QueryParam(QUERY_START_DATE) final String startDateStr,
@QueryParam(QUERY_INVOICE_WITH_ITEMS) @DefaultValue("false") final boolean withItems,
@QueryParam(QUERY_WITH_MIGRATION_INVOICES) @DefaultValue("false") final boolean withMigrationInvoices,
@QueryParam(QUERY_UNPAID_INVOICES_ONLY) @DefaultValue("false") final boolean unpaidInvoicesOnly,
@QueryParam(QUERY_INCLUDE_VOIDED_INVOICES) @DefaultValue("false") final boolean includeVoidedInvoices,
@QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode,
@javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException {
Preconditions.checkState(!unpaidInvoicesOnly || !withMigrationInvoices, "We don't support fetching unpaid invoices incl. migration");
Preconditions.checkState(startDateStr == null || !withMigrationInvoices, "We don't support fetching migration invoices and specifying a start date");
final TenantContext tenantContext = context.createTenantContextWithAccountId(accountId, request);
final LocalDate startDate = LOCAL_DATE_FORMATTER.parseLocalDate(startDateStr);
// Verify the account exists
accountUserApi.getAccountById(accountId, tenantContext);
final List<Invoice> invoices = unpaidInvoicesOnly ?
new ArrayList<Invoice>(invoiceApi.getUnpaidInvoicesByAccountId(accountId, null, tenantContext)) :
invoiceApi.getInvoicesByAccount(accountId, withMigrationInvoices, includeVoidedInvoices, tenantContext);
final List<Invoice> invoices;
if (unpaidInvoicesOnly) {
invoices = new ArrayList<Invoice>(invoiceApi.getUnpaidInvoicesByAccountId(accountId, startDate, tenantContext));
} else {
invoices = startDate != null ?
invoiceApi.getInvoicesByAccount(accountId, startDate, includeVoidedInvoices, tenantContext) :
invoiceApi.getInvoicesByAccount(accountId, withMigrationInvoices, includeVoidedInvoices, tenantContext);
}
final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(accountId, auditMode.getLevel(), tenantContext);
@@ -79,7 +79,7 @@ public void testAccountTimelineWithAudits() throws Exception {
final DateTime endTime = clock.getUTCNow();
// Add credit
final Invoice invoice = accountApi.getInvoicesForAccount(accountJson.getAccountId(), requestOptions).get(1);
final Invoice invoice = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, requestOptions).get(1);
final BigDecimal creditAmount = BigDecimal.ONE;
final Credit credit = new Credit();
credit.setAccountId(accountJson.getAccountId());
@@ -102,7 +102,7 @@ public void testAdminInvoiceEndpoint() throws Exception {
clock.addDays(2);
Assert.assertEquals(invoiceApi.getInvoices(requestOptions).getPaginationMaxNbRecords(), i + 1);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), false, false, false, false, AuditLevel.NONE, requestOptions);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, false, false, false, false, AuditLevel.NONE, requestOptions);
assertEquals(invoices.size(), 1);
}
@@ -115,7 +115,7 @@ public void testAdminInvoiceEndpoint() throws Exception {
Assert.assertEquals(invoiceApi.getInvoices(requestOptions).getPaginationMaxNbRecords(), 10);
for (final UUID accountId : accounts) {
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountId, false, false, false, false, AuditLevel.NONE, requestOptions);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountId, null, false, false, false, false, AuditLevel.NONE, requestOptions);
assertEquals(invoices.size(), 2);
}
@@ -137,7 +137,7 @@ public void testAdminInvoiceEndpoint() throws Exception {
Assert.assertEquals(invoiceApi.getInvoices(requestOptions).getPaginationMaxNbRecords(), 10);
for (final UUID accountId : accounts) {
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountId, false, false, false, false, AuditLevel.NONE, requestOptions);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountId, null, false, false, false, false, AuditLevel.NONE, requestOptions);
assertEquals(invoices.size(), 2);
}
@@ -183,7 +183,7 @@ private Invoice createAccountWithInvoice() throws Exception {
callbackServlet.assertListenerStatus();
// Retrieve the invoice
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), requestOptions);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, requestOptions);
// We should have two invoices, one for the trial (zero dollar amount) and one for the first month
assertEquals(invoices.size(), 2);
assertTrue(invoices.get(1).getAmount().doubleValue() > 0);
@@ -73,7 +73,7 @@ public void testAddCreditToInvoice() throws Exception {
@Test(groups = "slow", description = "Can add a credit to an existing account",
expectedExceptions = KillBillClientException.class, expectedExceptionsMessageRegExp = ".*it is already in COMMITTED status")
public void testAddCreditToCommittedInvoice() throws Exception {
final Invoice invoice = accountApi.getInvoicesForAccount(accountJson.getAccountId(), requestOptions).get(1);
final Invoice invoice = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, requestOptions).get(1);
final BigDecimal creditAmount = BigDecimal.ONE;
final Credit credit = new Credit();
@@ -296,7 +296,7 @@ public void testOverridePrice() throws Exception {
Assert.assertEquals(subscription.getEvents().get(2).getPriceList(), PriceListSet.DEFAULT_PRICELIST_NAME);
Assert.assertEquals(subscription.getEvents().get(2).getProduct(), "Shotgun");
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), true, false, false, false, AuditLevel.FULL, requestOptions);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, true, false, false, false, AuditLevel.FULL, requestOptions);
assertEquals(invoices.size(), 1);
assertEquals(invoices.get(0).getAmount().compareTo(BigDecimal.TEN), 0);
@@ -389,7 +389,7 @@ public void testEntitlementWithAddOnsWithWRITTEN_OFF() throws Exception {
assertEquals(bundle.getExternalKey(), "base");
assertEquals(bundle.getSubscriptions().size(), 3);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), true, false, false, false, AuditLevel.NONE, requestOptions);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, true, false, false, false, AuditLevel.NONE, requestOptions);
assertEquals(invoices.size(), 1);
assertEquals(invoices.get(0).getBalance().compareTo(BigDecimal.ZERO), 1);
assertEquals(invoiceApi.getInvoiceTags(invoices.get(0).getInvoiceId(), requestOptions).size(), 0);
@@ -408,7 +408,7 @@ public void testEntitlementWithAddOnsWithWRITTEN_OFF() throws Exception {
assertEquals(subscription.getState(), EntitlementState.CANCELLED);
}
final List<Invoice> invoicesAfterClose = accountApi.getInvoicesForAccount(accountJson.getAccountId(), requestOptions);
final List<Invoice> invoicesAfterClose = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, requestOptions);
assertEquals(invoicesAfterClose.size(), 1);
assertEquals(invoicesAfterClose.get(0).getBalance().compareTo(BigDecimal.ZERO), 0);
assertEquals(invoiceApi.getInvoiceTags(invoicesAfterClose.get(0).getInvoiceId(), requestOptions).size(), 1);
@@ -482,7 +482,7 @@ public void testCreateEntitlementsWithAddOnsThenCloseAccountWithItemAdjustment()
assertEquals(bundles.size(), 2);
assertFalse(bundles.get(0).getExternalKey().equals(bundles.get(1).getExternalKey()));
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), requestOptions);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, requestOptions);
assertEquals(invoices.size(), 1);
assertEquals(invoices.get(0).getBalance().compareTo(BigDecimal.ZERO), 1);
assertEquals(invoiceApi.getInvoiceTags(invoices.get(0).getInvoiceId(), requestOptions).size(), 0);
@@ -534,7 +534,7 @@ public void testCreateEntitlementsWithAddOnsThenCloseAccountWithItemAdjustment()
}
}
final List<Invoice> invoicesAfterClose = accountApi.getInvoicesForAccount(accountJson.getAccountId(), true, false, false, false, AuditLevel.NONE, requestOptions);
final List<Invoice> invoicesAfterClose = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, true, false, false, false, AuditLevel.NONE, requestOptions);
assertEquals(invoicesAfterClose.size(), 2);
assertEquals(invoicesAfterClose.get(0).getBalance().compareTo(BigDecimal.ZERO), 0);
assertEquals(invoiceApi.getInvoiceTags(invoicesAfterClose.get(0).getInvoiceId(), requestOptions).size(), 0);
@@ -623,7 +623,7 @@ public void testEntitlementsWithAddOnsAndAlreadyExistingBP() throws Exception {
assertEquals(bundles.size(), 1);
assertEquals(bundles.get(0).getSubscriptions().size(), 3);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), true, false, false, false, AuditLevel.NONE, requestOptions);
final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, true, false, false, false, AuditLevel.NONE, requestOptions);
assertEquals(invoices.size(), 2);
}
@@ -669,7 +669,7 @@ public void testcreateSubscriptionWithAutoPayOff() throws Exception {
assertEquals(tags.get(0).getTagDefinitionName(), "AUTO_PAY_OFF");
// verify that number of invoices and payments for account is still 0
assertEquals(accountApi.getInvoicesForAccount(accountJson.getAccountId(), requestOptions).size(), 0);
assertEquals(accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, requestOptions).size(), 0);
assertEquals(accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions).size(), 0);
// create a subscription with no trial plan
@@ -684,7 +684,7 @@ public void testcreateSubscriptionWithAutoPayOff() throws Exception {
assertNotNull(subscriptionJson);
// verify that number of invoices is now 1
assertEquals(accountApi.getInvoicesForAccount(accountJson.getAccountId(), requestOptions).size(), 1);
assertEquals(accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, requestOptions).size(), 1);
// verify that number of payments is still 0 (no attempts)
assertEquals(accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions).size(), 0);
@@ -54,7 +54,7 @@ public void testAutomaticPaymentAndRefund() throws Exception {
final Payments paymentsForAccount = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
final Payment payment = paymentsForAccount.get(paymentsForAccount.size() - 1);
Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), true, true, false, false, AuditLevel.NONE, requestOptions);
Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, true, true, false, false, AuditLevel.NONE, requestOptions);
// regular refund
final InvoicePaymentTransaction invoicePaymentTransactionRequest = new InvoicePaymentTransaction();
@@ -78,7 +78,7 @@ public void testAutomaticPaymentAndRefundWithAdjustments() throws Exception {
final Payments paymentsForAccount = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
final Payment payment = paymentsForAccount.get(paymentsForAccount.size() - 1);
Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), true, true, false, false, AuditLevel.NONE, requestOptions);
Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, true, true, false, false, AuditLevel.NONE, requestOptions);
final List<InvoiceItem> itemsToBeAdjusted = invoices.get(1).getItems();
// regular refund
@@ -104,7 +104,7 @@ public void testManualPaymentAndExternalRefund() throws Exception {
final Account accountJson = createAccountWithExternalPMBundleAndSubscriptionAndManualPayTagAndWaitForFirstInvoice();
final Invoices invoicesForAccount = accountApi.getInvoicesForAccount(accountJson.getAccountId(), requestOptions);
final Invoices invoicesForAccount = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, requestOptions);
final Invoice unpaidInvoice = invoicesForAccount.get(1);
assertEquals(unpaidInvoice.getBalance().compareTo(BigDecimal.valueOf(249.95)), 0);
@@ -138,7 +138,7 @@ public void testManualPaymentAndExternalRefundWithAdjustments() throws Exception
final Account accountJson = createAccountWithExternalPMBundleAndSubscriptionAndManualPayTagAndWaitForFirstInvoice();
final Invoices invoicesForAccount = accountApi.getInvoicesForAccount(accountJson.getAccountId(), true, false, false, false, AuditLevel.NONE, requestOptions);
final Invoices invoicesForAccount = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, true, false, false, false, AuditLevel.NONE, requestOptions);
final Invoice unpaidInvoice = invoicesForAccount.get(1);
assertEquals(unpaidInvoice.getBalance().compareTo(BigDecimal.valueOf(249.95)), 0);
@@ -204,7 +204,7 @@ public void testAutomaticPaymentAndExternalRefundWithAdjustments() throws Except
final Payments paymentsForAccount = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
final Payment payment = paymentsForAccount.get(paymentsForAccount.size() - 1);
final Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), true, true, false, false, AuditLevel.NONE, requestOptions);
final Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, true, true, false, false, AuditLevel.NONE, requestOptions);
final List<InvoiceItem> itemsToBeAdjusted = invoices.get(1).getItems();
// external refund
@@ -241,7 +241,7 @@ public void testAutomaticPaymentAndExternalRefundWithDifferentPM() throws Except
final Payments paymentsForAccount = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
final Payment payment = paymentsForAccount.get(paymentsForAccount.size() - 1);
final Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), true, true, false, false, AuditLevel.NONE, requestOptions);
final Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, true, true, false, false, AuditLevel.NONE, requestOptions);
final List<InvoiceItem> itemsToBeAdjusted = invoices.get(1).getItems();
// external refund
@@ -263,7 +263,7 @@ public void testAutomaticPaymentAndExternalRefundWithDifferentPM() throws Except
private void assertRefundInvoiceAdjustments(final UUID accountId) throws KillBillClientException {
final Invoices invoices;
invoices = accountApi.getInvoicesForAccount(accountId, true, true, false, false, AuditLevel.NONE, requestOptions);
invoices = accountApi.getInvoicesForAccount(accountId, null, true, true, false, false, AuditLevel.NONE, requestOptions);
final Invoice invoiceWithRefund = invoices.get(1);
assertEquals(invoiceWithRefund.getAmount().compareTo(BigDecimal.ZERO), 0);
assertEquals(invoiceWithRefund.getRefundAdj().compareTo(BigDecimal.valueOf(249.95).negate()), 0);
@@ -275,7 +275,7 @@ private void assertRefundInvoiceAdjustments(final UUID accountId) throws KillBil
}
private void assertRefundInvoiceNoAdjustments(final UUID accountId) throws KillBillClientException {
final Invoices invoices = accountApi.getInvoicesForAccount(accountId, true, true, false, false, AuditLevel.NONE, requestOptions);
final Invoices invoices = accountApi.getInvoicesForAccount(accountId, null, true, true, false, false, AuditLevel.NONE, requestOptions);
final Invoice invoiceWithRefund = invoices.get(1);
assertEquals(invoiceWithRefund.getAmount().compareTo(BigDecimal.valueOf(249.95)), 0);
assertEquals(invoiceWithRefund.getRefundAdj().compareTo(BigDecimal.valueOf(249.95).negate()), 0);
Oops, something went wrong.

1 comment on commit 42c1cdf

@pierre

This comment has been minimized.

Show comment
Hide comment
@pierre

pierre Jun 21, 2018

Member

👍

Member

pierre commented on 42c1cdf Jun 21, 2018

👍

Please sign in to comment.