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 fe73046f2..60b576864 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 @@ -30,7 +30,7 @@ import org.mifosng.platform.api.data.LoanPermissionData; import org.mifosng.platform.api.data.LoanRepaymentPeriodData; import org.mifosng.platform.api.data.LoanSchedule; import org.mifosng.platform.api.data.LoanTransactionData; -import org.mifosng.platform.api.data.LoanTransactionDataDTO; +import org.mifosng.platform.api.data.LoanRepaymentTransactionData; import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.api.infrastructure.ApiDataConversionService; import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService; @@ -99,7 +99,7 @@ public class LoansApiResource { LoanBasicDetailsData loanBasicDetails = this.loanReadPlatformService .retrieveLoanAccountDetails(loanId); - Collection loanRepayments = this.loanReadPlatformService + Collection loanRepayments = this.loanReadPlatformService .retrieveLoanPayments(loanId); Collection repaymentSchedule = this.loanReadPlatformService .retrieveRepaymentSchedule(loanId); 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 292d5d9af..f16703ca3 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 @@ -45,7 +45,7 @@ public class LoanAccountData { private LoanAccountSummaryData summary; private Collection repaymentSchedule; - private Collection loanRepayments; + private Collection loanRepayments; private LoanPermissionData permissions; @@ -58,7 +58,7 @@ public class LoanAccountData { public LoanAccountData(LoanBasicDetailsData basicDetails, LoanAccountSummaryData summary, Collection repaymentSchedule, - Collection loanRepayments, + Collection loanRepayments, LoanPermissionData permissions) { this.summary = summary; this.repaymentSchedule = repaymentSchedule; @@ -150,12 +150,12 @@ public class LoanAccountData { this.repaymentSchedule = repaymentSchedule; } - public Collection getLoanRepayments() { + public Collection getLoanRepayments() { return loanRepayments; } public void setLoanRepayments( - Collection loanRepayments) { + Collection loanRepayments) { this.loanRepayments = loanRepayments; } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionDataDTO.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanRepaymentTransactionData.java similarity index 78% rename from mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionDataDTO.java rename to mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanRepaymentTransactionData.java index 91548d9a5..5db7e0458 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionDataDTO.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanRepaymentTransactionData.java @@ -2,18 +2,21 @@ package org.mifosng.platform.api.data; import org.joda.time.LocalDate; -public class LoanTransactionDataDTO { +/** + * This data object replaces {@link LoanTransactionData}. + */ +public class LoanRepaymentTransactionData { private Long id; private EnumOptionData transactionType; private LocalDate date; private MoneyData total; - public LoanTransactionDataDTO() { + public LoanRepaymentTransactionData() { // } - public LoanTransactionDataDTO(Long id, EnumOptionData transactionType, + public LoanRepaymentTransactionData(Long id, EnumOptionData transactionType, final LocalDate date, final MoneyData total) { this.id = id; this.transactionType = transactionType; diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionData.java index 81e3f765b..cc9f5a548 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanTransactionData.java @@ -3,6 +3,10 @@ package org.mifosng.platform.api.data; import org.codehaus.jackson.map.annotate.JsonFilter; import org.joda.time.LocalDate; +/** + * TODO - KW - Think this is deprecated now. + */ +@Deprecated @JsonFilter("loanRepaymentFilter") public class LoanTransactionData { 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 c9e20ad77..5eca6b550 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 @@ -413,15 +413,31 @@ public class Loan extends AbstractAuditableCustom { } } - public void makeRepayment(final LoanTransaction loanTransaction, - LoanLifecycleStateMachine loanLifecycleStateMachine) { - this.loanStatus = loanLifecycleStateMachine.transition( - LoanEvent.LOAN_REPAYMENT, this.loanStatus); + public void makeRepayment(final LoanTransaction loanTransaction, final LoanLifecycleStateMachine loanLifecycleStateMachine) { + this.loanStatus = loanLifecycleStateMachine.transition(LoanEvent.LOAN_REPAYMENT, this.loanStatus); loanTransaction.updateLoan(this); this.loanTransactions.add(loanTransaction); deriveLoanRepaymentScheduleCompletedData(); - + +// Money totalRepaidOrWaived = Money.zero(this.loanRepaymentScheduleDetail.getPrincipal().getCurrency()); +// MonetaryCurrency currency = totalRepaidOrWaived.getCurrency(); +// +// int repaymentIndex = 0; +// +// for (LoanTransaction transaction : this.loanTransactions) { +// if (transaction.isRepayment() || transaction.isWaiver()) { +// totalRepaidOrWaived = totalRepaidOrWaived.plus(transaction.getAmount()); +// +// LoanRepaymentScheduleInstallment currentInstallment = this.repaymentScheduleInstallments.get(repaymentIndex); +// if (currentInstallment.unpaid()) { +// currentInstallment.payOffWith(transaction, currency); +// } else { +// repaymentIndex++; +// } +// } +// } + if (loanTransaction.isNotRepayment()) { final String errorMessage = "A transaction of type repayment was expected but not received."; throw new InvalidLoanTransactionTypeException("transaction", @@ -466,23 +482,18 @@ public class Loan extends AbstractAuditableCustom { Money totalRepaidOrWaivedAgainstLoan = calculateTotalPaidOrWaived(); Money remainingToPayoffAgainstLoanSchedule = totalRepaidOrWaivedAgainstLoan; - Money totalOverpaid = Money.zero(totalRepaidOrWaivedAgainstLoan - .getCurrency()); + Money totalOverpaid = Money.zero(totalRepaidOrWaivedAgainstLoan.getCurrency()); int repaymentInstallmentIndex = 0; while (remainingToPayoffAgainstLoanSchedule.isGreaterThanZero()) { - if (repaymentInstallmentIndex == this.repaymentScheduleInstallments - .size()) { + if (repaymentInstallmentIndex == this.repaymentScheduleInstallments.size()) { totalOverpaid = remainingToPayoffAgainstLoanSchedule; // to exit while loop - remainingToPayoffAgainstLoanSchedule = remainingToPayoffAgainstLoanSchedule - .minus(totalOverpaid); + remainingToPayoffAgainstLoanSchedule = remainingToPayoffAgainstLoanSchedule.minus(totalOverpaid); } else { - LoanRepaymentScheduleInstallment scheduledRepaymentInstallment = this.repaymentScheduleInstallments - .get(repaymentInstallmentIndex); - remainingToPayoffAgainstLoanSchedule = scheduledRepaymentInstallment - .updateDerivedComponents(remainingToPayoffAgainstLoanSchedule); + LoanRepaymentScheduleInstallment scheduledRepaymentInstallment = this.repaymentScheduleInstallments.get(repaymentInstallmentIndex); + remainingToPayoffAgainstLoanSchedule = scheduledRepaymentInstallment.updateDerivedComponents(remainingToPayoffAgainstLoanSchedule); repaymentInstallmentIndex++; } } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/LoanRepaymentScheduleInstallment.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/LoanRepaymentScheduleInstallment.java index 4f3addb96..f8f340a03 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/LoanRepaymentScheduleInstallment.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/LoanRepaymentScheduleInstallment.java @@ -151,4 +151,18 @@ public class LoanRepaymentScheduleInstallment extends AbstractAuditableCustom retrieveRepaymentSchedule(Long loanId); - Collection retrieveLoanPayments(Long loanId); + Collection retrieveLoanPayments(Long loanId); LoanAccountSummaryData retrieveSummary(MoneyData principal, Collection repaymentSchedule, - Collection loanRepayments); + Collection loanRepayments); LoanPermissionData retrieveLoanPermissions( LoanBasicDetailsData loanBasicDetails, boolean isWaiverAllowed, 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 58c5b4835..717f05d8f 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 @@ -17,7 +17,7 @@ import org.mifosng.platform.api.data.LoanProductData; import org.mifosng.platform.api.data.LoanProductLookup; import org.mifosng.platform.api.data.LoanRepaymentPeriodData; import org.mifosng.platform.api.data.LoanTransactionData; -import org.mifosng.platform.api.data.LoanTransactionDataDTO; +import org.mifosng.platform.api.data.LoanRepaymentTransactionData; import org.mifosng.platform.api.data.MoneyData; import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.client.service.ClientReadPlatformService; @@ -110,7 +110,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { @Override public LoanAccountSummaryData retrieveSummary(MoneyData principal, Collection repaymentSchedule, - Collection loanRepayments) { + Collection loanRepayments) { CurrencyData currencyData = new CurrencyData( principal.getCurrencyCode(), principal.getDefaultName(), @@ -157,7 +157,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { } Long waiverType = (long) 4; - for (LoanTransactionDataDTO loanRepayment : loanRepayments) { + for (LoanRepaymentTransactionData loanRepayment : loanRepayments) { Long transactionType = loanRepayment.getTransactionType().getId(); if (transactionType.equals(waiverType)) { totalWaived = totalWaived.add(loanRepayment.getTotal() @@ -179,17 +179,17 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { } @Override - public Collection retrieveLoanPayments(Long loanId) { + 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) + // disbursements (1) and have not been 'contra'ed by another transaction String sql = "select " + rm.LoanPaymentsSchema() - + " where tr.loan_id = ? and tr.transaction_type_enum not in (0, 1) order by tr.transaction_date"; + + " where tr.loan_id = ? and tr.transaction_type_enum not in (0, 1) and tr.contra_id is null order by tr.transaction_date ASC"; return this.jdbcTemplate.query(sql, rm, new Object[] { loanId }); } catch (EmptyResultDataAccessException e) { @@ -581,7 +581,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { } private static final class LoanPaymentsMapper implements - RowMapper { + RowMapper { public String LoanPaymentsSchema() { @@ -593,7 +593,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { } @Override - public LoanTransactionDataDTO mapRow(final ResultSet rs, + public LoanRepaymentTransactionData mapRow(final ResultSet rs, final int rowNum) throws SQLException { String currencyCode = rs.getString("currencyCode"); @@ -616,7 +616,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { BigDecimal totalBD = rs.getBigDecimal("total"); MoneyData total = MoneyData.of(currencyData, totalBD); - return new LoanTransactionDataDTO(id, transactionType, date, total); + return new LoanRepaymentTransactionData(id, transactionType, date, total); } }