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

Add cancel_reason field in contribute query whereClauseSingle #12761

Closed
wants to merge 1 commit into from

Conversation

sluc23
Copy link
Contributor

@sluc23 sluc23 commented Sep 3, 2018

Overview

Contribution's Cancel Reason is not working when used in Search Builder using IN operator

Before

civicrm_bug1

Query produced is wrong

SELECT 
  DISTINCT UPPER(LEFT(contact_a.sort_name, 1)) AS sort_name
FROM 
  civicrm_contact contact_a
LEFT JOIN 
  civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id
WHERE 
  ((civicrm_contribution.financial_type_id IN ("3", "1",  "4","2")
        AND civicrm_contribution.cancel_reason IN ''))
  AND (contact_a.is_deleted = 0)
ORDER BY UPPER(LEFT(contact_a.sort_name, 1)) ASC 
[nativecode=1064 ** You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' )  )  AND (contact_a.is_deleted = 0)    ORDER BY UPPER(LEFT(contact_a.sort_na' at line 1]

After

civicrm_bug2

Technical Details

cancel_reason field is not treated as rest of contribution's fields in whereClauseSingle().
This PR just add this field to be treated as the rest of common contribution's fields

@jitendrapurohit
Copy link
Contributor

Did a quick testing on search forms. It fixes the main issue and code changes looks fine to me.

@@ -258,6 +258,7 @@ public static function whereClauseSingle(&$values, &$query) {
case (strpos($name, '_amount') !== FALSE):
case (strpos($name, '_date') !== FALSE && $name != 'contribution_fulfilled_date'):
case 'contribution_campaign_id':
case 'contribution_cancel_reason':
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: Why do we need to add case for every contribution field of type varchar? Doesn't the default section responsible to handle this automatically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the default: section doesn't take care properly of string fields. If you see the result query, it adds where clause civicrm_contribution.financial_type_id IN ("3", "1", "4","2") when it's not necessary, and then the IN operator it's not parsed properly

Basically, I added cancel_reason in the same case block as other string field are parsed properly, like contribution_source

Copy link
Contributor

Choose a reason for hiding this comment

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

But i still think we should fix the default section rather than adding additional case as it will break in future if anyone wants to add additional filter through hook.

I believe civicrm_contribution.financial_type_id IN ("3", "1", "4","2") is needed when ACL Financial type is enabled but shouldn't be on default section as it will keep adding same clause.

I am not sure why cancel reason is treated as IN operator rather 'LIKE' or '='

@eileenmcnaughton @monishdeb thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree the default section should work / be fixed. The switch statement mostly exists because of bad coding historically.

-We should really get a test on it too.

Copy link
Contributor Author

@sluc23 sluc23 Sep 4, 2018

Choose a reason for hiding this comment

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

ok, I agree as well that the proper fix is to correct default: code block. But for that fix I'm afraid I won't be able to take the lead to make a new PR
Refactoring that, might have a big impact on the whole Contribution Query class, and every piece of code / feature that's related with it, and I don't think I'm confident on how to fix it properly

@eileenmcnaughton
Copy link
Contributor

@sluc23 can you please test #12775 - which achieves the same thing by fixing the metadata for the field (& using addField to add the field to the form - which is the metadata-based methodology)

@sluc23
Copy link
Contributor Author

sluc23 commented Nov 6, 2018

This PR #12775 fixes the issue. Closing!

@sluc23 sluc23 closed this Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants