From aa2fb7e3f280251a75c456ebce34618f4be077f9 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 --- Koha/Patron.pm | 24 +++++++++++++++ t/db_dependent/Koha/Patrons.t | 70 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 9a2fd18..ded37a7 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -449,6 +449,30 @@ 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) = @_; + # FIXME add date_due < now + 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 6b85dcc..cd19954 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,9 +19,12 @@ use Modern::Perl; -use Test::More tests => 11; +use Test::More tests => 12; use Test::Warn; +use DateTime; +use C4::Biblio; +use C4::Circulation; use C4::Members; use Koha::Holds; @@ -337,6 +340,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.1.4