From 8c099ab9f7591a1a5e1ca72ec24424ba4a5a9673 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 28 Feb 2023 16:13:56 +0100 Subject: [PATCH] Bug 32894: Koha::Item->last_returned_by Signed-off-by: Martin Renvoize Signed-off-by: Nick Clemens (cherry picked from commit 3545292513453b2383c406e96c3d51a3a2f5e6c5) Signed-off-by: Martin Renvoize (cherry picked from commit 143c0e59c821bd08f1014bc5c61f320f28a520b3) Signed-off-by: Matt Blenkinsop --- C4/Circulation.pm | 2 +- Koha/Item.pm | 28 +++++----------------------- Koha/Schema/Result/Item.pm | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 27cf592241..09b1a7cb79 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2562,7 +2562,7 @@ sub MarkIssueReturned { if ( C4::Context->preference('StoreLastBorrower') ) { my $item = Koha::Items->find( $itemnumber ); - $item->last_returned_by( $patron ); + $item->last_returned_by( $patron->borrowernumber )->store; } # Remove any OVERDUES related debarment if the borrower has no overdues diff --git a/Koha/Item.pm b/Koha/Item.pm index 6d5a75a235..46b34c2477 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -599,35 +599,17 @@ sub get_transfers { =head3 last_returned_by -Gets and sets the last borrower to return an item. - -Accepts and returns Koha::Patron objects - -$item->last_returned_by( $borrowernumber ); +Gets the last borrower to return an item. $last_returned_by = $item->last_returned_by(); =cut sub last_returned_by { - my ( $self, $borrower ) = @_; - - my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower'); - - if ($borrower) { - return $items_last_returned_by_rs->update_or_create( - { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } ); - } - else { - unless ( $self->{_last_returned_by} ) { - my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } ); - if ($result) { - $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') ); - } - } - - return $self->{_last_returned_by}; - } + my ( $self ) = @_; + my $rs = $self->_result->last_returned_by; + return unless $rs; + return Koha::Patron->_new_from_dbic($rs); } =head3 can_article_request diff --git a/Koha/Schema/Result/Item.pm b/Koha/Schema/Result/Item.pm index 55d33e4a19..9bc8bf7fbb 100644 --- a/Koha/Schema/Result/Item.pm +++ b/Koha/Schema/Result/Item.pm @@ -968,6 +968,23 @@ __PACKAGE__->many_to_many( "ordernumber", ); +__PACKAGE__->has_many( + "current_branchtransfers", + "Koha::Schema::Result::Branchtransfer", + { 'foreign.itemnumber' => 'self.itemnumber' }, + { + where => { datearrived => undef, datecancelled => undef }, + order_by => [ { -desc => 'datesent' }, { -asc => 'daterequested' } ] + } +); + +__PACKAGE__->might_have( + "last_returned_by", + "Koha::Schema::Result::ItemsLastBorrower", + { "foreign.itemnumber" => "self.itemnumber" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + use C4::Context; sub effective_itemtype { my ( $self ) = @_; -- 2.39.5