Example usage for org.joda.time LocalDate isAfter

List of usage examples for org.joda.time LocalDate isAfter

Introduction

In this page you can find the example usage for org.joda.time LocalDate isAfter.

Prototype

public boolean isAfter(ReadablePartial partial) 

Source Link

Document

Is this partial later than the specified partial.

Usage

From source file:com.ning.billing.invoice.generator.DefaultInvoiceGenerator.java

License:Apache License

private List<InvoiceItem> processEvents(final UUID invoiceId, final UUID accountId,
        final BillingEvent thisEvent, @Nullable final BillingEvent nextEvent, final LocalDate targetDate,
        final Currency currency, final StringBuilder logStringBuilder) throws InvoiceApiException {
    final List<InvoiceItem> items = new ArrayList<InvoiceItem>();

    // Handle fixed price items
    final InvoiceItem fixedPriceInvoiceItem = generateFixedPriceItem(invoiceId, accountId, thisEvent,
            targetDate, currency);//w  ww  .  j  a va2 s.  co m
    if (fixedPriceInvoiceItem != null) {
        items.add(fixedPriceInvoiceItem);
    }

    // Handle recurring items
    final BillingPeriod billingPeriod = thisEvent.getBillingPeriod();
    if (billingPeriod != BillingPeriod.NO_BILLING_PERIOD) {
        final BillingMode billingMode = instantiateBillingMode(thisEvent.getBillingMode());
        final LocalDate startDate = new LocalDate(thisEvent.getEffectiveDate(), thisEvent.getTimeZone());

        if (!startDate.isAfter(targetDate)) {
            final LocalDate endDate = (nextEvent == null) ? null
                    : new LocalDate(nextEvent.getEffectiveDate(), nextEvent.getTimeZone());

            final int billCycleDayLocal = thisEvent.getBillCycleDayLocal();

            final List<RecurringInvoiceItemData> itemData;
            try {
                itemData = billingMode.calculateInvoiceItemData(startDate, endDate, targetDate,
                        billCycleDayLocal, billingPeriod);
            } catch (InvalidDateSequenceException e) {
                throw new InvoiceApiException(ErrorCode.INVOICE_INVALID_DATE_SEQUENCE, startDate, endDate,
                        targetDate);
            }

            for (final RecurringInvoiceItemData itemDatum : itemData) {
                final BigDecimal rate = thisEvent.getRecurringPrice();

                if (rate != null) {
                    final BigDecimal amount = itemDatum.getNumberOfCycles().multiply(rate)
                            .setScale(NUMBER_OF_DECIMALS, ROUNDING_MODE);

                    final RecurringInvoiceItem recurringItem = new RecurringInvoiceItem(invoiceId, accountId,
                            thisEvent.getSubscription().getBundleId(), thisEvent.getSubscription().getId(),
                            thisEvent.getPlan().getName(), thisEvent.getPlanPhase().getName(),
                            itemDatum.getStartDate(), itemDatum.getEndDate(), amount, rate, currency);
                    items.add(recurringItem);
                }
            }
        }
    }

    // For debugging purposes
    logStringBuilder.append("\n").append(thisEvent);
    for (final InvoiceItem item : items) {
        logStringBuilder.append("\n\t").append(item);
    }

    return items;
}

From source file:com.ning.billing.invoice.generator.DefaultInvoiceGenerator.java

License:Apache License

InvoiceItem generateFixedPriceItem(final UUID invoiceId, final UUID accountId, final BillingEvent thisEvent,
        final LocalDate targetDate, final Currency currency) {
    final LocalDate roundedStartDate = new LocalDate(thisEvent.getEffectiveDate(), thisEvent.getTimeZone());

    if (roundedStartDate.isAfter(targetDate)) {
        return null;
    } else {//from  w ww.ja va  2  s .c o  m
        final BigDecimal fixedPrice = thisEvent.getFixedPrice();

        if (fixedPrice != null) {
            return new FixedPriceInvoiceItem(invoiceId, accountId, thisEvent.getSubscription().getBundleId(),
                    thisEvent.getSubscription().getId(), thisEvent.getPlan().getName(),
                    thisEvent.getPlanPhase().getName(), roundedStartDate, fixedPrice, currency);
        } else {
            return null;
        }
    }
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static LocalDate calculateLastBillingCycleDateBefore(final LocalDate date,
        final LocalDate previousBillCycleDate, final int billingCycleDay, final BillingPeriod billingPeriod) {
    LocalDate proposedDate = previousBillCycleDate;

    int numberOfPeriods = 0;
    while (!proposedDate.isAfter(date)) {
        proposedDate = previousBillCycleDate.plusMonths(numberOfPeriods * billingPeriod.getNumberOfMonths());
        numberOfPeriods += 1;//from w ww  . j  a  va2 s. c  om
    }

    proposedDate = proposedDate.plusMonths(-billingPeriod.getNumberOfMonths());

    if (proposedDate.dayOfMonth().get() < billingCycleDay) {
        final int lastDayOfTheMonth = proposedDate.dayOfMonth().getMaximumValue();
        if (lastDayOfTheMonth < billingCycleDay) {
            proposedDate = new LocalDate(proposedDate.getYear(), proposedDate.getMonthOfYear(),
                    lastDayOfTheMonth);
        } else {
            proposedDate = new LocalDate(proposedDate.getYear(), proposedDate.getMonthOfYear(),
                    billingCycleDay);
        }
    }

    if (proposedDate.isBefore(previousBillCycleDate)) {
        // Make sure not to go too far in the past
        return previousBillCycleDate;
    } else {
        return proposedDate;
    }
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static LocalDate calculateEffectiveEndDate(final LocalDate billCycleDate, final LocalDate targetDate,
        final BillingPeriod billingPeriod) {
    if (targetDate.isBefore(billCycleDate)) {
        return billCycleDate;
    }// w  ww  . j a v  a  2s.  c  om

    final int numberOfMonthsInPeriod = billingPeriod.getNumberOfMonths();
    int numberOfPeriods = 0;
    LocalDate proposedDate = billCycleDate;

    while (!proposedDate.isAfter(targetDate)) {
        proposedDate = billCycleDate.plusMonths(numberOfPeriods * numberOfMonthsInPeriod);
        numberOfPeriods += 1;
    }

    return proposedDate;
}

From source file:com.ning.billing.invoice.generator.InvoiceDateUtils.java

License:Apache License

public static LocalDate calculateEffectiveEndDate(final LocalDate billCycleDate, final LocalDate targetDate,
        final LocalDate endDate, final BillingPeriod billingPeriod) {
    if (targetDate.isBefore(endDate)) {
        if (targetDate.isBefore(billCycleDate)) {
            return billCycleDate;
        }/*from  w  w  w  .j  a  v  a 2  s  .c o m*/

        final int numberOfMonthsInPeriod = billingPeriod.getNumberOfMonths();
        int numberOfPeriods = 0;
        LocalDate proposedDate = billCycleDate;

        while (!proposedDate.isAfter(targetDate)) {
            proposedDate = billCycleDate.plusMonths(numberOfPeriods * numberOfMonthsInPeriod);
            numberOfPeriods += 1;
        }

        // the current period includes the target date
        // check to see whether the end date truncates the period
        if (endDate.isBefore(proposedDate)) {
            return endDate;
        } else {
            return proposedDate;
        }
    } else {
        return endDate;
    }
}

From source file:com.ning.billing.invoice.model.InAdvanceBillingMode.java

License:Apache License

@Override
public List<RecurringInvoiceItemData> calculateInvoiceItemData(final LocalDate startDate,
        @Nullable final LocalDate endDate, final LocalDate targetDate, final int billingCycleDayLocal,
        final BillingPeriod billingPeriod) throws InvalidDateSequenceException {
    if (endDate != null && endDate.isBefore(startDate)) {
        throw new InvalidDateSequenceException();
    }/*  ww w  . j a  v  a 2 s.  c  om*/
    if (targetDate.isBefore(startDate)) {
        throw new InvalidDateSequenceException();
    }

    final List<RecurringInvoiceItemData> results = new ArrayList<RecurringInvoiceItemData>();

    final LocalDate firstBillingCycleDate = calculateBillingCycleDateOnOrAfter(startDate, billingCycleDayLocal);

    // We are not billing for less than a day (we could...)
    if (endDate != null && endDate.equals(startDate)) {
        return results;
    }
    //
    // If there is an endDate and that endDate is before our first coming firstBillingCycleDate, all we have to do
    // is to charge for that period
    //
    if (endDate != null && !endDate.isAfter(firstBillingCycleDate)) {
        final BigDecimal leadingProRationPeriods = calculateProRationBeforeFirstBillingPeriod(startDate,
                endDate, billingPeriod);
        final RecurringInvoiceItemData itemData = new RecurringInvoiceItemData(startDate, endDate,
                leadingProRationPeriods);
        results.add(itemData);
        return results;
    }

    //
    // Leading proration if
    // i) The first firstBillingCycleDate is strictly after our start date AND
    // ii) The endDate is is not null and is strictly after our firstBillingCycleDate (previous check)
    //
    if (firstBillingCycleDate.isAfter(startDate)) {
        final BigDecimal leadingProRationPeriods = calculateProRationBeforeFirstBillingPeriod(startDate,
                firstBillingCycleDate, billingPeriod);
        if (leadingProRationPeriods != null && leadingProRationPeriods.compareTo(BigDecimal.ZERO) > 0) {
            // Not common - add info in the logs for debugging purposes
            final RecurringInvoiceItemData itemData = new RecurringInvoiceItemData(startDate,
                    firstBillingCycleDate, leadingProRationPeriods);
            log.info("Adding pro-ration: {}", itemData);
            results.add(itemData);
        }
    }

    //
    // Calculate the effectiveEndDate from the firstBillingCycleDate:
    // - If endDate != null and targetDate is after endDate => this is the endDate and will lead to a trailing pro-ration
    // - If not, this is the last billingCycleDate calculation right after the targetDate
    //
    final LocalDate effectiveEndDate;
    if (endDate != null) {
        effectiveEndDate = calculateEffectiveEndDate(firstBillingCycleDate, targetDate, endDate, billingPeriod);
    } else {
        effectiveEndDate = calculateEffectiveEndDate(firstBillingCycleDate, targetDate, billingPeriod);
    }

    //
    // Based on what we calculated previously, code recompute one more time the numberOfWholeBillingPeriods
    //
    final LocalDate lastBillingCycleDate = calculateLastBillingCycleDateBefore(effectiveEndDate,
            firstBillingCycleDate, billingCycleDayLocal, billingPeriod);
    final int numberOfWholeBillingPeriods = calculateNumberOfWholeBillingPeriods(firstBillingCycleDate,
            lastBillingCycleDate, billingPeriod);
    final int numberOfMonthsPerBillingPeriod = billingPeriod.getNumberOfMonths();

    for (int i = 0; i < numberOfWholeBillingPeriods; i++) {
        final LocalDate servicePeriodStartDate;
        if (results.size() > 0) {
            // Make sure the periods align, especially with the pro-ration calculations above
            servicePeriodStartDate = results.get(results.size() - 1).getEndDate();
        } else if (i == 0) {
            // Use the specified start date
            servicePeriodStartDate = startDate;
        } else {
            throw new IllegalStateException("We should at least have one invoice item!");
        }

        // Make sure to align the end date with the BCD
        final LocalDate servicePeriodEndDate = firstBillingCycleDate
                .plusMonths((i + 1) * numberOfMonthsPerBillingPeriod);

        results.add(new RecurringInvoiceItemData(servicePeriodStartDate, servicePeriodEndDate, BigDecimal.ONE));
    }

    //
    // Now we check if indeed we need a trailing proration and add that incomplete item
    //
    if (effectiveEndDate.isAfter(lastBillingCycleDate)) {
        final BigDecimal trailingProRationPeriods = calculateProRationAfterLastBillingCycleDate(
                effectiveEndDate, lastBillingCycleDate, billingPeriod);
        if (trailingProRationPeriods.compareTo(BigDecimal.ZERO) > 0) {
            // Not common - add info in the logs for debugging purposes
            final RecurringInvoiceItemData itemData = new RecurringInvoiceItemData(lastBillingCycleDate,
                    effectiveEndDate, trailingProRationPeriods);
            log.info("Adding trailing pro-ration: {}", itemData);
            results.add(itemData);
        }
    }
    return results;
}

From source file:com.ning.billing.overdue.config.DefaultCondition.java

License:Apache License

@Override
public boolean evaluate(final BillingState<T> state, final LocalDate date) {
    LocalDate unpaidInvoiceTriggerDate = null;
    if (timeSinceEarliestUnpaidInvoiceEqualsOrExceeds != null
            && state.getDateOfEarliestUnpaidInvoice() != null) { // no date => no unpaid invoices
        unpaidInvoiceTriggerDate = state.getDateOfEarliestUnpaidInvoice()
                .plus(timeSinceEarliestUnpaidInvoiceEqualsOrExceeds.toJodaPeriod());
    }/*  ww  w . j a v a  2  s . c  o m*/

    return (numberOfUnpaidInvoicesEqualsOrExceeds == null
            || state.getNumberOfUnpaidInvoices() >= numberOfUnpaidInvoicesEqualsOrExceeds)
            && (totalUnpaidInvoiceBalanceEqualsOrExceeds == null || totalUnpaidInvoiceBalanceEqualsOrExceeds
                    .compareTo(state.getBalanceOfUnpaidInvoices()) <= 0)
            && (timeSinceEarliestUnpaidInvoiceEqualsOrExceeds == null
                    || (unpaidInvoiceTriggerDate != null && !unpaidInvoiceTriggerDate.isAfter(date)))
            && (responseForLastFailedPayment == null
                    || responseIsIn(state.getResponseForLastFailedPayment(), responseForLastFailedPayment))
            && (controlTag == null || isTagIn(controlTag, state.getTags()));
}

From source file:com.phloc.datetime.PDTUtils.java

License:Apache License

/**
 * Count all non-weekend days in the range. Does not consider holidays!
 *
 * @param aStartDate//from   ww w  .  j  a  v  a2s. c o m
 *        start date
 * @param aEndDate
 *        end date
 * @return days not counting Saturdays and Sundays. If start date is after end
 *         date, the value will be negative! If start date equals end date the
 *         return will be 1 if it is a week day.
 */
public static int getWeekDays(@Nonnull final LocalDate aStartDate, @Nonnull final LocalDate aEndDate) {
    ValueEnforcer.notNull(aStartDate, "StartDate");
    ValueEnforcer.notNull(aEndDate, "EndDate");

    final boolean bFlip = aStartDate.isAfter(aEndDate);
    LocalDate aCurDate = bFlip ? aEndDate : aStartDate;
    final LocalDate aRealEndDate = bFlip ? aStartDate : aEndDate;

    int ret = 0;
    while (!aRealEndDate.isBefore(aCurDate)) {
        if (!isWeekend(aCurDate))
            ret++;
        aCurDate = aCurDate.plusDays(1);
    }
    return bFlip ? -1 * ret : ret;
}

From source file:com.phloc.datetime.PDTUtils.java

License:Apache License

public static boolean isBetweenIncl(@Nullable final LocalDate aDate, @Nullable final LocalDate aLowerBound,
        @Nullable final LocalDate aUpperBound) {
    if (aDate == null || aLowerBound == null || aUpperBound == null)
        return false;
    return !aLowerBound.isAfter(aDate) && !aDate.isAfter(aUpperBound);
}

From source file:com.redhat.engineering.jenkins.report.plugin.ReportPluginPortlet.java

License:Open Source License

/**
 * Graph of duration of tests over time.
 *//*from  w  ww.java  2 s  .c o  m*/
public Graph getSummaryGraph() {
    // The standard equals doesn't work because two LocalDate objects can
    // be differente even if the date is the same (different internal timestamp)
    Comparator<LocalDate> localDateComparator = new Comparator<LocalDate>() {

        @Override
        public int compare(LocalDate d1, LocalDate d2) {
            if (d1.isEqual(d2)) {
                return 0;
            }
            if (d1.isAfter(d2)) {
                return 1;
            }
            return -1;
        }
    };

    // We need a custom comparator for LocalDate objects
    final Map<LocalDate, TestResultAggrSummary> summaries = //new HashMap<LocalDate, TestResultSummary>();
            new TreeMap<LocalDate, TestResultAggrSummary>(localDateComparator);
    LocalDate today = new LocalDate();

    // for each job, for each day, add last build of the day to summary
    for (Job job : getDashboard().getJobs()) {
        Filter filter = job.getAction(ReportPluginProjectAction.class).getInitializedFilter();
        filter.addCombinationFilter(combinationFilter);
        Run run = job.getFirstBuild();

        if (run != null) { // execute only if job has builds
            LocalDate runDay = new LocalDate(run.getTimestamp());
            LocalDate firstDay = (dateRange != 0) ? new LocalDate().minusDays(dateRange) : runDay;

            while (run != null) {
                runDay = new LocalDate(run.getTimestamp());
                Run nextRun = run.getNextBuild();

                if (nextRun != null) {
                    LocalDate nextRunDay = new LocalDate(nextRun.getTimestamp());
                    // skip run before firstDay, but keep if next build is after start date
                    if (!runDay.isBefore(firstDay)
                            || runDay.isBefore(firstDay) && !nextRunDay.isBefore(firstDay)) {
                        // if next run is not the same day, use this test to summarize
                        if (nextRunDay.isAfter(runDay)) {
                            summarize(summaries, run.getAction(ReportPluginBuildAction.class).getTestResults(),
                                    filter, (runDay.isBefore(firstDay) ? firstDay : runDay),
                                    nextRunDay.minusDays(1));
                        }
                    }
                } else {
                    // use this run's test result from last run to today
                    summarize(summaries, run.getAction(ReportPluginBuildAction.class).getTestResults(), filter,
                            (runDay.isBefore(firstDay) ? firstDay : runDay), today);
                }
                run = nextRun;
            }
        }
    }

    return new Graph(-1, getGraphWidth(), getGraphHeight()) {

        protected JFreeChart createGraph() {
            return GraphHelper.createChart(buildDataSet(summaries));
        }
    };
}