Search in sources :

Example 36 with PersonnelBO

use of org.mifos.customers.personnel.business.PersonnelBO 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 37 with PersonnelBO

use of org.mifos.customers.personnel.business.PersonnelBO in project head by mifos.

the class SavingsServiceFacadeWebTier method createSavingsAccount.

@Override
public String createSavingsAccount(OpeningBalanceSavingsAccount openingBalanceSavingsAccount) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    LocalDate createdDate = new LocalDate();
    Integer createdById = user.getUserId();
    PersonnelBO createdBy = this.personnelDao.findPersonnelById(createdById.shortValue());
    CustomerBO customer = this.customerDao.findCustomerBySystemId(openingBalanceSavingsAccount.getCustomerGlobalId());
    SavingsOfferingBO savingsProduct = this.savingsProductDao.findBySystemId(openingBalanceSavingsAccount.getProductGlobalId());
    AccountState savingsAccountState = AccountState.fromShort(openingBalanceSavingsAccount.getAccountState());
    Money recommendedOrMandatory = new Money(savingsProduct.getCurrency(), openingBalanceSavingsAccount.getRecommendedOrMandatoryAmount());
    Money openingBalance = new Money(savingsProduct.getCurrency(), new BigDecimal(0));
    LocalDate activationDate = openingBalanceSavingsAccount.getActivationDate();
    CalendarEvent calendarEvents = this.holidayDao.findCalendarEventsForThisYearAndNext(customer.getOfficeId());
    SavingsAccountActivationDetail activationDetails = SavingsBO.generateAccountActivationDetails(customer, savingsProduct, recommendedOrMandatory, savingsAccountState, calendarEvents, activationDate);
    SavingsBO savingsAccount = SavingsBO.createOpeningBalanceIndividualSavingsAccount(customer, savingsProduct, recommendedOrMandatory, savingsAccountState, createdDate, createdById, activationDetails, createdBy, openingBalance);
    savingsAccount.setGlobalAccountNum(openingBalanceSavingsAccount.getAccountNumber());
    savingsAccount.setSavingsBalance(openingBalance);
    DateTimeService dateTimeService = new DateTimeService();
    savingsAccount.setDateTimeService(dateTimeService);
    openingBalance = new Money(savingsProduct.getCurrency(), openingBalanceSavingsAccount.getOpeningBalance());
    if (savingsAccountState.isActiveLoanAccountState()) {
        savingsAccount.resetRecommendedAmountOnFutureInstallments();
    }
    try {
        if (openingBalance.isGreaterThanZero()) {
            PaymentData paymentData = new PaymentData(openingBalance, createdBy, Short.valueOf("1"), activationDate.toDateMidnight().toDate());
            paymentData.setCustomer(customer);
            savingsAccount.applyPayment(paymentData);
        }
        this.transactionHelper.startTransaction();
        this.savingsDao.save(savingsAccount);
        this.transactionHelper.flushSession();
        // savingsAccount.generateSystemId(createdBy.getOffice().getGlobalOfficeNum());
        this.savingsDao.save(savingsAccount);
        this.transactionHelper.commitTransaction();
        return savingsAccount.getGlobalAccountNum();
    } 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 : AccountPaymentData(org.mifos.accounts.util.helpers.AccountPaymentData) PaymentData(org.mifos.accounts.util.helpers.PaymentData) SavingsPaymentData(org.mifos.accounts.util.helpers.SavingsPaymentData) 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) BigDecimal(java.math.BigDecimal) 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) SavingsAccountActivationDetail(org.mifos.accounts.savings.business.SavingsAccountActivationDetail) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) SavingsOfferingBO(org.mifos.accounts.productdefinition.business.SavingsOfferingBO) CustomerBO(org.mifos.customers.business.CustomerBO) DateTimeService(org.mifos.framework.util.DateTimeService) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 38 with PersonnelBO

use of org.mifos.customers.personnel.business.PersonnelBO in project head by mifos.

the class SavingsServiceFacadeWebTier method closeSavingsAccount.

@Override
public void closeSavingsAccount(Long savingsId, String notes, SavingsWithdrawalDto closeAccountDto) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    SavingsBO savingsAccount = this.savingsDao.findById(savingsId);
    savingsAccount.updateDetails(userContext);
    PersonnelBO createdBy = this.personnelDao.findPersonnelById(userContext.getId());
    LocalDate closureDate = closeAccountDto.getDateOfWithdrawal();
    // Assumption that all previous interest postings occurred correctly
    InterestScheduledEvent postingSchedule = savingsInterestScheduledEventFactory.createScheduledEventFrom(savingsAccount.getInterestPostingMeeting());
    LocalDate nextPostingDate = new LocalDate(savingsAccount.getNextIntPostDate());
    LocalDate startOfPostingPeriod = postingSchedule.findFirstDateOfPeriodForMatchingDate(nextPostingDate);
    CalendarPeriod postingPeriodAtClosure;
    if (startOfPostingPeriod.isAfter(closureDate)) {
        postingPeriodAtClosure = new CalendarPeriod(closureDate, closureDate);
    } else {
        postingPeriodAtClosure = new CalendarPeriod(startOfPostingPeriod, closureDate);
    }
    List<EndOfDayDetail> allEndOfDayDetailsForAccount = savingsDao.retrieveAllEndOfDayDetailsFor(savingsAccount.getCurrency(), Long.valueOf(savingsId));
    InterestPostingPeriodResult postingPeriodAtClosureResult = determinePostingPeriodResult(postingPeriodAtClosure, savingsAccount, allEndOfDayDetailsForAccount);
    savingsAccount.postInterest(postingSchedule, postingPeriodAtClosureResult, createdBy);
    AccountNotesEntity notesEntity = new AccountNotesEntity(new DateTimeService().getCurrentJavaSqlDate(), notes, createdBy, savingsAccount);
    try {
        CustomerBO customer = savingsAccount.getCustomer();
        if (closeAccountDto.getCustomerId() != null) {
            List<CustomerBO> clientList = new CustomerPersistence().getActiveAndOnHoldChildren(savingsAccount.getCustomer().getSearchId(), savingsAccount.getCustomer().getOfficeId(), CustomerLevel.CLIENT);
            for (CustomerBO client : clientList) {
                if (closeAccountDto.getCustomerId().intValue() == client.getCustomerId().intValue()) {
                    customer = client;
                    break;
                }
            }
        }
        Money amount = new Money(savingsAccount.getCurrency(), closeAccountDto.getAmount().toString());
        PaymentTypeEntity paymentType = new PaymentTypeEntity(closeAccountDto.getModeOfPayment().shortValue());
        Date receiptDate = null;
        if (closeAccountDto.getDateOfReceipt() != null) {
            receiptDate = closeAccountDto.getDateOfReceipt().toDateMidnight().toDate();
        }
        AccountPaymentEntity closeAccount = new AccountPaymentEntity(savingsAccount, amount, closeAccountDto.getReceiptId(), receiptDate, paymentType, closeAccountDto.getDateOfWithdrawal().toDateMidnight().toDate());
        this.transactionHelper.startTransaction();
        this.transactionHelper.beginAuditLoggingFor(savingsAccount);
        savingsAccount.closeAccount(closeAccount, notesEntity, customer, createdBy);
        this.savingsDao.save(savingsAccount);
        this.transactionHelper.commitTransaction();
    } catch (BusinessRuleException e) {
        this.transactionHelper.rollbackTransaction();
        throw new BusinessRuleException(e.getMessageKey(), e);
    } catch (PersistenceException e) {
        this.transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } finally {
        this.transactionHelper.closeSession();
    }
}
Also used : InterestPostingPeriodResult(org.mifos.accounts.savings.interest.InterestPostingPeriodResult) UserContext(org.mifos.security.util.UserContext) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) AccountNotesEntity(org.mifos.accounts.business.AccountNotesEntity) MifosUser(org.mifos.security.MifosUser) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) LocalDate(org.joda.time.LocalDate) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) PaymentTypeEntity(org.mifos.application.master.business.PaymentTypeEntity) Money(org.mifos.framework.util.helpers.Money) BusinessRuleException(org.mifos.service.BusinessRuleException) InterestScheduledEvent(org.mifos.accounts.savings.interest.schedule.InterestScheduledEvent) EndOfDayDetail(org.mifos.accounts.savings.interest.EndOfDayDetail) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) CalendarPeriod(org.mifos.accounts.savings.interest.CalendarPeriod) PersistenceException(org.mifos.framework.exceptions.PersistenceException) CustomerBO(org.mifos.customers.business.CustomerBO) CustomerPersistence(org.mifos.customers.persistence.CustomerPersistence) DateTimeService(org.mifos.framework.util.DateTimeService) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 39 with PersonnelBO

use of org.mifos.customers.personnel.business.PersonnelBO in project head by mifos.

the class SavingsServiceFacadeWebTier method postInterestForLastPostingPeriod.

/**
     * This method is responsible for posting interest for the last posting period only.
     * 
     * It assumes that interest calculation and interest posting frequencies cannot change on savings product of savings
     * account. It assumes that the interest posting date is correct and valid with respect to the interest posting
     * frequency of the product/account.
     */
@Override
public void postInterestForLastPostingPeriod(LocalDate dateBatchJobIsScheduled) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    PersonnelBO createdBy = this.personnelDao.findPersonnelById(userContext.getId());
    List<Integer> accountIds = this.savingsDao.retrieveAllActiveAndInActiveSavingsAccountsPendingInterestPostingOn(dateBatchJobIsScheduled);
    for (Integer savingsId : accountIds) {
        postInterestForAccount(savingsId, userContext, createdBy, false);
    }
}
Also used : PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) UserContext(org.mifos.security.util.UserContext) MifosUser(org.mifos.security.MifosUser)

Example 40 with PersonnelBO

use of org.mifos.customers.personnel.business.PersonnelBO in project head by mifos.

the class SavingsServiceFacadeWebTier method updateSavingsAccountStatus.

@Override
public void updateSavingsAccountStatus(AccountUpdateStatus updateStatus) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    PersonnelBO loggedInUser = this.personnelDao.findPersonnelById(userContext.getId());
    SavingsBO savingsAccount = this.savingsDao.findById(updateStatus.getSavingsId());
    savingsAccount.updateDetails(userContext);
    try {
        this.transactionHelper.startTransaction();
        this.transactionHelper.beginAuditLoggingFor(savingsAccount);
        AccountState newStatus = AccountState.fromShort(updateStatus.getNewStatusId());
        // FIXME - keithw - refactor savings specific logic out of changeStatus and create savings statue machine
        // wrapper.
        savingsAccount.changeStatus(newStatus, updateStatus.getFlagId(), updateStatus.getComment(), loggedInUser);
        this.savingsDao.save(savingsAccount);
        this.transactionHelper.commitTransaction();
    } 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 : BusinessRuleException(org.mifos.service.BusinessRuleException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) UserContext(org.mifos.security.util.UserContext) MifosUser(org.mifos.security.MifosUser) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) AccountState(org.mifos.accounts.util.helpers.AccountState) 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) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Aggregations

PersonnelBO (org.mifos.customers.personnel.business.PersonnelBO)239 Test (org.junit.Test)91 UserContext (org.mifos.security.util.UserContext)65 ArrayList (java.util.ArrayList)62 Money (org.mifos.framework.util.helpers.Money)46 MifosRuntimeException (org.mifos.core.MifosRuntimeException)44 OfficeBO (org.mifos.customers.office.business.OfficeBO)44 MifosUser (org.mifos.security.MifosUser)44 PersistenceException (org.mifos.framework.exceptions.PersistenceException)41 Date (java.util.Date)39 LocalDate (org.joda.time.LocalDate)37 AccountException (org.mifos.accounts.exceptions.AccountException)37 DateTime (org.joda.time.DateTime)36 AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)34 BusinessRuleException (org.mifos.service.BusinessRuleException)33 PersonnelBuilder (org.mifos.domain.builders.PersonnelBuilder)31 MeetingBO (org.mifos.application.meeting.business.MeetingBO)30 CustomerBO (org.mifos.customers.business.CustomerBO)27 ServiceException (org.mifos.framework.exceptions.ServiceException)27 LoanBO (org.mifos.accounts.loan.business.LoanBO)25