- my $seen = 0;
- PATRON: while ( my $patron = $patrons->next ) {
- # If there is a reserve that cannot be filled we are done
- return ( 0, "on_reserve" ) if ( $seen > $reservable );
- my $items_any_available = ItemsAnyAvailableAndNotRestricted( { biblionumber => $item->biblionumber, patron => $patron });
- while ( my $other_item = $items->next ) {
- next if defined $matched_items{$other_item->itemnumber};
- next if IsItemOnHoldAndFound( $other_item->itemnumber );
- next unless IsAvailableForItemLevelRequest($other_item, $patron, undef, $items_any_available);
- next unless CanItemBeReserved($patron,$other_item,undef,{ignore_hold_counts=>1})->{status} eq 'OK';
- # NOTE: At checkin we call 'CheckReserves' which checks hold 'policy'
- # CanItemBeReserved checks 'rules' and 'policies' which means
- # items will fill holds at checkin that are rejected here
- $reservable++;
- if ($reservable >= $patron_count) {
- $resfound = 0;
- last PATRON;
- }
- $matched_items{$other_item->itemnumber} = 1;
- last;
+ foreach my $possible_hold (@possible_holds) {
+ my $fillable = 0;
+ my $patron_with_reserve = Koha::Patrons->find($possible_hold->borrowernumber);
+ my $items_any_available = ItemsAnyAvailableAndNotRestricted( { biblionumber => $item->biblionumber, patron => $patron_with_reserve });
+
+ # FIXME: We are not checking whether the item we are renewing can fill the hold
+
+ foreach my $other_item (@other_items) {
+ next if defined $matched_items{$other_item->itemnumber};
+ next if IsItemOnHoldAndFound( $other_item->itemnumber );
+ next unless IsAvailableForItemLevelRequest($other_item, $patron_with_reserve, undef, $items_any_available);
+ next unless CanItemBeReserved($patron_with_reserve,$other_item,undef,{ignore_hold_counts=>1})->{status} eq 'OK';
+ # NOTE: At checkin we call 'CheckReserves' which checks hold 'policy'
+ # CanItemBeReserved checks 'rules' and 'policies' which means
+ # items will fill holds at checkin that are rejected here
+ $fillable = 1;
+ $matched_items{$other_item->itemnumber} = 1;
+ last;