Search in sources :

Example 1 with LoanScheduleEntity

use of org.mifos.accounts.loan.business.LoanScheduleEntity in project head by mifos.

the class LoanAccountServiceFacadeWebTier method getUpcomingInstallmentDetailsForGroupLoan.

private InstallmentDetailsDto getUpcomingInstallmentDetailsForGroupLoan(final List<AccountActionDateEntity> upcomingAccountActionDate, final MifosCurrency currency) {
    if (upcomingAccountActionDate != null && !upcomingAccountActionDate.isEmpty()) {
        Money subTotal = new Money(Money.getDefaultCurrency());
        Money principalDue = new Money(Money.getDefaultCurrency());
        Money interestDue = new Money(Money.getDefaultCurrency());
        Money totalFeesDueWithMiscFee = new Money(Money.getDefaultCurrency());
        Money penaltyDue = new Money(Money.getDefaultCurrency());
        for (AccountActionDateEntity accAction : upcomingAccountActionDate) {
            LoanScheduleEntity upcomingInstallment = (LoanScheduleEntity) accAction;
            principalDue = principalDue.add(upcomingInstallment.getPenaltyDue());
            interestDue = interestDue.add(upcomingInstallment.getInterestDue());
            totalFeesDueWithMiscFee = totalFeesDueWithMiscFee.add(upcomingInstallment.getTotalFeeDueWithMiscFeeDue());
            penaltyDue = penaltyDue.add(upcomingInstallment.getPenaltyDue());
            subTotal = upcomingInstallment.getPrincipalDue().add(upcomingInstallment.getInterestDue()).add(upcomingInstallment.getTotalFeesDueWithMiscFee()).add(upcomingInstallment.getPenaltyDue());
        }
        return new InstallmentDetailsDto(principalDue.toString(), interestDue.toString(), totalFeesDueWithMiscFee.toString(), penaltyDue.toString(), subTotal.toString());
    }
    String zero = new Money(currency).toString();
    return new InstallmentDetailsDto(zero, zero, zero, zero, zero);
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) OriginalLoanScheduleEntity(org.mifos.accounts.loan.business.OriginalLoanScheduleEntity) Money(org.mifos.framework.util.helpers.Money) AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) InstallmentDetailsDto(org.mifos.dto.domain.InstallmentDetailsDto) LoanInstallmentDetailsDto(org.mifos.dto.domain.LoanInstallmentDetailsDto)

Example 2 with LoanScheduleEntity

use of org.mifos.accounts.loan.business.LoanScheduleEntity in project head by mifos.

the class LoanAccountServiceFacadeWebTier method interestDueForNextInstallment.

BigDecimal interestDueForNextInstallment(BigDecimal totalRepaymentAmount, BigDecimal waivedAmount, LoanBO loan, boolean waiveInterest) {
    BigDecimal result = BigDecimal.ZERO;
    if (!waiveInterest) {
        if (loan.isDecliningBalanceInterestRecalculation()) {
            result = totalRepaymentAmount.subtract(waivedAmount);
        } else {
            AccountActionDateEntity nextInstallment = loan.getDetailsOfNextInstallment();
            if (nextInstallment != null) {
                LoanScheduleEntity loanScheduleEntity = (LoanScheduleEntity) nextInstallment;
                result = loanScheduleEntity.getInterestDue().getAmount();
            }
        }
    }
    return result;
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) OriginalLoanScheduleEntity(org.mifos.accounts.loan.business.OriginalLoanScheduleEntity) AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) BigDecimal(java.math.BigDecimal)

Example 3 with LoanScheduleEntity

use of org.mifos.accounts.loan.business.LoanScheduleEntity in project head by mifos.

the class LoanAccountServiceFacadeWebTier method updateMemberLoansFeeAmounts.

@Override
public void updateMemberLoansFeeAmounts(Integer accountId) {
    LoanBO loan = this.loanDao.findById(accountId);
    List<LoanBO> individualLoans = this.loanDao.findIndividualLoans(accountId);
    try {
        transactionHelper.startTransaction();
        Map<Integer, LoanScheduleEntity> parentScheduleEntities = loan.getLoanScheduleEntityMap();
        for (Integer installmentId : parentScheduleEntities.keySet()) {
            LoanScheduleEntity parentEntity = parentScheduleEntities.get(installmentId);
            Map<Short, BigDecimal> feeAmountsForInstallment = new HashMap<Short, BigDecimal>();
            for (AccountFeesActionDetailEntity feesActionDetailEntity : parentEntity.getAccountFeesActionDetails()) {
                feeAmountsForInstallment.put(feesActionDetailEntity.getFee().getFeeId(), feesActionDetailEntity.getFeeAmount().getAmount());
            }
            for (int i = 0; i < individualLoans.size(); i++) {
                LoanScheduleEntity memberEntity = individualLoans.get(i).getLoanScheduleEntityMap().get(installmentId);
                for (AccountFeesActionDetailEntity feesActionDetailEntity : memberEntity.getAccountFeesActionDetails()) {
                    if (feesActionDetailEntity.getFee().getFeeType().equals(RateAmountFlag.RATE)) {
                        continue;
                    }
                    BigDecimal currentAmount = feeAmountsForInstallment.get(feesActionDetailEntity.getFee().getFeeId());
                    currentAmount = currentAmount.subtract(feesActionDetailEntity.getFeeAmount().getAmount());
                    if (currentAmount.compareTo(BigDecimal.ZERO) != 0 && i == individualLoans.size() - 1) {
                        BigDecimal toUpdate = feesActionDetailEntity.getFeeAmount().getAmount().add(currentAmount);
                        feesActionDetailEntity.updateFeeAmount(toUpdate);
                        currentAmount = BigDecimal.ZERO;
                    }
                    feeAmountsForInstallment.put(feesActionDetailEntity.getFee().getFeeId(), currentAmount);
                }
            }
        }
        for (LoanBO memberLoan : individualLoans) {
            memberLoan.updateLoanSummary();
            loanDao.save(memberLoan);
        }
        loanDao.save(loan);
        transactionHelper.flushSession();
        this.loanBusinessService.clearAndPersistOriginalSchedule(loan);
        for (LoanBO memberLoan : individualLoans) {
            this.loanBusinessService.clearAndPersistOriginalSchedule(memberLoan);
        }
        transactionHelper.commitTransaction();
    } catch (BusinessRuleException e) {
        this.transactionHelper.rollbackTransaction();
        throw new BusinessRuleException(e.getMessageKey(), e);
    } catch (PersistenceException e) {
        throw new MifosRuntimeException();
    } finally {
        this.transactionHelper.closeSession();
    }
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) OriginalLoanScheduleEntity(org.mifos.accounts.loan.business.OriginalLoanScheduleEntity) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) AccountFeesActionDetailEntity(org.mifos.accounts.business.AccountFeesActionDetailEntity) LoanBO(org.mifos.accounts.loan.business.LoanBO) BigDecimal(java.math.BigDecimal) BusinessRuleException(org.mifos.service.BusinessRuleException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 4 with LoanScheduleEntity

use of org.mifos.accounts.loan.business.LoanScheduleEntity in project head by mifos.

the class LoanRepaymentTag method generateGroupSchedule.

private List<AccountActionDateEntity> generateGroupSchedule(List<LoanBO> memberAccounts, LoanBO parent) {
    Money principal = new Money(Money.getDefaultCurrency(), 0.0), principalPaid = new Money(Money.getDefaultCurrency(), 0.0), interest = new Money(Money.getDefaultCurrency(), 0.0), interestPaid = new Money(Money.getDefaultCurrency(), 0.0), penalty = new Money(Money.getDefaultCurrency(), 0.0), penaltyPaid = new Money(Money.getDefaultCurrency(), 0.0), extraInterest = new Money(Money.getDefaultCurrency(), 0.0), extraInterestPaid = new Money(Money.getDefaultCurrency(), 0.0), miscFee = new Money(Money.getDefaultCurrency(), 0.0), miscFeePaid = new Money(Money.getDefaultCurrency(), 0.0), miscPenalty = new Money(Money.getDefaultCurrency(), 0.0), miscPenaltyPaid = new Money(Money.getDefaultCurrency(), 0.0);
    List<List<AccountActionDateEntity>> accActionList = new ArrayList<List<AccountActionDateEntity>>();
    for (LoanBO memberLoan : memberAccounts) {
        accActionList.add(new ArrayList<AccountActionDateEntity>(memberLoan.getAccountActionDates()));
    }
    List<AccountActionDateEntity> newGroupSchedule = copyAndSetToZeroSchedule(principal, principalPaid, interest, interestPaid, penalty, extraInterest, extraInterestPaid, miscFee, miscFeePaid, miscPenalty, penaltyPaid, miscPenaltyPaid, new ArrayList<AccountActionDateEntity>(parent.getAccountActionDates()));
    for (List<AccountActionDateEntity> accList : accActionList) {
        for (int i = 0; i < accList.size(); i++) {
            LoanScheduleEntity schedule = (LoanScheduleEntity) accList.get(i);
            LoanScheduleEntity newSchedule = (LoanScheduleEntity) newGroupSchedule.get(i);
            principal = principal.add(schedule.getPrincipal());
            newSchedule.setPrincipal(newSchedule.getPrincipal().add(principal));
            principal = new Money(Money.getDefaultCurrency(), 0.0);
            principalPaid = principalPaid.add(schedule.getPrincipalPaid());
            newSchedule.setPrincipalPaid(newSchedule.getPrincipalPaid().add(principalPaid));
            principalPaid = new Money(Money.getDefaultCurrency(), 0.0);
            interest = interest.add(schedule.getInterest());
            newSchedule.setInterest(newSchedule.getInterest().add(interest));
            interest = new Money(Money.getDefaultCurrency(), 0.0);
            interestPaid = interestPaid.add(schedule.getInterestPaid());
            newSchedule.setInterestPaid(newSchedule.getInterestPaid().add(interestPaid));
            interestPaid = new Money(Money.getDefaultCurrency(), 0.0);
            penalty = penalty.add(schedule.getPenalty());
            newSchedule.setPenalty(newSchedule.getPenalty().add(penaltyPaid));
            penalty = new Money(Money.getDefaultCurrency(), 0.0);
            penaltyPaid = penaltyPaid.add(schedule.getPenaltyPaid());
            newSchedule.setPenaltyPaid(newSchedule.getPenaltyPaid().add(penaltyPaid));
            penaltyPaid = new Money(Money.getDefaultCurrency(), 0.0);
            extraInterest = extraInterest.add(schedule.getExtraInterest());
            newSchedule.setExtraInterest(newSchedule.getExtraInterest().add(extraInterest));
            extraInterest = new Money(Money.getDefaultCurrency(), 0.0);
            extraInterestPaid = extraInterestPaid.add(schedule.getExtraInterestPaid());
            newSchedule.setExtraInterestPaid(newSchedule.getExtraInterestPaid().add(extraInterestPaid));
            extraInterestPaid = new Money(Money.getDefaultCurrency(), 0.0);
            miscFee = miscFee.add(schedule.getMiscFee());
            newSchedule.setMiscFee(newSchedule.getMiscFee().add(miscFee));
            miscFee = new Money(Money.getDefaultCurrency(), 0.0);
            miscFeePaid = miscFeePaid.add(schedule.getMiscFeePaid());
            newSchedule.setMiscFeePaid(newSchedule.getMiscFeePaid().add(miscFeePaid));
            miscFeePaid = new Money(Money.getDefaultCurrency(), 0.0);
            miscPenalty = miscPenalty.add(schedule.getMiscPenalty());
            newSchedule.setMiscPenalty(newSchedule.getMiscPenalty().add(miscPenalty));
            miscPenalty = new Money(Money.getDefaultCurrency(), 0.0);
            miscPenaltyPaid = miscPenaltyPaid.add(schedule.getMiscPenaltyPaid());
            newSchedule.setMiscPenaltyPaid(newSchedule.getMiscPenaltyPaid().add(miscPenaltyPaid));
            miscPenaltyPaid = new Money(Money.getDefaultCurrency(), 0.0);
        }
    }
    return newGroupSchedule;
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) Money(org.mifos.framework.util.helpers.Money) AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) LoanBO(org.mifos.accounts.loan.business.LoanBO) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with LoanScheduleEntity

use of org.mifos.accounts.loan.business.LoanScheduleEntity in project head by mifos.

the class ApplyPenaltyToLoanAccountsHelper method execute.

@Override
public void execute(final long timeInMillis) throws BatchJobException {
    setCurrentDates(timeInMillis);
    List<String> errorList = new ArrayList<String>();
    List<LoanBO> loanAccounts;
    try {
        loanAccounts = getLoanAccounts();
    } catch (Exception e) {
        throw new BatchJobException(e);
    }
    if (loanAccounts != null && !loanAccounts.isEmpty()) {
        Integer loanAccountId = null;
        try {
            for (LoanBO loanAccount : loanAccounts) {
                loanAccountId = loanAccount.getAccountId();
                List<AccountPenaltiesEntity> penaltyEntities = new ArrayList<AccountPenaltiesEntity>(loanAccount.getAccountPenalties());
                for (AccountPenaltiesEntity penaltyEntity : penaltyEntities) {
                    List<LoanScheduleEntity> lateInstallments = loanAccount.getDetailsOfLateInstallmentsPeriod(new LocalDate(penaltyEntity.getCreatedDate()), currentLocalDate);
                    for (LoanScheduleEntity entity : lateInstallments) {
                        //check grace period for installment period type
                        if (penaltyEntity.getPenalty().getPeriodType().getPenaltyPeriod() == PenaltyPeriod.INSTALLMENTS && penaltyEntity.hasPeriodType()) {
                            if (lateInstallments.get(0).getInstallmentId().equals(entity.getInstallmentId()) && checkGracePeriodTypeInstallments(lateInstallments, penaltyEntity.getPenalty().getPeriodDuration())) {
                                continue;
                            }
                        } else //check grace period for daily period type
                        if (penaltyEntity.getPenalty().getPeriodType().getPenaltyPeriod() == PenaltyPeriod.DAYS && penaltyEntity.hasPeriodType()) {
                            if (checkGracePeriodTypeDays(entity, penaltyEntity.getPenalty().getPeriodDuration())) {
                                continue;
                            }
                        }
                        LoanPenaltyScheduleEntity penaltySchedule = entity.getPenaltyScheduleEntity(penaltyEntity.getPenalty().getPenaltyId());
                        if (checkPeriod(penaltyEntity, new LocalDate(entity.getActionDate().getTime())) || (penaltySchedule != null && penaltySchedule.isOn(currentLocalDate))) {
                            continue;
                        }
                        if (penaltyEntity.isAmountPenalty()) {
                            addAmountPenalty(penaltyEntity, loanAccount, entity);
                        } else {
                            addRatePenalty(penaltyEntity, loanAccount, entity);
                        }
                    }
                }
            }
        } catch (Exception e) {
            if (loanAccountId != null) {
                getLogger().error(String.format("ApplyPenaltyToLoanAccountsTask execute failed with exception %s: %s at loan account %s", e.getClass().getName(), e.getMessage(), loanAccountId.toString()), e);
                errorList.add(loanAccountId.toString());
            }
            StaticHibernateUtil.rollbackTransaction();
        } finally {
            StaticHibernateUtil.closeSession();
        }
    }
    if (!errorList.isEmpty()) {
        throw new BatchJobException(SchedulerConstants.FAILURE, errorList);
    }
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) LoanBO(org.mifos.accounts.loan.business.LoanBO) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) LoanPenaltyScheduleEntity(org.mifos.accounts.loan.business.LoanPenaltyScheduleEntity)

Aggregations

LoanScheduleEntity (org.mifos.accounts.loan.business.LoanScheduleEntity)54 Money (org.mifos.framework.util.helpers.Money)32 ArrayList (java.util.ArrayList)19 OriginalLoanScheduleEntity (org.mifos.accounts.loan.business.OriginalLoanScheduleEntity)18 Test (org.junit.Test)16 AccountActionDateEntity (org.mifos.accounts.business.AccountActionDateEntity)14 BigDecimal (java.math.BigDecimal)11 AccountFeesActionDetailEntity (org.mifos.accounts.business.AccountFeesActionDetailEntity)8 LoanBO (org.mifos.accounts.loan.business.LoanBO)8 AccountFeesEntity (org.mifos.accounts.business.AccountFeesEntity)7 Date (java.util.Date)5 CustomerBO (org.mifos.customers.business.CustomerBO)5 UserContext (org.mifos.security.util.UserContext)5 LocalDate (org.joda.time.LocalDate)4 AccountPenaltiesEntity (org.mifos.accounts.business.AccountPenaltiesEntity)4 OriginalLoanScheduleEntitiesMatcher (org.mifos.accounts.loan.business.matchers.OriginalLoanScheduleEntitiesMatcher)4 InstallmentDetailsDto (org.mifos.dto.domain.InstallmentDetailsDto)4 LoanInstallmentDetailsDto (org.mifos.dto.domain.LoanInstallmentDetailsDto)4 MifosUser (org.mifos.security.MifosUser)4 Date (java.sql.Date)3