From 46432511a600f0f22b35d004ee47eb573a33f94f Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 8 Nov 2016 12:16:49 +0000 Subject: [PATCH] Bug 17580: Add the Koha::Patron->get_overdues method This method will be used by several patches later. Test plan: prove t/db_dependent/Koha/Patrons.t should return green Signed-off-by: Josef Moravec Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall --- Koha/Patron.pm | 23 ++++++++++++ t/db_dependent/Koha/Patrons.t | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 92aa9cfaee..a40d6f0bb5 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -496,6 +496,29 @@ sub add_enrolment_fee_if_needed { return $enrolment_fee || 0; } +=head3 get_overdues + +my $overdue_items = $patron->get_overdues + +Return the overdued items + +=cut + +sub get_overdues { + my ($self) = @_; + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + my $issues = Koha::Issues->search( + { + 'me.borrowernumber' => $self->borrowernumber, + 'me.date_due' => { '<' => $dtf->format_datetime(dt_from_string) }, + }, + { + prefetch => { item => { biblio => 'biblioitems' } }, + } + ); + return $issues; +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index ddd667a950..1a9a1ea07b 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -21,7 +21,10 @@ use Modern::Perl; use Test::More tests => 14; use Test::Warn; +use DateTime; +use C4::Biblio; +use C4::Circulation; use C4::Members; use Koha::Holds; @@ -403,6 +406,71 @@ subtest 'add_enrolment_fee_if_needed' => sub { $patron->delete; }; +subtest 'get_overdues' => sub { + plan tests => 4; + + my $library = $builder->build( { source => 'Branch' } ); + my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' ); + my $item_1 = $builder->build( + { + source => 'Item', + value => { + homebranch => $library->{branchcode}, + holdingbranch => $library->{branchcode}, + biblionumber => $biblionumber_1 + } + } + ); + my $item_2 = $builder->build( + { + source => 'Item', + value => { + homebranch => $library->{branchcode}, + holdingbranch => $library->{branchcode}, + biblionumber => $biblionumber_1 + } + } + ); + my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' ); + my $item_3 = $builder->build( + { + source => 'Item', + value => { + homebranch => $library->{branchcode}, + holdingbranch => $library->{branchcode}, + biblionumber => $biblionumber_2 + } + } + ); + my $patron = $builder->build( + { + source => 'Borrower', + value => { branchcode => $library->{branchcode} } + } + ); + + # Not sure how this is useful, but AddIssue pass this variable to different other subroutines + $patron = GetMember( borrowernumber => $patron->{borrowernumber} ); + + my $module = new Test::MockModule('C4::Context'); + $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } ); + + AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) ); + AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) ); + AddIssue( $patron, $item_3->{barcode} ); + + $patron = Koha::Patrons->find( $patron->{borrowernumber} ); + my $overdues = $patron->get_overdues; + is( $overdues->count, 2, 'Patron should have 2 overdues'); + is( ref($overdues), 'Koha::Issues', 'Koha::Patron->get_overdues should return Koha::Issues' ); + is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' ); + is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' ); + + # Clean stuffs + Koha::Issues->search( { borrowernumber => $patron->borrowernumber } )->delete; + $patron->delete; +}; + $retrieved_patron_1->delete; is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' ); -- 2.39.5