Search in sources :

Example 1 with EndOfDayDetail

use of org.mifos.accounts.savings.interest.EndOfDayDetail in project head by mifos.

the class SavingsDaoHibernate method retrieveAllEndOfDayDetailsFor.

@SuppressWarnings("unchecked")
@Override
public List<EndOfDayDetail> retrieveAllEndOfDayDetailsFor(MifosCurrency currency, Long savingsId) {
    List<EndOfDayDetail> allEndOfDayDetailsForAccount = new ArrayList<EndOfDayDetail>();
    Map<String, Object> queryParameters = new HashMap<String, Object>();
    queryParameters.put("ACCOUNT_ID", savingsId.intValue());
    List<Object[]> queryResult = (List<Object[]>) this.baseDao.executeNamedQuery("savings.retrieveAllEndOfDayTransactionDetails", queryParameters);
    if (queryResult != null) {
        for (Object[] dailyRecord : queryResult) {
            Date dayOfYear = (Date) dailyRecord[0];
            BigDecimal totalDeposits = (BigDecimal) dailyRecord[1];
            BigDecimal totalWithdrawals = (BigDecimal) dailyRecord[2];
            BigDecimal totalInterest = (BigDecimal) dailyRecord[3];
            EndOfDayDetail endOfDayDetail = new EndOfDayDetail(new LocalDate(dayOfYear), new Money(currency, totalDeposits), new Money(currency, totalWithdrawals), new Money(currency, totalInterest));
            allEndOfDayDetailsForAccount.add(endOfDayDetail);
        }
    }
    return allEndOfDayDetailsForAccount;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Money(org.mifos.framework.util.helpers.Money) EndOfDayDetail(org.mifos.accounts.savings.interest.EndOfDayDetail) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with EndOfDayDetail

use of org.mifos.accounts.savings.interest.EndOfDayDetail in project head by mifos.

the class SavingsServiceFacadeWebTier method retrieveClosingDetails.

@Override
public SavingsAccountClosureDto retrieveClosingDetails(Long savingsId, LocalDate closureDate) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    SavingsBO savingsAccount = this.savingsDao.findById(savingsId);
    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);
    Money endOfAccountBalance = postingPeriodAtClosureResult.getPeriodBalance();
    Money interestAmountAtClosure = postingPeriodAtClosureResult.getPeriodInterest();
    List<ListElement> depositPaymentTypes = retrieveDepositPaymentTypes(userContext);
    return new SavingsAccountClosureDto(new LocalDate(), endOfAccountBalance.toString(), interestAmountAtClosure.toString(), depositPaymentTypes);
}
Also used : InterestPostingPeriodResult(org.mifos.accounts.savings.interest.InterestPostingPeriodResult) UserContext(org.mifos.security.util.UserContext) MifosUser(org.mifos.security.MifosUser) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) LocalDate(org.joda.time.LocalDate) SavingsAccountClosureDto(org.mifos.dto.domain.SavingsAccountClosureDto) Money(org.mifos.framework.util.helpers.Money) InterestScheduledEvent(org.mifos.accounts.savings.interest.schedule.InterestScheduledEvent) EndOfDayDetail(org.mifos.accounts.savings.interest.EndOfDayDetail) CalendarPeriod(org.mifos.accounts.savings.interest.CalendarPeriod) ListElement(org.mifos.dto.screen.ListElement)

Example 3 with EndOfDayDetail

use of org.mifos.accounts.savings.interest.EndOfDayDetail in project head by mifos.

the class SavingsServiceFacadeWebTier method postInterestForAccount.

private void postInterestForAccount(Integer savingsId, UserContext userContext, PersonnelBO createdBy, boolean inTransaction) {
    SavingsBO savingsAccount = this.savingsDao.findById(Long.valueOf(savingsId));
    savingsAccount.updateDetails(userContext);
    List<EndOfDayDetail> allEndOfDayDetailsForAccount = savingsDao.retrieveAllEndOfDayDetailsFor(savingsAccount.getCurrency(), Long.valueOf(savingsId));
    LocalDate interestPostingDate = new LocalDate(savingsAccount.getNextIntPostDate());
    InterestScheduledEvent postingSchedule = savingsInterestScheduledEventFactory.createScheduledEventFrom(savingsAccount.getInterestPostingMeeting());
    LocalDate startOfPeriod = postingSchedule.findFirstDateOfPeriodForMatchingDate(interestPostingDate);
    CalendarPeriod lastInterestPostingPeriod = new CalendarPeriod(startOfPeriod, interestPostingDate);
    InterestPostingPeriodResult interestPostingPeriodResult = determinePostingPeriodResult(lastInterestPostingPeriod, savingsAccount, allEndOfDayDetailsForAccount);
    savingsAccount.postInterest(postingSchedule, interestPostingPeriodResult, createdBy);
    StringBuilder postingInfoMessage = new StringBuilder().append("account id: ").append(savingsAccount.getAccountId()).append("posting interest: ").append(interestPostingPeriodResult);
    logger.info(postingInfoMessage.toString());
    try {
        if (!inTransaction) {
            this.transactionHelper.startTransaction();
        }
        this.savingsDao.save(savingsAccount);
        if (!inTransaction) {
            this.transactionHelper.commitTransaction();
        }
    } catch (Exception e) {
        if (!inTransaction) {
            this.transactionHelper.rollbackTransaction();
        }
        throw new BusinessRuleException(savingsAccount.getAccountId().toString(), e);
    } finally {
        if (!inTransaction) {
            this.transactionHelper.closeSession();
        }
    }
}
Also used : InterestPostingPeriodResult(org.mifos.accounts.savings.interest.InterestPostingPeriodResult) BusinessRuleException(org.mifos.service.BusinessRuleException) InterestScheduledEvent(org.mifos.accounts.savings.interest.schedule.InterestScheduledEvent) EndOfDayDetail(org.mifos.accounts.savings.interest.EndOfDayDetail) CalendarPeriod(org.mifos.accounts.savings.interest.CalendarPeriod) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) 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)

Example 4 with EndOfDayDetail

use of org.mifos.accounts.savings.interest.EndOfDayDetail 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 5 with EndOfDayDetail

use of org.mifos.accounts.savings.interest.EndOfDayDetail in project head by mifos.

the class SavingsServiceFacadeWebTier method withdrawalMakesBalanceNegativeOnDate.

private boolean withdrawalMakesBalanceNegativeOnDate(SavingsBO savingsAccount, Money amount, LocalDate trxnDate) {
    List<EndOfDayDetail> allEndOfDayDetailsForAccount = savingsDao.retrieveAllEndOfDayDetailsFor(savingsAccount.getCurrency(), savingsAccount.getAccountId().longValue());
    MifosCurrency currencyInUse = savingsAccount.getCurrency();
    Money balanceOnDateOfWithdrawal = calculateAccountBalanceOn(trxnDate.plusDays(1), allEndOfDayDetailsForAccount, currencyInUse);
    return amount.isGreaterThan(balanceOnDateOfWithdrawal);
}
Also used : Money(org.mifos.framework.util.helpers.Money) EndOfDayDetail(org.mifos.accounts.savings.interest.EndOfDayDetail) MifosCurrency(org.mifos.application.master.business.MifosCurrency)

Aggregations

EndOfDayDetail (org.mifos.accounts.savings.interest.EndOfDayDetail)7 Money (org.mifos.framework.util.helpers.Money)6 LocalDate (org.joda.time.LocalDate)4 SavingsBO (org.mifos.accounts.savings.business.SavingsBO)3 CalendarPeriod (org.mifos.accounts.savings.interest.CalendarPeriod)3 InterestPostingPeriodResult (org.mifos.accounts.savings.interest.InterestPostingPeriodResult)3 InterestScheduledEvent (org.mifos.accounts.savings.interest.schedule.InterestScheduledEvent)3 Date (java.util.Date)2 MifosRuntimeException (org.mifos.core.MifosRuntimeException)2 PersistenceException (org.mifos.framework.exceptions.PersistenceException)2 MifosUser (org.mifos.security.MifosUser)2 UserContext (org.mifos.security.util.UserContext)2 BusinessRuleException (org.mifos.service.BusinessRuleException)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AccountNotesEntity (org.mifos.accounts.business.AccountNotesEntity)1 AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)1 AccountException (org.mifos.accounts.exceptions.AccountException)1