From 40f917944320c8dc6ab6beafc3fca724ea58cfa3 Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Mon, 22 Sep 2025 21:41:54 +0000 Subject: [PATCH] Bug 26993: Implement changes Test plan: 1. APPLY patch, restart_all, updatedatabase 2. Search for the StoreLastBorrower system preference, it should now allow you to enter a number of borrowers to store 3. Set the pref to 3. 4. Check an item out to any patron. Then immediately check it back in. 5. On catalogue/moredetail.pl?biblionumber=X ( where X is you biblionumber ) you should see a "Last returned by:" entry with the correct cardnumber for the borrower. 7. Confirm this is right in the database with: SELECT * FROM items_last_borrower where itemnumber = X; ( Where X is your itemnumber ) 8. Repeat step 4 to a different borrower. 9. Repeat steps 5 and 6. You should see 2 cardnumbers of each of the borrowers on catalogue/moredetail.pl?biblionumber=X 10. Repeat step 4 with another new borrower. 11. Repeat steps 5 and 6. You should see 3 cardnumbers of each of the borrowers on catalogue/moredetail.pl?biblionumber=X 12. Choose a different item and repeat steps 4 - 11 for this item. 13. Now set the StoreLastBorrower preference to 0. 14. Using the original item from step 4, check something out to a patron and immediately check it back in. 15. catalogue/moredetail.pl?biblionumber=X should NOT have an entry for "Last returned by:" 16. Check the database, all entries for the itemnumber from step 14 should be deleted. Signed-off-by: Trevor Diamond --- C4/Circulation.pm | 6 +- Koha/Item.pm | 60 ++++++++++++++++++- .../prog/en/modules/catalogue/moredetail.tt | 19 ++++-- 3 files changed, 73 insertions(+), 12 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 5aa42a4d25f..a14b0fdb9ed 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2918,10 +2918,8 @@ sub MarkIssueReturned { } ); - if ( C4::Context->preference('StoreLastBorrower') ) { - my $item = Koha::Items->find($itemnumber); - $item->last_returned_by( $patron->borrowernumber )->store; - } + my $item = Koha::Items->find($itemnumber); + $item->last_returned_by( $patron->borrowernumber )->store; # Possibly remove any OVERDUES related debarment my $overdue_restrictions = $patron->restrictions->search( { type => 'OVERDUES' } ); diff --git a/Koha/Item.pm b/Koha/Item.pm index d55099e14cf..a85aa60abd0 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -927,6 +927,28 @@ sub get_transfers { return Koha::Item::Transfers->_new_from_dbic($transfers_rs); } +=head3 last_returned_by_all + +Returns all patrons who have returned this item, ordered by most recent first. + +=cut + +sub last_returned_by_all { + my ($self) = @_; + + my $max_stored_borrowers = C4::Context->preference('StoreLastBorrower') || 0; + + my @borrowers = $self->_result->last_returned_by( + {}, + { + order_by => [ { '-desc' => 'created_on' }, { '-desc' => 'id' } ], + ( $max_stored_borrowers > 0 ? ( rows => $max_stored_borrowers ) : () ), + } + ); + + return map { Koha::Patron->_new_from_dbic( $_->borrowernumber ) } @borrowers; +} + =head3 last_returned_by Gets and sets the last patron to return an item. @@ -941,13 +963,47 @@ my $patron = $item->last_returned_by(); sub last_returned_by { my ( $self, $borrowernumber ) = @_; + if ($borrowernumber) { - $self->_result->update_or_create_related( + + # Clean up the table by deleting older entries, depending on what StoreLastBorrower is set to + my $max_stored_borrowers = C4::Context->preference('StoreLastBorrower') || 0; + + # If StoreLastBorrower is 0 or disabled, bail without storing anything. Also delete any remaining rows from the table. + if ( $max_stored_borrowers == 0 ) { + $self->_result->last_returned_by->delete_all; + return $self; + } + + #Create an entry for last_returned_by + my $new_record = $self->_result->create_related( 'last_returned_by', { borrowernumber => $borrowernumber, itemnumber => $self->itemnumber } ); + + # Get all entries, ordered by newest first + my @all_entries = + $self->_result->last_returned_by( {}, { order_by => [ { '-desc' => 'created_on' }, { '-desc' => 'id' } ] } ) + ->all; + + # If we have more than the limit, delete the excess oldest ones + if ( scalar(@all_entries) > $max_stored_borrowers ) { + my @entries_to_delete = splice( @all_entries, $max_stored_borrowers ); + $_->delete for @entries_to_delete; + } + + return $self; } - my $rs = $self->_result->last_returned_by; + + # Return the most recent borrower + my $rs = $self->_result->last_returned_by( + {}, + { + order_by => [ { '-desc' => 'created_on' }, { '-desc' => 'id' } ], + rows => 1 + } + )->first; + return unless $rs; return Koha::Patron->_new_from_dbic( $rs->borrowernumber ); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/moredetail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/moredetail.tt index 93faa13bec9..655f0244a8b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/moredetail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/moredetail.tt @@ -493,13 +493,20 @@ Last borrowed: [% IF (ITEM_DAT.datelastborrowed ) %][% ITEM_DAT.datelastborrowed | $KohaDates %][% END %] - [% IF Koha.Preference('StoreLastBorrower') && ITEM_DAT.object.last_returned_by %] - [% SET last_returned_by = ITEM_DAT.object.last_returned_by.cardnumber || ITEM_DAT.object.last_returned_by.borrowernumber %] -
  • - Last returned by: - [% last_returned_by | html %] -
  • + + [% IF Koha.Preference('StoreLastBorrower') && Koha.Preference('StoreLastBorrower') > 0 %] + [% SET all_borrowers = ITEM_DAT.object.last_returned_by_all %] + [% IF all_borrowers.size > 0 %] +
  • + Last returned by: + [% FOREACH patron IN all_borrowers %] + [% SET returned_by = patron.cardnumber || patron.borrowernumber %] + [% returned_by | html %][% UNLESS loop.last %],[% END %] + [% END %] +
  • + [% END %] [% END %] + [% FOR i IN ITEM_DAT.old_issues %] [% SET b = i.patron %]
  • -- 2.39.5