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


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanproduct/serialization/LoanProductDataValidator.java:
##########
@@ -2109,6 +2111,58 @@ private void validateChargeOffToExpenseMappings(final 
DataValidatorBuilder baseD
         }
     }
 
+    private void validateWriteOffToExpenseMappings(final DataValidatorBuilder 
baseDataValidator, final JsonElement element) {
+        String parameterName = 
LoanProductAccountingParams.WRITE_OFF_REASON_TO_EXPENSE_ACCOUNT_MAPPINGS.getValue();
+
+        if (this.fromApiJsonHelper.parameterExists(parameterName, element)) {
+            final JsonArray writeOffToExpenseMappingArray = 
this.fromApiJsonHelper.extractJsonArrayNamed(parameterName, element);
+            if (writeOffToExpenseMappingArray != null && 
writeOffToExpenseMappingArray.size() > 0) {
+                Map<Long, Set<Long>> writeOffReasonToAccounts = new 
HashMap<>();
+                List<JsonObject> processedMappings = new ArrayList<>(); // 
Collect processed mappings for the new method
+
+                int i = 0;
+                do {
+                    final JsonObject jsonObject = 
writeOffToExpenseMappingArray.get(i).getAsJsonObject();
+                    final Long expenseGlAccountId = this.fromApiJsonHelper
+                            
.extractLongNamed(LoanProductAccountingParams.EXPENSE_GL_ACCOUNT_ID.getValue(), 
jsonObject);
+                    final Long writeOffReasonCodeValueId = 
this.fromApiJsonHelper
+                            
.extractLongNamed(LoanProductAccountingParams.WRITE_OFF_REASON_CODE_VALUE_ID.getValue(),
 jsonObject);
+
+                    // Validate parameters locally
+                    baseDataValidator.reset().parameter(parameterName + 
OPENING_SQUARE_BRACKET + i + CLOSING_SQUARE_BRACKET + DOT
+                            + 
LoanProductAccountingParams.EXPENSE_GL_ACCOUNT_ID.getValue()).value(expenseGlAccountId).notNull();
+                    baseDataValidator.reset()
+                            .parameter(parameterName + OPENING_SQUARE_BRACKET 
+ i + CLOSING_SQUARE_BRACKET + DOT
+                                    + 
LoanProductAccountingParams.WRITE_OFF_REASON_CODE_VALUE_ID.getValue())
+                            .value(writeOffReasonCodeValueId).notNull();
+
+                    // Handle duplicate charge-off reason and GL Account 
validation
+                    
writeOffReasonToAccounts.putIfAbsent(writeOffReasonCodeValueId, new 
HashSet<>());
+                    Set<Long> associatedAccounts = 
writeOffReasonToAccounts.get(writeOffReasonCodeValueId);
+
+                    if (associatedAccounts.contains(expenseGlAccountId)) {
+                        baseDataValidator.reset().parameter(parameterName + 
OPENING_SQUARE_BRACKET + i + CLOSING_SQUARE_BRACKET)
+                                
.failWithCode("duplicate.writeOffReason.and.glAccount");
+                    }
+                    associatedAccounts.add(expenseGlAccountId);
+
+                    if (associatedAccounts.size() > 1) {
+                        baseDataValidator.reset().parameter(parameterName + 
OPENING_SQUARE_BRACKET + i + CLOSING_SQUARE_BRACKET)
+                                
.failWithCode("multiple.glAccounts.for.writeOffReason");
+                    }
+
+                    // Collect mapping for additional validations
+                    processedMappings.add(jsonObject);
+
+                    i++;
+                } while (i < writeOffToExpenseMappingArray.size());
+
+                // Call the new validation method for additional checks
+                
productToGLAccountMappingHelper.validateWriteOffMappingsInDatabase(processedMappings);
+            }
+        }

Review Comment:
   Looks 90% duplicate. Would you mind to extract the common parts from this 
and charge off validation?



-- 
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