From 180c49fce9a33d4a2f2317bc110ee7cce98d8c0f Mon Sep 17 00:00:00 2001 From: Vishwas Babu A J Date: Wed, 19 Nov 2014 03:17:34 +0530 Subject: [PATCH] initial work around MIFOSX-1634 --- ...uctToGLAccountMappingInvalidException.java | 2 +- .../LoanProductToGLAccountMappingHelper.java | 11 ++-- .../ProductToGLAccountMappingHelper.java | 53 +++++++++++++++---- ...ToGLAccountMappingReadPlatformService.java | 4 +- ...AccountMappingReadPlatformServiceImpl.java | 2 +- ...avingsProductToGLAccountMappingHelper.java | 11 ++-- .../api/LoanProductsApiResource.java | 2 +- 7 files changed, 62 insertions(+), 23 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/exception/ProductToGLAccountMappingInvalidException.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/exception/ProductToGLAccountMappingInvalidException.java index 4e888a7f8..c4180a566 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/exception/ProductToGLAccountMappingInvalidException.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/exception/ProductToGLAccountMappingInvalidException.java @@ -16,7 +16,7 @@ public class ProductToGLAccountMappingInvalidException extends AbstractPlatformD public ProductToGLAccountMappingInvalidException(final String paramName, final String accountName, final Long accountId, final String actualAccountCategory, final String expectedAccountCategory) { super("error.msg." + paramName + ".invalid.account.type", "Passed in GLAccount " + paramName + " with Id " + accountId - + "maps to the account " + accountName + " of type " + actualAccountCategory + ", the expected account type was " + + "maps to the account " + accountName + " of type " + actualAccountCategory + ", the expected account type was one among" + expectedAccountCategory, paramName, accountId, accountName, actualAccountCategory, expectedAccountCategory); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/LoanProductToGLAccountMappingHelper.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/LoanProductToGLAccountMappingHelper.java index 49dc9cf26..1ef834190 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/LoanProductToGLAccountMappingHelper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/LoanProductToGLAccountMappingHelper.java @@ -13,6 +13,7 @@ import org.mifosplatform.accounting.common.AccountingConstants.CASH_ACCOUNTS_FOR import org.mifosplatform.accounting.common.AccountingConstants.LOAN_PRODUCT_ACCOUNTING_PARAMS; import org.mifosplatform.accounting.common.AccountingRuleType; import org.mifosplatform.accounting.glaccount.domain.GLAccountRepository; +import org.mifosplatform.accounting.glaccount.domain.GLAccountRepositoryWrapper; import org.mifosplatform.accounting.glaccount.domain.GLAccountType; import org.mifosplatform.accounting.producttoaccountmapping.domain.PortfolioProductType; import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository; @@ -31,8 +32,10 @@ public class LoanProductToGLAccountMappingHelper extends ProductToGLAccountMappi @Autowired public LoanProductToGLAccountMappingHelper(final GLAccountRepository glAccountRepository, final ProductToGLAccountMappingRepository glAccountMappingRepository, final FromJsonHelper fromApiJsonHelper, - final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper) { - super(glAccountRepository, glAccountMappingRepository, fromApiJsonHelper, codeValueRepositoryWrapper, chargeRepositoryWrapper); + final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper, + final GLAccountRepositoryWrapper accountRepositoryWrapper) { + super(glAccountRepository, glAccountMappingRepository, fromApiJsonHelper, codeValueRepositoryWrapper, chargeRepositoryWrapper, + accountRepositoryWrapper); } /*** Set of abstractions for saving Loan Products to GL Account Mappings ***/ @@ -97,8 +100,8 @@ public class LoanProductToGLAccountMappingHelper extends ProductToGLAccountMappi public void saveChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId, final Map changes) { // save both fee and penalty charges - saveChargesToIncomeAccountMappings(command, element, productId, changes, PortfolioProductType.LOAN, true); - saveChargesToIncomeAccountMappings(command, element, productId, changes, PortfolioProductType.LOAN, false); + saveChargesToIncomeOrLiabilityAccountMappings(command, element, productId, changes, PortfolioProductType.LOAN, true); + saveChargesToIncomeOrLiabilityAccountMappings(command, element, productId, changes, PortfolioProductType.LOAN, false); } public void updateChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId, diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java index 6b59c31f1..92770b2e5 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java @@ -5,6 +5,7 @@ */ package org.mifosplatform.accounting.producttoaccountmapping.service; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -15,8 +16,8 @@ import org.mifosplatform.accounting.common.AccountingConstants.CASH_ACCOUNTS_FOR import org.mifosplatform.accounting.common.AccountingConstants.LOAN_PRODUCT_ACCOUNTING_PARAMS; import org.mifosplatform.accounting.glaccount.domain.GLAccount; import org.mifosplatform.accounting.glaccount.domain.GLAccountRepository; +import org.mifosplatform.accounting.glaccount.domain.GLAccountRepositoryWrapper; import org.mifosplatform.accounting.glaccount.domain.GLAccountType; -import org.mifosplatform.accounting.glaccount.exception.GLAccountNotFoundException; import org.mifosplatform.accounting.producttoaccountmapping.domain.PortfolioProductType; import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMapping; import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository; @@ -40,6 +41,7 @@ import com.google.gson.JsonObject; public class ProductToGLAccountMappingHelper { protected final GLAccountRepository accountRepository; + protected final GLAccountRepositoryWrapper accountRepositoryWrapper; protected final ProductToGLAccountMappingRepository accountMappingRepository; protected final FromJsonHelper fromApiJsonHelper; private final CodeValueRepositoryWrapper codeValueRepositoryWrapper; @@ -48,12 +50,14 @@ public class ProductToGLAccountMappingHelper { @Autowired public ProductToGLAccountMappingHelper(final GLAccountRepository glAccountRepository, final ProductToGLAccountMappingRepository glAccountMappingRepository, final FromJsonHelper fromApiJsonHelper, - final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper) { + final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper, + final GLAccountRepositoryWrapper accountRepositoryWrapper) { this.accountRepository = glAccountRepository; this.accountMappingRepository = glAccountMappingRepository; this.fromApiJsonHelper = fromApiJsonHelper; this.codeValueRepositoryWrapper = codeValueRepositoryWrapper; this.chargeRepositoryWrapper = chargeRepositoryWrapper; + this.accountRepositoryWrapper = accountRepositoryWrapper; } @@ -115,7 +119,7 @@ public class ProductToGLAccountMappingHelper { } /** - * Saves the Charge to Income account mappings for a particular + * Saves the Charge to Income / Liability account mappings for a particular * product/product type (also populates the changes array if passed in) * * @param command @@ -123,7 +127,7 @@ public class ProductToGLAccountMappingHelper { * @param productId * @param changes */ - public void saveChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId, + public void saveChargesToIncomeOrLiabilityAccountMappings(final JsonCommand command, final JsonElement element, final Long productId, final Map changes, final PortfolioProductType portfolioProductType, final boolean isPenalty) { String arrayName; if (isPenalty) { @@ -213,8 +217,15 @@ public class ProductToGLAccountMappingHelper { if (inputChargeToIncomeAccountMap.containsKey(currentCharge)) { final Long newGLAccountId = inputChargeToIncomeAccountMap.get(currentCharge); if (newGLAccountId != chargeToIncomeAccountMapping.getGlAccount().getId()) { - final GLAccount glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), - GLAccountType.INCOME, newGLAccountId); + final GLAccount glAccount; + if (isPenalty) { + glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), + GLAccountType.INCOME, newGLAccountId); + } else { + List allowedAccountTypes = getAllowedAccountTypesForFeeMapping(); + glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), + allowedAccountTypes, newGLAccountId); + } chargeToIncomeAccountMapping.setGlAccount(glAccount); this.accountMappingRepository.save(chargeToIncomeAccountMapping); } @@ -341,16 +352,20 @@ public class ProductToGLAccountMappingHelper { // TODO Vishwas: Need to validate if given charge is fee or Penalty // based on input condition - final GLAccount glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), - GLAccountType.INCOME, incomeAccountId); + GLAccount glAccount; /** * Both CASH and Accrual placeholders have the same value for income * from Interest and penalties **/ CASH_ACCOUNTS_FOR_LOAN placeHolderAccountType; if (isPenalty) { + glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), GLAccountType.INCOME, + incomeAccountId); placeHolderAccountType = CASH_ACCOUNTS_FOR_LOAN.INCOME_FROM_PENALTIES; } else { + List allowedAccountTypes = getAllowedAccountTypesForFeeMapping(); + glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), allowedAccountTypes, + incomeAccountId); placeHolderAccountType = CASH_ACCOUNTS_FOR_LOAN.INCOME_FROM_FEES; } final ProductToGLAccountMapping accountMapping = new ProductToGLAccountMapping(glAccount, productId, @@ -358,6 +373,13 @@ public class ProductToGLAccountMappingHelper { this.accountMappingRepository.save(accountMapping); } + private List getAllowedAccountTypesForFeeMapping() { + List allowedAccountTypes = new ArrayList<>(); + allowedAccountTypes.add(GLAccountType.INCOME); + allowedAccountTypes.add(GLAccountType.LIABILITY); + return allowedAccountTypes; + } + /** * Fetches account with a particular Id and throws and Exception it is not * of the expected Account Category ('ASSET','liability' etc) @@ -368,8 +390,7 @@ public class ProductToGLAccountMappingHelper { * @return */ public GLAccount getAccountByIdAndType(final String paramName, final GLAccountType expectedAccountType, final Long accountId) { - final GLAccount glAccount = this.accountRepository.findOne(accountId); - if (glAccount == null) { throw new GLAccountNotFoundException(accountId); } + final GLAccount glAccount = this.accountRepositoryWrapper.findOneWithNotFoundDetection(accountId); // validate account is of the expected Type if (glAccount.getType().intValue() != expectedAccountType.getValue()) { throw new ProductToGLAccountMappingInvalidException( @@ -378,6 +399,18 @@ public class ProductToGLAccountMappingHelper { return glAccount; } + public GLAccount getAccountByIdAndType(final String paramName, final List expectedAccountTypes, final Long accountId) { + final GLAccount glAccount = this.accountRepositoryWrapper.findOneWithNotFoundDetection(accountId); + // validate account is of the expected Type + List glAccountTypeValues = new ArrayList<>(); + for (GLAccountType glAccountType : expectedAccountTypes) { + glAccountTypeValues.add(glAccountType.getValue()); + } + if (!glAccountTypeValues.contains(glAccount.getType())) { throw new ProductToGLAccountMappingInvalidException(paramName, + glAccount.getName(), accountId, GLAccountType.fromInt(glAccount.getType()).toString(), glAccountTypeValues.toString()); } + return glAccount; + } + public void deleteProductToGLAccountMapping(final Long loanProductId, final PortfolioProductType portfolioProductType) { final List productToGLAccountMappings = this.accountMappingRepository.findByProductIdAndProductType( loanProductId, portfolioProductType.getValue()); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingReadPlatformService.java index 8c56d01ff..85fdf8ed1 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingReadPlatformService.java @@ -17,14 +17,14 @@ public interface ProductToGLAccountMappingReadPlatformService { public List fetchPaymentTypeToFundSourceMappingsForLoanProduct(final Long loanProductId); - public List fetchFeeToIncomeAccountMappingsForLoanProduct(final Long loanProductId); + public List fetchFeeToIncomeOrLiabilityAccountMappingsForLoanProduct(final Long loanProductId); public List fetchPenaltyToIncomeAccountMappingsForLoanProduct(final Long loanProductId); public Map fetchAccountMappingDetailsForSavingsProduct(final Long savingsProductId, final Integer accountingType); public List fetchPaymentTypeToFundSourceMappingsForSavingsProduct(final Long savingsProductId); - + public List fetchFeeToIncomeAccountMappingsForSavingsProduct(final Long savingsProductId); public List fetchPenaltyToIncomeAccountMappingsForSavingsProduct(final Long savingsProductId); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingReadPlatformServiceImpl.java index b49e6ea49..7d5e789c5 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/ProductToGLAccountMappingReadPlatformServiceImpl.java @@ -262,7 +262,7 @@ public class ProductToGLAccountMappingReadPlatformServiceImpl implements Product } @Override - public List fetchFeeToIncomeAccountMappingsForLoanProduct(final Long loanProductId) { + public List fetchFeeToIncomeOrLiabilityAccountMappingsForLoanProduct(final Long loanProductId) { return fetchChargeToIncomeAccountMappings(PortfolioProductType.LOAN, loanProductId, false); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/SavingsProductToGLAccountMappingHelper.java b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/SavingsProductToGLAccountMappingHelper.java index 3ddb9464b..b4650d3ab 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/SavingsProductToGLAccountMappingHelper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/accounting/producttoaccountmapping/service/SavingsProductToGLAccountMappingHelper.java @@ -12,6 +12,7 @@ import org.mifosplatform.accounting.common.AccountingConstants.CASH_ACCOUNTS_FOR import org.mifosplatform.accounting.common.AccountingConstants.SAVINGS_PRODUCT_ACCOUNTING_PARAMS; import org.mifosplatform.accounting.common.AccountingRuleType; import org.mifosplatform.accounting.glaccount.domain.GLAccountRepository; +import org.mifosplatform.accounting.glaccount.domain.GLAccountRepositoryWrapper; import org.mifosplatform.accounting.glaccount.domain.GLAccountType; import org.mifosplatform.accounting.producttoaccountmapping.domain.PortfolioProductType; import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository; @@ -30,8 +31,10 @@ public class SavingsProductToGLAccountMappingHelper extends ProductToGLAccountMa @Autowired public SavingsProductToGLAccountMappingHelper(final GLAccountRepository glAccountRepository, final ProductToGLAccountMappingRepository glAccountMappingRepository, final FromJsonHelper fromApiJsonHelper, - final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper) { - super(glAccountRepository, glAccountMappingRepository, fromApiJsonHelper, codeValueRepositoryWrapper, chargeRepositoryWrapper); + final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper, + final GLAccountRepositoryWrapper accountRepositoryWrapper) { + super(glAccountRepository, glAccountMappingRepository, fromApiJsonHelper, codeValueRepositoryWrapper, chargeRepositoryWrapper, + accountRepositoryWrapper); } /*** Set of abstractions for saving Saving Products to GL Account Mappings ***/ @@ -97,8 +100,8 @@ public class SavingsProductToGLAccountMappingHelper extends ProductToGLAccountMa public void saveChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId, final Map changes) { // save both fee and penalty charges - saveChargesToIncomeAccountMappings(command, element, productId, changes, PortfolioProductType.SAVING, true); - saveChargesToIncomeAccountMappings(command, element, productId, changes, PortfolioProductType.SAVING, false); + saveChargesToIncomeOrLiabilityAccountMappings(command, element, productId, changes, PortfolioProductType.SAVING, true); + saveChargesToIncomeOrLiabilityAccountMappings(command, element, productId, changes, PortfolioProductType.SAVING, false); } public void updateChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId, diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/api/LoanProductsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/api/LoanProductsApiResource.java index 2830122a0..71bec9c97 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/api/LoanProductsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/api/LoanProductsApiResource.java @@ -205,7 +205,7 @@ public class LoanProductsApiResource { .accountingRuleType().getId().intValue()); paymentChannelToFundSourceMappings = this.accountMappingReadPlatformService .fetchPaymentTypeToFundSourceMappingsForLoanProduct(productId); - feeToGLAccountMappings = this.accountMappingReadPlatformService.fetchFeeToIncomeAccountMappingsForLoanProduct(productId); + feeToGLAccountMappings = this.accountMappingReadPlatformService.fetchFeeToIncomeOrLiabilityAccountMappingsForLoanProduct(productId); penaltyToGLAccountMappings = this.accountMappingReadPlatformService .fetchPenaltyToIncomeAccountMappingsForLoanProduct(productId); loanProduct = LoanProductData.withAccountingDetails(loanProduct, accountingMappings, paymentChannelToFundSourceMappings,