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