use of org.mifos.dto.screen.LoanInstallmentsDto in project head by mifos.
the class CashFlowSummaryFormBean method validateSummaryOfCashflow.
/**
* validateXXXX is invoked on transition from state
*/
public void validateSummaryOfCashflow(ValidationContext context) {
MessageContext messageContext = context.getMessageContext();
if (this.variableInstallmentsAllowed) {
prevalidateDueDateIsNonNull(messageContext);
prevalidateActualPaymentDateIsNonNull(messageContext);
prevalidateTotalIsNonNull(messageContext);
prevalidateAmountPaidIsNonNull(messageContext);
if (!messageContext.hasErrorMessages()) {
LoanScheduleDto recalculatedLoanSchedule = this.loanAccountController.retrieveLoanSchedule(customerId, productId, loanAccountFormBean, this, false);
// set values on fields
this.variableInstallments = recalculatedLoanSchedule.getInstallments();
int installIndex = 0;
for (LoanCreationInstallmentDto installment : this.variableInstallments) {
this.installmentAmounts.set(installIndex, installment.getTotal());
installIndex++;
}
}
Errors inputInstallmentsErrors = loanAccountServiceFacade.validateInputInstallments(disbursementDate, minGapInDays, maxGapInDays, minInstallmentAmount, variableInstallments, customerId);
Errors scheduleErrors = loanAccountServiceFacade.validateInstallmentSchedule(variableInstallments, minInstallmentAmount);
if (inputInstallmentsErrors.hasErrors()) {
for (ErrorEntry fieldError : inputInstallmentsErrors.getErrorEntries()) {
addErrorMessageToContext(messageContext, fieldError);
}
}
if (scheduleErrors.hasErrors()) {
for (ErrorEntry fieldError : scheduleErrors.getErrorEntries()) {
addErrorMessageToContext(messageContext, fieldError);
}
}
} else {
prevalidateTotalIsNonNull(messageContext);
for (int index = 0; index < this.installmentAmounts.size(); index++) {
Double newTotal = Double.valueOf("0.0");
Number newTotalEntry = this.installmentAmounts.get(index);
if (newTotalEntry != null) {
newTotal = newTotalEntry.doubleValue();
} else {
this.installmentAmounts.set(index, newTotal);
}
}
}
if (!messageContext.hasErrorMessages()) {
DateTime firstInstallmentDueDate = installments.get(0);
DateTime lastInstallmentDueDate = installments.get(installments.size() - 1);
this.loanInstallmentsDto = new LoanInstallmentsDto(this.loanInstallmentsDto.getLoanAmount(), this.loanInstallmentsDto.getTotalInstallmentAmount(), firstInstallmentDueDate.toDate(), lastInstallmentDueDate.toDate());
Errors warnings = loanAccountServiceFacade.validateCashFlowForInstallmentsForWarnings(cashFlowDataDtos, productId);
Errors errors = loanAccountServiceFacade.validateCashFlowForInstallments(loanInstallmentsDto, monthlyCashFlows, repaymentCapacity, cashFlowTotalBalance);
if (warnings.hasErrors()) {
for (ErrorEntry fieldError : warnings.getErrorEntries()) {
addErrorMessageToContext(messageContext, fieldError);
}
}
if (errors.hasErrors()) {
for (ErrorEntry fieldError : errors.getErrorEntries()) {
addErrorMessageToContext(messageContext, fieldError);
}
}
}
List<LoanRepaymentTransaction> loanRepaymentTransaction = new ArrayList<LoanRepaymentTransaction>();
this.loanRepaymentPaidInstallmentsWithRunningBalance = new ArrayList<LoanRepaymentRunningBalance>();
this.loanRepaymentFutureInstallments = new ArrayList<LoanRepaymentFutureInstallments>();
// if any actual payment data exists, calculate
validatePaymentsAndAmounts(messageContext, this.actualPaymentDates, this.actualPaymentAmounts);
List<LoanInstallmentPostPayment> installmentsPostPayment = new ArrayList<LoanInstallmentPostPayment>();
int paymentIndex = 0;
if (!messageContext.hasErrorMessages()) {
for (Number actualPayment : this.actualPaymentAmounts) {
BigDecimal remainingPayment = BigDecimal.valueOf(actualPayment.doubleValue());
LocalDate paymentDate = new LocalDate(this.actualPaymentDates.get(paymentIndex));
loanRepaymentTransaction.add(new LoanRepaymentTransaction(paymentDate, remainingPayment));
int installmentIndex = 0;
while (remainingPayment.doubleValue() > BigDecimal.ZERO.doubleValue() && installmentIndex < this.actualPaymentAmounts.size()) {
LoanCreationInstallmentDto installmentDetails = this.repaymentInstallments.get(installmentIndex);
Double installmentTotalAmount = this.installmentAmounts.get(installmentIndex).doubleValue();
LocalDate dueDate = new LocalDate(this.installments.get(installmentIndex));
if (installmentsPostPayment.isEmpty() || installmentsPostPayment.size() <= installmentIndex) {
BigDecimal feesPaid = BigDecimal.valueOf(installmentDetails.getFees());
if (remainingPayment.doubleValue() >= installmentDetails.getFees()) {
remainingPayment = remainingPayment.subtract(feesPaid);
} else {
feesPaid = remainingPayment;
remainingPayment = remainingPayment.subtract(feesPaid);
}
BigDecimal interestPaid = BigDecimal.valueOf(installmentDetails.getInterest());
if (remainingPayment.doubleValue() >= installmentDetails.getInterest()) {
remainingPayment = remainingPayment.subtract(interestPaid);
} else {
interestPaid = remainingPayment;
remainingPayment = remainingPayment.subtract(interestPaid);
}
BigDecimal principalPaid = BigDecimal.valueOf(installmentDetails.getPrincipal());
if (remainingPayment.doubleValue() >= installmentDetails.getPrincipal()) {
remainingPayment = remainingPayment.subtract(principalPaid);
} else {
principalPaid = remainingPayment;
remainingPayment = remainingPayment.subtract(principalPaid);
}
BigDecimal totalInstallmentPaid = feesPaid.add(interestPaid).add(principalPaid);
LoanInstallmentPostPayment loanInstallmentPostPayment = new LoanInstallmentPostPayment(installmentDetails.getInstallmentNumber(), dueDate, paymentDate, feesPaid, interestPaid, principalPaid, totalInstallmentPaid, installmentTotalAmount);
installmentsPostPayment.add(loanInstallmentPostPayment);
} else {
LoanInstallmentPostPayment paidInstallment = installmentsPostPayment.get(installmentIndex);
if (paidInstallment.isNotFullyPaid()) {
BigDecimal feesToBePaid = BigDecimal.valueOf(installmentDetails.getFees()).subtract(paidInstallment.getFeesPaid());
if (remainingPayment.doubleValue() >= feesToBePaid.doubleValue()) {
remainingPayment = remainingPayment.subtract(feesToBePaid);
} else {
feesToBePaid = remainingPayment;
remainingPayment = remainingPayment.subtract(feesToBePaid);
}
BigDecimal interestToBePaid = BigDecimal.valueOf(installmentDetails.getInterest()).subtract(paidInstallment.getInterestPaid());
if (remainingPayment.doubleValue() >= interestToBePaid.doubleValue()) {
remainingPayment = remainingPayment.subtract(interestToBePaid);
} else {
interestToBePaid = remainingPayment;
remainingPayment = remainingPayment.subtract(interestToBePaid);
}
BigDecimal principalToBePaid = BigDecimal.valueOf(installmentDetails.getPrincipal()).subtract(paidInstallment.getPrincipalPaid());
if (remainingPayment.doubleValue() >= principalToBePaid.doubleValue()) {
remainingPayment = remainingPayment.subtract(principalToBePaid);
} else {
principalToBePaid = remainingPayment;
remainingPayment = remainingPayment.subtract(principalToBePaid);
}
BigDecimal totalInstallmentPaid = feesToBePaid.add(interestToBePaid).add(principalToBePaid);
paidInstallment.setLastPaymentDate(paymentDate);
paidInstallment.setFeesPaid(paidInstallment.getFeesPaid().add(feesToBePaid));
paidInstallment.setInterestPaid(paidInstallment.getInterestPaid().add(interestToBePaid));
paidInstallment.setPrincipalPaid(paidInstallment.getPrincipalPaid().add(principalToBePaid));
paidInstallment.setTotalInstallmentPaid(paidInstallment.getTotalInstallmentPaid().add(totalInstallmentPaid));
}
}
installmentIndex++;
}
paymentIndex++;
}
}
// remaining running balance
BigDecimal cumulativeFeesPaid = BigDecimal.ZERO;
BigDecimal cumulativeInterestPaid = BigDecimal.ZERO;
BigDecimal cumulativePrincipalPaid = BigDecimal.ZERO;
BigDecimal cumulativeTotalInstallmentPaid = BigDecimal.ZERO;
for (LoanInstallmentPostPayment installment : installmentsPostPayment) {
cumulativeFeesPaid = cumulativeFeesPaid.add(installment.getFeesPaid());
cumulativeInterestPaid = cumulativeInterestPaid.add(installment.getInterestPaid());
cumulativePrincipalPaid = cumulativePrincipalPaid.add(installment.getPrincipalPaid());
cumulativeTotalInstallmentPaid = cumulativeTotalInstallmentPaid.add(installment.getTotalInstallmentPaid());
if (installment.isNotFullyPaid()) {
BigDecimal remainingFees = this.totalLoanFees.subtract(cumulativeFeesPaid);
BigDecimal remainingInterest = this.totalLoanInterest.subtract(cumulativeInterestPaid);
BigDecimal remainingTotalInstallment = this.loanPrincipal.add(this.totalLoanFees).add(this.totalLoanInterest).subtract(cumulativeTotalInstallmentPaid);
BigDecimal remainingPrincipal = remainingTotalInstallment.subtract(remainingInterest).subtract(remainingFees);
LoanCreationInstallmentDto installmentDetails = this.repaymentInstallments.get(installment.getInstallmentNumber() - 1);
LoanCreationInstallmentDto installmentPaidDetails = new LoanCreationInstallmentDto(installment.getInstallmentNumber(), new LocalDate(installmentDetails.getDueDate()), installment.getPrincipalPaid().doubleValue(), installment.getInterestPaid().doubleValue(), installment.getFeesPaid().doubleValue(), BigDecimal.ZERO.doubleValue(), installment.getTotalInstallmentPaid().doubleValue());
this.loanRepaymentPaidInstallmentsWithRunningBalance.add(new LoanRepaymentRunningBalance(installmentPaidDetails, installment.getTotalInstallmentPaid(), remainingPrincipal, remainingInterest, remainingFees, remainingTotalInstallment, installment.getLastPaymentDate(), this.actualPaymentTypes.get(installment.getInstallmentNumber() - 1)));
BigDecimal outstandingInstallmentPrincipal = BigDecimal.valueOf(installmentDetails.getPrincipal()).subtract(installment.getPrincipalPaid());
BigDecimal outstandingInstallmentInterest = BigDecimal.valueOf(installmentDetails.getInterest()).subtract(installment.getInterestPaid());
BigDecimal outstandingInstallmentFees = BigDecimal.valueOf(installmentDetails.getFees()).subtract(installment.getFeesPaid());
this.loanRepaymentFutureInstallments.add(new LoanRepaymentFutureInstallments(installmentDetails.getInstallmentNumber(), installmentDetails.getDueDate(), outstandingInstallmentPrincipal, outstandingInstallmentInterest, outstandingInstallmentFees, outstandingInstallmentPrincipal.add(outstandingInstallmentInterest).add(outstandingInstallmentFees)));
} else {
BigDecimal remainingFees = this.totalLoanFees.subtract(cumulativeFeesPaid);
BigDecimal remainingInterest = this.totalLoanInterest.subtract(cumulativeInterestPaid);
BigDecimal remainingTotalInstallment = this.loanPrincipal.add(this.totalLoanFees).add(this.totalLoanInterest).subtract(cumulativeTotalInstallmentPaid);
BigDecimal remainingPrincipal = remainingTotalInstallment.subtract(remainingInterest).subtract(remainingFees);
LoanCreationInstallmentDto installmentDetails = this.repaymentInstallments.get(installment.getInstallmentNumber() - 1);
this.loanRepaymentPaidInstallmentsWithRunningBalance.add(new LoanRepaymentRunningBalance(installmentDetails, installment.getTotalInstallmentPaid(), remainingPrincipal, remainingInterest, remainingFees, remainingTotalInstallment, installment.getLastPaymentDate(), this.actualPaymentTypes.get(installment.getInstallmentNumber() - 1)));
}
}
int lastHandledFutureInstallmentNumber = loanRepaymentPaidInstallmentsWithRunningBalance.size();
if (!this.loanRepaymentFutureInstallments.isEmpty()) {
lastHandledFutureInstallmentNumber = this.loanRepaymentFutureInstallments.get(this.loanRepaymentFutureInstallments.size() - 1).getInstallmentNumber();
}
for (LoanCreationInstallmentDto installmentDto : this.repaymentInstallments) {
if (installmentDto.getInstallmentNumber() > lastHandledFutureInstallmentNumber) {
this.loanRepaymentFutureInstallments.add(new LoanRepaymentFutureInstallments(installmentDto.getInstallmentNumber(), installmentDto.getDueDate(), BigDecimal.valueOf(installmentDto.getPrincipal()), BigDecimal.valueOf(installmentDto.getInterest()), BigDecimal.valueOf(installmentDto.getFees()), BigDecimal.valueOf(installmentDto.getTotal())));
}
}
}
use of org.mifos.dto.screen.LoanInstallmentsDto in project head by mifos.
the class LoanAccountController method retrieveCashflowSummaryDetails.
public List<CashFlowDataDto> retrieveCashflowSummaryDetails(CashFlowSummaryFormBean formBean, CashFlowDto cashFlowDto, List<MonthlyCashFlowDto> monthlyCashFlow, LoanScheduleDto loanScheduleDto, int productId, LoanAccountFormBean loanAccountFormBean) {
List<CashFlowDataDto> cashFlowDataDtos = this.loanAccountServiceFacade.retrieveCashFlowSummary(monthlyCashFlow, loanScheduleDto);
formBean.setCashFlowDataDtos(cashFlowDataDtos);
formBean.setProductId(productId);
BigDecimal loanAmount = BigDecimal.valueOf(loanScheduleDto.getLoanAmount());
BigDecimal totalInstallmentAmount = BigDecimal.ZERO;
DateTime firstInstallmentDueDate = new DateTime();
DateTime lastInstallmentDueDate = new DateTime();
List<DateTime> installments = new ArrayList<DateTime>();
List<Number> installmentAmounts = new ArrayList<Number>();
if (!loanScheduleDto.getInstallments().isEmpty()) {
firstInstallmentDueDate = loanScheduleDto.firstInstallment();
lastInstallmentDueDate = loanScheduleDto.lastInstallment();
for (LoanCreationInstallmentDto installment : loanScheduleDto.getInstallments()) {
totalInstallmentAmount = totalInstallmentAmount.add(BigDecimal.valueOf(installment.getTotal()));
installments.add(new DateTime(installment.getDueDate()));
installmentAmounts.add(installment.getTotal());
}
}
formBean.setInstallments(installments);
formBean.setInstallmentAmounts(installmentAmounts);
formBean.setLoanPrincipal(loanAmount);
LoanInstallmentsDto loanInstallmentsDto = new LoanInstallmentsDto(loanAmount, totalInstallmentAmount, firstInstallmentDueDate.toDate(), lastInstallmentDueDate.toDate());
BigDecimal cashFlowTotalBalance = BigDecimal.ZERO;
for (MonthlyCashFlowDto monthlyCashFlowDto : monthlyCashFlow) {
cashFlowTotalBalance = cashFlowTotalBalance.add(monthlyCashFlowDto.calculateRevenueMinusExpenses());
}
formBean.setMonthlyCashFlows(monthlyCashFlow);
formBean.setLoanInstallmentsDto(loanInstallmentsDto);
formBean.setCashFlowTotalBalance(cashFlowTotalBalance);
formBean.setRepaymentCapacity(cashFlowDto.getRepaymentCapacity());
LocalDate disbursementDate = LoanCreationHelper.translateDisbursementDateToLocalDate(loanAccountFormBean);
loanControllerHelper.populateFormBeanFromDto(loanAccountFormBean.getCustomerId(), productId, loanAccountFormBean, formBean, disbursementDate, loanScheduleDto, true);
return cashFlowDataDtos;
}
Aggregations