private Integer computeAccountBCD(final BillingEventSet result) throws CatalogApiException { BillingEvent oldestAccountAlignedBillingEvent = null; for (final BillingEvent event : result) { if (event.getBillingAlignment() != BillingAlignment.ACCOUNT) { continue; } final BigDecimal recurringPrice = event.getRecurringPrice(event.getEffectiveDate()); final boolean hasRecurringPrice = recurringPrice != null; // Note: could be zero (BCD would still be set, by convention) final boolean hasUsage = event.getUsages() != null && !event.getUsages().isEmpty(); if (!hasRecurringPrice && !hasUsage) { // Nothing to bill, ignored for the purpose of BCD calculation continue; } if (oldestAccountAlignedBillingEvent == null || event.getEffectiveDate().compareTo(oldestAccountAlignedBillingEvent.getEffectiveDate()) < 0 || (event.getEffectiveDate().compareTo(oldestAccountAlignedBillingEvent.getEffectiveDate()) == 0 && event.getTotalOrdering().compareTo(oldestAccountAlignedBillingEvent.getTotalOrdering()) < 0)) { oldestAccountAlignedBillingEvent = event; } } if (oldestAccountAlignedBillingEvent == null) { return null; } // BCD in the account timezone final int accountBCDCandidate = oldestAccountAlignedBillingEvent.getBillCycleDayLocal(); Preconditions.checkState(accountBCDCandidate > 0, "Wrong Account BCD calculation for event: " + oldestAccountAlignedBillingEvent); return accountBCDCandidate; }
@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()); } } } };
return getTotalOrdering().compareTo(e1.getTotalOrdering());