final Collection<Loan> groupLoans = this.loanRepository.findByGroupId(groupOrCenter.getId());
for (final Loan loan : groupLoans) {
final LoanStatusMapper loanStatus = new LoanStatusMapper(loan.status().getValue());
if (loanStatus.isOpen()) {
final String errorMessage = groupOrCenter.getGroupLevel().getLevelName() + " cannot be closed because of non-closed loans.";
throw new InvalidGroupStateTransitionException(groupOrCenter.getGroupLevel().getLevelName(), "close", "loan.not.closed",
errorMessage);
} else if (loanStatus.isClosed() && loan.getClosedOnDate().after(closureDate.toDate())) {
final String errorMessage = groupOrCenter.getGroupLevel().getLevelName()
+ "closureDate cannot be before the loan closedOnDate.";
throw new InvalidGroupStateTransitionException(groupOrCenter.getGroupLevel().getLevelName(), "close",
"date.cannot.before.loan.closed.date", errorMessage, closureDate, loan.getClosedOnDate());
} else if (loanStatus.isPendingApproval()) {
final String errorMessage = groupOrCenter.getGroupLevel().getLevelName() + " cannot be closed because of non-closed loans.";
throw new InvalidGroupStateTransitionException(groupOrCenter.getGroupLevel().getLevelName(), "close", "loan.not.closed",
errorMessage);
} else if (loanStatus.isAwaitingDisbursal()) {
final String errorMessage = "Group cannot be closed because of non-closed loans.";
throw new InvalidGroupStateTransitionException(groupOrCenter.getGroupLevel().getLevelName(), "close", "loan.not.closed",
errorMessage);
}
}
final List<SavingsAccount> groupSavingAccounts = this.savingsRepository.findByGroupId(groupOrCenter.getId());
for (final SavingsAccount saving : groupSavingAccounts) {
if (saving.isActive() || saving.isSubmittedAndPendingApproval() || saving.isApproved()) {
final String errorMessage = groupOrCenter.getGroupLevel().getLevelName()
+ " cannot be closed with active savings accounts associated.";
throw new InvalidGroupStateTransitionException(groupOrCenter.getGroupLevel().getLevelName(), "close",
"savings.account.not.closed", errorMessage);
} else if (saving.isClosed() && saving.getClosedOnDate().isAfter(closureDate)) {
final String errorMessage = groupOrCenter.getGroupLevel().getLevelName()
+ " closureDate cannot be before the loan closedOnDate.";
throw new InvalidGroupStateTransitionException(groupOrCenter.getGroupLevel().getLevelName(), "close",
"date.cannot.before.loan.closed.date", errorMessage, closureDate, saving.getClosedOnDate());
}
}
}