From e9d8cc08c500ceb48b6394a9b100c3ac236ce0a9 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 7 Jun 2023 14:28:42 +0200 Subject: [PATCH] Bug 30825: Move holds_control_library to Koha::Policy::Holds Signed-off-by: David Nind Signed-off-by: Tomas Cohen Arazi --- C4/Reserves.pm | 7 ++-- Koha/Item.pm | 22 ++---------- Koha/Policy/Holds.pm | 58 ++++++++++++++++++++++++++++++ opac/opac-reserve.pl | 2 +- t/db_dependent/Koha/Item.t | 25 +------------ t/db_dependent/Koha/Policy/Holds.t | 54 ++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 48 deletions(-) create mode 100644 Koha/Policy/Holds.pm create mode 100755 t/db_dependent/Koha/Policy/Holds.t diff --git a/C4/Reserves.pm b/C4/Reserves.pm index a28e3dcba3..3625737885 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -47,6 +47,7 @@ use Koha::Libraries; use Koha::Old::Holds; use Koha::Patrons; use Koha::Plugins; +use Koha::Policy::Holds; use List::MoreUtils qw( any ); @@ -937,7 +938,7 @@ sub CheckReserves { next if $res->{item_group_id} && ( !$item->item_group || $item->item_group->id != $res->{item_group_id} ); next if $res->{itemtype} && $res->{itemtype} ne $item->effective_itemtype; $patron //= Koha::Patrons->find( $res->{borrowernumber} ); - my $branch = $item->holds_control_library( $patron ); + my $branch = Koha::Policy::Holds->holds_control_library( $item, $patron ); my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$item->effective_itemtype); next if ($branchitemrule->{'holdallowed'} eq 'not_allowed'); next if (($branchitemrule->{'holdallowed'} eq 'from_home_library') && ($item->homebranch ne $patron->branchcode)); @@ -1357,7 +1358,7 @@ sub IsAvailableForItemLevelRequest { return 0 unless $destination; return 0 unless $destination->pickup_location; return 0 unless $item->can_be_transferred( { to => $destination } ); - my $reserves_control_branch = $item->holds_control_library( $patron ); + my $reserves_control_branch = Koha::Policy::Holds->holds_control_library( $item, $patron ); my $branchitemrule = C4::Circulation::GetBranchItemRule( $reserves_control_branch, $item->itype ); my $home_library = Koha::Libraries->find( {branchcode => $item->homebranch} ); @@ -1406,7 +1407,7 @@ sub ItemsAnyAvailableAndNotRestricted { my @items = Koha::Items->search( { biblionumber => $param->{biblionumber} } )->as_list; foreach my $i (@items) { - my $reserves_control_branch = $i->holds_control_library( $param->{patron} ); + my $reserves_control_branch = Koha::Policy::Holds->holds_control_library( $i, $param->{patron} ); my $branchitemrule = C4::Circulation::GetBranchItemRule( $reserves_control_branch, $i->itype ); my $item_library = Koha::Libraries->find( { branchcode => $i->homebranch } ); diff --git a/Koha/Item.pm b/Koha/Item.pm index 935f33ceb2..9f41eb16ef 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -54,6 +54,7 @@ use Koha::SearchEngine::Indexer; use Koha::StockRotationItem; use Koha::StockRotationRotas; use Koha::TrackedLinks; +use Koha::Policy::Holds; use base qw(Koha::Object); @@ -483,25 +484,6 @@ sub holds { return Koha::Holds->_new_from_dbic( $holds_rs ); } -=head3 holds_control_library - - my $control_library = $item->holds_control_library( $patron ); - -Given a I object, this method returns a library id, for -the library that is to be used for calculating circulation rules. It relies -on the B system preference. - -=cut - -sub holds_control_library { - my ( $self, $patron ) = @_; - - return ( - C4::Context->preference('ReservesControlBranch') eq 'ItemHomeLibrary' ) - ? $self->homebranch - : $patron->branchcode; -} - =head3 request_transfer my $transfer = $item->request_transfer( @@ -759,7 +741,7 @@ sub pickup_locations { my $patron = $params->{patron}; - my $circ_control_branch = $self->holds_control_library( $patron ); + my $circ_control_branch = Koha::Policy::Holds->holds_control_library( $self, $patron ); my $branchitemrule = C4::Circulation::GetBranchItemRule( $circ_control_branch, $self->itype ); diff --git a/Koha/Policy/Holds.pm b/Koha/Policy/Holds.pm new file mode 100644 index 0000000000..f08eee6740 --- /dev/null +++ b/Koha/Policy/Holds.pm @@ -0,0 +1,58 @@ +package Koha::Policy::Holds; + +# Copyright 2023 Koha Development team +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use C4::Context; + +=head1 NAME + +Koha::Policy::Holds - module to deal with holds policy + +=head1 API + +=head2 Class Methods + +=head3 new + +=cut + +sub new { + return bless {}, shift; +} + +=head3 holds_control_library + + my $control_library = Koha::Policy::Holds->holds_control_library( $item, $patron ); + +Given I and I objects, this method returns a library id, for +the library that is to be used for calculating circulation rules. It relies +on the B system preference. + +=cut + +sub holds_control_library { + my ( $class, $item, $patron ) = @_; + + return ( C4::Context->preference('ReservesControlBranch') eq 'ItemHomeLibrary' ) + ? $item->homebranch + : $patron->branchcode; +} + +1; diff --git a/opac/opac-reserve.pl b/opac/opac-reserve.pl index 3e1878e9cc..38150b04df 100755 --- a/opac/opac-reserve.pl +++ b/opac/opac-reserve.pl @@ -477,7 +477,7 @@ foreach my $biblioNum (@biblionumbers) { $item_info->{hosttitle} = Koha::Biblios->find( $item_info->{biblionumber} )->title; } - my $branch = $item->holds_control_library( $patron ); + my $branch = Koha::Policy::Holds->holds_control_library( $item, $patron ); my $policy_holdallowed = CanItemBeReserved( $patron, $item, undef, { get_from_cache => 1 } )->{status} eq 'OK' && diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index d7f530da7e..176e8feed1 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -20,7 +20,7 @@ use Modern::Perl; use utf8; -use Test::More tests => 31; +use Test::More tests => 30; use Test::Exception; use Test::MockModule; @@ -2295,26 +2295,3 @@ subtest 'current_branchtransfers relationship' => sub { $schema->storage->txn_rollback; }; - -subtest 'holds_control_library() tests' => sub { - - plan tests => 2; - - $schema->storage->txn_begin; - - my $library_1 = $builder->build_object({ class => 'Koha::Libraries' }); - my $library_2 = $builder->build_object({ class => 'Koha::Libraries' }); - - my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_1->id } }); - my $item = $builder->build_sample_item({ library => $library_2->id }); - - t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' ); - - is( $item->holds_control_library( $patron ), $library_2->id ); - - t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' ); - - is( $item->holds_control_library( $patron ), $library_1->id ); - - $schema->storage->txn_rollback; -}; diff --git a/t/db_dependent/Koha/Policy/Holds.t b/t/db_dependent/Koha/Policy/Holds.t new file mode 100755 index 0000000000..fc54c886f6 --- /dev/null +++ b/t/db_dependent/Koha/Policy/Holds.t @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +# Copyright 2023 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; + +use Koha::Database; +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'holds_control_library() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library_1->id } } ); + my $item = $builder->build_sample_item( { library => $library_2->id } ); + + t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' ); + + my $policy = Koha::Policy::Holds->new; + + is( $policy->holds_control_library( $item, $patron ), $library_2->id ); + + t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' ); + + is( $policy->holds_control_library( $item, $patron ), $library_1->id ); + + $schema->storage->txn_rollback; +}; -- 2.39.5