Add Api4 Contribution.UpgradeGiftAid action
Note: this adds new code and doesn't change a line of existing code.
It adds an action which can be used - typically when things go wrong - to bulk (or single) update the custom data on a contribution.
Before: if you had to do an update you had to manually loop all the records you wanted to change and call an api3 action for each one.
After: you can do any type of Api4 query you like to identify a group you need to recalculate values for.
Features:
- Won't touch contributions in a gift aid batch (unless you choose to override it with
setAllowUpdatingExistingBatches(TRUE)
) - Offers two levels of update:
setUpdateLevel('amountsOnly')
will only update the amount and gift aid amount. You might use this, for example, if the tax rate changed. - The default level of update is
'all'
which will review whether the contribution should be eligible and update the amounts. Eligibility is done by the financial types of line items (except in the case of everything being eligible).
While there's some duplicate code here, being able to make it re-check whether a contribution is eligible is a new feature because the current code deliberately leaves that alone (because a typical case is that someone has manually set the eligibility so it shouldn't change).
New test class included.
Aside: my use case
Chronologically:
- a big number of recurring contributions were set up with financial type X which was giftaid eligible.
- later, someone set financial type X as inactive because there's new types in use now.
- on some other date, someone edited the list of eligible types using the UI, which would have removed inactive ones.
- this meant that the contributions created from that point on were no longer eligible for GA.
- !38 (merged) will hopefully fix the cause of this problem.
- This PR will help fix the symptoms of the problem: I can do (pseudo code:)
Api4\Contribution::UpdateGiftAid()->addWhere('payment_processor_id', '=', 123)->addWhere('date_received', '>', '2023-01-02')->execute();
to update all the contributions.
Comments
The event hook that updates contributions' ga data after an edit(or creation) gets in the way here - it duplicates work. I did wonder about putting a GLOBAL such that code like this could temporarily disable the event handler to avoid the repeat work. But I left that out for the sake of keeping the PR clean.
It would be good to move to Api4 one day anyway.