Search in sources :

Example 21 with AccountException

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

the class SavingsApplyAdjustmentAction method adjustLastUserAction.

@TransactionDemarcate(validateAndResetToken = true)
public ActionForward adjustLastUserAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
    request.setAttribute("method", "adjustLastUserAction");
    UserContext uc = (UserContext) SessionUtils.getAttribute(Constants.USER_CONTEXT_KEY, request.getSession());
    SavingsBO savings = (SavingsBO) SessionUtils.getAttribute(Constants.BUSINESS_KEY, request);
    Integer accountId = savings.getAccountId();
    Integer versionNum = savings.getVersionNo();
    savings = savingsDao.findById(accountId);
    // NOTE: initialise so when error occurs when apply adjustment, savings object is correctly initialised for
    // use within Tag library in jsp.
    new SavingsPersistence().initialize(savings);
    checkVersionMismatch(versionNum, savings.getVersionNo());
    savings.setUserContext(uc);
    SessionUtils.setAttribute(Constants.BUSINESS_KEY, savings, request);
    if (savings.getPersonnel() != null) {
        getBizService().checkPermissionForAdjustment(AccountTypes.SAVINGS_ACCOUNT, null, uc, savings.getOffice().getOfficeId(), savings.getPersonnel().getPersonnelId());
    } else {
        getBizService().checkPermissionForAdjustment(AccountTypes.SAVINGS_ACCOUNT, null, uc, savings.getOffice().getOfficeId(), uc.getId());
    }
    SavingsApplyAdjustmentActionForm actionForm = (SavingsApplyAdjustmentActionForm) form;
    if (actionForm.getLastPaymentAmount() == null) {
        throw new MifosRuntimeException("Null payment amount is not allowed");
    }
    // date validation
    Date meetingDate = new CustomerPersistence().getLastMeetingDateForCustomer(savings.getCustomer().getCustomerId());
    boolean repaymentIndependentOfMeetingEnabled = new ConfigurationPersistence().isRepaymentIndepOfMeetingEnabled();
    if (!savings.isTrxnDateValid(actionForm.getTrxnDateLocal().toDateMidnight().toDate(), meetingDate, repaymentIndependentOfMeetingEnabled)) {
        throw new AccountException(AccountConstants.ERROR_INVALID_TRXN);
    }
    Long savingsId = Long.valueOf(accountId.toString());
    Double adjustedAmount = Double.valueOf(actionForm.getLastPaymentAmount());
    String note = actionForm.getNote();
    AccountPaymentEntity adjustedPayment = savings.findPaymentById(actionForm.getPaymentId());
    AccountPaymentEntity otherTransferPayment = adjustedPayment.getOtherTransferPayment();
    try {
        if (otherTransferPayment == null || otherTransferPayment.isSavingsDepositOrWithdrawal()) {
            // regular savings payment adjustment or savings-savings transfer adjustment
            SavingsAdjustmentDto savingsAdjustment = new SavingsAdjustmentDto(savingsId, adjustedAmount, note, actionForm.getPaymentId(), actionForm.getTrxnDateLocal());
            this.savingsServiceFacade.adjustTransaction(savingsAdjustment);
        } else {
            // adjust repayment from savings account
            AccountBO account = adjustedPayment.getOtherTransferPayment().getAccount();
            AdjustedPaymentDto adjustedPaymentDto = new AdjustedPaymentDto(actionForm.getLastPaymentAmount(), actionForm.getTrxnDateLocal().toDateMidnight().toDate(), otherTransferPayment.getPaymentType().getId());
            this.accountServiceFacade.applyHistoricalAdjustment(account.getGlobalAccountNum(), otherTransferPayment.getPaymentId(), note, uc.getId(), adjustedPaymentDto);
        }
    } catch (BusinessRuleException e) {
        throw new AccountException(e.getMessageKey(), e);
    } finally {
        doCleanUp(request);
    }
    return mapping.findForward("account_detail_page");
}
Also used : SavingsPersistence(org.mifos.accounts.savings.persistence.SavingsPersistence) UserContext(org.mifos.security.util.UserContext) ConfigurationPersistence(org.mifos.config.persistence.ConfigurationPersistence) SavingsAdjustmentDto(org.mifos.dto.domain.SavingsAdjustmentDto) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) AccountBO(org.mifos.accounts.business.AccountBO) BusinessRuleException(org.mifos.service.BusinessRuleException) AccountException(org.mifos.accounts.exceptions.AccountException) AdjustedPaymentDto(org.mifos.dto.domain.AdjustedPaymentDto) SavingsApplyAdjustmentActionForm(org.mifos.accounts.savings.struts.actionforms.SavingsApplyAdjustmentActionForm) CustomerPersistence(org.mifos.customers.persistence.CustomerPersistence) MifosRuntimeException(org.mifos.core.MifosRuntimeException) TransactionDemarcate(org.mifos.framework.util.helpers.TransactionDemarcate)

Example 22 with AccountException

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

the class SavingsBOIntegrationTest method testMaxWithdrawAmount.

@Test
public void testMaxWithdrawAmount() throws AccountException, Exception {
    MeetingBO meeting = TestObjectFactory.createMeeting(TestObjectFactory.getNewMeetingForToday(WEEKLY, EVERY_WEEK, CUSTOMER_MEETING));
    center = TestObjectFactory.createWeeklyFeeCenter("Center", meeting);
    group = TestObjectFactory.createWeeklyFeeGroupUnderCenter("Group", CustomerStatus.GROUP_ACTIVE, center);
    client1 = TestObjectFactory.createClient("Client", CustomerStatus.CLIENT_ACTIVE, group);
    savingsOffering = helper.createSavingsOffering("dfasdasd1", "sad1");
    savings = TestObjectFactory.createSavingsAccount("43245434", client1, Short.valueOf("16"), new Date(System.currentTimeMillis()), savingsOffering);
    savings = (SavingsBO) legacyAccountDao.getAccount(savings.getAccountId());
    savings.setSavingsBalance(TestUtils.createMoney("100.0"));
    Money enteredAmount = new Money(currency, "300.0");
    PaymentData paymentData = PaymentData.createPaymentData(enteredAmount, savings.getPersonnel(), Short.valueOf("1"), new Date(System.currentTimeMillis()));
    paymentData.setCustomer(client1);
    paymentData.setReceiptDate(new Date(System.currentTimeMillis()));
    paymentData.setReceiptNum("34244");
    try {
        boolean persist = true;
        savings.withdraw(paymentData, persist);
        Assert.assertTrue("No Exception is thrown. Even amount greater than max withdrawal allowed to be withdrawn", false);
    } catch (AccountException ae) {
        Assert.assertTrue("Exception is thrown. Amount greater than max withdrawal not allowed to be withdrawn", true);
    }
    savings.getAccountPayments().clear();
}
Also used : Money(org.mifos.framework.util.helpers.Money) PaymentData(org.mifos.accounts.util.helpers.PaymentData) SavingsPaymentData(org.mifos.accounts.util.helpers.SavingsPaymentData) AccountException(org.mifos.accounts.exceptions.AccountException) MeetingBO(org.mifos.application.meeting.business.MeetingBO) Date(java.util.Date) Test(org.junit.Test)

Example 23 with AccountException

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

the class LoanBO method removeFeesAssociatedWithUpcomingAndAllKnownFutureInstallments.

/**
     * Remove the fee from all unpaid current or future installments, and update the loan accordingly.
     */
@Override
public final void removeFeesAssociatedWithUpcomingAndAllKnownFutureInstallments(final Short feeId, final Short personnelId) throws AccountException {
    List<Short> installmentIds = getApplicableInstallmentIdsForRemoveFees();
    Money totalFeeAmount;
    if (installmentIds != null && installmentIds.size() != 0 && isFeeActive(feeId)) {
        FeeBO fee = getAccountFeesObject(feeId);
        if (havePaymentsBeenMade() && fee.doesFeeInvolveFractionalAmounts()) {
            throw new AccountException(AccountExceptionConstants.CANT_REMOVE_FEE_EXCEPTION);
        }
        if (fee.isTimeOfDisbursement()) {
            AccountFeesEntity accountFee = getAccountFees(feeId);
            totalFeeAmount = accountFee.getAccountFeeAmount();
            removeAccountFee(accountFee);
            this.delete(accountFee);
        } else {
            totalFeeAmount = updateAccountActionDateEntity(installmentIds, feeId);
            updateAccountFeesEntity(feeId);
        }
        updateTotalFeeAmount(totalFeeAmount);
        String description = fee.getFeeName() + " " + AccountConstants.FEES_REMOVED;
        updateAccountActivity(null, null, totalFeeAmount, null, personnelId, description);
        if (!havePaymentsBeenMade()) {
            LoanScheduleRounderHelper loanScheduleRounderHelper = new DefaultLoanScheduleRounderHelper();
            LoanScheduleRounder loanScheduleInstallmentRounder = getLoanScheduleRounder(loanScheduleRounderHelper);
            List<LoanScheduleEntity> unroundedLoanSchedules = new ArrayList<LoanScheduleEntity>();
            List<LoanScheduleEntity> allExistingLoanSchedules = new ArrayList<LoanScheduleEntity>();
            List<AccountActionDateEntity> installmentsToRound = getInstallmentsToRound();
            for (AccountActionDateEntity installment : installmentsToRound) {
                unroundedLoanSchedules.add((LoanScheduleEntity) installment);
            }
            List<AccountActionDateEntity> allExistingInstallments = this.getAllInstallments();
            for (AccountActionDateEntity installment : allExistingInstallments) {
                allExistingLoanSchedules.add((LoanScheduleEntity) installment);
            }
            List<LoanScheduleEntity> roundedLoanSchedules = loanScheduleInstallmentRounder.round(this.gracePeriodType.asEnum(), this.gracePeriodDuration, this.loanAmount, this.interestType.asEnum(), unroundedLoanSchedules, allExistingLoanSchedules);
        //                applyRounding_v2();
        }
        try {
            ApplicationContextProvider.getBean(LegacyAccountDao.class).createOrUpdate(this);
        } catch (PersistenceException e) {
            throw new AccountException(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DefaultLoanScheduleRounderHelper(org.mifos.clientportfolio.newloan.domain.DefaultLoanScheduleRounderHelper) Money(org.mifos.framework.util.helpers.Money) AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) AccountException(org.mifos.accounts.exceptions.AccountException) LegacyAccountDao(org.mifos.accounts.persistence.LegacyAccountDao) FirstInstallmentRoudingDifferenceLoanScheduleRounder(org.mifos.clientportfolio.newloan.domain.FirstInstallmentRoudingDifferenceLoanScheduleRounder) LoanScheduleRounder(org.mifos.clientportfolio.newloan.domain.LoanScheduleRounder) DefaultLoanScheduleRounder(org.mifos.clientportfolio.newloan.domain.DefaultLoanScheduleRounder) PersistenceException(org.mifos.framework.exceptions.PersistenceException) FeeBO(org.mifos.accounts.fees.business.FeeBO) RateFeeBO(org.mifos.accounts.fees.business.RateFeeBO) DefaultLoanScheduleRounderHelper(org.mifos.clientportfolio.newloan.domain.DefaultLoanScheduleRounderHelper) LoanScheduleRounderHelper(org.mifos.clientportfolio.newloan.domain.LoanScheduleRounderHelper) AccountFeesEntity(org.mifos.accounts.business.AccountFeesEntity)

Example 24 with AccountException

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

the class LoanBO method updateLoan.

public void updateLoan(final Boolean interestDeductedAtDisbursement, final Money loanAmount, final Double interestRate, final Short noOfInstallments, final Date disbursementDate, final Short gracePeriodDuration, final Integer businessActivityId, final String collateralNote, final Integer collateralTypeId, final List<CustomFieldDto> customFields, final boolean isRepaymentIndepOfMeetingEnabled, final MeetingBO newMeetingForRepaymentDay, final FundBO fund) throws AccountException {
    if (interestDeductedAtDisbursement) {
        try {
            if (noOfInstallments <= 1) {
                throw new AccountException(LoanExceptionConstants.INVALIDNOOFINSTALLMENTS);
            }
            setGracePeriodType(legacyMasterDao.findMasterDataEntityWithLocale(GracePeriodTypeEntity.class, GraceType.NONE.getValue()));
        } catch (PersistenceException e) {
            throw new AccountException(e);
        }
    } else {
        setGracePeriodType(getLoanOffering().getGracePeriodType());
    }
    setLoanAmount(loanAmount);
    setInterestRate(interestRate);
    setNoOfInstallments(noOfInstallments);
    setGracePeriodDuration(gracePeriodDuration);
    setInterestDeductedAtDisbursement(interestDeductedAtDisbursement);
    setBusinessActivityId(businessActivityId);
    setCollateralNote(collateralNote);
    setCollateralTypeId(collateralTypeId);
    setFund(fund);
    if (getAccountState().getId().equals(AccountState.LOAN_APPROVED.getValue()) || getAccountState().getId().equals(AccountState.LOAN_DISBURSED_TO_LOAN_OFFICER.getValue()) || getAccountState().getId().equals(AccountState.LOAN_PARTIAL_APPLICATION.getValue()) || getAccountState().getId().equals(AccountState.LOAN_PENDING_APPROVAL.getValue())) {
        // only check the disbursement date if it has changed
        if (disbursementDate != null && !disbursementDate.equals(getDisbursementDate()) && isDisbursementDateLessThanCurrentDate(disbursementDate)) {
            throw new AccountException(LoanExceptionConstants.ERROR_INVALIDDISBURSEMENTDATE);
        }
        setDisbursementDate(disbursementDate);
        regeneratePaymentSchedule(isRepaymentIndepOfMeetingEnabled, newMeetingForRepaymentDay);
    }
    try {
        updateCustomFields(customFields);
    } catch (InvalidDateException ide) {
        throw new AccountException(ide);
    }
    loanSummary.setOriginalPrincipal(loanAmount);
    update();
}
Also used : AccountException(org.mifos.accounts.exceptions.AccountException) InvalidDateException(org.mifos.application.admin.servicefacade.InvalidDateException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) GracePeriodTypeEntity(org.mifos.accounts.productdefinition.business.GracePeriodTypeEntity)

Example 25 with AccountException

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

the class LoanBO method waiveOverdueChargesFromMemberAccounts.

public void waiveOverdueChargesFromMemberAccounts(String chargeType) {
    for (LoanBO member : getMemberAccounts()) {
        List<AccountActionDateEntity> memberAccountActionDateList = member.getApplicableIdsForNextInstallmentAndArrears();
        List<LoanScheduleEntity> overdueMemberAccountActionDateEntities = new ArrayList<LoanScheduleEntity>();
        for (AccountActionDateEntity accountActionDateEntity : memberAccountActionDateList) {
            if (accountActionDateEntity.getActionDate().before(DateUtils.getCurrentDateWithoutTimeStamp())) {
                overdueMemberAccountActionDateEntities.add((LoanScheduleEntity) accountActionDateEntity);
            }
        }
        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) {
            for (LoanScheduleEntity memberAccountActionDateEntity : overdueMemberAccountActionDateEntities) {
                fee = fee.add(memberAccountActionDateEntity.waiveFeeCharges());
            }
            member.updateTotalFeeAmount(fee);
        } else if (chargeType == LoanConstants.PENALTY_WAIVED) {
            for (LoanScheduleEntity memberAccountActionDateEntity : overdueMemberAccountActionDateEntities) {
                penalty = penalty.add(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) ArrayList(java.util.ArrayList)

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