Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify / Cleanup Contribution BAO create #17115

Closed
wants to merge 3 commits into from

Conversation

mattwire
Copy link
Contributor

Overview

Retrieve full contribution object once so we don't have to keep checking if bits are set. Fix bug where DAO retrieve fails if you had passed in an unrounded fee_amount which causes addActivity () to fail with missing contact ID and triggering an obscure message "Failed to update contribution in database" but only if there is no logged in user (because otherwise it will default to the logged in contact ID and not fail).

Before

CRM_Contribute_BAO_Contribution::create function complex and does not always retrieve the params needed. Also has various bits of code just to deal with retrieving missing params.

After

Call $contribution->find(TRUE) once from add. In some cases this will result in one extra database query per request, but in multiple cases it will reduce the number of queries later on. And it significantly simplifies the calling create function which will make maintenance and improvements to this function much easier.

Technical Details

Once the contribution has been created in the database we retrieve it using standard DAO functions.
We only use the contribution ID to retrieve it because we always have this value after saving. This prevents unrounded amounts causing the database select to fail (eg. fee_amount=0.3142424 gets saved as 0.31 in the database but then the contribution cannot be retrieved with the same params (because 0.3142424 != 0.31). The $params array that is passed in is copied to the contribution before saving so we won't lose any data.

Comments

There are not many payment processors I'm aware of that actually record the fees. But if we do then the anonymous user is likely to see an error page despite the payment going through successfully.

@civibot
Copy link

civibot bot commented Apr 20, 2020

(Standard links)

@civibot civibot bot added the master label Apr 20, 2020
@eileenmcnaughton
Copy link
Contributor

". In some cases this will result in one extra database query per request, but in multiple cases it will reduce the number of queries later on."

  • We have had some push back on this in the past as it adds up when, say, importing contributions. In other classes I have a helper function that loads extra data if needed - & if it DOES load extra data it loads 'all of it' - rather than multiple 'get this field'

];
if (!$noteParams['contact_id']) {
$noteParams['contact_id'] = $params['contact_id'];
}
CRM_Core_BAO_Note::add($noteParams);
}

// make entry in batch entity batch table
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So all of this is to do a load if we don't have all the contribution fields - maybe a helper function that identifies if there IS missing required info

@mattwire mattwire force-pushed the contributionbaofullobject branch from c4b414b to 65ec8f1 Compare April 20, 2020 21:53
if ($retrieveRequired == 1) {
$contribution->find(TRUE);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattwire why on earth do we do this if batch_id is present? It looks to me an awful lot like logic to support a specific form layer flow - I just added #17120 to figure out if it is tested at any point if so where it is hit from - I'm guessing we can likely remove this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - it's not clear the loading that is happening adds any value though!

CRM_Activity_BAO_Activity::addActivity($contribution, 'Contribution');

// do not add to recent items for import, CRM-4399
if (empty($params['skipRecentView'])) {
$url = CRM_Utils_System::url('civicrm/contact/view/contribution',
"action=view&reset=1&id={$contribution->id}&cid={$contribution->contact_id}&context=home"
);
// in some update cases we need to get extra fields - ie an update that doesn't pass in all these params
$titleFields = [
'contact_id',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - so we need these for addRecent. Maybe we just need a function to addContributionRecent that loads extra if need-be

…ing if bits are set. Fix bug where retrieve fails if you had passed in an unrounded fee_amount which led to addActivity failing with missing contact ID
@eileenmcnaughton
Copy link
Contributor

@mattwire I think we can just move the code as it relates to the batch - ie #17176

I'm very keen to avoid adding an extra FIND except where required & it seems that would rarely be the case on imports (as long as skipRecent is off)

@mattwire mattwire closed this Jun 3, 2020
@mattwire mattwire deleted the contributionbaofullobject branch June 3, 2020 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants