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 previous before and previous 2 fiscal year options #24752

Conversation

larssandergreen
Copy link
Contributor

@larssandergreen larssandergreen commented Oct 14, 2022

Overview

Having the Year prior to previous fiscal year and Previous 2 fiscal years relative date options would be convenient and enable useful SearchKit searches without manually entering and updating dates (e.g. people who donated two fiscal years ago and last fiscal year or people who donated in the last 2 fiscal years, but not this fiscal year).

Luckily, these were already present and working properly in code, so the options just needed to be added to enable them.

Before

Year prior to previous fiscal year and Previous 2 fiscal years relative date options not present.

After

Year prior to previous fiscal year and Previous 2 fiscal years relative date options present.

Comments

Have tested the upgrade, but not the fresh install. Trying to use civibuild with --patch, but I get "ERROR: Missing main script (/download.sh)".

Is there any way to test civicrm_data.tpl without a new install?

@civibot
Copy link

civibot bot commented Oct 14, 2022

(Standard links)

@civibot civibot bot added the master label Oct 14, 2022
@demeritcowboy
Copy link
Contributor

demeritcowboy commented Oct 14, 2022

Is there any way to test civicrm_data.tpl without a new install?

Not really. But you'll need to generate a sql/civicrm_generated.mysql which indirectly tests it because the test site here will use that. Somebody wrote this cool script that allows you to do that online in your fork/branch: https://github.com/civicrm/civicrm-core/blob/master/.github/workflows/regen.yml. Or you can run bin/regen.sh locally just note it wipes your db.

UPDATE `civicrm_option_value` SET weight = 57 WHERE option_group_id = @option_group_id_date_filter AND weight = 56;
UPDATE `civicrm_option_value` SET weight = 56 WHERE option_group_id = @option_group_id_date_filter AND weight = 55;
UPDATE `civicrm_option_value` SET weight = 55 WHERE option_group_id = @option_group_id_date_filter AND weight = 54;
INSERT INTO
Copy link
Contributor

Choose a reason for hiding this comment

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

INSERT IGNORE INTO
in case somebody reruns the upgrade, or has already made this customization which believe it or not is supported/documented: https://docs.civicrm.org/user/en/latest/searching/relative-date-formats/#adding-new-filters

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks.

INSERT INTO
`civicrm_option_value` (`option_group_id`, `label`, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `description`, `is_optgroup`, `is_reserved`, `is_active`, `component_id`, `visibility_id`, `icon`)
VALUES
(@option_group_id_date_filter, {localize}'{ts escape="sql"}Previous 2 fiscal years{/ts}'{/localize}, 'previous_2.fiscal_year', 'previous_2.fiscal_year', NULL, NULL, 0,54, NULL, 0, 0, 1, NULL, NULL, NULL),
Copy link
Contributor

@demeritcowboy demeritcowboy Oct 14, 2022

Choose a reason for hiding this comment

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

I'm not really familiar with {localize}, but it looks like you'd need to also localize the INSERT list, e.g.

INSERT IGNORE INTO civicrm_option_value (option_group_id, {localize field='label'}label{/localize}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@larssandergreen
Copy link
Contributor Author

Somebody wrote this cool script that allows you to do that online in your fork/branch: https://github.com/civicrm/civicrm-core/blob/master/.github/workflows/regen.yml. Or you can run bin/regen.sh locally just note it wipes your db.

Great, I added the script option to the dev guide.

@larssandergreen larssandergreen force-pushed the add-previous-before-and-previous-2-fiscal-years branch from 2c28ec7 to e384e70 Compare October 15, 2022 02:48
@larssandergreen larssandergreen changed the title WIP: Add previous before and previous 2 fiscal year options Add previous before and previous 2 fiscal year options Oct 15, 2022
@larssandergreen larssandergreen marked this pull request as ready for review October 15, 2022 02:49
@@ -1,5 +1,23 @@
{* file to handle db changes in 5.56.alpha1 during upgrade *}

-- Add in Year prior to previous fiscal year and Previous 2 fiscal years
SELECT @option_group_id_date_filter := max(id) from civicrm_option_group where name = 'relative_date_filters';
UPDATE `civicrm_option_value` SET weight = 64 WHERE option_group_id = @option_group_id_date_filter AND weight = 62;
Copy link
Contributor

Choose a reason for hiding this comment

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

I was wrong about the insert ignore because this table allows two with the same name, so if someone reruns the upgrade you get two (or more) copies of the rows. Also these update statements are going to keep shifting the weight each time the upgrade is run. Or if someone has manually rearranged then the insert might not be in the "right" place.

So after all that wrangling:
(a) While nice-to-have, given that someone can just add their own option values and it's documented, how much more time do you want to spend fighting with it?
(b) You could do this in php using ensureOptionValueExists, and this multilingual technique and maybe just leave the weight updating out and insert at max(weight)+1.

Copy link
Contributor Author

@larssandergreen larssandergreen Oct 15, 2022

Choose a reason for hiding this comment

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

Yes, I see this could get messy. I did wonder a bit about the order shifting. Thanks for helping out with this even though it's pretty unimportant (but we've come this far...)

ensureOptionValueExists does seem like a much better way to go.

For the order, I've done some experimenting and if you specify a weight in API4, it cleverly increments all the higher weight options, so nothing else needs to be changed. I can update ensureOptionValueExists to use API4. I could also add a param called insert_after, which looks up the name of the option (in the specified option group) we want to insert after and then sets the weight to one higher (or does not set a weight if we don't find the option, in which case it gets added as max weight + 1.

That leaves the multilingual part. How about adding that multilingual logic to ensureOptionValueExists, so this is easier in the future?
If we just created an option value and we're multilingual, for each locale, update the row with the translated label and description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or probably better just to create a new function that uses API4 and does localization and leave this one as is to ensure compatibility.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you want to split this into 2 PRs - one that only adds it on new installs and then a separate for the upgrade, I think it would be an easy merge for the first one.

Also FYI there's some open questions about how much api should be used in upgrade steps - see here and here.

Copy link
Contributor Author

@larssandergreen larssandergreen Oct 16, 2022

Choose a reason for hiding this comment

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

OK, keep it low-level. So it turns out there's actually a readme that specifically covers how to add option values (I've added a link to this in the docs).

But the SQL in there still doesn't deal with the case where the option already exists, so I've done that in SQL as well. If that makes sense, I can update the readme along the lines of this example.

I can still split this — or maybe it's getting there now?

@yashodha
Copy link
Contributor

@demeritcowboy @eileenmcnaughton @larssandergreen I suggest we take this opportunity to add hook to relativeToAbsolute method so that developers can add their own definition of range and what it translates to in an extension without having to add any more options here :

https://github.com/civicrm/civicrm-core/blob/master/CRM/Utils/Date.php#L1911

@magnolia61
Copy link
Contributor

I think the name 'year before previous fiscal year' is ambiguous. Might be better to call it 'fiscal year before previous fiscal year'. But this might be also a change in core of it is already in there.

@demeritcowboy
Copy link
Contributor

add hook to relativeToAbsolute method so that developers can add their own definition of range and what it translates to in an extension without having to add any more options here

@yashodha It's an interesting idea but seems out-of-scope here. Do you want to make a lab ticket?

@larssandergreen
Copy link
Contributor Author

@magnolia61 Fair enough. I was trying to keep it short, but I agree it is more clear and have updated to "Fiscal year before prior to previous fiscal year".

@yashodha
Copy link
Contributor

@demeritcowboy sure will do. Thanks!

@demeritcowboy
Copy link
Contributor

It seems previous_2.fiscal_year is not consistent with previous_2.year. But it's not because of the PR so is a separate issue, but could lead to confusion. Note the difference in the QILL (2021 vs 2022):

Previous 2 calendar years (between January 1st, 2020 12:00 AM and December 31st, 2021 11:59 PM)

Previous 2 fiscal years (between January 1st, 2020 12:00 AM and December 31st, 2022 11:59 PM)

Will finish testing the rest of this later today.

@demeritcowboy
Copy link
Contributor

Lab ticket for the other issue: https://lab.civicrm.org/dev/core/-/issues/3922

@demeritcowboy
Copy link
Contributor

So the scope of the problems are smaller than I thought, so since upgrade PR's go stale fast and because I've tested this otherwise in terms of its mechanics, I think it's mergeable. Install, upgrade, multilingual all work. Then can look at the additional PR.

@demeritcowboy demeritcowboy added the merge ready PR will be merged after a few days if there are no objections label Oct 19, 2022
@demeritcowboy
Copy link
Contributor

It might be slightly cleaner to squash the commits so have put merge-ready.

@larssandergreen larssandergreen force-pushed the add-previous-before-and-previous-2-fiscal-years branch from ca2679f to 74c4e3a Compare October 19, 2022 19:43
@larssandergreen
Copy link
Contributor Author

Thanks @demeritcowboy. I added the SQL for the upgrade as an example for handling values that might already exist to the readme for SQL upgrades and squashed.

@demeritcowboy demeritcowboy added merge on pass and removed merge ready PR will be merged after a few days if there are no objections labels Oct 19, 2022
@demeritcowboy demeritcowboy merged commit 116a6b0 into civicrm:master Oct 19, 2022
@eileenmcnaughton
Copy link
Contributor

Nice - @larssandergreen can you check the docs https://docs.civicrm.org/user/en/latest/searching/relative-date-formats/ & see if this triggers an update?

@yashodha I think to your point there are 2 things

  • options that follow an established patterns (ie those that can be added like this by just adding more option values)
  • options that follow a custom pattern that require a hook. At some point we probably need to figure out what this would look like

In terms of adding more options - at some point it makes sense to add them as managed entities in an extension - ie if they seem obscure / overloading the UI. I don't know when we hit that point & will take @demeritcowboy & @larssandergreen judgement that this PR was not that point

@larssandergreen
Copy link
Contributor Author

@eileenmcnaughton Yes, have already done the edits to add these two options to the docs.

@eileenmcnaughton
Copy link
Contributor

thanks @larssandergreen

@demeritcowboy
Copy link
Contributor

I'm not sure what's different now but I'm getting an error on upgrade where it doesn't like the WHERE, i.e. this part is fine:

SELECT @option_group_id_date_filter, {localize}'{ts escape="sql"}Previous 2 fiscal years{/ts}'{/localize}, 'previous_2.fiscal_year', 'previous_2.fiscal_year', NULL, NULL, 0, (SELECT @max_wt := @max_wt+1), {localize}NULL{/localize}, 0, 0, 1, NULL, NULL, NULL

but it doesn't like the WHERE that comes after, even if I change it to something simple like WHERE 1=1. It says syntax error. Does anyone else get this?

@demeritcowboy
Copy link
Contributor

Might be a mariadb thing. If I add FROM DUAL before the WHERE it's happy. Going to check some more things first.

@larssandergreen larssandergreen deleted the add-previous-before-and-previous-2-fiscal-years branch November 5, 2022 00:09
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