Bug 17453: Take into account items that are lost or damaged
[srvgit] / opac / opac-reserve.pl
index 1ba5ab3..d42cc15 100755 (executable)
@@ -73,6 +73,14 @@ sub get_out {
 my ( $borr ) = GetMember( borrowernumber => $borrowernumber );
 my $patron = Koha::Patrons->find( $borrowernumber );
 
+my $can_place_hold_if_available_at_pickup = C4::Context->preference('OPACHoldsIfAvailableAtPickup');
+unless ( $can_place_hold_if_available_at_pickup ) {
+    my @patron_categories = split '\|', C4::Context->preference('OPACHoldsIfAvailableAtPickupExceptions');
+    if ( @patron_categories ) {
+        $can_place_hold_if_available_at_pickup = grep /$borr->{categorycode}/, @patron_categories;
+    }
+}
+
 # check if this user can place a reserve, -1 means use sys pref, 0 means dont block, 1 means block
 if ( $patron->category->effective_BlockExpiredPatronOpacActions ) {
 
@@ -278,8 +286,13 @@ if ( $query->param('place_reserve') ) {
             $canreserve = 0;
         }
 
-        unless ( C4::Context->preference('OPACHoldsIfAvailableAtPickup') ) {
-            $canreserve = 0 if Koha::Items->search({ biblionumber => $biblioNum, holdingbranch => $branch })->count;
+        unless ( $can_place_hold_if_available_at_pickup ) {
+            my $items_in_this_library = Koha::Items->search({ biblionumber => $biblioNum, holdingbranch => $branch });
+            my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
+            my $nb_of_items_unavailable = $items_in_this_library->search({ -or => { lost => { '!=' => 0 }, damaged => { '!=' => 0 }, } });
+            if ( $items_in_this_library->count > $nb_of_items_issued + $nb_of_items_unavailable ) {
+                $canreserve = 0
+            }
         }
 
         my $itemtype = $query->param('itemtype') || undef;
@@ -526,8 +539,12 @@ foreach my $biblioNum (@biblionumbers) {
             }
             $numCopiesAvailable++;
 
-            if ( not C4::Context->preference('OPACHoldsIfAvailableAtPickup') ) {
-                push @not_available_at, $itemInfo->{holdingbranch};
+            unless ( $can_place_hold_if_available_at_pickup ) {
+                my $items_in_this_library = Koha::Items->search({ biblionumber => $itemInfo->{biblionumber}, holdingbranch => $itemInfo->{holdingbranch} });
+                my $nb_of_items_issued = $items_in_this_library->search({ 'issue.itemnumber' => { not => undef }}, { join => 'issue' })->count;
+                if ( $items_in_this_library->count > $nb_of_items_issued ) {
+                    push @not_available_at, $itemInfo->{holdingbranch};
+                }
             }
         }
 
@@ -562,7 +579,7 @@ foreach my $biblioNum (@biblionumbers) {
         $biblioLoopIter{not_available_at} = \@not_available_at ;
     }
 
-    unless ( C4::Context->preference('OPACHoldsIfAvailableAtPickup') ) {
+    unless ( $can_place_hold_if_available_at_pickup ) {
         @not_available_at = uniq @not_available_at;
         $biblioLoopIter{not_available_at} = \@not_available_at ;
         # The record is not holdable is not available at any of the libraries