Search in sources :

Example 91 with AccountException

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

the class LoanBO method makeEarlyRepayment.

public void makeEarlyRepayment(final AccountPaymentDto paymentDto, final Short personnelId, boolean waiveInterest, Money interestDue, Integer savingsPaymentId, AccountPaymentEntity parentPayment) throws AccountException {
    try {
        PersonnelBO currentUser = legacyPersonnelDao.getPersonnel(personnelId);
        this.setUpdatedBy(personnelId);
        this.setUpdatedDate(paymentDto.getTransactionDate());
        AccountPaymentEntity accountPaymentEntity;
        if (this.isGroupLoanAccount() && null != this.getParentAccount()) {
            accountPaymentEntity = new AccountPaymentEntity(this, new Money(getCurrency(), paymentDto.getTotalAmount()), paymentDto.getReceiptNumber(), paymentDto.getReceiptDate(), getPaymentTypeEntity(Short.valueOf(paymentDto.getPaymentTypeId())), paymentDto.getTransactionDate(), parentPayment);
        } else {
            accountPaymentEntity = new AccountPaymentEntity(this, new Money(getCurrency(), paymentDto.getTotalAmount()), paymentDto.getReceiptNumber(), paymentDto.getReceiptDate(), getPaymentTypeEntity(Short.valueOf(paymentDto.getPaymentTypeId())), paymentDto.getTransactionDate());
        }
        if (savingsPaymentId != null) {
            AccountPaymentEntity withdrawal = legacyAccountDao.findPaymentById(savingsPaymentId);
            accountPaymentEntity.setOtherTransferPayment(withdrawal);
        }
        addAccountPayment(accountPaymentEntity);
        makeEarlyRepaymentForArrears(accountPaymentEntity, AccountConstants.PAYMENT_RCVD, AccountActionTypes.LOAN_REPAYMENT, currentUser);
        makeEarlyRepaymentForNextInstallment(currentUser, accountPaymentEntity, waiveInterest, interestDue);
        makeEarlyRepaymentForFutureInstallments(accountPaymentEntity, AccountConstants.PAYMENT_RCVD, AccountActionTypes.LOAN_REPAYMENT, currentUser);
        if (getPerformanceHistory() != null) {
            getPerformanceHistory().setNoOfPayments(getPerformanceHistory().getNoOfPayments() + 1);
        }
        LoanActivityEntity loanActivity = buildLoanActivity(accountPaymentEntity.getAccountTrxns(), currentUser, AccountConstants.LOAN_REPAYMENT, paymentDto.getTransactionDate());
        addLoanActivity(loanActivity);
        buildFinancialEntries(accountPaymentEntity.getAccountTrxns());
        AccountStateEntity newAccountState = legacyMasterDao.getPersistentObject(AccountStateEntity.class, AccountStates.LOANACC_OBLIGATIONSMET);
        addAccountStatusChangeHistory(new AccountStatusChangeHistoryEntity(getAccountState(), newAccountState, legacyPersonnelDao.getPersonnel(personnelId), this));
        setAccountState(legacyMasterDao.getPersistentObject(AccountStateEntity.class, AccountStates.LOANACC_OBLIGATIONSMET));
        changeStateForAllFees(FeeStatus.INACTIVE);
        setClosedDate(paymentDto.getTransactionDate());
        // Client performance entry
        updateCustomerHistoryOnRepayment();
        this.delete(loanArrearsAgingEntity);
        loanArrearsAgingEntity = null;
        // GLIM
        if (this.isGroupLoanAccountParent()) {
            for (Entry<String, AmountWithInterest> entry : paymentDto.getMemberNumWithAmount().entrySet()) {
                AccountPaymentDto memberPayment = new AccountPaymentDto(entry.getValue().getAmount(), paymentDto.getTransactionDate(), paymentDto.getReceiptNumber(), paymentDto.getReceiptDate(), paymentDto.getPaymentTypeId());
                legacyLoanDao.getAccount(Integer.valueOf(entry.getKey())).makeEarlyRepayment(memberPayment, personnelId, waiveInterest, new Money(this.getCurrency(), entry.getValue().getInterest()), null, accountPaymentEntity);
            }
        } else if (hasMemberAccounts() && !this.isGroupLoanAccount()) {
            for (LoanBO memberAccount : this.memberAccounts) {
                BigDecimal fraction = memberAccount.calcFactorOfEntireLoan();
                paymentDto.setTotalAmount(new BigDecimal(paymentDto.getTotalAmount()).divide(fraction, RoundingMode.HALF_UP).doubleValue());
                memberAccount.makeEarlyRepayment(paymentDto, personnelId, waiveInterest, interestDue, null, null);
            }
        }
        this.legacyAccountDao.createOrUpdate(accountPaymentEntity);
        this.legacyAccountDao.createOrUpdate(this);
    } catch (PersistenceException e) {
        throw new AccountException(e);
    }
}
Also used : AmountWithInterest(org.mifos.dto.domain.AccountPaymentDto.AmountWithInterest) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) AccountStateEntity(org.mifos.accounts.business.AccountStateEntity) BigDecimal(java.math.BigDecimal) Money(org.mifos.framework.util.helpers.Money) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) PersistenceException(org.mifos.framework.exceptions.PersistenceException) AccountStatusChangeHistoryEntity(org.mifos.accounts.business.AccountStatusChangeHistoryEntity) AccountPaymentDto(org.mifos.dto.domain.AccountPaymentDto)

Example 92 with AccountException

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

the class LoanBO method buildLoanMeeting.

private MeetingBO buildLoanMeeting(final MeetingBO customerMeeting, final MeetingBO loanOfferingMeeting, final Date disbursementDate) throws AccountException {
    // this is called from 'proper constructor' only if LSIM is disabled
    if (customerMeeting != null && loanOfferingMeeting != null && customerMeeting.hasSameRecurrenceAs(loanOfferingMeeting) && customerMeeting.recursOnMultipleOf(loanOfferingMeeting)) {
        RecurrenceType meetingFrequency = customerMeeting.getMeetingDetails().getRecurrenceTypeEnum();
        MeetingType meetingType = MeetingType.fromInt(customerMeeting.getMeetingType().getMeetingTypeId());
        Short recurAfter = loanOfferingMeeting.getMeetingDetails().getRecurAfter();
        try {
            MeetingBO meetingToReturn;
            if (meetingFrequency.equals(RecurrenceType.MONTHLY)) {
                if (customerMeeting.isMonthlyOnDate()) {
                    meetingToReturn = new MeetingBO(customerMeeting.getMeetingDetails().getDayNumber(), recurAfter, disbursementDate, meetingType, customerMeeting.getMeetingPlace());
                } else {
                    meetingToReturn = new MeetingBO(customerMeeting.getMeetingDetails().getWeekDay(), customerMeeting.getMeetingDetails().getWeekRank(), recurAfter, disbursementDate, meetingType, customerMeeting.getMeetingPlace());
                }
            } else if (meetingFrequency.equals(RecurrenceType.WEEKLY)) {
                meetingToReturn = new MeetingBO(customerMeeting.getMeetingDetails().getMeetingRecurrence().getWeekDayValue(), recurAfter, disbursementDate, meetingType, customerMeeting.getMeetingPlace());
            } else {
                meetingToReturn = new MeetingBO(meetingFrequency, recurAfter, disbursementDate, meetingType);
            }
            return meetingToReturn;
        } catch (MeetingException me) {
            throw new AccountException(me);
        }
    }
    throw new AccountException(AccountExceptionConstants.CHANGEINLOANMEETING);
}
Also used : MeetingException(org.mifos.application.meeting.exceptions.MeetingException) AccountException(org.mifos.accounts.exceptions.AccountException) MeetingBO(org.mifos.application.meeting.business.MeetingBO) RecurrenceType(org.mifos.application.meeting.util.helpers.RecurrenceType) MeetingType(org.mifos.application.meeting.util.helpers.MeetingType)

Example 93 with AccountException

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

the class LoanBO method waiveChargesFromMemberAccounts.

public void waiveChargesFromMemberAccounts(String chargeType) {
    for (LoanBO member : getMemberAccounts()) {
        List<AccountActionDateEntity> memberAccountActionDateList = member.getApplicableIdsForNextInstallmentAndArrears();
        LoanScheduleEntity memberAccountActionDateEntity = (LoanScheduleEntity) memberAccountActionDateList.get(memberAccountActionDateList.size() - 1);
        Money principal = new Money(getCurrency());
        Money interest = new Money(getCurrency());
        Money fee = new Money(getCurrency());
        Money penalty = new Money(getCurrency());
        if (chargeType == LoanConstants.FEE_WAIVED) {
            fee = memberAccountActionDateEntity.waiveFeeCharges();
            member.updateTotalFeeAmount(fee);
        } else if (chargeType == LoanConstants.PENALTY_WAIVED) {
            penalty = memberAccountActionDateEntity.waivePenaltyCharges();
            member.updateTotalPenaltyAmount(penalty);
        }
        try {
            member.updateAccountActivity(principal, interest, fee, penalty, userContext.getId(), chargeType);
        } catch (AccountException e) {
            throw new BusinessRuleException(e.getKey());
        }
    }
}
Also used : AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) Money(org.mifos.framework.util.helpers.Money) BusinessRuleException(org.mifos.service.BusinessRuleException) AccountException(org.mifos.accounts.exceptions.AccountException)

Example 94 with AccountException

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

the class LoanBO method adjustLastPaymentForMember.

private void adjustLastPaymentForMember(AccountPaymentEntity parentPayment, final String adjustmentComment, PersonnelBO loggedInUser) throws AccountException {
    if (isAdjustPossibleOnLastTrxn()) {
        logger.debug("Adjustment is possible hence attempting to adjust.");
        AccountPaymentEntity lastPmntToBeAdjusted = getLastPmntToBeAdjusted();
        if (lastPmntToBeAdjusted.hasParentPayment() && lastPmntToBeAdjusted.equals(parentPayment)) {
            adjustPayment(lastPmntToBeAdjusted, loggedInUser, adjustmentComment);
        }
    } 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 : AccountException(org.mifos.accounts.exceptions.AccountException) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity)

Example 95 with AccountException

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

the class LoanBO method applyMifos5722Fix.

public void applyMifos5722Fix() throws AccountException, PersistenceException {
    if (!validateNoOfInstallments(getMemberAccounts().iterator().next()))
        return;
    PersonnelBO personnel = legacyPersonnelDao.getPersonnel((short) 1);
    UserContext userContext = new UserContext();
    userContext.setId(PersonnelConstants.SYSTEM_USER);
    String comment = "Mifos-5722";
    //clear accounts from previous payments
    for (LoanBO memberAccount : getMemberAccounts()) {
        memberAccount.setUserContext(userContext);
        memberAccount.setAccountState(getAccountState());
        for (AccountPaymentEntity payment : memberAccount.getAccountPayments()) {
            memberAccount.adjustPayment(payment, personnel, comment);
        }
    }
    List<LoanScheduleEntity> parentInstallments = getLoanInstallments();
    for (LoanBO memberAccount : getMemberAccounts()) {
        List<LoanScheduleEntity> memberInstallments = memberAccount.getLoanInstallments();
        for (int i = 0; i < parentInstallments.size(); i++) {
            LoanScheduleEntity parentInstallment = parentInstallments.get(i);
            LoanScheduleEntity memberInstallment = memberInstallments.get(i);
            //remove fees and penalties from installment
            memberInstallment.removeAllFees();
            memberInstallment.removeAllPenalties();
            equalizeMiscFeesAndPenaltiesOnInstallments(parentInstallment, memberInstallment, memberAccount.calcFactorOfEntireLoan());
        }
        //remove fees and penalties from account
        while (memberAccount.getAccountFeesIncludingInactiveFees().iterator().hasNext()) {
            AccountFeesEntity fee = memberAccount.getAccountFeesIncludingInactiveFees().iterator().next();
            memberAccount.getAccountFeesIncludingInactiveFees().remove(fee);
        }
        while (memberAccount.getAccountPenaltiesIncludingInactivePenalties().iterator().hasNext()) {
            AccountPenaltiesEntity penalty = memberAccount.getAccountPenaltiesIncludingInactivePenalties().iterator().next();
            memberAccount.getAccountPenaltiesIncludingInactivePenalties().remove(penalty);
        }
    }
    //apply fees and penalties
    Set<AccountFeesEntity> fees = getAccountFees();
    Set<AccountPenaltiesEntity> penalties = getAccountPenalties();
    for (LoanBO memberAccount : getMemberAccounts()) {
        for (AccountFeesEntity fee : fees) {
            try {
                if (fee.getFees().getFeeType() == RateAmountFlag.RATE) {
                    RateFeeBO fbo = getFeeDao().findRateFeeById(fee.getFees().getFeeId());
                    if (fbo != null) {
                        memberAccount.applyChargeMifos5722(fee.getFees().getFeeId(), fbo.getRate());
                    }
                } else {
                    memberAccount.applyChargeMifos5722(fee.getFees().getFeeId(), fee.getFeeAmount() / memberAccount.calcFactorOfEntireLoan().doubleValue());
                }
            } catch (AccountException e) {
                e.printStackTrace();
            }
            for (AccountPenaltiesEntity penalty : penalties) {
                memberAccount.addAccountPenalty(penalty);
            }
        }
    }
    applyAllPenaltiesToMemberAccounts();
    List<AccountPaymentEntity> parentPayments = getAccountPayments();
    BigDecimal currentAmount = BigDecimal.ZERO;
    for (int i = parentPayments.size() - 2; i >= 0; i--) {
        AccountPaymentEntity currentPayment = parentPayments.get(i);
        currentAmount = currentAmount.add(currentPayment.getAmount().getAmount());
        BigDecimal currentProportion = findNumberOfInstallmentsPaidByAmount(currentAmount);
        PaymentData paymentData = new PaymentData(currentPayment.getAmount(), personnel, currentPayment.getPaymentType().getId(), currentPayment.getPaymentDate());
        applyPaymentToMemberAccounts(paymentData, currentProportion);
    }
}
Also used : RateFeeBO(org.mifos.accounts.fees.business.RateFeeBO) AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) PaymentData(org.mifos.accounts.util.helpers.PaymentData) UserContext(org.mifos.security.util.UserContext) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) BigDecimal(java.math.BigDecimal) AccountException(org.mifos.accounts.exceptions.AccountException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) AccountFeesEntity(org.mifos.accounts.business.AccountFeesEntity)

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