Merge pull request #1349 from chandrikamohith/1842

MIFOSX-1842 validations added for add/delete tranches.
This commit is contained in:
Vishwas Babu A J
2015-04-13 12:21:25 -07:00
4 changed files with 131 additions and 75 deletions
@@ -1564,56 +1564,60 @@ public class Loan extends AbstractPersistable<Long> {
int i = 0;
do {
final JsonObject jsonObject = disbursementDataArray.get(i).getAsJsonObject();
Date expectedDisbursementDate = null;
BigDecimal principal = null;
LocalDate date = null;
Long id = null;
if (jsonObject.has(LoanApiConstants.disbursementDateParameterName)
&& jsonObject.get(LoanApiConstants.disbursementDateParameterName) != null
&& jsonObject.get(LoanApiConstants.disbursementDateParameterName).isJsonPrimitive()) {
&& jsonObject.has(LoanApiConstants.disbursementPrincipalParameterName)) {
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 String valueAsString = primitive.getAsString();
if (StringUtils.isNotBlank(valueAsString)) {
date = JsonParserHelper.convertFrom(valueAsString, LoanApiConstants.disbursementDateParameterName, dateFormat,
locale);
final JsonPrimitive primitive = jsonObject.get(LoanApiConstants.disbursementDateParameterName)
.getAsJsonPrimitive();
final String valueAsString = primitive.getAsString();
if (StringUtils.isNotBlank(valueAsString)) {
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)
&& jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).isJsonPrimitive()
&& StringUtils.isNotBlank((jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).getAsString()))) {
principal = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementPrincipalParameterName).getAsBigDecimal();
}
if (jsonObject.has(LoanApiConstants.disbursementIdParameterName)
&& jsonObject.get(LoanApiConstants.disbursementIdParameterName).isJsonPrimitive()
&& StringUtils.isNotBlank((jsonObject.get(LoanApiConstants.disbursementIdParameterName).getAsString()))) {
id = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementIdParameterName).getAsLong();
}
if (id != null) {
LoanDisbursementDetails loanDisbursementDetail = fetchLoanDisbursementsById(id);
list.remove(id);
if (loanDisbursementDetail.actualDisbursementDate() == null) {
if (jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).isJsonPrimitive()
&& StringUtils.isNotBlank((jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName)
.getAsString()))) {
principal = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementPrincipalParameterName)
.getAsBigDecimal();
}
if (jsonObject.has(LoanApiConstants.disbursementIdParameterName)
&& jsonObject.get(LoanApiConstants.disbursementIdParameterName).isJsonPrimitive()
&& StringUtils.isNotBlank((jsonObject.get(LoanApiConstants.disbursementIdParameterName).getAsString()))) {
id = jsonObject.getAsJsonPrimitive(LoanApiConstants.disbursementIdParameterName).getAsLong();
}
if (id != null) {
LoanDisbursementDetails loanDisbursementDetail = fetchLoanDisbursementsById(id);
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;
LoanDisbursementDetails disbursementDetails = new LoanDisbursementDetails(expectedDisbursementDate,
actualDisbursementDate, principal);
if (!loanDisbursementDetail.equals(disbursementDetails)) {
loanDisbursementDetail.copy(disbursementDetails);
actualChanges.put("disbursementDetailId", id);
actualChanges.put("recalculateLoanSchedule", true);
}
disbursementDetails.updateLoan(this);
this.disbursementDetails.add(disbursementDetails);
actualChanges.put(LoanApiConstants.disbursementDataParameterName, expectedDisbursementDate + "-" + principal);
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++;
} 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) {
for (int i = 0; i < variationArray.size(); i++) {
final JsonObject jsonObject1 = variationArray.get(i).getAsJsonObject();
LocalDate date1 = this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.disbursementDateParameterName, jsonObject1,
dateFormat, locale);
if(jsonObject1.has(LoanApiConstants.disbursementDateParameterName)){
LocalDate date1 = this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.disbursementDateParameterName, jsonObject1,
dateFormat, locale);
for (int j = i + 1; j < variationArray.size(); j++) {
final JsonObject jsonObject2 = variationArray.get(j).getAsJsonObject();
LocalDate date2 = this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.disbursementDateParameterName,
jsonObject2, dateFormat, locale);
if (date1.isAfter(date2)) {
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName)
.failWithCode(LoanApiConstants.DISBURSEMENT_DATES_NOT_IN_ORDER);
for (int j = i + 1; j < variationArray.size(); j++) {
final JsonObject jsonObject2 = variationArray.get(j).getAsJsonObject();
if(jsonObject2.has(LoanApiConstants.disbursementDateParameterName)){
LocalDate date2 = this.fromApiJsonHelper.extractLocalDateNamed(LoanApiConstants.disbursementDateParameterName,
jsonObject2, dateFormat, locale);
if (date1.isAfter(date2)) {
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName)
.failWithCode(LoanApiConstants.DISBURSEMENT_DATES_NOT_IN_ORDER);
}
}
}
}
}
}
}
@@ -875,33 +880,37 @@ public final class LoanApplicationCommandFromApiJsonHelper {
int i = 0;
do {
final JsonObject jsonObject = variationArray.get(i).getAsJsonObject();
LocalDate expectedDisbursementDate = this.fromApiJsonHelper.extractLocalDateNamed(
LoanApiConstants.disbursementDateParameterName, jsonObject, dateFormat, locale);
if (expectedDisbursementDates.contains(expectedDisbursementDate)) {
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDateParameterName)
.failWithCode(LoanApiConstants.DISBURSEMENT_DATE_UNIQUE_ERROR);
}
if (expectedDisbursementDate.isBefore(expectedDisbursement)) {
if (jsonObject.has(LoanApiConstants.disbursementDateParameterName)
&& jsonObject.has(LoanApiConstants.disbursementPrincipalParameterName)) {
LocalDate expectedDisbursementDate = this.fromApiJsonHelper.extractLocalDateNamed(
LoanApiConstants.disbursementDateParameterName, jsonObject, dateFormat, locale);
if (expectedDisbursementDates.contains(expectedDisbursementDate)) {
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDateParameterName)
.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)
.failWithCode(LoanApiConstants.DISBURSEMENT_DATE_BEFORE_ERROR);
}
expectedDisbursementDates.add(expectedDisbursementDate);
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);
}
.parameterAtIndexArray(LoanApiConstants.disbursementPrincipalParameterName, i).value(principal).notBlank();
if (principal != null) {
tatalDisbursement = tatalDisbursement.add(principal);
}
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName)
.parameterAtIndexArray(LoanApiConstants.disbursementDateParameterName, i).value(expectedDisbursementDate)
.notNull();
baseDataValidator.reset().parameter(LoanApiConstants.disbursementDataParameterName)
.parameterAtIndexArray(LoanApiConstants.disbursementDateParameterName, i).value(expectedDisbursementDate)
.notNull();
if (expectedDisbursement.equals(expectedDisbursementDate)) {
isFirstinstallmentOnExpectedDisbursementDate = true;
}
i++;
if (expectedDisbursement.equals(expectedDisbursementDate)) {
isFirstinstallmentOnExpectedDisbursementDate = true;
}
}i++;
} while (i < variationArray.size());
if (!isFirstinstallmentOnExpectedDisbursementDate) {
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.LoanTransactionNotFoundException;
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.loanschedule.data.OverdueLoanScheduleData;
import org.mifosplatform.portfolio.loanaccount.loanschedule.domain.DefaultScheduledDateGenerator;
@@ -2584,6 +2585,21 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
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
@Transactional
public CommandProcessingResult addAndDeleteLoanDisburseDetails(Long loanId, JsonCommand command) {
@@ -2592,7 +2608,17 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
checkClientOrGroupActive(loan);
final Map<String, Object> actualChanges = new LinkedHashMap<>();
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);
this.validateForAddAndDeleteTranche(loan);
loan.updateDisbursementDetails(command, actualChanges);