Search in sources :

Example 66 with AccountPaymentEntity

use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.

the class WebTierAccountServiceFacade method checkPermissionForAdjustment.

private void checkPermissionForAdjustment(AccountBO accountBO) throws ServiceException {
    AccountPaymentEntity lastPmntToBeAdjusted = accountBO.getLastPmntToBeAdjusted();
    if (lastPmntToBeAdjusted == null)
        return;
    UserContext userContext = accountBO.getUserContext();
    Date lastPaymentDate = lastPmntToBeAdjusted.getPaymentDate();
    PersonnelBO personnel = accountBO.getPersonnel();
    Short personnelId = personnel != null ? personnel.getPersonnelId() : userContext.getId();
    Short officeId = accountBO.getOfficeId();
    accountBusinessService.checkPermissionForAdjustment(AccountTypes.LOAN_ACCOUNT, null, userContext, officeId, personnelId);
    accountBusinessService.checkPermissionForAdjustmentOnBackDatedPayments(lastPaymentDate, userContext, officeId, personnelId);
}
Also used : PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) UserContext(org.mifos.security.util.UserContext) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) Date(java.util.Date) LocalDate(org.joda.time.LocalDate)

Example 67 with AccountPaymentEntity

use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.

the class WebTierAccountServiceFacade method getLastPaymentDate.

private Date getLastPaymentDate(AccountBO account) {
    Date lastPaymentDate = new Date(0);
    AccountPaymentEntity lastPayment = account.findMostRecentNonzeroPaymentByPaymentDate();
    if (lastPayment != null) {
        lastPaymentDate = lastPayment.getPaymentDate();
    }
    return lastPaymentDate;
}
Also used : AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) Date(java.util.Date) LocalDate(org.joda.time.LocalDate)

Example 68 with AccountPaymentEntity

use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.

the class WebTierAccountServiceFacade method applyHistoricalAdjustment.

@Override
public void applyHistoricalAdjustment(String globalAccountNum, Integer paymentId, String adjustmentNote, Short personnelId, AdjustedPaymentDto adjustedPaymentDto) {
    try {
        AccountBO accountBO = accountBusinessService.findBySystemId(globalAccountNum);
        accountBO.setUserContext(getUserContext());
        checkPermissionForAdjustment(accountBO);
        PersonnelBO personnelBO = personnelPersistence.findPersonnelById(personnelId);
        AccountPaymentEntity accountPaymentEntity = accountBO.findPaymentById(paymentId);
        if (accountPaymentEntity == null) {
            throw new AccountException(AccountExceptionConstants.CANNOTADJUST);
        }
        monthClosingServiceFacade.validateTransactionDate(accountPaymentEntity.getPaymentDate());
        PaymentDto otherTransferPayment = accountPaymentEntity.getOtherTransferPaymentDto();
        //flush to avoid proxy casting problems
        transactionHelper.flushAndClearSession();
        transactionHelper.startTransaction();
        Integer newSavingsPaymentId = null;
        if (otherTransferPayment != null) {
            SavingsAdjustmentDto savingsAdjustment = new SavingsAdjustmentDto(otherTransferPayment.getAccountId().longValue(), (adjustedPaymentDto == null) ? 0 : Double.valueOf(adjustedPaymentDto.getAmount()), adjustmentNote, otherTransferPayment.getPaymentId(), (adjustedPaymentDto == null) ? otherTransferPayment.getPaymentDate() : new LocalDate(adjustedPaymentDto.getPaymentDate()));
            PaymentDto newSavingsPayment = this.savingsServiceFacade.adjustTransaction(savingsAdjustment, true);
            newSavingsPaymentId = (newSavingsPayment == null) ? null : newSavingsPayment.getPaymentId();
        }
        //reload after flush & clear
        accountBO = accountBusinessService.findBySystemId(globalAccountNum);
        accountBO.setUserContext(getUserContext());
        AccountPaymentEntity adjustedPayment = null;
        Integer adjustedId;
        Stack<PaymentData> paymentsToBeReapplied = new Stack<PaymentData>();
        Map<Integer, Stack<PaymentData>> memberPaymentsToBeReappliedMap = new HashMap<Integer, Stack<PaymentData>>();
        if (accountBO.isGroupLoanAccount()) {
            for (LoanBO memberAccount : ((LoanBO) accountBO).getMemberAccounts()) {
                Stack<PaymentData> memberPaymentsToBeReapplied = new Stack<PaymentData>();
                memberPaymentsToBeReappliedMap.put(memberAccount.getAccountId(), memberPaymentsToBeReapplied);
            }
        }
        do {
            adjustedPayment = accountBO.getLastPmntToBeAdjusted();
            if (adjustedPayment == null) {
                break;
            }
            adjustedId = adjustedPayment.getPaymentId();
            if (!accountPaymentEntity.getPaymentId().equals(adjustedId)) {
                PersonnelBO paymentCreator = (adjustedPayment.getCreatedByUser() == null) ? personnelBO : adjustedPayment.getCreatedByUser();
                PaymentData paymentData = accountBO.createPaymentData(adjustedPayment.getAmount(), adjustedPayment.getPaymentDate(), adjustedPayment.getReceiptNumber(), adjustedPayment.getReceiptDate(), adjustedPayment.getPaymentType().getId(), paymentCreator);
                paymentData.setOtherTransferPayment(adjustedPayment.getOtherTransferPayment());
                paymentsToBeReapplied.push(paymentData);
                // handling new Group Loan Members payments
                for (AccountPaymentEntity memberAdjustedPayment : adjustedPayment.getMemberPayments()) {
                    PaymentData memberPaymentData = memberAdjustedPayment.getAccount().createPaymentData(memberAdjustedPayment.getAmount(), adjustedPayment.getPaymentDate(), adjustedPayment.getReceiptNumber(), adjustedPayment.getReceiptDate(), adjustedPayment.getPaymentType().getId(), paymentCreator);
                    memberPaymentsToBeReappliedMap.get(memberAdjustedPayment.getAccount().getAccountId()).push(memberPaymentData);
                }
            }
            transactionHelper.flushAndClearSession();
            //reload after flush & clear
            accountBO = accountBusinessService.findBySystemId(globalAccountNum);
            accountBO.setUserContext(getUserContext());
            accountBO.adjustLastPayment(adjustmentNote, personnelBO);
            legacyAccountDao.createOrUpdate(accountBO);
            //adjust New Group Loan member payments
            if (accountBO.isGroupLoanAccount()) {
                for (LoanBO memberAccount : ((LoanBO) accountBO).getMemberAccounts()) {
                    AccountPaymentEntity memberPayment = memberAccount.getLastPmntToBeAdjusted();
                    if (memberPayment.getParentPaymentId() == null) {
                        continue;
                    }
                    memberAccount.setUserContext(getUserContext());
                    memberAccount.adjustLastPayment(adjustmentNote, personnelBO);
                    legacyAccountDao.createOrUpdate(memberAccount);
                }
            }
            transactionHelper.flushSession();
        } while (!accountPaymentEntity.getPaymentId().equals(adjustedId));
        if (adjustedPaymentDto != null) {
            //reapply adjusted payment
            PersonnelBO paymentCreator = (accountPaymentEntity.getCreatedByUser() == null) ? personnelBO : accountPaymentEntity.getCreatedByUser();
            Money amount = new Money(accountBO.getCurrency(), adjustedPaymentDto.getAmount());
            PaymentData paymentData = accountBO.createPaymentData(amount, adjustedPaymentDto.getPaymentDate(), accountPaymentEntity.getReceiptNumber(), accountPaymentEntity.getReceiptDate(), adjustedPaymentDto.getPaymentType(), paymentCreator);
            paymentData.setAdjustment(true);
            //new adjusted savings payment must be tied to this payment
            if (newSavingsPaymentId != null) {
                AccountPaymentEntity newSvngPayment = legacyAccountDao.findPaymentById(newSavingsPaymentId);
                paymentData.setOtherTransferPayment(newSvngPayment);
            }
            accountBO.applyPayment(paymentData);
            legacyAccountDao.createOrUpdate(accountBO);
            transactionHelper.flushSession();
            // handling new Group Loan Members payments
            if (accountBO.isGroupLoanAccount()) {
                for (AdjustedPaymentDto adjustedMemberPayment : adjustedPaymentDto.getMemberPayments()) {
                    AccountBO memberAccount = ((LoanBO) accountBO).findMemberById(adjustedMemberPayment.getAccountId());
                    BigDecimal adjustedMemberPaymentAmount = BigDecimal.ZERO;
                    if (!StringUtils.isBlank(adjustedMemberPayment.getAmount())) {
                        adjustedMemberPaymentAmount = new BigDecimal(adjustedMemberPayment.getAmount());
                    }
                    Money memberAmount = new Money(memberAccount.getCurrency(), adjustedMemberPaymentAmount.toString());
                    PaymentData memberPaymentData = memberAccount.createPaymentData(memberAmount, adjustedPaymentDto.getPaymentDate(), accountPaymentEntity.getReceiptNumber(), accountPaymentEntity.getReceiptDate(), adjustedPaymentDto.getPaymentType(), paymentCreator);
                    memberPaymentData.setParentPayment(accountBO.getLastPmnt());
                    memberAccount.applyPayment(memberPaymentData);
                    legacyAccountDao.createOrUpdate(memberAccount);
                }
            }
        }
        while (!paymentsToBeReapplied.isEmpty()) {
            PaymentData paymentData = paymentsToBeReapplied.pop();
            //avoid lazy loading exception
            if (paymentData.getOtherTransferPayment() != null) {
                legacyAccountDao.updatePayment(paymentData.getOtherTransferPayment());
            }
            accountBO.applyPayment(paymentData);
            legacyAccountDao.createOrUpdate(accountBO);
            transactionHelper.flushSession();
            if (accountBO.isGroupLoanAccount()) {
                for (LoanBO memberAccount : ((LoanBO) accountBO).getMemberAccounts()) {
                    PaymentData memberPaymentData = memberPaymentsToBeReappliedMap.get(memberAccount.getAccountId()).pop();
                    memberPaymentData.setParentPayment(accountBO.getLastPmnt());
                    memberAccount.applyPayment(memberPaymentData);
                    legacyAccountDao.createOrUpdate(memberAccount);
                }
            }
        }
        transactionHelper.commitTransaction();
    } catch (ServiceException e) {
        transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } catch (AccountException e) {
        transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } catch (PersistenceException e) {
        transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } catch (RuntimeException e) {
        transactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } finally {
        transactionHelper.closeSession();
    }
}
Also used : PaymentData(org.mifos.accounts.util.helpers.PaymentData) HashMap(java.util.HashMap) SavingsAdjustmentDto(org.mifos.dto.domain.SavingsAdjustmentDto) LoanBO(org.mifos.accounts.loan.business.LoanBO) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) PaymentDto(org.mifos.dto.domain.PaymentDto) AdjustedPaymentDto(org.mifos.dto.domain.AdjustedPaymentDto) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Stack(java.util.Stack) AccountBO(org.mifos.accounts.business.AccountBO) Money(org.mifos.framework.util.helpers.Money) MifosRuntimeException(org.mifos.core.MifosRuntimeException) AccountException(org.mifos.accounts.exceptions.AccountException) AdjustedPaymentDto(org.mifos.dto.domain.AdjustedPaymentDto) ServiceException(org.mifos.framework.exceptions.ServiceException) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) PersistenceException(org.mifos.framework.exceptions.PersistenceException) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 69 with AccountPaymentEntity

use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.

the class WebTierAccountServiceFacade method applyAdjustment.

@Override
public void applyAdjustment(String globalAccountNum, String adjustmentNote, Short loggedInUser) {
    try {
        AccountBO account = accountBusinessService.findBySystemId(globalAccountNum);
        AccountPaymentEntity lastPayment = account.getLastPmntToBeAdjusted();
        applyHistoricalAdjustment(globalAccountNum, (lastPayment == null) ? null : lastPayment.getPaymentId(), adjustmentNote, loggedInUser, null);
    } catch (ServiceException e) {
        throw new MifosRuntimeException(e);
    }
}
Also used : AccountBO(org.mifos.accounts.business.AccountBO) ServiceException(org.mifos.framework.exceptions.ServiceException) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 70 with AccountPaymentEntity

use of org.mifos.accounts.business.AccountPaymentEntity in project head by mifos.

the class SavingsBO method makePayment.

/**
     * @deprecated for deposits use {@link SavingsBO#deposit(AccountPaymentEntity, Integer)} and withdrawals use
     *             {@link SavingsBO#withdraw(AccountPaymentEntity)}
     */
@Deprecated
@Override
protected AccountPaymentEntity makePayment(final PaymentData paymentData) throws AccountException {
    Money totalAmount = paymentData.getTotalAmount();
    Money enteredAmount = totalAmount;
    Date transactionDate = paymentData.getTransactionDate();
    List<AccountPaymentData> accountPayments = paymentData.getAccountPayments();
    if (paymentData.getCustomer() == null) {
        throw new NullPointerException("Customer in payment data during payment should not be null");
    }
    CustomerBO customer = paymentData.getCustomer();
    AccountPaymentEntity accountPayment = new AccountPaymentEntity(this, totalAmount, paymentData.getReceiptNum(), paymentData.getReceiptDate(), getPaymentTypeEntity(paymentData.getPaymentTypeId()), transactionDate);
    accountPayment.setCreatedByUser(paymentData.getPersonnel());
    accountPayment.setComment(paymentData.getComment());
    // make savings account active if inactive
    if (this.getState().getValue().equals(AccountState.SAVINGS_INACTIVE.getValue())) {
        this.changeStatus(AccountState.SAVINGS_ACTIVE, null, "Account Made Active Due to Payment", paymentData.getPersonnel());
    }
    if (totalAmount.isGreaterThanZero() && paymentData.getAccountPayments().size() <= 0) {
        SavingsTrxnDetailEntity accountTrxn = buildUnscheduledDeposit(accountPayment, totalAmount, paymentData.getPersonnel(), customer, transactionDate);
        accountPayment.addAccountTrxn(accountTrxn);
        addSavingsActivityDetails(buildSavingsActivity(totalAmount, getSavingsBalance(), AccountActionTypes.SAVINGS_DEPOSIT.getValue(), transactionDate, paymentData.getPersonnel()));
        return accountPayment;
    }
    for (AccountPaymentData accountPaymentData : accountPayments) {
        SavingsScheduleEntity accountAction = (SavingsScheduleEntity) getAccountActionDate(accountPaymentData.getInstallmentId(), customer.getCustomerId());
        if (accountAction != null && depositAmountIsInExcess(enteredAmount)) {
            if (accountAction.isPaid()) {
                throw new AccountException("errors.update", new String[] { getGlobalAccountNum() });
            }
            Money depositAmount = new Money(getCurrency());
            PaymentStatus paymentStatus = PaymentStatus.UNPAID;
            if (enteredAmount.isGreaterThanOrEqual(accountAction.getTotalDepositDue())) {
                depositAmount = accountAction.getTotalDepositDue();
                enteredAmount = enteredAmount.subtract(accountAction.getTotalDepositDue());
                paymentStatus = PaymentStatus.PAID;
            } else {
                depositAmount = enteredAmount;
                enteredAmount = new Money(getCurrency());
            }
            if (this.isVoluntary() && depositAmountIsInExcess(depositAmount)) {
                paymentStatus = PaymentStatus.PAID;
            }
            savingsBalance = savingsBalance.add(depositAmount);
            savingsPerformance.setPaymentDetails(depositAmount);
            accountAction.setPaymentDetails(depositAmount, paymentStatus, new java.sql.Date(transactionDate.getTime()));
            Short installmentId = accountAction.getInstallmentId();
            SavingsTrxnDetailEntity accountTrxn = SavingsTrxnDetailEntity.savingsDeposit(accountPayment, customer, this.savingsBalance, depositAmount, paymentData.getPersonnel(), accountAction.getActionDate(), paymentData.getTransactionDate(), paymentData.getTransactionDate(), installmentId);
            accountPayment.addAccountTrxn(accountTrxn);
        }
    }
    if (depositAmountIsInExcess(enteredAmount)) {
        SavingsTrxnDetailEntity accountTrxn = buildUnscheduledDeposit(accountPayment, enteredAmount, paymentData.getPersonnel(), customer, transactionDate);
        accountPayment.addAccountTrxn(accountTrxn);
    }
    addSavingsActivityDetails(buildSavingsActivity(totalAmount, getSavingsBalance(), AccountActionTypes.SAVINGS_DEPOSIT.getValue(), transactionDate, paymentData.getPersonnel()));
    return accountPayment;
}
Also used : AccountPaymentData(org.mifos.accounts.util.helpers.AccountPaymentData) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) Money(org.mifos.framework.util.helpers.Money) AccountException(org.mifos.accounts.exceptions.AccountException) CustomerBO(org.mifos.customers.business.CustomerBO) PaymentStatus(org.mifos.accounts.util.helpers.PaymentStatus)

Aggregations

AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)113 Money (org.mifos.framework.util.helpers.Money)56 Date (java.util.Date)43 LocalDate (org.joda.time.LocalDate)41 Test (org.junit.Test)36 PersonnelBO (org.mifos.customers.personnel.business.PersonnelBO)34 AccountException (org.mifos.accounts.exceptions.AccountException)30 PaymentTypeEntity (org.mifos.application.master.business.PaymentTypeEntity)24 SavingsBO (org.mifos.accounts.savings.business.SavingsBO)23 UserContext (org.mifos.security.util.UserContext)23 ArrayList (java.util.ArrayList)22 AccountTrxnEntity (org.mifos.accounts.business.AccountTrxnEntity)22 MifosRuntimeException (org.mifos.core.MifosRuntimeException)19 PaymentData (org.mifos.accounts.util.helpers.PaymentData)18 CustomerBO (org.mifos.customers.business.CustomerBO)18 AccountBO (org.mifos.accounts.business.AccountBO)16 PersistenceException (org.mifos.framework.exceptions.PersistenceException)16 BigDecimal (java.math.BigDecimal)14 LoanBO (org.mifos.accounts.loan.business.LoanBO)14 Date (java.sql.Date)13