private TransferType resolveTransferType(final ActionContext context) {
    final HttpServletRequest request = context.getRequest();

    // Try by transfer type directly
    final TransferType transferType =
        CoercionHelper.coerce(TransferType.class, request.getParameter("typeId"));
    if (transferType != null) {
      return transferType;
    }

    // Try by invoice details: destination account type, currencty, from and to
    final AccountType accountType =
        CoercionHelper.coerce(AccountType.class, request.getParameter("destinationAccountTypeId"));
    final Member fromMember = CoercionHelper.coerce(Member.class, request.getParameter("fromId"));
    final AccountOwner from = fromMember != null ? fromMember : context.getAccountOwner();
    final Member toMember = CoercionHelper.coerce(Member.class, request.getParameter("toId"));
    final AccountOwner to = toMember != null ? toMember : context.getAccountOwner();

    // Find the first TT with the matching criteria. It's important that only return one if A SINGLE
    // TT is found, because when there are multiple
    // possible TTs, custom fields won't be used
    final TransferTypeQuery query = new TransferTypeQuery();
    query.setUsePriority(true);
    query.setChannel(Channel.WEB);
    query.setFromOwner(from);
    query.setToOwner(to);
    query.setToAccountType(accountType);
    final List<TransferType> transferTypes = transferTypeService.search(query);
    if (transferTypes.size() == 1) {
      return transferTypes.iterator().next();
    }
    return null;
  }
  @Override
  protected ActionForward executeAction(final ActionContext context) throws Exception {
    final HttpServletRequest request = context.getRequest();
    final TransferType transferType = resolveTransferType(context);
    if (transferType == null) {
      // TT not found
      return null;
    }

    final Collection<PaymentCustomField> customFields =
        paymentCustomFieldService.list(transferType, false);
    if (CollectionUtils.isEmpty(customFields)) {
      // No custom fields
      return null;
    }

    // Set some specific use cases attributes
    final boolean isForLoan = CoercionHelper.coerce(boolean.class, request.getParameter("forLoan"));
    final String fieldName = isForLoan ? "loan(customValues).field" : "customValues.field";
    final String valueName = isForLoan ? "loan(customValues).value" : "customValues.value";
    request.setAttribute("isForLoan", isForLoan);
    request.setAttribute("fieldName", fieldName);
    request.setAttribute("valueName", valueName);
    request.setAttribute(
        "columnWidth",
        ObjectUtils.defaultIfNull(
            StringHelper.removeMarkupTags(request.getParameter("columnWidth")), "25%"));

    // Delegate the rendering to a jsp
    request.setAttribute("customFields", customFields);
    context.getResponse().setContentType("text/html");
    return new ActionForward("/pages/payments/paymentCustomFields.jsp", false);
  }
Ejemplo n.º 3
0
  @Override
  protected ActionForward handleDisplay(final ActionContext context) throws Exception {
    final HttpServletRequest request = context.getRequest();
    final SendInvoiceForm form = context.getForm();
    final boolean toSystem = form.isToSystem();
    final boolean selectMember = form.isSelectMember();

    AccountOwner to;
    final Member fromMember =
        (form.getFrom() == null)
            ? null
            : (Member) elementService.load(Long.valueOf(form.getFrom()));
    final Element loggedElement = context.getElement();
    if (toSystem) {
      // System invoice
      to = SystemAccountOwner.instance();
    } else {
      if (!selectMember) {
        // Retrieve the member to send invoice for
        Member member = null;
        final Long memberId = IdConverter.instance().valueOf(form.getTo());
        if (memberId != null && memberId != loggedElement.getId()) {
          final Element element = elementService.load(memberId, Element.Relationships.USER);
          if (element instanceof Member) {
            member = (Member) element;
          }
        }
        if (member == null) {
          throw new ValidationException();
        }
        request.setAttribute("member", member);
        to = member;
      } else {
        // The member will be selected later
        to = null;
      }
    }

    // If we know who will receive the invoice, get the transfer types or dest account types
    if (to != null) {
      if (context.isAdmin() && fromMember == null) {
        // Only admins may select the transfer type
        final TransferTypeQuery query = new TransferTypeQuery();
        query.setChannel(Channel.WEB);
        query.setContext(TransactionContext.PAYMENT);
        query.setFromOwner(to);
        query.setToOwner(context.getAccountOwner());
        query.setUsePriority(true);
        request.setAttribute("transferTypes", transferTypeService.search(query));
      } else {
        // Members may select the destination account type
        final MemberAccountTypeQuery query = new MemberAccountTypeQuery();
        query.setOwner(fromMember == null ? (Member) loggedElement.getAccountOwner() : fromMember);
        query.setCanPay(to);
        final List<? extends AccountType> accountTypes = accountTypeService.search(query);
        if (accountTypes.isEmpty()) {
          return context.sendError("invoice.error.noAccountType");
        }
        request.setAttribute("accountTypes", accountTypes);
      }
    }

    // Resolve the possible currencies
    final MemberGroup group = getMemberGroup(context);
    final List<Currency> currencies;
    if (group != null) {
      currencies = currencyService.listByMemberGroup(group);
      final MemberAccountType defaultAccountType =
          accountTypeService.getDefault(group, AccountType.Relationships.CURRENCY);
      // Preselect the default currency
      if (defaultAccountType != null) {
        form.setCurrency(CoercionHelper.coerce(String.class, defaultAccountType.getCurrency()));
      }
    } else {
      currencies = currencyService.listAll();
    }
    request.setAttribute("currencies", currencies);

    if (currencies.isEmpty()) {
      // No currencies means no possible payment!!!
      throw new ValidationException("payment.error.noTransferType");
    } else if (currencies.size() == 1) {
      // Special case: There is a single currency. The JSP will use this object
      request.setAttribute("singleCurrency", currencies.get(0));
    }

    request.setAttribute("toSystem", toSystem);
    request.setAttribute("toMember", !toSystem);
    request.setAttribute("selectMember", selectMember);
    request.setAttribute("from", fromMember);

    final boolean useTransferType = context.isAdmin() && fromMember == null;
    request.setAttribute("useTransferType", useTransferType);

    // Check whether scheduled payments may be performed
    boolean allowsScheduling = false;
    boolean allowsMultipleScheduling = false;
    if (context.isAdmin() && fromMember == null) {
      allowsScheduling = true;
      allowsMultipleScheduling = true;
    } else {
      MemberGroup memberGroup;
      if (fromMember == null) {
        memberGroup = ((Member) context.getAccountOwner()).getMemberGroup();
      } else {
        memberGroup = fromMember.getMemberGroup();
      }
      final MemberGroupSettings memberSettings = memberGroup.getMemberSettings();
      allowsScheduling = memberSettings.isAllowsScheduledPayments();
      allowsMultipleScheduling = memberSettings.isAllowsMultipleScheduledPayments();
    }
    if (allowsScheduling) {
      request.setAttribute("allowsScheduling", allowsScheduling);
      request.setAttribute("allowsMultipleScheduling", allowsMultipleScheduling);
      final Collection<SchedulingType> schedulingTypes =
          EnumSet.of(SchedulingType.IMMEDIATELY, SchedulingType.SINGLE_FUTURE);
      if (allowsMultipleScheduling) {
        schedulingTypes.add(SchedulingType.MULTIPLE_FUTURE);
      }
      request.setAttribute("schedulingTypes", schedulingTypes);
      request.setAttribute(
          "schedulingFields",
          Arrays.asList(TimePeriod.Field.MONTHS, TimePeriod.Field.WEEKS, TimePeriod.Field.DAYS));
    }

    return context.getInputForward();
  }