From 6856b74f770e258feeeedce94c438436a4efc60d 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 --- C4/Reserves.pm | 7 ++-- Koha/Item.pm | 22 +---------- Koha/Policy/Holds.pm | 60 ++++++++++++++++++++++++++++++ opac/opac-reserve.pl | 2 +- t/db_dependent/Koha/Item.t | 25 +------------ t/db_dependent/Koha/Policy/Holds.t | 54 +++++++++++++++++++++++++++ 6 files changed, 122 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 d6013401de5..e71e689baa8 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -46,6 +46,7 @@ use Koha::Libraries; use Koha::Old::Holds; use Koha::Patrons; use Koha::Plugins; +use Koha::Policy::Holds; use List::MoreUtils qw( any ); @@ -921,7 +922,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)); @@ -1344,7 +1345,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} ); @@ -1386,7 +1387,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 9d49d81d459..73075388d70 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); @@ -484,25 +485,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( @@ -770,7 +752,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 00000000000..d7adde062f3 --- /dev/null +++ b/Koha/Policy/Holds.pm @@ -0,0 +1,60 @@ +package Koha::Policy::Holds; + +# Copyright ByWater Solutions 2014 +# Copyright PTFS Europe 2016 +# +# 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 ea69979113b..e1357a0a198 100755 --- a/opac/opac-reserve.pl +++ b/opac/opac-reserve.pl @@ -479,7 +479,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 ); # items_any_available defined outside of the current loop, # so we avoiding loop inside IsAvailableForItemLevelRequest: diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index b04202165ec..283cb6e6869 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 => 30; +use Test::More tests => 29; use Test::Exception; use Test::MockModule; @@ -2211,26 +2211,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 00000000000..8a8297b902c --- /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.25.1