Search in sources :

Example 86 with AccountException

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

the class LoanBO method disburseLoan.

private void disburseLoan(final String receiptNum, final Date transactionDate, final Short paymentTypeId, final PersonnelBO loggedInUser, final Date receiptDate, final Short rcvdPaymentTypeId, final boolean persistChange, final Short paymentTypeIdForFees, Integer accountForTransferId) throws AccountException, PersistenceException {
    if ((this.getState().compareTo(AccountState.LOAN_APPROVED) != 0) && (this.getState().compareTo(AccountState.LOAN_DISBURSED_TO_LOAN_OFFICER) != 0)) {
        throw new AccountException("Loan not in a State to be Disbursed: " + this.getState().toString());
    }
    if (this.getCustomer().isDisbursalPreventedDueToAnyExistingActiveLoansForTheSameProduct(this.getLoanOffering())) {
        throw new AccountException("errors.cannotDisburseLoan.because.otherLoansAreActive");
    }
    try {
        new ProductMixValidator().checkIfProductsOfferingCanCoexist(this);
    } catch (ServiceException e1) {
        throw new AccountException(e1.getMessage());
    }
    addLoanActivity(buildLoanActivity(this.loanAmount, loggedInUser, AccountConstants.LOAN_DISBURSAL, transactionDate));
    // regenerate the installments
    if (!isFixedRepaymentSchedule() && !DateUtils.getDateWithoutTimeStamp(disbursementDate.getTime()).equals(DateUtils.getDateWithoutTimeStamp(transactionDate.getTime()))) {
        final boolean lsimEnabled = new ConfigurationPersistence().isRepaymentIndepOfMeetingEnabled();
        if (lsimEnabled) {
            // QUESTION: does minDays
            final int minDaysInterval = new ConfigurationPersistence().getConfigurationValueInteger(MIN_DAYS_BETWEEN_DISBURSAL_AND_FIRST_REPAYMENT_DAY);
            this.disbursementDate = new DateTime(transactionDate).plusDays(minDaysInterval - 1).toDate();
        } else {
            this.disbursementDate = transactionDate;
        }
        regeneratePaymentSchedule(lsimEnabled, null);
        if (this.isParentGroupLoanAccount()) {
            groupLoanAccountServiceFacade.fixMemberAndParentInstallmentDetails(this.getAccountId());
        }
    }
    this.disbursementDate = transactionDate;
    final AccountStateEntity newState = new AccountStateEntity(AccountState.LOAN_ACTIVE_IN_GOOD_STANDING);
    this.addAccountStatusChangeHistory(new AccountStatusChangeHistoryEntity(this.getAccountState(), newState, loggedInUser, this));
    this.setAccountState(newState);
    //
    // Client performance entry
    updateCustomerHistoryOnDisbursement(this.loanAmount);
    if (getPerformanceHistory() != null) {
        getPerformanceHistory().setLoanMaturityDate(getLastInstallmentAccountAction().getActionDate());
    }
    //
    //
    // build up account payment related data
    AccountPaymentEntity accountPayment = null;
    if (this.isInterestDeductedAtDisbursement()) {
        // the 1st payment is made and creates an initial accountPaymentEntity.
        // This disbursal process carries on with that accountPaymentEntity by updating the 'amount' to the actual
        // disbursed amount.
        accountPayment = payInterestAtDisbursement(receiptNum, transactionDate, rcvdPaymentTypeId, loggedInUser, receiptDate);
        accountPayment.setAmount(this.loanAmount.subtract(accountPayment.getAmount()));
    } else {
        // Disbursal process has to create its own accountPayment taking into account any disbursement fees
        Money feeAmountAtDisbursement = getFeesDueAtDisbursement();
        accountPayment = new AccountPaymentEntity(this, this.loanAmount.subtract(feeAmountAtDisbursement), receiptNum, receiptDate, getPaymentTypeEntity(paymentTypeId), transactionDate);
        accountPayment.setCreatedByUser(loggedInUser);
        if (feeAmountAtDisbursement.isGreaterThanZero()) {
            processFeesAtDisbursement(accountPayment, feeAmountAtDisbursement, paymentTypeIdForFees, accountForTransferId);
        }
    }
    // create trxn entry for disbursal
    final LoanTrxnDetailEntity loanTrxnDetailEntity = new LoanTrxnDetailEntity(accountPayment, AccountActionTypes.DISBURSAL, Short.valueOf("0"), transactionDate, loggedInUser, transactionDate, this.loanAmount, "-", null, this.loanAmount, new Money(getCurrency()), new Money(getCurrency()), new Money(getCurrency()), new Money(getCurrency()), null, null);
    accountPayment.addAccountTrxn(loanTrxnDetailEntity);
    this.addAccountPayment(accountPayment);
    this.buildFinancialEntries(accountPayment.getAccountTrxns());
    if (persistChange) {
        try {
            ApplicationContextProvider.getBean(LegacyAccountDao.class).createOrUpdate(this);
        } catch (PersistenceException e) {
            throw new AccountException(e);
        }
    }
}
Also used : ConfigurationPersistence(org.mifos.config.persistence.ConfigurationPersistence) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) AccountStateEntity(org.mifos.accounts.business.AccountStateEntity) DateTime(org.joda.time.DateTime) Money(org.mifos.framework.util.helpers.Money) ProductMixValidator(org.mifos.accounts.loan.struts.action.validate.ProductMixValidator) AccountException(org.mifos.accounts.exceptions.AccountException) ServiceException(org.mifos.framework.exceptions.ServiceException) LegacyAccountDao(org.mifos.accounts.persistence.LegacyAccountDao) PersistenceException(org.mifos.framework.exceptions.PersistenceException) AccountStatusChangeHistoryEntity(org.mifos.accounts.business.AccountStatusChangeHistoryEntity)

Example 87 with AccountException

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

the class LoanBO method writeOff.

@Override
protected void writeOff(Date transactionDate) throws AccountException {
    try {
        if (!isTrxnDateValid(transactionDate, new CustomerPersistence().getLastMeetingDateForCustomer(getCustomer().getCustomerId()), new ConfigurationPersistence().isRepaymentIndepOfMeetingEnabled())) {
            throw new BusinessRuleException("errors.invalidTxndate");
        }
        Short personnelId = this.getUserContext().getId();
        PersonnelBO currentUser = legacyPersonnelDao.getPersonnel(personnelId);
        this.setUpdatedBy(personnelId);
        this.setUpdatedDate(transactionDate);
        AccountPaymentEntity accountPaymentEntity = new AccountPaymentEntity(this, getEarlyClosureAmount(), null, null, getPaymentTypeEntity(Short.valueOf("1")), transactionDate);
        this.addAccountPayment(accountPaymentEntity);
        makeEarlyRepaymentForArrears(accountPaymentEntity, AccountConstants.LOAN_WRITTEN_OFF, AccountActionTypes.WRITEOFF, currentUser);
        //for past arrears installments writeOff and reschedule are the same as 'make early repayment'
        //but differ in processing for future installments
        makeWriteOffOrReschedulePaymentForFutureInstallments(accountPaymentEntity, AccountConstants.LOAN_WRITTEN_OFF, AccountActionTypes.WRITEOFF, currentUser);
        addLoanActivity(buildLoanActivity(accountPaymentEntity.getAccountTrxns(), currentUser, AccountConstants.LOAN_WRITTEN_OFF, transactionDate));
        buildFinancialEntries(accountPaymentEntity.getAccountTrxns());
        // Client performance entry
        updateCustomerHistoryOnWriteOff();
    } catch (PersistenceException e) {
        throw new AccountException(e);
    }
}
Also used : BusinessRuleException(org.mifos.service.BusinessRuleException) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) ConfigurationPersistence(org.mifos.config.persistence.ConfigurationPersistence) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) PersistenceException(org.mifos.framework.exceptions.PersistenceException) CustomerPersistence(org.mifos.customers.persistence.CustomerPersistence)

Example 88 with AccountException

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

the class LoanBO method reschedule.

@Override
protected void reschedule(Date transactionDate) throws AccountException {
    try {
        if (!isTrxnDateValid(transactionDate, new CustomerPersistence().getLastMeetingDateForCustomer(getCustomer().getCustomerId()), new ConfigurationPersistence().isRepaymentIndepOfMeetingEnabled())) {
            throw new BusinessRuleException("errors.invalidTxndate");
        }
        Short personnelId = this.getUserContext().getId();
        PersonnelBO currentUser = legacyPersonnelDao.getPersonnel(personnelId);
        this.setUpdatedBy(personnelId);
        this.setUpdatedDate(transactionDate);
        AccountPaymentEntity accountPaymentEntity = new AccountPaymentEntity(this, getEarlyClosureAmount(), null, null, getPaymentTypeEntity(Short.valueOf("1")), transactionDate);
        this.addAccountPayment(accountPaymentEntity);
        makeEarlyRepaymentForArrears(accountPaymentEntity, AccountConstants.LOAN_RESCHEDULED, AccountActionTypes.LOAN_RESCHEDULED, currentUser);
        //for past arrears installments writeOff and reschedule are the same as 'make early repayment'
        //but differ in processing for future installments
        makeWriteOffOrReschedulePaymentForFutureInstallments(accountPaymentEntity, AccountConstants.LOAN_RESCHEDULED, AccountActionTypes.LOAN_RESCHEDULED, currentUser);
        addLoanActivity(buildLoanActivity(accountPaymentEntity.getAccountTrxns(), currentUser, AccountConstants.LOAN_RESCHEDULED, transactionDate));
        buildFinancialEntries(accountPaymentEntity.getAccountTrxns());
        // Client performance entry using the same as write off.
        updateCustomerHistoryOnWriteOff();
    } catch (PersistenceException e) {
        throw new AccountException(e);
    }
}
Also used : BusinessRuleException(org.mifos.service.BusinessRuleException) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) ConfigurationPersistence(org.mifos.config.persistence.ConfigurationPersistence) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) PersistenceException(org.mifos.framework.exceptions.PersistenceException) CustomerPersistence(org.mifos.customers.persistence.CustomerPersistence)

Example 89 with AccountException

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

the class LoanBO method removePenalty.

public void removePenalty(Short penaltyId, Short personnelId) throws AccountException {
    List<Short> installmentIds = getApplicableInstallmentIdsForRemovePenalties();
    Money totalPenaltyAmount = Money.zero();
    if (isPenaltyActive(penaltyId)) {
        PenaltyBO penalty = getAccountPenaltyObject(penaltyId);
        if (!installmentIds.isEmpty()) {
            totalPenaltyAmount = totalPenaltyAmount.add(removePenaltyFromLoanScheduleEntity(installmentIds, penaltyId));
            updateTotalPenaltyAmount(totalPenaltyAmount);
        }
        updateAccountPenaltiesEntity(penaltyId);
        String description = penalty.getPenaltyName() + " " + AccountConstants.PENALTIES_REMOVED;
        updateAccountActivity(null, null, totalPenaltyAmount, null, personnelId, description);
        try {
            ApplicationContextProvider.getBean(LegacyAccountDao.class).createOrUpdate(this);
        } catch (PersistenceException e) {
            throw new AccountException(e);
        }
    }
}
Also used : Money(org.mifos.framework.util.helpers.Money) PenaltyBO(org.mifos.accounts.penalties.business.PenaltyBO) AccountException(org.mifos.accounts.exceptions.AccountException) LegacyAccountDao(org.mifos.accounts.persistence.LegacyAccountDao) PersistenceException(org.mifos.framework.exceptions.PersistenceException)

Example 90 with AccountException

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

the class LoanBO method adjustLastPayment.

@Override
public void adjustLastPayment(final String adjustmentComment, PersonnelBO loggedInUser) throws AccountException {
    if (isAdjustPossibleOnLastTrxn()) {
        logger.debug("Adjustment is possible hence attempting to adjust.");
        AccountPaymentEntity lastPmntToBeAdjusted = getLastPmntToBeAdjusted();
        Money equalizingPaymentAmount = lastPmntToBeAdjusted.getAmount();
        short equalizingPaymentTypeId = lastPmntToBeAdjusted.getPaymentType().getId();
        Date equalizingPaymentDate = lastPmntToBeAdjusted.getPaymentDate();
        adjustPayment(lastPmntToBeAdjusted, loggedInUser, adjustmentComment);
        if (hasMemberAccounts()) {
            for (LoanBO memberAccount : this.memberAccounts) {
                memberAccount.setUserContext(this.userContext);
                if (memberAccount.isGroupLoanAccountMember()) {
                    memberAccount.adjustLastPaymentForMember(lastPmntToBeAdjusted, adjustmentComment, loggedInUser);
                } else {
                    memberAccount.adjustLastPayment(adjustmentComment, loggedInUser);
                }
            }
            //MIFOS-5742: equalizing payment made to solve the problem with adjusting very small payment on GLIM account
            PaymentData equalizingPaymentData = new PaymentData(equalizingPaymentAmount, loggedInUser, equalizingPaymentTypeId, equalizingPaymentDate);
            BigDecimal installmentsPaid = findNumberOfPaidInstallments();
            applyPaymentToMemberAccounts(equalizingPaymentData, installmentsPaid);
        }
    } else if (this.parentAccount == null) {
        //MIFOS-5694: if member account has no payments it could mean that payment was made before 2.4.0, remove this condition when MIFOS-5692 is done 
        throw new AccountException(AccountExceptionConstants.CANNOTADJUST);
    }
}
Also used : Money(org.mifos.framework.util.helpers.Money) PaymentData(org.mifos.accounts.util.helpers.PaymentData) AccountException(org.mifos.accounts.exceptions.AccountException) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) Date(java.util.Date) InstallmentDate(org.mifos.accounts.util.helpers.InstallmentDate) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal)

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