Search in sources :

Example 96 with CustomerBO

use of org.mifos.customers.business.CustomerBO 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)

Example 97 with CustomerBO

use of org.mifos.customers.business.CustomerBO in project head by mifos.

the class SavingsBO method createDepositTrxnsForMandatoryAccountsAfterAdjust.

private Set<AccountTrxnEntity> createDepositTrxnsForMandatoryAccountsAfterAdjust(final AccountPaymentEntity newAccountPayment, final AccountPaymentEntity lastAccountPayment, Money newAmount, LocalDate adjustmentDate, PersonnelBO createdBy) {
    Set<AccountTrxnEntity> newTrxns = new LinkedHashSet<AccountTrxnEntity>();
    SavingsTrxnDetailEntity accountTrxn = null;
    CustomerBO customer = null;
    Date trxnDate = adjustmentDate.toDateMidnight().toDate();
    for (AccountTrxnEntity oldAccntTrxn : lastAccountPayment.getAccountTrxns()) {
        customer = oldAccntTrxn.getCustomer();
        break;
    }
    List<AccountActionDateEntity> accountActionList = getAccountActions(lastAccountPayment.getPaymentDate(), customer.getCustomerId());
    for (AccountActionDateEntity accountActionDateEntity : accountActionList) {
        SavingsScheduleEntity accountAction = (SavingsScheduleEntity) accountActionDateEntity;
        if (newAmount.isZero()) {
            break;
        }
        accountTrxn = null;
        // if payment covers required deposit
        if (accountAction.getDeposit().isLessThanOrEqual(newAmount)) {
            this.savingsBalance = this.savingsBalance.add(accountAction.getDeposit());
            Short installmentId = accountAction.getInstallmentId();
            Date dueDate = accountAction.getActionDate();
            Date transactionCreatedDate = new DateTimeService().getCurrentJavaDateTime();
            accountTrxn = SavingsTrxnDetailEntity.savingsDeposit(newAccountPayment, customer, this.savingsBalance, accountAction.getDeposit(), createdBy, dueDate, trxnDate, transactionCreatedDate, installmentId);
            newAmount = newAmount.subtract(accountAction.getDeposit());
            accountAction.setDepositPaid(accountAction.getDepositPaid().add(accountTrxn.getDepositAmount()));
            accountAction.setPaymentStatus(PaymentStatus.PAID);
        } else {
            this.savingsBalance = this.savingsBalance.add(newAmount);
            Short installmentId = accountAction.getInstallmentId();
            Date dueDate = accountAction.getActionDate();
            Date transactionCreatedDate = new DateTimeService().getCurrentJavaDateTime();
            accountTrxn = SavingsTrxnDetailEntity.savingsDeposit(newAccountPayment, customer, this.savingsBalance, newAmount, createdBy, dueDate, trxnDate, transactionCreatedDate, installmentId);
            newAmount = newAmount.subtract(newAmount);
            accountAction.setDepositPaid(accountAction.getDepositPaid().add(accountTrxn.getDepositAmount()));
            accountAction.setPaymentStatus(PaymentStatus.UNPAID);
        }
        accountAction.setPaymentDate(new DateTimeService().getCurrentJavaSqlDate());
        getSavingsPerformance().setTotalDeposits(getSavingsPerformance().getTotalDeposits().add(accountTrxn.getDepositAmount()));
        newTrxns.add(accountTrxn);
    }
    // add trxn for excess amount
    if (newAmount.isGreaterThanZero()) {
        this.savingsBalance = this.savingsBalance.add(newAmount);
        Short installmentId = null;
        Date dueDate = null;
        Date transactionCreatedDate = new DateTimeService().getCurrentJavaDateTime();
        accountTrxn = SavingsTrxnDetailEntity.savingsDeposit(newAccountPayment, customer, this.savingsBalance, newAmount, createdBy, dueDate, trxnDate, transactionCreatedDate, installmentId);
        newAmount = newAmount.subtract(newAmount);
        getSavingsPerformance().setTotalDeposits(getSavingsPerformance().getTotalDeposits().add(accountTrxn.getDepositAmount()));
        newTrxns.add(accountTrxn);
    }
    return newTrxns;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AccountActionDateEntity(org.mifos.accounts.business.AccountActionDateEntity) AccountTrxnEntity(org.mifos.accounts.business.AccountTrxnEntity) CustomerBO(org.mifos.customers.business.CustomerBO) DateTimeService(org.mifos.framework.util.DateTimeService) Date(java.util.Date) LocalDate(org.joda.time.LocalDate)

Example 98 with CustomerBO

use of org.mifos.customers.business.CustomerBO in project head by mifos.

the class SavingsBO method createDepositTrxnsForVolAccountsAfterAdjust.

/*
     * FIXME - keithw - it doesnt make sense to be that voluntary account break up account payments into more than one account transaction
     * just because the amount deposited is greater than the 'recommended' amount.
     *
     * As a result there is no need to make a distinction between the amount deposited (be it less or greater than recommended amount)
     */
private Set<AccountTrxnEntity> createDepositTrxnsForVolAccountsAfterAdjust(final AccountPaymentEntity newAccountPayment, final AccountPaymentEntity lastAccountPayment, Money newAmount, LocalDate adjustmentDate, PersonnelBO loggedInUser) {
    Set<AccountTrxnEntity> newTrxns = new LinkedHashSet<AccountTrxnEntity>();
    SavingsTrxnDetailEntity accountTrxn = null;
    CustomerBO customer = null;
    Date trxnDate = adjustmentDate.toDateMidnight().toDate();
    for (AccountTrxnEntity oldAccntTrxn : lastAccountPayment.getAccountTrxns()) {
        customer = oldAccntTrxn.getCustomer();
        break;
    }
    Short installmentId = null;
    Date dueDate = null;
    Date transactionCreatedDate = new DateTimeService().getCurrentJavaDateTime();
    for (AccountTrxnEntity oldAccntTrxn : lastAccountPayment.getAccountTrxns()) {
        if (oldAccntTrxn.getAccountActionEntity().getId().equals(AccountActionTypes.SAVINGS_DEPOSIT.getValue())) {
            SavingsTrxnDetailEntity oldSavingsAccntTrxn = (SavingsTrxnDetailEntity) oldAccntTrxn;
            if (oldAccntTrxn.getInstallmentId() != null) {
                SavingsScheduleEntity accountAction = (SavingsScheduleEntity) getAccountActionDate(oldSavingsAccntTrxn.getInstallmentId(), oldSavingsAccntTrxn.getCustomer().getCustomerId());
                installmentId = accountAction.getInstallmentId();
                dueDate = accountAction.getActionDate();
                // if recommended amount is covered by payment
                if (accountAction.getDeposit().isLessThanOrEqual(newAmount)) {
                    this.savingsBalance = this.savingsBalance.add(accountAction.getDeposit());
                    accountTrxn = SavingsTrxnDetailEntity.savingsDeposit(newAccountPayment, customer, this.savingsBalance, accountAction.getDeposit(), loggedInUser, dueDate, trxnDate, transactionCreatedDate, installmentId);
                    newAmount = newAmount.subtract(accountAction.getDeposit());
                    accountAction.setDepositPaid(accountAction.getDepositPaid().add(accountTrxn.getDepositAmount()));
                    accountAction.setPaymentStatus(PaymentStatus.PAID);
                    accountAction.setPaymentDate(new DateTimeService().getCurrentJavaSqlDate());
                    this.savingsPerformance.setTotalDeposits(this.savingsPerformance.getTotalDeposits().add(accountTrxn.getDepositAmount()));
                } else if (newAmount.isNonZero()) {
                    // not zero and amount paid is less that recommended amount
                    this.savingsBalance = this.savingsBalance.add(newAmount);
                    accountTrxn = SavingsTrxnDetailEntity.savingsDeposit(newAccountPayment, customer, this.savingsBalance, newAmount, loggedInUser, dueDate, trxnDate, transactionCreatedDate, installmentId);
                    newAmount = newAmount.subtract(newAmount);
                    accountAction.setDepositPaid(accountAction.getDepositPaid().add(accountTrxn.getDepositAmount()));
                    accountAction.setPaymentStatus(PaymentStatus.UNPAID);
                    accountAction.setPaymentDate(new DateTimeService().getCurrentJavaSqlDate());
                    this.savingsPerformance.setTotalDeposits(this.savingsPerformance.getTotalDeposits().add(accountTrxn.getDepositAmount()));
                }
                break;
            }
        }
    }
    if (accountTrxn != null) {
        newTrxns.add(accountTrxn);
    }
    // Create a new transaction with remaining amount
    if (newAmount.isGreaterThanZero()) {
        this.savingsBalance = this.savingsBalance.add(newAmount);
        accountTrxn = SavingsTrxnDetailEntity.savingsDeposit(newAccountPayment, customer, this.savingsBalance, newAmount, loggedInUser, dueDate, trxnDate, transactionCreatedDate, installmentId);
        this.savingsPerformance.setTotalDeposits(this.savingsPerformance.getTotalDeposits().add(accountTrxn.getDepositAmount()));
        newTrxns.add(accountTrxn);
    }
    return newTrxns;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AccountTrxnEntity(org.mifos.accounts.business.AccountTrxnEntity) CustomerBO(org.mifos.customers.business.CustomerBO) DateTimeService(org.mifos.framework.util.DateTimeService) Date(java.util.Date) LocalDate(org.joda.time.LocalDate)

Example 99 with CustomerBO

use of org.mifos.customers.business.CustomerBO in project head by mifos.

the class CustomerServiceImpl method transferClientTo.

@Override
public void transferClientTo(ClientBO client, OfficeBO receivingBranch) {
    try {
        this.hibernateTransactionHelper.startTransaction();
        this.hibernateTransactionHelper.beginAuditLoggingFor(client);
        client.transferToBranch(receivingBranch);
        if (client.getParentCustomer() != null) {
            CustomerBO parent = client.getParentCustomer();
            parent.incrementChildCount();
        }
        this.hibernateTransactionHelper.flushSession();
        client.generateSearchId();
        this.customerDao.save(client);
        if (client.getParentCustomer() != null) {
            this.customerDao.save(client.getParentCustomer());
        }
        this.hibernateTransactionHelper.commitTransaction();
    } catch (ApplicationException e) {
        this.hibernateTransactionHelper.rollbackTransaction();
        throw new BusinessRuleException(e.getKey(), e);
    } catch (Exception e) {
        this.hibernateTransactionHelper.rollbackTransaction();
        throw new MifosRuntimeException(e);
    } finally {
        this.hibernateTransactionHelper.closeSession();
    }
}
Also used : BusinessRuleException(org.mifos.service.BusinessRuleException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) CustomerBO(org.mifos.customers.business.CustomerBO) InvalidDateException(org.mifos.application.admin.servicefacade.InvalidDateException) BusinessRuleException(org.mifos.service.BusinessRuleException) CustomerException(org.mifos.customers.exceptions.CustomerException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) AccountException(org.mifos.accounts.exceptions.AccountException) MeetingException(org.mifos.application.meeting.exceptions.MeetingException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 100 with CustomerBO

use of org.mifos.customers.business.CustomerBO in project head by mifos.

the class CustomerServiceImpl method updateCustomerStatus.

@Override
public final void updateCustomerStatus(UserContext userContext, CustomerStatusUpdate customerStatusUpdate) throws CustomerException {
    CustomerBO customer = this.customerDao.findCustomerById(customerStatusUpdate.getCustomerId());
    customer.validateVersion(customerStatusUpdate.getVersionNum());
    customer.updateDetails(userContext);
    checkPermission(customer, userContext, customerStatusUpdate.getNewStatus(), customerStatusUpdate.getCustomerStatusFlag());
    Short oldStatusId = customer.getCustomerStatus().getId();
    CustomerStatus oldStatus = CustomerStatus.fromInt(oldStatusId);
    PersonnelBO loggedInUser = this.personnelDao.findPersonnelById(userContext.getId());
    CustomerNoteEntity customerNote = new CustomerNoteEntity(customerStatusUpdate.getNotes(), new Date(), loggedInUser, customer);
    if (customer.isGroup()) {
        GroupBO group = (GroupBO) customer;
        updateGroupStatus(group, oldStatus, customerStatusUpdate.getNewStatus(), customerStatusUpdate.getCustomerStatusFlag(), customerNote);
    } else if (customer.isClient()) {
        ClientBO client = (ClientBO) customer;
        updateClientStatus(client, oldStatus, customerStatusUpdate.getNewStatus(), customerStatusUpdate.getCustomerStatusFlag(), customerNote);
    } else {
        CenterBO center = (CenterBO) customer;
        updateCenterStatus(center, customerStatusUpdate.getNewStatus(), customerStatusUpdate.getCustomerStatusFlag(), customerNote);
    }
}
Also used : CustomerNoteEntity(org.mifos.customers.business.CustomerNoteEntity) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) CustomerStatus(org.mifos.customers.util.helpers.CustomerStatus) ClientBO(org.mifos.customers.client.business.ClientBO) CustomerBO(org.mifos.customers.business.CustomerBO) GroupBO(org.mifos.customers.group.business.GroupBO) CenterBO(org.mifos.customers.center.business.CenterBO) Date(java.util.Date) LocalDate(org.joda.time.LocalDate)

Aggregations

CustomerBO (org.mifos.customers.business.CustomerBO)138 ArrayList (java.util.ArrayList)39 Money (org.mifos.framework.util.helpers.Money)38 MifosUser (org.mifos.security.MifosUser)38 UserContext (org.mifos.security.util.UserContext)37 LocalDate (org.joda.time.LocalDate)35 MifosRuntimeException (org.mifos.core.MifosRuntimeException)31 AccountException (org.mifos.accounts.exceptions.AccountException)30 PersonnelBO (org.mifos.customers.personnel.business.PersonnelBO)27 BusinessRuleException (org.mifos.service.BusinessRuleException)27 MeetingBO (org.mifos.application.meeting.business.MeetingBO)23 ClientBO (org.mifos.customers.client.business.ClientBO)23 PersistenceException (org.mifos.framework.exceptions.PersistenceException)22 DateTime (org.joda.time.DateTime)20 TransactionDemarcate (org.mifos.framework.util.helpers.TransactionDemarcate)20 Date (java.util.Date)19 Test (org.junit.Test)19 AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)18 LoanBO (org.mifos.accounts.loan.business.LoanBO)17 HashMap (java.util.HashMap)15