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);
}
}
Aggregations