Search in sources :

Example 1 with LoanPenaltyScheduleEntity

use of org.mifos.accounts.loan.business.LoanPenaltyScheduleEntity 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

ArrayList (java.util.ArrayList)1 LocalDate (org.joda.time.LocalDate)1 AccountPenaltiesEntity (org.mifos.accounts.business.AccountPenaltiesEntity)1 LoanBO (org.mifos.accounts.loan.business.LoanBO)1 LoanPenaltyScheduleEntity (org.mifos.accounts.loan.business.LoanPenaltyScheduleEntity)1 LoanScheduleEntity (org.mifos.accounts.loan.business.LoanScheduleEntity)1 BatchJobException (org.mifos.framework.components.batchjobs.exceptions.BatchJobException)1