From 56dd483b4ec48ad42e078b880410e9b282a29f4f Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 19 Nov 2021 13:27:44 +0100 Subject: [PATCH] Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed --- Koha/Items.pm | 30 ++++++++++++++++++++++++++---- t/db_dependent/Koha/Holds.t | 7 +++++++ t/db_dependent/Koha/Items.t | 7 +++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Koha/Items.pm b/Koha/Items.pm index 0730935a0b9..b5ffbd29d54 100644 --- a/Koha/Items.pm +++ b/Koha/Items.pm @@ -61,15 +61,37 @@ placing a hold on one of those items. sub filter_by_for_hold { my ($self) = @_; - my @hold_not_allowed_itypes = Koha::CirculationRules->search( + my $default_rule = Koha::CirculationRules->get_effective_rule( { rule_name => 'holdallowed', - branchcode => undef, - categorycode => undef, rule_value => 'not_allowed', } - )->get_column('itemtype'); + ); + my @hold_not_allowed_itypes; + if ( $default_rule ) { + @hold_not_allowed_itypes = Koha::ItemTypes->search->get_column('itemtype'); + my @hold_allowed_itypes = Koha::CirculationRules->search( + { + rule_name => 'holdallowed', + rule_value => { '!=' => 'not_allowed' }, + branchcode => undef, + categorycode => undef, + } + )->get_column('itemtype'); + @hold_not_allowed_itypes = array_minus( @hold_not_allowed_itypes, @hold_allowed_itypes ) + } else { + @hold_not_allowed_itypes = Koha::CirculationRules->search( + { + rule_name => 'holdallowed', + branchcode => undef, + categorycode => undef, + rule_value => 'not_allowed', + } + )->get_column('itemtype'); + } + push @hold_not_allowed_itypes, Koha::ItemTypes->search({ notforloan => 1 })->get_column('itemtype'); + @hold_not_allowed_itypes = uniq @hold_not_allowed_itypes; my $params = { itemlost => 0, diff --git a/t/db_dependent/Koha/Holds.t b/t/db_dependent/Koha/Holds.t index 51c33832771..05e213aeec3 100755 --- a/t/db_dependent/Koha/Holds.t +++ b/t/db_dependent/Koha/Holds.t @@ -412,6 +412,13 @@ subtest 'Desks' => sub { subtest 'get_items_that_can_fill' => sub { plan tests => 2; + Koha::CirculationRules->search( + { + rule_name => 'holdallowed', + rule_value => 'not_allowed', + } + )->delete; + my $biblio = $builder->build_sample_biblio; my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' }); # For 1, 2, 3, 4 my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' }); diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t index dc258b45106..33fccac8dab 100755 --- a/t/db_dependent/Koha/Items.t +++ b/t/db_dependent/Koha/Items.t @@ -1512,6 +1512,13 @@ subtest 'can_be_transferred' => sub { subtest 'filter_by_for_hold' => sub { plan tests => 12; + Koha::CirculationRules->search( + { + rule_name => 'holdallowed', + rule_value => 'not_allowed', + } + )->delete; + my $biblio = $builder->build_sample_biblio; is( $biblio->items->filter_by_for_hold->count, 0, 'no item yet' ); $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 1 } ); -- 2.25.1