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 cb1c317a8..7399fd76d 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 @@ -1,5 +1,7 @@ package org.mifosng.platform.api; +import java.util.Collection; + import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -22,7 +24,9 @@ import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand; import org.mifosng.platform.api.commands.UndoStateTransitionCommand; import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.api.data.LoanAccountData; +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.LoanSchedule; import org.mifosng.platform.api.data.LoanTransactionData; import org.mifosng.platform.api.data.NewLoanData; @@ -90,9 +94,14 @@ public class LoansApiResource { LoanBasicDetailsData loanBasicDetails = this.loanReadPlatformService .retrieveLoanAccountDetails(loanId); - LoanAccountData loanAccount = this.loanReadPlatformService.convertToData(loanBasicDetails); + //LoanAccountData loanAccount = this.loanReadPlatformService.convertToData(loanBasicDetails); - //LoanAccountData loanAccount = new LoanAccountData(loanBasicDetails, null, null, null, null ); + Collection repaymentSchedule = this.loanReadPlatformService.retrieveRepaymentSchedule(loanId); + + LoanAccountSummaryData summary = this.loanReadPlatformService.retrieveSummary(loanBasicDetails.getPrincipal(), repaymentSchedule); + + + LoanAccountData loanAccount = new LoanAccountData(loanBasicDetails, summary, repaymentSchedule, null, 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 d1b0be035..f57e51470 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 @@ -1,6 +1,7 @@ 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; @@ -44,7 +45,7 @@ public class LoanAccountData { private LocalDate lifeCycleStatusDate; private LoanAccountSummaryData summary; - private LoanRepaymentScheduleData repaymentSchedule; + private Collection repaymentSchedule; private List loanRepayments; private LoanPermissionData permissions; @@ -57,7 +58,7 @@ public class LoanAccountData { public LoanAccountData(LoanBasicDetailsData basicDetails, LoanAccountSummaryData summary, - LoanRepaymentScheduleData repaymentSchedule, + Collection repaymentSchedule, List loanRepayments, LoanPermissionData permissions) { this.summary = summary; @@ -141,11 +142,11 @@ public class LoanAccountData { this.summary = summary; } - public LoanRepaymentScheduleData getRepaymentSchedule() { + public Collection getRepaymentSchedule() { return repaymentSchedule; } - public void setRepaymentSchedule(LoanRepaymentScheduleData repaymentSchedule) { + public void setRepaymentSchedule(Collection repaymentSchedule) { this.repaymentSchedule = repaymentSchedule; } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanRepaymentPeriodDatajpw.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanRepaymentPeriodDatajpw.java new file mode 100644 index 000000000..06cba188c --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanRepaymentPeriodDatajpw.java @@ -0,0 +1,139 @@ +package org.mifosng.platform.api.data; + +import org.joda.time.LocalDate; + +public class LoanRepaymentPeriodDatajpw { + + private Long loanId; + private Integer period; + private LocalDate date; + private MoneyData principal; + private MoneyData principalPaid; + private MoneyData principalOutstanding; + private MoneyData interest; + private MoneyData interestPaid; + private MoneyData interestOutstanding; + private MoneyData total; + private MoneyData totalPaid; + private MoneyData totalOutstanding; + + public LoanRepaymentPeriodDatajpw() { + // + } + + public LoanRepaymentPeriodDatajpw(Long loanId, Integer period, + LocalDate date, MoneyData principal, MoneyData principalPaid, + MoneyData principalOutstanding, MoneyData interest, + MoneyData interestPaid, MoneyData interestOutstanding, + MoneyData total, MoneyData totalPaid, MoneyData totalOutstanding) { + this.loanId = loanId; + this.period = period; + this.date = date; + this.principal = principal; + this.principalPaid = principalPaid; + this.principalOutstanding = principalOutstanding; + this.interest = interest; + this.interestPaid = interestPaid; + this.interestOutstanding = interestOutstanding; + this.total = total; + this.totalPaid = totalPaid; + this.totalOutstanding = totalOutstanding; + } + + public Long getLoanId() { + return loanId; + } + + public void setLoanId(Long loanId) { + this.loanId = loanId; + } + + public Integer getPeriod() { + return period; + } + + public void setPeriod(Integer period) { + this.period = period; + } + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } + + public MoneyData getPrincipal() { + return principal; + } + + public void setPrincipal(MoneyData principal) { + this.principal = principal; + } + + public MoneyData getPrincipalPaid() { + return principalPaid; + } + + public void setPrincipalPaid(MoneyData principalPaid) { + this.principalPaid = principalPaid; + } + + public MoneyData getPrincipalOutstanding() { + return principalOutstanding; + } + + public void setPrincipalOutstanding(MoneyData principalOutstanding) { + this.principalOutstanding = principalOutstanding; + } + + public MoneyData getInterest() { + return interest; + } + + public void setInterest(MoneyData interest) { + this.interest = interest; + } + + public MoneyData getInterestPaid() { + return interestPaid; + } + + public void setInterestPaid(MoneyData interestPaid) { + this.interestPaid = interestPaid; + } + + public MoneyData getInterestOutstanding() { + return interestOutstanding; + } + + public void setInterestOutstanding(MoneyData interestOutstanding) { + this.interestOutstanding = interestOutstanding; + } + + public MoneyData getTotal() { + return total; + } + + public void setTotal(MoneyData total) { + this.total = total; + } + + public MoneyData getTotalPaid() { + return totalPaid; + } + + public void setTotalPaid(MoneyData totalPaid) { + this.totalPaid = totalPaid; + } + + public MoneyData getTotalOutstanding() { + return totalOutstanding; + } + + public void setTotalOutstanding(MoneyData totalOutstanding) { + this.totalOutstanding = totalOutstanding; + } + +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/DerivedLoanDataProcessor.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/DerivedLoanDataProcessor.java index dad474d9a..12a5cd003 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/DerivedLoanDataProcessor.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/DerivedLoanDataProcessor.java @@ -1,5 +1,6 @@ package org.mifosng.platform.loan.domain; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -23,7 +24,7 @@ import org.mifosng.platform.loanproduct.service.LoanEnumerations; public class DerivedLoanDataProcessor { public DerivedLoanData process(List repaymentSchedulePeriods, List loanTransactions, - CurrencyData currencyDetail, Money arrearsTolerance) { + CurrencyData currencyDetail, BigDecimal arrearsTolerance) { // 1. derive repayment schedule LoanRepaymentScheduleData repaymentScheduleDetails = generateRepaymentScheduleData(repaymentSchedulePeriods, currencyDetail); @@ -136,13 +137,13 @@ public class DerivedLoanDataProcessor { private List calculateArrears( List loanRepaymentPeriods, - Money arrearsTolerance, CurrencyData currencyData) { + BigDecimal arrearsTolerance, CurrencyData currencyData) { LocalDate today = new LocalDate(); int repaymentIndex = 0; for (LoanRepaymentPeriodData currentRepaymentSchedule : loanRepaymentPeriods) { if (currentRepaymentSchedule.isInArrearsWithToleranceOf( - moneyDataFrom(currencyData, arrearsTolerance), today)) { + MoneyData.of(currencyData, arrearsTolerance), today)) { currentRepaymentSchedule .setTotalArrears(currentRepaymentSchedule .getTotalOutstanding()); @@ -277,7 +278,7 @@ public class DerivedLoanDataProcessor { LoanRepaymentPeriodData scheduledRepayment, int repaymentScheduleIndex, List repaymentSchedulePeriods, - Money arrearsTolerance, CurrencyData currencyDetail) { + BigDecimal arrearsTolerance, CurrencyData currencyDetail) { MoneyData zero = MoneyData.zero(currencyDetail); MoneyData cumulativeInterestPaid = MoneyData.zero(currencyDetail); @@ -422,7 +423,7 @@ public class DerivedLoanDataProcessor { } // check if this installment has - if (currentRepaymentSchedule.isInArrearsWithToleranceOf(moneyDataFrom(currencyDetail, arrearsTolerance), repayment.getTransactionDate())) { + if (currentRepaymentSchedule.isInArrearsWithToleranceOf(MoneyData.of(currencyDetail, arrearsTolerance), repayment.getTransactionDate())) { currentRepaymentSchedule.setTotalArrears(currentRepaymentSchedule.getTotalOutstanding()); } else { currentRepaymentSchedule.setTotalArrears(MoneyData.zero(currencyDetail)); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/Loan.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/Loan.java index 4ab1fd2db..b0e631b20 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/Loan.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/Loan.java @@ -653,8 +653,9 @@ public class Loan extends AbstractAuditableCustom { public List getRepaymentScheduleInstallments() { return this.repaymentScheduleInstallments; } - - public DerivedLoanData deriveLoanData(CurrencyData currencyData) { +//delete after jpw + /* + public DerivedLoanData deriveLoanDataxxx(CurrencyData currencyData) { List repaymentTransactions = new ArrayList(); for (LoanTransaction loanTransaction : this.loanTransactions) { @@ -671,6 +672,7 @@ public class Loan extends AbstractAuditableCustom { this.repaymentScheduleInstallments), repaymentTransactions, currencyData, arrearsTolerance); } + */ public String getExternalId() { return this.externalId; @@ -964,35 +966,5 @@ public class Loan extends AbstractAuditableCustom { public String getCurrencyCode() { return this.loanRepaymentScheduleDetail.getPrincipal().getCurrencyCode(); } - -// jpw will be deleting this shortly - public LoanAccountData toLoanAccountData(LoanBasicDetailsData basicDetails, LoanAccountSummaryData summary, LoanRepaymentScheduleData repaymentSchedule, - List loanRepayments) { - - //LoanBasicDetailsData basicDetails = toBasicDetailsData(currencyData, fundData); - - // permissions - boolean waiveAllowed = summary.isWaiveAllowed(basicDetails.getInArrearsTolerance()) - && isNotClosed(); - boolean undoDisbursalAllowed = isDisbursed() - && isOpenWithNoRepaymentMade(); - boolean makeRepaymentAllowed = isDisbursed() - && isNotClosed(); - - boolean rejectAllowed = isNotApproved() - && isNotDisbursed() && isNotClosed(); - boolean withdrawnByApplicantAllowed = isNotDisbursed() - && isNotClosed(); - boolean undoApprovalAllowed = isApproved() - && isNotClosed(); - boolean disbursalAllowed = isApproved() - && isNotDisbursed() && isNotClosed(); - - LoanPermissionData permissions = new LoanPermissionData(waiveAllowed, makeRepaymentAllowed, rejectAllowed, withdrawnByApplicantAllowed, - undoApprovalAllowed, undoDisbursalAllowed, disbursalAllowed, isSubmittedAndPendingApproval(), - isWaitingForDisbursal()); - - return new LoanAccountData(basicDetails, summary, repaymentSchedule, loanRepayments, permissions); - } } \ 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 aaffaadec..711096387 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 @@ -1,14 +1,24 @@ package org.mifosng.platform.loan.service; +import java.util.Collection; + import org.mifosng.platform.api.data.LoanAccountData; +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.MoneyData; import org.mifosng.platform.api.data.NewLoanData; public interface LoanReadPlatformService { LoanBasicDetailsData retrieveLoanAccountDetails(Long loanId); + Collection retrieveRepaymentSchedule(Long loanId); + + LoanAccountSummaryData retrieveSummary(MoneyData principal, + Collection repaymentSchedule); + NewLoanData retrieveClientAndProductDetails(Long clientId, Long productId); LoanTransactionData retrieveNewLoanRepaymentDetails(Long loanId); @@ -17,6 +27,7 @@ public interface LoanReadPlatformService { LoanTransactionData retrieveLoanTransactionDetails(Long loanId, Long transactionId); - - LoanAccountData convertToData(LoanBasicDetailsData loanBasic);//del jpw after + + LoanAccountData convertToData(LoanBasicDetailsData loanBasic);// del jpw + // after } \ No newline at end of file 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 713afed04..5eb268265 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 @@ -5,16 +5,20 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; 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.DerivedLoanData; import org.mifosng.platform.api.data.EnumOptionData; import org.mifosng.platform.api.data.LoanAccountData; +import org.mifosng.platform.api.data.LoanAccountSummaryData; import org.mifosng.platform.api.data.LoanBasicDetailsData; import org.mifosng.platform.api.data.LoanProductData; 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.MoneyData; import org.mifosng.platform.api.data.NewLoanData; @@ -28,6 +32,7 @@ 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; @@ -35,6 +40,8 @@ import org.mifosng.platform.loan.domain.LoanTransactionType; import org.mifosng.platform.loanproduct.service.LoanEnumerations; import org.mifosng.platform.loanproduct.service.LoanProductReadPlatformService; import org.mifosng.platform.security.PlatformSecurityContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -44,6 +51,9 @@ import org.springframework.stereotype.Service; @Service 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; @@ -73,39 +83,167 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { @Override public LoanBasicDetailsData retrieveLoanAccountDetails(final Long loanId) { - LoanBasicDetailsData selectedLoan = null; try { context.authenticatedUser(); LoanMapper rm = new LoanMapper(); String sql = "select " + rm.loanSchema() + " where l.id = ?"; - - selectedLoan = this.jdbcTemplate.queryForObject(sql, rm, + return this.jdbcTemplate.queryForObject(sql, rm, new Object[] { loanId }); } catch (EmptyResultDataAccessException e) { throw new LoanNotFoundException(loanId); } - return selectedLoan; } -//jpw delete after + + @Override + public Collection retrieveRepaymentSchedule( + Long loanId) { + + try { + context.authenticatedUser(); + + LoanScheduleMapper rm = new LoanScheduleMapper(); + String sql = "select " + rm.loanScheduleSchema() + + " where l.id = ? order by ls.loan_id, ls.installment"; + return this.jdbcTemplate.query(sql, rm, new Object[] { loanId }); + + } catch (EmptyResultDataAccessException e) { + throw new LoanNotFoundException(loanId); + } + } + + @Override + public LoanAccountSummaryData retrieveSummary(MoneyData principal, + Collection repaymentSchedule) { + + CurrencyData currencyData = new CurrencyData( + principal.getCurrencyCode(), principal.getDefaultName(), + principal.getDigitsAfterDecimal(), + principal.getDisplaySymbol(), principal.getNameCode()); + + BigDecimal originalPrincipal = BigDecimal.ZERO; + BigDecimal principalPaid = BigDecimal.ZERO; + BigDecimal principalOutstanding = BigDecimal.ZERO; + BigDecimal originalInterest = BigDecimal.ZERO; + BigDecimal interestPaid = BigDecimal.ZERO; + BigDecimal interestOutstanding = BigDecimal.ZERO; + BigDecimal originalTotal = BigDecimal.ZERO; + BigDecimal totalPaid = BigDecimal.ZERO; + BigDecimal totalOutstanding = BigDecimal.ZERO; + 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()); + + 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; + totalWaived = BigDecimal.ONE; + + return new LoanAccountSummaryData(MoneyData.of(currencyData, + originalPrincipal), MoneyData.of(currencyData, principalPaid), + MoneyData.of(currencyData, principalOutstanding), MoneyData.of( + currencyData, originalInterest), MoneyData.of( + currencyData, interestPaid), MoneyData.of(currencyData, + interestOutstanding), MoneyData.of(currencyData, + originalTotal), MoneyData.of(currencyData, totalPaid), + MoneyData.of(currencyData, totalOutstanding), MoneyData.of( + currencyData, totalInArrears), MoneyData.of( + currencyData, totalWaived)); + } + public LoanAccountData convertToData(LoanBasicDetailsData loanBasic) { MoneyData princ = loanBasic.getPrincipal(); - - CurrencyData currencyData = new CurrencyData(princ.getCurrencyCode(), - princ.getDefaultName(), princ.getDigitsAfterDecimal(), princ.getDisplaySymbol(), - princ.getNameCode()); - - final Loan realLoan = this.loanRepository.findOne(loanBasic.getId()); - if (realLoan == null) { - throw new LoanNotFoundException(loanBasic.getId()); - } - DerivedLoanData loanData = realLoan.deriveLoanData(currencyData); - return realLoan.toLoanAccountData(loanBasic, loanData.getSummary(), - loanData.getRepaymentSchedule(), loanData.getLoanRepayments()); + CurrencyData currencyData = new CurrencyData(princ.getCurrencyCode(), + princ.getDefaultName(), princ.getDigitsAfterDecimal(), + princ.getDisplaySymbol(), princ.getNameCode()); + + // DerivedLoanData loanData = deriveLoanData(loanBasic.getId(), + // currencyData, loanBasic.getInArrearsTolerance().getAmount()); + + /* + * return toLoanAccountData(loanBasic, loanData.getSummary(), + * loanData.getRepaymentSchedule(), loanData.getLoanRepayments()); + */ + + // return new LoanAccountData(loanBasic, null, repaymentSchedule, null, + // null); + return null; + } + + /* + * private DerivedLoanData deriveLoanData(Long loanId, CurrencyData + * currencyData, BigDecimal arrearsTolerance) { + * + * List repaymentTransactions = new + * ArrayList(); //for (LoanTransaction loanTransaction : + * this.loanTransactions) { // if (loanTransaction.isRepayment() || + * loanTransaction.isWaiver()) { // + * repaymentTransactions.add(loanTransaction); // } //} + * + * + * LoanRepaymentPeriodDatajpw selectedLoan = null; + * + * Collection periods; try { + * context.authenticatedUser(); + * + * LoanScheduleMapper rm = new LoanScheduleMapper(); String sql = "select " + * + rm.loanScheduleSchema() + " where l.id = ?"; periods = + * this.jdbcTemplate.query(sql, rm, new Object[] { loanId }); + * + * } catch (EmptyResultDataAccessException e) { throw new + * LoanNotFoundException(loanId); } + * + * return new DerivedLoanDataProcessor().process( new + * ArrayList( periods), + * repaymentTransactions, currencyData, arrearsTolerance); } + */ + + private LoanAccountData toLoanAccountData( + LoanBasicDetailsData basicDetails, LoanAccountSummaryData summary, + LoanRepaymentScheduleData repaymentSchedule, + List loanRepayments) { + + // permissions + /* + * boolean waiveAllowed = + * summary.isWaiveAllowed(basicDetails.getInArrearsTolerance()) && + * isNotClosed(); boolean undoDisbursalAllowed = isDisbursed() && + * isOpenWithNoRepaymentMade(); boolean makeRepaymentAllowed = + * isDisbursed() && isNotClosed(); + * + * boolean rejectAllowed = isNotApproved() && isNotDisbursed() && + * isNotClosed(); boolean withdrawnByApplicantAllowed = isNotDisbursed() + * && isNotClosed(); boolean undoApprovalAllowed = isApproved() && + * isNotClosed(); boolean disbursalAllowed = isApproved() && + * isNotDisbursed() && isNotClosed(); + * + * LoanPermissionData permissions = new LoanPermissionData(waiveAllowed, + * makeRepaymentAllowed, rejectAllowed, withdrawnByApplicantAllowed, + * undoApprovalAllowed, undoDisbursalAllowed, disbursalAllowed, + * isSubmittedAndPendingApproval(), isWaitingForDisbursal()); + * + * return new LoanAccountData(basicDetails, summary, repaymentSchedule, + * loanRepayments, permissions); + */ + return null; } @Override @@ -407,4 +545,67 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { } } + private static final class LoanScheduleMapper implements + RowMapper { + + public String loanScheduleSchema() { + + return " ls.loan_id as loanId, ls.installment as period, ls.duedate as `date`, " + + " ls.principal_amount as principal, ls.principal_completed_derived as principalPaid, " + + " ls.interest_amount as interest, ls.interest_completed_derived as interestPaid, " + + " 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_repayment_schedule ls on ls.loan_id = l.id " + + " join ref_currency rc on rc.`code` = l.currency_code "; + } + + @Override + public LoanRepaymentPeriodDatajpw 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 loanId = rs.getLong("loanId"); + Integer period = JdbcSupport.getInteger(rs, "period"); + LocalDate date = JdbcSupport.getLocalDate(rs, "date"); + + BigDecimal principalBD = rs.getBigDecimal("principal"); + MoneyData principal = MoneyData.of(currencyData, principalBD); + BigDecimal principalPaidBD = rs.getBigDecimal("principalPaid"); + MoneyData principalPaid = MoneyData.of(currencyData, + principalPaidBD); + MoneyData principalOutstanding = MoneyData.of(currencyData, + principalBD.subtract(principalPaidBD)); + + BigDecimal interestBD = rs.getBigDecimal("interest"); + MoneyData interest = MoneyData.of(currencyData, interestBD); + BigDecimal interestPaidBD = rs.getBigDecimal("interestPaid"); + MoneyData interestPaid = MoneyData.of(currencyData, interestPaidBD); + MoneyData interestOutstanding = MoneyData.of(currencyData, + interestBD.subtract(interestPaidBD)); + + MoneyData total = MoneyData.of(currencyData, + principalBD.add(interestBD)); + MoneyData totalPaid = MoneyData.of(currencyData, + principalPaidBD.add(interestPaidBD)); + MoneyData totalOutstanding = MoneyData.of(currencyData, + principalBD.add(interestBD).subtract(principalPaidBD) + .subtract(interestPaidBD)); + + return new LoanRepaymentPeriodDatajpw(loanId, period, date, + principal, principalPaid, principalOutstanding, interest, + interestPaid, interestOutstanding, total, totalPaid, + totalOutstanding); + } + } + } \ No newline at end of file