diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/Loan.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/Loan.java index 13e10f278..b0755a080 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/Loan.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/Loan.java @@ -1840,6 +1840,10 @@ public class Loan extends AbstractPersistable { return hasRepaymentTransaction; } + public boolean isSubmittedOnDateAfter(final LocalDate compareDate) { + return this.submittedOnDate == null ? false : (new LocalDate(submittedOnDate)).isAfter(compareDate); + } + private LocalDate getSubmittedOnDate() { return (LocalDate) ObjectUtils.defaultIfNull(new LocalDate(this.submittedOnDate), null); } @@ -1973,31 +1977,31 @@ public class Loan extends AbstractPersistable { public void reassignLoanOfficer(final Staff newLoanOfficer, final LocalDate assignmentDate) { final LoanOfficerAssignmentHistory latestHistoryRecord = findLatestIncompleteHistoryRecord(); - final LoanOfficerAssignmentHistory lastAssignmentRecordWithNoEndDate = findLastAssignmentHistoryRecord(); + final LoanOfficerAssignmentHistory lastAssignmentRecord = findLastAssignmentHistoryRecord(newLoanOfficer); // assignment date should not be less than loan submitted date - if (getSubmittedOnDate().isAfter(assignmentDate)) { + if (this.isSubmittedOnDateAfter(assignmentDate)) { + final String errorMessage = "The Loan Officer assignment date (" + assignmentDate.toString() + ") cannot be before loan submitted date (" + getSubmittedOnDate().toString() + ")."; + throw new LoanOfficerAssignmentDateException("cannot.be.before.loan.submittal.date", errorMessage, assignmentDate, getSubmittedOnDate()); - // assignment date should not be less than latest unassignment date - // FIXME - rather then fetching the data from the object all the - // time e.g - // getEndDate() and asking questions of it - move the operation to - // the object e.g. obj.isEndDateAfter(date) - } else if (lastAssignmentRecordWithNoEndDate != null && lastAssignmentRecordWithNoEndDate.getEndDate() != null - && lastAssignmentRecordWithNoEndDate.getEndDate().isAfter(assignmentDate)) { + } else if (lastAssignmentRecord != null && lastAssignmentRecord.isEndDateAfter(assignmentDate)) { + final String errorMessage = "The Loan Officer assignment date (" + assignmentDate - + ") cannot be before previous Loan Officer unassigned date (" + lastAssignmentRecordWithNoEndDate.getEndDate() + ")."; + + ") cannot be before previous Loan Officer unassigned date (" + lastAssignmentRecord.getEndDate() + ")."; + throw new LoanOfficerAssignmentDateException("cannot.be.before.previous.unassignement.date", errorMessage, assignmentDate, - lastAssignmentRecordWithNoEndDate.getEndDate()); + lastAssignmentRecord.getEndDate()); - // assignment date should not be in the future } else if (DateUtils.getLocalDateOfTenant().isBefore(assignmentDate)) { + final String errorMessage = "The Loan Officer assignment date (" + assignmentDate + ") cannot be in the future."; + throw new LoanOfficerAssignmentDateException("cannot.be.a.future.date", errorMessage, assignmentDate); + } else if (latestHistoryRecord != null && this.loanOfficer.identifiedBy(newLoanOfficer)) { latestHistoryRecord.updateStartDate(assignmentDate); } else if (latestHistoryRecord != null && latestHistoryRecord.matchesStartDateOf(assignmentDate)) { @@ -2066,22 +2070,24 @@ public class Loan extends AbstractPersistable { return latestRecordWithNoEndDate; } - private LoanOfficerAssignmentHistory findLastAssignmentHistoryRecord() { + private LoanOfficerAssignmentHistory findLastAssignmentHistoryRecord(final Staff newLoanOfficer) { - LoanOfficerAssignmentHistory lastAssignmentRecordWithNoEndDate = null; + LoanOfficerAssignmentHistory lastAssignmentRecordLatestEndDate = null; for (LoanOfficerAssignmentHistory historyRecord : this.loanOfficerHistory) { - if (historyRecord.isCurrentRecord()) { - lastAssignmentRecordWithNoEndDate = historyRecord; + + if (historyRecord.isCurrentRecord() && !historyRecord.isSameLoanOfficer(newLoanOfficer)) { + lastAssignmentRecordLatestEndDate = historyRecord; break; } - if (lastAssignmentRecordWithNoEndDate == null) { - lastAssignmentRecordWithNoEndDate = historyRecord; - } else if (historyRecord.getEndDate().isAfter(lastAssignmentRecordWithNoEndDate.getEndDate())) { - lastAssignmentRecordWithNoEndDate = historyRecord; + if (lastAssignmentRecordLatestEndDate == null){ + lastAssignmentRecordLatestEndDate = historyRecord; + } else if (historyRecord.isEndDateAfter(lastAssignmentRecordLatestEndDate.getEndDate()) + && !historyRecord.isSameLoanOfficer(newLoanOfficer)) { + lastAssignmentRecordLatestEndDate = historyRecord; } } - return lastAssignmentRecordWithNoEndDate; + return lastAssignmentRecordLatestEndDate; } public Long getClientId() { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanOfficerAssignmentHistory.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanOfficerAssignmentHistory.java index 5e10a4fb9..e56956ade 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanOfficerAssignmentHistory.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanOfficerAssignmentHistory.java @@ -15,6 +15,7 @@ import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; +import org.apache.commons.lang.ObjectUtils; import org.joda.time.LocalDate; import org.mifosplatform.infrastructure.core.domain.AbstractAuditableCustom; import org.mifosplatform.organisation.staff.domain.Staff; @@ -29,7 +30,6 @@ public class LoanOfficerAssignmentHistory extends AbstractAuditableCustom