initial work around MIFOSX-1634

This commit is contained in:
Vishwas Babu A J
2014-11-19 03:17:34 +05:30
parent 61a096f33c
commit 180c49fce9
7 changed files with 62 additions and 23 deletions
@@ -16,7 +16,7 @@ public class ProductToGLAccountMappingInvalidException extends AbstractPlatformD
public ProductToGLAccountMappingInvalidException(final String paramName, final String accountName, final Long accountId, public ProductToGLAccountMappingInvalidException(final String paramName, final String accountName, final Long accountId,
final String actualAccountCategory, final String expectedAccountCategory) { final String actualAccountCategory, final String expectedAccountCategory) {
super("error.msg." + paramName + ".invalid.account.type", "Passed in GLAccount " + paramName + " with Id " + accountId 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); + expectedAccountCategory, paramName, accountId, accountName, actualAccountCategory, expectedAccountCategory);
} }
} }
@@ -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.AccountingConstants.LOAN_PRODUCT_ACCOUNTING_PARAMS;
import org.mifosplatform.accounting.common.AccountingRuleType; import org.mifosplatform.accounting.common.AccountingRuleType;
import org.mifosplatform.accounting.glaccount.domain.GLAccountRepository; 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.domain.GLAccountType;
import org.mifosplatform.accounting.producttoaccountmapping.domain.PortfolioProductType; import org.mifosplatform.accounting.producttoaccountmapping.domain.PortfolioProductType;
import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository; import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository;
@@ -31,8 +32,10 @@ public class LoanProductToGLAccountMappingHelper extends ProductToGLAccountMappi
@Autowired @Autowired
public LoanProductToGLAccountMappingHelper(final GLAccountRepository glAccountRepository, public LoanProductToGLAccountMappingHelper(final GLAccountRepository glAccountRepository,
final ProductToGLAccountMappingRepository glAccountMappingRepository, final FromJsonHelper fromApiJsonHelper, final ProductToGLAccountMappingRepository glAccountMappingRepository, final FromJsonHelper fromApiJsonHelper,
final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper) { final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper,
super(glAccountRepository, glAccountMappingRepository, fromApiJsonHelper, codeValueRepositoryWrapper, chargeRepositoryWrapper); final GLAccountRepositoryWrapper accountRepositoryWrapper) {
super(glAccountRepository, glAccountMappingRepository, fromApiJsonHelper, codeValueRepositoryWrapper, chargeRepositoryWrapper,
accountRepositoryWrapper);
} }
/*** Set of abstractions for saving Loan Products to GL Account Mappings ***/ /*** 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, public void saveChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId,
final Map<String, Object> changes) { final Map<String, Object> changes) {
// save both fee and penalty charges // save both fee and penalty charges
saveChargesToIncomeAccountMappings(command, element, productId, changes, PortfolioProductType.LOAN, true); saveChargesToIncomeOrLiabilityAccountMappings(command, element, productId, changes, PortfolioProductType.LOAN, true);
saveChargesToIncomeAccountMappings(command, element, productId, changes, PortfolioProductType.LOAN, false); saveChargesToIncomeOrLiabilityAccountMappings(command, element, productId, changes, PortfolioProductType.LOAN, false);
} }
public void updateChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId, public void updateChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId,
@@ -5,6 +5,7 @@
*/ */
package org.mifosplatform.accounting.producttoaccountmapping.service; package org.mifosplatform.accounting.producttoaccountmapping.service;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; 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.common.AccountingConstants.LOAN_PRODUCT_ACCOUNTING_PARAMS;
import org.mifosplatform.accounting.glaccount.domain.GLAccount; import org.mifosplatform.accounting.glaccount.domain.GLAccount;
import org.mifosplatform.accounting.glaccount.domain.GLAccountRepository; 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.domain.GLAccountType;
import org.mifosplatform.accounting.glaccount.exception.GLAccountNotFoundException;
import org.mifosplatform.accounting.producttoaccountmapping.domain.PortfolioProductType; import org.mifosplatform.accounting.producttoaccountmapping.domain.PortfolioProductType;
import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMapping; import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMapping;
import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository; import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository;
@@ -40,6 +41,7 @@ import com.google.gson.JsonObject;
public class ProductToGLAccountMappingHelper { public class ProductToGLAccountMappingHelper {
protected final GLAccountRepository accountRepository; protected final GLAccountRepository accountRepository;
protected final GLAccountRepositoryWrapper accountRepositoryWrapper;
protected final ProductToGLAccountMappingRepository accountMappingRepository; protected final ProductToGLAccountMappingRepository accountMappingRepository;
protected final FromJsonHelper fromApiJsonHelper; protected final FromJsonHelper fromApiJsonHelper;
private final CodeValueRepositoryWrapper codeValueRepositoryWrapper; private final CodeValueRepositoryWrapper codeValueRepositoryWrapper;
@@ -48,12 +50,14 @@ public class ProductToGLAccountMappingHelper {
@Autowired @Autowired
public ProductToGLAccountMappingHelper(final GLAccountRepository glAccountRepository, public ProductToGLAccountMappingHelper(final GLAccountRepository glAccountRepository,
final ProductToGLAccountMappingRepository glAccountMappingRepository, final FromJsonHelper fromApiJsonHelper, 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.accountRepository = glAccountRepository;
this.accountMappingRepository = glAccountMappingRepository; this.accountMappingRepository = glAccountMappingRepository;
this.fromApiJsonHelper = fromApiJsonHelper; this.fromApiJsonHelper = fromApiJsonHelper;
this.codeValueRepositoryWrapper = codeValueRepositoryWrapper; this.codeValueRepositoryWrapper = codeValueRepositoryWrapper;
this.chargeRepositoryWrapper = chargeRepositoryWrapper; 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) * product/product type (also populates the changes array if passed in)
* *
* @param command * @param command
@@ -123,7 +127,7 @@ public class ProductToGLAccountMappingHelper {
* @param productId * @param productId
* @param changes * @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<String, Object> changes, final PortfolioProductType portfolioProductType, final boolean isPenalty) { final Map<String, Object> changes, final PortfolioProductType portfolioProductType, final boolean isPenalty) {
String arrayName; String arrayName;
if (isPenalty) { if (isPenalty) {
@@ -213,8 +217,15 @@ public class ProductToGLAccountMappingHelper {
if (inputChargeToIncomeAccountMap.containsKey(currentCharge)) { if (inputChargeToIncomeAccountMap.containsKey(currentCharge)) {
final Long newGLAccountId = inputChargeToIncomeAccountMap.get(currentCharge); final Long newGLAccountId = inputChargeToIncomeAccountMap.get(currentCharge);
if (newGLAccountId != chargeToIncomeAccountMapping.getGlAccount().getId()) { if (newGLAccountId != chargeToIncomeAccountMapping.getGlAccount().getId()) {
final GLAccount glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), final GLAccount glAccount;
GLAccountType.INCOME, newGLAccountId); if (isPenalty) {
glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(),
GLAccountType.INCOME, newGLAccountId);
} else {
List<GLAccountType> allowedAccountTypes = getAllowedAccountTypesForFeeMapping();
glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(),
allowedAccountTypes, newGLAccountId);
}
chargeToIncomeAccountMapping.setGlAccount(glAccount); chargeToIncomeAccountMapping.setGlAccount(glAccount);
this.accountMappingRepository.save(chargeToIncomeAccountMapping); this.accountMappingRepository.save(chargeToIncomeAccountMapping);
} }
@@ -341,16 +352,20 @@ public class ProductToGLAccountMappingHelper {
// TODO Vishwas: Need to validate if given charge is fee or Penalty // TODO Vishwas: Need to validate if given charge is fee or Penalty
// based on input condition // based on input condition
final GLAccount glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), GLAccount glAccount;
GLAccountType.INCOME, incomeAccountId);
/** /**
* Both CASH and Accrual placeholders have the same value for income * Both CASH and Accrual placeholders have the same value for income
* from Interest and penalties * from Interest and penalties
**/ **/
CASH_ACCOUNTS_FOR_LOAN placeHolderAccountType; CASH_ACCOUNTS_FOR_LOAN placeHolderAccountType;
if (isPenalty) { if (isPenalty) {
glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), GLAccountType.INCOME,
incomeAccountId);
placeHolderAccountType = CASH_ACCOUNTS_FOR_LOAN.INCOME_FROM_PENALTIES; placeHolderAccountType = CASH_ACCOUNTS_FOR_LOAN.INCOME_FROM_PENALTIES;
} else { } else {
List<GLAccountType> allowedAccountTypes = getAllowedAccountTypesForFeeMapping();
glAccount = getAccountByIdAndType(LOAN_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), allowedAccountTypes,
incomeAccountId);
placeHolderAccountType = CASH_ACCOUNTS_FOR_LOAN.INCOME_FROM_FEES; placeHolderAccountType = CASH_ACCOUNTS_FOR_LOAN.INCOME_FROM_FEES;
} }
final ProductToGLAccountMapping accountMapping = new ProductToGLAccountMapping(glAccount, productId, final ProductToGLAccountMapping accountMapping = new ProductToGLAccountMapping(glAccount, productId,
@@ -358,6 +373,13 @@ public class ProductToGLAccountMappingHelper {
this.accountMappingRepository.save(accountMapping); this.accountMappingRepository.save(accountMapping);
} }
private List<GLAccountType> getAllowedAccountTypesForFeeMapping() {
List<GLAccountType> 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 * Fetches account with a particular Id and throws and Exception it is not
* of the expected Account Category ('ASSET','liability' etc) * of the expected Account Category ('ASSET','liability' etc)
@@ -368,8 +390,7 @@ public class ProductToGLAccountMappingHelper {
* @return * @return
*/ */
public GLAccount getAccountByIdAndType(final String paramName, final GLAccountType expectedAccountType, final Long accountId) { public GLAccount getAccountByIdAndType(final String paramName, final GLAccountType expectedAccountType, final Long accountId) {
final GLAccount glAccount = this.accountRepository.findOne(accountId); final GLAccount glAccount = this.accountRepositoryWrapper.findOneWithNotFoundDetection(accountId);
if (glAccount == null) { throw new GLAccountNotFoundException(accountId); }
// validate account is of the expected Type // validate account is of the expected Type
if (glAccount.getType().intValue() != expectedAccountType.getValue()) { throw new ProductToGLAccountMappingInvalidException( if (glAccount.getType().intValue() != expectedAccountType.getValue()) { throw new ProductToGLAccountMappingInvalidException(
@@ -378,6 +399,18 @@ public class ProductToGLAccountMappingHelper {
return glAccount; return glAccount;
} }
public GLAccount getAccountByIdAndType(final String paramName, final List<GLAccountType> expectedAccountTypes, final Long accountId) {
final GLAccount glAccount = this.accountRepositoryWrapper.findOneWithNotFoundDetection(accountId);
// validate account is of the expected Type
List<Integer> 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) { public void deleteProductToGLAccountMapping(final Long loanProductId, final PortfolioProductType portfolioProductType) {
final List<ProductToGLAccountMapping> productToGLAccountMappings = this.accountMappingRepository.findByProductIdAndProductType( final List<ProductToGLAccountMapping> productToGLAccountMappings = this.accountMappingRepository.findByProductIdAndProductType(
loanProductId, portfolioProductType.getValue()); loanProductId, portfolioProductType.getValue());
@@ -17,7 +17,7 @@ public interface ProductToGLAccountMappingReadPlatformService {
public List<PaymentTypeToGLAccountMapper> fetchPaymentTypeToFundSourceMappingsForLoanProduct(final Long loanProductId); public List<PaymentTypeToGLAccountMapper> fetchPaymentTypeToFundSourceMappingsForLoanProduct(final Long loanProductId);
public List<ChargeToGLAccountMapper> fetchFeeToIncomeAccountMappingsForLoanProduct(final Long loanProductId); public List<ChargeToGLAccountMapper> fetchFeeToIncomeOrLiabilityAccountMappingsForLoanProduct(final Long loanProductId);
public List<ChargeToGLAccountMapper> fetchPenaltyToIncomeAccountMappingsForLoanProduct(final Long loanProductId); public List<ChargeToGLAccountMapper> fetchPenaltyToIncomeAccountMappingsForLoanProduct(final Long loanProductId);
@@ -262,7 +262,7 @@ public class ProductToGLAccountMappingReadPlatformServiceImpl implements Product
} }
@Override @Override
public List<ChargeToGLAccountMapper> fetchFeeToIncomeAccountMappingsForLoanProduct(final Long loanProductId) { public List<ChargeToGLAccountMapper> fetchFeeToIncomeOrLiabilityAccountMappingsForLoanProduct(final Long loanProductId) {
return fetchChargeToIncomeAccountMappings(PortfolioProductType.LOAN, loanProductId, false); return fetchChargeToIncomeAccountMappings(PortfolioProductType.LOAN, loanProductId, false);
} }
@@ -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.AccountingConstants.SAVINGS_PRODUCT_ACCOUNTING_PARAMS;
import org.mifosplatform.accounting.common.AccountingRuleType; import org.mifosplatform.accounting.common.AccountingRuleType;
import org.mifosplatform.accounting.glaccount.domain.GLAccountRepository; 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.domain.GLAccountType;
import org.mifosplatform.accounting.producttoaccountmapping.domain.PortfolioProductType; import org.mifosplatform.accounting.producttoaccountmapping.domain.PortfolioProductType;
import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository; import org.mifosplatform.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository;
@@ -30,8 +31,10 @@ public class SavingsProductToGLAccountMappingHelper extends ProductToGLAccountMa
@Autowired @Autowired
public SavingsProductToGLAccountMappingHelper(final GLAccountRepository glAccountRepository, public SavingsProductToGLAccountMappingHelper(final GLAccountRepository glAccountRepository,
final ProductToGLAccountMappingRepository glAccountMappingRepository, final FromJsonHelper fromApiJsonHelper, final ProductToGLAccountMappingRepository glAccountMappingRepository, final FromJsonHelper fromApiJsonHelper,
final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper) { final CodeValueRepositoryWrapper codeValueRepositoryWrapper, final ChargeRepositoryWrapper chargeRepositoryWrapper,
super(glAccountRepository, glAccountMappingRepository, fromApiJsonHelper, codeValueRepositoryWrapper, chargeRepositoryWrapper); final GLAccountRepositoryWrapper accountRepositoryWrapper) {
super(glAccountRepository, glAccountMappingRepository, fromApiJsonHelper, codeValueRepositoryWrapper, chargeRepositoryWrapper,
accountRepositoryWrapper);
} }
/*** Set of abstractions for saving Saving Products to GL Account Mappings ***/ /*** 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, public void saveChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId,
final Map<String, Object> changes) { final Map<String, Object> changes) {
// save both fee and penalty charges // save both fee and penalty charges
saveChargesToIncomeAccountMappings(command, element, productId, changes, PortfolioProductType.SAVING, true); saveChargesToIncomeOrLiabilityAccountMappings(command, element, productId, changes, PortfolioProductType.SAVING, true);
saveChargesToIncomeAccountMappings(command, element, productId, changes, PortfolioProductType.SAVING, false); saveChargesToIncomeOrLiabilityAccountMappings(command, element, productId, changes, PortfolioProductType.SAVING, false);
} }
public void updateChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId, public void updateChargesToIncomeAccountMappings(final JsonCommand command, final JsonElement element, final Long productId,
@@ -205,7 +205,7 @@ public class LoanProductsApiResource {
.accountingRuleType().getId().intValue()); .accountingRuleType().getId().intValue());
paymentChannelToFundSourceMappings = this.accountMappingReadPlatformService paymentChannelToFundSourceMappings = this.accountMappingReadPlatformService
.fetchPaymentTypeToFundSourceMappingsForLoanProduct(productId); .fetchPaymentTypeToFundSourceMappingsForLoanProduct(productId);
feeToGLAccountMappings = this.accountMappingReadPlatformService.fetchFeeToIncomeAccountMappingsForLoanProduct(productId); feeToGLAccountMappings = this.accountMappingReadPlatformService.fetchFeeToIncomeOrLiabilityAccountMappingsForLoanProduct(productId);
penaltyToGLAccountMappings = this.accountMappingReadPlatformService penaltyToGLAccountMappings = this.accountMappingReadPlatformService
.fetchPenaltyToIncomeAccountMappingsForLoanProduct(productId); .fetchPenaltyToIncomeAccountMappingsForLoanProduct(productId);
loanProduct = LoanProductData.withAccountingDetails(loanProduct, accountingMappings, paymentChannelToFundSourceMappings, loanProduct = LoanProductData.withAccountingDetails(loanProduct, accountingMappings, paymentChannelToFundSourceMappings,