Search in sources :

Example 16 with FeesTrxnDetailEntity

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

the class CustomerAccountBO method makePayment.

@Override
protected AccountPaymentEntity makePayment(final PaymentData paymentData) throws AccountException {
    Money totalPaid = paymentData.getTotalAmount();
    if (totalPaid.isZero()) {
        throw new AccountException("errors.update", new String[] { "Attempting to pay a customer account balance of zero for customer: " + paymentData.getCustomer().getGlobalCustNum() });
    }
    final List<CustomerScheduleEntity> customerAccountPayments = findAllUnpaidInstallmentsUpToDatePlusNextMeeting(paymentData.getTransactionDate());
    if (customerAccountPayments.isEmpty()) {
        throw new AccountException(AccountConstants.NO_TRANSACTION_POSSIBLE, new String[] { "Trying to pay account charges before the due date." });
    }
    Money totalAllUnpaidInstallments = new Money(totalPaid.getCurrency(), "0.0");
    for (CustomerScheduleEntity customerSchedule : customerAccountPayments) {
        totalAllUnpaidInstallments = totalAllUnpaidInstallments.add(new Money(totalPaid.getCurrency(), dueAmountForCustomerSchedule(customerSchedule)));
    }
    if (totalAllUnpaidInstallments.compareTo(totalPaid) < 0) {
        throw new AccountException(AccountConstants.NO_TRANSACTION_POSSIBLE, new String[] { "Overpayments are not supported" });
    }
    final AccountPaymentEntity accountPayment = new AccountPaymentEntity(this, paymentData.getTotalAmount(), paymentData.getReceiptNum(), paymentData.getReceiptDate(), getPaymentTypeEntity(paymentData.getPaymentTypeId()), paymentData.getTransactionDate());
    BigDecimal leftFromPaidIn = totalPaid.getAmount();
    for (CustomerScheduleEntity customerSchedule : customerAccountPayments) {
        if (leftFromPaidIn.compareTo(BigDecimal.ZERO) == 0) {
            break;
        }
        final List<FeesTrxnDetailEntity> feeTrxns = new ArrayList<FeesTrxnDetailEntity>();
        for (AccountFeesActionDetailEntity accountFeesActionDetail : customerSchedule.getAccountFeesActionDetails()) {
            if (leftFromPaidIn.compareTo(BigDecimal.ZERO) > 0) {
                CustomerFeeScheduleEntity customerFeeSchedule = (CustomerFeeScheduleEntity) accountFeesActionDetail;
                BigDecimal feeFromScheduleToPay = leftFromPaidIn.min(customerFeeSchedule.getFeeDue().getAmount());
                customerFeeSchedule.makePayment(new Money(totalPaid.getCurrency(), feeFromScheduleToPay));
                final FeesTrxnDetailEntity feesTrxnDetailBO = new FeesTrxnDetailEntity(null, customerFeeSchedule.getAccountFee(), new Money(totalPaid.getCurrency(), feeFromScheduleToPay));
                feeTrxns.add(feesTrxnDetailBO);
                leftFromPaidIn = leftFromPaidIn.subtract(feeFromScheduleToPay);
            }
        }
        BigDecimal miscPenaltyToPay = leftFromPaidIn.min(customerSchedule.getMiscPenaltyDue().getAmount());
        if (miscPenaltyToPay.compareTo(BigDecimal.ZERO) > 0) {
            customerSchedule.payMiscPenalty(new Money(totalPaid.getCurrency(), miscPenaltyToPay));
            customerSchedule.setPaymentDate(new java.sql.Date(paymentData.getTransactionDate().getTime()));
            leftFromPaidIn = leftFromPaidIn.subtract(miscPenaltyToPay);
        }
        BigDecimal miscFeeToPay = BigDecimal.ZERO;
        if (leftFromPaidIn.compareTo(BigDecimal.ZERO) > 0) {
            miscFeeToPay = leftFromPaidIn.min(customerSchedule.getMiscFeeDue().getAmount());
            if (miscFeeToPay.compareTo(BigDecimal.ZERO) > 0) {
                customerSchedule.payMiscFee(new Money(totalPaid.getCurrency(), miscFeeToPay));
                customerSchedule.setPaymentDate(new java.sql.Date(paymentData.getTransactionDate().getTime()));
                leftFromPaidIn = leftFromPaidIn.subtract(miscFeeToPay);
            }
        }
        if (dueAmountForCustomerSchedule(customerSchedule).compareTo(BigDecimal.ZERO) == 0) {
            customerSchedule.setPaymentStatus(PaymentStatus.PAID);
        }
        Money customerScheduleAmountPaid = new Money(totalPaid.getCurrency(), miscFeeToPay.add(miscPenaltyToPay));
        final CustomerTrxnDetailEntity accountTrxn = new CustomerTrxnDetailEntity(accountPayment, AccountActionTypes.CUSTOMER_ACCOUNT_REPAYMENT, customerSchedule.getInstallmentId(), customerSchedule.getActionDate(), paymentData.getPersonnel(), paymentData.getTransactionDate(), customerScheduleAmountPaid, AccountConstants.PAYMENT_RCVD, null, new Money(totalPaid.getCurrency(), miscFeeToPay), new Money(totalPaid.getCurrency(), miscPenaltyToPay));
        for (FeesTrxnDetailEntity feesTrxnDetailEntity : feeTrxns) {
            accountTrxn.addFeesTrxnDetail(feesTrxnDetailEntity);
            feesTrxnDetailEntity.setAccountTrxn(accountTrxn);
        }
        accountPayment.addAccountTrxn(accountTrxn);
    }
    addCustomerActivity(new CustomerActivityEntity(this, paymentData.getPersonnel(), paymentData.getTotalAmount(), AccountConstants.PAYMENT_RCVD, paymentData.getTransactionDate()));
    return accountPayment;
}
Also used : AccountFeesActionDetailEntity(org.mifos.accounts.business.AccountFeesActionDetailEntity) AccountPaymentEntity(org.mifos.accounts.business.AccountPaymentEntity) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Money(org.mifos.framework.util.helpers.Money) AccountException(org.mifos.accounts.exceptions.AccountException) FeesTrxnDetailEntity(org.mifos.accounts.business.FeesTrxnDetailEntity)

Aggregations

FeesTrxnDetailEntity (org.mifos.accounts.business.FeesTrxnDetailEntity)16 Date (java.sql.Date)6 AccountFeesActionDetailEntity (org.mifos.accounts.business.AccountFeesActionDetailEntity)6 AccountPaymentEntity (org.mifos.accounts.business.AccountPaymentEntity)6 AccountTrxnEntity (org.mifos.accounts.business.AccountTrxnEntity)5 FinancialActionTypeEntity (org.mifos.accounts.financial.business.FinancialActionTypeEntity)4 LoanTrxnDetailEntity (org.mifos.accounts.loan.business.LoanTrxnDetailEntity)4 Money (org.mifos.framework.util.helpers.Money)4 Test (org.junit.Test)3 AccountException (org.mifos.accounts.exceptions.AccountException)3 PaymentTypeEntity (org.mifos.application.master.business.PaymentTypeEntity)3 CustomerTrxnDetailEntity (org.mifos.customers.business.CustomerTrxnDetailEntity)3 PaymentData (org.mifos.accounts.util.helpers.PaymentData)2 ApplicationException (org.mifos.framework.exceptions.ApplicationException)2 SystemException (org.mifos.framework.exceptions.SystemException)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LocalDate (org.joda.time.LocalDate)1 AccountActionDateEntity (org.mifos.accounts.business.AccountActionDateEntity)1