From 996952c5bd946d2619e76bd14f63e4e46040599f Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 7 Aug 2024 17:15:28 +0100 Subject: [PATCH] Bug 10190: Add support for incrementable circulation rules This patch adds support for _X_ grouped circulation rules to both the internal and external API's for handling circulation rules. This allows us to group overdue_X_rulename rules by an 'X' integer so we can have triggers incremented and grouped together. We also add the new overdue_x_* rule definition here for overdue trigger rules. Sponsored-by: Glasgow Colleges Library Group --- Koha/CirculationRules.pm | 19 +++++++++- Koha/REST/V1/CirculationRules.pm | 65 ++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/Koha/CirculationRules.pm b/Koha/CirculationRules.pm index a44be525df7..f308dd70a51 100644 --- a/Koha/CirculationRules.pm +++ b/Koha/CirculationRules.pm @@ -169,6 +169,22 @@ our $RULE_KINDS = { is_monetary => 1, can_be_blank => 1, }, + overdue_X_delay => { + scope => [ 'branchcode', 'categorycode', 'itemtype' ], + can_be_blank => 0, + }, + overdue_X_mtt => { + scope => [ 'branchcode', 'categorycode', 'itemtype' ], + can_be_blank => 1, + }, + overdue_X_notice => { + scope => [ 'branchcode', 'categorycode', 'itemtype' ], + can_be_blank => 1, + }, + overdue_X_restrict => { + scope => [ 'branchcode', 'categorycode', 'itemtype' ], + can_be_blank => 0, + }, renewalperiod => { scope => [ 'branchcode', 'categorycode', 'itemtype' ], }, @@ -365,7 +381,8 @@ sub set_rule { unless exists $params->{$mandatory_parameter}; } - my $kind_info = $RULE_KINDS->{ $params->{rule_name} }; + ( my $rule_key = $params->{rule_name} ) =~ s/_\d_/_X_/; + my $kind_info = $RULE_KINDS->{$rule_key}; Koha::Exceptions::MissingParameter->throw( "set_rule given unknown rule '$params->{rule_name}'!") unless defined $kind_info; diff --git a/Koha/REST/V1/CirculationRules.pm b/Koha/REST/V1/CirculationRules.pm index bf308ca3463..252a41b2ed6 100644 --- a/Koha/REST/V1/CirculationRules.pm +++ b/Koha/REST/V1/CirculationRules.pm @@ -50,11 +50,7 @@ sub list_rules { my $c = shift->openapi->valid_input or return; return try { - my $effective = $c->param('effective') // 1; - my $kinds = - defined( $c->param('rules') ) - ? [ split /\s*,\s*/, $c->param('rules') ] - : [ keys %{ Koha::CirculationRules->rule_kinds } ]; + my $effective = $c->param('effective') // 1; my $item_type = $c->param('item_type_id'); my $branchcode = $c->param('library_id'); my $patron_category = $c->param('patron_category_id'); @@ -114,6 +110,26 @@ sub list_rules { } } + my $kinds; + if ( defined( $c->param('rules') ) ) { + $kinds = [ split /\s*,\s*/, $c->param('rules') ]; + my $valid_kinds = Koha::CirculationRules->rule_kinds; + for my $rule ( @{$kinds} ) { + ( my $rule_key = $rule ) =~ s/_\d_/_X_/; + return $c->render_invalid_parameter_value( + { + path => '/query/rules', + values => { + uri => '/api/v1/kinds', + field => 'rule_name' + } + } + ) unless $valid_kinds->{$rule_key}; + } + } else { + $kinds = _all_kinds(); + } + my $rules; if ($effective) { @@ -125,7 +141,7 @@ sub list_rules { rules => $kinds } ) // {}; - my $return; + my $return = {}; for my $kind ( @{$kinds} ) { $return->{$kind} = $effective_rules->{$kind}; } @@ -154,7 +170,6 @@ sub list_rules { group_by => [ 'branchcode', 'categorycode', 'itemtype' ] } )->unblessed; - } # Map context into rules @@ -264,4 +279,40 @@ sub set_rules { } } +=head3 _all_kinds + +Utility function to get a list of all valid rule kinds including those that are repeatable + +=cut + +sub _all_kinds { + my @all_valid_kinds; + my @distinct_kinds = Koha::CirculationRules->search( + {}, + { + columns => ['rule_name'], + distinct => 1, + } + )->get_column('rule_name'); + + my %seen; + my $valid_kinds = Koha::CirculationRules->rule_kinds; + for my $kind (@distinct_kinds) { + ( my $kind_key = $kind ) =~ s/_\d_/_X_/; + if ( exists $valid_kinds->{$kind_key} && !$seen{$kind} ) { + push @all_valid_kinds, $kind; + $seen{$kind} = 1; + } + } + + # Add in any unset but valid rules + for my $valid_kind ( keys %{$valid_kinds} ) { + if ( $valid_kind !~ /_X_/ && !$seen{$valid_kind} ) { + push @all_valid_kinds, $valid_kind; + $seen{$valid_kind} = 1; + } + } + return \@all_valid_kinds; +} + 1; -- 2.46.0