Bug 22330: Transfer limits should be respected for placing holds in staff interface...
[koha.git] / C4 / Reserves.pm
index 575e486..48b4170 100644 (file)
@@ -23,35 +23,38 @@ package C4::Reserves;
 
 use strict;
 #use warnings; FIXME - Bug 2505
-use C4::Context;
+use C4::Accounts;
 use C4::Biblio;
-use C4::Members;
-use C4::Items;
 use C4::Circulation;
-use C4::Accounts;
+use C4::Context;
+use C4::Items;
+use C4::Members;
 
 # for _koha_notify_reserve
-use C4::Members::Messaging;
-use C4::Members qw();
 use C4::Letters;
 use C4::Log;
+use C4::Members qw();
+use C4::Members::Messaging;
 
+use Koha::Account::Lines;
 use Koha::Biblios;
-use Koha::DateUtils;
 use Koha::Calendar;
+use Koha::CirculationRules;
 use Koha::Database;
+use Koha::DateUtils;
 use Koha::Hold;
-use Koha::Old::Hold;
 use Koha::Holds;
-use Koha::Libraries;
 use Koha::IssuingRules;
-use Koha::Items;
 use Koha::ItemTypes;
+use Koha::Items;
+use Koha::Libraries;
+use Koha::Libraries;
+use Koha::Old::Hold;
 use Koha::Patrons;
 
-use List::MoreUtils qw( firstidx any );
 use Carp;
 use Data::Dumper;
+use List::MoreUtils qw( firstidx any );
 
 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
@@ -104,7 +107,6 @@ BEGIN {
     @EXPORT = qw(
         &AddReserve
 
-        &GetReservesForBranch
         &GetReserveStatus
 
         &GetOtherReserves
@@ -121,15 +123,12 @@ BEGIN {
         &CanBookBeReserved
         &CanItemBeReserved
         &CanReserveBeCanceledFromOpac
-        &CancelReserve
         &CancelExpiredReserves
 
         &AutoUnsuspendReserves
 
         &IsAvailableForItemLevelRequest
 
-        &OPACItemHoldsAllowed
-
         &AlterPriority
         &ToggleLowestPriority
 
@@ -175,6 +174,16 @@ sub AddReserve {
 
     $expdate = output_pref({ str => $expdate, dateonly => 1, dateformat => 'iso' });
 
+    # if we have an item selectionned, and the pickup branch is the same as the holdingbranch
+    # of the document, we force the value $priority and $found .
+    if ( $checkitem and not C4::Context->preference('ReservesNeedReturns') ) {
+        $priority = 0;
+        my $item = Koha::Items->find( $checkitem ); # FIXME Prevent bad calls
+        if ( $item->holdingbranch eq $branch ) {
+            $found = 'W';
+        }
+    }
+
     if ( C4::Context->preference('AllowHoldDateInFuture') ) {
 
         # Make room in reserves for this before those of a later reserve date
@@ -207,6 +216,7 @@ sub AddReserve {
             itemtype       => $itemtype,
         }
     )->store();
+    $hold->set_waiting() if $found eq 'W';
 
     logaction( 'HOLDS', 'CREATE', $hold->id, Dumper($hold->unblessed) )
         if C4::Context->preference('HoldsLog');
@@ -258,7 +268,7 @@ sub AddReserve {
 
 =head2 CanBookBeReserved
 
-  $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber)
+  $canReserve = &CanBookBeReserved($borrowernumber, $biblionumber, $branchcode)
   if ($canReserve eq 'OK') { #We can reserve this Item! }
 
 See CanItemBeReserved() for possible return values.
@@ -266,64 +276,68 @@ See CanItemBeReserved() for possible return values.
 =cut
 
 sub CanBookBeReserved{
-    my ($borrowernumber, $biblionumber) = @_;
+    my ($borrowernumber, $biblionumber, $pickup_branchcode) = @_;
 
-    my $items = GetItemnumbersForBiblio($biblionumber);
+    my @itemnumbers = Koha::Items->search({ biblionumber => $biblionumber})->get_column("itemnumber");
     #get items linked via host records
     my @hostitems = get_hostitemnumbers_of($biblionumber);
     if (@hostitems){
-    push (@$items,@hostitems);
+        push (@itemnumbers, @hostitems);
     }
 
-    my $canReserve;
-    foreach my $item (@$items) {
-        $canReserve = CanItemBeReserved( $borrowernumber, $item );
-        return 'OK' if $canReserve eq 'OK';
+    my $canReserve = { status => '' };
+    foreach my $itemnumber (@itemnumbers) {
+        $canReserve = CanItemBeReserved( $borrowernumber, $itemnumber, $pickup_branchcode );
+        return { status => 'OK' } if $canReserve->{status} eq 'OK';
     }
     return $canReserve;
 }
 
 =head2 CanItemBeReserved
 
-  $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber)
-  if ($canReserve eq 'OK') { #We can reserve this Item! }
+  $canReserve = &CanItemBeReserved($borrowernumber, $itemnumber, $branchcode)
+  if ($canReserve->{status} eq 'OK') { #We can reserve this Item! }
 
-@RETURNS OK,              if the Item can be reserved.
-         ageRestricted,   if the Item is age restricted for this borrower.
-         damaged,         if the Item is damaged.
-         cannotReserveFromOtherBranches, if syspref 'canreservefromotherbranches' is OK.
-         tooManyReserves, if the borrower has exceeded his maximum reserve amount.
-         notReservable,   if holds on this item are not allowed
+@RETURNS { status => OK },              if the Item can be reserved.
+         { status => ageRestricted },   if the Item is age restricted for this borrower.
+         { status => damaged },         if the Item is damaged.
+         { status => cannotReserveFromOtherBranches }, if syspref 'canreservefromotherbranches' is OK.
+         { status => tooManyReserves, limit => $limit }, if the borrower has exceeded their maximum reserve amount.
+         { status => notReservable },   if holds on this item are not allowed
+         { status => libraryNotFound },   if given branchcode is not an existing library
+         { status => libraryNotPickupLocation },   if given branchcode is not configured to be a pickup location
+         { status => cannotBeTransferred }, if branch transfer limit applies on given item and branchcode
 
 =cut
 
 sub CanItemBeReserved {
-    my ( $borrowernumber, $itemnumber ) = @_;
+    my ( $borrowernumber, $itemnumber, $pickup_branchcode ) = @_;
 
     my $dbh = C4::Context->dbh;
     my $ruleitemtype;    # itemtype of the matching issuing rule
     my $allowedreserves  = 0; # Total number of holds allowed across all records
     my $holds_per_record = 1; # Total number of holds allowed for this one given record
+    my $holds_per_day;        # Default to unlimited
 
     # we retrieve borrowers and items informations #
     # item->{itype} will come for biblioitems if necessery
-    my $item       = GetItem($itemnumber);
-    my $biblio     = Koha::Biblios->find( $item->{biblionumber} );
+    my $item       = Koha::Items->find($itemnumber);
+    my $biblio     = $item->biblio;
     my $patron = Koha::Patrons->find( $borrowernumber );
     my $borrower = $patron->unblessed;
 
     # If an item is damaged and we don't allow holds on damaged items, we can stop right here
-    return 'damaged'
-      if ( $item->{damaged}
+    return { status =>'damaged' }
+      if ( $item->damaged
         && !C4::Context->preference('AllowHoldsOnDamagedItems') );
 
     # Check for the age restriction
     my ( $ageRestriction, $daysToAgeRestriction ) =
       C4::Circulation::GetAgeRestriction( $biblio->biblioitem->agerestriction, $borrower );
-    return 'ageRestricted' if $daysToAgeRestriction && $daysToAgeRestriction > 0;
+    return { status => 'ageRestricted' } if $daysToAgeRestriction && $daysToAgeRestriction > 0;
 
     # Check that the patron doesn't have an item level hold on this item already
-    return 'itemAlreadyOnHold'
+    return { status =>'itemAlreadyOnHold' }
       if Koha::Holds->search( { borrowernumber => $borrowernumber, itemnumber => $itemnumber } )->count();
 
     my $controlbranch = C4::Context->preference('ReservesControlBranch');
@@ -342,7 +356,7 @@ sub CanItemBeReserved {
 
     if ( $controlbranch eq "ItemHomeLibrary" ) {
         $branchfield = "items.homebranch";
-        $branchcode  = $item->{homebranch};
+        $branchcode  = $item->homebranch;
     }
     elsif ( $controlbranch eq "PatronLibrary" ) {
         $branchfield = "borrowers.branchcode";
@@ -350,16 +364,16 @@ sub CanItemBeReserved {
     }
 
     # we retrieve rights
-    if ( my $rights = GetHoldRule( $borrower->{'categorycode'}, $item->{'itype'}, $branchcode ) ) {
+    if ( my $rights = GetHoldRule( $borrower->{'categorycode'}, $item->effective_itemtype, $branchcode ) ) {
         $ruleitemtype     = $rights->{itemtype};
         $allowedreserves  = $rights->{reservesallowed};
         $holds_per_record = $rights->{holds_per_record};
+        $holds_per_day    = $rights->{holds_per_day};
     }
     else {
         $ruleitemtype = '*';
     }
 
-    $item = Koha::Items->find( $itemnumber );
     my $holds = Koha::Holds->search(
         {
             borrowernumber => $borrowernumber,
@@ -368,7 +382,19 @@ sub CanItemBeReserved {
         }
     );
     if ( $holds->count() >= $holds_per_record ) {
-        return "tooManyHoldsForThisRecord";
+        return { status => "tooManyHoldsForThisRecord", limit => $holds_per_record };
+    }
+
+    my $today_holds = Koha::Holds->search({
+        borrowernumber => $borrowernumber,
+        reservedate    => dt_from_string->date
+    });
+
+    if ( defined $holds_per_day &&
+          (   ( $holds_per_day > 0 && $today_holds->count() >= $holds_per_day )
+           or ( $holds_per_day == 0 ) )
+        )  {
+        return { status => 'tooManyReservesToday', limit => $holds_per_day };
     }
 
     # we retrieve count
@@ -399,22 +425,40 @@ sub CanItemBeReserved {
 
     # we check if it's ok or not
     if ( $reservecount >= $allowedreserves ) {
-        return 'tooManyReserves';
+        return { status => 'tooManyReserves', limit => $allowedreserves };
+    }
+
+    # Now we need to check hold limits by patron category
+    my $rule = Koha::CirculationRules->get_effective_rule(
+        {
+            categorycode => $borrower->{categorycode},
+            branchcode   => $branchcode,
+            rule_name    => 'max_holds',
+        }
+    );
+    if ( $rule && defined( $rule->rule_value ) && $rule->rule_value ne '' ) {
+        my $total_holds_count = Koha::Holds->search(
+            {
+                borrowernumber => $borrower->{borrowernumber}
+            }
+        )->count();
+
+        return { status => 'tooManyReserves', limit => $rule->rule_value} if $total_holds_count >= $rule->rule_value;
     }
 
     my $circ_control_branch =
       C4::Circulation::_GetCircControlBranch( $item->unblessed(), $borrower );
     my $branchitemrule =
-      C4::Circulation::GetBranchItemRule( $circ_control_branch, $item->itype );
+      C4::Circulation::GetBranchItemRule( $circ_control_branch, $item->itype ); # FIXME Should not be item->effective_itemtype?
 
     if ( $branchitemrule->{holdallowed} == 0 ) {
-        return 'notReservable';
+        return { status => 'notReservable' };
     }
 
     if (   $branchitemrule->{holdallowed} == 1
         && $borrower->{branchcode} ne $item->homebranch )
     {
-        return 'cannotReserveFromOtherBranches';
+        return { status => 'cannotReserveFromOtherBranches' };
     }
 
     # If reservecount is ok, we check item branch if IndependentBranches is ON
@@ -422,13 +466,28 @@ sub CanItemBeReserved {
     if ( C4::Context->preference('IndependentBranches')
         and !C4::Context->preference('canreservefromotherbranches') )
     {
-        my $itembranch = $item->homebranch;
-        if ( $itembranch ne $borrower->{branchcode} ) {
-            return 'cannotReserveFromOtherBranches';
+        if ( $item->homebranch ne $borrower->{branchcode} ) {
+            return { status => 'cannotReserveFromOtherBranches' };
+        }
+    }
+
+    if ($pickup_branchcode) {
+        my $destination = Koha::Libraries->find({
+            branchcode => $pickup_branchcode,
+        });
+
+        unless ($destination) {
+            return { status => 'libraryNotFound' };
+        }
+        unless ($destination->pickup_location) {
+            return { status => 'libraryNotPickupLocation' };
+        }
+        unless ($item->can_be_transferred({ to => $destination })) {
+            return { status => 'cannotBeTransferred' };
         }
     }
 
-    return 'OK';
+    return { status => 'OK' };
 }
 
 =head2 CanReserveBeCanceledFromOpac
@@ -468,8 +527,8 @@ sub GetOtherReserves {
     my $nextreservinfo;
     my ( undef, $checkreserves, undef ) = CheckReserves($itemnumber);
     if ($checkreserves) {
-        my $iteminfo = GetItem($itemnumber);
-        if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
+        my $item = Koha::Items->find($itemnumber);
+        if ( $item->holdingbranch ne $checkreserves->{'branchcode'} ) {
             $messages->{'transfert'} = $checkreserves->{'branchcode'};
             #minus priorities of others reservs
             ModReserveMinusPriority(
@@ -480,7 +539,7 @@ sub GetOtherReserves {
             #launch the subroutine dotransfer
             C4::Items::ModItemTransfer(
                 $itemnumber,
-                $iteminfo->{'holdingbranch'},
+                $item->holdingbranch,
                 $checkreserves->{'branchcode'}
               ),
               ;
@@ -512,13 +571,20 @@ sub GetOtherReserves {
 
 sub ChargeReserveFee {
     my ( $borrowernumber, $fee, $title ) = @_;
-    return if !$fee || $fee==0; # the last test is needed to include 0.00
-    my $accquery = qq{
-INSERT INTO accountlines ( borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding ) VALUES (?, ?, NOW(), ?, ?, 'Res', ?)
-    };
-    my $dbh = C4::Context->dbh;
-    my $nextacctno = &getnextacctno( $borrowernumber );
-    $dbh->do( $accquery, undef, ( $borrowernumber, $nextacctno, $fee, "Reserve Charge - $title", $fee ) );
+    return if !$fee || $fee == 0;    # the last test is needed to include 0.00
+    Koha::Account->new( { patron_id => $borrowernumber } )->add_debit(
+        {
+            amount       => $fee,
+            description  => "Reserve Charge - " . $title,
+            note         => undef,
+            user_id      => C4::Context->userenv ? C4::Context->userenv->{'number'} : 0,
+            library_id   => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
+            sip          => undef,
+            invoice_type => undef,
+            type         => 'reserve',
+            item_id      => undef
+        }
+    );
 }
 
 =head2 GetReserveFee
@@ -563,41 +629,6 @@ SELECT COUNT(*) FROM reserves WHERE biblionumber=? AND borrowernumber<>?
     return $fee;
 }
 
-=head2 GetReservesForBranch
-
-  @transreserv = GetReservesForBranch($frombranch);
-
-=cut
-
-sub GetReservesForBranch {
-    my ($frombranch) = @_;
-    my $dbh = C4::Context->dbh;
-
-    my $query = "
-        SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate, expirationdate
-        FROM   reserves
-        WHERE   priority='0'
-        AND found='W'
-    ";
-    $query .= " AND branchcode=? " if ( $frombranch );
-    $query .= "ORDER BY waitingdate" ;
-
-    my $sth = $dbh->prepare($query);
-    if ($frombranch){
-     $sth->execute($frombranch);
-    } else {
-        $sth->execute();
-    }
-
-    my @transreserv;
-    my $i = 0;
-    while ( my $data = $sth->fetchrow_hashref ) {
-        $transreserv[$i] = $data;
-        $i++;
-    }
-    return (@transreserv);
-}
-
 =head2 GetReserveStatus
 
   $reservestatus = GetReserveStatus($itemnumber);
@@ -733,18 +764,22 @@ sub CheckReserves {
         my $priority = 10000000;
         foreach my $res (@reserves) {
             if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
-                return ( "Waiting", $res, \@reserves ); # Found it
+                if ($res->{'found'} eq 'W') {
+                    return ( "Waiting", $res, \@reserves ); # Found it, it is waiting
+                } else {
+                    return ( "Reserved", $res, \@reserves ); # Found determinated hold, e. g. the tranferred one
+                }
             } else {
                 my $patron;
-                my $iteminfo;
+                my $item;
                 my $local_hold_match;
 
                 if ($LocalHoldsPriority) {
                     $patron = Koha::Patrons->find( $res->{borrowernumber} );
-                    $iteminfo = C4::Items::GetItem($itemnumber);
+                    $item = Koha::Items->find($itemnumber);
 
                     my $local_holds_priority_item_branchcode =
-                      $iteminfo->{$LocalHoldsPriorityItemControl};
+                      $item->$LocalHoldsPriorityItemControl;
                     my $local_holds_priority_patron_branchcode =
                       ( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
                       ? $res->{branchcode}
@@ -758,14 +793,16 @@ sub CheckReserves {
 
                 # See if this item is more important than what we've got so far
                 if ( ( $res->{'priority'} && $res->{'priority'} < $priority ) || $local_hold_match ) {
-                    $iteminfo ||= C4::Items::GetItem($itemnumber);
-                    next if $res->{itemtype} && $res->{itemtype} ne _get_itype( $iteminfo );
+                    $item ||= Koha::Items->find($itemnumber);
+                    next if $res->{itemtype} && $res->{itemtype} ne $item->effective_itemtype;
                     $patron ||= Koha::Patrons->find( $res->{borrowernumber} );
-                    my $branch = GetReservesControlBranch( $iteminfo, $patron->unblessed );
-                    my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$iteminfo->{'itype'});
+                    my $branch = GetReservesControlBranch( $item->unblessed, $patron->unblessed );
+                    my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$item->effective_itemtype);
                     next if ($branchitemrule->{'holdallowed'} == 0);
                     next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $patron->branchcode));
-                    next if ( ($branchitemrule->{hold_fulfillment_policy} ne 'any') && ($res->{branchcode} ne $iteminfo->{ $branchitemrule->{hold_fulfillment_policy} }) );
+                    my $hold_fulfillment_policy = $branchitemrule->{hold_fulfillment_policy};
+                    next if ( ($branchitemrule->{hold_fulfillment_policy} ne 'any') && ($res->{branchcode} ne $item->$hold_fulfillment_policy) );
+                    next unless $item->can_be_transferred( { to => scalar Koha::Libraries->find( $res->{branchcode} ) } );
                     $priority = $res->{'priority'};
                     $highest  = $res;
                     last if $local_hold_match;
@@ -793,28 +830,27 @@ Cancels all reserves with an expiration date from before today.
 =cut
 
 sub CancelExpiredReserves {
-
     my $today = dt_from_string();
     my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
+    my $expireWaiting = C4::Context->preference('ExpireReservesMaxPickUpDelay');
 
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare( "
-        SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
-        AND expirationdate IS NOT NULL
-    " );
-    $sth->execute();
+    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+    my $params = { expirationdate => { '<', $dtf->format_date($today) } };
+    $params->{found} = undef unless $expireWaiting;
+
+    # FIXME To move to Koha::Holds->search_expired (?)
+    my $holds = Koha::Holds->search( $params );
 
-    while ( my $res = $sth->fetchrow_hashref() ) {
-        my $calendar = Koha::Calendar->new( branchcode => $res->{'branchcode'} );
-        my $cancel_params = { reserve_id => $res->{'reserve_id'} };
+    while ( my $hold = $holds->next ) {
+        my $calendar = Koha::Calendar->new( branchcode => $hold->branchcode );
 
         next if !$cancel_on_holidays && $calendar->is_holiday( $today );
 
-        if ( $res->{found} eq 'W' ) {
+        my $cancel_params = {};
+        if ( $hold->found eq 'W' ) {
             $cancel_params->{charge_cancel_fee} = 1;
         }
-
-        CancelReserve($cancel_params);
+        $hold->cancel( $cancel_params );
     }
 }
 
@@ -829,71 +865,9 @@ Unsuspends all suspended reserves with a suspend_until date from before today.
 sub AutoUnsuspendReserves {
     my $today = dt_from_string();
 
-    my @holds = Koha::Holds->search( { suspend_until => { '<' => $today->ymd() } } );
+    my @holds = Koha::Holds->search( { suspend_until => { '<=' => $today->ymd() } } );
 
-    map { $_->suspend(0)->suspend_until(undef)->store() } @holds;
-}
-
-=head2 CancelReserve
-
-  CancelReserve({ reserve_id => $reserve_id, [ biblionumber => $biblionumber, borrowernumber => $borrrowernumber, itemnumber => $itemnumber, ] [ charge_cancel_fee => 1 ] });
-
-Cancels a reserve. If C<charge_cancel_fee> is passed and the C<ExpireReservesMaxPickUpDelayCharge> syspref is set, charge that fee to the patron's account.
-
-=cut
-
-sub CancelReserve {
-    my ( $params ) = @_;
-
-    my $reserve_id = $params->{'reserve_id'};
-    # Filter out only the desired keys; this will insert undefined values for elements missing in
-    # \%params, but GetReserveId filters them out anyway.
-    $reserve_id = GetReserveId( { biblionumber => $params->{'biblionumber'}, borrowernumber => $params->{'borrowernumber'}, itemnumber => $params->{'itemnumber'} } ) unless ( $reserve_id );
-
-    return unless ( $reserve_id );
-
-    my $dbh = C4::Context->dbh;
-
-    my $hold = Koha::Holds->find( $reserve_id );
-    return unless $hold;
-
-    logaction( 'HOLDS', 'CANCEL', $hold->reserve_id, Dumper($hold->unblessed) )
-        if C4::Context->preference('HoldsLog');
-
-    my $query = "
-        UPDATE reserves
-        SET    cancellationdate = now(),
-               priority         = 0
-        WHERE  reserve_id = ?
-    ";
-    my $sth = $dbh->prepare($query);
-    $sth->execute( $reserve_id );
-
-    $query = "
-        INSERT INTO old_reserves
-        SELECT * FROM reserves
-        WHERE  reserve_id = ?
-    ";
-    $sth = $dbh->prepare($query);
-    $sth->execute( $reserve_id );
-
-    $query = "
-        DELETE FROM reserves
-        WHERE  reserve_id = ?
-    ";
-    $sth = $dbh->prepare($query);
-    $sth->execute( $reserve_id );
-
-    # now fix the priority on the others....
-    _FixPriority({ biblionumber => $hold->biblionumber });
-
-    # and, if desired, charge a cancel fee
-    my $charge = C4::Context->preference("ExpireReservesMaxPickUpDelayCharge");
-    if ( $charge && $params->{'charge_cancel_fee'} ) {
-        manualinvoice($hold->borrowernumber, $hold->itemnumber, '', 'HE', $charge);
-    }
-
-    return $hold->unblessed;
+    map { $_->resume() } @holds;
 }
 
 =head2 ModReserve
@@ -947,13 +921,21 @@ sub ModReserve {
     return if $rank eq "n";
 
     return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
-    $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber, itemnumber => $itemnumber }) unless ( $reserve_id );
+
+    my $hold;
+    unless ( $reserve_id ) {
+        my $holds = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $borrowernumber, itemnumber => $itemnumber });
+        return unless $holds->count; # FIXME Should raise an exception
+        $hold = $holds->next;
+        $reserve_id = $hold->reserve_id;
+    }
+
+    $hold ||= Koha::Holds->find($reserve_id);
 
     if ( $rank eq "del" ) {
-        CancelReserve({ reserve_id => $reserve_id });
+        $hold->cancel;
     }
     elsif ($rank =~ /^\d+/ and $rank > 0) {
-        my $hold = Koha::Holds->find($reserve_id);
         logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, Dumper($hold->unblessed) )
             if C4::Context->preference('HoldsLog');
 
@@ -1011,6 +993,7 @@ sub ModReserveFill {
         }
     );
 
+    # FIXME Must call Koha::Hold->cancel ? => No, should call ->filled and add the correct log
     Koha::Old::Hold->new( $hold->unblessed() )->store();
 
     $hold->delete();
@@ -1123,7 +1106,9 @@ sub ModReserveCancelAll {
     my ( $itemnumber, $borrowernumber ) = @_;
 
     #step 1 : cancel the reservation
-    my $CancelReserve = CancelReserve({ itemnumber => $itemnumber, borrowernumber => $borrowernumber });
+    my $holds = Koha::Holds->search({ itemnumber => $itemnumber, borrowernumber => $borrowernumber });
+    return unless $holds->count;
+    $holds->next->cancel;
 
     #step 2 launch the subroutine of the others reserves
     ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
@@ -1157,7 +1142,7 @@ sub ModReserveMinusPriority {
 
 =head2 IsAvailableForItemLevelRequest
 
-  my $is_available = IsAvailableForItemLevelRequest($item_record,$borrower_record);
+  my $is_available = IsAvailableForItemLevelRequest( $item_record, $borrower_record, $pickup_branchcode );
 
 Checks whether a given item record is available for an
 item-level hold request.  An item is available if
@@ -1165,6 +1150,7 @@ item-level hold request.  An item is available if
 * it is not lost AND
 * it is not damaged AND
 * it is not withdrawn AND
+* a waiting or in transit reserve is placed on
 * does not have a not for loan value > 0
 
 Need to check the issuingrules onshelfholds column,
@@ -1181,16 +1167,19 @@ and canreservefromotherbranches.
 sub IsAvailableForItemLevelRequest {
     my $item = shift;
     my $borrower = shift;
+    my $pickup_branchcode = shift;
 
     my $dbh = C4::Context->dbh;
     # must check the notforloan setting of the itemtype
     # FIXME - a lot of places in the code do this
     #         or something similar - need to be
     #         consolidated
-    my $itype = _get_itype($item);
+    my $patron = Koha::Patrons->find( $borrower->{borrowernumber} );
+    my $item_object = Koha::Items->find( $item->{itemnumber } );
+    my $itemtype = $item_object->effective_itemtype;
     my $notforloan_per_itemtype
       = $dbh->selectrow_array("SELECT notforloan FROM itemtypes WHERE itemtype = ?",
-                              undef, $itype);
+                              undef, $itemtype);
 
     return 0 if
         $notforloan_per_itemtype ||
@@ -1199,7 +1188,14 @@ sub IsAvailableForItemLevelRequest {
         $item->{withdrawn}        ||
         ($item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems'));
 
-    my $on_shelf_holds = _OnShelfHoldsAllowed($itype,$borrower->{categorycode},$item->{holdingbranch});
+    my $on_shelf_holds = Koha::IssuingRules->get_onshelfholds_policy( { item => $item_object, patron => $patron } );
+
+    if ($pickup_branchcode) {
+        my $destination = Koha::Libraries->find($pickup_branchcode);
+        return 0 unless $destination;
+        return 0 unless $destination->pickup_location;
+        return 0 unless $item_object->can_be_transferred( { to => $destination } );
+    }
 
     if ( $on_shelf_holds == 1 ) {
         return 1;
@@ -1210,6 +1206,10 @@ sub IsAvailableForItemLevelRequest {
         my $any_available = 0;
 
         foreach my $i (@items) {
+
+            my $circ_control_branch = C4::Circulation::_GetCircControlBranch( $i->unblessed(), $borrower );
+            my $branchitemrule = C4::Circulation::GetBranchItemRule( $circ_control_branch, $i->itype );
+
             $any_available = 1
               unless $i->itemlost
               || $i->notforloan > 0
@@ -1218,29 +1218,14 @@ sub IsAvailableForItemLevelRequest {
               || IsItemOnHoldAndFound( $i->id )
               || ( $i->damaged
                 && !C4::Context->preference('AllowHoldsOnDamagedItems') )
-              || Koha::ItemTypes->find( $i->effective_itemtype() )->notforloan;
+              || Koha::ItemTypes->find( $i->effective_itemtype() )->notforloan
+              || $branchitemrule->{holdallowed} == 1 && $borrower->{branchcode} ne $i->homebranch;
         }
 
         return $any_available ? 0 : 1;
+    } else { # on_shelf_holds == 0 "If any unavailable" (the description is rather cryptic and could still be improved)
+        return $item->{onloan} || IsItemOnHoldAndFound( $item->{itemnumber} );
     }
-
-    return $item->{onloan} || GetReserveStatus($item->{itemnumber}) eq "Waiting";
-}
-
-=head2 OnShelfHoldsAllowed
-
-  OnShelfHoldsAllowed($itemtype,$borrowercategory,$branchcode);
-
-Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see if onshelf
-holds are allowed, returns true if so.
-
-=cut
-
-sub OnShelfHoldsAllowed {
-    my ($item, $borrower) = @_;
-
-    my $itype = _get_itype($item);
-    return _OnShelfHoldsAllowed($itype,$borrower->{categorycode},$item->{holdingbranch});
 }
 
 sub _get_itype {
@@ -1270,16 +1255,9 @@ sub _get_itype {
     return $itype;
 }
 
-sub _OnShelfHoldsAllowed {
-    my ($itype,$borrowercategory,$branchcode) = @_;
-
-    my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowercategory, itemtype => $itype, branchcode => $branchcode });
-    return $issuing_rule ? $issuing_rule->onshelfholds : undef;
-}
-
 =head2 AlterPriority
 
-  AlterPriority( $where, $reserve_id );
+  AlterPriority( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority );
 
 This function changes a reserve's priority up, down, to the top, or to the bottom.
 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
@@ -1287,7 +1265,7 @@ Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was
 =cut
 
 sub AlterPriority {
-    my ( $where, $reserve_id ) = @_;
+    my ( $where, $reserve_id, $prev_priority, $next_priority, $first_priority, $last_priority ) = @_;
 
     my $hold = Koha::Holds->find( $reserve_id );
     return unless $hold;
@@ -1297,21 +1275,18 @@ sub AlterPriority {
         return;
     }
 
-    if ( $where eq 'up' || $where eq 'down' ) {
-
-      my $priority = $hold->priority;
-      $priority = $where eq 'up' ? $priority - 1 : $priority + 1;
-      _FixPriority({ reserve_id => $reserve_id, rank => $priority })
-
+    if ( $where eq 'up' ) {
+      return unless $prev_priority;
+      _FixPriority({ reserve_id => $reserve_id, rank => $prev_priority })
+    } elsif ( $where eq 'down' ) {
+      return unless $next_priority;
+      _FixPriority({ reserve_id => $reserve_id, rank => $next_priority })
     } elsif ( $where eq 'top' ) {
-
-      _FixPriority({ reserve_id => $reserve_id, rank => '1' })
-
+      _FixPriority({ reserve_id => $reserve_id, rank => $first_priority })
     } elsif ( $where eq 'bottom' ) {
-
-      _FixPriority({ reserve_id => $reserve_id, rank => '999999' });
-
+      _FixPriority({ reserve_id => $reserve_id, rank => $last_priority });
     }
+
     # FIXME Should return the new priority
 }
 
@@ -1447,13 +1422,18 @@ sub _FixPriority {
 
     my $dbh = C4::Context->dbh;
 
-    unless ( $biblionumber ) {
-        my $hold = Koha::Holds->find( $reserve_id );
+    my $hold;
+    if ( $reserve_id ) {
+        $hold = Koha::Holds->find( $reserve_id );
+        return unless $hold;
+    }
+
+    unless ( $biblionumber ) { # FIXME This is a very weird API
         $biblionumber = $hold->biblionumber;
     }
 
-    if ( $rank eq "del" ) {
-         CancelReserve({ reserve_id => $reserve_id });
+    if ( $rank eq "del" ) { # FIXME will crash if called without $hold
+        $hold->cancel;
     }
     elsif ( $rank eq "W" || $rank eq "0" ) {
 
@@ -1686,7 +1666,7 @@ sub _koha_notify_reserve {
     my $patron = Koha::Patrons->find( $borrowernumber );
 
     # Try to get the borrower's email address
-    my $to_address = C4::Members::GetNoticeEmailAddress($borrowernumber);
+    my $to_address = $patron->notice_email_address;
 
     my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( {
             borrowernumber => $borrowernumber,
@@ -1797,54 +1777,6 @@ sub _ShiftPriorityByDateAndPriority {
     return $new_priority;  # so the caller knows what priority they wind up receiving
 }
 
-=head2 OPACItemHoldsAllowed
-
-  OPACItemHoldsAllowed($item_record,$borrower_record);
-
-Checks issuingrules, using the borrowers categorycode, the itemtype, and branchcode to see
-if specific item holds are allowed, returns true if so.
-
-=cut
-
-sub OPACItemHoldsAllowed {
-    my ($item,$borrower) = @_;
-
-    my $branchcode = $item->{homebranch} or die "No homebranch";
-    my $itype;
-    my $dbh = C4::Context->dbh;
-    if (C4::Context->preference('item-level_itypes')) {
-       # We can't trust GetItem to honour the syspref, so safest to do it ourselves
-       # When GetItem is fixed, we can remove this
-       $itype = $item->{itype};
-    }
-    else {
-       my $query = "SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ? ";
-       my $sth = $dbh->prepare($query);
-       $sth->execute($item->{biblioitemnumber});
-       if (my $data = $sth->fetchrow_hashref()){
-           $itype = $data->{itemtype};
-       }
-    }
-
-    my $query = "SELECT opacitemholds,categorycode,itemtype,branchcode FROM issuingrules WHERE
-          (issuingrules.categorycode = ? OR issuingrules.categorycode = '*')
-        AND
-          (issuingrules.itemtype = ? OR issuingrules.itemtype = '*')
-        AND
-          (issuingrules.branchcode = ? OR issuingrules.branchcode = '*')
-        ORDER BY
-          issuingrules.categorycode desc,
-          issuingrules.itemtype desc,
-          issuingrules.branchcode desc
-       LIMIT 1";
-    my $sth = $dbh->prepare($query);
-    $sth->execute($borrower->{categorycode},$itype,$branchcode);
-    my $data = $sth->fetchrow_hashref;
-    my $opacitemholds = uc substr ($data->{opacitemholds}, 0, 1);
-    return '' if $opacitemholds eq 'N';
-    return $opacitemholds;
-}
-
 =head2 MoveReserve
 
   MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
@@ -1889,7 +1821,8 @@ sub MoveReserve {
             RevertWaitingStatus({ itemnumber => $itemnumber });
         }
         elsif ( $cancelreserve eq 'cancel' || $cancelreserve ) { # cancel reserves on this item
-            CancelReserve( { reserve_id => $res->{'reserve_id'} } );
+            my $hold = Koha::Holds->find( $res->{reserve_id} );
+            $hold->cancel;
         }
     }
 }
@@ -1999,33 +1932,17 @@ sub RevertWaitingStatus {
     _FixPriority( { biblionumber => $reserve->{biblionumber} } );
 }
 
-=head2 GetReserveId
-
-  $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber [, itemnumber => $itemnumber ] });
-
-  Returnes the first reserve id that matches the given criteria
-
-=cut
-
-sub GetReserveId {
-    my ( $params ) = @_;
-
-    return unless ( ( $params->{'biblionumber'} || $params->{'itemnumber'} ) && $params->{'borrowernumber'} );
-
-    foreach my $key ( keys %$params ) {
-        delete $params->{$key} unless defined( $params->{$key} );
-    }
-
-    my $hold = Koha::Holds->search( $params )->next();
-
-    return unless $hold;
-
-    return $hold->id();
-}
-
 =head2 ReserveSlip
 
-  ReserveSlip($branchcode, $borrowernumber, $biblionumber)
+ReserveSlip(
+    {
+        branchcode     => $branchcode,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        [ itemnumber   => $itemnumber, ]
+        [ barcode      => $barcode, ]
+    }
+  )
 
 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
 
@@ -2042,22 +1959,44 @@ available within the slip:
 =cut
 
 sub ReserveSlip {
-    my ($branch, $borrowernumber, $biblionumber) = @_;
+    my ($args) = @_;
+    my $branchcode     = $args->{branchcode};
+    my $borrowernumber = $args->{borrowernumber};
+    my $biblionumber   = $args->{biblionumber};
+    my $itemnumber     = $args->{itemnumber};
+    my $barcode        = $args->{barcode};
 
-#   return unless ( C4::Context->boolean_preference('printreserveslips') );
-    my $patron = Koha::Patrons->find( $borrowernumber );
 
-    my $reserve_id = GetReserveId({
-        biblionumber => $biblionumber,
-        borrowernumber => $borrowernumber
-    }) or return;
-    my $reserve = Koha::Holds->find($reserve_id) or return;
-    $reserve = $reserve->unblessed;
+    my $patron = Koha::Patrons->find($borrowernumber);
+
+    my $hold;
+    if ($itemnumber || $barcode ) {
+        $itemnumber ||= Koha::Items->find( { barcode => $barcode } )->itemnumber;
+
+        $hold = Koha::Holds->search(
+            {
+                biblionumber   => $biblionumber,
+                borrowernumber => $borrowernumber,
+                itemnumber     => $itemnumber
+            }
+        )->next;
+    }
+    else {
+        $hold = Koha::Holds->search(
+            {
+                biblionumber   => $biblionumber,
+                borrowernumber => $borrowernumber
+            }
+        )->next;
+    }
+
+    return unless $hold;
+    my $reserve = $hold->unblessed;
 
     return  C4::Letters::GetPreparedLetter (
         module => 'circulation',
         letter_code => 'HOLD_SLIP',
-        branchcode => $branch,
+        branchcode => $branchcode,
         lang => $patron->lang,
         tables => {
             'reserves'    => $reserve,
@@ -2219,7 +2158,7 @@ sub GetHoldRule {
 
     my $sth = $dbh->prepare(
         q{
-         SELECT categorycode, itemtype, branchcode, reservesallowed, holds_per_record
+         SELECT categorycode, itemtype, branchcode, reservesallowed, holds_per_record, holds_per_day
            FROM issuingrules
           WHERE (categorycode in (?,'*') )
             AND (itemtype IN (?,'*'))