Search in sources :

Example 1 with SavingsAdjustmentDto

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

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

the class SavingsAccountAdjustmentAndInterestCalculationServiceFacadeIntegrationTest method shouldHaveCorrectBalanceAfterDepositAndAdjustment.

@Test
public void shouldHaveCorrectBalanceAfterDepositAndAdjustment() throws Exception {
    createCenterGroupClientHierarchy(aWeeklyMeeting);
    SavingsOfferingBO savingsProduct = new SavingsProductBuilder().mandatory().withMandatoryAmount("33.0").appliesToClientsOnly().buildForIntegrationTests();
    SavingsBO savingsAccount = new SavingsAccountBuilder().active().withActivationDate(mondayTwoWeeksAgo()).withSavingsProduct(savingsProduct).withCustomer(client).withCreatedBy(IntegrationTestObjectMother.testUser()).withBalanceOf(TestUtils.createMoney("0")).withDepositOn("20", new DateTime()).build();
    IntegrationTestObjectMother.saveSavingsProductAndAssociatedSavingsAccounts(savingsProduct, savingsAccount);
    Long savingsId = Long.valueOf(savingsAccount.getAccountId());
    Double adjustedAmount = Double.valueOf("35");
    String note = "I entered 20 but it should of being 35 which is an overpayment of the mandatory sum.";
    SavingsAdjustmentDto savingsAdjustment = new SavingsAdjustmentDto(savingsId, adjustedAmount, note, savingsAccount.getLastPmnt().getPaymentId(), new LocalDate(savingsAccount.getLastPmnt().getPaymentDate()));
    // exercise test
    this.savingsServiceFacade.adjustTransaction(savingsAdjustment);
    // verification
    savingsAccount = IntegrationTestObjectMother.findSavingsAccountById(savingsId);
    assertThat(savingsAccount.getSavingsBalance(), is(TestUtils.createMoney("35")));
}
Also used : SavingsProductBuilder(org.mifos.domain.builders.SavingsProductBuilder) SavingsAdjustmentDto(org.mifos.dto.domain.SavingsAdjustmentDto) SavingsOfferingBO(org.mifos.accounts.productdefinition.business.SavingsOfferingBO) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) SavingsAccountBuilder(org.mifos.domain.builders.SavingsAccountBuilder) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with SavingsAdjustmentDto

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

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

the class SavingsAccountRESTController method applyAdjustment.

@RequestMapping(value = "account/savings/num-{globalAccountNum}/adjustment", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> applyAdjustment(@PathVariable String globalAccountNum, @RequestParam BigDecimal amount, @RequestParam String note, @RequestParam(required = false) Integer paymentId) throws Exception {
    validateAmount(amount);
    validateNote(note);
    SavingsBO savingsBO = savingsDao.findBySystemId(globalAccountNum);
    new SavingsPersistence().initialize(savingsBO);
    Integer accountId = savingsBO.getAccountId();
    Long savingsId = Long.valueOf(accountId.toString());
    SavingsAdjustmentDto savingsAdjustment = new SavingsAdjustmentDto(savingsId, amount.doubleValue(), note, (paymentId == null) ? savingsBO.getLastPmnt().getPaymentId() : paymentId, new LocalDate(savingsBO.getLastPmnt().getPaymentDate()));
    Money balanceBeforePayment = savingsBO.getSavingsBalance();
    this.savingsServiceFacade.adjustTransaction(savingsAdjustment);
    MifosUser user = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    DateTime today = new DateTime();
    CustomerBO client = savingsBO.getCustomer();
    Map<String, String> map = new HashMap<String, String>();
    map.put("status", "success");
    map.put("clientName", client.getDisplayName());
    map.put("clientNumber", client.getGlobalCustNum());
    map.put("savingsDisplayName", savingsBO.getSavingsOffering().getPrdOfferingName());
    map.put("adjustmentDate", today.toLocalDate().toString());
    map.put("adjustmentTime", today.toLocalTime().toString());
    map.put("adjustmentAmount", savingsBO.getLastPmnt().getAmount().toString());
    map.put("adjustmentMadeBy", personnelDao.findPersonnelById((short) user.getUserId()).getDisplayName());
    map.put("balanceBeforeAdjustment", balanceBeforePayment.toString());
    map.put("balanceAfterAdjustment", savingsBO.getSavingsBalance().toString());
    map.put("note", note);
    return map;
}
Also used : SavingsPersistence(org.mifos.accounts.savings.persistence.SavingsPersistence) HashMap(java.util.HashMap) SavingsAdjustmentDto(org.mifos.dto.domain.SavingsAdjustmentDto) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) MifosUser(org.mifos.security.MifosUser) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) Money(org.mifos.framework.util.helpers.Money) CustomerBO(org.mifos.customers.business.CustomerBO) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

LocalDate (org.joda.time.LocalDate)4 SavingsAdjustmentDto (org.mifos.dto.domain.SavingsAdjustmentDto)4 SavingsBO (org.mifos.accounts.savings.business.SavingsBO)3 HashMap (java.util.HashMap)2 DateTime (org.joda.time.DateTime)2 AccountBO (org.mifos.accounts.business.AccountBO)2 AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)2 AccountException (org.mifos.accounts.exceptions.AccountException)2 SavingsPersistence (org.mifos.accounts.savings.persistence.SavingsPersistence)2 MifosRuntimeException (org.mifos.core.MifosRuntimeException)2 AdjustedPaymentDto (org.mifos.dto.domain.AdjustedPaymentDto)2 Money (org.mifos.framework.util.helpers.Money)2 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)1 Stack (java.util.Stack)1 Test (org.junit.Test)1 LoanBO (org.mifos.accounts.loan.business.LoanBO)1 SavingsOfferingBO (org.mifos.accounts.productdefinition.business.SavingsOfferingBO)1 SavingsApplyAdjustmentActionForm (org.mifos.accounts.savings.struts.actionforms.SavingsApplyAdjustmentActionForm)1 PaymentData (org.mifos.accounts.util.helpers.PaymentData)1