Search in sources :

Example 1 with SavingsPersistence

use of org.mifos.accounts.savings.persistence.SavingsPersistence in project head by mifos.

the class CustomerServiceImpl method changeStatus.

private void changeStatus(CustomerBO customer, CustomerStatus oldStatus, CustomerStatus newStatus) throws CustomerException {
    Short oldStatusId = oldStatus.getValue();
    Short newStatusId = newStatus.getValue();
    if (customer.isClient()) {
        ClientBO client = (ClientBO) customer;
        if (client.isActiveForFirstTime(oldStatusId, newStatusId)) {
            if (client.getParentCustomer() != null) {
                CustomerHierarchyEntity hierarchy = new CustomerHierarchyEntity(client, client.getParentCustomer());
                client.addCustomerHierarchy(hierarchy);
            }
            CalendarEvent applicableCalendarEvents = holidayDao.findCalendarEventsForThisYearAndNext(customer.getOfficeId());
            List<AccountFeesEntity> accountFees = new ArrayList<AccountFeesEntity>(customer.getCustomerAccount().getAccountFees());
            client.getCustomerAccount().createSchedulesAndFeeSchedulesForFirstTimeActiveCustomer(customer, accountFees, customer.getCustomerMeetingValue(), applicableCalendarEvents, new DateMidnight().toDateTime());
            client.setCustomerActivationDate(new DateTimeService().getCurrentJavaDateTime());
            if (client.getOfferingsAssociatedInCreate() != null) {
                for (ClientInitialSavingsOfferingEntity clientOffering : client.getOfferingsAssociatedInCreate()) {
                    try {
                        SavingsOfferingBO savingsOffering = savingsProductDao.findById(clientOffering.getSavingsOffering().getPrdOfferingId().intValue());
                        if (savingsOffering.isActive()) {
                            List<CustomFieldDto> customerFieldsForSavings = new ArrayList<CustomFieldDto>();
                            client.addAccount(new SavingsBO(client.getUserContext(), savingsOffering, client, AccountState.SAVINGS_ACTIVE, savingsOffering.getRecommendedAmount(), customerFieldsForSavings));
                        }
                    } catch (AccountException pe) {
                        throw new CustomerException(pe);
                    }
                }
            }
            new SavingsPersistence().persistSavingAccounts(client);
            try {
                if (client.getParentCustomer() != null) {
                    List<SavingsBO> savingsList = new CustomerPersistence().retrieveSavingsAccountForCustomer(client.getParentCustomer().getCustomerId());
                    if (client.getParentCustomer().getParentCustomer() != null) {
                        savingsList.addAll(new CustomerPersistence().retrieveSavingsAccountForCustomer(client.getParentCustomer().getParentCustomer().getCustomerId()));
                    }
                    for (SavingsBO savings : savingsList) {
                        savings.setUserContext(client.getUserContext());
                        if (client.getCustomerMeeting().getMeeting() != null) {
                            if (!(savings.getCustomer().getLevel() == CustomerLevel.GROUP && savings.getRecommendedAmntUnit().getId().equals(RecommendedAmountUnit.COMPLETE_GROUP.getValue()))) {
                                DateTime today = new DateTime().toDateMidnight().toDateTime();
                                savings.generateDepositAccountActions(client, client.getCustomerMeeting().getMeeting(), applicableCalendarEvents.getWorkingDays(), applicableCalendarEvents.getHolidays(), today);
                                savings.update();
                            }
                        }
                    }
                }
            } catch (PersistenceException pe) {
                throw new CustomerException(pe);
            } catch (AccountException ae) {
                throw new CustomerException(ae);
            }
        }
    }
}
Also used : CustomerHierarchyEntity(org.mifos.customers.business.CustomerHierarchyEntity) CustomerException(org.mifos.customers.exceptions.CustomerException) SavingsPersistence(org.mifos.accounts.savings.persistence.SavingsPersistence) ClientBO(org.mifos.customers.client.business.ClientBO) CustomFieldDto(org.mifos.dto.domain.CustomFieldDto) ArrayList(java.util.ArrayList) CalendarEvent(org.mifos.calendar.CalendarEvent) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) ClientInitialSavingsOfferingEntity(org.mifos.customers.client.business.ClientInitialSavingsOfferingEntity) DateTime(org.joda.time.DateTime) AccountException(org.mifos.accounts.exceptions.AccountException) DateMidnight(org.joda.time.DateMidnight) SavingsOfferingBO(org.mifos.accounts.productdefinition.business.SavingsOfferingBO) PersistenceException(org.mifos.framework.exceptions.PersistenceException) CustomerPersistence(org.mifos.customers.persistence.CustomerPersistence) AccountFeesEntity(org.mifos.accounts.business.AccountFeesEntity) DateTimeService(org.mifos.framework.util.DateTimeService)

Example 2 with SavingsPersistence

use of org.mifos.accounts.savings.persistence.SavingsPersistence in project head by mifos.

the class LegacyClientDao method saveClient.

public void saveClient(final ClientBO clientBO) throws CustomerException {
    CustomerPersistence customerPersistence = new CustomerPersistence();
    customerPersistence.saveCustomer(clientBO);
    try {
        if (clientBO.getParentCustomer() != null) {
            customerPersistence.createOrUpdate(clientBO.getParentCustomer());
        }
        // seems fishy... why do savings accounts need updating here?
        new SavingsPersistence().persistSavingAccounts(clientBO);
    } catch (PersistenceException pe) {
        throw new CustomerException(CustomerConstants.CREATE_FAILED_EXCEPTION, pe);
    }
}
Also used : CustomerException(org.mifos.customers.exceptions.CustomerException) SavingsPersistence(org.mifos.accounts.savings.persistence.SavingsPersistence) PersistenceException(org.mifos.framework.exceptions.PersistenceException) CustomerPersistence(org.mifos.customers.persistence.CustomerPersistence)

Example 3 with SavingsPersistence

use of org.mifos.accounts.savings.persistence.SavingsPersistence 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 4 with SavingsPersistence

use of org.mifos.accounts.savings.persistence.SavingsPersistence in project head by mifos.

the class SavingsClosureAction method load.

@TransactionDemarcate(joinToken = true)
public ActionForward load(ActionMapping mapping, ActionForm form, HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
    SavingsClosureActionForm actionForm = (SavingsClosureActionForm) form;
    actionForm.clearForm();
    UserContext uc = (UserContext) SessionUtils.getAttribute(Constants.USER_CONTEXT_KEY, request.getSession());
    SavingsBO savings = (SavingsBO) SessionUtils.getAttribute(Constants.BUSINESS_KEY, request);
    Long savingsId = Long.valueOf(savings.getAccountId());
    savings = this.savingsDao.findById(savingsId);
    savings.setUserContext(uc);
    // 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);
    LocalDate accountCloseDate = new LocalDate();
    SavingsAccountClosureDto closureDetails = this.savingsServiceFacade.retrieveClosingDetails(savingsId, accountCloseDate);
    LegacyAcceptedPaymentTypeDao persistence = legacyAcceptedPaymentTypeDao;
    List<PaymentTypeEntity> acceptedPaymentTypes = persistence.getAcceptedPaymentTypesForATransaction(uc.getLocaleId(), TrxnTypes.savings_withdrawal.getValue());
    Money interestAmountDue = new Money(savings.getCurrency(), closureDetails.getInterestAmountAtClosure());
    Money endOfAccountBalance = new Money(savings.getCurrency(), closureDetails.getBalance());
    Date transactionDate = closureDetails.getClosureDate().toDateMidnight().toDate();
    Money withdrawalAmountAtClosure = endOfAccountBalance.add(interestAmountDue);
    AccountPaymentEntity payment = new AccountPaymentEntity(savings, withdrawalAmountAtClosure, null, null, null, transactionDate);
    actionForm.setAmount(withdrawalAmountAtClosure.toString());
    actionForm.setTrxnDate(DateUtils.getCurrentDate(uc.getPreferredLocale()));
    SessionUtils.setAttribute(Constants.BUSINESS_KEY, savings, request);
    SessionUtils.setCollectionAttribute(MasterConstants.PAYMENT_TYPE, acceptedPaymentTypes, request);
    SessionUtils.setAttribute(SavingsConstants.ACCOUNT_PAYMENT, payment, request);
    return mapping.findForward("load_success");
}
Also used : SavingsPersistence(org.mifos.accounts.savings.persistence.SavingsPersistence) UserContext(org.mifos.security.util.UserContext) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) SavingsClosureActionForm(org.mifos.accounts.savings.struts.actionforms.SavingsClosureActionForm) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) LocalDate(org.joda.time.LocalDate) SavingsAccountClosureDto(org.mifos.dto.domain.SavingsAccountClosureDto) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) PaymentTypeEntity(org.mifos.application.master.business.PaymentTypeEntity) Money(org.mifos.framework.util.helpers.Money) LegacyAcceptedPaymentTypeDao(org.mifos.accounts.acceptedpaymenttype.persistence.LegacyAcceptedPaymentTypeDao) TransactionDemarcate(org.mifos.framework.util.helpers.TransactionDemarcate)

Example 5 with SavingsPersistence

use of org.mifos.accounts.savings.persistence.SavingsPersistence 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

SavingsPersistence (org.mifos.accounts.savings.persistence.SavingsPersistence)5 SavingsBO (org.mifos.accounts.savings.business.SavingsBO)4 LocalDate (org.joda.time.LocalDate)3 CustomerPersistence (org.mifos.customers.persistence.CustomerPersistence)3 Date (java.util.Date)2 DateTime (org.joda.time.DateTime)2 AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)2 AccountException (org.mifos.accounts.exceptions.AccountException)2 CustomerException (org.mifos.customers.exceptions.CustomerException)2 SavingsAdjustmentDto (org.mifos.dto.domain.SavingsAdjustmentDto)2 PersistenceException (org.mifos.framework.exceptions.PersistenceException)2 Money (org.mifos.framework.util.helpers.Money)2 TransactionDemarcate (org.mifos.framework.util.helpers.TransactionDemarcate)2 UserContext (org.mifos.security.util.UserContext)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DateMidnight (org.joda.time.DateMidnight)1 LegacyAcceptedPaymentTypeDao (org.mifos.accounts.acceptedpaymenttype.persistence.LegacyAcceptedPaymentTypeDao)1 AccountBO (org.mifos.accounts.business.AccountBO)1 AccountFeesEntity (org.mifos.accounts.business.AccountFeesEntity)1