Search in sources :

Example 31 with UserContextFactory

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

the class CollectionSheetServiceFacadeWebTier method saveCollectionSheet.

@Override
public CollectionSheetErrorsDto saveCollectionSheet(final SaveCollectionSheetDto saveCollectionSheet) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    final SaveCollectionSheetDto saveCollectionSheetDto = saveCollectionSheet;
    UserContext userContext = new UserContextFactory().create(user);
    int customerId = saveCollectionSheetDto.getSaveCollectionSheetCustomers().get(0).getCustomerId();
    CustomerBO customerBO = this.customerDao.findCustomerById(customerId);
    try {
        personnelDao.checkAccessPermission(userContext, customerBO.getOfficeId(), customerBO.getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException("Access denied!", e);
    }
    CollectionSheetErrorsDto collectionSheetErrorsDto = null;
    try {
        collectionSheetErrorsDto = collectionSheetService.saveCollectionSheet(saveCollectionSheet);
    } catch (SaveCollectionSheetException e) {
        throw new MifosRuntimeException(e.printInvalidSaveCollectionSheetReasons());
    }
    return collectionSheetErrorsDto;
}
Also used : AccountException(org.mifos.accounts.exceptions.AccountException) UserContext(org.mifos.security.util.UserContext) CustomerBO(org.mifos.customers.business.CustomerBO) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 32 with UserContextFactory

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

the class AdminServiceFacadeWebTier method updateSavingsProduct.

@Override
public PrdOfferingDto updateSavingsProduct(SavingsProductDto savingsProductRequest) {
    SavingsOfferingBO savingsProductForUpdate = this.savingsProductDao.findById(savingsProductRequest.getProductDetails().getId());
    // enforced by integrity constraints on table also.
    if (savingsProductForUpdate.isDifferentName(savingsProductRequest.getProductDetails().getName())) {
        this.savingsProductDao.validateProductWithSameNameDoesNotExist(savingsProductRequest.getProductDetails().getName());
    }
    if (savingsProductForUpdate.isDifferentShortName(savingsProductRequest.getProductDetails().getShortName())) {
        this.savingsProductDao.validateProductWithSameShortNameDoesNotExist(savingsProductRequest.getProductDetails().getShortName());
    }
    // domain rule validation - put on domain entity
    if (savingsProductForUpdate.isDifferentStartDate(savingsProductRequest.getProductDetails().getStartDate())) {
        validateStartDateIsNotBeforeToday(savingsProductRequest.getProductDetails().getStartDate());
        validateStartDateIsNotOverOneYearFromToday(savingsProductRequest.getProductDetails().getStartDate());
        validateEndDateIsPastStartDate(savingsProductRequest.getProductDetails().getStartDate(), savingsProductRequest.getProductDetails().getEndDate());
    }
    boolean activeOrInactiveSavingsAccountExist = this.savingsProductDao.activeOrInactiveSavingsAccountsExistForProduct(savingsProductRequest.getProductDetails().getId());
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(user);
    SavingsOfferingBO newSavingsDetails = new SavingsProductAssembler(this.loanProductDao, this.savingsProductDao, this.generalLedgerDao).fromDto(user, savingsProductRequest);
    savingsProductForUpdate.updateDetails(userContext);
    HibernateTransactionHelper transactionHelper = new HibernateTransactionHelperForStaticHibernateUtil();
    try {
        transactionHelper.startTransaction();
        transactionHelper.beginAuditLoggingFor(savingsProductForUpdate);
        if (activeOrInactiveSavingsAccountExist) {
            LocalDate updateDate = new LocalDate();
            savingsProductForUpdate.updateProductDetails(newSavingsDetails.getPrdOfferingName(), newSavingsDetails.getPrdOfferingShortName(), newSavingsDetails.getDescription(), newSavingsDetails.getPrdCategory(), newSavingsDetails.getStartDate(), newSavingsDetails.getEndDate(), newSavingsDetails.getPrdStatus());
            savingsProductForUpdate.updateSavingsDetails(newSavingsDetails.getRecommendedAmount(), newSavingsDetails.getRecommendedAmntUnit(), newSavingsDetails.getMaxAmntWithdrawl(), newSavingsDetails.getInterestRate(), newSavingsDetails.getMinAmntForInt(), updateDate);
        } else {
            savingsProductForUpdate.updateDetailsOfProductNotInUse(newSavingsDetails.getPrdOfferingName(), newSavingsDetails.getPrdOfferingShortName(), newSavingsDetails.getDescription(), newSavingsDetails.getPrdCategory(), newSavingsDetails.getStartDate(), newSavingsDetails.getEndDate(), newSavingsDetails.getPrdApplicableMaster(), newSavingsDetails.getPrdStatus());
            savingsProductForUpdate.updateDetailsOfSavingsProductNotInUse(newSavingsDetails.getSavingsType(), newSavingsDetails.getRecommendedAmount(), newSavingsDetails.getRecommendedAmntUnit(), newSavingsDetails.getMaxAmntWithdrawl(), newSavingsDetails.getInterestRate(), newSavingsDetails.getInterestCalcType(), newSavingsDetails.getTimePerForInstcalc(), newSavingsDetails.getFreqOfPostIntcalc(), newSavingsDetails.getMinAmntForInt());
        }
        this.savingsProductDao.save(savingsProductForUpdate);
        transactionHelper.commitTransaction();
        return savingsProductForUpdate.toDto();
    } catch (Exception e) {
        transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } finally {
        transactionHelper.closeSession();
    }
}
Also used : HibernateTransactionHelper(org.mifos.framework.hibernate.helper.HibernateTransactionHelper) HibernateTransactionHelperForStaticHibernateUtil(org.mifos.framework.hibernate.helper.HibernateTransactionHelperForStaticHibernateUtil) UserContext(org.mifos.security.util.UserContext) SavingsOfferingBO(org.mifos.accounts.productdefinition.business.SavingsOfferingBO) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) LocalDate(org.joda.time.LocalDate) SystemException(org.mifos.framework.exceptions.SystemException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) BusinessRuleException(org.mifos.service.BusinessRuleException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) ServiceException(org.mifos.framework.exceptions.ServiceException) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 33 with UserContextFactory

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

the class OfficeServiceFacadeWebTier method updateOffice.

@Override
public boolean updateOffice(Short officeId, Integer versionNum, OfficeUpdateRequest officeUpdateRequest) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(user);
    try {
        boolean isParentOfficeChanged = false;
        OfficeBO office = officeDao.findOfficeById(officeId);
        office.validateVersion(versionNum);
        OfficeBO parentOffice = null;
        if (officeUpdateRequest.getParentOfficeId() != null) {
            parentOffice = officeDao.findOfficeById(officeUpdateRequest.getParentOfficeId());
            if (office.isDifferentParentOffice(parentOffice)) {
                holidayDao.validateNoExtraFutureHolidaysApplicableOnParentOffice(office.getParentOffice().getOfficeId(), officeUpdateRequest.getParentOfficeId());
            }
        }
        if (office.isNameDifferent(officeUpdateRequest.getOfficeName())) {
            officeDao.validateOfficeNameIsNotTaken(officeUpdateRequest.getOfficeName());
        }
        if (office.isShortNameDifferent(officeUpdateRequest.getShortName())) {
            officeDao.validateOfficeShortNameIsNotTaken(officeUpdateRequest.getShortName());
        }
        OfficeStatus newStatus = OfficeStatus.getOfficeStatus(officeUpdateRequest.getNewStatus());
        if (!office.isStatusDifferent(newStatus)) {
            if (OfficeStatus.INACTIVE.equals(officeUpdateRequest.getNewStatus())) {
                officeDao.validateNoActiveChildrenExist(office.getOfficeId());
                officeDao.validateNoActivePeronnelExist(office.getOfficeId());
            }
            if (parentOffice != null) {
                if (parentOffice.isInActive()) {
                    throw new OfficeException(OfficeConstants.KEYPARENTNOTACTIVE);
                }
            }
        }
        StaticHibernateUtil.startTransaction();
        office.update(userContext, officeUpdateRequest, parentOffice);
        StaticHibernateUtil.commitTransaction();
        return isParentOfficeChanged;
    } catch (OfficeException e1) {
        throw new BusinessRuleException(e1.getKey(), e1);
    } catch (ApplicationException e) {
        StaticHibernateUtil.rollbackTransaction();
        throw new BusinessRuleException(e.getKey(), e);
    } catch (Exception e) {
        StaticHibernateUtil.rollbackTransaction();
        throw new MifosRuntimeException(e.getMessage(), e);
    } finally {
        StaticHibernateUtil.closeSession();
    }
}
Also used : OfficeException(org.mifos.customers.office.exceptions.OfficeException) BusinessRuleException(org.mifos.service.BusinessRuleException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) OfficeBO(org.mifos.customers.office.business.OfficeBO) UserContext(org.mifos.security.util.UserContext) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) OfficeStatus(org.mifos.customers.office.util.helpers.OfficeStatus) OfficeValidationException(org.mifos.customers.office.exceptions.OfficeValidationException) BusinessRuleException(org.mifos.service.BusinessRuleException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) OfficeException(org.mifos.customers.office.exceptions.OfficeException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 34 with UserContextFactory

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

the class StandardAccountService method makeImportedPayments.

/**
     * method created for undo transaction import ability MIFOS-5702
     * returns Id of transaction  
     * */
public AccountBO makeImportedPayments(AccountPaymentParametersDto accountPaymentParametersDto, Integer savingsPaymentId) throws PersistenceException, AccountException {
    final int accountId = accountPaymentParametersDto.getAccountId();
    final AccountBO account = this.legacyAccountDao.getAccount(accountId);
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    try {
        personnelDao.checkAccessPermission(userContext, account.getOfficeId(), account.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException(SecurityConstants.KEY_ACTIVITY_NOT_ALLOWED, e);
    }
    monthClosingServiceFacade.validateTransactionDate(accountPaymentParametersDto.getPaymentDate().toDateMidnight().toDate());
    /**
         * Handle member payment if parent payment data not provided.
         * Situation may occur when payment is executed directly on group member account (e.g. from transaction import).
         * Loan Group Member payments should be posted after parent payment.
         */
    if (account.isGroupLoanAccountMember()) {
        AccountPaymentParametersDto parentPaymentParametersDto = this.createParentLoanPaymentData(account, accountPaymentParametersDto);
        return makeImportedPayments(parentPaymentParametersDto, savingsPaymentId);
    }
    PersonnelBO loggedInUser = ApplicationContextProvider.getBean(LegacyPersonnelDao.class).findPersonnelById(accountPaymentParametersDto.getUserMakingPayment().getUserId());
    List<InvalidPaymentReason> validationErrors = validatePayment(accountPaymentParametersDto);
    if (!(account instanceof CustomerAccountBO) && validationErrors.contains(InvalidPaymentReason.INVALID_DATE)) {
        throw new AccountException("errors.invalidTxndate");
    }
    Money overpaymentAmount = null;
    Money amount = new Money(account.getCurrency(), accountPaymentParametersDto.getPaymentAmount());
    if (account instanceof LoanBO && accountPaymentParametersDto.getPaymentOptions().contains(AccountPaymentParametersDto.PaymentOptions.ALLOW_OVERPAYMENTS) && amount.isGreaterThan(((LoanBO) account).getTotalRepayableAmount())) {
        overpaymentAmount = amount.subtract(((LoanBO) account).getTotalRepayableAmount());
        amount = ((LoanBO) account).getTotalRepayableAmount();
    }
    Date receiptDate = null;
    if (accountPaymentParametersDto.getReceiptDate() != null) {
        receiptDate = accountPaymentParametersDto.getReceiptDate().toDateMidnight().toDate();
    }
    PaymentData paymentData = account.createPaymentData(amount, accountPaymentParametersDto.getPaymentDate().toDateMidnight().toDate(), accountPaymentParametersDto.getReceiptId(), receiptDate, accountPaymentParametersDto.getPaymentType().getValue(), loggedInUser);
    if (savingsPaymentId != null) {
        AccountPaymentEntity withdrawal = legacyAccountDao.findPaymentById(savingsPaymentId);
        paymentData.setOtherTransferPayment(withdrawal);
    }
    if (accountPaymentParametersDto.getCustomer() != null) {
        paymentData.setCustomer(customerDao.findCustomerById(accountPaymentParametersDto.getCustomer().getCustomerId()));
    }
    paymentData.setComment(accountPaymentParametersDto.getComment());
    paymentData.setOverpaymentAmount(overpaymentAmount);
    if (accountPaymentParametersDto.getPaymentOptions().contains(PaymentOptions.ALLOW_OVERPAYMENTS)) {
        paymentData.setAllowOverpayment(true);
    }
    AccountPaymentEntity paymentEntity = account.applyPayment(paymentData);
    handleParentGroupLoanPayment(account, accountPaymentParametersDto, savingsPaymentId, paymentEntity);
    this.legacyAccountDao.createOrUpdate(account);
    /*
         * Return null when only overpayment is being apply
         */
    if (amount.isZero() && overpaymentAmount != null && overpaymentAmount.isGreaterThanZero()) {
        return null;
    }
    return account;
}
Also used : CustomerAccountBO(org.mifos.customers.business.CustomerAccountBO) PaymentData(org.mifos.accounts.util.helpers.PaymentData) UserContext(org.mifos.security.util.UserContext) LoanBO(org.mifos.accounts.loan.business.LoanBO) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) LegacyPersonnelDao(org.mifos.customers.personnel.persistence.LegacyPersonnelDao) AccountPaymentParametersDto(org.mifos.dto.domain.AccountPaymentParametersDto) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) AccountBO(org.mifos.accounts.business.AccountBO) CustomerAccountBO(org.mifos.customers.business.CustomerAccountBO) Money(org.mifos.framework.util.helpers.Money) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 35 with UserContextFactory

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

the class FeeServiceFacadeWebTier method createFee.

@Override
public String createFee(FeeCreateDto feeCreateDto) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(user);
    try {
        FeeCategory feeCategory = (feeCreateDto.getCategoryType() != null) ? FeeCategory.getFeeCategory(feeCreateDto.getCategoryType()) : null;
        FeeFrequencyType feeFrequencyType = (feeCreateDto.getFeeFrequencyType() != null) ? FeeFrequencyType.getFeeFrequencyType(feeCreateDto.getFeeFrequencyType()) : null;
        FeePayment feePayment = (feeCreateDto.getFeePaymentType() != null) ? FeePayment.getFeePayment(feeCreateDto.getFeePaymentType()) : null;
        FeeFormula feeFormula = (feeCreateDto.getFeeFormula() != null) ? FeeFormula.getFeeFormula(feeCreateDto.getFeeFormula()) : null;
        RecurrenceType feeRecurrenceType = (feeCreateDto.getFeeRecurrenceType() != null) ? RecurrenceType.fromInt(feeCreateDto.getFeeRecurrenceType()) : null;
        FeeCreateRequest feeCreateRequest = new FeeCreateRequest(feeCategory, feeFrequencyType, feeCreateDto.getGlCode(), feePayment, feeFormula, feeCreateDto.getFeeName(), feeCreateDto.isRateFee(), feeCreateDto.isCustomerDefaultFee(), feeCreateDto.getRate(), feeCreateDto.getCurrencyId(), feeCreateDto.getAmount(), feeRecurrenceType, feeCreateDto.getMonthRecurAfter(), feeCreateDto.getWeekRecurAfter());
        FeeBO fee = this.feeService.create(feeCreateRequest, userContext);
        return fee.getFeeId().toString();
    } catch (ApplicationException e) {
        throw new BusinessRuleException(e.getKey(), e);
    }
}
Also used : FeePayment(org.mifos.accounts.fees.util.helpers.FeePayment) BusinessRuleException(org.mifos.service.BusinessRuleException) FeeFormula(org.mifos.accounts.fees.util.helpers.FeeFormula) ApplicationException(org.mifos.framework.exceptions.ApplicationException) FeeFrequencyType(org.mifos.accounts.fees.util.helpers.FeeFrequencyType) UserContext(org.mifos.security.util.UserContext) RecurrenceType(org.mifos.application.meeting.util.helpers.RecurrenceType) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) FeeBO(org.mifos.accounts.fees.business.FeeBO) FeeCategory(org.mifos.accounts.fees.util.helpers.FeeCategory)

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