List of usage examples for org.joda.time LocalDate isBefore
public boolean isBefore(ReadablePartial partial)
From source file:org.killbill.billing.invoice.generator.BillingIntervalDetail.java
License:Apache License
@VisibleForTesting void calculateFirstBillingCycleDate() { final int lastDayOfMonth = startDate.dayOfMonth().getMaximumValue(); final LocalDate billingCycleDate; if (billingCycleDay > lastDayOfMonth) { billingCycleDate = new LocalDate(startDate.getYear(), startDate.getMonthOfYear(), lastDayOfMonth, startDate.getChronology()); } else {//from www. ja va 2 s. c o m billingCycleDate = new LocalDate(startDate.getYear(), startDate.getMonthOfYear(), billingCycleDay, startDate.getChronology()); } final int numberOfMonthsInPeriod = billingPeriod.getNumberOfMonths(); LocalDate proposedDate = billingCycleDate; while (proposedDate.isBefore(startDate)) { proposedDate = proposedDate.plusMonths(numberOfMonthsInPeriod); } firstBillingCycleDate = alignProposedBillCycleDate(proposedDate, billingCycleDay); }
From source file:org.killbill.billing.invoice.generator.BillingIntervalDetail.java
License:Apache License
private void calculateLastBillingCycleDate() { // Start from firstBillingCycleDate and billingPeriod until we pass the effectiveEndDate LocalDate proposedDate = firstBillingCycleDate; int numberOfPeriods = 0; while (!proposedDate.isAfter(effectiveEndDate)) { proposedDate = firstBillingCycleDate.plusMonths(numberOfPeriods * billingPeriod.getNumberOfMonths()); numberOfPeriods += 1;// ww w .j av a 2 s . c o m } // Our proposed date is billingCycleDate prior to the effectiveEndDate proposedDate = proposedDate.plusMonths(-billingPeriod.getNumberOfMonths()); proposedDate = alignProposedBillCycleDate(proposedDate, billingCycleDay); if (proposedDate.isBefore(firstBillingCycleDate)) { // Make sure not to go too far in the past lastBillingCycleDate = firstBillingCycleDate; } else { lastBillingCycleDate = proposedDate; } }
From source file:org.killbill.billing.invoice.generator.FixedAndRecurringInvoiceItemGenerator.java
License:Apache License
public RecurringInvoiceItemDataWithNextBillingCycleDate generateInvoiceItemData(final LocalDate startDate, @Nullable final LocalDate endDate, final LocalDate targetDate, final int billingCycleDayLocal, final BillingPeriod billingPeriod, final BillingMode billingMode) throws InvalidDateSequenceException { if (endDate != null && endDate.isBefore(startDate)) { throw new InvalidDateSequenceException(); }/*from ww w . j ava2 s . c o m*/ if (targetDate.isBefore(startDate)) { throw new InvalidDateSequenceException(); } final List<RecurringInvoiceItemData> results = new ArrayList<RecurringInvoiceItemData>(); final BillingIntervalDetail billingIntervalDetail = new BillingIntervalDetail(startDate, endDate, targetDate, billingCycleDayLocal, billingPeriod, billingMode); // We are not billing for less than a day if (!billingIntervalDetail.hasSomethingToBill()) { return new RecurringInvoiceItemDataWithNextBillingCycleDate(results, billingIntervalDetail); } // // 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(billingIntervalDetail.getFirstBillingCycleDate())) { final BigDecimal leadingProRationPeriods = calculateProRationBeforeFirstBillingPeriod(startDate, endDate, billingPeriod); final RecurringInvoiceItemData itemData = new RecurringInvoiceItemData(startDate, endDate, leadingProRationPeriods); results.add(itemData); return new RecurringInvoiceItemDataWithNextBillingCycleDate(results, billingIntervalDetail); } // // 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 (billingIntervalDetail.getFirstBillingCycleDate().isAfter(startDate)) { final BigDecimal leadingProRationPeriods = calculateProRationBeforeFirstBillingPeriod(startDate, billingIntervalDetail.getFirstBillingCycleDate(), 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, billingIntervalDetail.getFirstBillingCycleDate(), 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 = billingIntervalDetail.getEffectiveEndDate(); // // Based on what we calculated previously, code recompute one more time the numberOfWholeBillingPeriods // final LocalDate lastBillingCycleDate = billingIntervalDetail.getLastBillingCycleDate(); final int numberOfWholeBillingPeriods = calculateNumberOfWholeBillingPeriods( billingIntervalDetail.getFirstBillingCycleDate(), lastBillingCycleDate, billingPeriod); for (int i = 0; i < numberOfWholeBillingPeriods; i++) { final LocalDate servicePeriodStartDate; if (!results.isEmpty()) { // 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 = billingIntervalDetail.getFutureBillingDateFor(i + 1); 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 new RecurringInvoiceItemDataWithNextBillingCycleDate(results, billingIntervalDetail); }
From source file:org.killbill.billing.invoice.model.InAdvanceBillingMode.java
License:Apache License
@Override public List<RecurringInvoiceItemData> generateInvoiceItemData(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(); }/*from w w w .j a va 2 s . c om*/ if (targetDate.isBefore(startDate)) { throw new InvalidDateSequenceException(); } final List<RecurringInvoiceItemData> results = new ArrayList<RecurringInvoiceItemData>(); final BillingIntervalDetail billingIntervalDetail = new BillingIntervalDetail(startDate, endDate, targetDate, billingCycleDayLocal, billingPeriod); // 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(billingIntervalDetail.getFirstBillingCycleDate())) { 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 (billingIntervalDetail.getFirstBillingCycleDate().isAfter(startDate)) { final BigDecimal leadingProRationPeriods = calculateProRationBeforeFirstBillingPeriod(startDate, billingIntervalDetail.getFirstBillingCycleDate(), 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, billingIntervalDetail.getFirstBillingCycleDate(), 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 = billingIntervalDetail.getEffectiveEndDate(); // // Based on what we calculated previously, code recompute one more time the numberOfWholeBillingPeriods // final LocalDate lastBillingCycleDate = billingIntervalDetail.getLastBillingCycleDate(); final int numberOfWholeBillingPeriods = calculateNumberOfWholeBillingPeriods( billingIntervalDetail.getFirstBillingCycleDate(), lastBillingCycleDate, billingPeriod); 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 = billingIntervalDetail.getFutureBillingDateFor(i + 1); 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:org.killbill.billing.plugin.analytics.utils.CurrencyConverter.java
License:Apache License
public BigDecimal getConvertedValue(@Nullable final BigDecimal value, @Nullable final String currency, @Nullable final LocalDate effectiveDate) { // No data/*from w ww . ja v a 2s . c o m*/ if (referenceCurrency == null) { return null; } // Optimization if (referenceCurrency.equals(currency)) { return value; } if (value == null || currency == null || effectiveDate == null || currencyConversions.get(currency) == null) { return null; } CurrencyConversionModelDao currencyConversionCandidate = null; for (final CurrencyConversionModelDao currencyConversionModelDao : currencyConversions.get(currency)) { if (!effectiveDate.isBefore(currencyConversionModelDao.getStartDate()) && !effectiveDate.isAfter(currencyConversionModelDao.getEndDate()) && // In case of overlapping ranges, use the narrowest one (currencyConversionCandidate == null || currencyConversionModelDao.getStartDate() .isAfter(currencyConversionCandidate.getStartDate()))) { currencyConversionCandidate = currencyConversionModelDao; } } if (currencyConversionCandidate == null) { return null; } else { return value.multiply(currencyConversionCandidate.getReferenceRate()); } }
From source file:org.killbill.billing.plugin.simpletax.resolving.InvoiceItemEndDateBasedResolver.java
License:Apache License
@Override public TaxCode applicableCodeForItem(Iterable<TaxCode> taxCodes, InvoiceItem item) { DateTimeZone accountTimeZone = account.getTimeZone(); DateTimeZone taxationTimeZone = cfg.getTaxationTimeZone(); LocalDate applicableDate = firstNonNull(item.getEndDate(), item.getStartDate()); final LocalDate taxationDate = taxationTimeZone == null ? applicableDate : convertTimeZone(applicableDate, accountTimeZone, taxationTimeZone); return tryFind(taxCodes, new Predicate<TaxCode>() { @Override//from w w w . jav a 2s . co m public boolean apply(TaxCode taxCode) { LocalDate startDay = taxCode.getStartingOn(); if ((startDay != null) && taxationDate.isBefore(startDay)) { return false; } LocalDate stopDay = taxCode.getStoppingOn(); if ((stopDay != null) && (taxationDate.isEqual(stopDay) || taxationDate.isAfter(stopDay))) { return false; } return true; } }).orNull(); }
From source file:org.kitodo.production.forms.CalendarForm.java
License:Open Source License
/** * The function checkBlockPlausibility compares the dates entered against * some plausibility assumptions and sets hints otherwise. */// w w w. j av a 2s. c om private void checkBlockPlausibility() { LocalDate firstAppearance = blockShowing.getFirstAppearance(); LocalDate lastAppearance = blockShowing.getLastAppearance(); if (Objects.nonNull(firstAppearance) && Objects.nonNull(lastAppearance)) { if (firstAppearance.plusYears(100).isBefore(lastAppearance)) { Helper.setMessage(BLOCK + "long"); } if (firstAppearance.isAfter(lastAppearance)) { Helper.setErrorMessage(BLOCK_NEGATIVE); } if (firstAppearance.isBefore(START_RELATION)) { Helper.setMessage(BLOCK + "firstAppearance.early"); } if (firstAppearance.isAfter(today)) { Helper.setMessage(BLOCK + "firstAppearance.fiction"); } if (lastAppearance.isBefore(START_RELATION)) { Helper.setMessage(BLOCK + "lastAppearance.early"); } if (lastAppearance.isAfter(today)) { Helper.setMessage(BLOCK + "lastAppearance.fiction"); } } }
From source file:org.kitodo.production.forms.CalendarForm.java
License:Open Source License
private void executeForFirstAppearanceToChangeNull(LocalDate newLastAppearance) { if (Objects.isNull(blockShowing.getLastAppearance()) || !blockShowing.getLastAppearance().isEqual(newLastAppearance)) { if (Objects.nonNull(blockShowing.getFirstAppearance()) && newLastAppearance.isBefore(blockShowing.getFirstAppearance())) { Helper.setErrorMessage(BLOCK_NEGATIVE); return; }//from ww w . jav a 2 s . co m blockShowing.setLastAppearance(newLastAppearance); checkBlockPlausibility(); navigate(); } }
From source file:org.kitodo.production.forms.CalendarForm.java
License:Open Source License
private void executeForFirstAppearanceToChange(LocalDate newLastAppearance) { if (Objects.isNull(blockShowing.getLastAppearance()) || !blockShowing.getLastAppearance().isEqual(newLastAppearance)) { if (newLastAppearance.isBefore(firstAppearanceIsToChange)) { Helper.setErrorMessage(BLOCK_NEGATIVE); return; }/* w w w . ja v a 2 s . c o m*/ blockShowing.setPublicationPeriod(firstAppearanceIsToChange, newLastAppearance); } else { if (Objects.nonNull(blockShowing.getLastAppearance()) && blockShowing.getLastAppearance().isBefore(firstAppearanceIsToChange)) { Helper.setErrorMessage(BLOCK_NEGATIVE); return; } blockShowing.setFirstAppearance(firstAppearanceIsToChange); } checkBlockPlausibility(); navigate(); }
From source file:org.kuali.coeus.common.budget.impl.rate.TrainingStipendRateServiceImpl.java
License:Open Source License
@Override public TrainingStipendRateContract findClosestMatchTrainingStipendRate(Date effectiveDate, String careerLevel, int experienceLevel) { if (effectiveDate == null) { throw new IllegalArgumentException("effectiveDate is null"); }// w w w . jav a2s .c om if (StringUtils.isBlank(careerLevel)) { throw new IllegalArgumentException("careerLevel is null"); } final Map<String, Object> criteria = new HashMap<>(); criteria.put("careerLevel", careerLevel); criteria.put("experienceLevel", experienceLevel); final Collection<TrainingStipendRate> rates = CollectionUtils.emptyIfNull(businessObjectService .findMatchingOrderBy(TrainingStipendRate.class, criteria, "effectiveDate", false)); for (TrainingStipendRate rate : rates) { if (rate.getEffectiveDate() != null) { final LocalDate limit = LocalDate.fromDateFields(effectiveDate); final LocalDate rateDate = LocalDate.fromDateFields(rate.getEffectiveDate()); if (rateDate.isBefore(limit) || rateDate.isEqual(limit)) { return rate; } } } return null; }