use of org.mifos.clientportfolio.loan.service.CreateLoanSchedule in project head by mifos.
the class LoanAccountServiceFacadeWebTier method applyLoanRepayment.
@Override
public void applyLoanRepayment(String globalAccountNumber, LocalDate paymentDate, BigDecimal repaymentAmount, String receiptId, LocalDate receiptDate, Short modeOfPayment) {
MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = toUserContext(user);
try {
this.transactionHelper.startTransaction();
LoanBO loan = loanDao.findByGlobalAccountNum(globalAccountNumber);
PersonnelBO personnel = personnelDao.findPersonnelById((short) user.getUserId());
Money outstandingOverpayment = loan.applyNewPaymentMechanism(paymentDate, repaymentAmount, personnel, receiptId, receiptDate, modeOfPayment);
// 3. pay off principal of next installment and recalculate interest if 'over paid'
if (outstandingOverpayment.isGreaterThanZero()) {
Money totalPrincipalDueNow = loan.getTotalPrincipalDue().subtract(outstandingOverpayment);
// assemble into domain entities
LoanOfferingBO loanProduct = this.loanProductDao.findById(loan.getLoanOffering().getPrdOfferingId().intValue());
CustomerBO customer = this.customerDao.findCustomerById(loan.getCustomer().getCustomerId());
List<AccountFeesEntity> accountFeeEntities = new ArrayList<AccountFeesEntity>();
Integer unpaidInstallments = loan.getDetailsOfUnpaidInstallmentsOn(paymentDate).size();
Integer gracePeriodDiff = loan.getNoOfInstallments().intValue() - loan.getGracePeriodDuration().intValue();
Integer gracePeriodsRemaining = Math.max(0, unpaidInstallments - gracePeriodDiff);
LocalDate disbursementDate = new LocalDate(loan.getDetailsOfUpcomigInstallment().getActionDate());
LoanProductOverridenDetail overridenDetail = new LoanProductOverridenDetail(totalPrincipalDueNow, disbursementDate, loan.getInterestRate(), unpaidInstallments, gracePeriodsRemaining, accountFeeEntities, new ArrayList<AccountPenaltiesEntity>());
Integer interestDays = Integer.valueOf(AccountingRules.getNumberOfInterestDays().intValue());
boolean loanScheduleIndependentOfCustomerMeetingEnabled = false;
MeetingBO loanMeeting = customer.getCustomerMeetingValue();
if (loanScheduleIndependentOfCustomerMeetingEnabled) {
RecurringSchedule createLoanSchedule = new MonthlyOnDayOfMonthSchedule(Integer.valueOf(1), Integer.valueOf(5));
loanMeeting = this.createNewMeetingForRepaymentDay(disbursementDate, createLoanSchedule, customer);
if (loanProduct.isVariableInstallmentsAllowed()) {
loanMeeting.setMeetingStartDate(disbursementDate.toDateMidnight().toDate());
}
}
LoanScheduleConfiguration configuration = new LoanScheduleConfiguration(loanScheduleIndependentOfCustomerMeetingEnabled, interestDays);
Short userBranchOfficeId = userContext.getBranchId();
LoanSchedule loanSchedule = this.loanScheduleService.generate(loanProduct, customer, loanMeeting, overridenDetail, configuration, userBranchOfficeId, accountFeeEntities, disbursementDate);
loan.rescheduleRemainingUnpaidInstallments(loanSchedule, paymentDate);
loan.recordOverpayment(outstandingOverpayment, paymentDate, personnel, receiptId, receiptDate, modeOfPayment);
}
this.loanDao.save(loan);
this.transactionHelper.commitTransaction();
} catch (BusinessRuleException e) {
this.transactionHelper.rollbackTransaction();
throw new BusinessRuleException(e.getMessageKey(), e);
} catch (AccountException e) {
this.transactionHelper.rollbackTransaction();
throw new BusinessRuleException(e.getKey(), e);
} finally {
this.transactionHelper.closeSession();
}
}
use of org.mifos.clientportfolio.loan.service.CreateLoanSchedule in project head by mifos.
the class LoanAccountController method retrieveLoanSchedule.
public LoanScheduleDto retrieveLoanSchedule(int customerId, int productId, LoanAccountFormBean formBean, BackdatedPaymentable loanScheduleFormBean, boolean resetRedoLoanAccountDetails) {
LocalDate disbursementDate = LoanCreationHelper.translateDisbursementDateToLocalDate(formBean);
RecurringSchedule recurringSchedule = LoanCreationHelper.determineRecurringSchedule(formBean);
List<CreateAccountFeeDto> accountFees = LoanCreationHelper.translateToAccountFeeDtos(formBean);
List<CreateAccountFeeDto> additionalAccountFees = LoanCreationHelper.translateToAdditionalAccountFeeDtos(formBean);
accountFees.addAll(additionalAccountFees);
CreateLoanSchedule createLoanAccount = new CreateLoanSchedule(customerId, productId, BigDecimal.valueOf(formBean.getAmount().doubleValue()), formBean.getInterestRate().doubleValue(), disbursementDate, formBean.getNumberOfInstallments().intValue(), formBean.getGraceDuration().intValue(), formBean.isRepaymentScheduleIndependentOfCustomerMeeting(), recurringSchedule, accountFees);
LoanScheduleDto loanSchedule = null;
if (formBean.isVariableInstallmentsAllowed() && !loanScheduleFormBean.getInstallments().isEmpty()) {
loanSchedule = loanAccountServiceFacade.createLoanSchedule(createLoanAccount, loanScheduleFormBean.getInstallments(), loanScheduleFormBean.getInstallmentAmounts());
} else {
loanSchedule = loanAccountServiceFacade.createLoanSchedule(createLoanAccount);
}
loanControllerHelper.populateFormBeanFromDto(customerId, productId, formBean, loanScheduleFormBean, disbursementDate, loanSchedule, resetRedoLoanAccountDetails);
return loanSchedule;
}
use of org.mifos.clientportfolio.loan.service.CreateLoanSchedule in project head by mifos.
the class LoanAccountServiceFacadeWebTier method createLoanSchedule.
@Override
public LoanScheduleDto createLoanSchedule(CreateLoanSchedule createLoanSchedule, List<DateTime> loanScheduleDates, List<Number> totalInstallmentAmounts) {
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 = customer.getCustomerMeetingValue();
if (loanScheduleIndependentOfCustomerMeetingEnabled) {
loanMeeting = this.createNewMeetingForRepaymentDay(createLoanSchedule.getDisbursementDate(), createLoanSchedule, customer);
if (loanProduct.isVariableInstallmentsAllowed()) {
loanMeeting.setMeetingStartDate(createLoanSchedule.getDisbursementDate().toDateMidnight().toDate());
}
}
LoanScheduleConfiguration configuration = new LoanScheduleConfiguration(loanScheduleIndependentOfCustomerMeetingEnabled, interestDays);
LoanSchedule loanSchedule = this.loanScheduleService.generate(loanProduct, customer, loanMeeting, overridenDetail, configuration, accountFeeEntities, createLoanSchedule.getDisbursementDate(), loanScheduleDates, totalInstallmentAmounts);
// 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);
}
use of org.mifos.clientportfolio.loan.service.CreateLoanSchedule 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