@Override protected ActionForward handleSubmit(final ActionContext context) throws Exception { final SendInvoiceForm form = context.getForm(); final boolean fromProfile = !form.isToSystem() && !form.isSelectMember(); try { final Invoice invoice = invoiceService.send(resolveInvoice(context)); context.sendMessage("invoice.sent"); ActionForward forward = null; final Map<String, Object> params = new HashMap<String, Object>(); if (fromProfile) { forward = context.findForward("profile"); params.put("memberId", invoice.getToMember().getId()); } else { forward = context.findForward("newInvoice"); } final Member fromMember = invoice.getFromMember(); if (fromMember != null && !fromMember.equals(context.getMember())) { // From another member params.put("from", form.getFrom()); } else if (fromProfile) { params.put("to", form.getTo()); } if (form.isToSystem()) { params.put("toSystem", true); } else if (form.isSelectMember()) { params.put("selectMember", true); } return ActionHelper.redirectWithParams(context.getRequest(), forward, params); } catch (final SendingInvoiceWithMultipleTransferTypesWithCustomFields e) { return context.sendError("invoice.error.sendingWithMultipleTransferTypesWithCustomFields"); } }
private Invoice resolveInvoice(final ActionContext context) { final SendInvoiceForm form = context.getForm(); final Invoice invoice = getDataBinder().readFromString(form); if ((context.isMember() && invoice.getFromMember() == null) || context.isOperator()) { invoice.setFrom(context.getAccountOwner()); } return invoice; }
@Override protected ActionForward handleValidation(final ActionContext context) { try { final Invoice invoice = resolveInvoice(context); invoiceService.validate(invoice); // Retrive and fetch the destination account type AccountType accountType = invoice.getDestinationAccountType(); if (accountType == null) { final TransferType tt = transferTypeService.load( invoice.getTransferType().getId(), RelationshipHelper.nested( TransferType.Relationships.TO, AccountType.Relationships.CURRENCY)); accountType = tt.getTo(); } else { accountType = accountTypeService.load(accountType.getId()); } // If the validation passed, resolve the confirmation message final LocalSettings localSettings = settingsService.getLocalSettings(); final UnitsConverter unitsConverter = localSettings.getUnitsConverter(accountType.getCurrency().getPattern()); final AccountOwner toOwner = invoice.getTo(); final boolean toSystem = toOwner instanceof SystemAccountOwner; // Retrieve the message arguments String to; if (toSystem) { to = localSettings.getApplicationUsername(); } else { final Member member = elementService.load(((Member) toOwner).getId()); to = member.getName(); } final String amount = unitsConverter.toString(invoice.getAmount()); final String confirmationKey = "invoice.sendConfirmationMessage"; final Map<String, Object> fields = new HashMap<String, Object>(); fields.put("confirmationMessage", context.message(confirmationKey, to, amount)); responseHelper.writeStatus(context.getResponse(), ResponseHelper.Status.SUCCESS, fields); } catch (final ValidationException e) { responseHelper.writeValidationErrors(context.getResponse(), e); } return null; }