diff --git a/fineract-provider/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java b/fineract-provider/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java index 7e2ee411d..b6b93f674 100755 --- a/fineract-provider/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java +++ b/fineract-provider/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java @@ -103,6 +103,29 @@ public class ProductToGLAccountMappingHelper { } } + public void createOrmergeProductToAccountMappingChanges(final JsonElement element, final String paramName, final Long productId, + final int accountTypeId, final Map changes, + final GLAccountType expectedAccountType, final PortfolioProductType portfolioProductType) { + final Long accountId = this.fromApiJsonHelper.extractLongNamed(paramName, element); + + // get the existing product + if (accountId != null) { + final ProductToGLAccountMapping accountMapping = this.accountMappingRepository.findCoreProductToFinAccountMapping(productId, + portfolioProductType.getValue(), accountTypeId); + if(accountMapping == null) { + final GLAccount glAccount = getAccountByIdAndType(paramName, expectedAccountType, accountId); + changes.put(paramName, accountId); + ProductToGLAccountMapping newAccountMapping = new ProductToGLAccountMapping(glAccount, productId, + portfolioProductType.getValue(), accountTypeId) ; + this.accountMappingRepository.save(newAccountMapping); + }else if (accountMapping.getGlAccount().getId() != accountId) { + final GLAccount glAccount = getAccountByIdAndType(paramName, expectedAccountType, accountId); + changes.put(paramName, accountId); + accountMapping.setGlAccount(glAccount); + this.accountMappingRepository.save(accountMapping); + } + } + } /** * Saves the payment type to Fund source mappings for a particular * product/product type (also populates the changes array if passed in) diff --git a/fineract-provider/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/SavingsProductToGLAccountMappingHelper.java b/fineract-provider/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/SavingsProductToGLAccountMappingHelper.java index 529fb4438..f888e60be 100755 --- a/fineract-provider/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/SavingsProductToGLAccountMappingHelper.java +++ b/fineract-provider/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/SavingsProductToGLAccountMappingHelper.java @@ -98,6 +98,12 @@ public class SavingsProductToGLAccountMappingHelper extends ProductToGLAccountMa GLAccountType.LIABILITY, PortfolioProductType.SAVING); } + public void createOrmergeSavingsToLiabilityAccountMappingChanges(final JsonElement element, final String paramName, final Long productId, + final int accountTypeId, final Map changes) { + createOrmergeProductToAccountMappingChanges(element, paramName, productId, accountTypeId, changes, + GLAccountType.LIABILITY, PortfolioProductType.SAVING); + } + /*** Abstractions for payments channel related to savings products ***/ public void savePaymentChannelToFundSourceMappings(final JsonCommand command, final JsonElement element, final Long productId, @@ -222,9 +228,9 @@ public class SavingsProductToGLAccountMappingHelper extends ProductToGLAccountMa mergeSavingsToLiabilityAccountMappingChanges(element, SAVINGS_PRODUCT_ACCOUNTING_PARAMS.TRANSFERS_SUSPENSE.getValue(), savingsProductId, CASH_ACCOUNTS_FOR_SAVINGS.TRANSFERS_SUSPENSE.getValue(), CASH_ACCOUNTS_FOR_SAVINGS.TRANSFERS_SUSPENSE.toString(), changes); - mergeSavingsToLiabilityAccountMappingChanges(element, SAVINGS_PRODUCT_ACCOUNTING_PARAMS.ESCHEAT_LIABILITY.getValue(), + createOrmergeSavingsToLiabilityAccountMappingChanges(element, SAVINGS_PRODUCT_ACCOUNTING_PARAMS.ESCHEAT_LIABILITY.getValue(), savingsProductId, CASH_ACCOUNTS_FOR_SAVINGS.ESCHEAT_LIABILITY.getValue(), - CASH_ACCOUNTS_FOR_SAVINGS.ESCHEAT_LIABILITY.toString(), changes); + changes); break; case ACCRUAL_PERIODIC: break;