View | Details | Raw Unified | Return to bug 30825
Collapse All | Expand All

(-)a/C4/Reserves.pm (-3 / +4 lines)
Lines 46-51 use Koha::Libraries; Link Here
46
use Koha::Old::Holds;
46
use Koha::Old::Holds;
47
use Koha::Patrons;
47
use Koha::Patrons;
48
use Koha::Plugins;
48
use Koha::Plugins;
49
use Koha::Policy::Holds;
49
50
50
use List::MoreUtils qw( any );
51
use List::MoreUtils qw( any );
51
52
Lines 921-927 sub CheckReserves { Link Here
921
                    next if $res->{item_group_id} && ( !$item->item_group || $item->item_group->id != $res->{item_group_id} );
922
                    next if $res->{item_group_id} && ( !$item->item_group || $item->item_group->id != $res->{item_group_id} );
922
                    next if $res->{itemtype} && $res->{itemtype} ne $item->effective_itemtype;
923
                    next if $res->{itemtype} && $res->{itemtype} ne $item->effective_itemtype;
923
                    $patron //= Koha::Patrons->find( $res->{borrowernumber} );
924
                    $patron //= Koha::Patrons->find( $res->{borrowernumber} );
924
                    my $branch = $item->holds_control_library( $patron );
925
                    my $branch = Koha::Policy::Holds->holds_control_library( $item, $patron );
925
                    my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$item->effective_itemtype);
926
                    my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$item->effective_itemtype);
926
                    next if ($branchitemrule->{'holdallowed'} eq 'not_allowed');
927
                    next if ($branchitemrule->{'holdallowed'} eq 'not_allowed');
927
                    next if (($branchitemrule->{'holdallowed'} eq 'from_home_library') && ($item->homebranch ne $patron->branchcode));
928
                    next if (($branchitemrule->{'holdallowed'} eq 'from_home_library') && ($item->homebranch ne $patron->branchcode));
Lines 1344-1350 sub IsAvailableForItemLevelRequest { Link Here
1344
        return 0 unless $destination;
1345
        return 0 unless $destination;
1345
        return 0 unless $destination->pickup_location;
1346
        return 0 unless $destination->pickup_location;
1346
        return 0 unless $item->can_be_transferred( { to => $destination } );
1347
        return 0 unless $item->can_be_transferred( { to => $destination } );
1347
        my $reserves_control_branch = $item->holds_control_library( $patron );
1348
        my $reserves_control_branch = Koha::Policy::Holds->holds_control_library( $item, $patron );
1348
        my $branchitemrule =
1349
        my $branchitemrule =
1349
            C4::Circulation::GetBranchItemRule( $reserves_control_branch, $item->itype );
1350
            C4::Circulation::GetBranchItemRule( $reserves_control_branch, $item->itype );
1350
        my $home_library = Koha::Libraries->find( {branchcode => $item->homebranch} );
1351
        my $home_library = Koha::Libraries->find( {branchcode => $item->homebranch} );
Lines 1386-1392 sub ItemsAnyAvailableAndNotRestricted { Link Here
1386
    my @items = Koha::Items->search( { biblionumber => $param->{biblionumber} } )->as_list;
1387
    my @items = Koha::Items->search( { biblionumber => $param->{biblionumber} } )->as_list;
1387
1388
1388
    foreach my $i (@items) {
1389
    foreach my $i (@items) {
1389
        my $reserves_control_branch = $i->holds_control_library( $param->{patron} );
1390
        my $reserves_control_branch = Koha::Policy::Holds->holds_control_library( $i, $param->{patron} );
1390
        my $branchitemrule =
1391
        my $branchitemrule =
1391
            C4::Circulation::GetBranchItemRule( $reserves_control_branch, $i->itype );
1392
            C4::Circulation::GetBranchItemRule( $reserves_control_branch, $i->itype );
1392
        my $item_library = Koha::Libraries->find( { branchcode => $i->homebranch } );
1393
        my $item_library = Koha::Libraries->find( { branchcode => $i->homebranch } );
(-)a/Koha/Item.pm (-20 / +2 lines)
Lines 54-59 use Koha::SearchEngine::Indexer; Link Here
54
use Koha::StockRotationItem;
54
use Koha::StockRotationItem;
55
use Koha::StockRotationRotas;
55
use Koha::StockRotationRotas;
56
use Koha::TrackedLinks;
56
use Koha::TrackedLinks;
57
use Koha::Policy::Holds;
57
58
58
use base qw(Koha::Object);
59
use base qw(Koha::Object);
59
60
Lines 484-508 sub holds { Link Here
484
    return Koha::Holds->_new_from_dbic( $holds_rs );
485
    return Koha::Holds->_new_from_dbic( $holds_rs );
485
}
486
}
486
487
487
=head3 holds_control_library
488
489
    my $control_library = $item->holds_control_library( $patron );
490
491
Given a I<Koha::Patron> object, this method returns a library id, for
492
the library that is to be used for calculating circulation rules. It relies
493
on the B<ReservesControlBranch> system preference.
494
495
=cut
496
497
sub holds_control_library {
498
    my ( $self, $patron ) = @_;
499
500
    return (
501
        C4::Context->preference('ReservesControlBranch') eq 'ItemHomeLibrary' )
502
      ? $self->homebranch
503
      : $patron->branchcode;
504
}
505
506
=head3 request_transfer
488
=head3 request_transfer
507
489
508
  my $transfer = $item->request_transfer(
490
  my $transfer = $item->request_transfer(
Lines 770-776 sub pickup_locations { Link Here
770
752
771
    my $patron = $params->{patron};
753
    my $patron = $params->{patron};
772
754
773
    my $circ_control_branch = $self->holds_control_library( $patron );
755
    my $circ_control_branch = Koha::Policy::Holds->holds_control_library( $self, $patron );
774
    my $branchitemrule =
756
    my $branchitemrule =
775
      C4::Circulation::GetBranchItemRule( $circ_control_branch, $self->itype );
757
      C4::Circulation::GetBranchItemRule( $circ_control_branch, $self->itype );
776
758
(-)a/Koha/Policy/Holds.pm (+59 lines)
Line 0 Link Here
1
package Koha::Policy::Holds;
2
3
# Copyright 2023 Koha Development team
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use C4::Context;
23
24
=head1 NAME
25
26
Koha::Policy::Holds - module to deal with holds policy
27
28
=head1 API
29
30
=head2 Class Methods
31
32
=head3 new
33
34
=cut
35
36
sub new {
37
    return bless {}, shift;
38
}
39
40
=head3 holds_control_library
41
42
    my $control_library = Koha::Policy::Holds->holds_control_library( $item, $patron );
43
44
Given I<Koha::Item> and I<Koha::Patron> objects, this method returns a library id, for
45
the library that is to be used for calculating circulation rules. It relies
46
on the B<ReservesControlBranch> system preference.
47
48
=cut
49
50
sub holds_control_library {
51
    my ( $class, $item, $patron ) = @_;
52
53
    return (
54
        C4::Context->preference('ReservesControlBranch') eq 'ItemHomeLibrary' )
55
      ? $item->homebranch
56
      : $patron->branchcode;
57
}
58
59
1;
(-)a/opac/opac-reserve.pl (-1 / +1 lines)
Lines 479-485 foreach my $biblioNum (@biblionumbers) { Link Here
479
            $item_info->{hosttitle}        = Koha::Biblios->find( $item_info->{biblionumber} )->title;
479
            $item_info->{hosttitle}        = Koha::Biblios->find( $item_info->{biblionumber} )->title;
480
        }
480
        }
481
481
482
        my $branch = $item->holds_control_library( $patron );
482
        my $branch = Koha::Policy::Holds->holds_control_library( $item, $patron );
483
483
484
        # items_any_available defined outside of the current loop,
484
        # items_any_available defined outside of the current loop,
485
        # so we avoiding loop inside IsAvailableForItemLevelRequest:
485
        # so we avoiding loop inside IsAvailableForItemLevelRequest:
(-)a/t/db_dependent/Koha/Item.t (-24 / +1 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
use utf8;
21
use utf8;
22
22
23
use Test::More tests => 30;
23
use Test::More tests => 29;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
26
26
Lines 2211-2236 subtest 'current_branchtransfers relationship' => sub { Link Here
2211
2211
2212
    $schema->storage->txn_rollback;
2212
    $schema->storage->txn_rollback;
2213
};
2213
};
2214
2215
subtest 'holds_control_library() tests' => sub {
2216
2217
    plan tests => 2;
2218
2219
    $schema->storage->txn_begin;
2220
2221
    my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
2222
    my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
2223
2224
    my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_1->id } });
2225
    my $item   = $builder->build_sample_item({ library => $library_2->id });
2226
2227
    t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
2228
2229
    is( $item->holds_control_library( $patron ), $library_2->id );
2230
2231
    t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
2232
2233
    is( $item->holds_control_library( $patron ), $library_1->id );
2234
2235
    $schema->storage->txn_rollback;
2236
};
(-)a/t/db_dependent/Koha/Policy/Holds.t (-1 / +54 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2023 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 1;
23
24
use Koha::Database;
25
use t::lib::Mocks;
26
use t::lib::TestBuilder;
27
28
my $schema  = Koha::Database->new->schema;
29
my $builder = t::lib::TestBuilder->new;
30
31
subtest 'holds_control_library() tests' => sub {
32
33
    plan tests => 2;
34
35
    $schema->storage->txn_begin;
36
37
    my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
38
    my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
39
40
    my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library_1->id } });
41
    my $item   = $builder->build_sample_item({ library => $library_2->id });
42
43
    t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
44
45
    my $policy = Koha::Policy::Holds->new;
46
47
    is( $policy->holds_control_library( $item, $patron ), $library_2->id );
48
49
    t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
50
51
    is( $policy->holds_control_library( $item, $patron ), $library_1->id );
52
53
    $schema->storage->txn_rollback;
54
};

Return to bug 30825