Search in sources :

Example 96 with AccountException

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

the class LoanBO method waivePenaltyAmountOverDue.

private void waivePenaltyAmountOverDue() throws AccountException {
    Money chargeWaived = new Money(getCurrency());
    Money principal = new Money(getCurrency());
    Money interest = new Money(getCurrency());
    Money fee = new Money(getCurrency());
    List<AccountActionDateEntity> accountActionDateList = getApplicableIdsForNextInstallmentAndArrears();
    // Fix for http://mifosforge.jira.com/browse/MIFOS-2826
    if (getDetailsOfNextInstallment() != null) {
        accountActionDateList.remove(accountActionDateList.size() - 1);
    }
    for (AccountActionDateEntity accountActionDateEntity : accountActionDateList) {
        chargeWaived = chargeWaived.add(((LoanScheduleEntity) accountActionDateEntity).waivePenaltyCharges());
    }
    if (chargeWaived != null && chargeWaived.isGreaterThanZero()) {
        updateTotalPenaltyAmount(chargeWaived);
        updateAccountActivity(principal, interest, fee, chargeWaived, userContext.getId(), AccountConstants.AMOUNT + chargeWaived + AccountConstants.WAIVED);
        waiveOverdueChargesFromMemberAccounts(LoanConstants.PENALTY_WAIVED);
    }
    try {
        getlegacyLoanDao().createOrUpdate(this);
    } catch (PersistenceException e) {
        throw new AccountException(e);
    }
}
Also used : Money(org.mifos.framework.util.helpers.Money) AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) AccountException(org.mifos.accounts.exceptions.AccountException) PersistenceException(org.mifos.framework.exceptions.PersistenceException)

Example 97 with AccountException

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

the class LoanBO method updateAccountActivity.

@Override
protected void updateAccountActivity(final Money principal, final Money interest, final Money fee, final Money penalty, final Short personnelId, final String description) throws AccountException {
    try {
        PersonnelBO personnel = legacyPersonnelDao.getPersonnel(personnelId);
        LoanActivityEntity loanActivity = new LoanActivityEntity(this, personnel, description, principal, loanSummary.getOriginalPrincipal().subtract(loanSummary.getPrincipalPaid()), interest, loanSummary.getOriginalInterest().subtract(loanSummary.getInterestPaid()), fee, loanSummary.getOriginalFees().subtract(loanSummary.getFeesPaid()), penalty, loanSummary.getOriginalPenalty().subtract(loanSummary.getPenaltyPaid()), DateUtils.getCurrentDateWithoutTimeStamp());
        this.addLoanActivity(loanActivity);
    } catch (PersistenceException e) {
        throw new AccountException(e);
    }
}
Also used : AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) PersistenceException(org.mifos.framework.exceptions.PersistenceException)

Example 98 with AccountException

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

the class SavingsServiceFacadeWebTier method withdraw.

@Override
public PaymentDto withdraw(SavingsWithdrawalDto savingsWithdrawal, boolean inTransaction) {
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);
    SavingsBO savingsAccount = this.savingsDao.findById(savingsWithdrawal.getSavingsId());
    try {
        personnelDao.checkAccessPermission(userContext, savingsAccount.getOfficeId(), savingsAccount.getCustomer().getLoanOfficerId());
    } catch (AccountException e) {
        throw new MifosRuntimeException(e.getMessage(), e);
    }
    savingsAccount.updateDetails(userContext);
    PersonnelBO createdBy = this.personnelDao.findPersonnelById(Short.valueOf((short) user.getUserId()));
    CustomerBO customer = this.customerDao.findCustomerById(savingsWithdrawal.getCustomerId().intValue());
    Money totalAmount = new Money(savingsAccount.getCurrency(), BigDecimal.valueOf(savingsWithdrawal.getAmount()));
    PaymentData payment = PaymentData.createPaymentData(totalAmount, createdBy, savingsWithdrawal.getModeOfPayment().shortValue(), savingsWithdrawal.getDateOfWithdrawal().toDateMidnight().toDate());
    if (savingsWithdrawal.getDateOfReceipt() != null) {
        payment.setReceiptDate(savingsWithdrawal.getDateOfReceipt().toDateMidnight().toDate());
    }
    payment.setReceiptNum(savingsWithdrawal.getReceiptId());
    payment.setCustomer(customer);
    LocalDate dateOfWithdrawal = new LocalDate(payment.getTransactionDate());
    if (withdrawalMakesBalanceNegativeOnDate(savingsAccount, payment.getTotalAmount(), dateOfWithdrawal)) {
        throw new BusinessRuleException("errors.insufficentbalance", new String[] { savingsAccount.getGlobalAccountNum() });
    }
    try {
        if (!inTransaction) {
            this.transactionHelper.startTransaction();
        }
        this.transactionHelper.beginAuditLoggingFor(savingsAccount);
        AccountPaymentEntity paymentEntity = savingsAccount.withdraw(payment, false);
        this.savingsDao.save(savingsAccount);
        Date lastIntPostDate = savingsAccount.getLastIntPostDate();
        if (lastIntPostDate != null && DateUtils.dateFallsOnOrBeforeDate(payment.getTransactionDate(), lastIntPostDate)) {
            this.recalculateInterestPostings(savingsAccount.getAccountId(), new LocalDate(payment.getTransactionDate()));
        }
        // commit
        if (inTransaction) {
            this.transactionHelper.flushSession();
        } else {
            this.transactionHelper.commitTransaction();
        }
        return paymentEntity.toDto();
    } catch (AccountException e) {
        if (!inTransaction) {
            this.transactionHelper.rollbackTransaction();
        }
        throw new BusinessRuleException(e.getKey(), e);
    } finally {
        if (!inTransaction) {
            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) UserContext(org.mifos.security.util.UserContext) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) 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) 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) CustomerBO(org.mifos.customers.business.CustomerBO) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 99 with AccountException

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

the class SaveCollectionSheetAssembler method buildModelForSavingsAccounts.

private void buildModelForSavingsAccounts(final List<SaveCollectionSheetCustomerSavingDto> saveCollectionSheetCustomerSavings, final AccountPaymentEntity payment, final List<String> failedSavingsDepositAccountNums, final List<String> failedSavingsWithdrawalNums, final Integer customerId, final List<SavingsBO> savingsList) {
    if (null != saveCollectionSheetCustomerSavings && saveCollectionSheetCustomerSavings.size() > 0) {
        final String receiptNumber = payment.getReceiptNumber();
        final Date receiptDate = payment.getReceiptDate();
        final PaymentTypeEntity paymentType = payment.getPaymentType();
        final Date paymentDate = payment.getPaymentDate();
        final PersonnelBO user = payment.getCreatedByUser();
        for (SaveCollectionSheetCustomerSavingDto saveCollectionSheetCustomerSaving : saveCollectionSheetCustomerSavings) {
            final BigDecimal amountToDeposit = saveCollectionSheetCustomerSaving.getTotalDeposit();
            final BigDecimal amountToWithdraw = saveCollectionSheetCustomerSaving.getTotalWithdrawal();
            Boolean isDeposit = isPositiveAmountEntered(amountToDeposit);
            Boolean isWithdrawal = isPositiveAmountEntered(amountToWithdraw);
            if (isDeposit || isWithdrawal) {
                boolean storeAccountForSavingLater = false;
                final CustomerBO payingCustomer = customerDao.findCustomerById(customerId);
                SavingsBO account = savingsDao.findById(saveCollectionSheetCustomerSaving.getAccountId());
                if (isDeposit) {
                    final AccountPaymentEntity accountDeposit = new AccountPaymentEntity(account, new Money(Money.getDefaultCurrency(), amountToDeposit.toString()), receiptNumber, receiptDate, paymentType, paymentDate);
                    accountDeposit.setCreatedByUser(user);
                    try {
                        account.deposit(accountDeposit, payingCustomer);
                        storeAccountForSavingLater = true;
                    } catch (AccountException e) {
                        logger.warn("Savings deposit on account [" + account.getAccountId() + "] failed. Account changes will not be persisted due to: " + e.getMessage());
                        failedSavingsDepositAccountNums.add(account.getAccountId().toString());
                    }
                }
                if (isWithdrawal) {
                    final AccountPaymentEntity accountWithdrawal = new AccountPaymentEntity(account, new Money(Money.getDefaultCurrency(), amountToWithdraw.toString()), receiptNumber, receiptDate, paymentType, paymentDate);
                    accountWithdrawal.setCreatedByUser(user);
                    try {
                        account.withdraw(accountWithdrawal, payingCustomer);
                        storeAccountForSavingLater = true;
                    } catch (AccountException e) {
                        logger.warn("Savings withdrawal on account [" + account.getAccountId() + "] failed. Account changes will not be persisted due to: " + e.getMessage());
                        failedSavingsWithdrawalNums.add(account.getAccountId().toString());
                    }
                }
                if (storeAccountForSavingLater) {
                    if (!savingsList.contains(account)) {
                        savingsList.add(account);
                    }
                } else {
                    StaticHibernateUtil.getSessionTL().evict(account);
                }
            }
        }
    }
}
Also used : AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) PaymentTypeEntity(org.mifos.application.master.business.PaymentTypeEntity) Money(org.mifos.framework.util.helpers.Money) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) CustomerBO(org.mifos.customers.business.CustomerBO)

Example 100 with AccountException

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

the class SaveCollectionSheetAssembler method loanAccountAssemblerFromDto.

public List<LoanBO> loanAccountAssemblerFromDto(final List<SaveCollectionSheetCustomerDto> saveCollectionSheetCustomers, final AccountPaymentEntity payment, final List<String> failedLoanDisbursementAccountNumbers, final List<String> failedLoanRepaymentAccountNumbers, Short paymentTypeIdForFees) {
    final List<LoanBO> loans = new ArrayList<LoanBO>();
    Map<Integer, AccountPaymentEntity> groupLoanAccountParentsPayments = new HashMap<Integer, AccountPaymentEntity>();
    for (SaveCollectionSheetCustomerDto saveCollectionSheetCustomer : saveCollectionSheetCustomers) {
        if (saveCollectionSheetCustomer.getSaveCollectionSheetCustomerLoans() != null && saveCollectionSheetCustomer.getSaveCollectionSheetCustomerLoans().size() > 0) {
            for (SaveCollectionSheetCustomerLoanDto saveCollectionSheetCustomerLoan : saveCollectionSheetCustomer.getSaveCollectionSheetCustomerLoans()) {
                final Integer accountId = saveCollectionSheetCustomerLoan.getAccountId();
                LoanBO account = findLoanAccountById(accountId);
                final String globalAccountNum = account.getGlobalAccountNum();
                final BigDecimal disbursalAmount = saveCollectionSheetCustomerLoan.getTotalDisbursement();
                if (null != disbursalAmount && disbursalAmount.compareTo(BigDecimal.ZERO) > 0) {
                    try {
                        final AccountPaymentEntity accountDisbursalPayment = new AccountPaymentEntity(account, new Money(account.getCurrency(), disbursalAmount.toString()), payment.getReceiptNumber(), payment.getReceiptDate(), payment.getPaymentType(), payment.getPaymentDate());
                        accountDisbursalPayment.setCreatedByUser(payment.getCreatedByUser());
                        Integer transferAccountId = (payment.getOtherTransferPayment() == null || payment.getOtherTransferPayment().getAccount() == null || payment.getOtherTransferPayment().getAccount().getAccountId() == null) ? null : payment.getOtherTransferPayment().getAccount().getAccountId();
                        account.disburseLoan(accountDisbursalPayment, paymentTypeIdForFees, transferAccountId);
                        loans.add(account);
                    } catch (AccountException ae) {
                        logger.warn("Disbursal of loan on account [" + globalAccountNum + "] failed. Account changes will not be persisted due to: " + ae.getMessage());
                        failedLoanDisbursementAccountNumbers.add(globalAccountNum);
                        StaticHibernateUtil.getSessionTL().evict(account);
                    } catch (PersistenceException e) {
                        logger.warn("Disbursal of loan on account [" + globalAccountNum + "] failed. Account changes will not be persisted due to: " + e.getMessage());
                        failedLoanDisbursementAccountNumbers.add(globalAccountNum);
                        StaticHibernateUtil.getSessionTL().evict(account);
                    }
                } else {
                    final BigDecimal loanPaymentAmount = saveCollectionSheetCustomerLoan.getTotalLoanPayment();
                    if (loanPaymentAmount != null && loanPaymentAmount.compareTo(BigDecimal.ZERO) > 0) {
                        try {
                            final PaymentData paymentData = getCustomerAccountPaymentDataView(new Money(account.getCurrency(), loanPaymentAmount.toString()), payment);
                            AccountPaymentEntity paymentEntity = account.applyPayment(paymentData);
                            if (account.isGroupLoanAccountParent()) {
                                groupLoanAccountParentsPayments.put(account.getAccountId(), paymentEntity);
                            } else if (account.isGroupLoanAccountMember()) {
                                paymentEntity.setParentPaymentId(groupLoanAccountParentsPayments.get(account.getParentAccount().getAccountId()));
                            }
                            loans.add(account);
                        } catch (AccountException ae) {
                            logger.warn("Loan repayment on account [" + globalAccountNum + "] failed. Account changes will not be persisted due to: " + ae.getMessage());
                            failedLoanRepaymentAccountNumbers.add(globalAccountNum);
                            StaticHibernateUtil.getSessionTL().evict(account);
                        }
                    }
                }
            }
        }
    }
    return loans;
}
Also used : PaymentData(org.mifos.accounts.util.helpers.PaymentData) HashMap(java.util.HashMap) LoanBO(org.mifos.accounts.loan.business.LoanBO) ArrayList(java.util.ArrayList) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) BigDecimal(java.math.BigDecimal) Money(org.mifos.framework.util.helpers.Money) AccountException(org.mifos.accounts.exceptions.AccountException) PersistenceException(org.mifos.framework.exceptions.PersistenceException)

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