From 6ed05c2751f2ecc725379d2c29cb5a97b37971b8 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 --- 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 d0be998..285d3f9 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 2323d21..fab2807 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -427,8 +427,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, '' ); @@ -480,6 +480,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' ); @@ -498,6 +501,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.1.4