Search in sources :

Example 1 with AdjustedPaymentDto

use of org.mifos.dto.domain.AdjustedPaymentDto 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 2 with AdjustedPaymentDto

use of org.mifos.dto.domain.AdjustedPaymentDto in project head by mifos.

the class WebTierAccountServiceFacade method applyMemberAccountHistoricalAdjustment.

/**
     * adjustment for member account payment has to be handled along with parent account payments and other members payments.
     * This method prepares data and run applyHistoricalAdjustment so adjustment will be made for parent payment context.
     */
@Override
@PreAuthorize("isFullyAuthenticated()")
public void applyMemberAccountHistoricalAdjustment(String memberGlobalAccountNum, Integer memberPaymentId, String adjustmentNote, Short personnelId, AdjustedPaymentDto adjustedPaymentDto) {
    try {
        LoanBO memberAccount = (LoanBO) accountBusinessService.findBySystemId(memberGlobalAccountNum);
        LoanBO parentAccount = memberAccount.getParentAccount();
        if (parentAccount == null) {
            throw new AccountException(LoanExceptionConstants.NO_PARENT_ACCOUNT_EXCEPTION);
        }
        AccountPaymentEntity parentPaymentEntity = memberAccount.findParentPaymentByMemberPaymentId(memberPaymentId);
        List<AdjustedPaymentDto> membersAdjustedPaymentDtoList = new ArrayList<AdjustedPaymentDto>();
        for (AccountPaymentEntity memberPayment : parentPaymentEntity.getMemberPayments()) {
            if (memberPayment.getAccount().getAccountId().equals(memberAccount.getAccountId())) {
                membersAdjustedPaymentDtoList.add(new AdjustedPaymentDto(adjustedPaymentDto.getAmount(), adjustedPaymentDto.getPaymentDate(), adjustedPaymentDto.getPaymentType(), memberAccount.getAccountId()));
            } else {
                membersAdjustedPaymentDtoList.add(new AdjustedPaymentDto(memberPayment.getAmount().getAmount().toString(), adjustedPaymentDto.getPaymentDate(), adjustedPaymentDto.getPaymentType(), memberPayment.getAccount().getAccountId()));
            }
        }
        BigDecimal parentAmountSubstraction = memberAccount.findPaymentById(memberPaymentId).getAmount().getAmount().subtract((new BigDecimal(adjustedPaymentDto.getAmount())));
        String newParentAmount = parentPaymentEntity.getAmount().getAmount().subtract(parentAmountSubstraction).toString();
        AdjustedPaymentDto parentAdjustedPaymentDto = new AdjustedPaymentDto(newParentAmount, adjustedPaymentDto.getPaymentDate(), adjustedPaymentDto.getPaymentType(), parentAccount.getAccountId(), membersAdjustedPaymentDtoList);
        this.applyHistoricalAdjustment(parentAccount.getGlobalAccountNum(), parentPaymentEntity.getPaymentId(), adjustmentNote, personnelId, parentAdjustedPaymentDto);
    } catch (ServiceException e) {
        throw new MifosRuntimeException(e);
    } catch (AccountException e) {
        throw new MifosRuntimeException(e);
    }
}
Also used : AccountException(org.mifos.accounts.exceptions.AccountException) AdjustedPaymentDto(org.mifos.dto.domain.AdjustedPaymentDto) ServiceException(org.mifos.framework.exceptions.ServiceException) LoanBO(org.mifos.accounts.loan.business.LoanBO) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) MifosRuntimeException(org.mifos.core.MifosRuntimeException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with AdjustedPaymentDto

use of org.mifos.dto.domain.AdjustedPaymentDto in project head by mifos.

the class ApplyAdjustment method applyAdjustment.

@TransactionDemarcate(validateAndResetToken = true)
@CloseSession
public ActionForward applyAdjustment(ActionMapping mapping, ActionForm form, HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
    request.setAttribute("method", "applyAdjustment");
    ApplyAdjustmentActionForm appAdjustActionForm = (ApplyAdjustmentActionForm) form;
    Integer paymentId = appAdjustActionForm.getPaymentId();
    checkVersionMismatchForAccountBO(request, appAdjustActionForm);
    UserContext userContext = (UserContext) SessionUtils.getAttribute(Constants.USER_CONTEXT_KEY, request.getSession());
    try {
        if (paymentId == null) {
            accountServiceFacade.applyAdjustment(appAdjustActionForm.getGlobalAccountNum(), appAdjustActionForm.getAdjustmentNote(), userContext.getId());
        } else {
            AdjustedPaymentDto adjustedPaymentDto = appAdjustActionForm.getPaymentData();
            if (appAdjustActionForm.isGroupLoanMember()) {
                accountServiceFacade.applyMemberAccountHistoricalAdjustment(appAdjustActionForm.getGlobalAccountNum(), paymentId, appAdjustActionForm.getAdjustmentNote(), userContext.getId(), adjustedPaymentDto);
            } else {
                accountServiceFacade.applyHistoricalAdjustment(appAdjustActionForm.getGlobalAccountNum(), paymentId, appAdjustActionForm.getAdjustmentNote(), userContext.getId(), adjustedPaymentDto);
            }
        }
    } catch (MifosRuntimeException e) {
        request.setAttribute("method", "previewAdjustment");
        throw (Exception) e.getCause();
    }
    resetActionFormFields(appAdjustActionForm);
    return mapping.findForward("applyadj_success");
}
Also used : ApplyAdjustmentActionForm(org.mifos.accounts.struts.actionforms.ApplyAdjustmentActionForm) AdjustedPaymentDto(org.mifos.dto.domain.AdjustedPaymentDto) UserContext(org.mifos.security.util.UserContext) MifosRuntimeException(org.mifos.core.MifosRuntimeException) CloseSession(org.mifos.framework.util.helpers.CloseSession) TransactionDemarcate(org.mifos.framework.util.helpers.TransactionDemarcate)

Example 4 with AdjustedPaymentDto

use of org.mifos.dto.domain.AdjustedPaymentDto in project head by mifos.

the class ApplyAdjustmentActionForm method getPaymentData.

public AdjustedPaymentDto getPaymentData() throws InvalidDateException {
    AdjustedPaymentDto adjustedPaymentDto = null;
    if (adjustData || isGroupLoanMember()) {
        List<AdjustedPaymentDto> membersAdjustedPaymentDtoList = new ArrayList<AdjustedPaymentDto>();
        for (Map.Entry<Integer, String> member : this.newAmounts.entrySet()) {
            membersAdjustedPaymentDtoList.add(new AdjustedPaymentDto(member.getValue(), getTrxnDate(), Short.parseShort(paymentType), member.getKey()));
        }
        String newAmount = (adjustData) ? amount : BigDecimal.ZERO.toString();
        adjustedPaymentDto = new AdjustedPaymentDto(newAmount, getTrxnDate(), Short.parseShort(paymentType), this.accountId, membersAdjustedPaymentDtoList);
    }
    return adjustedPaymentDto;
}
Also used : AdjustedPaymentDto(org.mifos.dto.domain.AdjustedPaymentDto) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with AdjustedPaymentDto

use of org.mifos.dto.domain.AdjustedPaymentDto 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)

Aggregations

AdjustedPaymentDto (org.mifos.dto.domain.AdjustedPaymentDto)5 MifosRuntimeException (org.mifos.core.MifosRuntimeException)4 AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)3 AccountException (org.mifos.accounts.exceptions.AccountException)3 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LocalDate (org.joda.time.LocalDate)2 AccountBO (org.mifos.accounts.business.AccountBO)2 LoanBO (org.mifos.accounts.loan.business.LoanBO)2 SavingsAdjustmentDto (org.mifos.dto.domain.SavingsAdjustmentDto)2 ServiceException (org.mifos.framework.exceptions.ServiceException)2 TransactionDemarcate (org.mifos.framework.util.helpers.TransactionDemarcate)2 UserContext (org.mifos.security.util.UserContext)2 Date (java.util.Date)1 Map (java.util.Map)1 Stack (java.util.Stack)1 SavingsBO (org.mifos.accounts.savings.business.SavingsBO)1 SavingsPersistence (org.mifos.accounts.savings.persistence.SavingsPersistence)1 SavingsApplyAdjustmentActionForm (org.mifos.accounts.savings.struts.actionforms.SavingsApplyAdjustmentActionForm)1