Ejemplo n.º 1
0
  @Override
  public void applyCharge(Integer accountId, Short feeId, Double chargeAmount) {

    MifosUser user =
        (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    UserContext userContext = toUserContext(user);

    try {
      AccountBO account = new AccountBusinessService().getAccount(accountId);

      if (account instanceof LoanBO) {
        List<LoanBO> individualLoans = this.loanDao.findIndividualLoans(account.getAccountId());

        if (individualLoans != null && individualLoans.size() > 0) {
          for (LoanBO individual : individualLoans) {
            individual.updateDetails(userContext);

            FeeBO fee = this.feeDao.findById(feeId);

            if (fee instanceof RateFeeBO) {
              individual.applyCharge(feeId, chargeAmount);
            } else {
              Double radio =
                  individual.getLoanAmount().getAmount().doubleValue()
                      / ((LoanBO) account).getLoanAmount().getAmount().doubleValue();

              individual.applyCharge(feeId, chargeAmount * radio);
            }
          }
        }
      }

      account.updateDetails(userContext);

      CustomerLevel customerLevel = null;
      if (account.isCustomerAccount()) {
        customerLevel = account.getCustomer().getLevel();
      }
      if (account.getPersonnel() != null) {
        checkPermissionForApplyCharges(
            account.getType(),
            customerLevel,
            userContext,
            account.getOffice().getOfficeId(),
            account.getPersonnel().getPersonnelId());
      } else {
        checkPermissionForApplyCharges(
            account.getType(),
            customerLevel,
            userContext,
            account.getOffice().getOfficeId(),
            userContext.getId());
      }

      this.transactionHelper.startTransaction();
      account.applyCharge(feeId, chargeAmount);
      this.transactionHelper.commitTransaction();
    } catch (ServiceException e) {
      this.transactionHelper.rollbackTransaction();
      throw new MifosRuntimeException(e);
    } catch (ApplicationException e) {
      this.transactionHelper.rollbackTransaction();
      throw new BusinessRuleException(e.getKey(), e);
    }
  }