@@ -, +, @@ AllowOnShelfHolds off --- C4/Biblio.pm | 17 ++++++++++++++--- C4/Reserves.pm | 12 +++++------- opac/opac-ISBDdetail.pl | 2 +- opac/opac-MARCdetail.pl | 2 +- opac/opac-detail.pl | 2 +- opac/opac-search.pl | 2 +- tools/batch_delete_records.pl | 4 ++-- 7 files changed, 25 insertions(+), 16 deletions(-) --- a/C4/Biblio.pm +++ a/C4/Biblio.pm @@ -2523,14 +2523,25 @@ sub _adjust_pubyear { =head2 CountItemsIssued - my $count = CountItemsIssued( $biblionumber ); + my $count = CountItemsIssued( { biblionumber => $biblionumber, count_on_order => 0 || 1 ); =cut sub CountItemsIssued { - my ($biblionumber) = @_; + my ($params) = @_; + my $biblionumber = $params->{'biblionumber'}; + my $count_on_order = $params->{'count_on_order'}; + + my $sql = 'SELECT COUNT(*) AS issuedCount FROM items LEFT JOIN issues ON items.itemnumber = issues.itemnumber WHERE items.biblionumber = ?'; + + if ( $count_on_order ) { + $sql .= ' AND ( issues.itemnumber IS NOT NULL OR items.notforloan < 0 )'; + } else { + $sql .= ' AND issues.itemnumber IS NOT NULL'; + } + my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare('SELECT COUNT(*) as issuedCount FROM items, issues WHERE items.itemnumber = issues.itemnumber AND items.biblionumber = ?'); + my $sth = $dbh->prepare( $sql ); $sth->execute($biblionumber); my $row = $sth->fetchrow_hashref(); return $row->{'issuedCount'}; --- a/C4/Reserves.pm +++ a/C4/Reserves.pm @@ -1282,17 +1282,15 @@ sub IsAvailableForItemLevelRequest { if ( $on_shelf_holds == 1 ) { return 1; - } elsif ( $on_shelf_holds == 2 ) { + } elsif ( $on_shelf_holds == 2 ) { # On shelf holds == "If all unavailable" # if we have this param predefined from outer caller sub, we just need # to return it, so we saving from having loop inside other loop: - return $items_any_available ? 0 : 1 - if defined $items_any_available; + $items_any_available //= ItemsAnyAvailableForHold( { biblionumber => $item->biblionumber, patron => $patron }); - my $any_available = ItemsAnyAvailableForHold( { biblionumber => $item->biblionumber, patron => $patron }); - return $any_available ? 0 : 1; + return !$items_any_available; } 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->notforloan < 0 || $item->onloan || IsItemOnHoldAndFound( $item->itemnumber ); } } @@ -1322,7 +1320,7 @@ sub ItemsAnyAvailableForHold { $any_available = 1 unless $i->itemlost - || $i->notforloan > 0 + || $i->notforloan != 0 || $i->withdrawn || $i->onloan || IsItemOnHoldAndFound( $i->id ) --- a/opac/opac-ISBDdetail.pl +++ a/opac/opac-ISBDdetail.pl @@ -182,7 +182,7 @@ while ( my $item = $items->next ) { unless $allow_onshelf_holds; } -if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) { +if( $allow_onshelf_holds || CountItemsIssued({ biblionumber => $biblionumber, count_on_order => 1 }) || $biblio->has_items_waiting_or_intransit ) { $template->param( ReservableItems => 1 ); } --- a/opac/opac-MARCdetail.pl +++ a/opac/opac-MARCdetail.pl @@ -143,7 +143,7 @@ for my $itm (@all_items) { last if $allow_onshelf_holds; } -if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) { +if ( $allow_onshelf_holds || CountItemsIssued({ biblionumber => $biblionumber, count_on_order => 1 }) || $biblio->has_items_waiting_or_intransit ) { $template->param( ReservableItems => 1 ); } --- a/opac/opac-detail.pl +++ a/opac/opac-detail.pl @@ -736,7 +736,7 @@ if ( not $viewallitems and @items > $max_items_to_display ) { } } -if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) { +if ( $allow_onshelf_holds || CountItemsIssued({ biblionumber => $biblionumber, count_on_order => 1 }) || $biblio->has_items_waiting_or_intransit ) { $template->param( ReservableItems => 1 ); } --- a/opac/opac-search.pl +++ a/opac/opac-search.pl @@ -712,7 +712,7 @@ for (my $i=0;$i<@servers;$i++) { if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) { if ( my $bibnum = $res->{biblionumber} ) { - $res->{itemsissued} = CountItemsIssued( $bibnum ); + $res->{itemsissued} = CountItemsIssued( { biblionumber => $bibnum } ); $res->{'TagLoop'} = get_tags({ biblionumber => $bibnum, approved => 1, --- a/tools/batch_delete_records.pl +++ a/tools/batch_delete_records.pl @@ -96,7 +96,7 @@ if ( $op eq 'form' ) { my $record = &GetMarcBiblio({ biblionumber => $record_id }); $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')]; $biblio->{holds_count} = $holds_count; - $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id ); + $biblio->{issues_count} = C4::Biblio::CountItemsIssued({ biblionumber => $record_id, count_on_order => 1 }); push @records, $biblio; } else { # Retrieve authority information @@ -145,7 +145,7 @@ if ( $op eq 'form' ) { my $biblio = Koha::Biblios->find( $biblionumber ); # TODO Replace with $biblio->get_issues->count - if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) { + if ( C4::Biblio::CountItemsIssued({ biblionumber => $biblionumber, count_on_order => 1 }) ) { push @messages, { type => 'warning', code => 'item_issued', --