From 44677f99e7573e329c8f9bab5f8e771077e7f130 Mon Sep 17 00:00:00 2001 From: Arthur Suzuki Date: Fri, 3 Jul 2020 11:58:49 +0200 Subject: [PATCH] Bug 11999: QA follow-up --- C4/Reserves.pm | 22 ++++++++++++++-------- t/db_dependent/Reserves.t | 7 ++++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/C4/Reserves.pm b/C4/Reserves.pm index 633340349d..a63f91f12a 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -317,7 +317,7 @@ sub CanBookBeReserved{ # Check if borrower has reached the maximum number of holds allowed my $maxreserves = C4::Context->preference('maxreserves'); if ($maxreserves && $holds->count >= $maxreserves) { - return { status => 'tooManyReserves' }; + return { status => 'tooManyReserves', limit => $maxreserves }; } my @itemnumbers = Koha::Items->search({ biblionumber => $biblionumber})->get_column("itemnumber"); @@ -365,6 +365,9 @@ sub CanItemBeReserved { my $holds_per_record = 1; # Total number of holds allowed for this one given record my $holds_per_day; # Default to unlimited + # Get calling context + my $caller = (caller(1))[3]; + # we retrieve borrowers and items informations # # item->{itype} will come for biblioitems if necessery my $item = Koha::Items->find($itemnumber); @@ -386,13 +389,6 @@ sub CanItemBeReserved { return { status =>'itemAlreadyOnHold' } if Koha::Holds->search( { borrowernumber => $borrowernumber, itemnumber => $itemnumber } )->count(); - # Check if borrower has reached the maximum number of holds allowed - my $maxreserves = C4::Context->preference('maxreserves'); - my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } ); - if ($maxreserves && $holds->count >= $maxreserves) { - return { status => 'tooManyReserves', limit => $maxreserves }; - } - my $controlbranch = C4::Context->preference('ReservesControlBranch'); my $querycount = q{ @@ -416,6 +412,15 @@ sub CanItemBeReserved { $branchcode = $borrower->{branchcode}; } + # Check if borrower has reached the maximum number of holds allowed + my $holds = $patron->holds; + my $maxreserves = C4::Context->preference('maxreserves'); + if ( index($caller,"CanBookBeReserved") eq -1 + && $maxreserves + && $holds->count >= $maxreserves) { + return { status => 'tooManyReserves', limit => $maxreserves }; + } + # we retrieve rights if ( my $rights = GetHoldRule( $borrower->{'categorycode'}, $item->effective_itemtype, $branchcode ) ) { $ruleitemtype = $rights->{itemtype}; @@ -433,6 +438,7 @@ sub CanItemBeReserved { biblionumber => $item->biblionumber, } ); + if ( defined $holds_per_record && $holds_per_record ne '' && $holds->count() >= $holds_per_record ) { return { status => "tooManyHoldsForThisRecord", limit => $holds_per_record }; diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index c57d084de9..f0b890721e 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -1051,7 +1051,8 @@ subtest 'check maxreserve test' => sub { my $itype = $builder->build_object({ class => "Koha::ItemTypes", value => { notforloan => 0 } }); my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber,notforloan => 0, itype => $itype->itemtype }); my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber, notforloan => 0, itype => $itype->itemtype }); - my $patron = $builder->build_object({ class => "Koha::Patrons" }); + my $patron = $builder->build_object({ class => "Koha::Patrons", + value => { branchcode => $item_2->homebranch }}); # Place a hold on the title for both patrons @@ -1067,8 +1068,8 @@ subtest 'check maxreserve test' => sub { my $borrowernumber = $patron->borrowernumber; - is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblio_2->biblionumber)->{status} , 'tooManyReserves', "Reserving more than maxreserves is forbidden"); - is( C4::Reserves::CanItemBeReserved($borrowernumber, $item_2->itemnumber, $item_2->homebranch)->{status} , 'tooManyReserves', "Reserving more than maxreserves is forbidden"); + is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblio_2->biblionumber)->{status} , 'tooManyReserves', "CanBookBeReserved: Reserving more than maxreserves is forbidden"); + is( C4::Reserves::CanItemBeReserved($borrowernumber, $item_2->itemnumber, $item_2->homebranch)->{status} , 'tooManyReserves', "CanItemBeReserved: Reserving more than maxreserves is forbidden"); }; -- 2.20.1