| 
      
            Lines 2945-2996
          sub CanBookBeRenewed {
      
      
        Link Here
      
     | 
  
        
          | 2945 | 
                  }  | 
          2945 | 
                  }  | 
        
        
          | 2946 | 
              }  | 
          2946 | 
              }  | 
        
        
          | 2947 | 
           | 
          2947 | 
           | 
        
          
            
              | 2948 | 
                  my $biblio_has_holds = Koha::Holds->search({ biblionumber => $item->biblionumber, non_priority => 0 } )->count > 0; | 
              2948 | 
                  my $fillable_holds = Koha::Holds->search(  | 
            
            
              | 2949 | 
                  if ( $biblio_has_holds && C4::Context->preference('AllowRenewalIfOtherItemsAvailable') && !Koha::Holds->search( { itemnumber => $itemnumber, found => undef } )->count() | 
              2949 | 
                      { | 
            
            
              | 2950 | 
               )  | 
              2950 | 
                          biblionumber => $item->biblionumber,  | 
            
            
              | 2951 | 
                  { | 
              2951 | 
                          non_priority => 0,  | 
            
            
              | 2952 | 
                      my $biblio = Koha::Biblios->find($item->biblionumber);  | 
              2952 | 
                          found        => undef,  | 
            
            
              | 2953 | 
                      my @possible_holds = $biblio->current_holds->unfilled->search(  | 
              2953 | 
                          reservedate  => { '<=' => \'NOW()' }, | 
            
            
              | 2954 | 
                          {non_priority => 0}, | 
              2954 | 
                          suspend      => 0,  | 
            
            
              | 2955 | 
                          { prefetch => 'patron' } | 
              2955 | 
                      },  | 
            
            
              | 2956 | 
                      )->as_list;  | 
              2956 | 
                      { prefetch => 'patron' } | 
            
            
              | 2957 | 
               | 
              2957 | 
                  );  | 
            
            
              | 2958 | 
                      # Get all other items that could possibly fill reserves  | 
              2958 | 
                  if ( $fillable_holds->count ) { | 
            
            
              | 2959 | 
                      # FIXME We could join reserves (or more tables) here to eliminate some checks later  | 
              2959 | 
                      if ( C4::Context->preference('AllowRenewalIfOtherItemsAvailable') ) { | 
            
            
              | 2960 | 
                      my @other_items = Koha::Items->search({ | 
              2960 | 
                          my @possible_holds = $fillable_holds->as_list;  | 
            
            
              | 2961 | 
                          biblionumber => $biblio->biblionumber,  | 
              2961 | 
               | 
            
            
              | 2962 | 
                          onloan       => undef,  | 
              2962 | 
                          # Get all other items that could possibly fill reserves  | 
            
            
              | 2963 | 
                          notforloan   => 0,  | 
              2963 | 
                          # FIXME We could join reserves (or more tables) here to eliminate some checks later  | 
            
            
              | 2964 | 
                          -not         => { itemnumber => $itemnumber } })->as_list; | 
              2964 | 
                          my @other_items = Koha::Items->search({ | 
            
            
              | 2965 | 
               | 
              2965 | 
                              biblionumber => $item->biblionumber,  | 
            
            
              | 2966 | 
                      return ( 0, "on_reserve" ) if @possible_holds && (scalar @other_items < scalar @possible_holds);  | 
              2966 | 
                              onloan       => undef,  | 
            
            
              | 2967 | 
               | 
              2967 | 
                              notforloan   => 0,  | 
            
            
              | 2968 | 
                      my %matched_items;  | 
              2968 | 
                              -not         => { itemnumber => $itemnumber } })->as_list; | 
            
            
              | 2969 | 
                      foreach my $possible_hold (@possible_holds) { | 
              2969 | 
               | 
            
            
              | 2970 | 
                          my $fillable = 0;  | 
              2970 | 
                          return ( 0, "on_reserve" ) if @possible_holds && (scalar @other_items < scalar @possible_holds);  | 
            
            
              | 2971 | 
                          my $patron_with_reserve = Koha::Patrons->find($possible_hold->borrowernumber);  | 
              2971 | 
               | 
            
            
              | 2972 | 
                          my $items_any_available = ItemsAnyAvailableAndNotRestricted( { biblionumber => $item->biblionumber, patron => $patron_with_reserve }); | 
              2972 | 
                          my %matched_items;  | 
            
            
              | 2973 | 
               | 
              2973 | 
                          foreach my $possible_hold (@possible_holds) { | 
            
            
              | 2974 | 
                          # FIXME: We are not checking whether the item we are renewing can fill the hold  | 
              2974 | 
                              my $fillable = 0;  | 
            
            
              | 2975 | 
               | 
              2975 | 
                              my $patron_with_reserve = Koha::Patrons->find($possible_hold->borrowernumber);  | 
            
            
              | 2976 | 
                          foreach my $other_item (@other_items) { | 
              2976 | 
                              my $items_any_available = ItemsAnyAvailableAndNotRestricted( { biblionumber => $item->biblionumber, patron => $patron_with_reserve }); | 
            
            
              | 2977 | 
                            next if defined $matched_items{$other_item->itemnumber}; | 
              2977 | 
               | 
            
            
              | 2978 | 
                            next if IsItemOnHoldAndFound( $other_item->itemnumber );  | 
              2978 | 
                              # FIXME: We are not checking whether the item we are renewing can fill the hold  | 
            
            
              | 2979 | 
                            next unless IsAvailableForItemLevelRequest($other_item, $patron_with_reserve, undef, $items_any_available);  | 
              2979 | 
               | 
            
            
              | 2980 | 
                            next unless CanItemBeReserved($patron_with_reserve,$other_item,undef,{ignore_hold_counts=>1})->{status} eq 'OK'; | 
              2980 | 
                              foreach my $other_item (@other_items) { | 
            
            
              | 2981 | 
                            # NOTE: At checkin we call 'CheckReserves' which checks hold 'policy'  | 
              2981 | 
                                next if defined $matched_items{$other_item->itemnumber}; | 
            
            
              | 2982 | 
                            # CanItemBeReserved checks 'rules' and 'policies' which means  | 
              2982 | 
                                next if IsItemOnHoldAndFound( $other_item->itemnumber );  | 
            
            
              | 2983 | 
                            # items will fill holds at checkin that are rejected here  | 
              2983 | 
                                next unless IsAvailableForItemLevelRequest($other_item, $patron_with_reserve, undef, $items_any_available);  | 
            
            
              | 2984 | 
                            $fillable = 1;  | 
              2984 | 
                                next unless CanItemBeReserved($patron_with_reserve,$other_item,undef,{ignore_hold_counts=>1})->{status} eq 'OK'; | 
            
            
              | 2985 | 
                            $matched_items{$other_item->itemnumber} = 1; | 
              2985 | 
                                # NOTE: At checkin we call 'CheckReserves' which checks hold 'policy'  | 
            
            
              | 2986 | 
                            last;  | 
              2986 | 
                                # CanItemBeReserved checks 'rules' and 'policies' which means  | 
            
            
               | 
               | 
              2987 | 
                                # items will fill holds at checkin that are rejected here  | 
            
            
              | 2988 | 
                                $fillable = 1;  | 
            
            
              | 2989 | 
                                $matched_items{$other_item->itemnumber} = 1; | 
            
            
              | 2990 | 
                                last;  | 
            
            
              | 2991 | 
                              }  | 
            
            
              | 2992 | 
                              return ( 0, "on_reserve" ) unless $fillable;  | 
            
        
          | 2987 | 
                      }  | 
          2993 | 
                      }  | 
        
            
              | 2988 | 
                          return ( 0, "on_reserve" ) unless $fillable;  | 
               | 
               | 
            
            
              | 2989 | 
                      }  | 
            
        
          | 2990 | 
           | 
          2994 | 
           | 
        
          
            
              | 2991 | 
                  } elsif ($biblio_has_holds) { | 
              2995 | 
                      } else { | 
            
            
              | 2992 | 
                      my ($status, $matched_reserve, $possible_reserves) = CheckReserves($itemnumber);  | 
              2996 | 
                          my ($status, $matched_reserve, $possible_reserves) = CheckReserves($itemnumber);  | 
            
            
              | 2993 | 
                      return ( 0, "on_reserve" ) if $status;  | 
              2997 | 
                          return ( 0, "on_reserve" ) if $status;  | 
            
            
               | 
               | 
              2998 | 
                      }  | 
            
        
          | 2994 | 
              }  | 
          2999 | 
              }  | 
        
        
          | 2995 | 
           | 
          3000 | 
           | 
        
        
          | 2996 | 
              return ( 0, $auto_renew, { soonest_renew_date => $soonest } ) if $auto_renew =~ 'too_soon';#$auto_renew ne "no" && $auto_renew ne "ok"; | 
          3001 | 
              return ( 0, $auto_renew, { soonest_renew_date => $soonest } ) if $auto_renew =~ 'too_soon';#$auto_renew ne "no" && $auto_renew ne "ok"; |