use of org.mifos.dto.domain.LoanCreationInstallmentDto in project head by mifos.
the class LoanScheduleFormBean method validateCalculateAndReviewLoanSchedule.
/**
* validateXXXX is invoked on transition from state
*/
public void validateCalculateAndReviewLoanSchedule(ValidationContext context) {
MessageContext messageContext = context.getMessageContext();
if (this.variableInstallmentsAllowed) {
prevalidateDueDateIsNonNull(messageContext);
prevalidateActualPaymentDateIsNonNull(messageContext);
prevalidateTotalIsNonNull(messageContext);
prevalidateAmountPaidIsNonNull(messageContext);
if (!messageContext.hasErrorMessages()) {
boolean resetRedoLoanAccountDetails = false;
LoanScheduleDto recalculatedLoanSchedule = this.loanAccountController.retrieveLoanSchedule(customerId, loanAccountFormBean.getProductId(), loanAccountFormBean, this, resetRedoLoanAccountDetails);
// 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);
handleErrors(messageContext, inputInstallmentsErrors, scheduleErrors);
} else {
prevalidateAmountPaidIsNonNull(messageContext);
for (int index = 0; index < this.actualPaymentAmounts.size(); index++) {
Double newTotal = Double.valueOf("0.0");
Number newTotalEntry = this.actualPaymentAmounts.get(index);
if (newTotalEntry != null) {
newTotal = newTotalEntry.doubleValue();
} else {
this.actualPaymentAmounts.set(index, newTotal);
}
}
}
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.domain.LoanCreationInstallmentDto in project head by mifos.
the class LoanAccountServiceFacadeWebTier method createLoanSchedule.
@Override
public LoanScheduleDto createLoanSchedule(CreateLoanSchedule createLoanSchedule) {
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = toUserContext(user);
// assemble into domain entities
LoanOfferingBO loanProduct = this.loanProductDao.findById(createLoanSchedule.getProductId());
CustomerBO customer = this.customerDao.findCustomerById(createLoanSchedule.getCustomerId());
Money loanAmountDisbursed = new Money(loanProduct.getCurrency(), createLoanSchedule.getLoanAmount());
List<AccountFeesEntity> accountFeeEntities = assembleAccountFees(createLoanSchedule.getAccountFeeEntities());
LoanProductOverridenDetail overridenDetail = new LoanProductOverridenDetail(loanAmountDisbursed, createLoanSchedule.getDisbursementDate(), createLoanSchedule.getInterestRate(), createLoanSchedule.getNumberOfInstallments(), createLoanSchedule.getGraceDuration(), accountFeeEntities, new ArrayList<AccountPenaltiesEntity>());
Integer interestDays = Integer.valueOf(AccountingRules.getNumberOfInterestDays().intValue());
boolean loanScheduleIndependentOfCustomerMeetingEnabled = createLoanSchedule.isRepaymentIndependentOfCustomerMeetingSchedule();
MeetingBO loanMeeting = null;
if (loanScheduleIndependentOfCustomerMeetingEnabled) {
loanMeeting = this.createNewMeetingForRepaymentDay(createLoanSchedule.getDisbursementDate(), createLoanSchedule, customer);
if (loanProduct.isVariableInstallmentsAllowed()) {
loanMeeting.setMeetingStartDate(createLoanSchedule.getDisbursementDate().toDateMidnight().toDate());
}
} else {
MeetingDto customerMeetingDto = customer.getCustomerMeetingValue().toDto();
loanMeeting = new MeetingFactory().create(customerMeetingDto);
Short recurAfter = loanProduct.getLoanOfferingMeeting().getMeeting().getRecurAfter();
loanMeeting.getMeetingDetails().setRecurAfter(recurAfter);
}
LoanScheduleConfiguration configuration = new LoanScheduleConfiguration(loanScheduleIndependentOfCustomerMeetingEnabled, interestDays);
LoanSchedule loanSchedule = this.loanScheduleService.generate(loanProduct, customer, loanMeeting, overridenDetail, configuration, userContext.getBranchId(), accountFeeEntities, createLoanSchedule.getDisbursementDate());
// translate to DTO form
List<LoanCreationInstallmentDto> installments = new ArrayList<LoanCreationInstallmentDto>();
Short digitsAfterDecimal = AccountingRules.getDigitsAfterDecimal();
for (LoanScheduleEntity loanScheduleEntity : loanSchedule.getRoundedLoanSchedules()) {
Integer installmentNumber = loanScheduleEntity.getInstallmentId().intValue();
LocalDate dueDate = new LocalDate(loanScheduleEntity.getActionDate());
String principal = loanScheduleEntity.getPrincipal().toString(digitsAfterDecimal);
String interest = loanScheduleEntity.getInterest().toString(digitsAfterDecimal);
String fees = loanScheduleEntity.getTotalFees().toString(digitsAfterDecimal);
String penalty = "0.0";
String total = loanScheduleEntity.getPrincipal().add(loanScheduleEntity.getInterest()).add(loanScheduleEntity.getTotalFees()).toString(digitsAfterDecimal);
LoanCreationInstallmentDto installment = new LoanCreationInstallmentDto(installmentNumber, dueDate, Double.valueOf(principal), Double.valueOf(interest), Double.valueOf(fees), Double.valueOf(penalty), Double.valueOf(total));
installments.add(installment);
}
return new LoanScheduleDto(customer.getDisplayName(), Double.valueOf(createLoanSchedule.getLoanAmount().doubleValue()), createLoanSchedule.getDisbursementDate(), loanProduct.getGraceType().getValue().intValue(), installments);
}
Aggregations