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;
}
Aggregations