Bug 8474: Holds done by librarians cause silent errors in koha-error_log, placereques...
[koha.git] / reserve / request.pl
index 70c002a..d849a48 100755 (executable)
@@ -7,18 +7,18 @@
 #
 # 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 2 of the License, or (at your option) any later
-# version.
+# 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.
+# 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.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 =head1 request.pl
 
@@ -29,7 +29,7 @@ script to place reserves/requests
 use strict;
 use warnings;
 use C4::Branch;
-use CGI;
+use CGI qw ( -utf8 );
 use List::MoreUtils qw/uniq/;
 use Date::Calc qw/Date_to_Days/;
 use C4::Output;
@@ -40,6 +40,7 @@ use C4::Items;
 use C4::Koha;
 use C4::Circulation;
 use C4::Dates qw/format_date/;
+use C4::Utils::DataTables::Members;
 use C4::Members;
 use C4::Search;                # enabled_staff_search_views
 use Koha::DateUtils;
@@ -102,17 +103,25 @@ if ( $action eq 'move' ) {
 }
 
 if ($findborrower) {
-    my $borrowers = Search($findborrower, 'cardnumber');
-
-    if ($borrowers && @$borrowers) {
-        if ( @$borrowers == 1 ) {
-            $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
-        }
-        else {
-            $template->param( borrower_list => sort_borrowerlist($borrowers));
-        }
+    my $borrower = C4::Members::GetMember( cardnumber => $findborrower );
+    if ( $borrower ) {
+        $borrowernumber_hold = $borrower->{borrowernumber};
     } else {
-        $messageborrower = "'$findborrower'";
+        my $dt_params = { iDisplayLength => -1 };
+        my $results = C4::Utils::DataTables::Members::search(
+            {
+                searchmember => $findborrower,
+                dt_params => $dt_params,
+            }
+        );
+        my $borrowers = $results->{patrons};
+        if ( scalar @$borrowers == 1 ) {
+            $borrowernumber_hold = $borrowers->[0]->{borrowernumber};
+        } elsif ( @$borrowers ) {
+            $template->param( borrowers => $borrowers );
+        } else {
+            $messageborrower = "'$findborrower'";
+        }
     }
 }
 
@@ -334,9 +343,6 @@ foreach my $biblionumber (@biblionumbers) {
                        $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
                }
                
-            #   add information
-            $item->{itemcallnumber} = $item->{itemcallnumber};
-
             # if the item is currently on loan, we display its return date and
             # change the background color
             my $issues= GetItemIssue($itemnumber);
@@ -347,9 +353,9 @@ foreach my $biblionumber (@biblionumbers) {
 
             # checking reserve
             my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber($itemnumber);
-            my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
-
             if ( defined $reservedate ) {
+                my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
+
                 $item->{backgroundcolor} = 'reserved';
                 $item->{reservedate}     = format_date($reservedate);
                 $item->{ReservedForBorrowernumber}     = $reservedfor;
@@ -394,7 +400,7 @@ foreach my $biblionumber (@biblionumbers) {
             }
 
             # If there is no loan, return and transfer, we show a checkbox.
-            $item->{notforloan} = $item->{notforloan} || 0;
+            $item->{notforloan} ||= 0;
 
             # if independent branches is on we need to check if the person can reserve
             # for branches they arent logged in to
@@ -424,7 +430,7 @@ foreach my $biblionumber (@biblionumbers) {
             if (
                    $policy_holdallowed
                 && !$item->{cantreserve}
-                && IsAvailableForItemLevelRequest($itemnumber)
+                && IsAvailableForItemLevelRequest($item, $borrowerinfo)
                 && CanItemBeReserved(
                     $borrowerinfo->{borrowernumber}, $itemnumber
                 ) eq 'OK'