Search in sources :

Example 1 with CreateLoanSchedule

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();
    }
}
Also used : AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) CreateLoanSchedule(org.mifos.clientportfolio.loan.service.CreateLoanSchedule) LoanSchedule(org.mifos.clientportfolio.newloan.domain.LoanSchedule) UserContext(org.mifos.security.util.UserContext) MeetingBO(org.mifos.application.meeting.business.MeetingBO) LoanBO(org.mifos.accounts.loan.business.LoanBO) ArrayList(java.util.ArrayList) MifosUser(org.mifos.security.MifosUser) LoanProductOverridenDetail(org.mifos.clientportfolio.newloan.domain.LoanProductOverridenDetail) LocalDate(org.joda.time.LocalDate) LoanScheduleConfiguration(org.mifos.clientportfolio.newloan.domain.LoanScheduleConfiguration) Money(org.mifos.framework.util.helpers.Money) RecurringSchedule(org.mifos.clientportfolio.loan.service.RecurringSchedule) BusinessRuleException(org.mifos.service.BusinessRuleException) MonthlyOnDayOfMonthSchedule(org.mifos.clientportfolio.loan.service.MonthlyOnDayOfMonthSchedule) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) LoanOfferingBO(org.mifos.accounts.productdefinition.business.LoanOfferingBO) CustomerBO(org.mifos.customers.business.CustomerBO) AccountFeesEntity(org.mifos.accounts.business.AccountFeesEntity)

Example 2 with CreateLoanSchedule

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;
}
Also used : RecurringSchedule(org.mifos.clientportfolio.loan.service.RecurringSchedule) CreateLoanSchedule(org.mifos.clientportfolio.loan.service.CreateLoanSchedule) LoanScheduleDto(org.mifos.dto.screen.LoanScheduleDto) CreateAccountFeeDto(org.mifos.dto.domain.CreateAccountFeeDto) LocalDate(org.joda.time.LocalDate)

Example 3 with CreateLoanSchedule

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);
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) OriginalLoanScheduleEntity(org.mifos.accounts.loan.business.OriginalLoanScheduleEntity) AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) CreateLoanSchedule(org.mifos.clientportfolio.loan.service.CreateLoanSchedule) LoanSchedule(org.mifos.clientportfolio.newloan.domain.LoanSchedule) UserContext(org.mifos.security.util.UserContext) MeetingBO(org.mifos.application.meeting.business.MeetingBO) ArrayList(java.util.ArrayList) LoanScheduleDto(org.mifos.dto.screen.LoanScheduleDto) MifosUser(org.mifos.security.MifosUser) LoanCreationInstallmentDto(org.mifos.dto.domain.LoanCreationInstallmentDto) LoanProductOverridenDetail(org.mifos.clientportfolio.newloan.domain.LoanProductOverridenDetail) LocalDate(org.joda.time.LocalDate) LoanScheduleConfiguration(org.mifos.clientportfolio.newloan.domain.LoanScheduleConfiguration) Money(org.mifos.framework.util.helpers.Money) LoanOfferingBO(org.mifos.accounts.productdefinition.business.LoanOfferingBO) CustomerBO(org.mifos.customers.business.CustomerBO) AccountFeesEntity(org.mifos.accounts.business.AccountFeesEntity)

Example 4 with CreateLoanSchedule

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);
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) OriginalLoanScheduleEntity(org.mifos.accounts.loan.business.OriginalLoanScheduleEntity) AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) MeetingBO(org.mifos.application.meeting.business.MeetingBO) ArrayList(java.util.ArrayList) MeetingFactory(org.mifos.application.meeting.business.MeetingFactory) LocalDate(org.joda.time.LocalDate) LoanScheduleConfiguration(org.mifos.clientportfolio.newloan.domain.LoanScheduleConfiguration) Money(org.mifos.framework.util.helpers.Money) CustomerBO(org.mifos.customers.business.CustomerBO) AccountFeesEntity(org.mifos.accounts.business.AccountFeesEntity) CreateLoanSchedule(org.mifos.clientportfolio.loan.service.CreateLoanSchedule) LoanSchedule(org.mifos.clientportfolio.newloan.domain.LoanSchedule) UserContext(org.mifos.security.util.UserContext) LoanScheduleDto(org.mifos.dto.screen.LoanScheduleDto) MifosUser(org.mifos.security.MifosUser) LoanCreationInstallmentDto(org.mifos.dto.domain.LoanCreationInstallmentDto) LoanProductOverridenDetail(org.mifos.clientportfolio.newloan.domain.LoanProductOverridenDetail) MeetingDto(org.mifos.dto.domain.MeetingDto) LoanOfferingBO(org.mifos.accounts.productdefinition.business.LoanOfferingBO)

Aggregations

LocalDate (org.joda.time.LocalDate)4 CreateLoanSchedule (org.mifos.clientportfolio.loan.service.CreateLoanSchedule)4 ArrayList (java.util.ArrayList)3 AccountFeesEntity (org.mifos.accounts.business.AccountFeesEntity)3 AccountPenaltiesEntity (org.mifos.accounts.business.AccountPenaltiesEntity)3 LoanOfferingBO (org.mifos.accounts.productdefinition.business.LoanOfferingBO)3 MeetingBO (org.mifos.application.meeting.business.MeetingBO)3 LoanProductOverridenDetail (org.mifos.clientportfolio.newloan.domain.LoanProductOverridenDetail)3 LoanSchedule (org.mifos.clientportfolio.newloan.domain.LoanSchedule)3 LoanScheduleConfiguration (org.mifos.clientportfolio.newloan.domain.LoanScheduleConfiguration)3 CustomerBO (org.mifos.customers.business.CustomerBO)3 LoanScheduleDto (org.mifos.dto.screen.LoanScheduleDto)3 Money (org.mifos.framework.util.helpers.Money)3 MifosUser (org.mifos.security.MifosUser)3 UserContext (org.mifos.security.util.UserContext)3 LoanScheduleEntity (org.mifos.accounts.loan.business.LoanScheduleEntity)2 OriginalLoanScheduleEntity (org.mifos.accounts.loan.business.OriginalLoanScheduleEntity)2 RecurringSchedule (org.mifos.clientportfolio.loan.service.RecurringSchedule)2 LoanCreationInstallmentDto (org.mifos.dto.domain.LoanCreationInstallmentDto)2 AccountException (org.mifos.accounts.exceptions.AccountException)1