2

Lemme cite an example before explaining the cause.

  1. Updated the amount of an contribution id n via Contribution.create api from $100 to $150 and "Execute"d it from API Explorer. (No validation error)
  2. Open the edit back-office contribution form of same contribution ID n. The amount is rightfully changed to $150, but problem is when without doing anything simply save the form and it throws a validation error "Net amount and/or Fee amount must be equal to the Total amount"

The question arises "why didn't I got any validation error at step 1?". Answer lies on the native validation rules written under formRules() func which are not meant for corresponding API.actions. Likewise there are other entity attributes which raise the same concern.

So my suggestion/fix would be to introduce a BAO function for such validation(s),

Flexible enough to accept input params from both APIs and Forms

Provide API Exception(s) and validation error(s) for APIs and Forms respectively

Optimize API and Form validations

And of course would be a BAO function to get called in both APIs and Forms

Thoughts ?

1

Monish, I think the problem is that if you use the api to change the total amount & don't pass in other amount values the BAO should assume that net_amount has now changed similarly. I thought we had done something in the BAO - but maybe only on new contributions.

I think at the BAO level adding this logic makes sense

if (contributionTotalAmountHasChanged() && empty($params['net_amount')) {
   $fee_amount = !isset($params['fee_amount'] ? 
     getFeeAmountFromDB() : $params['fee_amount'];

  $params['net_amount'] = $params['total_amount'] - $fee_amount;
}

Possibly some argy bargy around whether $params['fee_amount'] = '' vs $params['fee_amount'] = 0;

At the form level - if someone changes the total_amount then probably we should either 1) update the other amounts using js or 2) reset them sensibly on submission (e.g like above) & present them with a notice that this has happened.

It's a bit of a pain having to change net_amount & for most users the field is not relevant

1
  • Yup that make sense for the cited example by making necessary changes in create fn, but its not just about "Total amount = net amount + fee amount" rule but addresses a major concern for other formrules too. Lets take another rule "Soft Credit Amount <= Total amount", when you create a soft-credit via api against any contribution, of soft-credit amount $100 (corresponding contribution total amount $70) it allows you to create it without any validation error, unlike back-office form. – Monish Deb Aug 6 '15 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.