Search in sources :

Example 6 with UserContext

use of org.mifos.security.util.UserContext 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 7 with UserContext

use of org.mifos.security.util.UserContext in project head by mifos.

the class LoanAccountServiceFacadeWebTier method retrieveAllActiveBranchesAndLoanOfficerDetails.

@Override
public ChangeAccountStatusDto retrieveAllActiveBranchesAndLoanOfficerDetails() {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    List<PersonnelDto> loanOfficers = new ArrayList<PersonnelDto>();
    List<OfficeDetailsDto> activeBranches = this.officeDao.findActiveBranches(userContext.getBranchId());
    if (onlyOneActiveBranchExists(activeBranches)) {
        OfficeDetailsDto singleOffice = activeBranches.get(0);
        CenterCreation officeDetails = new CenterCreation(singleOffice.getOfficeId(), userContext.getId(), userContext.getLevelId(), userContext.getPreferredLocale());
        loanOfficers = this.personnelDao.findActiveLoanOfficersForOffice(officeDetails);
    }
    boolean loanPendingApprovalStateEnabled = ProcessFlowRules.isLoanPendingApprovalStateEnabled();
    Short accountState = AccountState.LOAN_PARTIAL_APPLICATION.getValue();
    if (loanPendingApprovalStateEnabled) {
        accountState = AccountState.LOAN_PENDING_APPROVAL.getValue();
    }
    boolean centerHierarchyExists = ClientRules.getCenterHierarchyExists();
    return new ChangeAccountStatusDto(activeBranches, loanOfficers, loanPendingApprovalStateEnabled, accountState, centerHierarchyExists);
}
Also used : ChangeAccountStatusDto(org.mifos.dto.screen.ChangeAccountStatusDto) UserContext(org.mifos.security.util.UserContext) CenterCreation(org.mifos.dto.domain.CenterCreation) ArrayList(java.util.ArrayList) PersonnelDto(org.mifos.dto.domain.PersonnelDto) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) OfficeDetailsDto(org.mifos.dto.domain.OfficeDetailsDto)

Example 8 with UserContext

use of org.mifos.security.util.UserContext in project head by mifos.

the class LoanAccountServiceFacadeWebTier method retrieveLoanInformation.

@Override
public LoanInformationDto retrieveLoanInformation(String globalAccountNum) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    LoanBO loan = this.loanDao.findByGlobalAccountNum(globalAccountNum);
    if (loan.isDecliningBalanceInterestRecalculation()) {
        loanBusinessService.computeExtraInterest(loan, DateUtils.getCurrentDateWithoutTimeStamp());
    }
    try {
        personnelDao.checkAccessPermission(userContext, loan.getOfficeId(), loan.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException("Access denied!", e);
    }
    String fundName = null;
    if (loan.getFund() != null) {
        fundName = loan.getFund().getFundName();
    }
    //        boolean activeSurveys = surveysPersistence.isActiveSurveysForSurveyType(SurveyType.LOAN);
    boolean activeSurveys = false;
    List<SurveyDto> accountSurveys = loanDao.getAccountSurveyDto(loan.getAccountId());
    LoanSummaryDto loanSummary = new LoanSummaryDto(loan.getLoanSummary().getOriginalPrincipal().toString(), loan.getLoanSummary().getPrincipalPaid().toString(), loan.getLoanSummary().getPrincipalDue().toString(), loan.getLoanSummary().getOriginalInterest().toString(), loan.getLoanSummary().getInterestPaid().toString(), loan.getLoanSummary().getInterestDue().toString(), loan.getLoanSummary().getOriginalFees().toString(), loan.getLoanSummary().getFeesPaid().toString(), loan.getLoanSummary().getFeesDue().toString(), loan.getLoanSummary().getOriginalPenalty().toString(), loan.getLoanSummary().getPenaltyPaid().toString(), loan.getLoanSummary().getPenaltyDue().toString(), loan.getLoanSummary().getTotalLoanAmnt().toString(), loan.getLoanSummary().getTotalAmntPaid().toString(), loan.getLoanSummary().getTotalAmntDue().toString());
    LoanPerformanceHistoryEntity performanceHistory = loan.getPerformanceHistory();
    LoanPerformanceHistoryDto loanPerformanceHistory = new LoanPerformanceHistoryDto(performanceHistory.getNoOfPayments(), performanceHistory.getTotalNoOfMissedPayments(), performanceHistory.getDaysInArrears(), performanceHistory.getLoanMaturityDate());
    Set<AccountFeesDto> accountFeesDtos = new HashSet<AccountFeesDto>();
    if (!loan.getAccountFees().isEmpty()) {
        for (AccountFeesEntity accountFeesEntity : loan.getAccountFees()) {
            AccountFeesDto accountFeesDto = new AccountFeesDto(accountFeesEntity.getFees().getFeeFrequency().getFeeFrequencyType().getId(), (accountFeesEntity.getFees().getFeeFrequency().getFeePayment() != null ? accountFeesEntity.getFees().getFeeFrequency().getFeePayment().getId() : null), accountFeesEntity.getFeeStatus(), accountFeesEntity.getFees().getFeeName(), accountFeesEntity.getAccountFeeAmount().toString(), getMeetingRecurrence(accountFeesEntity.getFees().getFeeFrequency().getFeeMeetingFrequency(), userContext), accountFeesEntity.getFees().getFeeId());
            accountFeesDtos.add(accountFeesDto);
        }
    }
    Set<AccountPenaltiesDto> accountPenaltiesDtos = new HashSet<AccountPenaltiesDto>();
    if (!loan.getAccountPenalties().isEmpty()) {
        for (AccountPenaltiesEntity accountPenaltiesEntity : loan.getAccountPenalties()) {
            accountPenaltiesDtos.add(new AccountPenaltiesDto(accountPenaltiesEntity.getPenalty().getPenaltyFrequency().getId(), accountPenaltiesEntity.getPenaltyStatus(), accountPenaltiesEntity.getPenalty().getPenaltyName(), accountPenaltiesEntity.getAccountPenaltyAmount().toString(), accountPenaltiesEntity.getPenalty().getPenaltyFrequency().getName(), accountPenaltiesEntity.getPenalty().getPenaltyId()));
        }
    }
    Set<String> accountFlagNames = getAccountStateFlagEntityNames(loan.getAccountFlags());
    Short accountStateId = loan.getAccountState().getId();
    String accountStateName = getAccountStateName(accountStateId);
    boolean disbursed = AccountState.isDisbursed(accountStateId);
    String gracePeriodTypeName = getGracePeriodTypeName(loan.getGracePeriodType().getId());
    Short interestType = loan.getInterestType().getId();
    String interestTypeName = getInterestTypeName(interestType);
    List<CustomerNoteDto> recentNoteDtos = new ArrayList<CustomerNoteDto>();
    List<AccountNotesEntity> recentNotes = loan.getRecentAccountNotes();
    for (AccountNotesEntity accountNotesEntity : recentNotes) {
        recentNoteDtos.add(new CustomerNoteDto(accountNotesEntity.getCommentDate(), accountNotesEntity.getComment(), accountNotesEntity.getPersonnelName()));
    }
    CustomValueDto customValueDto = legacyMasterDao.getLookUpEntity(MasterConstants.COLLATERAL_TYPES);
    List<CustomValueListElementDto> collateralTypes = customValueDto.getCustomValueListElements();
    String collateralTypeName = null;
    for (CustomValueListElementDto collateralType : collateralTypes) {
        if (collateralType.getId() == loan.getCollateralTypeId()) {
            collateralTypeName = collateralType.getName();
            break;
        }
    }
    return new LoanInformationDto(loan.getLoanOffering().getPrdOfferingName(), globalAccountNum, accountStateId, accountStateName, disbursed, accountFlagNames, loan.getDisbursementDate(), loan.isRedone(), loan.getBusinessActivityId(), loan.getAccountId(), gracePeriodTypeName, interestType, interestTypeName, loan.getCustomer().getCustomerId(), loan.getAccountType().getAccountTypeId(), loan.getOffice().getOfficeId(), loan.getPersonnel().getPersonnelId(), loan.getNextMeetingDate(), loan.getTotalAmountDue().toString(), loan.getTotalAmountInArrears().toString(), loanSummary, loan.getLoanActivityDetails().isEmpty() ? false : true, loan.getInterestRate(), loan.isInterestDeductedAtDisbursement(), loan.getLoanMeeting().getMeetingDetails().getRecurAfter(), loan.getLoanMeeting().getMeetingDetails().getRecurrenceType().getRecurrenceId(), loan.getLoanOffering().isPrinDueLastInst(), loan.getNoOfInstallments(), loan.getMaxMinNoOfInstall().getMinNoOfInstall(), loan.getMaxMinNoOfInstall().getMaxNoOfInstall(), loan.getGracePeriodDuration(), fundName, loan.getCollateralTypeId(), collateralTypeName, loan.getCollateralNote(), loan.getExternalId(), accountFeesDtos, loan.getCreatedDate(), loanPerformanceHistory, loan.getCustomer().isGroup(), getRecentActivityView(globalAccountNum), activeSurveys, accountSurveys, loan.getCustomer().getDisplayName(), loan.getCustomer().getGlobalCustNum(), loan.getOffice().getOfficeName(), recentNoteDtos, accountPenaltiesDtos, AccountingRules.isGroupLoanWithMembers());
}
Also used : AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) SurveyDto(org.mifos.dto.domain.SurveyDto) CustomValueListElementDto(org.mifos.application.master.business.CustomValueListElementDto) ArrayList(java.util.ArrayList) AccountNotesEntity(org.mifos.accounts.business.AccountNotesEntity) AccountFeesDto(org.mifos.dto.screen.AccountFeesDto) CustomerNoteDto(org.mifos.dto.domain.CustomerNoteDto) LoanPerformanceHistoryDto(org.mifos.dto.screen.LoanPerformanceHistoryDto) AccountFeesEntity(org.mifos.accounts.business.AccountFeesEntity) LoanPerformanceHistoryEntity(org.mifos.accounts.loan.business.LoanPerformanceHistoryEntity) HashSet(java.util.HashSet) UserContext(org.mifos.security.util.UserContext) LoanBO(org.mifos.accounts.loan.business.LoanBO) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) AccountPenaltiesDto(org.mifos.dto.screen.AccountPenaltiesDto) AccountException(org.mifos.accounts.exceptions.AccountException) LoanSummaryDto(org.mifos.dto.screen.LoanSummaryDto) LoanInformationDto(org.mifos.dto.screen.LoanInformationDto) CustomValueDto(org.mifos.application.master.business.CustomValueDto) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 9 with UserContext

use of org.mifos.security.util.UserContext in project head by mifos.

the class LoanAccountServiceFacadeWebTier method retrieveLoanRepaymentDetails.

@Override
public RepayLoanDto retrieveLoanRepaymentDetails(String globalAccountNumber) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    LoanBO loan = this.loanDao.findByGlobalAccountNum(globalAccountNumber);
    try {
        personnelDao.checkAccessPermission(userContext, loan.getOfficeId(), loan.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException(e.getMessage(), e);
    }
    Money repaymentAmount;
    Money waiverAmount;
    if (loan.isDecliningBalanceInterestRecalculation()) {
        RepaymentResultsHolder repaymentResultsHolder = scheduleCalculatorAdaptor.computeRepaymentAmount(loan, DateUtils.getCurrentDateWithoutTimeStamp());
        repaymentAmount = new Money(loan.getCurrency(), repaymentResultsHolder.getTotalRepaymentAmount());
        waiverAmount = new Money(loan.getCurrency(), repaymentResultsHolder.getWaiverAmount());
    } else {
        repaymentAmount = loan.getEarlyRepayAmount();
        waiverAmount = loan.waiverAmount();
    }
    Money waivedRepaymentAmount = repaymentAmount.subtract(waiverAmount);
    List<SavingsDetailDto> savingsInUse = clientServiceFacade.retrieveSavingsInUseForClient(loan.getCustomer().getCustomerId());
    List<SavingsDetailDto> accountsForTransfer = new ArrayList<SavingsDetailDto>();
    if (savingsInUse != null) {
        for (SavingsDetailDto savingsAccount : savingsInUse) {
            if (savingsAccount.getAccountStateId().equals(AccountState.SAVINGS_ACTIVE.getValue())) {
                accountsForTransfer.add(savingsAccount);
            }
        }
    }
    return new RepayLoanDto(repaymentAmount.toString(), waivedRepaymentAmount.toString(), loan.isInterestWaived(), accountsForTransfer);
}
Also used : Money(org.mifos.framework.util.helpers.Money) SavingsDetailDto(org.mifos.dto.domain.SavingsDetailDto) AccountException(org.mifos.accounts.exceptions.AccountException) UserContext(org.mifos.security.util.UserContext) RepayLoanDto(org.mifos.dto.screen.RepayLoanDto) LoanBO(org.mifos.accounts.loan.business.LoanBO) ArrayList(java.util.ArrayList) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) RepaymentResultsHolder(org.mifos.accounts.loan.business.RepaymentResultsHolder) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 10 with UserContext

use of org.mifos.security.util.UserContext in project head by mifos.

the class LoanAccountServiceFacadeWebTier method retrieveInstallmentDetails.

@Override
public LoanInstallmentDetailsDto retrieveInstallmentDetails(Integer accountId) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    LoanBO loanBO = this.loanDao.findById(accountId);
    try {
        personnelDao.checkAccessPermission(userContext, loanBO.getOfficeId(), loanBO.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException(e.getMessage(), e);
    }
    InstallmentDetailsDto viewUpcomingInstallmentDetails;
    InstallmentDetailsDto viewOverDueInstallmentDetails;
    if (loanBO.isGroupLoanAccount() && null == loanBO.getParentAccount()) {
        List<AccountActionDateEntity> memberDetailsOfNextInstallment = new ArrayList<AccountActionDateEntity>();
        List<List<AccountActionDateEntity>> memberDetailsOfInstallmentsInArrears = new ArrayList<List<AccountActionDateEntity>>();
        for (LoanBO member : loanBO.getMemberAccounts()) {
            memberDetailsOfNextInstallment.add(member.getDetailsOfNextInstallment());
            memberDetailsOfInstallmentsInArrears.add(member.getDetailsOfInstallmentsInArrears());
        }
        viewUpcomingInstallmentDetails = getUpcomingInstallmentDetailsForGroupLoan(memberDetailsOfNextInstallment, loanBO.getCurrency());
        viewOverDueInstallmentDetails = getOverDueInstallmentDetailsForGroupLoan(memberDetailsOfInstallmentsInArrears, loanBO.getCurrency());
    } else {
        viewUpcomingInstallmentDetails = getUpcomingInstallmentDetails(loanBO.getDetailsOfNextInstallment(), loanBO.getCurrency());
        viewOverDueInstallmentDetails = getOverDueInstallmentDetails(loanBO.getDetailsOfInstallmentsInArrears(), loanBO.getCurrency());
    }
    Money upcomingInstallmentSubTotal = new Money(loanBO.getCurrency(), viewUpcomingInstallmentDetails.getSubTotal());
    Money overdueInstallmentSubTotal = new Money(loanBO.getCurrency(), viewOverDueInstallmentDetails.getSubTotal());
    Money totalAmountDue = upcomingInstallmentSubTotal.add(overdueInstallmentSubTotal);
    return new LoanInstallmentDetailsDto(viewUpcomingInstallmentDetails, viewOverDueInstallmentDetails, totalAmountDue.toString(), loanBO.getNextMeetingDate());
}
Also used : InstallmentDetailsDto(org.mifos.dto.domain.InstallmentDetailsDto) LoanInstallmentDetailsDto(org.mifos.dto.domain.LoanInstallmentDetailsDto) UserContext(org.mifos.security.util.UserContext) LoanBO(org.mifos.accounts.loan.business.LoanBO) ArrayList(java.util.ArrayList) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) Money(org.mifos.framework.util.helpers.Money) AccountException(org.mifos.accounts.exceptions.AccountException) ArrayList(java.util.ArrayList) List(java.util.List) LoanInstallmentDetailsDto(org.mifos.dto.domain.LoanInstallmentDetailsDto) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Aggregations

UserContext (org.mifos.security.util.UserContext)369 MifosUser (org.mifos.security.MifosUser)134 MifosRuntimeException (org.mifos.core.MifosRuntimeException)102 ArrayList (java.util.ArrayList)97 Test (org.junit.Test)81 BusinessRuleException (org.mifos.service.BusinessRuleException)75 TransactionDemarcate (org.mifos.framework.util.helpers.TransactionDemarcate)72 PersonnelBO (org.mifos.customers.personnel.business.PersonnelBO)65 AccountException (org.mifos.accounts.exceptions.AccountException)55 UserContextFactory (org.mifos.accounts.servicefacade.UserContextFactory)49 Money (org.mifos.framework.util.helpers.Money)46 LoanBO (org.mifos.accounts.loan.business.LoanBO)45 LocalDate (org.joda.time.LocalDate)44 SavingsBO (org.mifos.accounts.savings.business.SavingsBO)43 ServiceException (org.mifos.framework.exceptions.ServiceException)43 PersistenceException (org.mifos.framework.exceptions.PersistenceException)38 CustomerBO (org.mifos.customers.business.CustomerBO)37 MeetingBO (org.mifos.application.meeting.business.MeetingBO)34 Date (java.util.Date)31 CustomerException (org.mifos.customers.exceptions.CustomerException)31