From 896655a7c22c1f045bb1c06fd77dae5a87f01974 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Fri, 12 Feb 2016 12:18:35 +0100 Subject: [PATCH] Bug 15516: Holding first available item from x biblios only count 1 reserve Signed-off-by: Josef Moravec Signed-off-by: Benjamin Rokseth Signed-off-by: Michal Denar --- C4/Reserves.pm | 42 +++++++++++++ opac/opac-reserve.pl | 8 ++- t/db_dependent/Reserves/HoldCount.t | 116 ++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 t/db_dependent/Reserves/HoldCount.t diff --git a/C4/Reserves.pm b/C4/Reserves.pm index ccee945aa6..ec870f1411 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -591,6 +591,48 @@ sub CanReserveBeCanceledFromOpac { return $reserve->is_cancelable_from_opac; } +# TODO: Should be moved to Koha::Objects + +=head2 GetHoldCount + + $number = &GetHoldCount($borrowernumber); + +this function returns the number of reservation for a borrower given on input arg. + +=cut + +sub GetHoldCount { + my ($borrowernumber) = @_; + + my $dbh = C4::Context->dbh; + + my $count = 0; #added by 15516 + my $query = " + SELECT COUNT(*) AS counter + FROM reserves + WHERE borrowernumber = ? + AND reserve_group_id is NULL"; #added by 15516 + my $sth = $dbh->prepare($query); + $sth->execute($borrowernumber); + my $row = $sth->fetchrow_hashref; + + # section added by 15516 + $count += $row->{counter}; + + $query = " + SELECT COUNT(DISTINCT reserve_group_id) AS counter + FROM reserves + WHERE borrowernumber = ? + AND reserve_group_id is not NULL + "; + $sth = $dbh->prepare($query); + $sth->execute($borrowernumber); + $row = $sth->fetchrow_hashref; + $count += $row->{counter}; + # end of section added by 15516 + return $count; +} + =head2 GetOtherReserves ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber); diff --git a/opac/opac-reserve.pl b/opac/opac-reserve.pl index b0ed088689..252013d590 100755 --- a/opac/opac-reserve.pl +++ b/opac/opac-reserve.pl @@ -195,7 +195,8 @@ foreach my $biblioNumber (@biblionumbers) { if ( $query->param('place_reserve') ) { my $reserve_cnt = 0; if ($maxreserves) { - $reserve_cnt = $patron->holds->count; + # TODO: Should be: $reserve_cnt = $patron->holds->count; + $reserve_cnt = GetHoldCount( $borrowernumber ); } # List is composed of alternating biblio/item/branch @@ -374,12 +375,13 @@ if ( $patron->is_debarred ) { } my $holds = $patron->holds; -my $reserves_count = $holds->count; +#TODO: Should be: my $reserves_count = $holds->count; +my $reserves_count = GetHoldCount($borrowernumber); $template->param( RESERVES => $holds->unblessed ); if ( $maxreserves && ( $reserves_count >= $maxreserves ) ) { $template->param( message => 1 ); $noreserves = 1; - $template->param( too_many_reserves => $holds->count ); + $template->param( too_many_reserves => $reserves_count ); } unless ( $noreserves ) { diff --git a/t/db_dependent/Reserves/HoldCount.t b/t/db_dependent/Reserves/HoldCount.t new file mode 100644 index 0000000000..8a24ddcccf --- /dev/null +++ b/t/db_dependent/Reserves/HoldCount.t @@ -0,0 +1,116 @@ +#!/usr/bin/perl + +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Test::More tests => 3; +use t::lib::TestBuilder; + +use C4::Context; +use C4::Biblio; +use C4::Items; +use C4::Reserves; + +use Koha::Database; + + + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +my $builder = t::lib::TestBuilder->new(); + +my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; +my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; +my $borrower = $builder->build({ + source => 'Borrower', + value => { + branchcode => $branchcode, + categorycode => $categorycode + } +}); +my $borrowernumber = $borrower->{borrowernumber}; + +my $biblio = $builder->build( { source => 'Biblio', } ); +my $biblionumber = $biblio->{biblionumber}; + +# Use AddReserve instead of TestBuilder because i don't manage +# to make TestBuilder set reserve_group_id to null. Idea ? +my $reserve_id = AddReserve($branchcode, $borrowernumber, +$biblionumber, $biblionumber, 1, undef, undef, undef +$biblio->{title}, undef, undef, undef); + +is(GetHoldCount($borrowernumber), 1, 'Test borrower has 1 reserve.'); + +my $reserves_group = $builder->build({ + source => 'ReserveGroup', + value => { + reserve_group_id => 1 + } +}); + +my $reserve2 = $builder->build({ + source => 'Reserve', + value => { + borrowernumber => $borrowernumber, + reserve_group_id => 1 + } +}); +my $reserve3 = $builder->build({ + source => 'Reserve', + value => { + borrowernumber => $borrowernumber, + reserve_group_id => 1 + } +}); + +is(GetHoldCount($borrowernumber), 2, 'Test borrower has 2 reserve.'); + +my $reserves_group2 = $builder->build({ + source => 'ReserveGroup', + value => { + reserve_group_id => 2 + } +}); +my $reserve_group_id2 = $reserves_group2->{reserve_group_id}; + +my $reserve4 = $builder->build({ + source => 'Reserve', + value => { + borrowernumber => $borrowernumber, + reserve_group_id => 2 + } +}); +my $reserve5 = $builder->build({ + source => 'Reserve', + value => { + borrowernumber => $borrowernumber, + reserve_group_id => 2 + } +}); +my $reserve6 = $builder->build({ + source => 'Reserve', + value => { + borrowernumber => $borrowernumber, + reserve_group_id => 2 + } +}); + +is(GetHoldCount($borrowernumber), 3, 'Test borrower has 2 reserve.'); + +$dbh->rollback; -- 2.11.0