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


##########
fineract-accounting/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java:
##########
@@ -577,12 +677,46 @@ public void deleteProductToGLAccountMapping(final Long 
loanProductId, final Port
         }
     }
 
-    public void validateChargeOffMappingsInDatabase(final List<JsonObject> 
mappings) {
+    public void validateWriteOffMappingsInDatabase(final List<JsonObject> 
mappings) {
         final List<ApiParameterError> validationErrors = new ArrayList<>();
 
         for (JsonObject jsonObject : mappings) {
             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);
+
+            // Validation: writeOffReasonCodeValueId must exist in the database
+            CodeValue codeValue = 
this.codeValueRepository.findByCodeNameAndId("WriteOffReasons", 
writeOffReasonCodeValueId);
+            if (codeValue == null) {
+                
validationErrors.add(ApiParameterError.parameterError("validation.msg.writeoffreason.invalid",
+                        "Write-off reason with ID " + 
writeOffReasonCodeValueId + " does not exist",
+                        
LoanProductAccountingParams.WRITE_OFF_REASON_TO_EXPENSE_ACCOUNT_MAPPINGS.getValue()));
+            }
+
+            // Validation: expenseGLAccountId must exist as a valid Expense GL 
account
+            final Optional<GLAccount> glAccount = 
accountRepository.findById(expenseGlAccountId);
+
+            if (glAccount.isEmpty() || 
!glAccount.get().getType().equals(GL_ACCOUNT_EXPENSE_TYPE)) {
+                
validationErrors.add(ApiParameterError.parameterError("validation.msg.glaccount.not.found",
+                        "GL Account with ID " + expenseGlAccountId + " does 
not exist or is not an Expense GL account",
+                        
LoanProductAccountingParams.EXPENSE_ACCOUNT_ID.getValue()));
+
+            }
+        }
+
+        // Throw all collected validation errors, if any
+        if (!validationErrors.isEmpty()) {
+            throw new PlatformApiDataValidationException(validationErrors);
+        }
+    }

Review Comment:
   Same, please extract the common logic.



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