Search in sources :

Example 66 with SavingsBO

use of org.mifos.accounts.savings.business.SavingsBO in project head by mifos.

the class StandardAccountService method validatePayment.

@Override
public List<InvalidPaymentReason> validatePayment(AccountPaymentParametersDto payment) throws PersistenceException, AccountException {
    List<InvalidPaymentReason> errors = new ArrayList<InvalidPaymentReason>();
    AccountBO accountBo = this.legacyAccountDao.getAccount(payment.getAccountId());
    Date meetingDate = new CustomerPersistence().getLastMeetingDateForCustomer(accountBo.getCustomer().getCustomerId());
    boolean repaymentIndependentOfMeetingEnabled = new ConfigurationPersistence().isRepaymentIndepOfMeetingEnabled();
    if (!accountBo.isTrxnDateValid(payment.getPaymentDate().toDateMidnight().toDate(), meetingDate, repaymentIndependentOfMeetingEnabled)) {
        errors.add(InvalidPaymentReason.INVALID_DATE);
    }
    if (accountBo instanceof LoanBO) {
        if (((LoanBO) accountBo).paymentsNotAllowed()) {
            errors.add(InvalidPaymentReason.INVALID_LOAN_STATE);
        }
    }
    if (accountBo instanceof SavingsBO) {
        if (!accountBo.getState().equals(AccountState.SAVINGS_ACTIVE)) {
            errors.add(InvalidPaymentReason.INVALID_LOAN_STATE);
        }
    }
    if (AccountTypes.getAccountType(accountBo.getAccountType().getAccountTypeId()) == AccountTypes.LOAN_ACCOUNT) {
        if (!getLoanPaymentTypes().contains(payment.getPaymentType())) {
            errors.add(InvalidPaymentReason.UNSUPPORTED_PAYMENT_TYPE);
        }
    } else if (AccountTypes.getAccountType(accountBo.getAccountType().getAccountTypeId()) == AccountTypes.SAVINGS_ACCOUNT) {
        if (!getSavingsPaymentTypes().contains(payment.getPaymentType())) {
            errors.add(InvalidPaymentReason.UNSUPPORTED_PAYMENT_TYPE);
        }
    } else if (AccountTypes.getAccountType(accountBo.getAccountType().getAccountTypeId()) == AccountTypes.CUSTOMER_ACCOUNT) {
        if (!getFeePaymentTypes().contains(payment.getPaymentType())) {
            errors.add(InvalidPaymentReason.UNSUPPORTED_PAYMENT_TYPE);
        }
    }
    if (!accountBo.paymentAmountIsValid(new Money(accountBo.getCurrency(), payment.getPaymentAmount()), payment.getPaymentOptions())) {
        errors.add(InvalidPaymentReason.INVALID_PAYMENT_AMOUNT);
    }
    return errors;
}
Also used : AccountBO(org.mifos.accounts.business.AccountBO) CustomerAccountBO(org.mifos.customers.business.CustomerAccountBO) Money(org.mifos.framework.util.helpers.Money) ConfigurationPersistence(org.mifos.config.persistence.ConfigurationPersistence) LoanBO(org.mifos.accounts.loan.business.LoanBO) ArrayList(java.util.ArrayList) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) CustomerPersistence(org.mifos.customers.persistence.CustomerPersistence) Date(java.util.Date) LocalDate(org.joda.time.LocalDate)

Example 67 with SavingsBO

use of org.mifos.accounts.savings.business.SavingsBO in project head by mifos.

the class CollectionSheetServiceImpl method saveCollectionSheet.

/**
     * The method saves a collection sheet.
     *
     * @throws SaveCollectionSheetException
     * */
@Override
public CollectionSheetErrorsDto saveCollectionSheet(final SaveCollectionSheetDto saveCollectionSheet) throws SaveCollectionSheetException {
    Long totalTime;
    Long totalTimeStart = System.currentTimeMillis();
    Long readTime;
    Long saveTime = null;
    Long saveTimeStart;
    Integer topCustomerId = saveCollectionSheet.getSaveCollectionSheetCustomers().get(0).getCustomerId();
    CollectionSheetCustomerDto collectionSheetTopCustomer = new CustomerPersistence().findCustomerWithNoAssocationsLoaded(topCustomerId);
    if (collectionSheetTopCustomer == null) {
        List<InvalidSaveCollectionSheetReason> invalidSaveCollectionSheetReasons = new ArrayList<InvalidSaveCollectionSheetReason>();
        List<String> invalidSaveCollectionSheetReasonsExtended = new ArrayList<String>();
        invalidSaveCollectionSheetReasons.add(InvalidSaveCollectionSheetReason.INVALID_TOP_CUSTOMER);
        invalidSaveCollectionSheetReasonsExtended.add(InvalidSaveCollectionSheetReason.INVALID_TOP_CUSTOMER.toString() + ": Customer Id: " + topCustomerId);
        throw new SaveCollectionSheetException(invalidSaveCollectionSheetReasons, invalidSaveCollectionSheetReasonsExtended);
    }
    Short branchId = collectionSheetTopCustomer.getBranchId();
    String searchId = collectionSheetTopCustomer.getSearchId();
    // session caching: prefetch collection sheet data
    // done prior to structure validation because it loads
    // the customers and accounts to be validated into the session
    SaveCollectionSheetSessionCache saveCollectionSheetSessionCache = new SaveCollectionSheetSessionCache();
    saveCollectionSheetSessionCache.loadSessionCacheWithCollectionSheetData(saveCollectionSheet, branchId, searchId);
    try {
        new SaveCollectionSheetStructureValidator().execute(saveCollectionSheet);
    } catch (SaveCollectionSheetException e) {
        System.out.println(e.printInvalidSaveCollectionSheetReasons());
        throw e;
    }
    /*
         * With preprocessing complete...
         *
         * only errors and warnings from the business model remain
         */
    final List<String> failedSavingsDepositAccountNums = new ArrayList<String>();
    final List<String> failedSavingsWithdrawalNums = new ArrayList<String>();
    final List<String> failedLoanDisbursementAccountNumbers = new ArrayList<String>();
    final List<String> failedLoanRepaymentAccountNumbers = new ArrayList<String>();
    final List<String> failedCustomerAccountPaymentNums = new ArrayList<String>();
    final List<ClientAttendanceBO> clientAttendances = saveCollectionSheetAssembler.clientAttendanceAssemblerfromDto(saveCollectionSheet.getSaveCollectionSheetCustomers(), saveCollectionSheet.getTransactionDate(), branchId, searchId);
    final AccountPaymentEntity payment = saveCollectionSheetAssembler.accountPaymentAssemblerFromDto(saveCollectionSheet.getTransactionDate(), saveCollectionSheet.getPaymentType(), saveCollectionSheet.getReceiptId(), saveCollectionSheet.getReceiptDate(), saveCollectionSheet.getUserId());
    final List<SavingsBO> savingsAccounts = saveCollectionSheetAssembler.savingsAccountAssemblerFromDto(saveCollectionSheet.getSaveCollectionSheetCustomers(), payment, failedSavingsDepositAccountNums, failedSavingsWithdrawalNums);
    Short paymentTypeId = (payment.getPaymentType() == null || payment.getPaymentType().getId() == null) ? null : payment.getPaymentType().getId();
    final List<LoanBO> loanAccounts = saveCollectionSheetAssembler.loanAccountAssemblerFromDto(saveCollectionSheet.getSaveCollectionSheetCustomers(), payment, failedLoanDisbursementAccountNumbers, failedLoanRepaymentAccountNumbers, paymentTypeId);
    final List<AccountBO> customerAccounts = saveCollectionSheetAssembler.customerAccountAssemblerFromDto(saveCollectionSheet.getSaveCollectionSheetCustomers(), payment, failedCustomerAccountPaymentNums);
    boolean databaseErrorOccurred = false;
    Throwable databaseError = null;
    readTime = System.currentTimeMillis() - totalTimeStart;
    try {
        saveTimeStart = System.currentTimeMillis();
        persistCollectionSheet(clientAttendances, loanAccounts, customerAccounts, savingsAccounts);
        saveTime = System.currentTimeMillis() - saveTimeStart;
    } catch (HibernateException e) {
        logger.error("database error saving collection sheet", e);
        databaseErrorOccurred = true;
        databaseError = e;
    }
    totalTime = System.currentTimeMillis() - totalTimeStart;
    printTiming(saveCollectionSheet.printSummary(), totalTime, saveTime, readTime, saveCollectionSheetSessionCache);
    return new CollectionSheetErrorsDto(failedSavingsDepositAccountNums, failedSavingsWithdrawalNums, failedLoanDisbursementAccountNumbers, failedLoanRepaymentAccountNumbers, failedCustomerAccountPaymentNums, databaseErrorOccurred, databaseError);
}
Also used : ArrayList(java.util.ArrayList) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) AccountBO(org.mifos.accounts.business.AccountBO) CustomerPersistence(org.mifos.customers.persistence.CustomerPersistence) ClientAttendanceBO(org.mifos.customers.client.business.ClientAttendanceBO) HibernateException(org.hibernate.HibernateException) LoanBO(org.mifos.accounts.loan.business.LoanBO) SavingsBO(org.mifos.accounts.savings.business.SavingsBO)

Example 68 with SavingsBO

use of org.mifos.accounts.savings.business.SavingsBO in project head by mifos.

the class SavingsDaoHibernate method findBySystemId.

@Override
public SavingsBO findBySystemId(String globalAccountNumber) {
    logger.debug("In SavingsPersistence::findBySystemId(), globalAccountNumber: " + globalAccountNumber);
    Map<String, Object> queryParameters = new HashMap<String, Object>();
    queryParameters.put(AccountConstants.GLOBAL_ACCOUNT_NUMBER, globalAccountNumber);
    Object queryResult = baseDao.executeUniqueResultNamedQuery(NamedQueryConstants.FIND_ACCOUNT_BY_SYSTEM_ID, queryParameters);
    SavingsBO savings = queryResult == null ? null : (SavingsBO) queryResult;
    if (savings != null && savings.getRecommendedAmount() == null) {
        savings.setRecommendedAmount(new Money(savings.getCurrency()));
        Hibernate.initialize(savings.getAccountActionDates());
        Hibernate.initialize(savings.getAccountNotes());
        Hibernate.initialize(savings.getAccountFlags());
    }
    return savings;
}
Also used : Money(org.mifos.framework.util.helpers.Money) HashMap(java.util.HashMap) SavingsBO(org.mifos.accounts.savings.business.SavingsBO)

Example 69 with SavingsBO

use of org.mifos.accounts.savings.business.SavingsBO in project head by mifos.

the class CustomerDaoHibernate method getSavingsDetailDto.

@SuppressWarnings("unchecked")
@Override
public List<SavingsDetailDto> getSavingsDetailDto(Integer customerId, UserContext userContext) {
    Map<String, Object> queryParameters = new HashMap<String, Object>();
    queryParameters.put("CUSTOMER_ID", customerId);
    List<Object[]> queryResult = (List<Object[]>) this.genericDao.executeNamedQuery("Customer.getSavingsDetailDto", queryParameters);
    if (queryResult.size() == 0) {
        return null;
    }
    List<SavingsDetailDto> savingsDetails = new ArrayList<SavingsDetailDto>();
    String globalAccountNum;
    String prdOfferingName;
    Short accountStateId;
    String accountStateName;
    Money savingsBalance;
    String lookupName;
    Short currency;
    BigDecimal maxWithdrawalAmount;
    String savingsType = "";
    MifosCurrency mifosCurrency = Money.getDefaultCurrency();
    for (Object[] savingsDetail : queryResult) {
        globalAccountNum = (String) savingsDetail[0];
        prdOfferingName = (String) savingsDetail[1];
        accountStateId = (Short) savingsDetail[2];
        lookupName = (String) savingsDetail[3];
        accountStateName = ApplicationContextProvider.getBean(MessageLookup.class).lookup(lookupName);
        // TODO - use default currency or retrieved currency?
        currency = (Short) savingsDetail[4];
        savingsBalance = new Money(mifosCurrency, (BigDecimal) savingsDetail[5]);
        try {
            SavingsBO savingsBO = (SavingsBO) new AccountBusinessService().findBySystemId(globalAccountNum);
            maxWithdrawalAmount = savingsBO.getSavingsOffering().getMaxAmntWithdrawl().getAmount();
            if (savingsBO.getSavingsOffering().getSavingsType().getLookUpValue() != null) {
                savingsType = savingsBO.getSavingsOffering().getSavingsType().getName();
            }
            savingsDetails.add(new SavingsDetailDto(globalAccountNum, prdOfferingName, accountStateId, accountStateName, savingsBalance.toString(), maxWithdrawalAmount, savingsType));
        } catch (ServiceException e) {
            throw new MifosRuntimeException(e);
        }
    }
    return savingsDetails;
}
Also used : SavingsDetailDto(org.mifos.dto.domain.SavingsDetailDto) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) BigDecimal(java.math.BigDecimal) Money(org.mifos.framework.util.helpers.Money) AccountBusinessService(org.mifos.accounts.business.service.AccountBusinessService) ServiceException(org.mifos.framework.exceptions.ServiceException) ArrayList(java.util.ArrayList) List(java.util.List) MifosCurrency(org.mifos.application.master.business.MifosCurrency) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 70 with SavingsBO

use of org.mifos.accounts.savings.business.SavingsBO in project head by mifos.

the class CustomerServiceImpl method createSavingsAccountsForActiveSavingProducts.

private void createSavingsAccountsForActiveSavingProducts(ClientBO client, List<SavingsOfferingBO> savingProducts) {
    UserContext userContext = client.getUserContext();
    List<SavingsBO> savingsAccounts = new ArrayList<SavingsBO>();
    for (SavingsOfferingBO clientSavingsProduct : savingProducts) {
        try {
            if (clientSavingsProduct.isActive()) {
                List<CustomFieldDto> savingCustomFieldViews = new ArrayList<CustomFieldDto>();
                SavingsBO savingsAccount = new SavingsBO(userContext, clientSavingsProduct, client, AccountState.SAVINGS_ACTIVE, clientSavingsProduct.getRecommendedAmount(), savingCustomFieldViews);
                savingsAccounts.add(savingsAccount);
            }
        } catch (AccountException pe) {
            throw new MifosRuntimeException(pe);
        }
    }
    client.addSavingsAccounts(savingsAccounts);
}
Also used : AccountException(org.mifos.accounts.exceptions.AccountException) UserContext(org.mifos.security.util.UserContext) CustomFieldDto(org.mifos.dto.domain.CustomFieldDto) ArrayList(java.util.ArrayList) SavingsOfferingBO(org.mifos.accounts.productdefinition.business.SavingsOfferingBO) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Aggregations

SavingsBO (org.mifos.accounts.savings.business.SavingsBO)111 UserContext (org.mifos.security.util.UserContext)43 MifosRuntimeException (org.mifos.core.MifosRuntimeException)30 AccountException (org.mifos.accounts.exceptions.AccountException)28 LocalDate (org.joda.time.LocalDate)27 MifosUser (org.mifos.security.MifosUser)26 Test (org.junit.Test)25 Money (org.mifos.framework.util.helpers.Money)24 ArrayList (java.util.ArrayList)22 AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)22 PersistenceException (org.mifos.framework.exceptions.PersistenceException)22 Date (java.util.Date)20 TransactionDemarcate (org.mifos.framework.util.helpers.TransactionDemarcate)19 BusinessRuleException (org.mifos.service.BusinessRuleException)18 SavingsOfferingBO (org.mifos.accounts.productdefinition.business.SavingsOfferingBO)17 PersonnelBO (org.mifos.customers.personnel.business.PersonnelBO)17 CustomerBO (org.mifos.customers.business.CustomerBO)15 AccountActionDateEntity (org.mifos.accounts.business.AccountActionDateEntity)14 ServiceException (org.mifos.framework.exceptions.ServiceException)13 InvalidDateException (org.mifos.application.admin.servicefacade.InvalidDateException)12