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:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

public void loanApplicationSubmittal(final AppUser currentUser, final LoanScheduleModel loanSchedule,
        final LoanApplicationTerms loanApplicationTerms, final LoanLifecycleStateMachine lifecycleStateMachine,
        final LocalDate submittedOn, final String externalId, final boolean allowTransactionsOnHoliday,
        final List<Holiday> holidays, final WorkingDays workingDays,
        final boolean allowTransactionsOnNonWorkingDay, final LocalDate recalculationRestFrequencyDate,
        final LocalDate recalculationCompoundingFrequencyDate) {

    updateLoanSchedule(loanSchedule, currentUser);

    LoanStatus from = null;//  w  w w . j a  v a  2 s  .  co m
    if (this.loanStatus != null) {
        from = LoanStatus.fromInt(this.loanStatus);
    }

    final LoanStatus statusEnum = lifecycleStateMachine.transition(LoanEvent.LOAN_CREATED, from);
    this.loanStatus = statusEnum.getValue();

    this.externalId = externalId;
    this.termFrequency = loanApplicationTerms.getLoanTermFrequency();
    this.termPeriodFrequencyType = loanApplicationTerms.getLoanTermPeriodFrequencyType().getValue();
    this.submittedOnDate = submittedOn.toDate();
    this.submittedBy = currentUser;
    this.expectedDisbursementDate = loanApplicationTerms.getExpectedDisbursementDate().toDate();
    this.expectedFirstRepaymentOnDate = loanApplicationTerms.getRepaymentStartFromDate();
    this.interestChargedFromDate = loanApplicationTerms.getInterestChargedFromDate();

    if (loanApplicationTerms.getRepaymentPeriodFrequencyType() == PeriodFrequencyType.MONTHS) {
        this.repaymentFrequencyNthDayType = loanApplicationTerms.getNthDay();
        this.repaymentFrequencyDayOfWeekType = loanApplicationTerms.getWeekDayType().getValue();
    } else {
        this.repaymentFrequencyNthDayType = NthDayType.INVALID.getValue();
        this.repaymentFrequencyDayOfWeekType = DayOfWeekType.INVALID.getValue();
    }

    updateLoanScheduleDependentDerivedFields();

    if (submittedOn.isAfter(DateUtils.getLocalDateOfTenant())) {
        final String errorMessage = "The date on which a loan is submitted cannot be in the future.";
        throw new InvalidLoanStateTransitionException("submittal", "cannot.be.a.future.date", errorMessage,
                submittedOn);
    }

    if (this.client != null && this.client.isActivatedAfter(submittedOn)) {
        final String errorMessage = "The date on which a loan is submitted cannot be earlier than client's activation date.";
        throw new InvalidLoanStateTransitionException("submittal", "cannot.be.before.client.activation.date",
                errorMessage, submittedOn);
    }

    validateActivityNotBeforeClientOrGroupTransferDate(LoanEvent.LOAN_CREATED, submittedOn);

    if (this.group != null && this.group.isActivatedAfter(submittedOn)) {
        final String errorMessage = "The date on which a loan is submitted cannot be earlier than groups's activation date.";
        throw new InvalidLoanStateTransitionException("submittal", "cannot.be.before.group.activation.date",
                errorMessage, submittedOn);
    }

    if (submittedOn.isAfter(getExpectedDisbursedOnLocalDate())) {
        final String errorMessage = "The date on which a loan is submitted cannot be after its expected disbursement date: "
                + getExpectedDisbursedOnLocalDate().toString();
        throw new InvalidLoanStateTransitionException("submittal", "cannot.be.after.expected.disbursement.date",
                errorMessage, submittedOn, getExpectedDisbursedOnLocalDate());
    }

    // charges are optional
    int penaltyWaitPeriod = 0;
    for (final LoanCharge loanCharge : charges()) {
        recalculateLoanCharge(loanCharge, penaltyWaitPeriod);
    }

    updateSummaryWithTotalFeeChargesDueAtDisbursement(deriveSumTotalOfChargesDueAtDisbursement());

    // validate if disbursement date is a holiday or a non-working day
    validateDisbursementDateIsOnNonWorkingDay(workingDays, allowTransactionsOnNonWorkingDay);
    validateDisbursementDateIsOnHoliday(allowTransactionsOnHoliday, holidays);

    /**
     * Copy interest recalculation settings if interest recalculation is
     * enabled
     */
    if (this.loanRepaymentScheduleDetail.isInterestRecalculationEnabled()) {
        Date restFrequencyDate = null;
        if (recalculationRestFrequencyDate != null) {
            restFrequencyDate = recalculationRestFrequencyDate.toDate();
        }

        Date compoundingFrequencyDate = null;
        if (recalculationCompoundingFrequencyDate != null) {
            compoundingFrequencyDate = recalculationCompoundingFrequencyDate.toDate();
        }
        this.loanInterestRecalculationDetails = LoanInterestRecalculationDetails.createFrom(
                this.loanProduct.getProductInterestRecalculationDetails()
                        .getInterestRecalculationCompoundingMethod(),
                this.loanProduct.getProductInterestRecalculationDetails().getRescheduleStrategyMethod(),
                this.loanProduct.getProductInterestRecalculationDetails().getRestFrequencyType().getValue(),
                this.loanProduct.getProductInterestRecalculationDetails().getRestInterval(), restFrequencyDate,
                this.loanProduct.getProductInterestRecalculationDetails().getCompoundingFrequencyType()
                        .getValue(),
                this.loanProduct.getProductInterestRecalculationDetails().getCompoundingInterval(),
                compoundingFrequencyDate);
        this.loanInterestRecalculationDetails.updateLoan(this);
    }

}

From source file:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

private void handleDisbursementTransaction(final LocalDate disbursedOn, final LocalDateTime createdDate,
        final AppUser currentUser) {

    // add repayment transaction to track incoming money from client to mfi
    // for (charges due at time of disbursement)

    /***/* ww w .j a  v  a 2s.c o  m*/
     * TODO Vishwas: do we need to be able to pass in payment type details
     * for repayments at disbursements too?
     ***/

    final Money totalFeeChargesDueAtDisbursement = this.summary
            .getTotalFeeChargesDueAtDisbursement(loanCurrency());
    /**
     * all Charges repaid at disbursal is marked as repaid and
     * "APPLY Charge" transactions are created for all other fees ( which
     * are created during disbursal but not repaid)
     **/

    Money disbursentMoney = Money.zero(getCurrency());
    final LoanTransaction chargesPayment = LoanTransaction.repaymentAtDisbursement(getOffice(), disbursentMoney,
            null, disbursedOn, null, createdDate, currentUser);
    final Integer installmentNumber = null;
    for (final LoanCharge charge : charges()) {
        Date actualDisbursementDate = getActualDisbursementDate(charge);
        if (charge.getCharge().getChargeTimeType() == ChargeTimeType.DISBURSEMENT.getValue()
                || (charge.getCharge().getChargeTimeType() == ChargeTimeType.TRANCHE_DISBURSEMENT.getValue()
                        && disbursedOn.equals(new LocalDate(actualDisbursementDate))
                        && actualDisbursementDate != null && !charge.isWaived() && !charge.isFullyPaid())) {
            if (totalFeeChargesDueAtDisbursement.isGreaterThanZero()
                    && !charge.getChargePaymentMode().isPaymentModeAccountTransfer()) {
                charge.markAsFullyPaid();
                // Add "Loan Charge Paid By" details to this transaction
                final LoanChargePaidBy loanChargePaidBy = new LoanChargePaidBy(chargesPayment, charge,
                        charge.amount(), installmentNumber);
                chargesPayment.getLoanChargesPaid().add(loanChargePaidBy);
                disbursentMoney = disbursentMoney.plus(charge.amount());
            }
        } else if (disbursedOn.equals(new LocalDate(this.actualDisbursementDate))) {
            /**
             * create a Charge applied transaction if Up front Accrual, None
             * or Cash based accounting is enabled
             **/
            if (isNoneOrCashOrUpfrontAccrualAccountingEnabledOnLoanProduct()) {
                handleChargeAppliedTransaction(charge, disbursedOn, currentUser);
            }
        }
    }

    if (disbursentMoney.isGreaterThanZero()) {
        final Money zero = Money.zero(getCurrency());
        chargesPayment.updateComponentsAndTotal(zero, zero, disbursentMoney, zero);
        chargesPayment.updateLoan(this);
        this.loanTransactions.add(chargesPayment);
        updateLoanOutstandingBalaces();
    }

    if (getApprovedOnDate() != null && disbursedOn.isBefore(getApprovedOnDate())) {
        final String errorMessage = "The date on which a loan is disbursed cannot be before its approval date: "
                + getApprovedOnDate().toString();
        throw new InvalidLoanStateTransitionException("disbursal", "cannot.be.before.approval.date",
                errorMessage, disbursedOn, getApprovedOnDate());
    }

    if (getExpectedFirstRepaymentOnDate() != null
            && (disbursedOn.isAfter(this.fetchRepaymentScheduleInstallment(1).getDueDate())
                    || disbursedOn.isAfter(getExpectedFirstRepaymentOnDate()))
            && disbursedOn.toDate().equals(this.actualDisbursementDate)) {
        final String errorMessage = "submittedOnDate cannot be after the loans  expectedFirstRepaymentOnDate: "
                + getExpectedFirstRepaymentOnDate().toString();
        throw new InvalidLoanStateTransitionException("disbursal",
                "cannot.be.after.expected.first.repayment.date", errorMessage, disbursedOn,
                getExpectedFirstRepaymentOnDate());
    }

    validateActivityNotBeforeClientOrGroupTransferDate(LoanEvent.LOAN_DISBURSED, disbursedOn);

    if (disbursedOn.isAfter(new LocalDate())) {
        final String errorMessage = "The date on which a loan with identifier : " + this.accountNumber
                + " is disbursed cannot be in the future.";
        throw new InvalidLoanStateTransitionException("disbursal", "cannot.be.a.future.date", errorMessage,
                disbursedOn);
    }

}

From source file:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

private ChangedTransactionDetail handleRepaymentOrRecoveryOrWaiverTransaction(
        final LoanTransaction loanTransaction, final LoanLifecycleStateMachine loanLifecycleStateMachine,
        final LoanTransaction adjustedTransaction, final ScheduleGeneratorDTO scheduleGeneratorDTO,
        final AppUser currentUser) {

    ChangedTransactionDetail changedTransactionDetail = null;

    LoanStatus statusEnum = null;/*ww  w.j a v a2 s .  c  o  m*/

    LocalDate recalculateFrom = loanTransaction.getTransactionDate();
    if (adjustedTransaction != null && adjustedTransaction.getTransactionDate().isBefore(recalculateFrom)) {
        recalculateFrom = adjustedTransaction.getTransactionDate();
    }

    if (loanTransaction.isRecoveryRepayment()) {
        statusEnum = loanLifecycleStateMachine.transition(LoanEvent.LOAN_RECOVERY_PAYMENT,
                LoanStatus.fromInt(this.loanStatus));
    } else {
        statusEnum = loanLifecycleStateMachine.transition(LoanEvent.LOAN_REPAYMENT_OR_WAIVER,
                LoanStatus.fromInt(this.loanStatus));
    }

    this.loanStatus = statusEnum.getValue();

    loanTransaction.updateLoan(this);

    final boolean isTransactionChronologicallyLatest = isChronologicallyLatestRepaymentOrWaiver(loanTransaction,
            this.loanTransactions);

    if (loanTransaction.isNotZero(loanCurrency())) {
        this.loanTransactions.add(loanTransaction);
    }

    if (loanTransaction.isNotRepayment() && loanTransaction.isNotWaiver()
            && loanTransaction.isNotRecoveryRepayment()) {
        final String errorMessage = "A transaction of type repayment or recovery repayment or waiver was expected but not received.";
        throw new InvalidLoanTransactionTypeException("transaction",
                "is.not.a.repayment.or.waiver.or.recovery.transaction", errorMessage);
    }

    final LocalDate loanTransactionDate = loanTransaction.getTransactionDate();
    if (loanTransactionDate.isBefore(getDisbursementDate())) {
        final String errorMessage = "The transaction date cannot be before the loan disbursement date: "
                + getApprovedOnDate().toString();
        throw new InvalidLoanStateTransitionException("transaction", "cannot.be.before.disbursement.date",
                errorMessage, loanTransactionDate, getDisbursementDate());
    }

    if (loanTransactionDate.isAfter(DateUtils.getLocalDateOfTenant())) {
        final String errorMessage = "The transaction date cannot be in the future.";
        throw new InvalidLoanStateTransitionException("transaction", "cannot.be.a.future.date", errorMessage,
                loanTransactionDate);
    }

    if (loanTransaction.isInterestWaiver()) {
        Money totalInterestOutstandingOnLoan = getTotalInterestOutstandingOnLoan();
        if (adjustedTransaction != null) {
            totalInterestOutstandingOnLoan = totalInterestOutstandingOnLoan
                    .plus(adjustedTransaction.getAmount(loanCurrency()));
        }
        if (loanTransaction.getAmount(loanCurrency()).isGreaterThan(totalInterestOutstandingOnLoan)) {
            final String errorMessage = "The amount of interest to waive cannot be greater than total interest outstanding on loan.";
            throw new InvalidLoanStateTransitionException("waive.interest",
                    "amount.exceeds.total.outstanding.interest", errorMessage,
                    loanTransaction.getAmount(loanCurrency()), totalInterestOutstandingOnLoan.getAmount());
        }
    }

    if (this.loanProduct.isMultiDisburseLoan() && adjustedTransaction == null) {
        BigDecimal totalDisbursed = getDisbursedAmount();
        if (totalDisbursed.compareTo(this.summary.getTotalPrincipalRepaid()) < 0) {
            final String errorMessage = "The transaction cannot be done before the loan disbursement: "
                    + getApprovedOnDate().toString();
            throw new InvalidLoanStateTransitionException("transaction", "cannot.be.done.before.disbursement",
                    errorMessage);
        }
    }

    final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor = this.transactionProcessorFactory
            .determineProcessor(this.transactionProcessingStrategy);

    final LoanRepaymentScheduleInstallment currentInstallment = fetchLoanRepaymentScheduleInstallment(
            loanTransaction.getTransactionDate());
    boolean reprocess = true;

    if (isTransactionChronologicallyLatest && adjustedTransaction == null
            && loanTransaction.getTransactionDate().isEqual(LocalDate.now()) && currentInstallment != null
            && currentInstallment.getTotalOutstanding(getCurrency())
                    .isEqualTo(loanTransaction.getAmount(getCurrency()))) {
        reprocess = false;
    }

    if (isTransactionChronologicallyLatest && adjustedTransaction == null
            && (!reprocess || !this.repaymentScheduleDetail().isInterestRecalculationEnabled())) {
        loanRepaymentScheduleTransactionProcessor.handleTransaction(loanTransaction, getCurrency(),
                this.repaymentScheduleInstallments, charges());
        reprocess = false;
        if (this.repaymentScheduleDetail().isInterestRecalculationEnabled()) {
            if (currentInstallment == null || currentInstallment.isNotFullyPaidOff()) {
                reprocess = true;
            } else {
                final LoanRepaymentScheduleInstallment nextInstallment = fetchRepaymentScheduleInstallment(
                        currentInstallment.getInstallmentNumber() + 1);
                if (nextInstallment != null
                        && nextInstallment.getTotalPaidInAdvance(getCurrency()).isGreaterThanZero()) {
                    reprocess = true;
                }
            }
        }
    }
    if (reprocess) {
        if (this.repaymentScheduleDetail().isInterestRecalculationEnabled()) {
            regenerateRepaymentScheduleWithInterestRecalculation(scheduleGeneratorDTO, currentUser);
        }
        final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retreiveListOfTransactionsPostDisbursement();
        changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.handleTransaction(
                getDisbursementDate(), allNonContraTransactionsPostDisbursement, getCurrency(),
                this.repaymentScheduleInstallments, charges());
        for (final Map.Entry<Long, LoanTransaction> mapEntry : changedTransactionDetail
                .getNewTransactionMappings().entrySet()) {
            mapEntry.getValue().updateLoan(this);
        }
        /***
         * Commented since throwing exception if external id present for one
         * of the transactions. for this need to save the reversed
         * transactions first and then new transactions.
         */
        this.loanTransactions.addAll(changedTransactionDetail.getNewTransactionMappings().values());
    }

    updateLoanSummaryDerivedFields();

    /**
     * FIXME: Vishwas, skipping post loan transaction checks for Loan
     * recoveries
     **/
    if (loanTransaction.isNotRecoveryRepayment()) {
        doPostLoanTransactionChecks(loanTransaction.getTransactionDate(), loanLifecycleStateMachine);
    }

    if (this.loanProduct.isMultiDisburseLoan()) {
        BigDecimal totalDisbursed = getDisbursedAmount();
        if (totalDisbursed.compareTo(this.summary.getTotalPrincipalRepaid()) < 0
                && this.repaymentScheduleDetail().getPrincipal().minus(totalDisbursed).isGreaterThanZero()) {
            final String errorMessage = "The transaction cannot be done before the loan disbursement: "
                    + getApprovedOnDate().toString();
            throw new InvalidLoanStateTransitionException("transaction", "cannot.be.done.before.disbursement",
                    errorMessage);
        }
    }

    if (changedTransactionDetail != null) {
        this.loanTransactions.removeAll(changedTransactionDetail.getNewTransactionMappings().values());
    }
    return changedTransactionDetail;
}

From source file:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

public LocalDate possibleNextRepaymentDate() {
    LocalDate earliestUnpaidInstallmentDate = new LocalDate();
    for (final LoanRepaymentScheduleInstallment installment : this.repaymentScheduleInstallments) {
        if (installment.isNotFullyPaidOff()) {
            earliestUnpaidInstallmentDate = installment.getDueDate();
            break;
        }// ww  w  .j  av a 2s .  c o  m
    }

    LocalDate lastTransactionDate = null;
    for (final LoanTransaction transaction : this.loanTransactions) {
        if (transaction.isRepayment() && transaction.isNonZero()) {
            lastTransactionDate = transaction.getTransactionDate();
        }
    }

    LocalDate possibleNextRepaymentDate = earliestUnpaidInstallmentDate;
    if (lastTransactionDate != null && lastTransactionDate.isAfter(earliestUnpaidInstallmentDate)) {
        possibleNextRepaymentDate = lastTransactionDate;
    }

    return possibleNextRepaymentDate;
}

From source file:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

public ChangedTransactionDetail closeAsWrittenOff(final JsonCommand command,
        final LoanLifecycleStateMachine loanLifecycleStateMachine, final Map<String, Object> changes,
        final List<Long> existingTransactionIds, final List<Long> existingReversedTransactionIds,
        final AppUser currentUser, final ScheduleGeneratorDTO scheduleGeneratorDTO) {

    final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor = this.transactionProcessorFactory
            .determineProcessor(this.transactionProcessingStrategy);
    ChangedTransactionDetail changedTransactionDetail = closeDisbursements(scheduleGeneratorDTO,
            loanRepaymentScheduleTransactionProcessor, currentUser);

    validateAccountStatus(LoanEvent.WRITE_OFF_OUTSTANDING);

    final LoanStatus statusEnum = loanLifecycleStateMachine.transition(LoanEvent.WRITE_OFF_OUTSTANDING,
            LoanStatus.fromInt(this.loanStatus));

    LoanTransaction loanTransaction = null;
    if (!statusEnum.hasStateOf(LoanStatus.fromInt(this.loanStatus))) {
        this.loanStatus = statusEnum.getValue();
        changes.put("status", LoanEnumerations.status(this.loanStatus));

        existingTransactionIds.addAll(findExistingTransactionIds());
        existingReversedTransactionIds.addAll(findExistingReversedTransactionIds());

        final LocalDate writtenOffOnLocalDate = command.localDateValueOfParameterNamed("transactionDate");
        final String txnExternalId = command.stringValueOfParameterNamedAllowingNull("externalId");

        this.closedOnDate = writtenOffOnLocalDate.toDate();
        this.writtenOffOnDate = writtenOffOnLocalDate.toDate();
        this.closedBy = currentUser;
        changes.put("closedOnDate", command.stringValueOfParameterNamed("transactionDate"));
        changes.put("writtenOffOnDate", command.stringValueOfParameterNamed("transactionDate"));

        if (writtenOffOnLocalDate.isBefore(getDisbursementDate())) {
            final String errorMessage = "The date on which a loan is written off cannot be before the loan disbursement date: "
                    + getDisbursementDate().toString();
            throw new InvalidLoanStateTransitionException("writeoff", "cannot.be.before.submittal.date",
                    errorMessage, writtenOffOnLocalDate, getDisbursementDate());
        }//from ww w. j ava  2 s .  co m

        validateActivityNotBeforeClientOrGroupTransferDate(LoanEvent.WRITE_OFF_OUTSTANDING,
                writtenOffOnLocalDate);

        if (writtenOffOnLocalDate.isAfter(DateUtils.getLocalDateOfTenant())) {
            final String errorMessage = "The date on which a loan is written off cannot be in the future.";
            throw new InvalidLoanStateTransitionException("writeoff", "cannot.be.a.future.date", errorMessage,
                    writtenOffOnLocalDate);
        }

        LocalDateTime createdDate = DateUtils.getLocalDateTimeOfTenant();
        loanTransaction = LoanTransaction.writeoff(this, getOffice(), writtenOffOnLocalDate, txnExternalId,
                createdDate, currentUser);
        LocalDate lastTransactionDate = getLastUserTransactionDate();
        if (lastTransactionDate.isAfter(writtenOffOnLocalDate)) {
            final String errorMessage = "The date of the writeoff transaction must occur on or before previous transactions.";
            throw new InvalidLoanStateTransitionException("writeoff",
                    "must.occur.on.or.after.other.transaction.dates", errorMessage, writtenOffOnLocalDate);
        }

        this.loanTransactions.add(loanTransaction);

        loanRepaymentScheduleTransactionProcessor.handleWriteOff(loanTransaction, loanCurrency(),
                this.repaymentScheduleInstallments);

        updateLoanSummaryDerivedFields();
    }
    if (changedTransactionDetail == null) {
        changedTransactionDetail = new ChangedTransactionDetail();
    }
    changedTransactionDetail.getNewTransactionMappings().put(0L, loanTransaction);
    return changedTransactionDetail;
}

From source file:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

public ChangedTransactionDetail close(final JsonCommand command,
        final LoanLifecycleStateMachine loanLifecycleStateMachine, final Map<String, Object> changes,
        final List<Long> existingTransactionIds, final List<Long> existingReversedTransactionIds,
        final ScheduleGeneratorDTO scheduleGeneratorDTO, final AppUser currentUser) {

    validateAccountStatus(LoanEvent.LOAN_CLOSED);

    existingTransactionIds.addAll(findExistingTransactionIds());
    existingReversedTransactionIds.addAll(findExistingReversedTransactionIds());

    final LocalDate closureDate = command.localDateValueOfParameterNamed("transactionDate");
    final String txnExternalId = command.stringValueOfParameterNamedAllowingNull("externalId");

    this.closedOnDate = closureDate.toDate();
    changes.put("closedOnDate", command.stringValueOfParameterNamed("transactionDate"));

    validateActivityNotBeforeClientOrGroupTransferDate(LoanEvent.REPAID_IN_FULL, closureDate);
    if (closureDate.isBefore(getDisbursementDate())) {
        final String errorMessage = "The date on which a loan is closed cannot be before the loan disbursement date: "
                + getDisbursementDate().toString();
        throw new InvalidLoanStateTransitionException("close", "cannot.be.before.submittal.date", errorMessage,
                closureDate, getDisbursementDate());
    }//from ww  w.  ja  v  a2 s  . c  o  m

    if (closureDate.isAfter(DateUtils.getLocalDateOfTenant())) {
        final String errorMessage = "The date on which a loan is closed cannot be in the future.";
        throw new InvalidLoanStateTransitionException("close", "cannot.be.a.future.date", errorMessage,
                closureDate);
    }
    final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor = this.transactionProcessorFactory
            .determineProcessor(this.transactionProcessingStrategy);
    ChangedTransactionDetail changedTransactionDetail = closeDisbursements(scheduleGeneratorDTO,
            loanRepaymentScheduleTransactionProcessor, currentUser);

    LoanTransaction loanTransaction = null;
    if (isOpen()) {
        final Money totalOutstanding = this.summary.getTotalOutstanding(loanCurrency());
        if (totalOutstanding.isGreaterThanZero()
                && getInArrearsTolerance().isGreaterThanOrEqualTo(totalOutstanding)) {

            final LoanStatus statusEnum = loanLifecycleStateMachine.transition(LoanEvent.REPAID_IN_FULL,
                    LoanStatus.fromInt(this.loanStatus));
            if (!statusEnum.hasStateOf(LoanStatus.fromInt(this.loanStatus))) {
                this.loanStatus = statusEnum.getValue();
                changes.put("status", LoanEnumerations.status(this.loanStatus));
            }
            this.closedOnDate = closureDate.toDate();
            loanTransaction = LoanTransaction.writeoff(this, getOffice(), closureDate, txnExternalId,
                    DateUtils.getLocalDateTimeOfTenant(), currentUser);
            final boolean isLastTransaction = isChronologicallyLatestTransaction(loanTransaction,
                    this.loanTransactions);
            if (!isLastTransaction) {
                final String errorMessage = "The closing date of the loan must be on or after latest transaction date.";
                throw new InvalidLoanStateTransitionException("close.loan",
                        "must.occur.on.or.after.latest.transaction.date", errorMessage, closureDate);
            }

            this.loanTransactions.add(loanTransaction);

            loanRepaymentScheduleTransactionProcessor.handleWriteOff(loanTransaction, loanCurrency(),
                    this.repaymentScheduleInstallments);

            updateLoanSummaryDerivedFields();
        } else if (totalOutstanding.isGreaterThanZero()) {
            final String errorMessage = "A loan with money outstanding cannot be closed";
            throw new InvalidLoanStateTransitionException("close", "loan.has.money.outstanding", errorMessage,
                    totalOutstanding.toString());
        }
    }

    if (isOverPaid()) {
        final Money totalLoanOverpayment = calculateTotalOverpayment();
        if (totalLoanOverpayment.isGreaterThanZero()
                && getInArrearsTolerance().isGreaterThanOrEqualTo(totalLoanOverpayment)) {
            // TODO - KW - technically should set somewhere that this loan
            // has
            // 'overpaid' amount
            final LoanStatus statusEnum = loanLifecycleStateMachine.transition(LoanEvent.REPAID_IN_FULL,
                    LoanStatus.fromInt(this.loanStatus));
            if (!statusEnum.hasStateOf(LoanStatus.fromInt(this.loanStatus))) {
                this.loanStatus = statusEnum.getValue();
                changes.put("status", LoanEnumerations.status(this.loanStatus));
            }
            this.closedOnDate = closureDate.toDate();
        } else if (totalLoanOverpayment.isGreaterThanZero()) {
            final String errorMessage = "The loan is marked as 'Overpaid' and cannot be moved to 'Closed (obligations met).";
            throw new InvalidLoanStateTransitionException("close", "loan.is.overpaid", errorMessage,
                    totalLoanOverpayment.toString());
        }
    }

    if (changedTransactionDetail == null) {
        changedTransactionDetail = new ChangedTransactionDetail();
    }
    changedTransactionDetail.getNewTransactionMappings().put(0L, loanTransaction);
    return changedTransactionDetail;
}

From source file:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

public void updateLoanRepaymentScheduleDates(final LocalDate meetingStartDate, final String recuringRule,
        final boolean isHolidayEnabled, final List<Holiday> holidays, final WorkingDays workingDays,
        final Boolean reschedulebasedOnMeetingDates, final LocalDate presentMeetingDate,
        final LocalDate newMeetingDate) {

    // first repayment's from date is same as disbursement date.
    /*/*  ww  w. j  a v  a 2s.  co  m*/
     * meetingStartDate is used as seedDate Capture the seedDate from user
     * and use the seedDate as meetingStart date
     */

    LocalDate tmpFromDate = getDisbursementDate();
    final PeriodFrequencyType repaymentPeriodFrequencyType = this.loanRepaymentScheduleDetail
            .getRepaymentPeriodFrequencyType();
    final Integer loanRepaymentInterval = this.loanRepaymentScheduleDetail.getRepayEvery();
    final String frequency = CalendarUtils
            .getMeetingFrequencyFromPeriodFrequencyType(repaymentPeriodFrequencyType);

    LocalDate newRepaymentDate = null;
    Boolean isFirstTime = true;
    LocalDate latestRepaymentDate = null;
    for (final LoanRepaymentScheduleInstallment loanRepaymentScheduleInstallment : this.repaymentScheduleInstallments) {

        LocalDate oldDueDate = loanRepaymentScheduleInstallment.getDueDate();

        if (oldDueDate.isEqual(presentMeetingDate) || oldDueDate.isAfter(presentMeetingDate)) {

            if (isFirstTime) {

                isFirstTime = false;
                newRepaymentDate = newMeetingDate;

            } else {
                // tmpFromDate.plusDays(1) is done to make sure
                // getNewRepaymentMeetingDate method returns next meeting
                // date and not the same as tmpFromDate
                newRepaymentDate = CalendarUtils.getNewRepaymentMeetingDate(recuringRule, tmpFromDate,
                        tmpFromDate.plusDays(1), loanRepaymentInterval, frequency, workingDays);
            }

            if (isHolidayEnabled) {
                newRepaymentDate = HolidayUtil.getRepaymentRescheduleDateToIfHoliday(newRepaymentDate,
                        holidays);
            }
            if (latestRepaymentDate == null || latestRepaymentDate.isBefore(newRepaymentDate)) {
                latestRepaymentDate = newRepaymentDate;
            }
            loanRepaymentScheduleInstallment.updateDueDate(newRepaymentDate);
            // reset from date to get actual daysInPeriod

            if (!isFirstTime) {
                loanRepaymentScheduleInstallment.updateFromDate(tmpFromDate);
            }

            tmpFromDate = newRepaymentDate;// update with new repayment
            // date
        } else {
            tmpFromDate = oldDueDate;
        }
    }
    if (latestRepaymentDate != null) {
        this.expectedMaturityDate = latestRepaymentDate.toDate();
    }
}

From source file:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

public void updateLoanRepaymentScheduleDates(final LocalDate meetingStartDate, final String recuringRule,
        final boolean isHolidayEnabled, final List<Holiday> holidays, final WorkingDays workingDays) {

    // first repayment's from date is same as disbursement date.
    LocalDate tmpFromDate = getDisbursementDate();
    final PeriodFrequencyType repaymentPeriodFrequencyType = this.loanRepaymentScheduleDetail
            .getRepaymentPeriodFrequencyType();
    final Integer loanRepaymentInterval = this.loanRepaymentScheduleDetail.getRepayEvery();
    final String frequency = CalendarUtils
            .getMeetingFrequencyFromPeriodFrequencyType(repaymentPeriodFrequencyType);

    LocalDate newRepaymentDate = null;
    LocalDate seedDate = meetingStartDate;
    LocalDate latestRepaymentDate = null;
    for (final LoanRepaymentScheduleInstallment loanRepaymentScheduleInstallment : this.repaymentScheduleInstallments) {

        LocalDate oldDueDate = loanRepaymentScheduleInstallment.getDueDate();

        // FIXME: AA this won't update repayment dates before current date.

        if (oldDueDate.isAfter(seedDate) && oldDueDate.isAfter(DateUtils.getLocalDateOfTenant())) {

            newRepaymentDate = CalendarUtils.getNewRepaymentMeetingDate(recuringRule, seedDate, oldDueDate,
                    loanRepaymentInterval, frequency, workingDays);

            final LocalDate maxDateLimitForNewRepayment = getMaxDateLimitForNewRepayment(
                    repaymentPeriodFrequencyType, loanRepaymentInterval, tmpFromDate);

            if (newRepaymentDate.isAfter(maxDateLimitForNewRepayment)) {
                newRepaymentDate = CalendarUtils.getNextRepaymentMeetingDate(recuringRule, seedDate,
                        tmpFromDate, loanRepaymentInterval, frequency, workingDays);
            }//from  ww  w.j a v a 2 s  .c o  m

            if (isHolidayEnabled) {
                newRepaymentDate = HolidayUtil.getRepaymentRescheduleDateToIfHoliday(newRepaymentDate,
                        holidays);
            }
            if (latestRepaymentDate == null || latestRepaymentDate.isBefore(newRepaymentDate)) {
                latestRepaymentDate = newRepaymentDate;
            }

            loanRepaymentScheduleInstallment.updateDueDate(newRepaymentDate);
            // reset from date to get actual daysInPeriod
            loanRepaymentScheduleInstallment.updateFromDate(tmpFromDate);
            tmpFromDate = newRepaymentDate;// update with new repayment
            // date
        } else {
            tmpFromDate = oldDueDate;
        }
    }
    if (latestRepaymentDate != null) {
        this.expectedMaturityDate = latestRepaymentDate.toDate();
    }
}

From source file:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

public void applyHolidayToRepaymentScheduleDates(final Holiday holiday) {
    // first repayment's from date is same as disbursement date.
    LocalDate tmpFromDate = getDisbursementDate();
    // Loop through all loanRepayments
    for (final LoanRepaymentScheduleInstallment loanRepaymentScheduleInstallment : this.repaymentScheduleInstallments) {
        final LocalDate oldDueDate = loanRepaymentScheduleInstallment.getDueDate();

        // update from date if it's not same as previous installament's due
        // date.//  w  w  w.j a v  a2 s  . co  m
        if (!loanRepaymentScheduleInstallment.getFromDate().isEqual(tmpFromDate)) {
            loanRepaymentScheduleInstallment.updateFromDate(tmpFromDate);
        }
        if (oldDueDate.isAfter(holiday.getToDateLocalDate())) {
            break;
        }

        if (oldDueDate.equals(holiday.getFromDateLocalDate()) || oldDueDate.equals(holiday.getToDateLocalDate())
                || oldDueDate.isAfter(holiday.getFromDateLocalDate())
                        && oldDueDate.isBefore(holiday.getToDateLocalDate())) {
            // FIXME: AA do we need to apply non-working days.
            // Assuming holiday's repayment reschedule to date cannot be
            // created on a non-working day.
            final LocalDate newRepaymentDate = holiday.getRepaymentsRescheduledToLocalDate();
            loanRepaymentScheduleInstallment.updateDueDate(newRepaymentDate);
        }
        tmpFromDate = loanRepaymentScheduleInstallment.getDueDate();
    }
}

From source file:org.apache.fineract.portfolio.loanaccount.domain.Loan.java

License:Apache License

public void regenerateRepaymentScheduleWithInterestRecalculation(final ScheduleGeneratorDTO generatorDTO,
        final AppUser currentUser) {

    LocalDate lastTransactionDate = getLastUserTransactionDate();
    final LoanScheduleDTO loanSchedule = getRecalculatedSchedule(generatorDTO);
    if (loanSchedule == null) {
        return;/*  w w w .  j  av  a  2  s .co  m*/
    }
    updateLoanSchedule(loanSchedule.getInstallments(), currentUser);
    this.interestRecalculatedOn = DateUtils.getDateOfTenant();
    LocalDate lastRepaymentDate = this.getLastRepaymentPeriodDueDate();
    Set<LoanCharge> charges = this.charges();
    for (LoanCharge loanCharge : charges) {
        if (!loanCharge.isDueAtDisbursement()) {
            updateOverdueScheduleInstallment(loanCharge);
            if (loanCharge.getDueLocalDate() == null
                    || (!lastRepaymentDate.isBefore(loanCharge.getDueLocalDate()))) {
                if (!loanCharge.isWaived() && (loanCharge.getDueLocalDate() == null
                        || !lastTransactionDate.isAfter(loanCharge.getDueLocalDate()))) {
                    recalculateLoanCharge(loanCharge, generatorDTO.getPenaltyWaitPeriod());
                    loanCharge.updateWaivedAmount(getCurrency());
                }
            } else {
                loanCharge.setActive(false);
            }
        }
    }

    processPostDisbursementTransactions();
}