diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/data/AccountSummaryCollectionData.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/data/AccountSummaryCollectionData.java index f4faacaa32a..80de9a5f1b2 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/data/AccountSummaryCollectionData.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/data/AccountSummaryCollectionData.java @@ -29,27 +29,34 @@ public class AccountSummaryCollectionData { private final Collection loanAccounts; private final Collection savingsAccounts; private final Collection shareAccounts ; + private final Collection guarantorAccounts; private final Collection memberLoanAccounts; private final Collection memberSavingsAccounts; - + private final Collection memberGuarantorAccounts; + public AccountSummaryCollectionData(final Collection loanAccounts, - final Collection savingsAccounts, final Collection shareAccounts) { + final Collection savingsAccounts, final Collection shareAccounts, + final Collection guarantorAccounts) { this.loanAccounts = defaultLoanAccountsIfEmpty(loanAccounts); this.savingsAccounts = defaultSavingsAccountsIfEmpty(savingsAccounts); this.shareAccounts = defaultShareAccountsIfEmpty(shareAccounts) ; + this.guarantorAccounts = guarantorAccounts; this.memberLoanAccounts = null; this.memberSavingsAccounts = null; + this.memberGuarantorAccounts = null; } public AccountSummaryCollectionData(final Collection loanAccounts, - final Collection savingsAccounts, final Collection memberLoanAccounts, - final Collection memberSavingsAccounts) { + final Collection savingsAccounts, final Collection guarantorAccounts, final Collection memberLoanAccounts, + final Collection memberSavingsAccounts, final Collection memberGuarantorAccounts) { this.loanAccounts = defaultLoanAccountsIfEmpty(loanAccounts); this.savingsAccounts = defaultSavingsAccountsIfEmpty(savingsAccounts); + this.guarantorAccounts = guarantorAccounts; this.shareAccounts = null ; this.memberLoanAccounts = defaultLoanAccountsIfEmpty(memberLoanAccounts); this.memberSavingsAccounts = defaultSavingsAccountsIfEmpty(memberSavingsAccounts); + this.memberGuarantorAccounts = defaultGuarantorAccountsIfEmpty(memberGuarantorAccounts); } private Collection defaultLoanAccountsIfEmpty(final Collection collection) { @@ -76,4 +83,11 @@ private Collection defaultShareAccountsIfEmpty(final Co return returnCollection; } + private Collection defaultGuarantorAccountsIfEmpty(final Collection collection) { + Collection returnCollection = null; + if (collection != null && !collection.isEmpty()) { + returnCollection = collection; + } + return returnCollection; + } } \ No newline at end of file diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/data/GuarantorAccountSummaryData.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/data/GuarantorAccountSummaryData.java new file mode 100644 index 00000000000..64f444e82d9 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/data/GuarantorAccountSummaryData.java @@ -0,0 +1,70 @@ +/** + * 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.portfolio.accountdetails.data; + +import java.math.BigDecimal; + +import org.apache.fineract.infrastructure.core.data.EnumOptionData; +import org.apache.fineract.portfolio.loanaccount.data.LoanApplicationTimelineData; +import org.apache.fineract.portfolio.loanaccount.data.LoanStatusEnumData; + +/** + * Immutable data object for loan accounts. + */ +@SuppressWarnings("unused") +public class GuarantorAccountSummaryData { + + private final Long id; + private final String accountNo; + private final String externalId; + private final Long productId; + private final String productName; + private final String shortProductName; + private final LoanStatusEnumData status; + private final EnumOptionData loanType; + private final Integer loanCycle; + private final Boolean inArrears; + private final BigDecimal originalLoan; + private final BigDecimal loanBalance; + private final BigDecimal amountPaid; + private final Boolean isActive; + private final String relationship; + private final BigDecimal onHoldAmount; + public GuarantorAccountSummaryData(final Long id, final String accountNo, final String externalId, final Long productId, + final String loanProductName, final String shortLoanProductName, final LoanStatusEnumData loanStatus, final EnumOptionData loanType, final Integer loanCycle, + final Boolean inArrears,final BigDecimal originalLoan,final BigDecimal loanBalance,final BigDecimal amountPaid, + final Boolean isActive, final String relationship, final BigDecimal onHoldAmount) { + this.id = id; + this.accountNo = accountNo; + this.externalId = externalId; + this.productId = productId; + this.productName = loanProductName; + this.shortProductName = shortLoanProductName; + this.status = loanStatus; + this.loanType = loanType; + this.loanCycle = loanCycle; + this.inArrears = inArrears; + this.loanBalance = loanBalance; + this.originalLoan = originalLoan; + this.amountPaid = amountPaid; + this.isActive = isActive; + this.relationship = relationship; + this.onHoldAmount = onHoldAmount; + } +} \ No newline at end of file diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/service/AccountDetailsReadPlatformServiceJpaRepositoryImpl.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/service/AccountDetailsReadPlatformServiceJpaRepositoryImpl.java index 0fc91036b5c..b4b1bb7e585 100755 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/service/AccountDetailsReadPlatformServiceJpaRepositoryImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/accountdetails/service/AccountDetailsReadPlatformServiceJpaRepositoryImpl.java @@ -30,6 +30,7 @@ import org.apache.fineract.infrastructure.security.utils.ColumnValidator; import org.apache.fineract.organisation.monetary.data.CurrencyData; import org.apache.fineract.portfolio.accountdetails.data.AccountSummaryCollectionData; +import org.apache.fineract.portfolio.accountdetails.data.GuarantorAccountSummaryData; import org.apache.fineract.portfolio.accountdetails.data.LoanAccountSummaryData; import org.apache.fineract.portfolio.accountdetails.data.SavingsAccountSummaryData; import org.apache.fineract.portfolio.accountdetails.data.ShareAccountSummaryData; @@ -75,10 +76,14 @@ public AccountSummaryCollectionData retrieveClientAccountDetails(final Long clie this.clientReadPlatformService.retrieveOne(clientId); final String loanwhereClause = " where l.client_id = ?"; final String savingswhereClause = " where sa.client_id = ? order by sa.status_enum ASC, sa.account_no ASC"; + final String guarantorWhereClause = " where g.entity_id = ? and g.is_active = 1 order by l.account_no ASC"; + final List loanAccounts = retrieveLoanAccountDetails(loanwhereClause, new Object[] { clientId }); final List savingsAccounts = retrieveAccountDetails(savingswhereClause, new Object[] { clientId }); final List shareAccounts = retrieveShareAccountDetails(clientId) ; - return new AccountSummaryCollectionData(loanAccounts, savingsAccounts, shareAccounts); + final List guarantorloanAccounts = retrieveGuarantorLoanAccountDetails( + guarantorWhereClause, new Object[] { clientId }); + return new AccountSummaryCollectionData(loanAccounts, savingsAccounts, shareAccounts, guarantorloanAccounts); } @Override @@ -89,14 +94,21 @@ public AccountSummaryCollectionData retrieveGroupAccountDetails(final Long group final String loanWhereClauseForMembers = " where l.group_id = ? and l.client_id is not null"; final String savingswhereClauseForGroup = " where sa.group_id = ? and sa.client_id is null order by sa.status_enum ASC, sa.account_no ASC"; final String savingswhereClauseForMembers = " where sa.group_id = ? and sa.client_id is not null order by sa.status_enum ASC, sa.account_no ASC"; + final String guarantorWhereClauseForGroup = " where l.group_id = ? and l.client_id is null and g.is_active = 1 order by l.account_no ASC"; + final String guarantorWhereClauseForMembers = " where l.group_id = ? and l.client_id is not null and g.is_active = 1 order by l.account_no ASC"; + final List groupLoanAccounts = retrieveLoanAccountDetails(loanWhereClauseForGroup, new Object[] { groupId }); final List groupSavingsAccounts = retrieveAccountDetails(savingswhereClauseForGroup, new Object[] { groupId }); + final List groupGuarantorloanAccounts = retrieveGuarantorLoanAccountDetails( + guarantorWhereClauseForGroup, new Object[] { groupId }); final List memberLoanAccounts = retrieveLoanAccountDetails(loanWhereClauseForMembers, new Object[] { groupId }); final List memberSavingsAccounts = retrieveAccountDetails(savingswhereClauseForMembers, new Object[] { groupId }); - return new AccountSummaryCollectionData(groupLoanAccounts, groupSavingsAccounts, memberLoanAccounts, memberSavingsAccounts); + final List memberGuarantorloanAccounts = retrieveGuarantorLoanAccountDetails( + guarantorWhereClauseForMembers, new Object[] { groupId }); + return new AccountSummaryCollectionData(groupLoanAccounts, groupSavingsAccounts, groupGuarantorloanAccounts, memberLoanAccounts, memberSavingsAccounts, memberGuarantorloanAccounts); } @Override @@ -144,6 +156,14 @@ private List retrieveShareAccountDetails(final Long cli return this.jdbcTemplate.query(query, mapper, new Object [] {clientId}); } + private List retrieveGuarantorLoanAccountDetails( + final String loanwhereClause, final Object[] inputs) { + final GuarantorLoanAccountSummaryDataMapper rm = new GuarantorLoanAccountSummaryDataMapper(); + final String sql = "select " + rm.guarantorLoanAccountSummarySchema() + + loanwhereClause; + return this.jdbcTemplate.query(sql, rm, inputs); + } + private final static class ShareAccountSummaryDataMapper implements RowMapper { private final String schema ; @@ -493,6 +513,97 @@ public LoanAccountSummaryData mapRow(final ResultSet rs, @SuppressWarnings("unus return new LoanAccountSummaryData(id, accountNo, externalId, productId, loanProductName, shortLoanProductName, loanStatus, loanType, loanCycle, timeline, inArrears,originalLoan,loanBalance,amountPaid); } + + } + private static final class GuarantorLoanAccountSummaryDataMapper implements + RowMapper { + + public String guarantorLoanAccountSummarySchema() { + + final StringBuilder accountsSummary = new StringBuilder( + "l.id as id, l.account_no as accountNo, l.external_id as externalId,"); + accountsSummary + .append(" l.product_id as productId, lp.name as productName, lp.short_name as shortProductName,") + .append(" l.loan_status_id as statusId, l.loan_type_enum as loanType,") + + .append("l.principal_disbursed_derived as originalLoan,") + .append("l.total_outstanding_derived as loanBalance,") + .append("l.total_repayment_derived as amountPaid,") + + .append(" l.loan_product_counter as loanCycle,") + + .append(" l.submittedon_date as submittedOnDate,") + + .append(" l.rejectedon_date as rejectedOnDate,") + .append(" l.withdrawnon_date as withdrawnOnDate,") + .append(" l.approvedon_date as approvedOnDate,") + .append(" l.expected_disbursedon_date as expectedDisbursementDate, l.disbursedon_date as actualDisbursementDate,") + .append(" l.closedon_date as closedOnDate,") + .append(" la.overdue_since_date_derived as overdueSinceDate,") + .append(" l.writtenoffon_date as writtenOffOnDate, l.expected_maturedon_date as expectedMaturityDate,") + .append(" g.is_active as isActive,") + .append(" cv.code_value as relationship,") + .append(" sa.on_hold_funds_derived") + .append(" from m_loan l ") + .append(" join m_guarantor as g on g.loan_id = l.id ") + .append(" join m_client as c on c.id = g.entity_id ") + .append(" LEFT JOIN m_product_loan AS lp ON lp.id = l.product_id") + .append(" left join m_loan_arrears_aging la on la.loan_id = l.id") + .append(" left join m_code_value cv ON cv.id = g.client_reln_cv_id") + .append(" left join m_savings_account sa on sa.client_id = c.id") + + ; + + return accountsSummary.toString(); + } + + @Override + public GuarantorAccountSummaryData mapRow(final ResultSet rs, + @SuppressWarnings("unused") final int rowNum) + throws SQLException { + + final Long id = JdbcSupport.getLong(rs, "id"); + final String accountNo = rs.getString("accountNo"); + final String externalId = rs.getString("externalId"); + final Long productId = JdbcSupport.getLong(rs, "productId"); + final String loanProductName = rs.getString("productName"); + final String shortLoanProductName = rs + .getString("shortProductName"); + final Integer loanStatusId = JdbcSupport.getInteger(rs, "statusId"); + final LoanStatusEnumData loanStatus = LoanEnumerations + .status(loanStatusId); + final Integer loanTypeId = JdbcSupport.getInteger(rs, "loanType"); + final EnumOptionData loanType = AccountEnumerations + .loanType(loanTypeId); + final Integer loanCycle = JdbcSupport.getInteger(rs, "loanCycle"); + + final BigDecimal originalLoan = JdbcSupport + .getBigDecimalDefaultToNullIfZero(rs, "originalLoan"); + final BigDecimal loanBalance = JdbcSupport + .getBigDecimalDefaultToNullIfZero(rs, "loanBalance"); + final BigDecimal amountPaid = JdbcSupport + .getBigDecimalDefaultToNullIfZero(rs, "amountPaid"); + final BigDecimal onHoldAmount = JdbcSupport + .getBigDecimalDefaultToNullIfZero(rs, + "on_hold_funds_derived"); + + final LocalDate overdueSinceDate = JdbcSupport.getLocalDate(rs, + "overdueSinceDate"); + Boolean inArrears = true; + if (overdueSinceDate == null) { + inArrears = false; + } + + final Boolean isActive = rs.getBoolean("isActive"); + + final String relationship = rs.getString("relationship"); + return new GuarantorAccountSummaryData(id, accountNo, externalId, + productId, loanProductName, shortLoanProductName, + loanStatus, loanType, loanCycle, inArrears, originalLoan, + loanBalance, amountPaid, isActive, relationship, + onHoldAmount); + } + } -} +} \ No newline at end of file