From 98d0c990bf33bbe8ce76126181f64f078087fa45 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 8 Jan 2018 15:49:03 -0300 Subject: [PATCH] Bug 19935: Add Koha::Patron->pending_checkouts To move this subroutine out of the C4 namespace we face the same problematic as bug 17553 (with GetOverduesForPatron). We need to provide an equivalent method and so return all the related value for a given checkout. We can acchieve the easily using Koha::Object->unblessed_all_relateds, but we need to keep in mind that it is a temporary move. Indeed we will want to use our API to only access/retrive values we really need. The whole trick could be removed when the current syntax for notices will be deprecated and removed. Note: this method returns the same number of elements than ->checkouts They indeed returns the same things, but it sounds better to me to have a different method to highlight (from the callers) where does it come from (C4::Members::GetPendingIssues). Signed-off-by: Benjamin Rokseth Signed-off-by: Josef Moravec Signed-off-by: Jonathan Druart --- Koha/Patron.pm | 31 +++++++++++++++++++++++++++++++ t/db_dependent/Koha/Patrons.t | 13 +++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 0938bbb5f0..2322131713 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -523,6 +523,37 @@ sub checkouts { return Koha::Checkouts->_new_from_dbic( $checkouts ); } +=head3 pending_checkouts + +my $pending_checkouts = $patron->pending_checkouts + +This method will return the same as $self->checkouts, but with a prefetch on +items, biblio and biblioitems. + +It has been introduced to replaced the C4::Members::GetPendingIssues subroutine + +It should not be used directly, prefer to access fields you need instead of +retrieving all these fields in one go. + + +=cut + +sub pending_checkouts { + my( $self ) = @_; + my $checkouts = $self->_result->issues->search( + {}, + { + order_by => [ + { -desc => 'me.timestamp' }, + { -desc => 'issuedate' }, + { -desc => 'issue_id' }, # Sort by issue_id should be enough + ], + prefetch => { item => { biblio => 'biblioitems' } }, + } + ); + return Koha::Checkouts->_new_from_dbic( $checkouts ); +} + =head3 old_checkouts my $old_checkouts = $patron->old_checkouts diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index db39defa57..f0b1992054 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -429,8 +429,8 @@ subtest 'add_enrolment_fee_if_needed' => sub { $patron->delete; }; -subtest 'checkouts + get_overdues + old_checkouts' => sub { - plan tests => 12; +subtest 'checkouts + pending_checkouts + get_overdues + old_checkouts' => sub { + plan tests => 17; my $library = $builder->build( { source => 'Branch' } ); my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' ); @@ -482,6 +482,9 @@ subtest 'checkouts + get_overdues + old_checkouts' => sub { my $checkouts = $patron->checkouts; is( $checkouts->count, 0, 'checkouts should not return any issues for that patron' ); is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' ); + my $pending_checkouts = $patron->pending_checkouts; + is( $pending_checkouts->count, 0, 'pending_checkouts should not return any issues for that patron' ); + is( ref($pending_checkouts), 'Koha::Checkouts', 'pending_checkouts should return a Koha::Checkouts object' ); my $old_checkouts = $patron->old_checkouts; is( $old_checkouts->count, 0, 'old_checkouts should not return any issues for that patron' ); is( ref($old_checkouts), 'Koha::Old::Checkouts', 'old_checkouts should return a Koha::Old::Checkouts object' ); @@ -500,6 +503,12 @@ subtest 'checkouts + get_overdues + old_checkouts' => sub { $checkouts = $patron->checkouts; is( $checkouts->count, 3, 'checkouts should return 3 issues for that patron' ); is( ref($checkouts), 'Koha::Checkouts', 'checkouts should return a Koha::Checkouts object' ); + $pending_checkouts = $patron->pending_checkouts; + is( $pending_checkouts->count, 3, 'pending_checkouts should return 3 issues for that patron' ); + is( ref($pending_checkouts), 'Koha::Checkouts', 'pending_checkouts should return a Koha::Checkouts object' ); + + my $first_checkout = $pending_checkouts->next; + is( $first_checkout->unblessed_all_relateds->{biblionumber}, $item_3->{biblionumber}, 'pending_checkouts should prefetch values from other tables (here biblio)' ); my $overdues = $patron->get_overdues; is( $overdues->count, 2, 'Patron should have 2 overdues'); -- 2.39.5