Search in sources :

Example 16 with UserContextFactory

use of org.mifos.accounts.servicefacade.UserContextFactory in project head by mifos.

the class WebTierPenaltyServiceFacade method createPenalty.

@Override
public void createPenalty(PenaltyFormDto dto) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(user);
    try {
        PenaltyCategory penaltyCategory = dto.getCategoryType() != null ? PenaltyCategory.getPenaltyCategory(dto.getCategoryType()) : null;
        PenaltyFrequency penaltyFrequency = dto.getPenaltyFrequency() != null ? PenaltyFrequency.getPenaltyFrequencyType(dto.getPenaltyFrequency()) : null;
        PenaltyPeriod penaltyPeriod = dto.getPenaltyPeriod() != null ? PenaltyPeriod.getPenaltyPeriod(dto.getPenaltyPeriod()) : null;
        PenaltyFormula penaltyFormula = dto.getPenaltyFormula() != null ? PenaltyFormula.getPenaltyFormula(dto.getPenaltyFormula()) : null;
        PenaltyCategoryEntity penaltyCategoryEntity = this.penaltyDao.findPenaltyCategoryEntityByType(penaltyCategory);
        PenaltyPeriodEntity penaltyPeriodEntity = this.penaltyDao.findPenaltyPeriodEntityByType(penaltyPeriod);
        PenaltyFrequencyEntity penaltyFrequencyEntity = this.penaltyDao.findPenaltyFrequencyEntityByType(penaltyFrequency);
        GLCodeEntity glCodeEntity = this.generalLedgerDao.findGlCodeById(dto.getGlCode());
        PenaltyBO penaltyBO = null;
        String penaltyName = dto.getPenaltyName();
        Integer periodDuration = dto.getDuration();
        Double min = dto.getMin();
        Double max = dto.getMax();
        if (dto.isRatePenalty()) {
            Double rate = dto.getRate();
            PenaltyFormulaEntity formula = this.penaltyDao.findPenaltyFormulaEntityByType(penaltyFormula);
            penaltyBO = new RatePenaltyBO(userContext, penaltyName, penaltyCategoryEntity, penaltyPeriodEntity, periodDuration, min, max, penaltyFrequencyEntity, glCodeEntity, formula, rate);
        } else {
            Money amount = new Money(getCurrency(dto.getCurrencyId()), dto.getAmount());
            penaltyBO = new AmountPenaltyBO(userContext, penaltyName, penaltyCategoryEntity, penaltyPeriodEntity, periodDuration, min, max, penaltyFrequencyEntity, glCodeEntity, amount);
        }
        try {
            StaticHibernateUtil.startTransaction();
            this.penaltyDao.save(penaltyBO);
            StaticHibernateUtil.commitTransaction();
        } catch (Exception e) {
            StaticHibernateUtil.rollbackTransaction();
            throw new MifosRuntimeException(e);
        } finally {
            StaticHibernateUtil.closeSession();
        }
    } catch (ApplicationException e) {
        throw new BusinessRuleException(e.getKey(), e);
    }
}
Also used : PenaltyCategory(org.mifos.accounts.penalties.util.helpers.PenaltyCategory) RatePenaltyBO(org.mifos.accounts.penalties.business.RatePenaltyBO) PenaltyBO(org.mifos.accounts.penalties.business.PenaltyBO) AmountPenaltyBO(org.mifos.accounts.penalties.business.AmountPenaltyBO) RatePenaltyBO(org.mifos.accounts.penalties.business.RatePenaltyBO) UserContext(org.mifos.security.util.UserContext) PenaltyPeriod(org.mifos.accounts.penalties.util.helpers.PenaltyPeriod) GLCodeEntity(org.mifos.accounts.financial.business.GLCodeEntity) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) PenaltyPeriodEntity(org.mifos.accounts.penalties.business.PenaltyPeriodEntity) BusinessRuleException(org.mifos.service.BusinessRuleException) FinancialException(org.mifos.accounts.financial.exceptions.FinancialException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) Money(org.mifos.framework.util.helpers.Money) PenaltyCategoryEntity(org.mifos.accounts.penalties.business.PenaltyCategoryEntity) BusinessRuleException(org.mifos.service.BusinessRuleException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) PenaltyFrequency(org.mifos.accounts.penalties.util.helpers.PenaltyFrequency) PenaltyFrequencyEntity(org.mifos.accounts.penalties.business.PenaltyFrequencyEntity) AmountPenaltyBO(org.mifos.accounts.penalties.business.AmountPenaltyBO) PenaltyFormula(org.mifos.accounts.penalties.util.helpers.PenaltyFormula) PenaltyFormulaEntity(org.mifos.accounts.penalties.business.PenaltyFormulaEntity) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 17 with UserContextFactory

use of org.mifos.accounts.servicefacade.UserContextFactory in project head by mifos.

the class LoanAccountServiceFacadeWebTier method disburseLoan.

@Override
public void disburseLoan(AccountPaymentParametersDto loanDisbursement, Short paymentTypeId, Short paymentTypeIdForFees, Integer accountForTransferId) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    LoanBO loan = this.loanDao.findById(loanDisbursement.getAccountId());
    try {
        personnelDao.checkAccessPermission(userContext, loan.getOfficeId(), loan.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException(e.getMessage(), e);
    }
    PaymentTypeDto paymentType = null;
    try {
        for (org.mifos.dto.domain.PaymentTypeDto paymentTypeDto : accountService.getLoanDisbursementTypes()) {
            if (paymentTypeDto.getValue() == paymentTypeId) {
                paymentType = paymentTypeDto;
            }
        }
    } catch (Exception e) {
        throw new MifosRuntimeException(e.getMessage(), e);
    }
    if (paymentType == null) {
        throw new MifosRuntimeException("Expected loan PaymentTypeDto not found for id: " + paymentTypeId);
    }
    loanDisbursement.setPaymentType(paymentType);
    Date trxnDate = DateUtils.getDateWithoutTimeStamp(loanDisbursement.getPaymentDate().toDateMidnight().toDate());
    monthClosingServiceFacade.validateTransactionDate(trxnDate);
    if (!isTrxnDateValid(Integer.valueOf(loanDisbursement.getAccountId()), trxnDate)) {
        throw new BusinessRuleException("errors.invalidTxndate");
    }
    DateTime loanDisbursementDate = new DateTime(trxnDate);
    holidayServiceFacade.validateDisbursementDateForNewLoan(loan.getOfficeId(), loanDisbursementDate);
    if (loan.isFixedRepaymentSchedule()) {
        for (AccountActionDateEntity installment : loan.getAccountActionDates()) {
            if (installment.compareDate(trxnDate) <= 0) {
                throw new BusinessRuleException("errors.invalidTxndateWhenDisbursalAfterFirstRepayment");
            }
        }
    }
    List<AccountPaymentParametersDto> loanDisbursements = new ArrayList<AccountPaymentParametersDto>();
    loanDisbursements.add(loanDisbursement);
    try {
        accountService.disburseLoans(loanDisbursements, userContext.getPreferredLocale(), paymentTypeIdForFees, accountForTransferId);
    } catch (Exception e) {
        throw new MifosRuntimeException(e.getMessage(), e);
    }
}
Also used : 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) PaymentTypeDto(org.mifos.dto.domain.PaymentTypeDto) AccountPaymentParametersDto(org.mifos.dto.domain.AccountPaymentParametersDto) StatesInitializationException(org.mifos.framework.exceptions.StatesInitializationException) BusinessRuleException(org.mifos.service.BusinessRuleException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) ServiceException(org.mifos.framework.exceptions.ServiceException) HibernateSearchException(org.mifos.framework.exceptions.HibernateSearchException) PageExpiredException(org.mifos.framework.exceptions.PageExpiredException) SystemException(org.mifos.framework.exceptions.SystemException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) AccountException(org.mifos.accounts.exceptions.AccountException) PropertyNotFoundException(org.mifos.framework.exceptions.PropertyNotFoundException) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) MeetingException(org.mifos.application.meeting.exceptions.MeetingException) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) BusinessRuleException(org.mifos.service.BusinessRuleException) AccountException(org.mifos.accounts.exceptions.AccountException) PaymentTypeDto(org.mifos.dto.domain.PaymentTypeDto) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 18 with UserContextFactory

use of org.mifos.accounts.servicefacade.UserContextFactory in project head by mifos.

the class LoanAccountServiceFacadeWebTier method createMultipleLoans.

@Override
public List<String> createMultipleLoans(List<CreateLoanRequest> multipleLoans) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    OfficeBO userOffice = this.officeDao.findOfficeById(userContext.getBranchId());
    userContext.setBranchGlobalNum(userOffice.getGlobalOfficeNum());
    List<String> createdLoanAccountNumbers = new ArrayList<String>();
    for (CreateLoanRequest loanDetail : multipleLoans) {
        CustomerBO center = this.customerDao.findCustomerById(loanDetail.getCenterId());
        Short loanProductId = loanDetail.getLoanProductId();
        LoanOfferingBO loanProduct = this.loanProductDao.findById(loanProductId.intValue());
        List<QuestionGroupDetail> questionGroups = new ArrayList<QuestionGroupDetail>();
        LoanAccountCashFlow loanAccountCashFlow = null;
        BigDecimal loanAmount = BigDecimal.valueOf(Double.valueOf(loanDetail.getLoanAmount()));
        BigDecimal minAllowedLoanAmount = BigDecimal.valueOf(Double.valueOf(loanDetail.getMinLoanAmount()));
        BigDecimal maxAllowedLoanAmount = BigDecimal.valueOf(Double.valueOf(loanDetail.getMaxLoanAmount()));
        Double interestRate = loanProduct.getDefInterestRate();
        LocalDate disbursementDate = new LocalDate(center.getCustomerAccount().getNextMeetingDate());
        int numberOfInstallments = loanDetail.getDefaultNoOfInstall();
        int minAllowedNumberOfInstallments = loanDetail.getMinNoOfInstall();
        int maxAllowedNumberOfInstallments = loanDetail.getMaxNoOfInstall();
        int graceDuration = loanProduct.getGracePeriodDuration();
        boolean isRepaymentIndepOfMeetingEnabled = new ConfigurationBusinessService().isRepaymentIndepOfMeetingEnabled();
        Integer sourceOfFundId = null;
        Integer loanPurposeId = loanDetail.getLoanPurpose();
        Integer collateralTypeId = null;
        String collateralNotes = null;
        String externalId = null;
        List<CreateAccountFeeDto> accountFees = new ArrayList<CreateAccountFeeDto>();
        for (LoanOfferingFeesEntity loanOfferingFeesEntity : loanProduct.getLoanOfferingFees()) {
            FeeBO feeBO = loanOfferingFeesEntity.getFees();
            FeeDto feeDto = feeBO.toDto();
            String amountOrRate = feeDto.getAmountOrRate().toString();
            accountFees.add(new CreateAccountFeeDto(feeBO.getFeeId().intValue(), amountOrRate));
        }
        RecurringSchedule recurringSchedule = null;
        if (isRepaymentIndepOfMeetingEnabled) {
            MeetingBO meetingBO = loanProduct.getLoanOfferingMeetingValue();
            MeetingDetailsEntity meetingDetailsEntity = meetingBO.getMeetingDetails();
            Integer recursEvery = meetingDetailsEntity.getRecurAfter().intValue();
            Short dayOfMonth = meetingDetailsEntity.getDayNumber();
            RankOfDay weekOfMonth = meetingDetailsEntity.getWeekRank();
            WeekDay dayOfWeek = meetingDetailsEntity.getWeekDay();
            MeetingDetailsEntity customerMeetingDetailsEntity = center.getCustomerAccount().getMeetingForAccount().getMeetingDetails();
            Short customerRecurrenceType = customerMeetingDetailsEntity.getRecurrenceType().getRecurrenceId();
            if (meetingDetailsEntity.getRecurrenceType().getRecurrenceId().equals(customerRecurrenceType)) {
                dayOfMonth = customerMeetingDetailsEntity.getDayNumber();
                weekOfMonth = customerMeetingDetailsEntity.getWeekRank();
                dayOfWeek = customerMeetingDetailsEntity.getWeekDay();
            }
            if (meetingBO.isMonthly()) {
                if (meetingBO.isMonthlyOnDate()) {
                    recurringSchedule = new MonthlyOnDayOfMonthSchedule(recursEvery, dayOfMonth.intValue());
                } else {
                    recurringSchedule = new MonthlyOnWeekOfMonthSchedule(recursEvery, weekOfMonth.getValue().intValue(), dayOfWeek.getValue().intValue());
                }
            } else if (meetingBO.isWeekly()) {
                recurringSchedule = new WeeklySchedule(recursEvery, dayOfWeek.getValue().intValue());
            }
        }
        CreateLoanAccount loanAccountInfo = new CreateLoanAccount(loanDetail.getClientId(), loanDetail.getLoanProductId().intValue(), loanDetail.getAccountStateId().intValue(), loanAmount, minAllowedLoanAmount, maxAllowedLoanAmount, interestRate, disbursementDate, null, numberOfInstallments, minAllowedNumberOfInstallments, maxAllowedNumberOfInstallments, graceDuration, sourceOfFundId, loanPurposeId, collateralTypeId, collateralNotes, externalId, isRepaymentIndepOfMeetingEnabled, recurringSchedule, accountFees, new ArrayList<CreateAccountPenaltyDto>());
        LoanCreationResultDto result = this.createLoan(loanAccountInfo, questionGroups, loanAccountCashFlow);
        createdLoanAccountNumbers.add(result.getGlobalAccountNum());
    //            try {
    //                CustomerBO center = this.customerDao.findCustomerById(loanDetail.getCenterId());
    //
    //                Short loanProductId = loanDetail.getLoanProductId();
    //                LoanOfferingBO loanProduct = this.loanProductDao.findById(loanProductId.intValue());
    //                CustomerBO client = this.customerDao.findCustomerById(loanDetail.getClientId());
    //
    //                AccountState accountState = AccountState.fromShort(loanDetail.getAccountStateId());
    //                Money loanAmount = new Money(loanProduct.getCurrency(), loanDetail.getLoanAmount());
    //                Short defaultNumOfInstallments = loanDetail.getDefaultNoOfInstall();
    //                Date disbursementDate = center.getCustomerAccount().getNextMeetingDate();
    //                boolean interestDeductedAtDisbursement = loanProduct.isIntDedDisbursement();
    //                boolean isRepaymentIndepOfMeetingEnabled = new ConfigurationBusinessService().isRepaymentIndepOfMeetingEnabled();
    //
    //                MeetingBO newMeetingForRepaymentDay = null;
    //                if (isRepaymentIndepOfMeetingEnabled) {
    //                    MeetingBO meeting = center.getCustomerAccount().getMeetingForAccount();
    //                    LoanAccountMeetingDto loanAccountMeetingDto = null;
    //
    //                    if (meeting.isWeekly()) {
    //                        loanAccountMeetingDto = new LoanAccountMeetingDto(RecurrenceType.WEEKLY.getValue().toString(),
    //                                meeting.getMeetingDetails().getWeekDay().getValue().toString(),
    //                                meeting.getMeetingDetails().getRecurAfter().toString(),
    //                                null, null, null, null, null, null);
    //                    } else if (meeting.isMonthly()) {
    //                        if (meeting.isMonthlyOnDate()) {
    //                            loanAccountMeetingDto = new LoanAccountMeetingDto(RecurrenceType.MONTHLY.getValue().toString(),
    //                                    null, null, "1",
    //                                    meeting.getMeetingDetails().getDayNumber().toString(),
    //                                    meeting.getMeetingDetails().getRecurAfter().toString(),
    //                                    null, null, null);
    //                        } else {
    //                            loanAccountMeetingDto = new LoanAccountMeetingDto(RecurrenceType.MONTHLY.getValue().toString(),
    //                                    null, null, "2", null, null,
    //                                    meeting.getMeetingDetails().getWeekDay().getValue().toString(),
    //                                    meeting.getMeetingDetails().getRecurAfter().toString(),
    //                                    meeting.getMeetingDetails().getWeekRank().getValue().toString());
    //                        }
    //                    }
    //
    //                    newMeetingForRepaymentDay = this.createNewMeetingForRepaymentDay(new LocalDate(disbursementDate), loanAccountMeetingDto, client);
    //                }
    //
    //                checkPermissionForCreate(accountState.getValue(), userContext, userContext.getBranchId(), userContext.getId());
    //
    //                List<FeeDto> additionalFees = new ArrayList<FeeDto>();
    //                List<FeeDto> defaultFees = new ArrayList<FeeDto>();
    //
    //                new LoanProductService().getDefaultAndAdditionalFees(loanProductId, userContext, defaultFees, additionalFees);
    //
    //                FundBO fund = null;
    //                List<CustomFieldDto> customFields = new ArrayList<CustomFieldDto>();
    //                boolean isRedone = false;
    //
    //                // FIXME - keithw - tidy up constructor and use domain concepts rather than primitives, e.g. money v double, loanpurpose v integer.
    //
    //                LoanBO loan = new LoanBO(userContext, loanProduct, client, accountState, loanAmount, defaultNumOfInstallments,
    //                        disbursementDate, interestDeductedAtDisbursement, interestRate, gracePeriodDuration, fund, defaultFees,
    //                        customFields, isRedone, maxLoanAmount, minLoanAmount,
    //                        loanProduct.getMaxInterestRate(), loanProduct.getMinInterestRate(),
    //                        maxNoOfInstall, minNoOfInstall, isRepaymentIndepOfMeetingEnabled, newMeetingForRepaymentDay);
    //                loan.setBusinessActivityId(loanDetail.getLoanPurpose());
    //
    //                PersonnelBO loggedInUser = this.personnelDao.findPersonnelById(userContext.getId());
    //                AccountStateEntity newAccountState = new AccountStateEntity(accountState);
    //                AccountStatusChangeHistoryEntity statusChange = new AccountStatusChangeHistoryEntity(null, newAccountState, loggedInUser, loan);
    //
    //                this.transactionHelper.startTransaction();
    //                loan.addAccountStatusChangeHistory(statusChange);
    //                this.loanDao.save(loan);
    //                this.transactionHelper.flushSession();
    //                String globalAccountNum = loan.generateId(userContext.getBranchGlobalNum());
    //                loan.setGlobalAccountNum(globalAccountNum);
    //                this.loanDao.save(loan);
    //                this.transactionHelper.commitTransaction();
    //
    //                createdLoanAccountNumbers.add(loan.getGlobalAccountNum());
    //            } catch (ServiceException e) {
    //                this.transactionHelper.rollbackTransaction();
    //                throw new MifosRuntimeException(e);
    //            } catch (PersistenceException e) {
    //                this.transactionHelper.rollbackTransaction();
    //                throw new MifosRuntimeException(e);
    //            } catch (AccountException e) {
    //                this.transactionHelper.rollbackTransaction();
    //                throw new BusinessRuleException(e.getKey(), e);
    //            }
    }
    return createdLoanAccountNumbers;
}
Also used : MeetingBO(org.mifos.application.meeting.business.MeetingBO) ArrayList(java.util.ArrayList) WeeklySchedule(org.mifos.clientportfolio.loan.service.WeeklySchedule) LocalDate(org.joda.time.LocalDate) RankOfDay(org.mifos.application.meeting.util.helpers.RankOfDay) CreateLoanAccount(org.mifos.clientportfolio.newloan.applicationservice.CreateLoanAccount) CreateAccountPenaltyDto(org.mifos.dto.domain.CreateAccountPenaltyDto) OfficeBO(org.mifos.customers.office.business.OfficeBO) ConfigurationBusinessService(org.mifos.config.business.service.ConfigurationBusinessService) CustomerBO(org.mifos.customers.business.CustomerBO) CreateAccountFeeDto(org.mifos.dto.domain.CreateAccountFeeDto) MonthlyOnWeekOfMonthSchedule(org.mifos.clientportfolio.loan.service.MonthlyOnWeekOfMonthSchedule) QuestionGroupDetail(org.mifos.platform.questionnaire.service.QuestionGroupDetail) LoanOfferingFeesEntity(org.mifos.accounts.productdefinition.business.LoanOfferingFeesEntity) UserContext(org.mifos.security.util.UserContext) CreateAccountFeeDto(org.mifos.dto.domain.CreateAccountFeeDto) FeeDto(org.mifos.dto.domain.FeeDto) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) CreateLoanRequest(org.mifos.dto.domain.CreateLoanRequest) BigDecimal(java.math.BigDecimal) WeekDay(org.mifos.application.meeting.util.helpers.WeekDay) RecurringSchedule(org.mifos.clientportfolio.loan.service.RecurringSchedule) MonthlyOnDayOfMonthSchedule(org.mifos.clientportfolio.loan.service.MonthlyOnDayOfMonthSchedule) MeetingDetailsEntity(org.mifos.application.meeting.business.MeetingDetailsEntity) LoanOfferingBO(org.mifos.accounts.productdefinition.business.LoanOfferingBO) LoanAccountCashFlow(org.mifos.clientportfolio.newloan.applicationservice.LoanAccountCashFlow) FeeBO(org.mifos.accounts.fees.business.FeeBO) AmountFeeBO(org.mifos.accounts.fees.business.AmountFeeBO) LoanCreationResultDto(org.mifos.dto.screen.LoanCreationResultDto)

Example 19 with UserContextFactory

use of org.mifos.accounts.servicefacade.UserContextFactory 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 20 with UserContextFactory

use of org.mifos.accounts.servicefacade.UserContextFactory 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)

Aggregations

UserContextFactory (org.mifos.accounts.servicefacade.UserContextFactory)49 MifosUser (org.mifos.security.MifosUser)49 UserContext (org.mifos.security.util.UserContext)49 MifosRuntimeException (org.mifos.core.MifosRuntimeException)37 BusinessRuleException (org.mifos.service.BusinessRuleException)26 AccountException (org.mifos.accounts.exceptions.AccountException)18 PersistenceException (org.mifos.framework.exceptions.PersistenceException)17 ArrayList (java.util.ArrayList)13 LoanBO (org.mifos.accounts.loan.business.LoanBO)12 ServiceException (org.mifos.framework.exceptions.ServiceException)12 PersonnelBO (org.mifos.customers.personnel.business.PersonnelBO)9 ApplicationException (org.mifos.framework.exceptions.ApplicationException)9 Money (org.mifos.framework.util.helpers.Money)8 LocalDate (org.joda.time.LocalDate)7 SystemException (org.mifos.framework.exceptions.SystemException)6 CustomerBO (org.mifos.customers.business.CustomerBO)5 OfficeBO (org.mifos.customers.office.business.OfficeBO)5 Date (java.util.Date)4 MeetingException (org.mifos.application.meeting.exceptions.MeetingException)4 RoleBO (org.mifos.security.rolesandpermission.business.RoleBO)4