Search in sources :

Example 26 with AccountException

use of org.mifos.accounts.exceptions.AccountException in project head by mifos.

the class WebTierAccountServiceFacade method applyMemberAccountHistoricalAdjustment.

/**
     * adjustment for member account payment has to be handled along with parent account payments and other members payments.
     * This method prepares data and run applyHistoricalAdjustment so adjustment will be made for parent payment context.
     */
@Override
@PreAuthorize("isFullyAuthenticated()")
public void applyMemberAccountHistoricalAdjustment(String memberGlobalAccountNum, Integer memberPaymentId, String adjustmentNote, Short personnelId, AdjustedPaymentDto adjustedPaymentDto) {
    try {
        LoanBO memberAccount = (LoanBO) accountBusinessService.findBySystemId(memberGlobalAccountNum);
        LoanBO parentAccount = memberAccount.getParentAccount();
        if (parentAccount == null) {
            throw new AccountException(LoanExceptionConstants.NO_PARENT_ACCOUNT_EXCEPTION);
        }
        AccountPaymentEntity parentPaymentEntity = memberAccount.findParentPaymentByMemberPaymentId(memberPaymentId);
        List<AdjustedPaymentDto> membersAdjustedPaymentDtoList = new ArrayList<AdjustedPaymentDto>();
        for (AccountPaymentEntity memberPayment : parentPaymentEntity.getMemberPayments()) {
            if (memberPayment.getAccount().getAccountId().equals(memberAccount.getAccountId())) {
                membersAdjustedPaymentDtoList.add(new AdjustedPaymentDto(adjustedPaymentDto.getAmount(), adjustedPaymentDto.getPaymentDate(), adjustedPaymentDto.getPaymentType(), memberAccount.getAccountId()));
            } else {
                membersAdjustedPaymentDtoList.add(new AdjustedPaymentDto(memberPayment.getAmount().getAmount().toString(), adjustedPaymentDto.getPaymentDate(), adjustedPaymentDto.getPaymentType(), memberPayment.getAccount().getAccountId()));
            }
        }
        BigDecimal parentAmountSubstraction = memberAccount.findPaymentById(memberPaymentId).getAmount().getAmount().subtract((new BigDecimal(adjustedPaymentDto.getAmount())));
        String newParentAmount = parentPaymentEntity.getAmount().getAmount().subtract(parentAmountSubstraction).toString();
        AdjustedPaymentDto parentAdjustedPaymentDto = new AdjustedPaymentDto(newParentAmount, adjustedPaymentDto.getPaymentDate(), adjustedPaymentDto.getPaymentType(), parentAccount.getAccountId(), membersAdjustedPaymentDtoList);
        this.applyHistoricalAdjustment(parentAccount.getGlobalAccountNum(), parentPaymentEntity.getPaymentId(), adjustmentNote, personnelId, parentAdjustedPaymentDto);
    } catch (ServiceException e) {
        throw new MifosRuntimeException(e);
    } catch (AccountException e) {
        throw new MifosRuntimeException(e);
    }
}
Also used : AccountException(org.mifos.accounts.exceptions.AccountException) AdjustedPaymentDto(org.mifos.dto.domain.AdjustedPaymentDto) ServiceException(org.mifos.framework.exceptions.ServiceException) LoanBO(org.mifos.accounts.loan.business.LoanBO) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) MifosRuntimeException(org.mifos.core.MifosRuntimeException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 27 with AccountException

use of org.mifos.accounts.exceptions.AccountException in project head by mifos.

the class SavingsServiceFacadeWebTier method retrieveSavingsAccountDetails.

@Override
public SavingsAccountDetailDto retrieveSavingsAccountDetails(String globalAccountNum) {
    MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = new UserContextFactory().create(mifosUser);
    SavingsBO savingsAccount = this.savingsDao.findBySystemId(globalAccountNum);
    savingsAccount.setUserContext(userContext);
    try {
        personnelDao.checkAccessPermission(userContext, savingsAccount.getOfficeId(), savingsAccount.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException("Access denied!", e);
    }
    return savingsAccount.toDto();
}
Also used : AccountException(org.mifos.accounts.exceptions.AccountException) UserContext(org.mifos.security.util.UserContext) MifosUser(org.mifos.security.MifosUser) UserContextFactory(org.mifos.accounts.servicefacade.UserContextFactory) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 28 with AccountException

use of org.mifos.accounts.exceptions.AccountException in project head by mifos.

the class SavingsServiceFacadeWebTier method adjustTransaction.

@Override
public PaymentDto adjustTransaction(SavingsAdjustmentDto savingsAdjustment, boolean inTransaction) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    SavingsBO savingsAccount = this.savingsDao.findById(savingsAdjustment.getSavingsId());
    try {
        personnelDao.checkAccessPermission(userContext, savingsAccount.getOfficeId(), savingsAccount.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException(e.getMessage(), e);
    }
    PersonnelBO updatedBy = this.personnelDao.findPersonnelById(userContext.getId());
    savingsAccount.updateDetails(userContext);
    Money amountAdjustedTo = new Money(savingsAccount.getCurrency(), BigDecimal.valueOf(savingsAdjustment.getAdjustedAmount()));
    AccountPaymentEntity adjustedPayment = savingsAccount.findPaymentById(savingsAdjustment.getPaymentId());
    PaymentDto otherTransferPayment = adjustedPayment.getOtherTransferPaymentDto();
    Money originalAmount = adjustedPayment.getAmount();
    LocalDate dateOfWithdrawal = savingsAdjustment.getTrxnDate();
    if (adjustedPayment.isSavingsWithdrawal() && originalAmount.isLessThan(amountAdjustedTo)) {
        Money addedWithdrawalAmount = amountAdjustedTo.subtract(originalAmount);
        if (withdrawalMakesBalanceNegative(savingsAccount, addedWithdrawalAmount, dateOfWithdrawal)) {
            throw new BusinessRuleException("errors.insufficentbalance", new String[] { savingsAccount.getGlobalAccountNum() });
        }
    } else if (adjustedPayment.isSavingsDeposit() && originalAmount.isGreaterThan(amountAdjustedTo)) {
        Money substractedAmount = originalAmount.subtract(amountAdjustedTo);
        if (withdrawalMakesBalanceNegative(savingsAccount, substractedAmount, dateOfWithdrawal)) {
            throw new BusinessRuleException("errors.insufficentbalance", new String[] { savingsAccount.getGlobalAccountNum() });
        }
    }
    try {
        if (!inTransaction) {
            this.transactionHelper.startTransaction();
        }
        this.transactionHelper.beginAuditLoggingFor(savingsAccount);
        AccountPaymentEntity newPayment = savingsAccount.adjustUserAction(amountAdjustedTo, savingsAdjustment.getNote(), savingsAdjustment.getTrxnDate(), updatedBy, savingsAdjustment.getPaymentId());
        recalculateInterestPostings(savingsAccount.getAccountId(), new LocalDate(adjustedPayment.getPaymentDate()));
        if (hasAccountNegativeBalance(savingsAccount)) {
            throw new BusinessRuleException("errors.insufficentbalance", new String[] { savingsAccount.getGlobalAccountNum() });
        }
        this.savingsDao.save(savingsAccount);
        // savings-savings transfer adjustment
        if (otherTransferPayment != null && otherTransferPayment.isSavingsPayment()) {
            this.transactionHelper.flushAndClearSession();
            SavingsBO otherSavingsAccount = this.savingsDao.findById(otherTransferPayment.getAccountId());
            otherSavingsAccount.updateDetails(userContext);
            AccountPaymentEntity newOtherTransferPayment = otherSavingsAccount.adjustUserAction(amountAdjustedTo, savingsAdjustment.getNote(), savingsAdjustment.getTrxnDate(), updatedBy, otherTransferPayment.getPaymentId());
            recalculateInterestPostings(savingsAccount.getAccountId(), new LocalDate(adjustedPayment.getPaymentDate()));
            if (hasAccountNegativeBalance(otherSavingsAccount)) {
                throw new BusinessRuleException("errors.insufficentbalance", new String[] { savingsAccount.getGlobalAccountNum() });
            }
            transactionHelper.flushAndClearSession();
            if (newPayment != null) {
                newPayment = savingsAccount.findPaymentById(newPayment.getPaymentId());
                newPayment.setOtherTransferPayment(newOtherTransferPayment);
                newOtherTransferPayment.setOtherTransferPayment(newPayment);
                legacyAcccountDao.updatePayment(newPayment);
            }
            this.savingsDao.save(otherSavingsAccount);
        }
        if (!inTransaction) {
            this.transactionHelper.commitTransaction();
        }
        return (newPayment == null) ? null : newPayment.toDto();
    } catch (BusinessRuleException e) {
        if (!inTransaction) {
            this.transactionHelper.rollbackTransaction();
        }
        throw new BusinessRuleException(e.getMessageKey(), e);
    } catch (Exception e) {
        if (!inTransaction) {
            this.transactionHelper.rollbackTransaction();
        }
        throw new MifosRuntimeException(e.getMessage(), e);
    } finally {
        if (!inTransaction) {
            this.transactionHelper.closeSession();
        }
    }
}
Also used : UserContext(org.mifos.security.util.UserContext) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) MifosUser(org.mifos.security.MifosUser) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) AdjustableSavingsPaymentDto(org.mifos.dto.screen.AdjustableSavingsPaymentDto) PaymentDto(org.mifos.dto.domain.PaymentDto) LocalDate(org.joda.time.LocalDate) InvalidDateException(org.mifos.application.admin.servicefacade.InvalidDateException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) StatesInitializationException(org.mifos.framework.exceptions.StatesInitializationException) AccountException(org.mifos.accounts.exceptions.AccountException) 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) Money(org.mifos.framework.util.helpers.Money) BusinessRuleException(org.mifos.service.BusinessRuleException) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 29 with AccountException

use of org.mifos.accounts.exceptions.AccountException in project head by mifos.

the class SavingsServiceFacadeWebTier method retrieveDepositDueDetails.

@Override
public SavingsAccountDepositDueDto retrieveDepositDueDetails(String globalAccountNum) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    SavingsBO savingsAccount = this.savingsDao.findBySystemId(globalAccountNum);
    try {
        personnelDao.checkAccessPermission(userContext, savingsAccount.getOfficeId(), savingsAccount.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException(e.getMessage(), e);
    }
    List<DueOnDateDto> previousDueDates = new ArrayList<DueOnDateDto>();
    SavingsScheduleEntity nextInstallment = (SavingsScheduleEntity) savingsAccount.getDetailsOfNextInstallment();
    Money totalDepositDue = Money.zero(savingsAccount.getCurrency());
    LocalDate nextDueDate = new LocalDate();
    if (nextInstallment != null) {
        nextDueDate = new LocalDate(nextInstallment.getActionDate());
        totalDepositDue = nextInstallment.getTotalDepositDue();
    }
    List<AccountActionDateEntity> scheduledDeposits = savingsAccount.getAccountActionDatesSortedByInstallmentId();
    for (AccountActionDateEntity scheduledDeposit : scheduledDeposits) {
        if (!scheduledDeposit.isPaid() && scheduledDeposit.isBefore(nextDueDate)) {
            SavingsScheduleEntity savingsScheduledDeposit = (SavingsScheduleEntity) scheduledDeposit;
            previousDueDates.add(new DueOnDateDto(scheduledDeposit.getActionDate(), MoneyUtils.currencyRound(savingsScheduledDeposit.getTotalDepositDue()).toString()));
        }
    }
    DueOnDateDto nextDueDetail = new DueOnDateDto(new java.sql.Date(nextDueDate.toDateMidnight().toDate().getTime()), MoneyUtils.currencyRound(totalDepositDue).toString());
    AccountStateEntity accountStateEntity = savingsAccount.getAccountState();
    return new SavingsAccountDepositDueDto(nextDueDetail, previousDueDates, accountStateEntity.getId(), accountStateEntity.getName());
}
Also used : UserContext(org.mifos.security.util.UserContext) ArrayList(java.util.ArrayList) MifosUser(org.mifos.security.MifosUser) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) AccountStateEntity(org.mifos.accounts.business.AccountStateEntity) LocalDate(org.joda.time.LocalDate) SavingsAccountDepositDueDto(org.mifos.dto.screen.SavingsAccountDepositDueDto) DueOnDateDto(org.mifos.dto.domain.DueOnDateDto) Money(org.mifos.framework.util.helpers.Money) AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) AccountException(org.mifos.accounts.exceptions.AccountException) SavingsScheduleEntity(org.mifos.accounts.savings.business.SavingsScheduleEntity) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 30 with AccountException

use of org.mifos.accounts.exceptions.AccountException in project head by mifos.

the class SavingsServiceFacadeWebTier method createSavingsAccount.

@Override
public Long createSavingsAccount(SavingsAccountCreationDto savingsAccountCreation, List<QuestionGroupDetail> questionGroups) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    LocalDate createdDate = new LocalDate();
    Integer createdById = user.getUserId();
    PersonnelBO createdBy = this.personnelDao.findPersonnelById(createdById.shortValue());
    CustomerBO customer = this.customerDao.findCustomerById(savingsAccountCreation.getCustomerId());
    SavingsOfferingBO savingsProduct = this.savingsProductDao.findById(savingsAccountCreation.getProductId());
    Money recommendedOrMandatory = new Money(savingsProduct.getCurrency(), savingsAccountCreation.getRecommendedOrMandatoryAmount());
    AccountState savingsAccountState = AccountState.fromShort(savingsAccountCreation.getAccountState());
    CalendarEvent calendarEvents = this.holidayDao.findCalendarEventsForThisYearAndNext(customer.getOfficeId());
    SavingsAccountTypeInspector savingsAccountWrapper = new SavingsAccountTypeInspector(customer, savingsProduct.getRecommendedAmntUnit());
    try {
        SavingsBO savingsAccount = null;
        if (savingsAccountWrapper.isIndividualSavingsAccount()) {
            savingsAccount = SavingsBO.createIndividalSavingsAccount(customer, savingsProduct, recommendedOrMandatory, savingsAccountState, createdDate, createdById, calendarEvents, createdBy);
        } else if (savingsAccountWrapper.isJointSavingsAccountWithClientTracking()) {
            List<CustomerBO> activeAndOnHoldClients = new CustomerPersistence().getActiveAndOnHoldChildren(customer.getSearchId(), customer.getOfficeId(), CustomerLevel.CLIENT);
            savingsAccount = SavingsBO.createJointSavingsAccount(customer, savingsProduct, recommendedOrMandatory, savingsAccountState, createdDate, createdById, calendarEvents, createdBy, activeAndOnHoldClients);
        }
        try {
            personnelDao.checkAccessPermission(userContext, savingsAccount.getOfficeId(), savingsAccount.getCustomer().getLoanOfficerId());
        } catch (AccountException e) {
            throw new MifosRuntimeException("Access denied!", e);
        }
        this.transactionHelper.startTransaction();
        this.savingsDao.save(savingsAccount);
        this.transactionHelper.flushSession();
        savingsAccount.generateSystemId(createdBy.getOffice().getGlobalOfficeNum());
        this.savingsDao.save(savingsAccount);
        this.transactionHelper.flushSession();
        // save question groups
        if (!questionGroups.isEmpty()) {
            Integer eventSourceId = questionnaireServiceFacade.getEventSourceId("Create", "Savings");
            QuestionGroupDetails questionGroupDetails = new QuestionGroupDetails(Integer.valueOf(user.getUserId()).shortValue(), savingsAccount.getAccountId(), eventSourceId, questionGroups);
            questionnaireServiceFacade.saveResponses(questionGroupDetails);
        }
        this.transactionHelper.commitTransaction();
        return savingsAccount.getAccountId().longValue();
    } catch (BusinessRuleException e) {
        this.transactionHelper.rollbackTransaction();
        throw new BusinessRuleException(e.getMessageKey(), e);
    } catch (Exception e) {
        this.transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } finally {
        this.transactionHelper.closeSession();
    }
}
Also used : UserContext(org.mifos.security.util.UserContext) QuestionGroupDetails(org.mifos.platform.questionnaire.service.QuestionGroupDetails) CalendarEvent(org.mifos.calendar.CalendarEvent) MifosUser(org.mifos.security.MifosUser) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) AccountState(org.mifos.accounts.util.helpers.AccountState) LocalDate(org.joda.time.LocalDate) InvalidDateException(org.mifos.application.admin.servicefacade.InvalidDateException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) StatesInitializationException(org.mifos.framework.exceptions.StatesInitializationException) AccountException(org.mifos.accounts.exceptions.AccountException) 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) SavingsAccountTypeInspector(org.mifos.accounts.savings.business.SavingsAccountTypeInspector) Money(org.mifos.framework.util.helpers.Money) BusinessRuleException(org.mifos.service.BusinessRuleException) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) SavingsOfferingBO(org.mifos.accounts.productdefinition.business.SavingsOfferingBO) CustomerBO(org.mifos.customers.business.CustomerBO) ArrayList(java.util.ArrayList) List(java.util.List) CustomerPersistence(org.mifos.customers.persistence.CustomerPersistence) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Aggregations

AccountException (org.mifos.accounts.exceptions.AccountException)115 MifosRuntimeException (org.mifos.core.MifosRuntimeException)47 UserContext (org.mifos.security.util.UserContext)43 PersistenceException (org.mifos.framework.exceptions.PersistenceException)42 Money (org.mifos.framework.util.helpers.Money)42 MifosUser (org.mifos.security.MifosUser)38 LoanBO (org.mifos.accounts.loan.business.LoanBO)31 ArrayList (java.util.ArrayList)30 LocalDate (org.joda.time.LocalDate)30 BusinessRuleException (org.mifos.service.BusinessRuleException)29 AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)26 PersonnelBO (org.mifos.customers.personnel.business.PersonnelBO)25 Date (java.util.Date)22 AccountActionDateEntity (org.mifos.accounts.business.AccountActionDateEntity)21 CustomerBO (org.mifos.customers.business.CustomerBO)19 UserContextFactory (org.mifos.accounts.servicefacade.UserContextFactory)17 PaymentData (org.mifos.accounts.util.helpers.PaymentData)17 SavingsBO (org.mifos.accounts.savings.business.SavingsBO)16 ServiceException (org.mifos.framework.exceptions.ServiceException)16 BigDecimal (java.math.BigDecimal)14