Search in sources :

Example 1 with SavingsAccountTypeInspector

use of org.mifos.accounts.savings.business.SavingsAccountTypeInspector 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

ArrayList (java.util.ArrayList)1 List (java.util.List)1 LocalDate (org.joda.time.LocalDate)1 AccountException (org.mifos.accounts.exceptions.AccountException)1 SavingsOfferingBO (org.mifos.accounts.productdefinition.business.SavingsOfferingBO)1 SavingsAccountTypeInspector (org.mifos.accounts.savings.business.SavingsAccountTypeInspector)1 SavingsBO (org.mifos.accounts.savings.business.SavingsBO)1 AccountState (org.mifos.accounts.util.helpers.AccountState)1 InvalidDateException (org.mifos.application.admin.servicefacade.InvalidDateException)1 CalendarEvent (org.mifos.calendar.CalendarEvent)1 MifosRuntimeException (org.mifos.core.MifosRuntimeException)1 CustomerBO (org.mifos.customers.business.CustomerBO)1 CustomerPersistence (org.mifos.customers.persistence.CustomerPersistence)1 PersonnelBO (org.mifos.customers.personnel.business.PersonnelBO)1 HibernateSearchException (org.mifos.framework.exceptions.HibernateSearchException)1 PageExpiredException (org.mifos.framework.exceptions.PageExpiredException)1 PersistenceException (org.mifos.framework.exceptions.PersistenceException)1 ServiceException (org.mifos.framework.exceptions.ServiceException)1 StatesInitializationException (org.mifos.framework.exceptions.StatesInitializationException)1 Money (org.mifos.framework.util.helpers.Money)1