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

Respect calling code passing in 'null' for creditnote_id. #15232

Merged
merged 1 commit into from
Oct 9, 2019

Conversation

eileenmcnaughton
Copy link
Contributor

@eileenmcnaughton eileenmcnaughton commented Sep 6, 2019

Overview

Fixes an issue where the contribution bao disregards 'null' when passed in for 'credit_note_id'. This has performance implications as the code to get the credit note id is very slow and there is no precedent anywhere in our code for the BAO to deliberately override efforts by
calling code to null out a value

Before

The 'null' in the below query is ignored and the credit note id is calcluated anyway in a way that performs very badly, even on sites without the requirement for this particular calculation.

civicrm_api3('Contribution', 'create', [
  'id' => 'x',
 'contribution_status_id' => 'Refunded',
 'creditnote_id' => 'null'
);

After

The api call results in a credit note id of NULL and performance is not damaged.

civicrm_api3('Contribution', 'create', [
  'id' => 'x',
 'contribution_status_id' => 'Refunded',
 'creditnote_id' => 'null'
);

Technical Details

The credit note code was implemented in core quite a while ago, before we were really pushing these things
out to extensions. It was implemented such that it iterates through all credit notes & has performance
implications in order to meet a use case that is not common to all.

Ideally we would move it to an extension, but short of that we can at least respect
calling code setting the value to 'null' in order to override it.

Probably we should longer term simply have a hook

hook::getCreditNoteID() & more this function to an extension called by that hook

Comments

@JoeMurray @pfigel @jamienovick @lucianov88 it seems Patrick's fix mitigated the performance issue but it's still a thing - too much so for us to be able to have that code in production with hacking it out. This makes the credit note behaviour 'opt out' & makes our code more consistent - although per above - it should still be moved to an extension IMHO

@civibot
Copy link

civibot bot commented Sep 6, 2019

(Standard links)

@civibot civibot bot added the master label Sep 6, 2019
@eileenmcnaughton
Copy link
Contributor Author

test this please

Copy link
Contributor

@pfigel pfigel left a comment

Choose a reason for hiding this comment

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

@eileenmcnaughton there's two more places in the BAO where "null" is checked before createCreditNoteId() is called. At least one gets hit by the API during cancellation (Contribution.create with status changing to "Cancelled").

if (empty($params['contribution']->creditnote_id) || $params['contribution']->creditnote_id == "null") {

if (is_null($params['contribution']->creditnote_id) || $params['contribution']->creditnote_id == "null") {

@pfigel
Copy link
Contributor

pfigel commented Oct 9, 2019

@eileenmcnaughton I put together a simple extension that uses this to disable credit note generation. It has a test that can be used to verify this implementation works.

 for creditnote_id.

The credit note code was implemented in core quite a while ago, before we were really pushing these things
out to extensions. It was implemented such that it iterates through all credit notes & has performance
implications in order to meet a use case that is not common to all.

Ideally we would move it to an extension, but short of that we can at least respect
calling code setting the value to 'null' in order to override it.

There is no precedent anywhere in our code for the BAO to deliberately override efforts by
calling code to null out a value & in this case it solves a performance problem too

Probably we should longer term simply have a hook

hook::getCreditNoteID() & more this function to an extension called by that hook
@eileenmcnaughton
Copy link
Contributor Author

@pfigel thanks - I've updated it - is it OK to merge on pass now.

@ejegg @bjendres @sluc23 we can start using Patrick's extension above to resolve this performance issue in a less hack-core way from 5.20

@jamienovick this shouldn't affect much since it will still set creditnote_id using the slow-but-useful-for-UK-sites unless a developer intervenes to stop it from doing so. Longer term we have been discussing that an extension should opt in rather than out of slow-but-useful-for-UK functionality & @bjendres will log a gitlab issue to discuss further (don't worry nothing more than this will happen in the short-term)

@bjendres
Copy link
Contributor

bjendres commented Oct 9, 2019

See lab/Core #1308

Copy link
Contributor

@pfigel pfigel left a comment

Choose a reason for hiding this comment

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

Couldn't find any issues during r-run, MOP.

@mattwire
Copy link
Contributor

mattwire commented Oct 9, 2019

Merging based on comments and tests passed

@mattwire mattwire merged commit 9c5b4e0 into civicrm:master Oct 9, 2019
@eileenmcnaughton eileenmcnaughton deleted the credit branch October 9, 2019 17:32
eileenmcnaughton added a commit to eileenmcnaughton/civicrm-core that referenced this pull request Jan 7, 2020
Am trying to upstream here so we could override externally

civicrm#15232

Bug: T228826

Change-Id: Ie5178f12073a1b5f6fd289ed66a4245668cfa32b
eileenmcnaughton added a commit to eileenmcnaughton/civicrm-core that referenced this pull request Jan 7, 2020
Am trying to upstream here so we could override externally

civicrm#15232

Bug: T228826

Change-Id: Ie5178f12073a1b5f6fd289ed66a4245668cfa32b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants