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


##########
fineract-accounting/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java:
##########
@@ -389,6 +410,65 @@ public void updatePaymentChannelToFundSourceMappings(final 
JsonCommand command,
         }
     }
 
+    public void updateWriteOffReasonToGLAccountMappings(final JsonCommand 
command, final JsonElement element, final Long productId,
+            final Map<String, Object> changes, final PortfolioProductType 
portfolioProductType) {
+
+        final List<ProductToGLAccountMapping> 
existingWriteOffReasonToGLAccountMappings = this.accountMappingRepository
+                .findAllWriteOffReasonsMappings(productId, 
portfolioProductType.getValue());
+        final JsonArray writeOffReasonToGLAccountMappingArray = 
this.fromApiJsonHelper
+                
.extractJsonArrayNamed(LoanProductAccountingParams.WRITE_OFF_REASON_TO_EXPENSE_ACCOUNT_MAPPINGS.getValue(),
 element);
+
+        final Map<Long, Long> inputWriteOffReasonToGLAccountMap = new 
HashMap<>();
+
+        final Set<Long> existingWriteOffReasons = new HashSet<>();
+        if (writeOffReasonToGLAccountMappingArray != null) {
+            if (changes != null) {
+                
changes.put(LoanProductAccountingParams.WRITE_OFF_REASON_TO_EXPENSE_ACCOUNT_MAPPINGS.getValue(),
+                        
command.jsonFragment(LoanProductAccountingParams.WRITE_OFF_REASON_TO_EXPENSE_ACCOUNT_MAPPINGS.getValue()));
+            }
+
+            for (int i = 0; i < writeOffReasonToGLAccountMappingArray.size(); 
i++) {
+                final JsonObject jsonObject = 
writeOffReasonToGLAccountMappingArray.get(i).getAsJsonObject();
+                final Long expenseGlAccountId = 
jsonObject.get(LoanProductAccountingParams.EXPENSE_GL_ACCOUNT_ID.getValue()).getAsLong();
+                final Long writeOffReasonCodeValueId = 
jsonObject.get(LoanProductAccountingParams.WRITE_OFF_REASON_CODE_VALUE_ID.getValue())
+                        .getAsLong();
+                
inputWriteOffReasonToGLAccountMap.put(writeOffReasonCodeValueId, 
expenseGlAccountId);
+            }
+
+            // If input map is empty, delete all existing mappings
+            if (inputWriteOffReasonToGLAccountMap.isEmpty()) {
+                
this.accountMappingRepository.deleteAllInBatch(existingWriteOffReasonToGLAccountMappings);
+            } else {
+                for (final ProductToGLAccountMapping 
existingWriteOffReasonToGLAccountMapping : 
existingWriteOffReasonToGLAccountMappings) {
+                    final Long currentWriteOffReasonId = 
existingWriteOffReasonToGLAccountMapping.getWriteOffReason().getId();
+                    if (currentWriteOffReasonId != null) {
+                        existingWriteOffReasons.add(currentWriteOffReasonId);
+                        // update existing mappings (if required)
+                        if 
(inputWriteOffReasonToGLAccountMap.containsKey(currentWriteOffReasonId)) {
+                            final Long newGLAccountId = 
inputWriteOffReasonToGLAccountMap.get(currentWriteOffReasonId);
+                            if 
(!newGLAccountId.equals(existingWriteOffReasonToGLAccountMapping.getGlAccount().getId()))
 {
+                                final Optional<GLAccount> glAccount = 
accountRepository.findById(newGLAccountId);
+                                if (glAccount.isPresent()) {
+                                    
existingWriteOffReasonToGLAccountMapping.setGlAccount(glAccount.get());
+                                    
this.accountMappingRepository.saveAndFlush(existingWriteOffReasonToGLAccountMapping);
+                                }
+                            }
+                        } // deleted payment type
+                        else {
+                            
this.accountMappingRepository.delete(existingWriteOffReasonToGLAccountMapping);
+                        }
+                    }
+                }
+
+                // only the newly added
+                for (Map.Entry<Long, Long> entry : 
inputWriteOffReasonToGLAccountMap.entrySet().stream()
+                        .filter(e -> 
!existingWriteOffReasons.contains(e.getKey())).toList()) {
+                    saveWriteOffReasonToExpenseMapping(productId, 
entry.getKey(), entry.getValue(), portfolioProductType);
+                }
+            }
+        }
+    }
+

Review Comment:
   Same, 90% duplication with `update chargeoff reason mapping`. Would you mind 
to extract the differences and rather reuse the common parts?



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