adamsaghy commented on code in PR #4828:
URL: https://github.com/apache/fineract/pull/4828#discussion_r2180303097


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanValidatorImpl.java:
##########
@@ -0,0 +1,79 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.portfolio.loanaccount.serialization;
+
+import com.google.gson.JsonElement;
+import java.math.BigDecimal;
+import lombok.RequiredArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.fineract.infrastructure.core.api.JsonCommand;
+import org.apache.fineract.infrastructure.core.exception.InvalidJsonException;
+import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
+import org.apache.fineract.infrastructure.core.service.MathUtil;
+import org.apache.fineract.portfolio.common.service.Validator;
+import org.apache.fineract.portfolio.loanaccount.api.LoanApiConstants;
+import org.apache.fineract.portfolio.loanaccount.domain.Loan;
+import 
org.apache.fineract.portfolio.loanaccount.domain.LoanDisbursementDetails;
+import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository;
+import 
org.apache.fineract.portfolio.loanaccount.exception.LoanNotFoundException;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+public final class LoanValidatorImpl implements LoanValidator {
+
+    private final FromJsonHelper fromApiJsonHelper;
+    private final LoanRepository loanRepository;
+
+    @Override
+    public void validateLoanApprovedAmountModification(JsonCommand command) {
+        String json = command.json();
+        if (StringUtils.isBlank(json)) {
+            throw new InvalidJsonException();
+        }
+
+        Validator.validateOrThrowDomainViolation("loan.approved.amount", 
baseDataValidator -> {
+            final JsonElement element = this.fromApiJsonHelper.parse(json);
+            final BigDecimal newApprovedAmount = this.fromApiJsonHelper
+                    
.extractBigDecimalWithLocaleNamed(LoanApiConstants.amountParameterName, 
element);
+
+            
baseDataValidator.reset().parameter(LoanApiConstants.amountParameterName).value(newApprovedAmount).positiveAmount();
+
+            final Long loanId = command.getLoanId();
+            Loan loan = this.loanRepository.findById(loanId).orElseThrow(() -> 
new LoanNotFoundException(loanId));
+            BigDecimal approvedPrincipal = loan.getApprovedPrincipal();
+
+            if (MathUtil.isGreaterThan(newApprovedAmount, approvedPrincipal)) {
+                
baseDataValidator.reset().parameter(LoanApiConstants.amountParameterName).failWithCode("greater.than.approved.principal");
+            }
+
+            BigDecimal reservedPrincipal = BigDecimal.ZERO;
+            if (!loan.loanProduct().isDisallowExpectedDisbursements()) {

Review Comment:
   You can simplify this, by removing this condition and aggregate the 
principal of loan disbursement details (except the reversed=true entries). It 
will contains all already disbursed (actualDisbursementDate != null) and not 
yet disbursed but expected (actualDisbursementDate == null).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to