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


##########
fineract-accounting/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java:
##########
@@ -356,6 +382,66 @@ public void updatePaymentChannelToFundSourceMappings(final 
JsonCommand command,
         }
     }
 
+    public void updateChargeOffReasonToGLAccountMappings(final JsonCommand 
command, final JsonElement element, final Long productId,
+            final Map<String, Object> changes, final PortfolioProductType 
portfolioProductType) {
+
+        final List<ProductToGLAccountMapping> 
existingChargeOffReasonToGLAccountMappings = this.accountMappingRepository
+                .findAllChargesOffReasonsMappings(productId, 
portfolioProductType.getValue());
+        final JsonArray chargeOffReasonToGLAccountMappingArray = 
this.fromApiJsonHelper
+                
.extractJsonArrayNamed(LoanProductAccountingParams.CHARGE_OFF_REASONS_TO_EXPENSE.getValue(),
 element);
+
+        final Map<Long, Long> inputChargeOffReasonToGLAccountMap = new 
HashMap<>();
+
+        final Set<Long> existingChargeOffReasons = new HashSet<>();
+        if (chargeOffReasonToGLAccountMappingArray != null) {
+            if (changes != null) {
+                
changes.put(LoanProductAccountingParams.CHARGE_OFF_REASONS_TO_EXPENSE.getValue(),
+                        
command.jsonFragment(LoanProductAccountingParams.CHARGE_OFF_REASONS_TO_EXPENSE.getValue()));
+            }
+
+            for (int i = 0; i < chargeOffReasonToGLAccountMappingArray.size(); 
i++) {
+                final JsonObject jsonObject = 
chargeOffReasonToGLAccountMappingArray.get(i).getAsJsonObject();
+                final Long expenseGlAccountId = 
jsonObject.get(LoanProductAccountingParams.EXPENSE_GL_ACCOUNT_ID.getValue()).getAsLong();
+                final Long chargeOffReasonCodeValueId = jsonObject
+                        
.get(LoanProductAccountingParams.CHARGE_OFF_REASON_CODE_VALUE_ID.getValue()).getAsLong();
+                
inputChargeOffReasonToGLAccountMap.put(chargeOffReasonCodeValueId, 
expenseGlAccountId);
+            }
+
+            // If input map is empty, delete all existing mappings
+            if (inputChargeOffReasonToGLAccountMap.isEmpty()) {
+                
this.accountMappingRepository.deleteAllInBatch(existingChargeOffReasonToGLAccountMappings);
+            } else {
+                for (final ProductToGLAccountMapping 
existingChargeOffReasonToGLAccountMapping : 
existingChargeOffReasonToGLAccountMappings) {
+                    final Long currentChargeOffReasonId = 
existingChargeOffReasonToGLAccountMapping.getChargeOffReasonId();
+                    if (currentChargeOffReasonId != null) {
+                        existingChargeOffReasons.add(currentChargeOffReasonId);
+                        // update existing mappings (if required)
+                        if 
(inputChargeOffReasonToGLAccountMap.containsKey(currentChargeOffReasonId)) {
+                            final Long newGLAccountId = 
inputChargeOffReasonToGLAccountMap.get(currentChargeOffReasonId);
+                            if 
(!newGLAccountId.equals(existingChargeOffReasonToGLAccountMapping.getGlAccount().getId()))
 {
+                                final GLAccount glAccount = 
getAccountById(LoanProductAccountingParams.FUND_SOURCE.getValue(),

Review Comment:
   This look incorrect. Why FUND_SOURCE?



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