Search in sources :

Example 1 with GroupLoanScheduleDto

use of org.mifos.dto.screen.GroupLoanScheduleDto in project head by mifos.

the class GroupLoanAccountController method retrieveLoanSchedule.

public LoanScheduleDto retrieveLoanSchedule(int customerId, int productId, LoanAccountFormBean formBean, LoanScheduleFormBean loanScheduleFormBean, boolean resetRedoLoanAccountDetails) {
    LoanApplicationStateDto applicationState = loanAccountServiceFacade.retrieveLoanApplicationState();
    Integer accountState = applicationState.getConfiguredApplicationId();
    LocalDate disbursementDate = LoanCreationHelper.translateDisbursementDateToLocalDate(formBean);
    RecurringSchedule recurringSchedule = LoanCreationHelper.determineRecurringSchedule(formBean);
    List<CreateAccountFeeDto> accountFees = LoanCreationHelper.translateToAccountFeeDtos(formBean);
    List<CreateAccountFeeDto> additionalAccountFees = LoanCreationHelper.translateToAdditionalAccountFeeDtos(formBean);
    List<CreateAccountPenaltyDto> accountPenalties = LoanCreationHelper.translateToAccountPenaltyDtos(formBean);
    accountFees.addAll(additionalAccountFees);
    Map<String, List<CreateAccountFeeDto>> memberAccountsFees = LoanCreationHelper.translateToMemberAccountsFeeDtos(formBean);
    Map<String, List<CreateAccountFeeDto>> memberAccountsAdditionalFees = LoanCreationHelper.translateToMemberAccountsAdditionalFeeDtos(formBean);
    for (Map.Entry<String, List<CreateAccountFeeDto>> memberAccountEntry : memberAccountsAdditionalFees.entrySet()) {
        memberAccountsFees.get(memberAccountEntry.getKey()).addAll(memberAccountsAdditionalFees.get(memberAccountEntry.getKey()));
    }
    BigDecimal loanAmount = BigDecimal.valueOf(formBean.getAmount().doubleValue());
    BigDecimal minAllowedLoanAmount = BigDecimal.valueOf(formBean.getMinAllowedAmount().doubleValue());
    BigDecimal maxAllowedLoanAmount = BigDecimal.valueOf(formBean.getMaxAllowedAmount().doubleValue());
    CreateLoanAccount loanAccountDetails = prepareLoanAccount(accountState, formBean, disbursementDate, recurringSchedule, accountFees, accountPenalties, loanAmount, minAllowedLoanAmount, maxAllowedLoanAmount);
    List<CreateLoanAccount> memberAccounts = createGroupLoanMembers(accountState, formBean, disbursementDate, recurringSchedule, memberAccountsFees, accountPenalties, loanAmount, minAllowedLoanAmount, maxAllowedLoanAmount);
    BigDecimal totalLoanAmount = BigDecimal.valueOf(formBean.getAmount().doubleValue());
    CreateGroupLoanAccount createGroupLoanAccount = new CreateGroupLoanAccount(memberAccounts, totalLoanAmount, loanAccountDetails);
    GroupLoanScheduleDto groupLoanScheduleDto = groupLoanAccountServiceFacade.getGroupLoanScheduleDto(createGroupLoanAccount, memberAccounts);
    List<String> globalMemberIds = loanControllerHelper.getSelectedMemberGlobalIds(formBean);
    List<Integer> memberIds = loanControllerHelper.getSelectedMemberIdsFromFormBean(formBean);
    List<BigDecimal> memberAmounts = loanControllerHelper.getSelectedMemberAmounts(formBean);
    loanScheduleFormBean.setVariableInstallments(groupLoanScheduleDto.getInstallments());
    for (int i = 0; i < memberIds.size(); ++i) {
        Integer memberId = memberIds.get(i);
        String memberIdString = memberId.toString();
        LoanScheduleFormBean memberScheduleBean = loanScheduleFormBean.getMemberSchedules().get(memberIdString);
        if (memberScheduleBean == null) {
            memberScheduleBean = new LoanScheduleFormBean();
        }
        memberScheduleBean.setGlobalCustomerId(globalMemberIds.get(i));
        loanScheduleFormBean.getMemberSchedules().put(memberIdString, memberScheduleBean);
        LoanScheduleDto memberLoanSchedule = groupLoanScheduleDto.getMemberSchedules().get(memberId);
        loanControllerHelper.populateFormBeanFromDto(memberId, productId, formBean, memberScheduleBean, disbursementDate, memberLoanSchedule, resetRedoLoanAccountDetails);
        memberScheduleBean.setLoanPrincipal(memberAmounts.get(i));
        loanScheduleFormBean.setInstallments(memberScheduleBean.getInstallments());
    }
    loanScheduleFormBean.setActualPaymentAmounts(new ArrayList<Number>());
    return groupLoanScheduleDto;
}
Also used : LoanApplicationStateDto(org.mifos.clientportfolio.newloan.applicationservice.LoanApplicationStateDto) LocalDate(org.joda.time.LocalDate) CreateLoanAccount(org.mifos.clientportfolio.newloan.applicationservice.CreateLoanAccount) CreateAccountPenaltyDto(org.mifos.dto.domain.CreateAccountPenaltyDto) CreateGroupLoanAccount(org.mifos.clientportfolio.newloan.applicationservice.CreateGroupLoanAccount) ArrayList(java.util.ArrayList) List(java.util.List) CreateAccountFeeDto(org.mifos.dto.domain.CreateAccountFeeDto) LoanScheduleDto(org.mifos.dto.screen.LoanScheduleDto) GroupLoanScheduleDto(org.mifos.dto.screen.GroupLoanScheduleDto) BigDecimal(java.math.BigDecimal) RecurringSchedule(org.mifos.clientportfolio.loan.service.RecurringSchedule) HashMap(java.util.HashMap) Map(java.util.Map) GroupLoanScheduleDto(org.mifos.dto.screen.GroupLoanScheduleDto)

Example 2 with GroupLoanScheduleDto

use of org.mifos.dto.screen.GroupLoanScheduleDto in project head by mifos.

the class GroupLoanAccountServiceFacadeWebTier method getGroupLoanScheduleDto.

@Override
public GroupLoanScheduleDto getGroupLoanScheduleDto(CreateGroupLoanAccount loanAccountInfo, List<CreateLoanAccount> memberDetails) {
    DateTime creationDate = new DateTime();
    // 0. verify member details for GLIM group accounts
    for (CreateLoanAccount groupMemberAccount : memberDetails) {
        ClientBO member = this.customerDao.findClientById(groupMemberAccount.getCustomerId());
        if (creationDate.isBefore(new DateTime(member.getCreatedDate()))) {
            throw new BusinessRuleException("errors.cannotCreateLoan.because.clientsAreCreatedInFuture");
        }
    }
    // 1. assemble loan details
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    OfficeBO userOffice = this.officeDao.findOfficeById(user.getBranchId());
    PersonnelBO createdBy = this.personnelDao.findPersonnelById(userContext.getId());
    CustomerBO customer = this.customerDao.findCustomerById(loanAccountInfo.getGroupLoanAccountDetails().getCustomerId());
    if (customer.isGroup()) {
        customer = this.customerDao.findGroupBySystemId(customer.getGlobalCustNum());
    }
    // assemble
    LoanAccountDetail loanAccountDetail = assembleLoanAccountDetail(loanAccountInfo);
    List<AccountFeesEntity> accountFeeEntities = assembleAccountFees(loanAccountInfo.getGroupLoanAccountDetails().getAccountFees());
    List<AccountPenaltiesEntity> accountPenaltyEntities = assembleAccountPenalties(loanAccountInfo.getGroupLoanAccountDetails().getAccountPenalties());
    LoanProductOverridenDetail overridenDetail = new LoanProductOverridenDetail(loanAccountDetail.getLoanAmount(), loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), loanAccountInfo.getGroupLoanAccountDetails().getInterestRate(), loanAccountInfo.getGroupLoanAccountDetails().getNumberOfInstallments(), loanAccountInfo.getGroupLoanAccountDetails().getGraceDuration(), accountFeeEntities, accountPenaltyEntities);
    Integer interestDays = Integer.valueOf(AccountingRules.getNumberOfInterestDays().intValue());
    boolean loanScheduleIndependentOfCustomerMeetingEnabled = loanAccountInfo.getGroupLoanAccountDetails().isRepaymentScheduleIndependentOfCustomerMeeting();
    LoanScheduleConfiguration configuration = new LoanScheduleConfiguration(loanScheduleIndependentOfCustomerMeetingEnabled, interestDays);
    MeetingBO repaymentDayMeeting = null;
    if (loanScheduleIndependentOfCustomerMeetingEnabled) {
        repaymentDayMeeting = this.createNewMeetingForRepaymentDay(loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), loanAccountInfo.getGroupLoanAccountDetails(), loanAccountDetail.getCustomer());
    } else {
        MeetingDto customerMeetingDto = customer.getCustomerMeetingValue().toDto();
        repaymentDayMeeting = new MeetingFactory().create(customerMeetingDto);
        Short recurAfter = loanAccountDetail.getLoanProduct().getLoanOfferingMeeting().getMeeting().getRecurAfter();
        repaymentDayMeeting.getMeetingDetails().setRecurAfter(recurAfter);
    }
    List<DateTime> loanScheduleDates = new ArrayList<DateTime>();
    LoanSchedule loanSchedule = assembleLoanSchedule(loanAccountDetail.getCustomer(), loanAccountDetail.getLoanProduct(), overridenDetail, configuration, repaymentDayMeeting, userOffice, loanScheduleDates, loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), new ArrayList<Number>());
    // 2. create loan
    InstallmentRange installmentRange = new MaxMinNoOfInstall(loanAccountInfo.getGroupLoanAccountDetails().getMinAllowedNumberOfInstallments().shortValue(), loanAccountInfo.getGroupLoanAccountDetails().getMaxAllowedNumberOfInstallments().shortValue(), null);
    AmountRange amountRange = new MaxMinLoanAmount(loanAccountInfo.getGroupLoanAccountDetails().getMaxAllowedLoanAmount().doubleValue(), loanAccountInfo.getGroupLoanAccountDetails().getMinAllowedLoanAmount().doubleValue(), null);
    creationDate = loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate().toDateMidnight().toDateTime();
    CreationDetail creationDetail = new CreationDetail(creationDate, Integer.valueOf(user.getUserId()));
    LoanBO loan = LoanBO.openGroupLoanAccount(loanAccountDetail.getLoanProduct(), loanAccountDetail.getCustomer(), repaymentDayMeeting, loanSchedule, loanAccountDetail.getAccountState(), loanAccountDetail.getFund(), overridenDetail, configuration, installmentRange, amountRange, creationDetail, createdBy);
    loan.setBusinessActivityId(loanAccountInfo.getGroupLoanAccountDetails().getLoanPurposeId());
    loan.setExternalId(loanAccountInfo.getGroupLoanAccountDetails().getExternalId());
    loan.setCollateralNote(loanAccountInfo.getGroupLoanAccountDetails().getCollateralNotes());
    loan.setCollateralTypeId(loanAccountInfo.getGroupLoanAccountDetails().getCollateralTypeId());
    loan.markAsCreatedWithBackdatedPayments();
    //set up predefined loan account for importing loans
    if (loanAccountInfo.getGroupLoanAccountDetails().getPredefinedAccountNumber() != null) {
        loan.setGlobalAccountNum(loanAccountInfo.getGroupLoanAccountDetails().getPredefinedAccountNumber());
    }
    // for GLIM loans only
    List<CreateLoanAccount> individualMembersOfGroupLoan = new ArrayList<CreateLoanAccount>();
    List<BigDecimal> radio = new ArrayList<BigDecimal>(loan.getNoOfInstallments());
    List<LoanProductOverridenDetail> memberOverridenDetails = new ArrayList<LoanProductOverridenDetail>();
    List<LoanSchedule> memberLoanSchedules = new ArrayList<LoanSchedule>();
    List<ClientBO> clients = new ArrayList<ClientBO>();
    for (CreateLoanAccount groupMemberAccount : memberDetails) {
        ClientBO member = this.customerDao.findClientById(groupMemberAccount.getCustomerId());
        Money loanAmount = new Money(loanAccountDetail.getLoanProduct().getCurrency(), groupMemberAccount.getLoanAmount());
        List<CreateAccountPenaltyDto> defaultAccountPenalties = new ArrayList<CreateAccountPenaltyDto>();
        radio.add(loanAmount.divide(loan.getLoanAmount()));
        int memberCount = memberDetails.size();
        for (CreateAccountPenaltyDto createAccountPenaltyDto : loanAccountInfo.getGroupLoanAccountDetails().getAccountPenalties()) {
            Integer penaltyId = createAccountPenaltyDto.getPenaltyId();
            String amount = createAccountPenaltyDto.getAmount();
            PenaltyBO penaltyBO = this.penaltyDao.findPenaltyById(penaltyId.shortValue());
            if (penaltyBO instanceof AmountPenaltyBO) {
                amount = String.valueOf(Double.valueOf(createAccountPenaltyDto.getAmount()) / memberCount);
            }
            defaultAccountPenalties.add(new CreateAccountPenaltyDto(penaltyId, amount));
        }
        List<AccountFeesEntity> feeEntities = assembleAccountFees(groupMemberAccount.getAccountFees());
        List<AccountPenaltiesEntity> penaltyEntities = assembleAccountPenalties(defaultAccountPenalties);
        LoanProductOverridenDetail memberOverridenDetail = new LoanProductOverridenDetail(loanAmount, feeEntities, overridenDetail, penaltyEntities);
        LoanSchedule memberSchedule = assembleLoanSchedule(member, loanAccountDetail.getLoanProduct(), memberOverridenDetail, configuration, repaymentDayMeeting, userOffice, new ArrayList<DateTime>(), loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), new ArrayList<Number>());
        memberOverridenDetails.add(memberOverridenDetail);
        memberLoanSchedules.add(memberSchedule);
        clients.add(member);
        individualMembersOfGroupLoan.add(groupMemberAccount);
    }
    //for original schedule persisting
    List<LoanBO> memberLoans = new ArrayList<LoanBO>();
    int index = 0;
    for (CreateLoanAccount groupMemberAccount : individualMembersOfGroupLoan) {
        LoanBO memberLoan = LoanBO.openGroupLoanForAccount(loan, loanAccountDetail.getLoanProduct(), clients.get(index), repaymentDayMeeting, memberLoanSchedules.get(index), memberOverridenDetails.get(index), configuration, installmentRange, amountRange, creationDetail, createdBy, Boolean.TRUE);
        if (groupMemberAccount.getLoanPurposeId() > 0) {
            memberLoan.setBusinessActivityId(groupMemberAccount.getLoanPurposeId());
        }
        memberLoan.markAsCreatedWithBackdatedPayments();
        memberLoans.add(memberLoan);
        index++;
    }
    // update loan schedule for Group Loan Account
    loanSchedule = this.loanScheduleService.generateGroupLoanSchedule(loanAccountDetail.getLoanProduct(), repaymentDayMeeting, loanSchedule, memberLoanSchedules, loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), overridenDetail, configuration, userOffice.getOfficeId(), loanAccountDetail.getCustomer(), accountFeeEntities);
    fixMemberAndParentInstallmentDetails(loan, memberLoans);
    // translate schedules to dto form
    Map<Integer, LoanScheduleEntity> parentScheduleEntities = loan.getLoanScheduleEntityMap();
    List<LoanCreationInstallmentDto> parentInstallments = new ArrayList<LoanCreationInstallmentDto>();
    Map<Integer, List<LoanCreationInstallmentDto>> memberInstallments = new HashMap<Integer, List<LoanCreationInstallmentDto>>();
    for (CreateLoanAccount member : memberDetails) {
        memberInstallments.put(member.getCustomerId(), new ArrayList<LoanCreationInstallmentDto>());
    }
    Short digitsAfterDecimal = AccountingRules.getDigitsAfterDecimal();
    for (Integer installmentId : parentScheduleEntities.keySet()) {
        LoanScheduleEntity loanScheduleEntity = parentScheduleEntities.get(installmentId);
        parentInstallments.add(loanScheduleEntity.toLoanCreationInstallmentDto(digitsAfterDecimal));
        for (LoanBO memberLoan : memberLoans) {
            LoanScheduleEntity memberLoanScheduleEntity = memberLoan.getLoanScheduleEntityMap().get(installmentId);
            memberInstallments.get(memberLoan.getCustomer().getCustomerId()).add(memberLoanScheduleEntity.toLoanCreationInstallmentDto(digitsAfterDecimal));
        }
    }
    GroupLoanScheduleDto groupScheduleDto = new GroupLoanScheduleDto(customer.getDisplayName(), Double.valueOf(loan.getLoanAmount().getAmountDoubleValue()), loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), loan.getGraceType().getValue().intValue(), parentInstallments);
    groupScheduleDto.setMemberSchedules(new HashMap<Integer, LoanScheduleDto>());
    for (LoanBO memberLoan : memberLoans) {
        Integer id = memberLoan.getCustomer().getCustomerId();
        LoanScheduleDto memberScheduleDto = new LoanScheduleDto(memberLoan.getCustomer().getDisplayName(), Double.valueOf(memberLoan.getLoanAmount().getAmountDoubleValue()), loanAccountInfo.getGroupLoanAccountDetails().getDisbursementDate(), loan.getGraceType().getValue().intValue(), memberInstallments.get(id));
        groupScheduleDto.getMemberSchedules().put(id, memberScheduleDto);
    }
    return groupScheduleDto;
}
Also used : HashMap(java.util.HashMap) MeetingBO(org.mifos.application.meeting.business.MeetingBO) ClientBO(org.mifos.customers.client.business.ClientBO) ArrayList(java.util.ArrayList) MeetingFactory(org.mifos.application.meeting.business.MeetingFactory) DateTime(org.joda.time.DateTime) CreateLoanAccount(org.mifos.clientportfolio.newloan.applicationservice.CreateLoanAccount) OfficeBO(org.mifos.customers.office.business.OfficeBO) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) ArrayList(java.util.ArrayList) List(java.util.List) AccountFeesEntity(org.mifos.accounts.business.AccountFeesEntity) PenaltyBO(org.mifos.accounts.penalties.business.PenaltyBO) AmountPenaltyBO(org.mifos.accounts.penalties.business.AmountPenaltyBO) LoanSchedule(org.mifos.clientportfolio.newloan.domain.LoanSchedule) LoanBO(org.mifos.accounts.loan.business.LoanBO) LoanScheduleDto(org.mifos.dto.screen.LoanScheduleDto) GroupLoanScheduleDto(org.mifos.dto.screen.GroupLoanScheduleDto) LoanAccountDetail(org.mifos.clientportfolio.newloan.domain.LoanAccountDetail) MaxMinLoanAmount(org.mifos.accounts.loan.business.MaxMinLoanAmount) LoanProductOverridenDetail(org.mifos.clientportfolio.newloan.domain.LoanProductOverridenDetail) LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) LoanScheduleConfiguration(org.mifos.clientportfolio.newloan.domain.LoanScheduleConfiguration) Money(org.mifos.framework.util.helpers.Money) BusinessRuleException(org.mifos.service.BusinessRuleException) CreateAccountPenaltyDto(org.mifos.dto.domain.CreateAccountPenaltyDto) InstallmentRange(org.mifos.accounts.productdefinition.business.InstallmentRange) AmountRange(org.mifos.accounts.productdefinition.business.AmountRange) CustomerBO(org.mifos.customers.business.CustomerBO) UserContext(org.mifos.security.util.UserContext) MifosUser(org.mifos.security.MifosUser) CreationDetail(org.mifos.clientportfolio.newloan.domain.CreationDetail) LoanCreationInstallmentDto(org.mifos.dto.domain.LoanCreationInstallmentDto) BigDecimal(java.math.BigDecimal) MeetingDto(org.mifos.dto.domain.MeetingDto) MaxMinNoOfInstall(org.mifos.accounts.loan.business.MaxMinNoOfInstall) AmountPenaltyBO(org.mifos.accounts.penalties.business.AmountPenaltyBO) GroupLoanScheduleDto(org.mifos.dto.screen.GroupLoanScheduleDto)

Aggregations

BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CreateLoanAccount (org.mifos.clientportfolio.newloan.applicationservice.CreateLoanAccount)2 CreateAccountPenaltyDto (org.mifos.dto.domain.CreateAccountPenaltyDto)2 GroupLoanScheduleDto (org.mifos.dto.screen.GroupLoanScheduleDto)2 LoanScheduleDto (org.mifos.dto.screen.LoanScheduleDto)2 Map (java.util.Map)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 AccountFeesEntity (org.mifos.accounts.business.AccountFeesEntity)1 AccountPenaltiesEntity (org.mifos.accounts.business.AccountPenaltiesEntity)1 LoanBO (org.mifos.accounts.loan.business.LoanBO)1 LoanScheduleEntity (org.mifos.accounts.loan.business.LoanScheduleEntity)1 MaxMinLoanAmount (org.mifos.accounts.loan.business.MaxMinLoanAmount)1 MaxMinNoOfInstall (org.mifos.accounts.loan.business.MaxMinNoOfInstall)1 AmountPenaltyBO (org.mifos.accounts.penalties.business.AmountPenaltyBO)1 PenaltyBO (org.mifos.accounts.penalties.business.PenaltyBO)1 AmountRange (org.mifos.accounts.productdefinition.business.AmountRange)1