Cocoa-Puffs commented on code in PR #5986: URL: https://github.com/apache/fineract/pull/5986#discussion_r3413302635
########## fineract-provider/src/main/java/org/apache/fineract/infrastructure/jobs/service/retainedearning/services/RetainedEarningDataServiceImpl.java: ########## @@ -0,0 +1,225 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.jobs.service.retainedearning.services; + +import static org.apache.fineract.infrastructure.jobs.service.retainedearning.RetainedEarningJobConstant.DEFAULT_OFFICE_ID; +import static org.apache.fineract.infrastructure.jobs.service.retainedearning.RetainedEarningJobConstant.END_DATE_QUERY_PARAM; +import static org.apache.fineract.infrastructure.jobs.service.retainedearning.RetainedEarningJobConstant.OFFICE_ID_QUERY_PARAM; + +import com.google.common.base.Splitter; +import jakarta.ws.rs.core.MultivaluedMap; +import jakarta.ws.rs.core.Response; +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.fineract.accounting.retainedearning.domain.AccountGLJournalEntryAnnualSummary; +import org.apache.fineract.accounting.retainedearning.domain.AccountGLJournalEntryAnnualSummaryRepository; +import org.apache.fineract.infrastructure.dataqueries.service.DatatableExportTargetParameter; +import org.apache.fineract.infrastructure.jobs.service.retainedearning.RetainedEarningConfigurationService; +import org.apache.fineract.infrastructure.jobs.service.retainedearning.data.AccountGLJournalEntryAnnualSummaryData; +import org.apache.fineract.infrastructure.jobs.service.retainedearning.helper.DataParser; +import org.apache.fineract.infrastructure.report.service.ReportingProcessService; +import org.apache.fineract.portfolio.loanproduct.domain.LoanProduct; +import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRepository; +import org.glassfish.jersey.internal.util.collection.MultivaluedStringMap; +import org.springframework.stereotype.Component; + +/** + * Retained earning data service implementation. Handles data fetching, processing, and persistence. + */ +@Component +@AllArgsConstructor +@Slf4j +public class RetainedEarningDataServiceImpl implements RetainedEarningDataService { + + private final ReportingProcessService reportingProcessService; + + private final DataParser dataParser; + + private final AccountGLJournalEntryAnnualSummaryRepository retainedEarningSummaryRepository; + + private final LoanProductRepository loanProductRepository; + + private final RetainedEarningConfigurationService retainedEarningConfigurationService; + + @Override + public void insertRetainedEarningSummaryBatch(final List<AccountGLJournalEntryAnnualSummaryData> retainedEarningSummaries) { + if (retainedEarningSummaries == null || retainedEarningSummaries.isEmpty()) { + log.warn("No retained earning summaries provided for insertion, skipping batch save."); + return; + } + List<AccountGLJournalEntryAnnualSummary> entities = retainedEarningSummaries.stream().map(this::convertToRetainedEarningSummary) + .toList(); + retainedEarningSummaryRepository.saveAll(entities); + } + + private AccountGLJournalEntryAnnualSummary convertToRetainedEarningSummary(final AccountGLJournalEntryAnnualSummaryData summaryDTO) { + AccountGLJournalEntryAnnualSummary entrySummary = new AccountGLJournalEntryAnnualSummary(); + entrySummary.setProductId(summaryDTO.getProductId()); + entrySummary.setGlCode(String.valueOf(summaryDTO.getGlAccountCode())); + entrySummary.setOfficeId(summaryDTO.getOfficeId()); + entrySummary.setOwnerExternalId(summaryDTO.getOwnerExternalId()); + entrySummary.setOpeningBalanceAmount(summaryDTO.getOpeningBalanceAmount()); + entrySummary.setYearEndDate(summaryDTO.getYearEndDate()); + entrySummary.setCurrencyCode(summaryDTO.getCurrencyCode()); + return entrySummary; + } + + @Override + public List<AccountGLJournalEntryAnnualSummaryData> fetchTrialBalanceData(String reportName, LocalDate fiscalYearEnd) { + MultivaluedMap<String, String> queryParams = buildQueryParams(fiscalYearEnd); + Response response = reportingProcessService.processRequest(reportName, queryParams); + if (response.getStatus() != Response.Status.OK.getStatusCode()) { + throw new IllegalStateException("Trial balance report returned HTTP " + response.getStatus() + " for report: " + reportName); + } + String jsonResponse = (String) response.getEntity(); + return parseJsonResponse(jsonResponse); + } + + private MultivaluedMap<String, String> buildQueryParams(final LocalDate lastDayOfPreviousFiscalYear) { + final MultivaluedMap<String, String> queryParams = new MultivaluedStringMap(); + queryParams.add(DatatableExportTargetParameter.PRETTY_JSON.getValue(), BooleanUtils.TRUE); + queryParams.add(END_DATE_QUERY_PARAM, lastDayOfPreviousFiscalYear.toString()); + queryParams.add(OFFICE_ID_QUERY_PARAM, String.valueOf(retainedEarningConfigurationService.getOfficeId())); + return queryParams; + } + + @Override + public List<AccountGLJournalEntryAnnualSummaryData> processTrialBalanceData(List<AccountGLJournalEntryAnnualSummaryData> rawData, + LocalDate lastDayOfPreviousFiscalYear) { + + if (rawData == null || rawData.isEmpty()) { + log.warn("No data to process"); + return Collections.emptyList(); + } + + final String incomeAndExpenseGlAccounts = retainedEarningConfigurationService.getIncomeExpenseGlAccounts(); + final Predicate<String> glAccountMatcher = buildGlAccountMatcher(incomeAndExpenseGlAccounts); + + final List<AccountGLJournalEntryAnnualSummaryData> incomeExpenseRecords = rawData.stream() + .filter(r -> r != null && r.getGlAccountCode() != null && r.getOwnerExternalId() != null) + .filter(r -> glAccountMatcher.test(r.getGlAccountCode())).collect(Collectors.toList()); + + final Set<String> distinctGlCodes = incomeExpenseRecords.stream().map(r -> String.valueOf(r.getGlAccountCode())) + .collect(Collectors.toSet()); + final Set<String> distinctOwners = incomeExpenseRecords.stream().map(AccountGLJournalEntryAnnualSummaryData::getOwnerExternalId) + .collect(Collectors.toSet()); + + log.info( + "Retained earning validation: totalTrialBalanceRecords={}, matchedIncomeExpenseRecords={}, distinctGlAccounts={}, distinctAssetOwners={}, fiscalYearEnd={}", + rawData.size(), incomeExpenseRecords.size(), distinctGlCodes.size(), distinctOwners.size(), lastDayOfPreviousFiscalYear); + + if (incomeExpenseRecords.isEmpty()) { + log.info("No income/expense account records found hence skipping retained earning creation"); + return Collections.emptyList(); + } + + final Map<String, BigDecimal> retainedByOwner = incomeExpenseRecords.stream() + .collect(Collectors.toMap(AccountGLJournalEntryAnnualSummaryData::getOwnerExternalId, + r -> Optional.ofNullable(r.getEndingBalanceAmount()).orElse(BigDecimal.ZERO), BigDecimal::add)); + + final List<AccountGLJournalEntryAnnualSummaryData> retainedEarningRecords = createRetainedEarningRecords(retainedByOwner, + incomeExpenseRecords, lastDayOfPreviousFiscalYear); + + final LoanProduct loanProduct = getLoanProductFromRecords(incomeExpenseRecords); + + final List<AccountGLJournalEntryAnnualSummaryData> allRecords = Stream + .concat(incomeExpenseRecords.stream(), retainedEarningRecords.stream()) + .map(data -> data.toBuilder().productId(loanProduct.getId()).currencyCode(loanProduct.getCurrency().getCode()).build()) + .collect(Collectors.toList()); + + log.info( + "Retained earning processing complete: incomeExpenseOffsetRecords={}, retainedEarningRecords={}, totalRecordsToWrite={}, assetOwners={}", + incomeExpenseRecords.size(), retainedEarningRecords.size(), allRecords.size(), distinctOwners); + + return allRecords; + } + + private Predicate<String> buildGlAccountMatcher(String incomeAndExpenseGlAccounts) { + if (incomeAndExpenseGlAccounts == null || incomeAndExpenseGlAccounts.isBlank()) { + return code -> false; + } + List<Predicate<String>> predicates = new ArrayList<>(); + if (!incomeAndExpenseGlAccounts.contains(",")) { + return incomeAndExpenseGlAccounts::equals; + } + for (String token : Splitter.on(',').split(incomeAndExpenseGlAccounts)) { + String trimmed = token.trim(); + if (trimmed.matches("\\d+-\\d+")) { + String[] bounds = trimmed.split("-", 2); + String from = bounds[0].trim(); + String to = bounds[1].trim(); + predicates.add(code -> code.compareTo(from) >= 0 && code.compareTo(to) <= 0); Review Comment: fixed by comparing values as integers -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
