Bug 30825: Move holds_control_library to Koha::Policy::Holds

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Jonathan Druart 2023-06-07 14:28:42 +02:00 committed by Tomas Cohen Arazi
parent 752fb21b47
commit e9d8cc08c5
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
6 changed files with 120 additions and 48 deletions

View file

@ -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 } );

View file

@ -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<Koha::Patron> object, this method returns a library id, for
the library that is to be used for calculating circulation rules. It relies
on the B<ReservesControlBranch> 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 );

58
Koha/Policy/Holds.pm Normal file
View file

@ -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 <http://www.gnu.org/licenses>.
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<Koha::Item> and I<Koha::Patron> objects, this method returns a library id, for
the library that is to be used for calculating circulation rules. It relies
on the B<ReservesControlBranch> system preference.
=cut
sub holds_control_library {
my ( $class, $item, $patron ) = @_;
return ( C4::Context->preference('ReservesControlBranch') eq 'ItemHomeLibrary' )
? $item->homebranch
: $patron->branchcode;
}
1;

View file

@ -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' &&

View file

@ -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;
};

View file

@ -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 <http://www.gnu.org/licenses>.
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;
};