From fcf777198ae47a93e22930cca410673a7db57040 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 10 Mar 2023 16:14:50 +0000 Subject: [PATCH] Bug 33167: Add a filter rleationship for branchtransfer Prefetching is useful, but we don't want to fetch all the inactive transfers for an item. This patch adds a filtered relationship and updates get_transfer to use this --- Koha/Biblio.pm | 7 +++---- Koha/Item.pm | 4 +++- Koha/Schema/Result/Item.pm | 14 ++++++++++++++ catalogue/detail.pl | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 0e06a14fa0..481be204df 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -435,11 +435,10 @@ sub items { return Koha::Items->_new_from_dbic( $items_rs ) unless $params->{host_items}; + my @itemnumbers = $items_rs->get_column('itemnumber')->all; my $host_itemnumbers = $self->_host_itemnumbers(); - my $params = { -or => [biblionumber => $self->id] }; - push @{$params->{'-or'}}, itemnumber => { -in => $host_itemnumbers } if $host_itemnumbers; - - return Koha::Items->search($params); + push @itemnumbers, @{ $host_itemnumbers }; + return Koha::Items->search({ "me.itemnumber" => { -in => \@itemnumbers } }); } =head3 host_items diff --git a/Koha/Item.pm b/Koha/Item.pm index 89d1a948c4..e6c47c2474 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -566,7 +566,9 @@ we still expect the item to end up at a final location eventually. sub get_transfer { my ($self) = @_; - return $self->get_transfers->search( {}, { rows => 1 } )->next; + my $transfer = $self->_result->branchtransfer; + + return Koha::Item::Transfers->_new_from_dbic($transfer)->next; } =head3 get_transfers diff --git a/Koha/Schema/Result/Item.pm b/Koha/Schema/Result/Item.pm index 55d33e4a19..893172a367 100644 --- a/Koha/Schema/Result/Item.pm +++ b/Koha/Schema/Result/Item.pm @@ -968,6 +968,20 @@ __PACKAGE__->many_to_many( "ordernumber", ); +__PACKAGE__->has_many( + "branchtransfer", + "Koha::Schema::Result::Branchtransfer", + sub { + my $args = shift; + return { + "$args->{foreign_alias}.itemnumber" => { -ident => "$args->{self_alias}.itemnumber" }, + "$args->{foreign_alias}.datearrived" => undef, + "$args->{foreign_alias}.datecancelled" => undef, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); + use C4::Context; sub effective_itemtype { my ( $self ) = @_; diff --git a/catalogue/detail.pl b/catalogue/detail.pl index f20ebf850e..01e1bdf06d 100755 --- a/catalogue/detail.pl +++ b/catalogue/detail.pl @@ -193,7 +193,7 @@ my $itemtypes = { map { $_->itemtype => $_ } @{ Koha::ItemTypes->search_with_loc my $params; my $patron = Koha::Patrons->find( $borrowernumber ); $params->{ itemlost } = 0 if $patron->category->hidelostitems && !$showallitems; -my $items = $biblio->items({ host_items => 1 })->search_ordered( $params, { prefetch => ['issue','branchtransfers'] } ); +my $items = $biblio->items({ host_items => 1 })->search_ordered( $params, { prefetch => ['issue','branchtransfer'] } ); # flag indicating existence of at least one item linked via a host record my $hostrecords = $biblio->host_items->count; -- 2.30.2