From fb9e308cdc14549de62f07610a71143db7e2ed10 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 26 May 2023 15:42:08 +0000 Subject: [PATCH] Bug 33847: Database update replaces undefined rules with defaults rather than the value that would be used Bug 29012 introduces a database update that sets the default values for rules that are required but undefined. This functionally changes the results of the circulation rules. Instead, this update should find value that is being used for that rule combo and use that as the rule value, only using the default in the case that the derived rule doesn't exist or has a null value. Test Plan: 1) Check out Koha 22.05.05 2) Create a default all/all/all rule, 3 other rules. Ensure they all have issuelength set to 7, with one of the non-default rules having an issuelength of 14. 3) Delete all but one of the non-default rules with the following query: Delete from circulation_rules where rule_name = 'issuelength' and ( issuelength != 14 or ( branchcode is null and categorycode is null and itemtype is null ) ) limit 2; 4) Check out Koha 22.05.06 4) Run updatedatabase.pl 5) Note the rules were recreated with the value 0 6) Repeat steps 1-4 7) Apply this patch 8) Run updatedatabase.pl 9) Note the rules were recreated, but the value is the derived value from the all/all/all rule! --- installer/data/mysql/db_revs/220600024.pl | 65 +++++++++++++++-------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/installer/data/mysql/db_revs/220600024.pl b/installer/data/mysql/db_revs/220600024.pl index 50c01046c9..f5f58018d7 100755 --- a/installer/data/mysql/db_revs/220600024.pl +++ b/installer/data/mysql/db_revs/220600024.pl @@ -1,5 +1,7 @@ use Modern::Perl; +use Koha::CirculationRules; + return { bug_number => "29012", description => "Some rules are not saved when left blank while editing a 'rule' line in smart-rules.pl", @@ -13,29 +15,46 @@ return { rentaldiscount => 0, decreaseloanholds => '', ); - while (my ($rule_name, $rule_value) = each (%default_rule_values)) { - $dbh->do(q{ - INSERT IGNORE INTO circulation_rules (branchcode, categorycode, itemtype, rule_name, rule_value) - SELECT branchcode, categorycode, itemtype, ?, ? FROM circulation_rules cr - WHERE EXISTS ( - SELECT * FROM circulation_rules cr2 - WHERE - cr2.rule_name="suspension_chargeperiod" - AND ( (cr2.branchcode=cr.branchcode) OR ( ISNULL(cr2.branchcode) AND ISNULL(cr.branchcode) ) ) - AND ( (cr2.categorycode=cr.categorycode) OR ( ISNULL(cr2.categorycode) AND ISNULL(cr.categorycode) ) ) - AND ( (cr2.itemtype=cr.itemtype) OR ( ISNULL(cr2.itemtype) AND ISNULL(cr.itemtype) ) ) - ) - AND NOT EXISTS ( - SELECT * FROM circulation_rules cr2 - WHERE - cr2.rule_name=? - AND ( (cr2.branchcode=cr.branchcode) OR ( ISNULL(cr2.branchcode) AND ISNULL(cr.branchcode) ) ) - AND ( (cr2.categorycode=cr.categorycode) OR ( ISNULL(cr2.categorycode) AND ISNULL(cr.categorycode) ) ) - AND ( (cr2.itemtype=cr.itemtype) OR ( ISNULL(cr2.itemtype) AND ISNULL(cr.itemtype) ) ) - ) - GROUP BY branchcode, categorycode, itemtype - }, undef, $rule_name, $rule_value, $rule_name); + + my $rules = Koha::CirculationRules->search( { rule_name => 'suspension_chargeperiod' } ); + while ( my $rule = $rules->next ) { + foreach my $key ( keys %default_rule_values ) { + next + if # If there is a rule matching these criteria with defined value, we can skip it. + Koha::CirculationRules->search( + { + branchcode => $rule->branchcode, + categorycode => $rule->categorycode, + itemtype => $rule->itemtype, + rule_name => $key, + } + )->count; + + my $derived_rule = Koha::CirculationRules->get_effective_rule( + { + branchcode => $rule->branchcode, + categorycode => $rule->categorycode, + itemtype => $rule->itemtype, + rule_name => $key, + } + ); + my $value = + $derived_rule + ? $derived_rule->rule_value // $default_rule_values{$key} + : $default_rule_values{$key}; + + Koha::CirculationRule->new( + { + branchcode => $rule->branchcode, + categorycode => $rule->categorycode, + itemtype => $rule->itemtype, + rule_name => $key, + rule_value => $value, + } + )->store(); + } } - say $out "Add default values for blank circulation rules that weren't saved to the database"; + + say $out "Set derived values for blank circulation rules that weren't saved to the database"; }, } -- 2.30.2