MIFOSX-1842 validations added for add/delete tranches.

This commit is contained in:
Chandrika
2015-04-13 14:15:25 -04:00
parent f4ed5f80dd
commit 006b14db0a
4 changed files with 131 additions and 75 deletions
@@ -1564,56 +1564,60 @@ public class Loan extends AbstractPersistable<Long> {
int i = 0; int i = 0;
do { do {
final JsonObject jsonObject = disbursementDataArray.get(i).getAsJsonObject(); final JsonObject jsonObject = disbursementDataArray.get(i).getAsJsonObject();
Date expectedDisbursementDate = null;
BigDecimal principal = null;
LocalDate date = null;
Long id = null;
if (jsonObject.has(LoanApiConstants.disbursementDateParameterName) if (jsonObject.has(LoanApiConstants.disbursementDateParameterName)
&& jsonObject.get(LoanApiConstants.disbursementDateParameterName) != null && jsonObject.has(LoanApiConstants.disbursementPrincipalParameterName)) {
&& jsonObject.get(LoanApiConstants.disbursementDateParameterName).isJsonPrimitive()) { Date expectedDisbursementDate = null;
BigDecimal principal = null;
LocalDate date = null;
Long id = null;
if (jsonObject.get(LoanApiConstants.disbursementDateParameterName) != null
&& jsonObject.get(LoanApiConstants.disbursementDateParameterName).isJsonPrimitive()) {
final JsonPrimitive primitive = jsonObject.get(LoanApiConstants.disbursementDateParameterName).getAsJsonPrimitive(); final JsonPrimitive primitive = jsonObject.get(LoanApiConstants.disbursementDateParameterName)
final String valueAsString = primitive.getAsString(); .getAsJsonPrimitive();
if (StringUtils.isNotBlank(valueAsString)) { final String valueAsString = primitive.getAsString();
date = JsonParserHelper.convertFrom(valueAsString, LoanApiConstants.disbursementDateParameterName, dateFormat, if (StringUtils.isNotBlank(valueAsString)) {
locale); date = JsonParserHelper.convertFrom(valueAsString, LoanApiConstants.disbursementDateParameterName,
dateFormat, locale);
}
}
if (date != null) {
expectedDisbursementDate = date.toDate();
} }
}
if (date != null) {
expectedDisbursementDate = date.toDate();
}
if (jsonObject.has(LoanApiConstants.disbursementPrincipalParameterName) if (jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).isJsonPrimitive()
&& jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).isJsonPrimitive() && StringUtils.isNotBlank((jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName)
&& StringUtils.isNotBlank((jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).getAsString()))) { .getAsString()))) {
principal = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementPrincipalParameterName).getAsBigDecimal(); principal = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementPrincipalParameterName)
} .getAsBigDecimal();
if (jsonObject.has(LoanApiConstants.disbursementIdParameterName) }
&& jsonObject.get(LoanApiConstants.disbursementIdParameterName).isJsonPrimitive() if (jsonObject.has(LoanApiConstants.disbursementIdParameterName)
&& StringUtils.isNotBlank((jsonObject.get(LoanApiConstants.disbursementIdParameterName).getAsString()))) { && jsonObject.get(LoanApiConstants.disbursementIdParameterName).isJsonPrimitive()
id = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementIdParameterName).getAsLong(); && StringUtils.isNotBlank((jsonObject.get(LoanApiConstants.disbursementIdParameterName).getAsString()))) {
} id = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementIdParameterName).getAsLong();
if (id != null) { }
LoanDisbursementDetails loanDisbursementDetail = fetchLoanDisbursementsById(id); if (id != null) {
list.remove(id); LoanDisbursementDetails loanDisbursementDetail = fetchLoanDisbursementsById(id);
if (loanDisbursementDetail.actualDisbursementDate() == null) { list.remove(id);
if (loanDisbursementDetail.actualDisbursementDate() == null) {
Date actualDisbursementDate = null;
LoanDisbursementDetails disbursementDetails = new LoanDisbursementDetails(expectedDisbursementDate,
actualDisbursementDate, principal);
if (!loanDisbursementDetail.equals(disbursementDetails)) {
loanDisbursementDetail.copy(disbursementDetails);
actualChanges.put("disbursementDetailId", id);
actualChanges.put("recalculateLoanSchedule", true);
}
}
} else {
Date actualDisbursementDate = null; Date actualDisbursementDate = null;
LoanDisbursementDetails disbursementDetails = new LoanDisbursementDetails(expectedDisbursementDate, LoanDisbursementDetails disbursementDetails = new LoanDisbursementDetails(expectedDisbursementDate,
actualDisbursementDate, principal); actualDisbursementDate, principal);
if (!loanDisbursementDetail.equals(disbursementDetails)) { disbursementDetails.updateLoan(this);
loanDisbursementDetail.copy(disbursementDetails); this.disbursementDetails.add(disbursementDetails);
actualChanges.put("disbursementDetailId", id); actualChanges.put(LoanApiConstants.disbursementDataParameterName, expectedDisbursementDate + "-" + principal);
actualChanges.put("recalculateLoanSchedule", true); actualChanges.put("recalculateLoanSchedule", true);
}
} }
} else {
Date actualDisbursementDate = null;
LoanDisbursementDetails disbursementDetails = new LoanDisbursementDetails(expectedDisbursementDate,
actualDisbursementDate, principal);
disbursementDetails.updateLoan(this);
this.disbursementDetails.add(disbursementDetails);
actualChanges.put(LoanApiConstants.disbursementDataParameterName, expectedDisbursementDate + "-" + principal);
actualChanges.put("recalculateLoanSchedule", true);
} }
i++; i++;
} while (i < disbursementDataArray.size()); } while (i < disbursementDataArray.size());
@@ -0,0 +1,17 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.mifosplatform.portfolio.loanaccount.exception;
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException;
public class LoanMultiDisbursementException extends AbstractPlatformDomainRuleException {
public LoanMultiDisbursementException(final String entity, final String defaultUserMessage,
final Object... defaultUserMessageArgs) {
super("error.msg." + defaultUserMessage,defaultUserMessage);
}
}
@@ -840,18 +840,23 @@ public final class LoanApplicationCommandFromApiJsonHelper {
if (variationArray != null) { if (variationArray != null) {
for (int i = 0; i < variationArray.size(); i++) { for (int i = 0; i < variationArray.size(); i++) {
final JsonObject jsonObject1 = variationArray.get(i).getAsJsonObject(); final JsonObject jsonObject1 = variationArray.get(i).getAsJsonObject();
LocalDate date1 = this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.disbursementDateParameterName, jsonObject1, if(jsonObject1.has(LoanApiConstants.disbursementDateParameterName)){
dateFormat, locale); LocalDate date1 = this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.disbursementDateParameterName, jsonObject1,
dateFormat, locale);
for (int j = i + 1; j < variationArray.size(); j++) { for (int j = i + 1; j < variationArray.size(); j++) {
final JsonObject jsonObject2 = variationArray.get(j).getAsJsonObject(); final JsonObject jsonObject2 = variationArray.get(j).getAsJsonObject();
LocalDate date2 = this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.disbursementDateParameterName, if(jsonObject2.has(LoanApiConstants.disbursementDateParameterName)){
jsonObject2, dateFormat, locale); LocalDate date2 = this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.disbursementDateParameterName,
if (date1.isAfter(date2)) { jsonObject2, dateFormat, locale);
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName) if (date1.isAfter(date2)) {
.failWithCode(LoanApiConstants.DISBURSEMENT_DATES_NOT_IN_ORDER); baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName)
.failWithCode(LoanApiConstants.DISBURSEMENT_DATES_NOT_IN_ORDER);
}
}
} }
} }
} }
} }
} }
@@ -875,33 +880,37 @@ public final class LoanApplicationCommandFromApiJsonHelper {
int i = 0; int i = 0;
do { do {
final JsonObject jsonObject = variationArray.get(i).getAsJsonObject(); final JsonObject jsonObject = variationArray.get(i).getAsJsonObject();
LocalDate expectedDisbursementDate = this.fromApiJsonHelper.extractLocalDateNamed( if (jsonObject.has(LoanApiConstants.disbursementDateParameterName)
LoanApiConstants.disbursementDateParameterName, jsonObject, dateFormat, locale); && jsonObject.has(LoanApiConstants.disbursementPrincipalParameterName)) {
if (expectedDisbursementDates.contains(expectedDisbursementDate)) { LocalDate expectedDisbursementDate = this.fromApiJsonHelper.extractLocalDateNamed(
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDateParameterName) LoanApiConstants.disbursementDateParameterName, jsonObject, dateFormat, locale);
.failWithCode(LoanApiConstants.DISBURSEMENT_DATE_UNIQUE_ERROR); if (expectedDisbursementDates.contains(expectedDisbursementDate)) {
} baseDataValidator.reset().parameter(LoanApiConstants.disbursementDateParameterName)
if (expectedDisbursementDate.isBefore(expectedDisbursement)) { .failWithCode(LoanApiConstants.DISBURSEMENT_DATE_UNIQUE_ERROR);
}
if (expectedDisbursementDate.isBefore(expectedDisbursement)) {
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName)
.failWithCode(LoanApiConstants.DISBURSEMENT_DATE_BEFORE_ERROR);
}
expectedDisbursementDates.add(expectedDisbursementDate);
BigDecimal principal = this.fromApiJsonHelper.extractBigDecimalNamed(
LoanApiConstants.disbursementPrincipalParameterName, jsonObject, locale);
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName) baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName)
.failWithCode(LoanApiConstants.DISBURSEMENT_DATE_BEFORE_ERROR); .parameterAtIndexArray(LoanApiConstants.disbursementPrincipalParameterName, i).value(principal).notBlank();
} if (principal != null) {
expectedDisbursementDates.add(expectedDisbursementDate); tatalDisbursement = tatalDisbursement.add(principal);
BigDecimal principal = this.fromApiJsonHelper.extractBigDecimalNamed( }
LoanApiConstants.disbursementPrincipalParameterName, jsonObject, locale);
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName)
.parameterAtIndexArray(LoanApiConstants.disbursementPrincipalParameterName, i).value(principal).notBlank();
if (principal != null) {
tatalDisbursement = tatalDisbursement.add(principal);
}
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName) baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName)
.parameterAtIndexArray(LoanApiConstants.disbursementDateParameterName, i).value(expectedDisbursementDate) .parameterAtIndexArray(LoanApiConstants.disbursementDateParameterName, i).value(expectedDisbursementDate)
.notNull(); .notNull();
if (expectedDisbursement.equals(expectedDisbursementDate)) { if (expectedDisbursement.equals(expectedDisbursementDate)) {
isFirstinstallmentOnExpectedDisbursementDate = true; isFirstinstallmentOnExpectedDisbursementDate = true;
} }
i++;
}i++;
} while (i < variationArray.size()); } while (i < variationArray.size());
if (!isFirstinstallmentOnExpectedDisbursementDate) { if (!isFirstinstallmentOnExpectedDisbursementDate) {
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDateParameterName) baseDataValidator.reset().parameter(LoanApiConstants.disbursementDateParameterName)
@@ -130,6 +130,7 @@ import org.mifosplatform.portfolio.loanaccount.exception.LoanOfficerAssignmentEx
import org.mifosplatform.portfolio.loanaccount.exception.LoanOfficerUnassignmentException; import org.mifosplatform.portfolio.loanaccount.exception.LoanOfficerUnassignmentException;
import org.mifosplatform.portfolio.loanaccount.exception.LoanTransactionNotFoundException; import org.mifosplatform.portfolio.loanaccount.exception.LoanTransactionNotFoundException;
import org.mifosplatform.portfolio.loanaccount.exception.MultiDisbursementDataRequiredException; import org.mifosplatform.portfolio.loanaccount.exception.MultiDisbursementDataRequiredException;
import org.mifosplatform.portfolio.loanaccount.exception.LoanMultiDisbursementException;
import org.mifosplatform.portfolio.loanaccount.guarantor.service.GuarantorDomainService; import org.mifosplatform.portfolio.loanaccount.guarantor.service.GuarantorDomainService;
import org.mifosplatform.portfolio.loanaccount.loanschedule.data.OverdueLoanScheduleData; import org.mifosplatform.portfolio.loanaccount.loanschedule.data.OverdueLoanScheduleData;
import org.mifosplatform.portfolio.loanaccount.loanschedule.domain.DefaultScheduledDateGenerator; import org.mifosplatform.portfolio.loanaccount.loanschedule.domain.DefaultScheduledDateGenerator;
@@ -2584,6 +2585,21 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); }
} }
private void validateForAddAndDeleteTranche(final Loan loan){
BigDecimal totalDisbursedAmount = BigDecimal.ZERO;
Collection<LoanDisbursementDetails> loanDisburseDetails = loan.getDisbursementDetails();
for (LoanDisbursementDetails disbursementDetails : loanDisburseDetails) {
if(disbursementDetails.actualDisbursementDate() != null){
totalDisbursedAmount = totalDisbursedAmount.add(disbursementDetails.principal());
}
}
if(totalDisbursedAmount.compareTo(loan.getApprovedPrincipal()) == 0){
final String errorMessage = "loan.disbursement.cannot.be.a.edited";
throw new LoanMultiDisbursementException(LoanApiConstants.disbursementDataParameterName, errorMessage);
}
}
@Override @Override
@Transactional @Transactional
public CommandProcessingResult addAndDeleteLoanDisburseDetails(Long loanId, JsonCommand command) { public CommandProcessingResult addAndDeleteLoanDisburseDetails(Long loanId, JsonCommand command) {
@@ -2592,7 +2608,17 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
checkClientOrGroupActive(loan); checkClientOrGroupActive(loan);
final Map<String, Object> actualChanges = new LinkedHashMap<>(); final Map<String, Object> actualChanges = new LinkedHashMap<>();
LocalDate expectedDisbursementDate = loan.getExpectedDisbursedOnLocalDate(); LocalDate expectedDisbursementDate = loan.getExpectedDisbursedOnLocalDate();
if(!loan.loanProduct().isMultiDisburseLoan()){
final String errorMessage = "loan.product.does.not.support.multiple.disbursals";
throw new LoanMultiDisbursementException(LoanApiConstants.disbursementDataParameterName, errorMessage);
}
if(loan.isSubmittedAndPendingApproval()){
final String errorMessage = "loan.account.should.be.approved";
throw new LoanMultiDisbursementException(LoanApiConstants.disbursementDataParameterName, errorMessage);
}
validateMultiDisbursementData(command, expectedDisbursementDate); validateMultiDisbursementData(command, expectedDisbursementDate);
this.validateForAddAndDeleteTranche(loan);
loan.updateDisbursementDetails(command, actualChanges); loan.updateDisbursementDetails(command, actualChanges);