From 944637868f299dece7b86c75f2b12f8289ef042d Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
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 <josef.moravec@gmail.com>
Signed-off-by: Benjamin Rokseth <benjamin.rokseth@kul.oslo.kommune.no>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Michal Denar <black23@gmail.com>

Signed-off-by: Michal Denar <black23@gmail.com>
---
 C4/Reserves.pm                         |  41 ++++++++++++
 opac/opac-reserve.pl                   |   8 ++-
 t/db_dependent/Reserves/ReserveCount.t | 116 +++++++++++++++++++++++++++++++++
 3 files changed, 162 insertions(+), 3 deletions(-)
 create mode 100644 t/db_dependent/Reserves/ReserveCount.t

diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index ffd198f..1b9ffdf 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -515,6 +515,47 @@ sub CanReserveBeCanceledFromOpac {
 
 }
 
+# TODO: Should be moved to Koha::Objects
+=head2 GetReserveCount
+
+  $number = &GetReserveCount($borrowernumber);
+
+this function returns the number of reservation for a borrower given on input arg.
+
+=cut
+
+sub GetReserveCount {
+    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 472d0f5..f32eae9 100755
--- a/opac/opac-reserve.pl
+++ b/opac/opac-reserve.pl
@@ -193,7 +193,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 = GetReserveCount( $borrowernumber );
     }
 
     # List is composed of alternating biblio/item/branch
@@ -362,12 +363,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 = GetReserveCount($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/ReserveCount.t b/t/db_dependent/Reserves/ReserveCount.t
new file mode 100644
index 0000000..f5724a0
--- /dev/null
+++ b/t/db_dependent/Reserves/ReserveCount.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(GetReserveCount($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(GetReserveCount($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(GetReserveCount($borrowernumber), 3, 'Test borrower has 2 reserve.');
+
+$dbh->rollback;
-- 
2.1.4