Skip to content

Commit 8442c2d

Browse files
authored
Merge pull request #5804
FINERACT-2455: WC - Advance Accounting Entries - Loan product configuration
2 parents 259ceae + 65f4a38 commit 8442c2d

14 files changed

Lines changed: 977 additions & 6 deletions

File tree

fineract-accounting/src/main/java/org/apache/fineract/accounting/producttoaccountmapping/service/ProductToGLAccountMappingHelper.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,18 @@ public void savePaymentChannelToFundSourceMappings(final JsonCommand command, fi
167167
}
168168
for (int i = 0; i < paymentChannelMappingArray.size(); i++) {
169169
final JsonObject jsonObject = paymentChannelMappingArray.get(i).getAsJsonObject();
170-
final Long paymentTypeId = jsonObject.get(LoanProductAccountingParams.PAYMENT_TYPE.getValue()).getAsLong();
171-
final Long paymentSpecificFundAccountId = jsonObject.get(LoanProductAccountingParams.FUND_SOURCE.getValue()).getAsLong();
170+
JsonElement jsonPaymentTypeId = jsonObject.get(LoanProductAccountingParams.PAYMENT_TYPE.getValue());
171+
JsonElement jsonElementFoundId = jsonObject.get(LoanProductAccountingParams.FUND_SOURCE.getValue());
172+
if (jsonPaymentTypeId == null) {
173+
throw new PlatformApiDataValidationException("payment.type.id.is.mandatory", "field: paymentTypeId is mandatory",
174+
LoanProductAccountingParams.PAYMENT_TYPE.getValue());
175+
}
176+
if (jsonElementFoundId == null) {
177+
throw new PlatformApiDataValidationException("fund.source.account.id.is.mandatory",
178+
"field: fundSourceAccountId is mandatory", LoanProductAccountingParams.FUND_SOURCE.getValue());
179+
}
180+
final Long paymentTypeId = jsonPaymentTypeId.getAsLong();
181+
final Long paymentSpecificFundAccountId = jsonElementFoundId.getAsLong();
172182
savePaymentChannelToFundSourceMapping(productId, paymentTypeId, paymentSpecificFundAccountId, portfolioProductType);
173183
}
174184
}
@@ -200,7 +210,18 @@ public void saveChargesToGLAccountMappings(final JsonCommand command, final Json
200210
}
201211
for (int i = 0; i < chargeToIncomeAccountMappingArray.size(); i++) {
202212
final JsonObject jsonObject = chargeToIncomeAccountMappingArray.get(i).getAsJsonObject();
203-
final Long chargeId = jsonObject.get(LoanProductAccountingParams.CHARGE_ID.getValue()).getAsLong();
213+
JsonElement chargeIdJson = jsonObject.get(LoanProductAccountingParams.CHARGE_ID.getValue());
214+
JsonElement incomeAccountJson = jsonObject.get(LoanProductAccountingParams.INCOME_ACCOUNT_ID.getValue());
215+
if (chargeIdJson == null) {
216+
throw new PlatformApiDataValidationException("charge.id.is.mandatory", "chargeId is mandatory",
217+
LoanProductAccountingParams.CHARGE_ID.getValue());
218+
}
219+
if (incomeAccountJson == null) {
220+
throw new PlatformApiDataValidationException("income.account.id.is.mandatory", "incomeAccountId is mandatory",
221+
LoanProductAccountingParams.INCOME_ACCOUNT_ID.getValue());
222+
223+
}
224+
final Long chargeId = chargeIdJson.getAsLong();
204225
final Long incomeAccountId = jsonObject.get(LoanProductAccountingParams.INCOME_ACCOUNT_ID.getValue()).getAsLong();
205226
saveChargeToFundSourceMapping(productId, chargeId, incomeAccountId, portfolioProductType, isPenalty);
206227
}
@@ -222,8 +243,20 @@ public void saveReasonToGLAccountMappings(final JsonCommand command, final JsonE
222243

223244
for (int i = 0; i < reasonToExpenseAccountMappingArray.size(); i++) {
224245
final JsonObject jsonObject = reasonToExpenseAccountMappingArray.get(i).getAsJsonObject();
225-
final Long reasonId = jsonObject.get(reasonCodeValueIdParam.getValue()).getAsLong();
226-
final Long expenseAccountId = jsonObject.get(LoanProductAccountingParams.EXPENSE_GL_ACCOUNT_ID.getValue()).getAsLong();
246+
247+
JsonElement reasonIdJson = jsonObject.get(reasonCodeValueIdParam.getValue());
248+
JsonElement expenseAccountJson = jsonObject.get(LoanProductAccountingParams.EXPENSE_GL_ACCOUNT_ID.getValue());
249+
if (reasonIdJson == null) {
250+
throw new PlatformApiDataValidationException(reasonCodeValueIdParam.getValue() + ".is.mandatory",
251+
reasonCodeValueIdParam.getValue() + " is mandatory", reasonCodeValueIdParam.getValue());
252+
}
253+
if (expenseAccountJson == null) {
254+
throw new PlatformApiDataValidationException("expense.gl.account.id.is.mandatory", "expenseGlAccountId is mandatory",
255+
LoanProductAccountingParams.EXPENSE_GL_ACCOUNT_ID.getValue());
256+
}
257+
258+
final Long reasonId = reasonIdJson.getAsLong();
259+
final Long expenseAccountId = expenseAccountJson.getAsLong();
227260

228261
saveReasonToExpenseMapping(productId, reasonId, expenseAccountId, portfolioProductType, cashAccountsForLoan);
229262
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.accounting.producttoaccountmapping.service;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import lombok.RequiredArgsConstructor;
24+
import org.apache.fineract.accounting.glaccount.data.GLAccountData;
25+
import org.apache.fineract.accounting.producttoaccountmapping.data.AdvancedMappingToExpenseAccountData;
26+
import org.apache.fineract.accounting.producttoaccountmapping.data.ChargeToGLAccountMapper;
27+
import org.apache.fineract.accounting.producttoaccountmapping.data.PaymentTypeToGLAccountMapper;
28+
import org.apache.fineract.accounting.producttoaccountmapping.domain.ProductToGLAccountMapping;
29+
import org.apache.fineract.accounting.producttoaccountmapping.domain.ProductToGLAccountMappingRepository;
30+
import org.apache.fineract.infrastructure.codes.data.CodeValueData;
31+
import org.apache.fineract.infrastructure.codes.mapper.CodeValueMapper;
32+
import org.apache.fineract.portfolio.PortfolioProductType;
33+
import org.apache.fineract.portfolio.charge.data.ChargeData;
34+
import org.apache.fineract.portfolio.paymenttype.data.PaymentTypeData;
35+
import org.springframework.stereotype.Component;
36+
37+
@Component
38+
@RequiredArgsConstructor
39+
public class WorkingCapitalLoanProductAdvancedAccountingReadHelper {
40+
41+
private final ProductToGLAccountMappingRepository productToGLAccountMappingRepository;
42+
private final CodeValueMapper codeValueMapper;
43+
44+
public List<PaymentTypeToGLAccountMapper> fetchPaymentTypeToFundSourceMappings(final Long wcLoanProductId) {
45+
final List<ProductToGLAccountMapping> mappings = productToGLAccountMappingRepository.findAllPaymentTypeMappings(wcLoanProductId,
46+
PortfolioProductType.WORKING_CAPITAL_LOAN.getValue());
47+
final List<PaymentTypeToGLAccountMapper> result = new ArrayList<>();
48+
for (final ProductToGLAccountMapping mapping : mappings) {
49+
final PaymentTypeData paymentTypeData = PaymentTypeData.builder().id(mapping.getPaymentType().getId())
50+
.name(mapping.getPaymentType().getName()).build();
51+
final GLAccountData gLAccountData = new GLAccountData().setId(mapping.getGlAccount().getId())
52+
.setName(mapping.getGlAccount().getName()).setGlCode(mapping.getGlAccount().getGlCode());
53+
result.add(new PaymentTypeToGLAccountMapper().setPaymentType(paymentTypeData).setFundSourceAccount(gLAccountData));
54+
}
55+
return result.isEmpty() ? null : result;
56+
}
57+
58+
public List<ChargeToGLAccountMapper> fetchFeeToIncomeMappings(final Long wcLoanProductId) {
59+
return fetchChargeToIncomeMappings(wcLoanProductId, false);
60+
}
61+
62+
public List<ChargeToGLAccountMapper> fetchPenaltyToIncomeMappings(final Long wcLoanProductId) {
63+
return fetchChargeToIncomeMappings(wcLoanProductId, true);
64+
}
65+
66+
public List<AdvancedMappingToExpenseAccountData> fetchChargeOffReasonMappings(final Long wcLoanProductId) {
67+
return fetchReasonMappings(productToGLAccountMappingRepository.findAllChargeOffReasonsMappings(wcLoanProductId,
68+
PortfolioProductType.WORKING_CAPITAL_LOAN.getValue()));
69+
}
70+
71+
public List<AdvancedMappingToExpenseAccountData> fetchWriteOffReasonMappings(final Long wcLoanProductId) {
72+
return fetchReasonMappings(productToGLAccountMappingRepository.findAllWriteOffReasonsMappings(wcLoanProductId,
73+
PortfolioProductType.WORKING_CAPITAL_LOAN.getValue()));
74+
}
75+
76+
private List<ChargeToGLAccountMapper> fetchChargeToIncomeMappings(final Long wcLoanProductId, final boolean penalty) {
77+
final List<ProductToGLAccountMapping> mappings = penalty
78+
? productToGLAccountMappingRepository.findAllPenaltyMappings(wcLoanProductId,
79+
PortfolioProductType.WORKING_CAPITAL_LOAN.getValue())
80+
: productToGLAccountMappingRepository.findAllFeeMappings(wcLoanProductId,
81+
PortfolioProductType.WORKING_CAPITAL_LOAN.getValue());
82+
final List<ChargeToGLAccountMapper> result = new ArrayList<>();
83+
for (final ProductToGLAccountMapping mapping : mappings) {
84+
final GLAccountData gLAccountData = new GLAccountData().setId(mapping.getGlAccount().getId())
85+
.setName(mapping.getGlAccount().getName()).setGlCode(mapping.getGlAccount().getGlCode());
86+
final ChargeData chargeData = ChargeData.builder().id(mapping.getCharge().getId()).name(mapping.getCharge().getName())
87+
.penalty(mapping.getCharge().isPenalty()).build();
88+
result.add(new ChargeToGLAccountMapper().setCharge(chargeData).setIncomeAccount(gLAccountData));
89+
}
90+
return result.isEmpty() ? null : result;
91+
}
92+
93+
private List<AdvancedMappingToExpenseAccountData> fetchReasonMappings(final List<ProductToGLAccountMapping> mappings) {
94+
final List<AdvancedMappingToExpenseAccountData> result = new ArrayList<>();
95+
for (final ProductToGLAccountMapping mapping : mappings) {
96+
final GLAccountData expenseAccount = new GLAccountData().setId(mapping.getGlAccount().getId())
97+
.setName(mapping.getGlAccount().getName()).setGlCode(mapping.getGlAccount().getGlCode());
98+
final CodeValueData codeValue = mapping.getChargeOffReason() != null ? codeValueMapper.map(mapping.getChargeOffReason())
99+
: codeValueMapper.map(mapping.getWriteOffReason());
100+
result.add(new AdvancedMappingToExpenseAccountData().setReasonCodeValue(codeValue).setExpenseAccount(expenseAccount));
101+
}
102+
return result.isEmpty() ? null : result;
103+
}
104+
}

0 commit comments

Comments
 (0)