diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java index 7399fd76d..ceaca5a84 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java @@ -29,6 +29,7 @@ import org.mifosng.platform.api.data.LoanBasicDetailsData; import org.mifosng.platform.api.data.LoanRepaymentPeriodDatajpw; import org.mifosng.platform.api.data.LoanSchedule; import org.mifosng.platform.api.data.LoanTransactionData; +import org.mifosng.platform.api.data.LoanTransactionDatajpw; import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.api.infrastructure.ApiDataConversionService; import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService; @@ -100,8 +101,9 @@ public class LoansApiResource { LoanAccountSummaryData summary = this.loanReadPlatformService.retrieveSummary(loanBasicDetails.getPrincipal(), repaymentSchedule); - - LoanAccountData loanAccount = new LoanAccountData(loanBasicDetails, summary, repaymentSchedule, null, null ); + Collection loanRepayments = this.loanReadPlatformService.retrieveLoanPayments(loanId); + + LoanAccountData loanAccount = new LoanAccountData(loanBasicDetails, summary, repaymentSchedule, loanRepayments, null ); String selectedFields = ""; String associatedFields = "summary,repaymentSchedule,loanRepayments,permissions,convenienceData"; diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanAccountData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanAccountData.java index f57e51470..e2f0c898c 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanAccountData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanAccountData.java @@ -2,7 +2,6 @@ package org.mifosng.platform.api.data; import java.math.BigDecimal; import java.util.Collection; -import java.util.List; import org.codehaus.jackson.map.annotate.JsonFilter; import org.joda.time.LocalDate; @@ -46,7 +45,7 @@ public class LoanAccountData { private LoanAccountSummaryData summary; private Collection repaymentSchedule; - private List loanRepayments; + private Collection loanRepayments; private LoanPermissionData permissions; @@ -59,7 +58,7 @@ public class LoanAccountData { public LoanAccountData(LoanBasicDetailsData basicDetails, LoanAccountSummaryData summary, Collection repaymentSchedule, - List loanRepayments, + Collection loanRepayments, LoanPermissionData permissions) { this.summary = summary; this.repaymentSchedule = repaymentSchedule; @@ -146,15 +145,17 @@ public class LoanAccountData { return repaymentSchedule; } - public void setRepaymentSchedule(Collection repaymentSchedule) { + public void setRepaymentSchedule( + Collection repaymentSchedule) { this.repaymentSchedule = repaymentSchedule; } - public List getLoanRepayments() { + public Collection getLoanRepayments() { return loanRepayments; } - public void setLoanRepayments(List loanRepayments) { + public void setLoanRepayments( + Collection loanRepayments) { this.loanRepayments = loanRepayments; } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionDatajpw.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionDatajpw.java new file mode 100644 index 000000000..155d5e9a2 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionDatajpw.java @@ -0,0 +1,56 @@ +package org.mifosng.platform.api.data; + +import org.joda.time.LocalDate; + +public class LoanTransactionDatajpw { + + private Long id; + private EnumOptionData transactionType; + private LocalDate date; + private MoneyData total; + + public LoanTransactionDatajpw() { + // + } + + public LoanTransactionDatajpw(Long id, EnumOptionData transactionType, + final LocalDate date, final MoneyData total) { + this.id = id; + this.transactionType = transactionType; + this.date = date; + this.total = total; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public EnumOptionData getTransactionType() { + return transactionType; + } + + public void setTransactionType(EnumOptionData transactionType) { + this.transactionType = transactionType; + } + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } + + public MoneyData getTotal() { + return total; + } + + public void setTotal(MoneyData total) { + this.total = total; + } + +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformService.java index 711096387..1ef45f50b 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformService.java @@ -7,6 +7,7 @@ import org.mifosng.platform.api.data.LoanAccountSummaryData; import org.mifosng.platform.api.data.LoanBasicDetailsData; import org.mifosng.platform.api.data.LoanRepaymentPeriodDatajpw; import org.mifosng.platform.api.data.LoanTransactionData; +import org.mifosng.platform.api.data.LoanTransactionDatajpw; import org.mifosng.platform.api.data.MoneyData; import org.mifosng.platform.api.data.NewLoanData; @@ -19,6 +20,8 @@ public interface LoanReadPlatformService { LoanAccountSummaryData retrieveSummary(MoneyData principal, Collection repaymentSchedule); + Collection retrieveLoanPayments(Long loanId); + NewLoanData retrieveClientAndProductDetails(Long clientId, Long productId); LoanTransactionData retrieveNewLoanRepaymentDetails(Long loanId); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformServiceImpl.java index 5eb268265..c7c092729 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformServiceImpl.java @@ -8,7 +8,6 @@ import java.util.Collection; import java.util.List; import org.joda.time.LocalDate; -import org.mifosng.platform.ReadExtraDataAndReportingServiceImpl; import org.mifosng.platform.api.data.ClientData; import org.mifosng.platform.api.data.CurrencyData; import org.mifosng.platform.api.data.EnumOptionData; @@ -20,6 +19,7 @@ import org.mifosng.platform.api.data.LoanProductLookup; import org.mifosng.platform.api.data.LoanRepaymentPeriodDatajpw; import org.mifosng.platform.api.data.LoanRepaymentScheduleData; import org.mifosng.platform.api.data.LoanTransactionData; +import org.mifosng.platform.api.data.LoanTransactionDatajpw; import org.mifosng.platform.api.data.MoneyData; import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.client.service.ClientReadPlatformService; @@ -32,7 +32,6 @@ import org.mifosng.platform.exceptions.LoanTransactionNotFoundException; import org.mifosng.platform.infrastructure.JdbcSupport; import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource; import org.mifosng.platform.loan.domain.Loan; -import org.mifosng.platform.loan.domain.LoanRepaymentScheduleInstallment; import org.mifosng.platform.loan.domain.LoanRepository; import org.mifosng.platform.loan.domain.LoanTransaction; import org.mifosng.platform.loan.domain.LoanTransactionRepository; @@ -53,7 +52,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { private final static Logger logger = LoggerFactory .getLogger(LoanReadPlatformServiceImpl.class); - + private final JdbcTemplate jdbcTemplate; private final PlatformSecurityContext context; private final LoanRepository loanRepository; @@ -135,21 +134,27 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { BigDecimal totalInArrears = BigDecimal.ZERO; BigDecimal totalWaived = BigDecimal.ZERO; - logger.info("reading"); for (LoanRepaymentPeriodDatajpw installment : repaymentSchedule) { - logger.info("reading: " + installment.getPrincipal().getAmount()); - originalPrincipal.add(installment.getPrincipal().getAmount()); - principalPaid.add(installment.getPrincipalPaid().getAmount()); - principalOutstanding.add(installment.getPrincipalOutstanding().getAmount()); + originalPrincipal = originalPrincipal.add(installment + .getPrincipal().getAmount()); + principalPaid = principalPaid.add(installment.getPrincipalPaid() + .getAmount()); + principalOutstanding = principalOutstanding.add(installment + .getPrincipalOutstanding().getAmount()); + + originalInterest = originalInterest.add(installment.getInterest() + .getAmount()); + interestPaid = interestPaid.add(installment.getInterestPaid() + .getAmount()); + interestOutstanding = interestOutstanding.add(installment + .getInterestOutstanding().getAmount()); + + originalTotal = originalTotal.add(installment.getTotal() + .getAmount()); + totalPaid = totalPaid.add(installment.getTotalPaid().getAmount()); + totalOutstanding = totalOutstanding.add(installment + .getTotalOutstanding().getAmount()); - originalInterest.add(installment.getInterest().getAmount()); - interestPaid.add(installment.getInterestPaid().getAmount()); - interestOutstanding.add(installment.getInterestOutstanding().getAmount()); - - originalTotal.add(installment.getTotal().getAmount()); - totalPaid.add(installment.getTotalPaid().getAmount()); - totalOutstanding.add(installment.getTotalOutstanding().getAmount()); - } totalInArrears = BigDecimal.TEN; @@ -167,6 +172,25 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { currencyData, totalWaived)); } + @Override + public Collection retrieveLoanPayments(Long loanId) { + try { + context.authenticatedUser(); + + LoanPaymentsMapper rm = new LoanPaymentsMapper(); + + // retrieve all loan transactions that are not invalid (0) and not + // disbursements (1) + String sql = "select " + + rm.LoanPaymentsSchema() + + " where tr.loan_id = ? and tr.transaction_type_enum not in (0, 1) order by tr.transaction_date"; + return this.jdbcTemplate.query(sql, rm, new Object[] { loanId }); + + } catch (EmptyResultDataAccessException e) { + return null; + } + } + public LoanAccountData convertToData(LoanBasicDetailsData loanBasic) { MoneyData princ = loanBasic.getPrincipal(); @@ -608,4 +632,44 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { } } + private static final class LoanPaymentsMapper implements + RowMapper { + + public String LoanPaymentsSchema() { + + return " tr.id as id, tr.transaction_type_enum as transactionType, tr.transaction_date as `date`, tr.amount as total, " + + " l.currency_code as currencyCode, l.currency_digits as currencyDigits, rc.`name` as currencyName, rc.display_symbol as currencyDisplaySymbol, rc.internationalized_name_code as currencyNameCode " + + " from portfolio_loan l " + + " join portfolio_loan_transaction tr on tr.loan_id = l.id" + + " join ref_currency rc on rc.`code` = l.currency_code "; + } + + @Override + public LoanTransactionDatajpw mapRow(final ResultSet rs, + final int rowNum) throws SQLException { + + String currencyCode = rs.getString("currencyCode"); + String currencyName = rs.getString("currencyName"); + String currencyNameCode = rs.getString("currencyNameCode"); + String currencyDisplaySymbol = rs + .getString("currencyDisplaySymbol"); + Integer currencyDigits = JdbcSupport.getInteger(rs, + "currencyDigits"); + CurrencyData currencyData = new CurrencyData(currencyCode, + currencyName, currencyDigits, currencyDisplaySymbol, + currencyNameCode); + + Long id = rs.getLong("id"); + int transactionTypeInt = JdbcSupport.getInteger(rs, + "transactionType"); + EnumOptionData transactionType = LoanEnumerations + .transactionType(transactionTypeInt); + LocalDate date = JdbcSupport.getLocalDate(rs, "date"); + BigDecimal totalBD = rs.getBigDecimal("total"); + MoneyData total = MoneyData.of(currencyData, totalBD); + + return new LoanTransactionDatajpw(id, transactionType, date, total); + } + } + } \ No newline at end of file