From 1fad2d3a39ccb1d489acd4fbd4456feaa4d627c9 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 relationship for branchtransfers 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 dd66dc7039..7d29a21360 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -449,11 +449,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 a0a18e5fb1..e1aae7f29f 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 3c2bb5c88d..d6111ff961 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